Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Unified Diff: chrome/browser/ui/views/frame/glass_browser_frame_view.cc

Issue 1406403007: Eliminate HICON leaks caused by creating icons from bitmap image. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use ScopedHICON instead of HICON. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/views/frame/glass_browser_frame_view.cc
diff --git a/chrome/browser/ui/views/frame/glass_browser_frame_view.cc b/chrome/browser/ui/views/frame/glass_browser_frame_view.cc
index 7168d7c672257d63b1507c009f496039fd1d72ba..1607794c770fa8059074300dd49e937b2db15b00 100644
--- a/chrome/browser/ui/views/frame/glass_browser_frame_view.cc
+++ b/chrome/browser/ui/views/frame/glass_browser_frame_view.cc
@@ -64,13 +64,17 @@ const int kNewTabCaptionMaximizedSpacing = 16;
// Converts the |image| to a Windows icon and returns the corresponding HICON
// handle. |image| is resized to desired |width| and |height| if needed.
-HICON CreateHICONFromSkBitmapSizedTo(const gfx::ImageSkia& image,
- int width,
- int height) {
- if (width == image.width() && height == image.height())
- return IconUtil::CreateHICONFromSkBitmap(*image.bitmap());
- return IconUtil::CreateHICONFromSkBitmap(skia::ImageOperations::Resize(
- *image.bitmap(), skia::ImageOperations::RESIZE_BEST, width, height));
+base::win::ScopedHICON CreateHICONFromSkBitmapSizedTo(
+ const gfx::ImageSkia& image,
+ int width,
+ int height) {
+ return IconUtil::CreateHICONFromSkBitmap(
+ width == image.width() && height == image.height()
+ ? *image.bitmap()
+ : skia::ImageOperations::Resize(*image.bitmap(),
+ skia::ImageOperations::RESIZE_BEST,
+ width, height))
+ .Pass();
grt (UTC plus 2) 2015/11/10 16:44:43 can this be removed?
}
} // namespace
@@ -574,6 +578,8 @@ void GlassBrowserFrameView::StopThrobber() {
if (throbber_running_) {
throbber_running_ = false;
+ base::win::ScopedHICON previous_small_icon;
+ base::win::ScopedHICON previous_big_icon;
HICON small_icon = nullptr;
HICON big_icon = nullptr;
@@ -581,10 +587,22 @@ void GlassBrowserFrameView::StopThrobber() {
if (browser_view()->ShouldShowWindowIcon()) {
gfx::ImageSkia icon = browser_view()->GetWindowIcon();
if (!icon.isNull()) {
- small_icon = CreateHICONFromSkBitmapSizedTo(
- icon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
- big_icon = CreateHICONFromSkBitmapSizedTo(
- icon, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
+ // Keep previous icons alive as long as they are referenced by the HWND.
+ previous_small_icon = small_window_icon_.Pass();
+ previous_big_icon = big_window_icon_.Pass();
+
+ // Take responsibility for eventually destroying the created icons.
+ small_window_icon_ =
+ CreateHICONFromSkBitmapSizedTo(icon, GetSystemMetrics(SM_CXSMICON),
+ GetSystemMetrics(SM_CYSMICON))
+ .Pass();
+ big_window_icon_ =
+ CreateHICONFromSkBitmapSizedTo(icon, GetSystemMetrics(SM_CXICON),
+ GetSystemMetrics(SM_CYICON))
+ .Pass();
+
+ small_icon = small_window_icon_.get();
+ big_icon = big_window_icon_.get();
}
}
@@ -601,21 +619,13 @@ void GlassBrowserFrameView::StopThrobber() {
// This will reset the icon which we set in the throbber code.
// WM_SETICON with null icon restores the icon for title bar but not
// for taskbar. See http://crbug.com/29996
- HICON previous_small_icon = reinterpret_cast<HICON>(
- SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
- static_cast<WPARAM>(ICON_SMALL),
- reinterpret_cast<LPARAM>(small_icon)));
-
- HICON previous_big_icon = reinterpret_cast<HICON>(
- SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
- static_cast<WPARAM>(ICON_BIG),
- reinterpret_cast<LPARAM>(big_icon)));
-
- if (previous_small_icon)
- ::DestroyIcon(previous_small_icon);
+ SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
+ static_cast<WPARAM>(ICON_SMALL),
+ reinterpret_cast<LPARAM>(small_icon));
- if (previous_big_icon)
- ::DestroyIcon(previous_big_icon);
+ SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
+ static_cast<WPARAM>(ICON_BIG),
+ reinterpret_cast<LPARAM>(big_icon));
}
}

Powered by Google App Engine
This is Rietveld 408576698