Chromium Code Reviews| Index: chrome/browser/extensions/extension_icon_image.cc |
| diff --git a/chrome/browser/extensions/extension_icon_image.cc b/chrome/browser/extensions/extension_icon_image.cc |
| index dcd096815c03ab6c26653705b041539a73703acc..615e2be3bbfebc5a6deb373e43ffa5f68297e99e 100644 |
| --- a/chrome/browser/extensions/extension_icon_image.cc |
| +++ b/chrome/browser/extensions/extension_icon_image.cc |
| @@ -29,6 +29,32 @@ ExtensionResource GetExtensionIconResource( |
| return extension->GetResource(path); |
| } |
| +class BlankImageSource : public gfx::ImageSkiaSource { |
| + public: |
| + explicit BlankImageSource(const gfx::Size& size_in_dip) |
| + : size_in_dip_(size_in_dip) { |
| + } |
| + virtual ~BlankImageSource() {} |
| + |
| + private: |
| + // gfx::ImageSkiaSource overrides: |
| + virtual gfx::ImageSkiaRep GetImageForScale( |
| + ui::ScaleFactor scale_factor) OVERRIDE { |
| + SkBitmap bitmap; |
| + const float scale = ui::GetScaleFactorScale(scale_factor); |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
|
Jeffrey Yasskin
2012/08/31 18:43:14
Lots of boilerplate here. Would CanvasImageSource
tbarzic
2012/08/31 22:29:44
Done.
|
| + static_cast<int>(size_in_dip_.width() * scale), |
| + static_cast<int>(size_in_dip_.height() * scale)); |
| + bitmap.allocPixels(); |
| + bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
| + return gfx::ImageSkiaRep(bitmap, scale_factor); |
| + } |
| + |
| + const gfx::Size size_in_dip_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlankImageSource); |
| +}; |
| + |
| } // namespace |
| namespace extensions { |
| @@ -38,7 +64,7 @@ namespace extensions { |
| class IconImage::Source : public gfx::ImageSkiaSource { |
| public: |
| - explicit Source(IconImage* host); |
| + Source(IconImage* host, const gfx::Size& size_in_dip); |
| virtual ~Source(); |
| void ResetHost(); |
| @@ -50,10 +76,16 @@ class IconImage::Source : public gfx::ImageSkiaSource { |
| IconImage* host_; |
| + // Image whose representations will be used until |host_| load the real |
| + // representations for the image. |
| + gfx::ImageSkia blank_image_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(Source); |
| }; |
| -IconImage::Source::Source(IconImage* host) : host_(host) { |
| +IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) |
| + : host_(host), |
| + blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { |
|
Jeffrey Yasskin
2012/08/31 18:43:14
Why put blank here but default on the IconImage?
tbarzic
2012/08/31 22:29:44
I'd rather keep the default icon as an implementat
Jeffrey Yasskin
2012/09/01 01:10:27
The whole file is an implementation detail of Icon
|
| } |
| IconImage::Source::~Source() { |
| @@ -65,9 +97,14 @@ void IconImage::Source::ResetHost() { |
| gfx::ImageSkiaRep IconImage::Source::GetImageForScale( |
| ui::ScaleFactor scale_factor) { |
| + gfx::ImageSkiaRep representation; |
| if (host_) |
|
Jeffrey Yasskin
2012/08/31 18:43:14
How could host_ be null? Oooh: after the IconImage
tbarzic
2012/08/31 22:29:44
Done.
|
| - host_->LoadImageForScaleFactor(scale_factor); |
| - return gfx::ImageSkiaRep(); |
| + representation = host_->LoadImageForScaleFactor(scale_factor); |
| + |
| + if (!representation.is_null()) |
| + return representation; |
| + |
| + return blank_image_.GetRepresentation(scale_factor); |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -77,16 +114,18 @@ IconImage::IconImage( |
| const Extension* extension, |
| const ExtensionIconSet& icon_set, |
| int resource_size_in_dip, |
| + const gfx::ImageSkia& default_icon, |
| Observer* observer) |
| : extension_(extension), |
| icon_set_(icon_set), |
| resource_size_in_dip_(resource_size_in_dip), |
| - desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), |
| observer_(observer), |
| source_(NULL), |
| + default_icon_(default_icon), |
| ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| - source_ = new Source(this); |
| - image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); |
| + gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); |
| + source_ = new Source(this, resource_size); |
| + image_skia_ = gfx::ImageSkia(source_, resource_size); |
| registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| content::NotificationService::AllSources()); |
| @@ -98,10 +137,11 @@ IconImage::~IconImage() { |
| source_->ResetHost(); |
| } |
| -void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { |
| +gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( |
| + ui::ScaleFactor scale_factor) { |
| // Do nothing if extension is unloaded. |
| if (!extension_) |
| - return; |
| + return gfx::ImageSkiaRep(); |
| const float scale = ui::GetScaleFactorScale(scale_factor); |
| const int resource_size_in_pixel = |
| @@ -120,48 +160,67 @@ void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { |
| resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); |
| } |
| - // If there is no resource found, bail out and notify observer of failure. |
| - if (resource.empty()) { |
| - if (observer_) |
| - observer_->OnIconImageLoadFailed(this, scale_factor); |
| - return; |
| - } |
| + // If there is no resource found, return default icon. |
| + if (resource.empty()) |
| + return default_icon_.GetRepresentation(scale_factor); |
| int id = tracker_.next_id(); |
| - load_map_[id] = scale_factor; |
| + load_map_[id].scale_factor = scale_factor; |
| + load_map_[id].is_async = false; |
| std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| info_list.push_back(ImageLoadingTracker::ImageRepresentation( |
| resource, |
| ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, |
| - desired_size_in_dip_.Scale(scale), |
| + gfx::Size(resource_size_in_dip_, resource_size_in_dip_).Scale(scale), |
| scale_factor)); |
| tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); |
| + |
| + // If we have not received |OnImageLoaded|, image load request is |
| + // asynchronous. |
| + if (load_map_.find(id) != load_map_.end()) |
| + load_map_[id].is_async = true; |
| + |
| + // If LoadImages returned synchronously and the requested image rep is cached |
| + // in the extension, return the cached image rep. |
| + if (image_skia_.HasRepresentation(scale_factor)) |
| + return image_skia_.GetRepresentation(scale_factor); |
| + |
| + return gfx::ImageSkiaRep(); |
| } |
| -void IconImage::OnImageLoaded(const gfx::Image& image, |
| +void IconImage::OnImageLoaded(const gfx::Image& image_in, |
| const std::string& extension_id, |
| int index) { |
| LoadMap::iterator load_map_it = load_map_.find(index); |
| DCHECK(load_map_it != load_map_.end()); |
| - ui::ScaleFactor scale_factor = load_map_it->second; |
| + ui::ScaleFactor scale_factor = load_map_it->second.scale_factor; |
| + bool is_async = load_map_it->second.is_async; |
| load_map_.erase(load_map_it); |
| - if (image.IsEmpty()) { |
| - // There waas an error loading the image. |
| - if (observer_) |
| - observer_->OnIconImageLoadFailed(this, scale_factor); |
| + const gfx::ImageSkia* image = |
| + image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); |
| + |
| + // Maybe default icon was not set. |
| + if (image->isNull()) |
| return; |
| - } |
| - DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); |
| - gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); |
| + // TODO(tbarzic): add DCHECK(image->HasRepresentation(scale_factor)) once |
| + // we are certain that icons from resource bundle have all needed scale |
|
Jeffrey Yasskin
2012/08/31 18:43:14
Aren't these icons coming from extensions, where w
tbarzic
2012/08/31 22:29:44
yeah, but image loading tracker is supposed to ret
|
| + // factors. |
| + gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor); |
| DCHECK(!rep.is_null()); |
| + |
| + // Remove old representation if there is one. |
| + image_skia_.RemoveRepresentation(rep.scale_factor()); |
| image_skia_.AddRepresentation(rep); |
| - if (observer_) |
| + // If |tracker_| called us synchronously the image did not really change from |
| + // the observers perspective, since the initial image representation is |
| + // returned synchronously. |
| + if (is_async && observer_) |
| observer_->OnExtensionIconImageChanged(this); |
| } |