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_LOADING_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <set> |
| 10 #include <vector> | |
| 11 | 10 |
| 12 #include "base/compiler_specific.h" | 11 #include "base/callback_forward.h" |
| 13 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "chrome/common/extensions/extension_icon_set.h" | |
| 16 #include "chrome/common/extensions/extension_resource.h" | 13 #include "chrome/common/extensions/extension_resource.h" |
| 17 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | |
| 19 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
| 20 #include "ui/gfx/image/image_skia.h" | |
| 21 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
| 22 | 19 |
| 23 class SkBitmap; | 20 template <typename T> struct DefaultSingletonTraits; |
| 24 | 21 |
| 25 namespace extensions { | 22 FORWARD_DECLARE_TEST(ImageLoaderTest, Cache); |
| 26 class Extension; | 23 FORWARD_DECLARE_TEST(ImageLoaderTest, DeleteExtensionWhileWaitingForCache); |
| 27 } | 24 FORWARD_DECLARE_TEST(ImageLoaderTest, MultipleImages); |
| 28 | 25 |
| 29 namespace gfx { | 26 namespace gfx { |
| 30 class Image; | 27 class Image; |
| 31 } | 28 } |
| 32 | 29 |
| 33 // The views need to load their icons asynchronously but might be deleted before | 30 namespace extensions { |
| 34 // the images have loaded. This class encapsulates a loader class that stays | 31 |
| 35 // alive while the request is in progress (manages its own lifetime) and keeps | 32 class Extension; |
| 36 // track of whether the view still cares about the icon loading. | 33 |
| 34 // This class is responsible for asynchronously loading extension images and | |
| 35 // calling a callback when an image is loaded. | |
| 36 // The views need to load their icons asynchronously might be deleted before | |
| 37 // the images have loaded. If you pass your callback using a weak_ptr, this | |
| 38 // will make sure the callback won't be called after the view is deleted. | |
| 37 // | 39 // |
| 38 // To use this class, have your class derive from ImageLoadingTracker::Observer, | 40 // NOTE: if the image is available already, the callback is called immediately |
| 39 // and add a member variable ImageLoadingTracker tracker_. Then override | 41 // from the call to LoadImageAsync. In other words, by the time LoadImageAsync |
| 40 // Observer::OnImageLoaded and call: | 42 // returns the callback has been notified. |
| 41 // tracker_.LoadImage(extension, resource, max_size, false); | |
| 42 // ... and wait for OnImageLoaded to be called back on you with a pointer to the | |
| 43 // ImageSkia loaded. | |
| 44 // NOTE: if the image is available already (or the resource is not valid), the | |
| 45 // Observer is notified immediately from the call to LoadImage. In other words, | |
| 46 // by the time LoadImage returns the observer has been notified. | |
| 47 // | 43 // |
| 48 class ImageLoadingTracker : public content::NotificationObserver { | 44 class ImageLoader : public content::NotificationObserver { |
| 49 public: | 45 public: |
| 50 enum CacheParam { | |
| 51 CACHE, | |
| 52 DONT_CACHE | |
| 53 }; | |
| 54 | |
| 55 class Observer { | |
| 56 public: | |
| 57 // Will be called when the image with the given index has loaded. | |
| 58 // |image| can be empty if a valid image was not found or it failed to | |
| 59 // decode. |extension_id| is the ID of the extension the images are loaded | |
| 60 // from. |index| represents the index of the image just loaded (starts at 0 | |
| 61 // and increments every time LoadImage is called). | |
| 62 virtual void OnImageLoaded(const gfx::Image& image, | |
| 63 const std::string& extension_id, | |
| 64 int index) = 0; | |
| 65 | |
| 66 protected: | |
| 67 virtual ~Observer(); | |
| 68 }; | |
| 69 | |
| 70 // Information about a singe image representation to load from an extension | 46 // Information about a singe image representation to load from an extension |
| 71 // resource. | 47 // resource. |
| 72 struct ImageRepresentation { | 48 struct ImageRepresentation { |
| 73 // Enum values to indicate whether to resize loaded bitmap when it is larger | 49 // Enum values to indicate whether to resize loaded bitmap when it is larger |
| 74 // than |desired_size| or always resize it. | 50 // than |desired_size| or always resize it. |
| 75 enum ResizeCondition { | 51 enum ResizeCondition { |
| 76 RESIZE_WHEN_LARGER, | 52 RESIZE_WHEN_LARGER, |
| 77 ALWAYS_RESIZE, | 53 ALWAYS_RESIZE, |
| 78 }; | 54 }; |
| 79 | 55 |
| 80 ImageRepresentation(const ExtensionResource& resource, | 56 ImageRepresentation(const ExtensionResource& resource, |
| 81 ResizeCondition resize_method, | 57 ResizeCondition resize_condition, |
| 82 const gfx::Size& desired_size, | 58 const gfx::Size& desired_size, |
| 83 ui::ScaleFactor scale_factor); | 59 ui::ScaleFactor scale_factor); |
| 84 ~ImageRepresentation(); | 60 ~ImageRepresentation(); |
| 85 | 61 |
| 86 // Extension resource to load. | 62 // Extension resource to load. |
| 87 ExtensionResource resource; | 63 ExtensionResource resource; |
| 88 | 64 |
| 89 ResizeCondition resize_method; | 65 ResizeCondition resize_condition; |
| 90 | 66 |
| 91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger | 67 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger |
| 92 // than |desired_size| it will be resized to these dimensions. | 68 // than |desired_size| it will be resized to these dimensions. |
| 93 gfx::Size desired_size; | 69 gfx::Size desired_size; |
| 94 | 70 |
| 95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. | 71 // |scale_factor| is used to construct the loaded gfx::ImageSkia. |
| 96 ui::ScaleFactor scale_factor; | 72 ui::ScaleFactor scale_factor; |
| 97 }; | 73 }; |
| 98 | 74 |
| 99 explicit ImageLoadingTracker(Observer* observer); | 75 static ImageLoader* GetInstance(); |
| 100 virtual ~ImageLoadingTracker(); | |
| 101 | |
| 102 // Specify image resource to load. If the loaded image is larger than | |
| 103 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this | |
| 104 // function may call back your observer synchronously (ie before it returns) | |
| 105 // if the image was found in the cache. | |
| 106 // Note this method loads a raw bitmap from the resource. All sizes given are | |
| 107 // assumed to be in pixels. | |
| 108 void LoadImage(const extensions::Extension* extension, | |
| 109 const ExtensionResource& resource, | |
| 110 const gfx::Size& max_size, | |
| 111 CacheParam cache); | |
| 112 | |
| 113 // Same as LoadImage() above except it loads multiple images from the same | |
| 114 // extension. This is used to load multiple resolutions of the same image | |
| 115 // type. | |
| 116 void LoadImages(const extensions::Extension* extension, | |
| 117 const std::vector<ImageRepresentation>& info_list, | |
| 118 CacheParam cache); | |
| 119 | |
| 120 // Returns the ID used for the next image that is loaded. That is, the return | |
| 121 // value from this method corresponds to the int that is passed to | |
| 122 // OnImageLoaded() the next time LoadImage() is invoked. | |
| 123 int next_id() const { return next_id_; } | |
| 124 | 76 |
| 125 // Checks whether image is a component extension resource. Returns false | 77 // Checks whether image is a component extension resource. Returns false |
| 126 // 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 |
| 127 // resources. Otherwise fills |resource_id|. | 79 // resources. Otherwise fills |resource_id|. |
| 128 static bool IsComponentExtensionResource( | 80 static bool IsComponentExtensionResource( |
| 129 const extensions::Extension* extension, | 81 const extensions::Extension* extension, |
| 130 const FilePath& resource_path, | 82 const FilePath& resource_path, |
| 131 int* resource_id); | 83 int* resource_id); |
| 132 | 84 |
| 85 // Specify image resource to load. If the loaded image is larger than | |
| 86 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this | |
| 87 // function may call back your callback synchronously (ie before it returns) | |
| 88 // if the image was found in the cache. | |
| 89 // Note this method loads a raw bitmap from the resource. All sizes given are | |
| 90 // assumed to be in pixels. | |
| 91 void LoadImageAsync(const extensions::Extension* extension, | |
| 92 const ExtensionResource& resource, | |
| 93 const gfx::Size& max_size, | |
| 94 const base::Callback<void(const gfx::Image&)>& callback); | |
| 95 | |
| 96 // Same as LoadImage() above except it loads multiple images from the same | |
| 97 // extension. This is used to load multiple resolutions of the same image | |
| 98 // type. | |
| 99 void LoadImagesAsync(const extensions::Extension* extension, | |
| 100 const std::vector<ImageRepresentation>& info_list, | |
| 101 const base::Callback<void(const gfx::Image&)>& callback); | |
| 102 | |
| 103 void SetCachedImage(const ExtensionResource& source, | |
| 104 const SkBitmap& image, | |
| 105 const gfx::Size& original_size) const; | |
| 106 | |
| 133 private: | 107 private: |
| 134 // Information for pending resource load operation for one or more image | 108 friend struct DefaultSingletonTraits<ImageLoader>; |
| 135 // representations. | 109 |
| 110 // We keep a cache of images loaded from extension resources based on their | |
| 111 // path and a string representation of a size that may have been used to | |
| 112 // scale it (or the empty string if the image is at its original size). | |
| 113 typedef std::pair<FilePath, std::string> ImageCacheKey; | |
| 114 typedef std::map<ImageCacheKey, SkBitmap> ImageCache; | |
| 115 | |
| 136 struct PendingLoadInfo { | 116 struct PendingLoadInfo { |
| 137 PendingLoadInfo(); | 117 PendingLoadInfo(); |
| 138 ~PendingLoadInfo(); | 118 ~PendingLoadInfo(); |
| 139 | 119 |
| 140 const extensions::Extension* extension; | |
| 141 // This is cached separate from |extension| in case the extension is | |
| 142 // unloaded. | |
| 143 std::string extension_id; | 120 std::string extension_id; |
| 144 CacheParam cache; | 121 bool save_to_cache; |
| 145 size_t pending_count; | |
| 146 gfx::ImageSkia image_skia; | |
| 147 }; | 122 }; |
| 148 | 123 |
| 149 // Maps an integer identifying a load request to a PendingLoadInfo. | 124 typedef std::set<PendingLoadInfo*> LoadSet; |
| 150 typedef std::map<int, PendingLoadInfo> LoadMap; | |
| 151 | 125 |
| 152 class ImageLoader; | 126 struct LoadResult { |
| 127 LoadResult(const SkBitmap& bitmap, | |
| 128 const gfx::Size& original_size, | |
| 129 const ImageRepresentation& image_representation); | |
| 130 ~LoadResult(); | |
| 153 | 131 |
| 154 // Called on the calling thread when the bitmap finishes loading. | 132 SkBitmap bitmap; |
| 155 // |bitmap| may be null if the image file failed to decode. | 133 gfx::Size original_size; |
| 156 void OnBitmapLoaded(const SkBitmap* bitmap, | 134 ImageRepresentation image_representation; |
| 157 const ImageRepresentation& image_info, | 135 }; |
| 158 const gfx::Size& original_size, | 136 |
| 159 int id, | 137 ImageLoader(); |
| 160 bool should_cache); | 138 virtual ~ImageLoader(); |
| 139 | |
| 140 void LoadImagesOnBlockingPool( | |
| 141 const std::vector<ImageRepresentation>& info_list, | |
| 142 const std::vector<SkBitmap*>& bitmaps, | |
| 143 PendingLoadInfo* load_info, | |
| 144 const base::Callback<void(const gfx::Image&)>& callback); | |
| 145 | |
| 146 void ReplyBack( | |
| 147 const std::vector<LoadResult>& load_result, | |
| 148 PendingLoadInfo* load_info, | |
| 149 const base::Callback<void(const gfx::Image&)>& callback); | |
| 161 | 150 |
| 162 // content::NotificationObserver method. If an extension is uninstalled while | 151 // content::NotificationObserver method. If an extension is uninstalled while |
| 163 // we're waiting for the image we remove the entry from load_map_. | 152 // we're waiting for the image we make sure it won't be cached. |
| 164 virtual void Observe(int type, | 153 virtual void Observe(int type, |
| 165 const content::NotificationSource& source, | 154 const content::NotificationSource& source, |
| 166 const content::NotificationDetails& details) OVERRIDE; | 155 const content::NotificationDetails& details) OVERRIDE; |
| 167 | 156 |
| 168 // The view that is waiting for the image to load. | 157 void SetCachedImageImpl(const std::string& extension_id, |
| 169 Observer* observer_; | 158 const ImageCacheKey& key, |
| 159 const SkBitmap& image); | |
| 170 | 160 |
| 171 // ID to use for next image requested. This is an ever increasing integer. | 161 const SkBitmap* GetCachedImage(const ExtensionResource& source, |
| 172 int next_id_; | 162 const gfx::Size& max_size) const; |
| 173 | 163 |
| 174 // The object responsible for loading the image on the File thread. | 164 bool ShouldCacheImage(const ImageRepresentation& image_info) const; |
| 175 scoped_refptr<ImageLoader> loader_; | 165 void SetImageSizesToCache(const std::set<int> image_sizes_); |
| 176 | 166 |
| 177 // Information for each LoadImage request is cached here. The integer | 167 // Cached images for extensions. This should only be touched on the UI thread. |
| 178 // identifies the id assigned to the request. | 168 std::map<std::string, ImageCache> image_cache_; |
| 179 LoadMap load_map_; | 169 |
| 170 // | |
|
xiyuan
2012/10/10 20:30:22
nit: delete it or finish it? :)
Marijn Kruisselbrink
2012/10/10 22:33:03
Done.
| |
| 171 LoadSet pending_; | |
| 172 | |
| 173 // Sizes of images to cache | |
| 174 std::set<int> image_sizes_to_cache_; | |
| 180 | 175 |
| 181 content::NotificationRegistrar registrar_; | 176 content::NotificationRegistrar registrar_; |
| 182 | 177 |
| 183 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 178 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, Cache); |
| 179 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, | |
| 180 DeleteExtensionWhileWaitingForCache); | |
| 181 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, MultipleImages); | |
| 184 }; | 182 }; |
| 185 | 183 |
| 186 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 184 } // namespace extensions |
| 185 | |
| 186 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ | |
| OLD | NEW |