| 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" |
| 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" |
| 18 | 20 |
| 21 class GURL; |
| 19 class Profile; | 22 class Profile; |
| 20 | 23 |
| 24 namespace base { |
| 25 class RefCountedMemory; |
| 26 } |
| 27 |
| 28 namespace chrome { |
| 29 struct FaviconBitmapResult; |
| 30 } |
| 31 |
| 21 namespace gfx { | 32 namespace gfx { |
| 22 class Image; | 33 class Image; |
| 23 } | 34 } |
| 24 | 35 |
| 25 namespace extensions { | 36 namespace extensions { |
| 26 | 37 |
| 27 class Extension; | 38 class Extension; |
| 28 | 39 |
| 29 // This class is responsible for asynchronously loading extension images and | 40 // This class is responsible for asynchronously loading extension images and |
| 30 // calling a callback when an image is loaded. | 41 // 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. | 72 // |scale_factor| is used to construct the loaded gfx::ImageSkia. |
| 62 ui::ScaleFactor scale_factor; | 73 ui::ScaleFactor scale_factor; |
| 63 }; | 74 }; |
| 64 | 75 |
| 65 struct LoadResult; | 76 struct LoadResult; |
| 66 | 77 |
| 67 // Returns the instance for the given profile, or NULL if none. This is | 78 // Returns the instance for the given profile, or NULL if none. This is |
| 68 // a convenience wrapper around ImageLoaderFactory::GetForProfile. | 79 // a convenience wrapper around ImageLoaderFactory::GetForProfile. |
| 69 static ImageLoader* Get(Profile* profile); | 80 static ImageLoader* Get(Profile* profile); |
| 70 | 81 |
| 71 ImageLoader(); | |
| 72 virtual ~ImageLoader(); | |
| 73 | |
| 74 // Checks whether image is a component extension resource. Returns false | 82 // Checks whether image is a component extension resource. Returns false |
| 75 // if a given |resource| does not have a corresponding image in bundled | 83 // if a given |resource| does not have a corresponding image in bundled |
| 76 // resources. Otherwise fills |resource_id|. This doesn't check if the | 84 // resources. Otherwise fills |resource_id|. This doesn't check if the |
| 77 // extension the resource is in is actually a component extension. | 85 // extension the resource is in is actually a component extension. |
| 78 static bool IsComponentExtensionResource( | 86 static bool IsComponentExtensionResource( |
| 79 const base::FilePath& extension_path, | 87 const base::FilePath& extension_path, |
| 80 const base::FilePath& resource_path, | 88 const base::FilePath& resource_path, |
| 81 int* resource_id); | 89 int* resource_id); |
| 82 | 90 |
| 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 |
| 83 // Specify image resource to load. If the loaded image is larger than | 98 // 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 | 99 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this |
| 85 // function may call back your callback synchronously (ie before it returns) | 100 // function may call back your callback synchronously (ie before it returns) |
| 86 // if the image was found in the cache. | 101 // if the image was found in the cache. |
| 87 // Note this method loads a raw bitmap from the resource. All sizes given are | 102 // Note this method loads a raw bitmap from the resource. All sizes given are |
| 88 // assumed to be in pixels. | 103 // assumed to be in pixels. |
| 89 void LoadImageAsync(const extensions::Extension* extension, | 104 void LoadImageAsync(const extensions::Extension* extension, |
| 90 const ExtensionResource& resource, | 105 const ExtensionResource& resource, |
| 91 const gfx::Size& max_size, | 106 const gfx::Size& max_size, |
| 92 const base::Callback<void(const gfx::Image&)>& callback); | 107 const base::Callback<void(const gfx::Image&)>& callback); |
| 93 | 108 |
| 94 // Same as LoadImage() above except it loads multiple images from the same | 109 // 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 | 110 // extension. This is used to load multiple resolutions of the same image |
| 96 // type. | 111 // type. |
| 97 void LoadImagesAsync(const extensions::Extension* extension, | 112 void LoadImagesAsync(const extensions::Extension* extension, |
| 98 const std::vector<ImageRepresentation>& info_list, | 113 const std::vector<ImageRepresentation>& info_list, |
| 99 const base::Callback<void(const gfx::Image&)>& callback); | 114 const base::Callback<void(const gfx::Image&)>& |
| 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); |
| 100 | 139 |
| 101 private: | 140 private: |
| 102 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; | 141 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; |
| 103 | 142 |
| 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 |
| 104 static void LoadImagesOnBlockingPool( | 154 static void LoadImagesOnBlockingPool( |
| 105 const std::vector<ImageRepresentation>& info_list, | 155 const std::vector<ImageRepresentation>& info_list, |
| 106 const std::vector<SkBitmap>& bitmaps, | 156 const std::vector<SkBitmap>& bitmaps, |
| 107 std::vector<LoadResult>* load_result); | 157 std::vector<LoadResult>* load_result); |
| 108 | 158 |
| 109 void ReplyBack( | 159 void ReplyBack( |
| 110 const std::vector<LoadResult>* load_result, | 160 const std::vector<LoadResult>* load_result, |
| 111 const base::Callback<void(const gfx::Image&)>& callback); | 161 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(); |
| 112 }; | 279 }; |
| 113 | 280 |
| 114 } // namespace extensions | 281 } // namespace extensions |
| 115 | 282 |
| 116 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ | 283 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ |
| OLD | NEW |