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

Unified Diff: chrome/browser/extensions/extension_icon_image.cc

Issue 10861034: Supply default icon to extension icon image. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
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..b0087c0cbddb84cc61dd6c68c1efaa1314a3f263 100644
--- a/chrome/browser/extensions/extension_icon_image.cc
+++ b/chrome/browser/extensions/extension_icon_image.cc
@@ -29,6 +29,30 @@ ExtensionResource GetExtensionIconResource(
return extension->GetResource(path);
}
+class BlankImageSource : public gfx::ImageSkiaSource {
+ public:
+ explicit BlankImageSource(int 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,
+ static_cast<int>(size_in_dip_ * scale),
+ static_cast<int>(size_in_dip_ * scale));
+ bitmap.allocPixels();
+ bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0));
+ return gfx::ImageSkiaRep(bitmap, scale_factor);
+ }
+
+ int size_in_dip_;
+
+ DISALLOW_COPY_AND_ASSIGN(BlankImageSource);
+};
+
} // namespace
namespace extensions {
@@ -38,7 +62,7 @@ namespace extensions {
class IconImage::Source : public gfx::ImageSkiaSource {
public:
- explicit Source(IconImage* host);
+ Source(IconImage* host, int size_in_dip);
virtual ~Source();
void ResetHost();
@@ -50,10 +74,17 @@ 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, int size_in_dip)
+ : host_(host),
+ blank_image_(new BlankImageSource(size_in_dip),
+ gfx::Size(size_in_dip, size_in_dip)) {
}
IconImage::Source::~Source() {
@@ -65,9 +96,14 @@ void IconImage::Source::ResetHost() {
gfx::ImageSkiaRep IconImage::Source::GetImageForScale(
ui::ScaleFactor scale_factor) {
+ gfx::ImageSkiaRep representation;
if (host_)
- 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,6 +113,7 @@ 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),
@@ -84,8 +121,9 @@ IconImage::IconImage(
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);
+ source_ = new Source(this, resource_size_in_dip);
pkotwicz 2012/08/28 18:56:57 Nit 1: I find it confusing to have both desired_si
tbarzic 2012/08/29 18:41:28 Done.
image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_);
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
@@ -98,10 +136,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,15 +159,13 @@ 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(
@@ -137,31 +174,52 @@ void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) {
desired_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 LaodImages returned synchronously and the requested image rep is cached
pkotwicz 2012/08/28 18:56:57 Nit: LoadImages()
tbarzic 2012/08/29 18:41:28 Done.
+ // 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'll be certain that icons from resource bundle have all needed scale
pkotwicz 2012/08/28 18:56:57 Nit: we'll be -> we are
tbarzic 2012/08/29 18:41:28 Done.
+ // 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);
}

Powered by Google App Engine
This is Rietveld 408576698