From baa6d9730c7a7955e426a97cb922c31ce359dd11 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 Nov 2013 19:23:35 +0800 Subject: [PATCH] win: Enable setting window icon in creation options. --- browser/native_window.cc | 29 +++++++++++++++++++++++++++++ browser/native_window.h | 5 +++++ browser/native_window_win.cc | 24 +++++++++++------------- browser/native_window_win.h | 3 ++- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index de3910851aa1..db671f9a77a6 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -6,6 +6,7 @@ #include +#include "base/file_util.h" #include "base/message_loop.h" #include "base/utf_string_conversions.h" #include "base/values.h" @@ -31,6 +32,7 @@ #include "ui/gfx/point.h" #include "ui/gfx/rect.h" #include "ui/gfx/size.h" +#include "webkit/glue/image_decoder.h" using content::NavigationEntry; @@ -46,6 +48,12 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, brightray::InspectableWebContents::Create(web_contents)) { options->GetBoolean(switches::kFrame, &has_frame_); + std::string icon; + if (options->GetString(switches::kIcon, &icon)) { + if (!SetIcon(icon)) + LOG(ERROR) << "Failed to set icon to " << icon; + } + web_contents->SetDelegate(this); WindowList::AddWindow(this); @@ -169,6 +177,27 @@ void NativeWindow::RestartHangMonitorTimeout() { GetWebContents()->GetRenderViewHost()->RestartHangMonitorTimeout(); } +bool NativeWindow::SetIcon(const std::string& str_path) { + base::FilePath path = base::FilePath::FromUTF8Unsafe(str_path); + + // Read the file from disk. + std::string file_contents; + if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) + return false; + + // Decode the bitmap using WebKit's image decoder. + const unsigned char* data = + reinterpret_cast(file_contents.data()); + webkit_glue::ImageDecoder decoder; + scoped_ptr decoded(new SkBitmap()); + *decoded = decoder.Decode(data, file_contents.length()); + if (decoded->empty()) + return false; // Unable to decode. + + icon_ = gfx::Image::CreateFrom1xBitmap(*decoded.release()); + return true; +} + void NativeWindow::CloseWebContents() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, diff --git a/browser/native_window.h b/browser/native_window.h index 761f429429be..e10c0f7d88df 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -13,6 +13,7 @@ #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/web_contents_observer.h" +#include "ui/gfx/image/image.h" #include "vendor/brightray/browser/default_web_contents_delegate.h" namespace base { @@ -105,6 +106,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void BlurWebView(); virtual bool IsWebViewFocused(); virtual void RestartHangMonitorTimeout(); + virtual bool SetIcon(const std::string& path); // The same with closing a tab in a real browser. // @@ -176,6 +178,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // Whether window has standard frame. bool has_frame_; + // Window icon. + gfx::Image icon_; + private: void RendererUnresponsiveDelayed(); diff --git a/browser/native_window_win.cc b/browser/native_window_win.cc index 87afabcab883..f03d3365a1e8 100644 --- a/browser/native_window_win.cc +++ b/browser/native_window_win.cc @@ -4,7 +4,6 @@ #include "browser/native_window_win.h" -#include "app/win/resource.h" #include "base/stl_util.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" @@ -33,8 +32,6 @@ namespace { const int kResizeInsideBoundsSize = 5; const int kResizeAreaCornerSize = 16; -HANDLE g_exe_icon = NULL; - // Wrapper of NativeWidgetWin to handle WM_MENUCOMMAND messages, which are // triggered by window menus. class MenuCommandNativeWidget : public views::NativeWidgetWin { @@ -220,16 +217,10 @@ NativeWindowWin::NativeWindowWin(content::WebContents* web_contents, gfx::Size size(width, height); window_->CenterWindow(size); + window_->UpdateWindowIcon(); + web_view_->SetWebContents(web_contents); OnViewWasResized(); - - if (g_exe_icon == NULL) - g_exe_icon = ::LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(1), - IMAGE_ICON, 0, 0, 0); - ::SendMessage(window_->GetNativeWindow(), - WM_SETICON, - static_cast(ICON_BIG), - reinterpret_cast(g_exe_icon)); } NativeWindowWin::~NativeWindowWin() { @@ -474,8 +465,15 @@ bool NativeWindowWin::ShouldHandleSystemCommands() const { return true; } -bool NativeWindowWin::ShouldShowWindowIcon() const { - return true; +gfx::ImageSkia NativeWindowWin::GetWindowAppIcon() { + if (icon_.IsEmpty()) + return gfx::ImageSkia(); + else + return *icon_.ToImageSkia(); +} + +gfx::ImageSkia NativeWindowWin::GetWindowIcon() { + return GetWindowAppIcon(); } views::Widget* NativeWindowWin::GetWidget() { diff --git a/browser/native_window_win.h b/browser/native_window_win.h index f1c6b9428173..9d9ed0df61f1 100644 --- a/browser/native_window_win.h +++ b/browser/native_window_win.h @@ -98,7 +98,8 @@ class NativeWindowWin : public NativeWindow, virtual bool CanMaximize() const OVERRIDE; virtual string16 GetWindowTitle() const OVERRIDE; virtual bool ShouldHandleSystemCommands() const OVERRIDE; - virtual bool ShouldShowWindowIcon() const OVERRIDE; + virtual gfx::ImageSkia GetWindowAppIcon() OVERRIDE; + virtual gfx::ImageSkia GetWindowIcon() OVERRIDE; virtual views::Widget* GetWidget() OVERRIDE; virtual const views::Widget* GetWidget() const OVERRIDE; virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE;