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. |
44 // | |
45 // To supports DIP, the returned ImageSkia has all representations for scale | |
46 // factors in use when LoadImage is called. And if you care about DIP change | |
47 // after that, you need to keep this class around. And when screen scale factor | |
48 // is change and ImageSkia requests a representation for the new scale factor, | |
oshima
2012/07/16 17:22:33
"and Image" (space)
xiyuan
2012/07/16 20:11:28
Done.
| |
49 // ImageLoadingTracker::ImageSource would get the request, finds a relevant | |
50 // extension resource and loads it. After the new resource is loaded, | |
51 // ImageLoadingTracker::Observer would be notified again with updated image. | |
52 // | |
39 // NOTE: if the image is available already (or the resource is not valid), the | 53 // 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, | 54 // Observer is notified immediately from the call to LoadImage. In other words, |
41 // by the time LoadImage returns the observer has been notified. | 55 // by the time LoadImage returns the observer has been notified. |
42 // | 56 // |
43 class ImageLoadingTracker : public content::NotificationObserver { | 57 class ImageLoadingTracker : public content::NotificationObserver { |
44 public: | 58 public: |
45 enum CacheParam { | 59 enum CacheParam { |
46 CACHE, | 60 CACHE, |
47 DONT_CACHE | 61 DONT_CACHE |
48 }; | 62 }; |
49 | 63 |
50 class Observer { | 64 class Observer { |
51 public: | 65 public: |
52 // Will be called when the image with the given index has loaded. | 66 // 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 | 67 // |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 | 68 // 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 | 69 // from. |index| represents the index of the image just loaded (starts at 0 |
56 // and increments every time LoadImage is called). | 70 // and increments every time LoadImage is called). |
57 virtual void OnImageLoaded(const gfx::Image& image, | 71 virtual void OnImageLoaded(const gfx::Image& image, |
58 const std::string& extension_id, | 72 const std::string& extension_id, |
59 int index) = 0; | 73 int index) = 0; |
60 | 74 |
61 protected: | 75 protected: |
62 virtual ~Observer(); | 76 virtual ~Observer(); |
63 }; | 77 }; |
64 | 78 |
65 // Information about a single image to load from a extension resource. | 79 // Information about a single image to load from a extension resource. |
pkotwicz
2012/07/16 17:33:52
image->bitmap/image rep
xiyuan
2012/07/16 20:11:28
Done.
| |
66 struct ImageInfo { | 80 struct ImageInfo { |
67 ImageInfo(const ExtensionResource& resource, gfx::Size max_size); | 81 ImageInfo(const ExtensionResource& resource, |
82 const gfx::Size& max_size, | |
83 ui::ScaleFactor scale_factor); | |
68 ~ImageInfo(); | 84 ~ImageInfo(); |
85 | |
69 ExtensionResource resource; | 86 ExtensionResource resource; |
70 // If the loaded image is larger than |max_size| it will be resized to those | 87 // If the loaded image is larger than |max_size| it will be resized to those |
71 // dimensions. | 88 // dimensions. |
72 gfx::Size max_size; | 89 gfx::Size max_size; |
90 // |scale_factor| is used to construct loaded gfx::ImageSkia. | |
91 ui::ScaleFactor scale_factor; | |
73 }; | 92 }; |
74 | 93 |
75 explicit ImageLoadingTracker(Observer* observer); | 94 explicit ImageLoadingTracker(Observer* observer); |
76 virtual ~ImageLoadingTracker(); | 95 virtual ~ImageLoadingTracker(); |
77 | 96 |
78 // 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 |
79 // |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 |
80 // function may call back your observer synchronously (ie before it returns) | 99 // function may call back your observer synchronously (ie before it returns) |
81 // if the image was found in the cache. | 100 // if the image was found in the cache. |
82 void LoadImage(const extensions::Extension* extension, | 101 void LoadImage(const extensions::Extension* extension, |
pkotwicz
2012/07/16 17:33:52
Can you put a comment to the effect that the metho
xiyuan
2012/07/16 20:11:28
Added a comment that indicates this method loads r
| |
83 const ExtensionResource& resource, | 102 const ExtensionResource& resource, |
84 const gfx::Size& max_size, | 103 const gfx::Size& max_size, |
85 CacheParam cache); | 104 CacheParam cache); |
86 | 105 |
106 // Similar to LoadImage above but loads an ImageSkia that supports DIP. | |
107 // |resource_size| and |max_size| are in DIP coordinates. | |
108 void LoadImageInDIP(const extensions::Extension* extension, | |
pkotwicz
2012/07/16 17:33:52
Can you call this simply "LoadImageSkia"?
xiyuan
2012/07/16 20:11:28
Done.
| |
109 int resource_size, | |
110 ExtensionIconSet::MatchType resource_match_type, | |
111 const gfx::Size& max_size, | |
112 CacheParam cache); | |
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<ImageInfo>& 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: |
127 class ImageLoader; | |
128 class ImageSource; | |
129 | |
100 // Information for pending image load operation for one or more images. | 130 // Information for pending image load operation for one or more images. |
101 struct PendingLoadInfo { | 131 struct PendingLoadInfo { |
102 PendingLoadInfo(); | 132 PendingLoadInfo(); |
103 ~PendingLoadInfo(); | 133 ~PendingLoadInfo(); |
104 | 134 |
105 const extensions::Extension* extension; | 135 const extensions::Extension* extension; |
106 // This is cached separate from |extension| in case the extension in | 136 // This is cached separate from |extension| in case the extension is |
107 // unloaded. | 137 // unloaded. |
108 std::string extension_id; | 138 std::string extension_id; |
139 | |
140 // Size info from a LoadImage call. | |
141 int resource_size_in_dip; | |
142 ExtensionIconSet::MatchType resource_match_type; | |
143 gfx::Size max_size_in_dip; | |
144 | |
145 // ImageSource loads image for additional scale factor. This is only set if | |
146 // above size info is valid. | |
147 ImageSource* image_source; // Owned by ImageSkiaStorage. | |
148 | |
109 CacheParam cache; | 149 CacheParam cache; |
110 size_t pending_count; | 150 size_t pending_count; |
111 std::vector<SkBitmap> bitmaps; | 151 |
152 gfx::ImageSkia images; | |
pkotwicz
2012/07/16 17:33:52
Can you call this variable image_skia? I think it
xiyuan
2012/07/16 20:11:28
Done.
| |
112 }; | 153 }; |
113 | 154 |
114 // Maps an integer identifying a load request to a PendingLoadInfo. | 155 // Maps an integer identifying a load request to a PendingLoadInfo. |
115 typedef std::map<int, PendingLoadInfo> LoadMap; | 156 typedef std::map<int, PendingLoadInfo> LoadMap; |
116 | 157 |
117 class ImageLoader; | 158 // Loads image resources in |info_list| for load request identified by |id|. |
159 void DoLoadImages(int id, const std::vector<ImageInfo>& info_list); | |
160 | |
161 // Loads image for additional scale factor for given load id. | |
162 void LoadImageForScaleFactor(int id, ui::ScaleFactor scale_factor); | |
118 | 163 |
119 // When an image has finished loaded and been resized on the file thread, it | 164 // When an image has finished loaded and been resized on the file thread, it |
120 // is posted back to this method on the original thread. This method then | 165 // is posted back to this method on the original thread. This method then |
121 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if | 166 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if |
122 // it was the last image in the list. The |original_size| should be the size | 167 // it was the last image in the list. The |original_size| should be the size |
123 // of the image before any resizing was done. | 168 // of the image before any resizing was done. |
124 // |image| may be null if the file failed to decode. | 169 // |image| may be null if the file failed to decode. |
125 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, | 170 void OnImageLoaded(SkBitmap* image, const ImageInfo& image_info, |
126 const gfx::Size& original_size, int id, bool should_cache); | 171 const gfx::Size& original_size, int id, bool should_cache); |
127 | 172 |
128 // Checks whether image is a component extension resource. Returns false | 173 // Checks whether image is a component extension resource. Returns false |
129 // if a given |resource| does not have a corresponding image in bundled | 174 // if a given |resource| does not have a corresponding image in bundled |
130 // resources. Otherwise fills |resource_id|. | 175 // resources. Otherwise fills |resource_id|. |
131 bool IsComponentExtensionResource(const extensions::Extension* extension, | 176 bool IsComponentExtensionResource(const extensions::Extension* extension, |
132 const ExtensionResource& resource, | 177 const ExtensionResource& resource, |
133 int& resource_id) const; | 178 int& resource_id) const; |
134 | 179 |
135 // content::NotificationObserver method. If an extension is uninstalled while | 180 // content::NotificationObserver method. If an extension is uninstalled while |
(...skipping 17 matching lines...) Expand all Loading... | |
153 | 198 |
154 content::NotificationRegistrar registrar_; | 199 content::NotificationRegistrar registrar_; |
155 | 200 |
156 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, | 201 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, |
157 IsComponentExtensionResource); | 202 IsComponentExtensionResource); |
158 | 203 |
159 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 204 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
160 }; | 205 }; |
161 | 206 |
162 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 207 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |