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

Side by Side Diff: ui/gfx/icon_util.h

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: Rebase Created 5 years 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 unified diff | Download patch
« no previous file with comments | « remoting/host/disconnect_window_win.cc ('k') | ui/gfx/icon_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef UI_GFX_ICON_UTIL_H_ 5 #ifndef UI_GFX_ICON_UTIL_H_
6 #define UI_GFX_ICON_UTIL_H_ 6 #define UI_GFX_ICON_UTIL_H_
7 7
8 #include <windows.h> 8 #include <windows.h>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/win/scoped_gdi_object.h"
14 #include "ui/gfx/geometry/point.h" 15 #include "ui/gfx/geometry/point.h"
15 #include "ui/gfx/geometry/size.h" 16 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/gfx_export.h" 17 #include "ui/gfx/gfx_export.h"
17 18
18 namespace base { 19 namespace base {
19 class FilePath; 20 class FilePath;
20 } 21 }
21 22
22 namespace gfx { 23 namespace gfx {
23 class ImageFamily; 24 class ImageFamily;
(...skipping 11 matching lines...) Expand all
35 // 36 //
36 // SkBitmap bitmap; 37 // SkBitmap bitmap;
37 // 38 //
38 // // Fill |bitmap| with valid data 39 // // Fill |bitmap| with valid data
39 // bitmap.setConfig(...); 40 // bitmap.setConfig(...);
40 // bitmap.allocPixels(); 41 // bitmap.allocPixels();
41 // 42 //
42 // ... 43 // ...
43 // 44 //
44 // // Convert the bitmap into a Windows HICON 45 // // Convert the bitmap into a Windows HICON
45 // HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap); 46 // base::win::ScopedHICON icon(IconUtil::CreateHICONFromSkBitmap(bitmap));
46 // if (icon == NULL) { 47 // if (!icon.is_valid()) {
47 // // Handle error 48 // // Handle error
48 // ... 49 // ...
49 // } 50 // }
50 // 51 //
51 // // Use the icon with a WM_SETICON message 52 // // Use the icon with a WM_SETICON message
52 // ::SendMessage(hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG), 53 // ::SendMessage(hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG),
53 // reinterpret_cast<LPARAM>(icon)); 54 // reinterpret_cast<LPARAM>(icon.get()));
54 //
55 // // Destroy the icon when we are done
56 // ::DestroyIcon(icon);
57 // 55 //
58 /////////////////////////////////////////////////////////////////////////////// 56 ///////////////////////////////////////////////////////////////////////////////
59 class GFX_EXPORT IconUtil { 57 class GFX_EXPORT IconUtil {
60 public: 58 public:
61 // ATOMIC_WRITE ensures that a partially written icon won't be created even if 59 // ATOMIC_WRITE ensures that a partially written icon won't be created even if
62 // Chrome crashes part way through, but ATOMIC_WRITE is more expensive than 60 // Chrome crashes part way through, but ATOMIC_WRITE is more expensive than
63 // NORMAL_WRITE. See CreateIconFileFromImageFamily. ATOMIC_WRITE is the 61 // NORMAL_WRITE. See CreateIconFileFromImageFamily. ATOMIC_WRITE is the
64 // default for historical reasons. 62 // default for historical reasons.
65 enum WriteType { ATOMIC_WRITE, NORMAL_WRITE }; 63 enum WriteType { ATOMIC_WRITE, NORMAL_WRITE };
66 // The size of the large icon entries in .ico files on Windows Vista+. 64 // The size of the large icon entries in .ico files on Windows Vista+.
(...skipping 12 matching lines...) Expand all
79 static const size_t kNumIconDimensions; 77 static const size_t kNumIconDimensions;
80 // The number of elements in kIconDimensions <= kMediumIconSize. 78 // The number of elements in kIconDimensions <= kMediumIconSize.
81 static const size_t kNumIconDimensionsUpToMediumSize; 79 static const size_t kNumIconDimensionsUpToMediumSize;
82 80
83 // Given an SkBitmap object, the function converts the bitmap to a Windows 81 // Given an SkBitmap object, the function converts the bitmap to a Windows
84 // icon and returns the corresponding HICON handle. If the function cannot 82 // icon and returns the corresponding HICON handle. If the function cannot
85 // convert the bitmap, NULL is returned. 83 // convert the bitmap, NULL is returned.
86 // 84 //
87 // The client is responsible for destroying the icon when it is no longer 85 // The client is responsible for destroying the icon when it is no longer
88 // needed by calling ::DestroyIcon(). 86 // needed by calling ::DestroyIcon().
89 static HICON CreateHICONFromSkBitmap(const SkBitmap& bitmap); 87 static base::win::ScopedHICON CreateHICONFromSkBitmap(const SkBitmap& bitmap);
90 88
91 // Given a valid HICON handle representing an icon, this function converts 89 // Given a valid HICON handle representing an icon, this function converts
92 // the icon into an SkBitmap object containing an ARGB bitmap using the 90 // the icon into an SkBitmap object containing an ARGB bitmap using the
93 // dimensions specified in |s|. |s| must specify valid dimensions (both 91 // dimensions specified in |s|. |s| must specify valid dimensions (both
94 // width() an height() must be greater than zero). If the function cannot 92 // width() an height() must be greater than zero). If the function cannot
95 // convert the icon to a bitmap (most probably due to an invalid parameter), 93 // convert the icon to a bitmap (most probably due to an invalid parameter),
96 // the return value is NULL. 94 // the return value is NULL.
97 // 95 //
98 // The client owns the returned bitmap object and is responsible for deleting 96 // The client owns the returned bitmap object and is responsible for deleting
99 // it when it is no longer needed. 97 // it when it is no longer needed.
(...skipping 29 matching lines...) Expand all
129 // 127 //
130 // The function returns true on success and false otherwise. Returns false if 128 // The function returns true on success and false otherwise. Returns false if
131 // |image_family| is empty. 129 // |image_family| is empty.
132 static bool CreateIconFileFromImageFamily( 130 static bool CreateIconFileFromImageFamily(
133 const gfx::ImageFamily& image_family, 131 const gfx::ImageFamily& image_family,
134 const base::FilePath& icon_path, 132 const base::FilePath& icon_path,
135 WriteType write_type = ATOMIC_WRITE); 133 WriteType write_type = ATOMIC_WRITE);
136 134
137 // Creates a cursor of the specified size from the DIB passed in. 135 // Creates a cursor of the specified size from the DIB passed in.
138 // Returns the cursor on success or NULL on failure. 136 // Returns the cursor on success or NULL on failure.
139 static HICON CreateCursorFromDIB(const gfx::Size& icon_size, 137 static base::win::ScopedHICON CreateCursorFromDIB(const gfx::Size& icon_size,
140 const gfx::Point& hotspot, 138 const gfx::Point& hotspot,
141 const void* dib_bits, 139 const void* dib_bits,
142 size_t dib_size); 140 size_t dib_size);
143 141
144 private: 142 private:
145 // The icon format is published in the MSDN but there is no definition of 143 // The icon format is published in the MSDN but there is no definition of
146 // the icon file structures in any of the Windows header files so we need to 144 // the icon file structures in any of the Windows header files so we need to
147 // define these structure within the class. We must make sure we use 2 byte 145 // define these structure within the class. We must make sure we use 2 byte
148 // packing so that the structures are laid out properly within the file. 146 // packing so that the structures are laid out properly within the file.
149 // See: http://msdn.microsoft.com/en-us/library/ms997538.aspx 147 // See: http://msdn.microsoft.com/en-us/library/ms997538.aspx
150 #pragma pack(push) 148 #pragma pack(push)
151 #pragma pack(2) 149 #pragma pack(2)
152 150
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 // A helper function of CreateSkBitmapFromHICON. 263 // A helper function of CreateSkBitmapFromHICON.
266 static SkBitmap CreateSkBitmapFromHICONHelper(HICON icon, 264 static SkBitmap CreateSkBitmapFromHICONHelper(HICON icon,
267 const gfx::Size& s); 265 const gfx::Size& s);
268 266
269 // Prevent clients from instantiating objects of that class by declaring the 267 // Prevent clients from instantiating objects of that class by declaring the
270 // ctor/dtor as private. 268 // ctor/dtor as private.
271 DISALLOW_IMPLICIT_CONSTRUCTORS(IconUtil); 269 DISALLOW_IMPLICIT_CONSTRUCTORS(IconUtil);
272 }; 270 };
273 271
274 #endif // UI_GFX_ICON_UTIL_H_ 272 #endif // UI_GFX_ICON_UTIL_H_
OLDNEW
« no previous file with comments | « remoting/host/disconnect_window_win.cc ('k') | ui/gfx/icon_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698