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 #include "chrome/browser/extensions/image_loading_tracker.h" | 5 #include "chrome/browser/extensions/image_loading_tracker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 12 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
15 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
16 #include "chrome/common/extensions/extension_resource.h" | 16 #include "chrome/common/extensions/extension_resource.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "grit/component_extension_resources_map.h" | 19 #include "grit/component_extension_resources_map.h" |
20 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
21 #include "skia/ext/image_operations.h" | 21 #include "skia/ext/image_operations.h" |
22 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
24 #include "ui/gfx/image/image_skia.h" | |
25 #include "ui/gfx/image/image_skia_rep.h" | 24 #include "ui/gfx/image/image_skia_rep.h" |
25 #include "ui/gfx/image/image_skia_source.h" | |
26 #include "ui/gfx/screen.h" | |
26 #include "webkit/glue/image_decoder.h" | 27 #include "webkit/glue/image_decoder.h" |
27 | 28 |
28 using content::BrowserThread; | 29 using content::BrowserThread; |
29 using extensions::Extension; | 30 using extensions::Extension; |
30 | 31 |
31 //////////////////////////////////////////////////////////////////////////////// | 32 //////////////////////////////////////////////////////////////////////////////// |
32 // ImageLoadingTracker::Observer | 33 // ImageLoadingTracker::Observer |
33 | 34 |
34 ImageLoadingTracker::Observer::~Observer() {} | 35 ImageLoadingTracker::Observer::~Observer() {} |
35 | 36 |
36 //////////////////////////////////////////////////////////////////////////////// | 37 //////////////////////////////////////////////////////////////////////////////// |
37 // ImageLoadingTracker::ImageInfo | 38 // ImageLoadingTracker::ImageRepInfo |
38 | 39 |
39 ImageLoadingTracker::ImageInfo::ImageInfo( | 40 ImageLoadingTracker::ImageRepInfo::ImageRepInfo( |
40 const ExtensionResource& resource, gfx::Size max_size) | 41 const ExtensionResource& resource, |
41 : resource(resource), max_size(max_size) { | 42 ResizeMethod resize_method, |
43 const gfx::Size& desired_size, | |
44 ui::ScaleFactor scale_factor) | |
45 : resource(resource), | |
46 resize_method(resize_method), | |
47 desired_size(desired_size), | |
48 scale_factor(scale_factor) { | |
42 } | 49 } |
43 | 50 |
44 ImageLoadingTracker::ImageInfo::~ImageInfo() { | 51 ImageLoadingTracker::ImageRepInfo::~ImageRepInfo() { |
45 } | 52 } |
46 | 53 |
47 //////////////////////////////////////////////////////////////////////////////// | 54 //////////////////////////////////////////////////////////////////////////////// |
48 // ImageLoadingTracker::PendingLoadInfo | 55 // ImageLoadingTracker::PendingLoadInfo |
49 | 56 |
50 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() | 57 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() |
51 : extension(NULL), | 58 : extension(NULL), |
59 resource_size_in_dip(ExtensionIconSet::EXTENSION_ICON_INVALID), | |
60 resource_match_type(ExtensionIconSet::MATCH_BIGGER), | |
61 image_source(NULL), | |
52 cache(CACHE), | 62 cache(CACHE), |
53 pending_count(0) { | 63 pending_count(0) { |
54 } | 64 } |
55 | 65 |
56 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {} | 66 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {} |
57 | 67 |
58 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
59 // ImageLoadingTracker::ImageLoader | 69 // ImageLoadingTracker::ImageLoader |
60 | 70 |
61 // A RefCounted class for loading images on the File thread and reporting back | 71 // A RefCounted class for loading bitmaps/image reps on the File thread and |
62 // on the UI thread. | 72 // reporting back on the UI thread. |
63 class ImageLoadingTracker::ImageLoader | 73 class ImageLoadingTracker::ImageLoader |
64 : public base::RefCountedThreadSafe<ImageLoader> { | 74 : public base::RefCountedThreadSafe<ImageLoader> { |
65 public: | 75 public: |
66 explicit ImageLoader(ImageLoadingTracker* tracker) | 76 explicit ImageLoader(ImageLoadingTracker* tracker) |
67 : tracker_(tracker) { | 77 : tracker_(tracker) { |
68 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); | 78 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); |
69 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 79 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
70 } | 80 } |
71 | 81 |
72 // Lets this class know that the tracker is no longer interested in the | 82 // Lets this class know that the tracker is no longer interested in the |
73 // results. | 83 // results. |
74 void StopTracking() { | 84 void StopTracking() { |
75 tracker_ = NULL; | 85 tracker_ = NULL; |
76 } | 86 } |
77 | 87 |
78 // Instructs the loader to load a task on the File thread. | 88 // Instructs the loader to load a task on the File thread. |
79 void LoadImage(const ExtensionResource& resource, | 89 void LoadImage(const ImageRepInfo& image_info, int id) { |
80 const gfx::Size& max_size, | |
81 int id) { | |
82 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 90 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
83 BrowserThread::PostTask( | 91 BrowserThread::PostTask( |
84 BrowserThread::FILE, FROM_HERE, | 92 BrowserThread::FILE, FROM_HERE, |
85 base::Bind(&ImageLoader::LoadOnFileThread, this, resource, | 93 base::Bind(&ImageLoader::LoadOnFileThread, this, image_info, id)); |
86 max_size, id)); | |
87 } | 94 } |
88 | 95 |
89 void LoadOnFileThread(const ExtensionResource& resource, | 96 void LoadOnFileThread(const ImageRepInfo& image_info, int id) { |
90 const gfx::Size& max_size, | |
91 int id) { | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
93 | 98 |
94 // Read the file from disk. | 99 // Read the file from disk. |
95 std::string file_contents; | 100 std::string file_contents; |
96 FilePath path = resource.GetFilePath(); | 101 FilePath path = image_info.resource.GetFilePath(); |
97 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 102 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
98 ReportBack(NULL, resource, gfx::Size(), id); | 103 ReportBack(NULL, image_info, gfx::Size(), id); |
99 return; | 104 return; |
100 } | 105 } |
101 | 106 |
102 // Decode the image using WebKit's image decoder. | 107 // Decode the bitmap using WebKit's image decoder. |
103 const unsigned char* data = | 108 const unsigned char* data = |
104 reinterpret_cast<const unsigned char*>(file_contents.data()); | 109 reinterpret_cast<const unsigned char*>(file_contents.data()); |
105 webkit_glue::ImageDecoder decoder; | 110 webkit_glue::ImageDecoder decoder; |
106 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 111 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
107 // Note: This class only decodes images from extension resources. Chrome | 112 // Note: This class only decodes bitmaps from extension resources. Chrome |
108 // doesn't (for security reasons) directly load extension resources provided | 113 // doesn't (for security reasons) directly load extension resources provided |
109 // by the extension author, but instead decodes them in a separate | 114 // by the extension author, but instead decodes them in a separate |
110 // locked-down utility process. Only if the decoding succeeds is the image | 115 // locked-down utility process. Only if the decoding succeeds is the image |
111 // saved from memory to disk and subsequently used in the Chrome UI. | 116 // saved from memory to disk and subsequently used in the Chrome UI. |
112 // Chrome is therefore decoding images here that were generated by Chrome. | 117 // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
113 *decoded = decoder.Decode(data, file_contents.length()); | 118 *decoded = decoder.Decode(data, file_contents.length()); |
114 if (decoded->empty()) { | 119 if (decoded->empty()) { |
115 ReportBack(NULL, resource, gfx::Size(), id); | 120 ReportBack(NULL, image_info, gfx::Size(), id); |
116 return; // Unable to decode. | 121 return; // Unable to decode. |
117 } | 122 } |
118 | 123 |
119 gfx::Size original_size(decoded->width(), decoded->height()); | 124 gfx::Size original_size(decoded->width(), decoded->height()); |
120 | 125 |
121 if (decoded->width() > max_size.width() || | 126 if (image_info.resize_method == ImageRepInfo::ALWAYS_RESIZE || |
122 decoded->height() > max_size.height()) { | 127 decoded->width() > image_info.desired_size.width() || |
128 decoded->height() > image_info.desired_size.height()) { | |
123 // The bitmap is too big, re-sample. | 129 // The bitmap is too big, re-sample. |
124 *decoded = skia::ImageOperations::Resize( | 130 *decoded = skia::ImageOperations::Resize( |
125 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 131 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
126 max_size.width(), max_size.height()); | 132 image_info.desired_size.width(), image_info.desired_size.height()); |
127 } | 133 } |
128 | 134 |
129 ReportBack(decoded.release(), resource, original_size, id); | 135 ReportBack(decoded.release(), image_info, original_size, id); |
130 } | 136 } |
131 | 137 |
132 // Instructs the loader to load a resource on the File thread. | 138 // Instructs the loader to load a resource on the File thread. |
133 void LoadResource(const ExtensionResource& resource, | 139 void LoadResource(const ImageRepInfo& image_info, int id, int resource_id) { |
134 const gfx::Size& max_size, | |
135 int id, | |
136 int resource_id) { | |
137 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
138 BrowserThread::PostTask( | 141 BrowserThread::PostTask( |
139 BrowserThread::FILE, FROM_HERE, | 142 BrowserThread::FILE, FROM_HERE, |
140 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource, | 143 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, |
141 max_size, id, resource_id)); | 144 id, resource_id)); |
142 } | 145 } |
143 | 146 |
144 void LoadResourceOnFileThread(const ExtensionResource& resource, | 147 void LoadResourceOnFileThread(const ImageRepInfo& image_info, |
145 const gfx::Size& max_size, | |
146 int id, | 148 int id, |
147 int resource_id) { | 149 int resource_id) { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
149 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( | 151 SkBitmap* bitmap = ExtensionIconSource::LoadImageByResourceId( |
150 resource_id); | 152 resource_id); |
151 ReportBack(image, resource, max_size, id); | 153 ReportBack(bitmap, image_info, image_info.desired_size, id); |
152 } | 154 } |
153 | 155 |
154 void ReportBack(SkBitmap* image, const ExtensionResource& resource, | 156 void ReportBack(SkBitmap* bitmap, const ImageRepInfo& image_info, |
155 const gfx::Size& original_size, int id) { | 157 const gfx::Size& original_size, int id) { |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
157 | 159 |
158 BrowserThread::PostTask( | 160 BrowserThread::PostTask( |
159 callback_thread_id_, FROM_HERE, | 161 callback_thread_id_, FROM_HERE, |
160 base::Bind(&ImageLoader::ReportOnUIThread, this, | 162 base::Bind(&ImageLoader::ReportOnUIThread, this, |
161 image, resource, original_size, id)); | 163 bitmap, image_info, original_size, id)); |
162 } | 164 } |
163 | 165 |
164 void ReportOnUIThread(SkBitmap* image, const ExtensionResource& resource, | 166 void ReportOnUIThread(SkBitmap* bitmap, const ImageRepInfo& image_info, |
165 const gfx::Size& original_size, int id) { | 167 const gfx::Size& original_size, int id) { |
166 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 168 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
167 | 169 |
168 if (tracker_) | 170 if (tracker_) |
169 tracker_->OnImageLoaded(image, resource, original_size, id, true); | 171 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
170 | 172 |
171 delete image; | 173 delete bitmap; |
172 } | 174 } |
173 | 175 |
174 private: | 176 private: |
175 friend class base::RefCountedThreadSafe<ImageLoader>; | 177 friend class base::RefCountedThreadSafe<ImageLoader>; |
176 ~ImageLoader() {} | 178 ~ImageLoader() {} |
177 | 179 |
178 // The tracker we are loading the image for. If NULL, it means the tracker is | 180 // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
179 // no longer interested in the reply. | 181 // no longer interested in the reply. |
180 ImageLoadingTracker* tracker_; | 182 ImageLoadingTracker* tracker_; |
181 | 183 |
182 // The thread that we need to call back on to report that we are done. | 184 // The thread that we need to call back on to report that we are done. |
183 BrowserThread::ID callback_thread_id_; | 185 BrowserThread::ID callback_thread_id_; |
184 | 186 |
185 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 187 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
186 }; | 188 }; |
187 | 189 |
188 //////////////////////////////////////////////////////////////////////////////// | 190 //////////////////////////////////////////////////////////////////////////////// |
191 // ImageLoadingTracker::ImageSource | |
192 | |
193 // An ImageSkiaSource to load bitmap/image rep for additional scale factors. | |
194 class ImageLoadingTracker::ImageSource : public gfx::ImageSkiaSource { | |
195 public: | |
196 ImageSource(ImageLoadingTracker* tracker, int id); | |
197 virtual ~ImageSource(); | |
198 | |
199 void StopTracking(); | |
200 | |
201 // gfx::ImageSkiaSource overrides: | |
202 virtual gfx::ImageSkiaRep GetImageForScale( | |
203 ui::ScaleFactor scale_factor) OVERRIDE; | |
204 | |
205 private: | |
206 ImageLoadingTracker* tracker_; | |
207 int id_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(ImageSource); | |
210 }; | |
211 | |
212 ImageLoadingTracker::ImageSource::ImageSource(ImageLoadingTracker* tracker, | |
213 int id) | |
214 : tracker_(tracker), | |
215 id_(id) { | |
216 } | |
217 | |
218 ImageLoadingTracker::ImageSource::~ImageSource() { | |
219 } | |
220 | |
221 void ImageLoadingTracker::ImageSource::StopTracking() { | |
222 tracker_ = NULL; | |
223 } | |
224 | |
225 gfx::ImageSkiaRep ImageLoadingTracker::ImageSource::GetImageForScale( | |
226 ui::ScaleFactor scale_factor) { | |
227 // Asks tracker to load new bitmap/image rep for |scale_factor|. | |
228 if (tracker_) | |
229 tracker_->LoadImageForScaleFactor(id_, scale_factor); | |
230 | |
231 // Returns an empty representation for |scale_factor| here. If |tracker_| is | |
232 // valid, it loads the bitmap asynchronously. When loading is done, it updates | |
233 // existing ImageSkia and notifies its observer. | |
234 return gfx::ImageSkiaRep(); | |
235 } | |
236 | |
237 //////////////////////////////////////////////////////////////////////////////// | |
189 // ImageLoadingTracker | 238 // ImageLoadingTracker |
190 | 239 |
191 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) | 240 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
192 : observer_(observer), | 241 : observer_(observer), |
193 next_id_(0) { | 242 next_id_(0) { |
194 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 243 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
195 content::NotificationService::AllSources()); | 244 content::NotificationService::AllSources()); |
196 } | 245 } |
197 | 246 |
198 ImageLoadingTracker::~ImageLoadingTracker() { | 247 ImageLoadingTracker::~ImageLoadingTracker() { |
199 // The loader is created lazily and is NULL if the tracker is destroyed before | 248 // The loader is created lazily and is NULL if the tracker is destroyed before |
200 // any valid image load tasks have been posted. | 249 // any valid image load tasks have been posted. |
201 if (loader_) | 250 if (loader_) |
202 loader_->StopTracking(); | 251 loader_->StopTracking(); |
252 | |
253 for (LoadMap::iterator it = load_map_.begin(); it != load_map_.end(); ++it) { | |
254 if (it->second.image_source) | |
255 it->second.image_source->StopTracking(); | |
256 } | |
203 } | 257 } |
204 | 258 |
205 void ImageLoadingTracker::LoadImage(const Extension* extension, | 259 void ImageLoadingTracker::LoadImage(const Extension* extension, |
206 const ExtensionResource& resource, | 260 const ExtensionResource& resource, |
207 const gfx::Size& max_size, | 261 const gfx::Size& max_size, |
208 CacheParam cache) { | 262 CacheParam cache) { |
209 std::vector<ImageInfo> info_list; | 263 std::vector<ImageRepInfo> info_list; |
210 info_list.push_back(ImageInfo(resource, max_size)); | 264 info_list.push_back(ImageRepInfo(resource, |
265 ImageRepInfo::RESIZE_WHEN_LARGER, | |
266 max_size, | |
267 ui::SCALE_FACTOR_100P)); | |
211 LoadImages(extension, info_list, cache); | 268 LoadImages(extension, info_list, cache); |
212 } | 269 } |
213 | 270 |
271 void ImageLoadingTracker::LoadImageSkia( | |
272 const Extension* extension, | |
273 int resource_size, | |
274 ExtensionIconSet::MatchType resource_match_type, | |
275 const gfx::Size& desired_size, | |
276 CacheParam cache) { | |
277 PendingLoadInfo load_info; | |
278 load_info.extension = extension; | |
279 load_info.extension_id = extension->id(); | |
280 load_info.cache = cache; | |
281 | |
282 load_info.resource_size_in_dip = resource_size; | |
283 load_info.resource_match_type = resource_match_type; | |
284 load_info.desired_size_in_dip = desired_size; | |
285 | |
286 int id = next_id_++; | |
287 load_info.image_source = new ImageSource(this, id); | |
288 load_info.image_skia = gfx::ImageSkia(load_info.image_source, | |
289 load_info.desired_size_in_dip); | |
290 | |
291 load_map_[id] = load_info; | |
292 | |
293 // Notify |observer_| with an image that has ImageSource. Relevant bitmap | |
294 // resources would be loaded when ImageSource is asked to provide a | |
295 // representation for certain scale factor. | |
296 observer_->OnImageLoaded(gfx::Image(load_info.image_skia), | |
pkotwicz
2012/07/18 00:07:25
You need to validate that you are capable of gener
pkotwicz
2012/07/18 14:17:53
The validation is super cheap so you can also vali
xiyuan
2012/07/18 17:28:32
Good catch. Added a CanLoadImage to check whether
| |
297 load_info.extension_id, | |
298 id); | |
299 } | |
300 | |
214 void ImageLoadingTracker::LoadImages(const Extension* extension, | 301 void ImageLoadingTracker::LoadImages(const Extension* extension, |
215 const std::vector<ImageInfo>& info_list, | 302 const std::vector<ImageRepInfo>& info_list, |
216 CacheParam cache) { | 303 CacheParam cache) { |
217 PendingLoadInfo load_info; | 304 PendingLoadInfo load_info; |
218 load_info.extension = extension; | 305 load_info.extension = extension; |
306 load_info.extension_id = extension->id(); | |
219 load_info.cache = cache; | 307 load_info.cache = cache; |
220 load_info.extension_id = extension->id(); | 308 |
221 load_info.pending_count = info_list.size(); | |
222 int id = next_id_++; | 309 int id = next_id_++; |
223 load_map_[id] = load_info; | 310 load_map_[id] = load_info; |
224 | 311 |
225 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); | 312 DoLoadImage(id, info_list); |
313 } | |
314 | |
315 void ImageLoadingTracker::DoLoadImage( | |
316 int id, | |
317 const std::vector<ImageRepInfo>& info_list) { | |
318 LoadMap::iterator load_map_it = load_map_.find(id); | |
319 DCHECK(load_map_it != load_map_.end()); | |
320 | |
321 PendingLoadInfo* load_info = &load_map_it->second; | |
322 | |
323 // Count |info_list| requests as additional requests to existing load | |
324 // requests to handle the case LoadImageForScaleFactor is called before | |
325 // previous pending loads are finished. | |
326 load_info->pending_count += info_list.size(); | |
327 | |
328 const extensions::Extension* extension = load_info->extension; | |
329 for (std::vector<ImageRepInfo>::const_iterator it = info_list.begin(); | |
226 it != info_list.end(); ++it) { | 330 it != info_list.end(); ++it) { |
227 // Load resources for special component extensions. | 331 // Load resources for special component extensions. |
228 if (load_info.extension_id == extension_misc::kWebStoreAppId) { | 332 if (load_info->extension_id == extension_misc::kWebStoreAppId) { |
229 if (!loader_) | 333 if (!loader_) |
230 loader_ = new ImageLoader(this); | 334 loader_ = new ImageLoader(this); |
231 loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON); | 335 loader_->LoadResource(*it, id, IDR_WEBSTORE_ICON); |
232 continue; | 336 continue; |
233 } else if (load_info.extension_id == extension_misc::kChromeAppId) { | 337 } else if (load_info->extension_id == extension_misc::kChromeAppId) { |
234 if (!loader_) | 338 if (!loader_) |
235 loader_ = new ImageLoader(this); | 339 loader_ = new ImageLoader(this); |
236 loader_->LoadResource(it->resource, | 340 loader_->LoadResource(*it, id, IDR_PRODUCT_LOGO_128); |
237 it->max_size, | |
238 id, | |
239 IDR_PRODUCT_LOGO_128); | |
240 continue; | 341 continue; |
241 } | 342 } |
242 | 343 |
243 // If we don't have a path we don't need to do any further work, just | 344 // If we don't have a path we don't need to do any further work, just |
244 // respond back. | 345 // respond back. |
245 if (it->resource.relative_path().empty()) { | 346 if (it->resource.relative_path().empty()) { |
246 OnImageLoaded(NULL, it->resource, it->max_size, id, false); | 347 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
247 continue; | 348 continue; |
248 } | 349 } |
249 | 350 |
250 DCHECK(extension->path() == it->resource.extension_root()); | 351 DCHECK(extension->path() == it->resource.extension_root()); |
251 | 352 |
252 // See if the extension has the image already. | 353 // See if the extension has the bitmap/image rep already. |
253 if (extension->HasCachedImage(it->resource, it->max_size)) { | 354 if (extension->HasCachedImage(it->resource, it->desired_size)) { |
254 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | 355 SkBitmap bitmap = extension->GetCachedImage(it->resource, |
255 OnImageLoaded(&image, it->resource, it->max_size, id, false); | 356 it->desired_size); |
357 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); | |
256 continue; | 358 continue; |
257 } | 359 } |
258 | 360 |
259 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 361 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
260 // LoadResource do not block. | 362 // LoadResource do not block. |
261 if (!loader_) | 363 if (!loader_) |
262 loader_ = new ImageLoader(this); | 364 loader_ = new ImageLoader(this); |
263 | 365 |
264 int resource_id; | 366 int resource_id; |
265 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 367 if (IsComponentExtensionResource(extension, it->resource, resource_id)) |
266 loader_->LoadResource(it->resource, it->max_size, id, resource_id); | 368 loader_->LoadResource(*it, id, resource_id); |
267 else | 369 else |
268 loader_->LoadImage(it->resource, it->max_size, id); | 370 loader_->LoadImage(*it, id); |
269 } | 371 } |
270 } | 372 } |
271 | 373 |
374 void ImageLoadingTracker::LoadImageForScaleFactor( | |
375 int id, | |
376 ui::ScaleFactor scale_factor) { | |
377 LoadMap::iterator load_map_it = load_map_.find(id); | |
378 DCHECK(load_map_it != load_map_.end()); | |
379 PendingLoadInfo* load_info = &load_map_it->second; | |
380 | |
381 // Do nothing if extension is unloaded. | |
382 if (load_info->extension == NULL) | |
383 return; | |
384 | |
385 const float scale = ui::GetScaleFactorScale(scale_factor); | |
386 const int resource_size_in_pixel = | |
387 static_cast<int>(load_info->resource_size_in_dip * scale); | |
388 | |
389 ExtensionResource resource = load_info->extension->GetIconResource( | |
390 resource_size_in_pixel, | |
391 load_info->resource_match_type); | |
oshima
2012/07/18 15:16:28
you can move this to previous line.
xiyuan
2012/07/18 17:28:32
Done.
| |
392 | |
393 std::vector<ImageRepInfo> info_list; | |
394 info_list.push_back(ImageRepInfo(resource, | |
395 ImageRepInfo::ALWAYS_RESIZE, | |
396 load_info->desired_size_in_dip.Scale(scale), | |
397 scale_factor)); | |
398 DoLoadImage(id, info_list); | |
399 } | |
400 | |
272 bool ImageLoadingTracker::IsComponentExtensionResource( | 401 bool ImageLoadingTracker::IsComponentExtensionResource( |
273 const Extension* extension, | 402 const Extension* extension, |
274 const ExtensionResource& resource, | 403 const ExtensionResource& resource, |
275 int& resource_id) const { | 404 int& resource_id) const { |
276 if (extension->location() != Extension::COMPONENT) | 405 if (extension->location() != Extension::COMPONENT) |
277 return false; | 406 return false; |
278 | 407 |
279 FilePath directory_path = extension->path(); | 408 FilePath directory_path = extension->path(); |
280 FilePath relative_path = directory_path.BaseName().Append( | 409 FilePath relative_path = directory_path.BaseName().Append( |
281 resource.relative_path()); | 410 resource.relative_path()); |
282 | 411 |
283 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 412 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
284 FilePath resource_path = | 413 FilePath resource_path = |
285 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 414 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
286 resource_path = resource_path.NormalizePathSeparators(); | 415 resource_path = resource_path.NormalizePathSeparators(); |
287 | 416 |
288 if (relative_path == resource_path) { | 417 if (relative_path == resource_path) { |
289 resource_id = kComponentExtensionResources[i].value; | 418 resource_id = kComponentExtensionResources[i].value; |
290 return true; | 419 return true; |
291 } | 420 } |
292 } | 421 } |
293 return false; | 422 return false; |
294 } | 423 } |
295 | 424 |
296 void ImageLoadingTracker::OnImageLoaded( | 425 void ImageLoadingTracker::OnBitmapLoaded( |
297 SkBitmap* image, | 426 SkBitmap* bitmap, |
298 const ExtensionResource& resource, | 427 const ImageRepInfo& image_info, |
299 const gfx::Size& original_size, | 428 const gfx::Size& original_size, |
300 int id, | 429 int id, |
301 bool should_cache) { | 430 bool should_cache) { |
302 LoadMap::iterator load_map_it = load_map_.find(id); | 431 LoadMap::iterator load_map_it = load_map_.find(id); |
303 DCHECK(load_map_it != load_map_.end()); | 432 DCHECK(load_map_it != load_map_.end()); |
304 PendingLoadInfo* info = &load_map_it->second; | 433 PendingLoadInfo* info = &load_map_it->second; |
305 | 434 |
306 // Save the pending results. | 435 // Save the pending results. |
307 DCHECK(info->pending_count > 0); | 436 DCHECK_GT(info->pending_count, 0u); |
308 info->pending_count--; | 437 info->pending_count--; |
309 if (image) | 438 if (bitmap) { |
oshima
2012/07/18 15:16:28
how about add dcheck to make sure image_skia dones
xiyuan
2012/07/18 17:28:32
Should not do that because ImageLoadingTracker is
oshima
2012/07/18 20:41:49
Can you point me the code? We shouldn't add multip
xiyuan
2012/07/18 20:45:01
The one I mentioned is AppShortcutManager::Install
| |
310 info->bitmaps.push_back(*image); | 439 info->image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap, |
440 image_info.scale_factor)); | |
441 } | |
311 | 442 |
312 // Add to the extension's image cache if requested. | 443 // Add to the extension's bitmap cache if requested. |
313 DCHECK(info->cache != CACHE || info->extension); | 444 DCHECK(info->cache != CACHE || info->extension); |
314 if (should_cache && info->cache == CACHE && | 445 if (should_cache && info->cache == CACHE && |
315 !info->extension->HasCachedImage(resource, original_size)) { | 446 !info->extension->HasCachedImage(image_info.resource, original_size)) { |
316 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(), | 447 info->extension->SetCachedImage(image_info.resource, |
448 bitmap ? *bitmap : SkBitmap(), | |
317 original_size); | 449 original_size); |
318 } | 450 } |
319 | 451 |
320 // If all pending images are done then report back. | 452 // If all pending bitmaps/image reps are done then report back. |
321 if (info->pending_count == 0) { | 453 if (info->pending_count == 0) { |
322 gfx::Image image; | 454 gfx::Image image(info->image_skia); |
323 std::string extension_id = info->extension_id; | 455 std::string extension_id = info->extension_id; |
324 | 456 |
325 if (info->bitmaps.size() > 0) { | 457 if (info->image_source == NULL) |
326 gfx::ImageSkia image_skia; | 458 load_map_.erase(load_map_it); |
327 for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin(); | |
328 it != info->bitmaps.end(); ++it) { | |
329 // TODO(pkotwicz): Do something better but ONLY when DIP is enabled. | |
330 image_skia.AddRepresentation( | |
331 gfx::ImageSkiaRep(*it, ui::SCALE_FACTOR_100P)); | |
332 } | |
333 image = gfx::Image(image_skia); | |
334 } | |
335 | 459 |
336 load_map_.erase(load_map_it); | 460 // ImageLoadingTracker might be deleted after the callback so don't do |
337 | |
338 // ImageLoadingTracker might be deleted after the callback so don't | |
339 // anything after this statement. | 461 // anything after this statement. |
340 observer_->OnImageLoaded(image, extension_id, id); | 462 observer_->OnImageLoaded(image, extension_id, id); |
341 } | 463 } |
342 } | 464 } |
343 | 465 |
344 void ImageLoadingTracker::Observe(int type, | 466 void ImageLoadingTracker::Observe(int type, |
345 const content::NotificationSource& source, | 467 const content::NotificationSource& source, |
346 const content::NotificationDetails& details) { | 468 const content::NotificationDetails& details) { |
347 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 469 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
348 | 470 |
349 const Extension* extension = | 471 const Extension* extension = |
350 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 472 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
351 | 473 |
352 // Remove reference to this extension from all pending load entries. This | 474 // Remove reference to this extension from all pending load entries. This |
353 // ensures we don't attempt to cache the image when the load completes. | 475 // ensures we don't attempt to cache the bitmap when the load completes. |
354 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 476 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
355 PendingLoadInfo* info = &i->second; | 477 PendingLoadInfo* info = &i->second; |
356 if (info->extension == extension) { | 478 if (info->extension == extension) { |
357 info->extension = NULL; | 479 info->extension = NULL; |
358 info->cache = DONT_CACHE; | 480 info->cache = DONT_CACHE; |
359 } | 481 } |
360 } | 482 } |
361 } | 483 } |
OLD | NEW |