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> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 ResizeCondition resize_method; | 89 ResizeCondition resize_method; |
90 | 90 |
91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger | 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. | 92 // than |desired_size| it will be resized to these dimensions. |
93 gfx::Size desired_size; | 93 gfx::Size desired_size; |
94 | 94 |
95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. | 95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. |
96 ui::ScaleFactor scale_factor; | 96 ui::ScaleFactor scale_factor; |
97 }; | 97 }; |
98 | 98 |
99 // Returns true if given extension id is a special component extension that | |
100 // has its resource bundled. | |
101 // TODO(xiyuan): Move this out of this class. | |
102 static bool IsSpecialBundledExtensionId(const std::string& extension_id); | |
103 | |
104 explicit ImageLoadingTracker(Observer* observer); | 99 explicit ImageLoadingTracker(Observer* observer); |
105 virtual ~ImageLoadingTracker(); | 100 virtual ~ImageLoadingTracker(); |
106 | 101 |
107 // 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 |
108 // |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 |
109 // function may call back your observer synchronously (ie before it returns) | 104 // function may call back your observer synchronously (ie before it returns) |
110 // if the image was found in the cache. | 105 // if the image was found in the cache. |
111 // Note this method loads a raw bitmap from the resource. All sizes given are | 106 // Note this method loads a raw bitmap from the resource. All sizes given are |
112 // assumed to be in pixels. | 107 // assumed to be in pixels. |
113 void LoadImage(const extensions::Extension* extension, | 108 void LoadImage(const extensions::Extension* extension, |
114 const ExtensionResource& resource, | 109 const ExtensionResource& resource, |
115 const gfx::Size& max_size, | 110 const gfx::Size& max_size, |
116 CacheParam cache); | 111 CacheParam cache); |
117 | 112 |
118 // Same as LoadImage() above except it loads multiple images from the same | 113 // Same as LoadImage() above except it loads multiple images from the same |
119 // extension. This is used to load multiple resolutions of the same image | 114 // extension. This is used to load multiple resolutions of the same image |
120 // type. | 115 // type. |
121 void LoadImages(const extensions::Extension* extension, | 116 void LoadImages(const extensions::Extension* extension, |
122 const std::vector<ImageRepresentation>& info_list, | 117 const std::vector<ImageRepresentation>& info_list, |
123 CacheParam cache); | 118 CacheParam cache); |
124 | 119 |
125 // Returns the ID used for the next image that is loaded. That is, the return | 120 // Returns the ID used for the next image that is loaded. That is, the return |
126 // value from this method corresponds to the int that is passed to | 121 // value from this method corresponds to the int that is passed to |
127 // OnImageLoaded() the next time LoadImage() is invoked. | 122 // OnImageLoaded() the next time LoadImage() is invoked. |
128 int next_id() const { return next_id_; } | 123 int next_id() const { return next_id_; } |
129 | 124 |
| 125 // Checks whether image is a component extension resource. Returns false |
| 126 // if a given |resource| does not have a corresponding image in bundled |
| 127 // resources. Otherwise fills |resource_id|. |
| 128 static bool IsComponentExtensionResource( |
| 129 const extensions::Extension* extension, |
| 130 const FilePath& resource_path, |
| 131 int* resource_id); |
| 132 |
130 private: | 133 private: |
131 // Information for pending resource load operation for one or more image | 134 // Information for pending resource load operation for one or more image |
132 // representations. | 135 // representations. |
133 struct PendingLoadInfo { | 136 struct PendingLoadInfo { |
134 PendingLoadInfo(); | 137 PendingLoadInfo(); |
135 ~PendingLoadInfo(); | 138 ~PendingLoadInfo(); |
136 | 139 |
137 const extensions::Extension* extension; | 140 const extensions::Extension* extension; |
138 // This is cached separate from |extension| in case the extension is | 141 // This is cached separate from |extension| in case the extension is |
139 // unloaded. | 142 // unloaded. |
140 std::string extension_id; | 143 std::string extension_id; |
141 CacheParam cache; | 144 CacheParam cache; |
142 size_t pending_count; | 145 size_t pending_count; |
143 gfx::ImageSkia image_skia; | 146 gfx::ImageSkia image_skia; |
144 }; | 147 }; |
145 | 148 |
146 // Maps an integer identifying a load request to a PendingLoadInfo. | 149 // Maps an integer identifying a load request to a PendingLoadInfo. |
147 typedef std::map<int, PendingLoadInfo> LoadMap; | 150 typedef std::map<int, PendingLoadInfo> LoadMap; |
148 | 151 |
149 class ImageLoader; | 152 class ImageLoader; |
150 | 153 |
151 // Called on the calling thread when the bitmap finishes loading. | 154 // Called on the calling thread when the bitmap finishes loading. |
152 // |bitmap| may be null if the image file failed to decode. | 155 // |bitmap| may be null if the image file failed to decode. |
153 void OnBitmapLoaded(const SkBitmap* bitmap, | 156 void OnBitmapLoaded(const SkBitmap* bitmap, |
154 const ImageRepresentation& image_info, | 157 const ImageRepresentation& image_info, |
155 const gfx::Size& original_size, | 158 const gfx::Size& original_size, |
156 int id, | 159 int id, |
157 bool should_cache); | 160 bool should_cache); |
158 | 161 |
159 // Checks whether image is a component extension resource. Returns false | |
160 // if a given |resource| does not have a corresponding image in bundled | |
161 // resources. Otherwise fills |resource_id|. | |
162 bool IsComponentExtensionResource(const extensions::Extension* extension, | |
163 const ExtensionResource& resource, | |
164 int& resource_id) const; | |
165 | |
166 // content::NotificationObserver method. If an extension is uninstalled while | 162 // content::NotificationObserver method. If an extension is uninstalled while |
167 // 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_. |
168 virtual void Observe(int type, | 164 virtual void Observe(int type, |
169 const content::NotificationSource& source, | 165 const content::NotificationSource& source, |
170 const content::NotificationDetails& details) OVERRIDE; | 166 const content::NotificationDetails& details) OVERRIDE; |
171 | 167 |
172 // The view that is waiting for the image to load. | 168 // The view that is waiting for the image to load. |
173 Observer* observer_; | 169 Observer* observer_; |
174 | 170 |
175 // ID to use for next image requested. This is an ever increasing integer. | 171 // ID to use for next image requested. This is an ever increasing integer. |
176 int next_id_; | 172 int next_id_; |
177 | 173 |
178 // The object responsible for loading the image on the File thread. | 174 // The object responsible for loading the image on the File thread. |
179 scoped_refptr<ImageLoader> loader_; | 175 scoped_refptr<ImageLoader> loader_; |
180 | 176 |
181 // Information for each LoadImage request is cached here. The integer | 177 // Information for each LoadImage request is cached here. The integer |
182 // identifies the id assigned to the request. | 178 // identifies the id assigned to the request. |
183 LoadMap load_map_; | 179 LoadMap load_map_; |
184 | 180 |
185 content::NotificationRegistrar registrar_; | 181 content::NotificationRegistrar registrar_; |
186 | 182 |
187 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, | |
188 IsComponentExtensionResource); | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 183 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
191 }; | 184 }; |
192 | 185 |
193 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 186 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |