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 single bitmap/image rep to load from an extension |
Aaron Boodman
2012/08/02 17:50:56
When you're dealing with ImageRepInfo (or ImageRep
tbarzic
2012/08/02 20:12:52
Done.
| |
66 struct ImageInfo { | 71 // resource. |
67 ImageInfo(const ExtensionResource& resource, gfx::Size max_size); | 72 struct ImageRepInfo { |
Aaron Boodman
2012/08/02 17:50:56
per styleguide [1] http://google-styleguide.google
tbarzic
2012/08/02 20:12:52
Done.
| |
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 { | |
76 RESIZE_WHEN_LARGER, | |
77 ALWAYS_RESIZE, | |
78 }; | |
79 | |
80 ImageRepInfo(const ExtensionResource& resource, | |
81 ResizeMethod resize_method, | |
82 const gfx::Size& desired_size, | |
83 ui::ScaleFactor scale_factor); | |
84 ~ImageRepInfo(); | |
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 |
102 // Checks whether given extension resource could be loaded. | |
103 static bool CanLoadImage(const extensions::Extension* extension, | |
104 const ExtensionResource& resource); | |
105 | |
78 // Specify image resource to load. If the loaded image is larger than | 106 // 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 | 107 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this |
80 // function may call back your observer synchronously (ie before it returns) | 108 // function may call back your observer synchronously (ie before it returns) |
81 // if the image was found in the cache. | 109 // if the image was found in the cache. |
110 // Note this method loads a raw bitmap from the resource. All sizes given are | |
111 // assumed to be in pixels. If you want to load a DIP-aware image, use | |
112 // |LoadImageSkia| instead. | |
82 void LoadImage(const extensions::Extension* extension, | 113 void LoadImage(const extensions::Extension* extension, |
83 const ExtensionResource& resource, | 114 const ExtensionResource& resource, |
84 const gfx::Size& max_size, | 115 const gfx::Size& max_size, |
85 CacheParam cache); | 116 CacheParam cache); |
86 | 117 |
87 // Same as LoadImage() above except it loads multiple images from the same | 118 // 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 | 119 // extension. This is used to load multiple resolutions of the same image |
89 // type. | 120 // type. |
90 void LoadImages(const extensions::Extension* extension, | 121 void LoadImages(const extensions::Extension* extension, |
91 const std::vector<ImageInfo>& info_list, | 122 const std::vector<ImageRepInfo>& info_list, |
92 CacheParam cache); | 123 CacheParam cache); |
93 | 124 |
94 // Returns the ID used for the next image that is loaded. That is, the return | 125 // 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 | 126 // value from this method corresponds to the int that is passed to |
96 // OnImageLoaded() the next time LoadImage() is invoked. | 127 // OnImageLoaded() the next time LoadImage() is invoked. |
97 int next_id() const { return next_id_; } | 128 int next_id() const { return next_id_; } |
98 | 129 |
99 private: | 130 private: |
100 // Information for pending image load operation for one or more images. | 131 // Information for pending resource load operation for one or more |
132 // bitmaps/image reps. | |
Aaron Boodman
2012/08/02 17:50:56
Same thing here with 'bitmap/', and throughout.
tbarzic
2012/08/02 20:12:52
Done.
| |
101 struct PendingLoadInfo { | 133 struct PendingLoadInfo { |
102 PendingLoadInfo(); | 134 PendingLoadInfo(); |
103 ~PendingLoadInfo(); | 135 ~PendingLoadInfo(); |
104 | 136 |
105 const extensions::Extension* extension; | 137 const extensions::Extension* extension; |
106 // This is cached separate from |extension| in case the extension in | 138 // This is cached separate from |extension| in case the extension is |
107 // unloaded. | 139 // unloaded. |
108 std::string extension_id; | 140 std::string extension_id; |
109 CacheParam cache; | 141 CacheParam cache; |
110 size_t pending_count; | 142 size_t pending_count; |
111 std::vector<SkBitmap> bitmaps; | 143 gfx::ImageSkia image_skia; |
112 }; | 144 }; |
113 | 145 |
114 // Maps an integer identifying a load request to a PendingLoadInfo. | 146 // Maps an integer identifying a load request to a PendingLoadInfo. |
115 typedef std::map<int, PendingLoadInfo> LoadMap; | 147 typedef std::map<int, PendingLoadInfo> LoadMap; |
116 | 148 |
117 class ImageLoader; | 149 class ImageLoader; |
118 | 150 |
119 // When an image has finished loaded and been resized on the file thread, it | 151 // When a bitmap has finished loading and been resized on the file thread, it |
Aaron Boodman
2012/08/02 17:50:56
This comment contains way too much implementation
tbarzic
2012/08/02 20:12:52
agree
| |
120 // is posted back to this method on the original thread. This method then | 152 // is posted back to this method on the original thread. This method then |
121 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if | 153 // 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 | 154 // the caller will not request image reps for aditional scale factors from the |
123 // of the image before any resizing was done. | 155 // ImageLoadingTracker. The |original_size| should be the size of the bitmap |
124 // |image| may be null if the file failed to decode. | 156 // before any resizing was done. |
125 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, | 157 // |bitmap| may be null if the file failed to decode. |
126 const gfx::Size& original_size, int id, bool should_cache); | 158 void OnBitmapLoaded(SkBitmap* bitmap, |
159 const ImageRepInfo& image_info, | |
160 const gfx::Size& original_size, | |
161 int id, | |
162 bool should_cache); | |
127 | 163 |
128 // Checks whether image is a component extension resource. Returns false | 164 // Checks whether image is a component extension resource. Returns false |
129 // if a given |resource| does not have a corresponding image in bundled | 165 // if a given |resource| does not have a corresponding image in bundled |
130 // resources. Otherwise fills |resource_id|. | 166 // resources. Otherwise fills |resource_id|. |
131 bool IsComponentExtensionResource(const extensions::Extension* extension, | 167 bool IsComponentExtensionResource(const extensions::Extension* extension, |
132 const ExtensionResource& resource, | 168 const ExtensionResource& resource, |
133 int& resource_id) const; | 169 int& resource_id) const; |
134 | 170 |
135 // content::NotificationObserver method. If an extension is uninstalled while | 171 // content::NotificationObserver method. If an extension is uninstalled while |
136 // we're waiting for the image we remove the entry from load_map_. | 172 // we're waiting for the image we remove the entry from load_map_. |
(...skipping 16 matching lines...) Expand all Loading... | |
153 | 189 |
154 content::NotificationRegistrar registrar_; | 190 content::NotificationRegistrar registrar_; |
155 | 191 |
156 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, | 192 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, |
157 IsComponentExtensionResource); | 193 IsComponentExtensionResource); |
158 | 194 |
159 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 195 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
160 }; | 196 }; |
161 | 197 |
162 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 198 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |