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_LOADING_TRACKER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | |
10 #include <vector> | |
9 | 11 |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "chrome/common/extensions/extension_icon_set.h" | |
13 #include "chrome/common/extensions/extension_resource.h" | 16 #include "chrome/common/extensions/extension_resource.h" |
14 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
15 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
19 #include "ui/base/layout.h" | |
20 #include "ui/gfx/image/image_skia.h" | |
16 #include "ui/gfx/size.h" | 21 #include "ui/gfx/size.h" |
17 | 22 |
18 class SkBitmap; | 23 class SkBitmap; |
19 | 24 |
20 namespace extensions { | 25 namespace extensions { |
21 class Extension; | 26 class Extension; |
22 } | 27 } |
23 | 28 |
24 namespace gfx { | 29 namespace gfx { |
25 class Image; | 30 class Image; |
26 } | 31 } |
27 | 32 |
28 // The views need to load their icons asynchronously but might be deleted before | 33 // The views need to load their icons asynchronously but might be deleted before |
29 // the images have loaded. This class encapsulates a loader class that stays | 34 // the images have loaded. This class encapsulates a loader class that stays |
30 // alive while the request is in progress (manages its own lifetime) and keeps | 35 // alive while the request is in progress (manages its own lifetime) and keeps |
31 // track of whether the view still cares about the icon loading. | 36 // track of whether the view still cares about the icon loading. |
32 // | 37 // |
33 // To use this class, have your class derive from ImageLoadingTracker::Observer, | 38 // To use this class, have your class derive from ImageLoadingTracker::Observer, |
34 // and add a member variable ImageLoadingTracker tracker_. Then override | 39 // and add a member variable ImageLoadingTracker tracker_. Then override |
35 // Observer::OnImageLoaded and call: | 40 // Observer::OnImageLoaded and call: |
36 // tracker_.LoadImage(extension, resource, max_size, false); | 41 // tracker_.LoadImage(extension, resource, max_size, false); |
37 // ... and wait for OnImageLoaded to be called back on you with a pointer to the | 42 // ... and wait for OnImageLoaded to be called back on you with a pointer to the |
38 // SkBitmap loaded. | 43 // ImageSkia loaded. |
39 // NOTE: if the image is available already (or the resource is not valid), the | 44 // NOTE: if the image is available already (or the resource is not valid), the |
40 // Observer is notified immediately from the call to LoadImage. In other words, | 45 // Observer is notified immediately from the call to LoadImage. In other words, |
41 // by the time LoadImage returns the observer has been notified. | 46 // by the time LoadImage returns the observer has been notified. |
42 // | 47 // |
43 class ImageLoadingTracker : public content::NotificationObserver { | 48 class ImageLoadingTracker : public content::NotificationObserver { |
44 public: | 49 public: |
45 enum CacheParam { | 50 enum CacheParam { |
46 CACHE, | 51 CACHE, |
47 DONT_CACHE | 52 DONT_CACHE |
48 }; | 53 }; |
49 | 54 |
50 class Observer { | 55 class Observer { |
51 public: | 56 public: |
52 // Will be called when the image with the given index has loaded. | 57 // Will be called when the image with the given index has loaded. |
53 // |image| can be empty if a valid image was not found or it failed to | 58 // |image| can be empty if a valid image was not found or it failed to |
54 // decode. |extension_id| is the ID of the extension the images are loaded | 59 // decode. |extension_id| is the ID of the extension the images are loaded |
55 // from. |index| represents the index of the image just loaded (starts at 0 | 60 // from. |index| represents the index of the image just loaded (starts at 0 |
56 // and increments every time LoadImage is called). | 61 // and increments every time LoadImage is called). |
57 virtual void OnImageLoaded(const gfx::Image& image, | 62 virtual void OnImageLoaded(const gfx::Image& image, |
58 const std::string& extension_id, | 63 const std::string& extension_id, |
59 int index) = 0; | 64 int index) = 0; |
60 | 65 |
61 protected: | 66 protected: |
62 virtual ~Observer(); | 67 virtual ~Observer(); |
63 }; | 68 }; |
64 | 69 |
65 // Information about a single image to load from a extension resource. | 70 // Information about a singe image representation to load from an extension |
66 struct ImageInfo { | 71 // resource. |
67 ImageInfo(const ExtensionResource& resource, gfx::Size max_size); | 72 struct ImageRepresentation { |
68 ~ImageInfo(); | 73 // Enum values to indicate whether to resize loaded bitmap when it is larger |
74 // than |desired_size| or always resize it. | |
75 enum ResizeMethod { | |
Aaron Boodman
2012/08/10 21:42:08
Nit: Better name might be ResizeCondition?
tbarzic
2012/08/10 23:01:23
Done.
| |
76 RESIZE_WHEN_LARGER, | |
77 ALWAYS_RESIZE, | |
78 }; | |
79 | |
80 ImageRepresentation(const ExtensionResource& resource, | |
81 ResizeMethod resize_method, | |
82 const gfx::Size& desired_size, | |
83 ui::ScaleFactor scale_factor); | |
84 ~ImageRepresentation(); | |
85 | |
86 // Extension resource to load. | |
69 ExtensionResource resource; | 87 ExtensionResource resource; |
70 // If the loaded image is larger than |max_size| it will be resized to those | 88 |
71 // dimensions. | 89 ResizeMethod resize_method; |
72 gfx::Size max_size; | 90 |
91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger | |
92 // than |desired_size| it will be resized to these dimensions. | |
93 gfx::Size desired_size; | |
94 | |
95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. | |
96 ui::ScaleFactor scale_factor; | |
73 }; | 97 }; |
74 | 98 |
75 explicit ImageLoadingTracker(Observer* observer); | 99 explicit ImageLoadingTracker(Observer* observer); |
76 virtual ~ImageLoadingTracker(); | 100 virtual ~ImageLoadingTracker(); |
77 | 101 |
78 // Specify image resource to load. If the loaded image is larger than | 102 // Specify image resource to load. If the loaded image is larger than |
79 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this | 103 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this |
80 // function may call back your observer synchronously (ie before it returns) | 104 // function may call back your observer synchronously (ie before it returns) |
81 // if the image was found in the cache. | 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. If you want to load a DIP-aware image, use | |
108 // |LoadImageSkia| instead. | |
Aaron Boodman
2012/08/10 21:42:08
There's no LoadImageSkia() in this class.
tbarzic
2012/08/10 23:01:23
Done.
| |
82 void LoadImage(const extensions::Extension* extension, | 109 void LoadImage(const extensions::Extension* extension, |
83 const ExtensionResource& resource, | 110 const ExtensionResource& resource, |
84 const gfx::Size& max_size, | 111 const gfx::Size& max_size, |
85 CacheParam cache); | 112 CacheParam cache); |
86 | 113 |
87 // Same as LoadImage() above except it loads multiple images from the same | 114 // Same as LoadImage() above except it loads multiple images from the same |
88 // extension. This is used to load multiple resolutions of the same image | 115 // extension. This is used to load multiple resolutions of the same image |
89 // type. | 116 // type. |
90 void LoadImages(const extensions::Extension* extension, | 117 void LoadImages(const extensions::Extension* extension, |
91 const std::vector<ImageInfo>& info_list, | 118 const std::vector<ImageRepresentation>& info_list, |
92 CacheParam cache); | 119 CacheParam cache); |
93 | 120 |
94 // Returns the ID used for the next image that is loaded. That is, the return | 121 // Returns the ID used for the next image that is loaded. That is, the return |
95 // value from this method corresponds to the int that is passed to | 122 // value from this method corresponds to the int that is passed to |
96 // OnImageLoaded() the next time LoadImage() is invoked. | 123 // OnImageLoaded() the next time LoadImage() is invoked. |
97 int next_id() const { return next_id_; } | 124 int next_id() const { return next_id_; } |
98 | 125 |
99 private: | 126 private: |
100 // Information for pending image load operation for one or more images. | 127 // Information for pending resource load operation for one or more image |
128 // representations. | |
101 struct PendingLoadInfo { | 129 struct PendingLoadInfo { |
102 PendingLoadInfo(); | 130 PendingLoadInfo(); |
103 ~PendingLoadInfo(); | 131 ~PendingLoadInfo(); |
104 | 132 |
105 const extensions::Extension* extension; | 133 const extensions::Extension* extension; |
106 // This is cached separate from |extension| in case the extension in | 134 // This is cached separate from |extension| in case the extension is |
107 // unloaded. | 135 // unloaded. |
108 std::string extension_id; | 136 std::string extension_id; |
109 CacheParam cache; | 137 CacheParam cache; |
110 size_t pending_count; | 138 size_t pending_count; |
111 std::vector<SkBitmap> bitmaps; | 139 gfx::ImageSkia image_skia; |
112 }; | 140 }; |
113 | 141 |
114 // Maps an integer identifying a load request to a PendingLoadInfo. | 142 // Maps an integer identifying a load request to a PendingLoadInfo. |
115 typedef std::map<int, PendingLoadInfo> LoadMap; | 143 typedef std::map<int, PendingLoadInfo> LoadMap; |
116 | 144 |
117 class ImageLoader; | 145 class ImageLoader; |
118 | 146 |
119 // When an image has finished loaded and been resized on the file thread, it | 147 // Called on the calling thread when the bitmap finishes loading. |
120 // is posted back to this method on the original thread. This method then | 148 // |bitmap| may be null if the image file failed to decode. |
121 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if | 149 void OnBitmapLoaded(const SkBitmap* bitmap, |
122 // it was the last image in the list. The |original_size| should be the size | 150 const ImageRepresentation& image_info, |
123 // of the image before any resizing was done. | 151 const gfx::Size& original_size, |
124 // |image| may be null if the file failed to decode. | 152 int id, |
125 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, | 153 bool should_cache); |
126 const gfx::Size& original_size, int id, bool should_cache); | |
127 | 154 |
128 // Checks whether image is a component extension resource. Returns false | 155 // Checks whether image is a component extension resource. Returns false |
129 // if a given |resource| does not have a corresponding image in bundled | 156 // if a given |resource| does not have a corresponding image in bundled |
130 // resources. Otherwise fills |resource_id|. | 157 // resources. Otherwise fills |resource_id|. |
131 bool IsComponentExtensionResource(const extensions::Extension* extension, | 158 bool IsComponentExtensionResource(const extensions::Extension* extension, |
132 const ExtensionResource& resource, | 159 const ExtensionResource& resource, |
133 int& resource_id) const; | 160 int& resource_id) const; |
134 | 161 |
135 // content::NotificationObserver method. If an extension is uninstalled while | 162 // content::NotificationObserver method. If an extension is uninstalled while |
136 // we're waiting for the image we remove the entry from load_map_. | 163 // we're waiting for the image we remove the entry from load_map_. |
(...skipping 16 matching lines...) Expand all Loading... | |
153 | 180 |
154 content::NotificationRegistrar registrar_; | 181 content::NotificationRegistrar registrar_; |
155 | 182 |
156 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, | 183 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, |
157 IsComponentExtensionResource); | 184 IsComponentExtensionResource); |
158 | 185 |
159 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 186 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
160 }; | 187 }; |
161 | 188 |
162 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 189 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |