Chromium Code Reviews| 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" | |
| 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 Loading... | |
| 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| and return it via |callback| as a | |
| 117 // gfx::Image. The size in pixels of the returned icon can be chosen with | |
| 118 // |icon_size|. The icon can also be converted to grayscale if |grayscale| is | |
| 119 // set to true. | |
| 120 // | |
| 121 // |extension| is set to NULL if the default icon is requested. | |
| 122 // |icon_size| is set to -1 if no resize is requested. | |
|
Finnur
2013/10/02 15:19:27
Suggest minor reordering:
Load the icon of the gi
| |
| 123 void LoadExtensionIconAsync(const extensions::Extension* extension, | |
| 124 int icon_size, | |
| 125 bool grayscale, | |
| 126 const base::Callback<void(const gfx::Image&)>& | |
| 127 callback); | |
| 128 | |
| 129 // Same as LoadExtensionIconAsync() above except the result icon is returned | |
| 130 // as a PNG image data URL. | |
| 131 void LoadExtensionIconDataURLAsync(const extensions::Extension* extension, | |
| 132 int icon_size, | |
| 133 bool grayscale, | |
| 134 const base::Callback<void(const GURL&)>& | |
| 135 callback); | |
| 100 | 136 |
| 101 private: | 137 private: |
| 102 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; | 138 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; |
| 103 | 139 |
| 140 Profile* profile_; | |
| 141 | |
| 142 // Task tracker when getting favicon. | |
| 143 CancelableTaskTracker cancelable_task_tracker_; | |
| 144 | |
| 145 // Cache for the default app icon. | |
| 146 scoped_ptr<SkBitmap> default_app_icon_; | |
| 147 | |
| 148 // Cache for the default extension icon. | |
| 149 scoped_ptr<SkBitmap> default_extension_icon_; | |
| 150 | |
| 104 static void LoadImagesOnBlockingPool( | 151 static void LoadImagesOnBlockingPool( |
| 105 const std::vector<ImageRepresentation>& info_list, | 152 const std::vector<ImageRepresentation>& info_list, |
| 106 const std::vector<SkBitmap>& bitmaps, | 153 const std::vector<SkBitmap>& bitmaps, |
| 107 std::vector<LoadResult>* load_result); | 154 std::vector<LoadResult>* load_result); |
| 108 | 155 |
| 109 void ReplyBack( | 156 void ReplyBack( |
| 110 const std::vector<LoadResult>* load_result, | 157 const std::vector<LoadResult>* load_result, |
| 111 const base::Callback<void(const gfx::Image&)>& callback); | 158 const base::Callback<void(const gfx::Image&)>& callback); |
| 159 | |
| 160 // The sequence for LoadExtensionIconAsync() is the following: | |
| 161 // 1) It loads the icon image using LoadImageAsync(). | |
| 162 // 2) When it finishes, LoadExtensionIconLoaded() will be called. | |
| 163 // 3) On success, it will call FinalizeImage(). If it failed, it will call | |
| 164 // LoadIconFailed(). See below for more on those methods. | |
| 165 void LoadExtensionIconDone(const extensions::Extension* extension, | |
| 166 int icon_size, | |
| 167 bool grayscale, | |
| 168 const base::Callback<void(const gfx::Image&)>& | |
| 169 callback, | |
| 170 const gfx::Image& image); | |
| 171 | |
| 172 // Called when the extension doesn't have an icon. We fall back to multiple | |
| 173 // sources, using the following order: | |
| 174 // 1) The icons as listed in the extension manifests. | |
| 175 // 2) If a 16px icon and the extension has a launch URL, see if Chrome has a | |
| 176 // corresponding favicon. | |
| 177 // 3) If still no matches, load the default extension icon. | |
| 178 void LoadIconFailed(const extensions::Extension* extension, | |
| 179 int icon_size, | |
| 180 bool grayscale, | |
| 181 const base::Callback<void(const gfx::Image&)>& callback); | |
| 182 | |
| 183 // Loads the favicon image for the given |extension|. If the image does not | |
| 184 // exist, we fall back to the default image. | |
| 185 void LoadFaviconImage(const extensions::Extension* extension, | |
| 186 int icon_size, | |
| 187 bool grayscale, | |
| 188 const base::Callback<void(const gfx::Image&)>& | |
| 189 callback); | |
| 190 | |
| 191 // FaviconService callback. It will call FinalizedImage() on success or try | |
| 192 // another fallback. | |
| 193 void OnFaviconDataAvailable( | |
| 194 const extensions::Extension* extension, | |
| 195 int icon_size, | |
| 196 bool grayscale, | |
| 197 const base::Callback<void(const gfx::Image&)>& callback, | |
| 198 const chrome::FaviconBitmapResult& bitmap_result); | |
| 199 | |
| 200 // The sequence for LoadDefaultImage() will be the following: | |
| 201 // 1) LoadDefaultImage() will invoke LoadDefaultImageOnFileThread() on the | |
| 202 // file thread. | |
| 203 // 2) LoadDefaultImageOnFileThread() will perform the work, then invoke | |
| 204 // LoadDefaultImageDone() on the UI thread. | |
| 205 void LoadDefaultImage(const extensions::Extension* extension, | |
| 206 int icon_size, | |
| 207 bool grayscale, | |
| 208 const base::Callback<void(const gfx::Image&)>& | |
| 209 callback); | |
| 210 | |
| 211 // Loads the default image on the file thread. | |
| 212 void LoadDefaultImageOnFileThread( | |
| 213 const extensions::Extension* extension, | |
| 214 int icon_size, | |
| 215 bool grayscale, | |
| 216 const base::Callback<void(const gfx::Image&)>& callback); | |
| 217 | |
| 218 // When loading of default image is done, it will call FinalizeImage(). | |
| 219 void LoadDefaultImageDone(const gfx::Image& image, | |
| 220 bool grayscale, | |
| 221 const base::Callback<void(const gfx::Image&)>& | |
| 222 callback); | |
| 223 | |
| 224 // Performs any remaining transformations (like desaturating the |image|), | |
| 225 // then returns the |image| to the |callback|. | |
| 226 // | |
| 227 // The sequence for FinalizeImage() will be the following: | |
| 228 // 1) FinalizeImage() will invoke FinalizeImageOnFileThread() on the file | |
| 229 // thread. | |
| 230 // 2) FinalizeImageOnFileThread() will perform the work, then invoke | |
| 231 // FinalizeImageDone() on the UI thread. | |
| 232 void FinalizeImage(const gfx::Image& image, | |
| 233 bool grayscale, | |
| 234 const base::Callback<void(const gfx::Image&)>& callback); | |
| 235 | |
| 236 // Process the "finalize" operation on the file thread. | |
| 237 void FinalizeImageOnFileThread(const gfx::Image& image, | |
| 238 bool grayscale, | |
| 239 const base::Callback<void(const gfx::Image&)>& | |
| 240 callback); | |
| 241 | |
| 242 // Called when the "finalize" operation on the file thread is done. | |
| 243 void FinalizeImageDone(const gfx::Image& image, | |
| 244 const base::Callback<void(const gfx::Image&)> & | |
| 245 callback); | |
| 246 | |
| 247 // The sequence for LoadExtensionIconDataURLAsync() will be the following: | |
| 248 // 1) Call LoadExtensionIconAsync() to fetch the icon of the extension. | |
| 249 // 2) When the icon is loaded, OnIconAvailable() will be called and will | |
| 250 // invoke ConvertIconToURLOnFileThread() on the file thread. | |
| 251 // 3) OnIconConvertedToURL() will be called on the UI thread when it's done | |
| 252 // and will call the callback. | |
| 253 // | |
| 254 // LoadExtensionIconDataURLAsync() will use LoadExtensionIconAsync() to get | |
| 255 // the icon of the extension. The following method will be called when the | |
| 256 // image has been fetched. | |
| 257 void OnIconAvailable(const base::Callback<void(const GURL&)>& callback, | |
| 258 const gfx::Image& image); | |
| 259 | |
| 260 // ConvertIconToURLOnFileThread() will convert the image to a PNG image data | |
| 261 // URL on the file thread. | |
| 262 void ConvertIconToURLOnFileThread(const gfx::Image& image, | |
| 263 const base::Callback<void(const GURL&)>& | |
| 264 callback); | |
| 265 | |
| 266 // This method will call the callback of LoadExtensionIconDataURLAsync() with | |
| 267 // the result. | |
| 268 void OnIconConvertedToURL(const GURL& url, | |
| 269 const base::Callback<void(const GURL&)>& callback); | |
| 270 | |
| 271 // Returns the bitmap for the default extension icon. | |
| 272 const SkBitmap* GetDefaultExtensionImage(); | |
| 273 | |
| 274 // Returns the bitmap for the default app icon. | |
| 275 const SkBitmap* GetDefaultAppImage(); | |
| 112 }; | 276 }; |
| 113 | 277 |
| 114 } // namespace extensions | 278 } // namespace extensions |
| 115 | 279 |
| 116 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ | 280 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ |
| OLD | NEW |