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

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: 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"
14 #include "chrome/common/favicon/favicon_types.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" 15 #include "components/browser_context_keyed_service/browser_context_keyed_service .h"
14 #include "extensions/common/extension_resource.h" 16 #include "extensions/common/extension_resource.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/base/layout.h" 18 #include "ui/base/layout.h"
17 #include "ui/gfx/size.h" 19 #include "ui/gfx/size.h"
20 #include "url/gurl.h"
Finnur 2013/09/30 15:11:21 nit: Gurl and chrome::FaviconBitmapResult can not
dvh-g 2013/10/01 04:19:26 Done.
18 21
19 class Profile; 22 class Profile;
20 23
21 namespace gfx { 24 namespace gfx {
22 class Image; 25 class Image;
23 } 26 }
24 27
25 namespace extensions { 28 namespace extensions {
26 29
27 class Extension; 30 class Extension;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 // |scale_factor| is used to construct the loaded gfx::ImageSkia. 64 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
62 ui::ScaleFactor scale_factor; 65 ui::ScaleFactor scale_factor;
63 }; 66 };
64 67
65 struct LoadResult; 68 struct LoadResult;
66 69
67 // Returns the instance for the given profile, or NULL if none. This is 70 // Returns the instance for the given profile, or NULL if none. This is
68 // a convenience wrapper around ImageLoaderFactory::GetForProfile. 71 // a convenience wrapper around ImageLoaderFactory::GetForProfile.
69 static ImageLoader* Get(Profile* profile); 72 static ImageLoader* Get(Profile* profile);
70 73
71 ImageLoader(); 74 explicit ImageLoader(Profile* profile);
Finnur 2013/09/30 15:11:21 Shouldn't this be below the static function below
dvh-g 2013/10/01 04:19:26 Done.
72 virtual ~ImageLoader();
73 75
74 // Checks whether image is a component extension resource. Returns false 76 // Checks whether image is a component extension resource. Returns false
75 // if a given |resource| does not have a corresponding image in bundled 77 // if a given |resource| does not have a corresponding image in bundled
76 // resources. Otherwise fills |resource_id|. This doesn't check if the 78 // resources. Otherwise fills |resource_id|. This doesn't check if the
77 // extension the resource is in is actually a component extension. 79 // extension the resource is in is actually a component extension.
78 static bool IsComponentExtensionResource( 80 static bool IsComponentExtensionResource(
79 const base::FilePath& extension_path, 81 const base::FilePath& extension_path,
80 const base::FilePath& resource_path, 82 const base::FilePath& resource_path,
81 int* resource_id); 83 int* resource_id);
82 84
83 // Specify image resource to load. If the loaded image is larger than 85 // 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 86 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
85 // function may call back your callback synchronously (ie before it returns) 87 // function may call back your callback synchronously (ie before it returns)
86 // if the image was found in the cache. 88 // if the image was found in the cache.
87 // Note this method loads a raw bitmap from the resource. All sizes given are 89 // Note this method loads a raw bitmap from the resource. All sizes given are
88 // assumed to be in pixels. 90 // assumed to be in pixels.
89 void LoadImageAsync(const extensions::Extension* extension, 91 void LoadImageAsync(const extensions::Extension* extension,
90 const ExtensionResource& resource, 92 const ExtensionResource& resource,
91 const gfx::Size& max_size, 93 const gfx::Size& max_size,
92 const base::Callback<void(const gfx::Image&)>& callback); 94 const base::Callback<void(const gfx::Image&)>& callback);
93 95
94 // Same as LoadImage() above except it loads multiple images from the same 96 // 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 97 // extension. This is used to load multiple resolutions of the same image
96 // type. 98 // type.
97 void LoadImagesAsync(const extensions::Extension* extension, 99 void LoadImagesAsync(const extensions::Extension* extension,
98 const std::vector<ImageRepresentation>& info_list, 100 const std::vector<ImageRepresentation>& info_list,
99 const base::Callback<void(const gfx::Image&)>& callback); 101 const base::Callback<void(const gfx::Image&)>&
102 callback);
103
104 // Load the icon of the given extension.
105 void LoadExtensionIconAsync(const extensions::Extension* extension,
106 int icon_size,
107 bool grayscale,
108 const base::Callback<void(const gfx::Image&)>&
109 callback);
Finnur 2013/09/30 15:11:21 nit: indent callback some more? It looks like the
dvh-g 2013/10/01 04:19:26 Done.
110
111 // Load the data URL of the icon of the given extension.
Finnur 2013/09/30 15:11:21 The pre-existing API was pretty clear. You had two
dvh-g 2013/10/01 04:19:26 - One loads the icon of an extension (it's a speci
Finnur 2013/10/01 11:14:15 I don't think these need better names, necessarily
112 void LoadExtensionIconDataURLAsync(const extensions::Extension* extension,
113 int icon_size,
114 bool grayscale,
115 const base::Callback<void(const GURL&)>&
116 callback);
117
118 public:
119 virtual ~ImageLoader();
Finnur 2013/09/30 15:11:21 Umm... what's the point of moving this into a sepa
dvh-g 2013/10/01 04:19:26 I moved it at the previous location, below ImageLo
100 120
101 private: 121 private:
102 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; 122 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_;
Finnur 2013/09/30 15:11:21 nit: The indentation for this member and the next
dvh-g 2013/10/01 04:19:26 Done.
123
124 Profile* profile_;
125
126 // Task tracker when getting favicon.
127 CancelableTaskTracker cancelable_task_tracker_;
128
129 // Cache for the default app icon.
130 scoped_ptr<SkBitmap> default_app_data_;
131
132 // Cache for the extension app icon.
133 scoped_ptr<SkBitmap> default_extension_data_;
103 134
104 static void LoadImagesOnBlockingPool( 135 static void LoadImagesOnBlockingPool(
105 const std::vector<ImageRepresentation>& info_list, 136 const std::vector<ImageRepresentation>& info_list,
106 const std::vector<SkBitmap>& bitmaps, 137 const std::vector<SkBitmap>& bitmaps,
107 std::vector<LoadResult>* load_result); 138 std::vector<LoadResult>* load_result);
108 139
109 void ReplyBack( 140 void ReplyBack(
110 const std::vector<LoadResult>* load_result, 141 const std::vector<LoadResult>* load_result,
111 const base::Callback<void(const gfx::Image&)>& callback); 142 const base::Callback<void(const gfx::Image&)>& callback);
143
144 // The sequence for LoadExtensionIconAsync() is the following:
145 // 1) It loads the icon image using LoadImageAsync()
Finnur 2013/09/30 15:11:21 nit: End comments with period.
dvh-g 2013/10/01 04:19:26 Done.
146 // 2) When it finishes, LoadExtensionIconLoaded() will be called.
147 // 3) On success, it will call FinalizeImage(). If it failed, it will call
148 // LoadIconFailed(). See below for more on those methods.
149 void LoadExtensionIconLoaded(const extensions::Extension* extension,
Finnur 2013/09/30 15:11:21 Weird name: LoadFooLoaded. LoadFooDone is better,
dvh-g 2013/10/01 04:19:26 Done.
150 int icon_size,
151 bool grayscale,
152 const base::Callback<void(const gfx::Image&)>&
153 callback,
154 const gfx::Image& image);
155
156 // Called when the extension doesn't have an icon. We fall back to multiple
157 // sources, using the following order:
158 // 1) The icons as listed in the extension / app manifests.
Finnur 2013/09/30 15:11:21 Extension is a superset of apps, when it comes to
dvh-g 2013/10/01 04:19:26 Done.
159 // 2) If a 16px icon and the extension has a launch URL, see if Chrome
160 // has a corresponding favicon.
161 // 3) If still no matches, load the default extension / application icon.
162 void LoadIconFailed(const extensions::Extension* extension,
163 int icon_size,
164 bool grayscale,
165 const base::Callback<void(const gfx::Image&)>& callback);
166
167 // Loads the favicon image for the given app/extension associated. If the
Finnur 2013/09/30 15:11:21 ... for the given |extension|.
dvh-g 2013/10/01 04:19:26 Done.
Finnur 2013/10/01 11:14:15 'Associated' is redundant. On 2013/10/01 04:19:26
dvh 2013/10/02 05:15:08 Done.
168 // image does not exist, we fall back to the default image.
169 void LoadFaviconImage(const extensions::Extension* extension,
170 int icon_size,
171 bool grayscale,
172 const base::Callback<void(const gfx::Image&)>&
173 callback);
174
175 // FaviconService callback. It will call FinalizedImage() on success or try
176 // an other fallback.
Finnur 2013/09/30 15:11:21 type: s/an other/another/
dvh-g 2013/10/01 04:19:26 Done.
177 void OnFaviconDataAvailable(
178 const extensions::Extension* extension,
179 int icon_size,
180 bool grayscale,
181 const base::Callback<void(const gfx::Image&)>& callback,
182 const chrome::FaviconBitmapResult& bitmap_result);
183
184 // The sequence for LoadDefaultImage() will be the following:
185 // 1) LoadDefaultImage() will invoke LoadDefaultImageOnFileThread() on the
186 // file thread.
187 // 2) LoadDefaultImageOnFileThread() will perform the work, then invoke
188 // LoadDefaultImageDone() on the UI thread.
189 void LoadDefaultImage(const extensions::Extension* extension,
190 int icon_size,
191 bool grayscale,
192 const base::Callback<void(const gfx::Image&)>&
193 callback);
Finnur 2013/09/30 15:11:21 nit: indent? Same above and below.
dvh-g 2013/10/01 04:19:26 Done.
194
195 // Loads the default image on the file thread.
196 void LoadDefaultImageOnFileThread(
197 const extensions::Extension* extension,
198 int icon_size,
199 bool grayscale,
200 const base::Callback<void(const gfx::Image&)>& callback);
201
202 // When loading of default image is done, it will call FinalizeImage().
203 void LoadDefaultImageDone(const gfx::Image& image,
204 bool grayscale,
205 const base::Callback<void(const gfx::Image&)>&
206 callback);
207
208 // Performs any remaining transformations (like desaturating the |image|),
209 // then returns the |image| to the |callback|.
210 //
211 // The sequence for FinalizeImage() will be the following:
212 // 1) FinalizeImage() will invoke FinalizeImageOnFileThread() on the file
213 // thread.
214 // 2) FinalizeImageOnFileThread() will perform the work, then invoke
215 // FinalizeImageDone() on the UI thread.
216 void FinalizeImage(const gfx::Image& image,
217 bool grayscale,
218 const base::Callback<void(const gfx::Image&)>& callback);
219
220 // Process on the "finalize" operation on the file thread.
Finnur 2013/09/30 15:11:21 nit: 'on' is redundant.
dvh-g 2013/10/01 04:19:26 Done.
221 void FinalizeImageOnFileThread(const gfx::Image& image,
222 bool grayscale,
223 const base::Callback<void(const gfx::Image&)>&
224 callback);
225
226 // Called when the "finalize" operation on the file thread is done.
227 void FinalizeImageDone(const gfx::Image& image,
228 const base::Callback<void(const gfx::Image&)> &
229 callback);
230
231 // The sequence for LoadExtensionIconDataURLAsync() will be the following:
232 // 1) Call LoadExtensionIconAsync() to fetch the icon of the app/extension.
233 // 2) When the icon is loaded, OnIconAvailable() will be called and will
234 // invoke ConvertIconToURLOnFileThread() on the file thread.
235 // 3) OnIconConvertedToURL() will be called on the UI thread when it's done
236 // and will call the callback.
237 //
238 // LoadExtensionIconDataURLAsync() will use LoadExtensionIconAsync() to get
239 // the icon of the app/extension. The following method will be called when
240 // the image has been fetched.
241 void OnIconAvailable(const base::Callback<void(const GURL&)>& callback,
242 const gfx::Image& image);
243
244 // ConvertIconToURLOnFileThread() will convert the image to a PNG image data
245 // URL in the file thread.
Finnur 2013/09/30 15:11:21 nit: s/in/on/
dvh-g 2013/10/01 04:19:26 Done.
246 void ConvertIconToURLOnFileThread(const gfx::Image& image,
247 const base::Callback<void(const GURL&)>&
248 callback);
249
250 // This method will call the callback of LoadExtensionIconDataURLAsync() with
251 // the result.
252 void OnIconConvertedToURL(const GURL& url,
253 const base::Callback<void(const GURL&)>& callback);
254
255 // Returns the bitmap for the default extension.
256 const SkBitmap* GetDefaultExtensionImage();
257
258 // Returns the bitmap for the default app image.
259 const SkBitmap* GetDefaultAppImage();
112 }; 260 };
113 261
114 } // namespace extensions 262 } // namespace extensions
115 263
116 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ 264 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698