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

Side by Side Diff: chrome/browser/extensions/image_loader.h

Issue 25050005: Refactored loading of applications / extensions icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests. Created 7 years, 2 months 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
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 CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
7 7
8 #include <set> 8 #include <set>
9 9
10 #include "base/callback_forward.h" 10 #include "base/callback_forward.h"
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "chrome/common/cancelable_task_tracker.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" 14 #include "components/browser_context_keyed_service/browser_context_keyed_service .h"
14 #include "extensions/common/extension_resource.h" 15 #include "extensions/common/extension_resource.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/base/layout.h" 17 #include "ui/base/layout.h"
17 #include "ui/gfx/size.h" 18 #include "ui/gfx/size.h"
18 19
20 class GURL;
19 class Profile; 21 class Profile;
20 22
23 namespace base {
24 class RefCountedMemory;
25 }
26
27 namespace chrome {
28 struct FaviconBitmapResult;
29 }
30
21 namespace gfx { 31 namespace gfx {
22 class Image; 32 class Image;
23 } 33 }
24 34
25 namespace extensions { 35 namespace extensions {
26 36
27 class Extension; 37 class Extension;
28 38
29 // This class is responsible for asynchronously loading extension images and 39 // This class is responsible for asynchronously loading extension images and
30 // calling a callback when an image is loaded. 40 // calling a callback when an image is loaded.
(...skipping 30 matching lines...) Expand all
61 // |scale_factor| is used to construct the loaded gfx::ImageSkia. 71 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
62 ui::ScaleFactor scale_factor; 72 ui::ScaleFactor scale_factor;
63 }; 73 };
64 74
65 struct LoadResult; 75 struct LoadResult;
66 76
67 // Returns the instance for the given profile, or NULL if none. This is 77 // Returns the instance for the given profile, or NULL if none. This is
68 // a convenience wrapper around ImageLoaderFactory::GetForProfile. 78 // a convenience wrapper around ImageLoaderFactory::GetForProfile.
69 static ImageLoader* Get(Profile* profile); 79 static ImageLoader* Get(Profile* profile);
70 80
71 ImageLoader();
72 virtual ~ImageLoader();
73
74 // Checks whether image is a component extension resource. Returns false 81 // Checks whether image is a component extension resource. Returns false
75 // if a given |resource| does not have a corresponding image in bundled 82 // if a given |resource| does not have a corresponding image in bundled
76 // resources. Otherwise fills |resource_id|. This doesn't check if the 83 // resources. Otherwise fills |resource_id|. This doesn't check if the
77 // extension the resource is in is actually a component extension. 84 // extension the resource is in is actually a component extension.
78 static bool IsComponentExtensionResource( 85 static bool IsComponentExtensionResource(
79 const base::FilePath& extension_path, 86 const base::FilePath& extension_path,
80 const base::FilePath& resource_path, 87 const base::FilePath& resource_path,
81 int* resource_id); 88 int* resource_id);
82 89
90 // Converts a bitmap image to a PNG representation.
91 static scoped_refptr<base::RefCountedMemory> BitmapToMemory(
92 const SkBitmap* image);
93
94 explicit ImageLoader(Profile* profile);
95 virtual ~ImageLoader();
96
83 // Specify image resource to load. If the loaded image is larger than 97 // Specify image resource to load. If the loaded image is larger than
84 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this 98 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
85 // function may call back your callback synchronously (ie before it returns) 99 // function may call back your callback synchronously (ie before it returns)
86 // if the image was found in the cache. 100 // if the image was found in the cache.
87 // Note this method loads a raw bitmap from the resource. All sizes given are 101 // Note this method loads a raw bitmap from the resource. All sizes given are
88 // assumed to be in pixels. 102 // assumed to be in pixels.
89 void LoadImageAsync(const extensions::Extension* extension, 103 void LoadImageAsync(const extensions::Extension* extension,
90 const ExtensionResource& resource, 104 const ExtensionResource& resource,
91 const gfx::Size& max_size, 105 const gfx::Size& max_size,
92 const base::Callback<void(const gfx::Image&)>& callback); 106 const base::Callback<void(const gfx::Image&)>& callback);
93 107
94 // Same as LoadImage() above except it loads multiple images from the same 108 // Same as LoadImage() above except it loads multiple images from the same
95 // extension. This is used to load multiple resolutions of the same image 109 // extension. This is used to load multiple resolutions of the same image
96 // type. 110 // type.
97 void LoadImagesAsync(const extensions::Extension* extension, 111 void LoadImagesAsync(const extensions::Extension* extension,
98 const std::vector<ImageRepresentation>& info_list, 112 const std::vector<ImageRepresentation>& info_list,
99 const base::Callback<void(const gfx::Image&)>& callback); 113 const base::Callback<void(const gfx::Image&)>&
114 callback);
115
116 // Load the icon of the given |extension|. The size in pixels of the returned
Finnur 2013/10/02 09:52:15 nit: Add to the first sentence: and return it via
dvh-g 2013/10/02 15:08:13 Done.
117 // icon can be chosen with |icon_size|. The icon can also be converted to
Finnur 2013/10/02 09:52:15 Can be? Looks to me like it must be chosen. Unless
dvh-g 2013/10/02 15:08:13 Done.
118 // grayscale if |grayscale| is set to true. This function will call the
119 // callback with the resulting icon as parameter.
120 void LoadExtensionIconAsync(const extensions::Extension* extension,
121 int icon_size,
122 bool grayscale,
123 const base::Callback<void(const gfx::Image&)>&
124 callback);
125
126 // Same as LoadExtensionIconAsync() above except the result icon is returned
127 // as a PNG image data URL.
128 void LoadExtensionIconDataURLAsync(const extensions::Extension* extension,
129 int icon_size,
130 bool grayscale,
131 const base::Callback<void(const GURL&)>&
132 callback);
100 133
101 private: 134 private:
102 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; 135 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_;
103 136
137 Profile* profile_;
138
139 // Task tracker when getting favicon.
140 CancelableTaskTracker cancelable_task_tracker_;
141
142 // Cache for the default app icon.
143 scoped_ptr<SkBitmap> default_app_icon_;
144
145 // Cache for the default extension icon.
146 scoped_ptr<SkBitmap> default_extension_icon_;
147
104 static void LoadImagesOnBlockingPool( 148 static void LoadImagesOnBlockingPool(
105 const std::vector<ImageRepresentation>& info_list, 149 const std::vector<ImageRepresentation>& info_list,
106 const std::vector<SkBitmap>& bitmaps, 150 const std::vector<SkBitmap>& bitmaps,
107 std::vector<LoadResult>* load_result); 151 std::vector<LoadResult>* load_result);
108 152
109 void ReplyBack( 153 void ReplyBack(
110 const std::vector<LoadResult>* load_result, 154 const std::vector<LoadResult>* load_result,
111 const base::Callback<void(const gfx::Image&)>& callback); 155 const base::Callback<void(const gfx::Image&)>& callback);
156
157 // The sequence for LoadExtensionIconAsync() is the following:
158 // 1) It loads the icon image using LoadImageAsync().
159 // 2) When it finishes, LoadExtensionIconLoaded() will be called.
160 // 3) On success, it will call FinalizeImage(). If it failed, it will call
161 // LoadIconFailed(). See below for more on those methods.
162 void LoadExtensionIconDone(const extensions::Extension* extension,
163 int icon_size,
164 bool grayscale,
165 const base::Callback<void(const gfx::Image&)>&
166 callback,
167 const gfx::Image& image);
168
169 // Called when the extension doesn't have an icon. We fall back to multiple
170 // sources, using the following order:
171 // 1) The icons as listed in the extension manifests.
172 // 2) If a 16px icon and the extension has a launch URL, see if Chrome has a
173 // corresponding favicon.
174 // 3) If still no matches, load the default extension icon.
175 void LoadIconFailed(const extensions::Extension* extension,
176 int icon_size,
177 bool grayscale,
178 const base::Callback<void(const gfx::Image&)>& callback);
179
180 // Loads the favicon image for the given |extension|. If the image does not
181 // exist, we fall back to the default image.
182 void LoadFaviconImage(const extensions::Extension* extension,
183 int icon_size,
184 bool grayscale,
185 const base::Callback<void(const gfx::Image&)>&
186 callback);
187
188 // FaviconService callback. It will call FinalizedImage() on success or try
189 // another fallback.
190 void OnFaviconDataAvailable(
191 const extensions::Extension* extension,
192 int icon_size,
193 bool grayscale,
194 const base::Callback<void(const gfx::Image&)>& callback,
195 const chrome::FaviconBitmapResult& bitmap_result);
196
197 // The sequence for LoadDefaultImage() will be the following:
198 // 1) LoadDefaultImage() will invoke LoadDefaultImageOnFileThread() on the
199 // file thread.
200 // 2) LoadDefaultImageOnFileThread() will perform the work, then invoke
201 // LoadDefaultImageDone() on the UI thread.
202 void LoadDefaultImage(const extensions::Extension* extension,
203 int icon_size,
204 bool grayscale,
205 const base::Callback<void(const gfx::Image&)>&
206 callback);
207
208 // Loads the default image on the file thread.
209 void LoadDefaultImageOnFileThread(
210 const extensions::Extension* extension,
211 int icon_size,
212 bool grayscale,
213 const base::Callback<void(const gfx::Image&)>& callback);
214
215 // When loading of default image is done, it will call FinalizeImage().
216 void LoadDefaultImageDone(const gfx::Image& image,
217 bool grayscale,
218 const base::Callback<void(const gfx::Image&)>&
219 callback);
220
221 // Performs any remaining transformations (like desaturating the |image|),
222 // then returns the |image| to the |callback|.
223 //
224 // The sequence for FinalizeImage() will be the following:
225 // 1) FinalizeImage() will invoke FinalizeImageOnFileThread() on the file
226 // thread.
227 // 2) FinalizeImageOnFileThread() will perform the work, then invoke
228 // FinalizeImageDone() on the UI thread.
229 void FinalizeImage(const gfx::Image& image,
230 bool grayscale,
231 const base::Callback<void(const gfx::Image&)>& callback);
232
233 // Process the "finalize" operation on the file thread.
234 void FinalizeImageOnFileThread(const gfx::Image& image,
235 bool grayscale,
236 const base::Callback<void(const gfx::Image&)>&
237 callback);
238
239 // Called when the "finalize" operation on the file thread is done.
240 void FinalizeImageDone(const gfx::Image& image,
241 const base::Callback<void(const gfx::Image&)> &
242 callback);
243
244 // The sequence for LoadExtensionIconDataURLAsync() will be the following:
245 // 1) Call LoadExtensionIconAsync() to fetch the icon of the extension.
246 // 2) When the icon is loaded, OnIconAvailable() will be called and will
247 // invoke ConvertIconToURLOnFileThread() on the file thread.
248 // 3) OnIconConvertedToURL() will be called on the UI thread when it's done
249 // and will call the callback.
250 //
251 // LoadExtensionIconDataURLAsync() will use LoadExtensionIconAsync() to get
252 // the icon of the extension. The following method will be called when the
253 // image has been fetched.
254 void OnIconAvailable(const base::Callback<void(const GURL&)>& callback,
255 const gfx::Image& image);
256
257 // ConvertIconToURLOnFileThread() will convert the image to a PNG image data
258 // URL on the file thread.
259 void ConvertIconToURLOnFileThread(const gfx::Image& image,
260 const base::Callback<void(const GURL&)>&
261 callback);
262
263 // This method will call the callback of LoadExtensionIconDataURLAsync() with
264 // the result.
265 void OnIconConvertedToURL(const GURL& url,
266 const base::Callback<void(const GURL&)>& callback);
267
268 // Returns the bitmap for the default extension icon.
269 const SkBitmap* GetDefaultExtensionImage();
270
271 // Returns the bitmap for the default app icon.
272 const SkBitmap* GetDefaultAppImage();
112 }; 273 };
113 274
114 } // namespace extensions 275 } // namespace extensions
115 276
116 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ 277 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698