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