Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: chrome/browser/extensions/image_loading_tracker.cc

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

Powered by Google App Engine
This is Rietveld 408576698