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