Chromium Code Reviews| 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/extension_icon_image.h" | 5 #include "chrome/browser/extensions/extension_icon_image.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "ui/gfx/canvas.h" | |
| 13 #include "ui/gfx/image/canvas_image_source.h" | |
| 12 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
| 13 #include "ui/gfx/image/image_skia_source.h" | 15 #include "ui/gfx/image/image_skia_source.h" |
| 14 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 15 | 17 |
| 18 // The ImageSkia provided by extensions::IconImage contains ImageSkiaReps that | |
| 19 // are computed and updated using the following algorithm (if no default icon | |
| 20 // was supplied, transparent icon is considered the default): | |
| 21 // - |LoadImageForScaleFactors()| searches the extension for an icon of an | |
| 22 // appropriate size. If the extension doesn't have a icon resource needed for | |
| 23 // the image representation, the default icon's representation for the | |
| 24 // requested scale factor is returned by ImageSkiaSource. | |
| 25 // - If the extension has the resource, IconImage tries to load it using | |
| 26 // ImageLoadingTracker. | |
| 27 // - |ImageLoaderTracker| may return both synchronously and asynchronously. | |
| 28 // 1. |ImageLoaderTracker| is synchronous. | |
| 29 // - If image representation resource is successfully loaded, the | |
| 30 // representation returned by ImageSkiaSource is created from the loaded | |
| 31 // bitmap. | |
| 32 // - If resource loading fails, ImageSkiaSource returns default icon's | |
| 33 // representation. | |
| 34 // 2. |ImageLoadingTracker| is asynchronous. | |
| 35 // - ImageSkiaSource will initially return transparent image resource of the | |
| 36 // desired size. | |
| 37 // - The image will be updated with an appropriate image representation when | |
| 38 // the |ImageLoadingTracker| finishes. The image representation is chosen | |
| 39 // the same way as in the synchronous case. The observer is notified of the | |
| 40 // image change, unless the added image representation is transparent (in | |
| 41 // which case the image had already contained the appropriate image | |
| 42 // representation). | |
| 43 | |
| 16 namespace { | 44 namespace { |
| 17 | 45 |
| 18 const int kMatchBiggerTreshold = 32; | 46 const int kMatchBiggerTreshold = 32; |
| 19 | 47 |
| 20 ExtensionResource GetExtensionIconResource( | 48 ExtensionResource GetExtensionIconResource( |
| 21 const extensions::Extension* extension, | 49 const extensions::Extension* extension, |
| 22 const ExtensionIconSet& icons, | 50 const ExtensionIconSet& icons, |
| 23 int size, | 51 int size, |
| 24 ExtensionIconSet::MatchType match_type) { | 52 ExtensionIconSet::MatchType match_type) { |
| 25 std::string path = icons.Get(size, match_type); | 53 std::string path = icons.Get(size, match_type); |
| 26 if (path.empty()) | 54 if (path.empty()) |
| 27 return ExtensionResource(); | 55 return ExtensionResource(); |
| 28 | 56 |
| 29 return extension->GetResource(path); | 57 return extension->GetResource(path); |
| 30 } | 58 } |
| 31 | 59 |
| 60 class BlankImageSource : public gfx::CanvasImageSource { | |
| 61 public: | |
| 62 explicit BlankImageSource(const gfx::Size& size_in_dip) | |
| 63 : CanvasImageSource(size_in_dip, /*is_opaque =*/ false) { | |
| 64 } | |
| 65 virtual ~BlankImageSource() {} | |
| 66 | |
| 67 private: | |
| 68 // gfx::CanvasImageSource overrides: | |
| 69 virtual void Draw(gfx::Canvas* canvas) OVERRIDE { | |
| 70 canvas->DrawColor(SkColorSetARGB(0, 0, 0, 0)); | |
| 71 } | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(BlankImageSource); | |
| 74 }; | |
| 75 | |
| 32 } // namespace | 76 } // namespace |
| 33 | 77 |
| 34 namespace extensions { | 78 namespace extensions { |
| 35 | 79 |
| 36 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
| 37 // ExtensionIconImage::Source | 81 // ExtensionIconImage::Source |
| 38 | 82 |
| 39 class IconImage::Source : public gfx::ImageSkiaSource { | 83 class IconImage::Source : public gfx::ImageSkiaSource { |
| 40 public: | 84 public: |
| 41 explicit Source(IconImage* host); | 85 Source(IconImage* host, const gfx::Size& size_in_dip); |
| 42 virtual ~Source(); | 86 virtual ~Source(); |
| 43 | 87 |
| 44 void ResetHost(); | 88 void ResetHost(); |
| 45 | 89 |
| 46 private: | 90 private: |
| 47 // gfx::ImageSkiaSource overrides: | 91 // gfx::ImageSkiaSource overrides: |
| 48 virtual gfx::ImageSkiaRep GetImageForScale( | 92 virtual gfx::ImageSkiaRep GetImageForScale( |
| 49 ui::ScaleFactor scale_factor) OVERRIDE; | 93 ui::ScaleFactor scale_factor) OVERRIDE; |
| 50 | 94 |
| 95 // Used to load images, possibly asynchronously. NULLed out when the IconImage | |
| 96 // is destroyed. | |
| 51 IconImage* host_; | 97 IconImage* host_; |
| 52 | 98 |
| 99 // Image whose representations will be used until |host_| load the real | |
| 100 // representations for the image. | |
| 101 gfx::ImageSkia blank_image_; | |
| 102 | |
| 53 DISALLOW_COPY_AND_ASSIGN(Source); | 103 DISALLOW_COPY_AND_ASSIGN(Source); |
| 54 }; | 104 }; |
| 55 | 105 |
| 56 IconImage::Source::Source(IconImage* host) : host_(host) { | 106 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) |
| 107 : host_(host), | |
| 108 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { | |
| 57 } | 109 } |
| 58 | 110 |
| 59 IconImage::Source::~Source() { | 111 IconImage::Source::~Source() { |
| 60 } | 112 } |
| 61 | 113 |
| 62 void IconImage::Source::ResetHost() { | 114 void IconImage::Source::ResetHost() { |
| 63 host_ = NULL; | 115 host_ = NULL; |
| 64 } | 116 } |
| 65 | 117 |
| 66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( | 118 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( |
| 67 ui::ScaleFactor scale_factor) { | 119 ui::ScaleFactor scale_factor) { |
| 120 gfx::ImageSkiaRep representation; | |
| 68 if (host_) | 121 if (host_) |
| 69 host_->LoadImageForScaleFactor(scale_factor); | 122 representation = host_->LoadImageForScaleFactor(scale_factor); |
| 70 return gfx::ImageSkiaRep(); | 123 |
| 124 if (!representation.is_null()) | |
| 125 return representation; | |
| 126 | |
| 127 return blank_image_.GetRepresentation(scale_factor); | |
| 71 } | 128 } |
| 72 | 129 |
| 73 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
| 74 // ExtensionIconImage | 131 // ExtensionIconImage |
| 75 | 132 |
| 76 IconImage::IconImage( | 133 IconImage::IconImage( |
| 77 const Extension* extension, | 134 const Extension* extension, |
| 78 const ExtensionIconSet& icon_set, | 135 const ExtensionIconSet& icon_set, |
| 79 int resource_size_in_dip, | 136 int resource_size_in_dip, |
| 137 const gfx::ImageSkia& default_icon, | |
| 80 Observer* observer) | 138 Observer* observer) |
| 81 : extension_(extension), | 139 : extension_(extension), |
| 82 icon_set_(icon_set), | 140 icon_set_(icon_set), |
| 83 resource_size_in_dip_(resource_size_in_dip), | 141 resource_size_in_dip_(resource_size_in_dip), |
| 84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), | |
| 85 observer_(observer), | 142 observer_(observer), |
| 86 source_(NULL), | 143 source_(NULL), |
| 144 default_icon_(default_icon), | |
| 87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | 145 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 88 source_ = new Source(this); | 146 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); |
| 89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); | 147 source_ = new Source(this, resource_size); |
| 148 image_skia_ = gfx::ImageSkia(source_, resource_size); | |
| 90 | 149 |
| 91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 150 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 92 content::NotificationService::AllSources()); | 151 content::NotificationService::AllSources()); |
| 93 } | 152 } |
| 94 | 153 |
| 95 IconImage::~IconImage() { | 154 IconImage::~IconImage() { |
| 96 // |source_| could be NULL if resource does not exist. | 155 source_->ResetHost(); |
| 97 if (source_) | |
| 98 source_->ResetHost(); | |
| 99 } | 156 } |
| 100 | 157 |
| 101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { | 158 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( |
| 159 ui::ScaleFactor scale_factor) { | |
| 102 // Do nothing if extension is unloaded. | 160 // Do nothing if extension is unloaded. |
| 103 if (!extension_) | 161 if (!extension_) |
| 104 return; | 162 return gfx::ImageSkiaRep(); |
| 105 | 163 |
| 106 const float scale = ui::GetScaleFactorScale(scale_factor); | 164 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 107 const int resource_size_in_pixel = | 165 const int resource_size_in_pixel = |
| 108 static_cast<int>(resource_size_in_dip_ * scale); | 166 static_cast<int>(resource_size_in_dip_ * scale); |
| 109 | 167 |
| 110 ExtensionResource resource; | 168 ExtensionResource resource; |
| 111 // We try loading bigger image only if resource size is >= 32. | 169 // We try loading bigger image only if resource size is >= 32. |
| 112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { | 170 if (resource_size_in_pixel >= kMatchBiggerTreshold) { |
| 113 resource = GetExtensionIconResource(extension_, icon_set_, | 171 resource = GetExtensionIconResource(extension_, icon_set_, |
| 114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); | 172 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); |
| 115 } | 173 } |
| 116 | 174 |
| 117 // If resource is not found by now, try matching smaller one. | 175 // If resource is not found by now, try matching smaller one. |
| 118 if (resource.empty()) { | 176 if (resource.empty()) { |
| 119 resource = GetExtensionIconResource(extension_, icon_set_, | 177 resource = GetExtensionIconResource(extension_, icon_set_, |
| 120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); | 178 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); |
| 121 } | 179 } |
| 122 | 180 |
| 123 // If there is no resource found, bail out and notify observer of failure. | 181 // If there is no resource found, return default icon. |
| 124 if (resource.empty()) { | 182 if (resource.empty()) |
| 125 if (observer_) | 183 return default_icon_.GetRepresentation(scale_factor); |
| 126 observer_->OnIconImageLoadFailed(this, scale_factor); | |
| 127 return; | |
| 128 } | |
| 129 | 184 |
| 130 int id = tracker_.next_id(); | 185 int id = tracker_.next_id(); |
| 131 load_map_[id] = scale_factor; | 186 load_map_[id].scale_factor = scale_factor; |
| 187 load_map_[id].is_async = false; | |
| 132 | 188 |
| 133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 189 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| 134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | 190 info_list.push_back(ImageLoadingTracker::ImageRepresentation( |
| 135 resource, | 191 resource, |
| 136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, | 192 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, |
| 137 desired_size_in_dip_.Scale(scale), | 193 gfx::Size(resource_size_in_dip_, resource_size_in_dip_).Scale(scale), |
| 138 scale_factor)); | 194 scale_factor)); |
| 139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); | 195 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); |
| 196 | |
| 197 // If we have not received |OnImageLoaded|, image load request is | |
| 198 // asynchronous. | |
| 199 if (load_map_.find(id) != load_map_.end()) | |
| 200 load_map_[id].is_async = true; | |
| 201 | |
| 202 // If LoadImages returned synchronously and the requested image rep is cached | |
| 203 // in the extension, return the cached image rep. | |
| 204 if (image_skia_.HasRepresentation(scale_factor)) | |
| 205 return image_skia_.GetRepresentation(scale_factor); | |
| 206 | |
| 207 return gfx::ImageSkiaRep(); | |
| 140 } | 208 } |
| 141 | 209 |
| 142 void IconImage::OnImageLoaded(const gfx::Image& image, | 210 void IconImage::OnImageLoaded(const gfx::Image& image_in, |
| 143 const std::string& extension_id, | 211 const std::string& extension_id, |
| 144 int index) { | 212 int index) { |
| 145 LoadMap::iterator load_map_it = load_map_.find(index); | 213 LoadMap::iterator load_map_it = load_map_.find(index); |
| 146 DCHECK(load_map_it != load_map_.end()); | 214 DCHECK(load_map_it != load_map_.end()); |
| 147 | 215 |
| 148 ui::ScaleFactor scale_factor = load_map_it->second; | 216 ui::ScaleFactor scale_factor = load_map_it->second.scale_factor; |
| 217 bool is_async = load_map_it->second.is_async; | |
| 149 | 218 |
| 150 load_map_.erase(load_map_it); | 219 load_map_.erase(load_map_it); |
| 151 | 220 |
| 152 if (image.IsEmpty()) { | 221 const gfx::ImageSkia* image = |
| 153 // There waas an error loading the image. | 222 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); |
| 154 if (observer_) | 223 |
| 155 observer_->OnIconImageLoadFailed(this, scale_factor); | 224 // Maybe default icon was not set. |
| 225 if (image->isNull()) | |
| 156 return; | 226 return; |
| 157 } | |
| 158 | 227 |
| 159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); | 228 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor); |
| 160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | |
| 161 DCHECK(!rep.is_null()); | 229 DCHECK(!rep.is_null()); |
| 230 DCHECK_EQ(scale_factor, rep.scale_factor()); | |
|
Jeffrey Yasskin
2012/09/05 23:14:49
Great, thanks. I think you're still missing a test
tbarzic
2012/09/06 00:39:53
sorry, forgot to upload patch with the test..
| |
| 231 | |
| 232 // Remove old representation if there is one. | |
| 233 image_skia_.RemoveRepresentation(rep.scale_factor()); | |
| 162 image_skia_.AddRepresentation(rep); | 234 image_skia_.AddRepresentation(rep); |
| 163 | 235 |
| 164 if (observer_) | 236 // If |tracker_| called us synchronously the image did not really change from |
| 237 // the observers perspective, since the initial image representation is | |
| 238 // returned synchronously. | |
| 239 if (is_async && observer_) | |
| 165 observer_->OnExtensionIconImageChanged(this); | 240 observer_->OnExtensionIconImageChanged(this); |
| 166 } | 241 } |
| 167 | 242 |
| 168 void IconImage::Observe(int type, | 243 void IconImage::Observe(int type, |
| 169 const content::NotificationSource& source, | 244 const content::NotificationSource& source, |
| 170 const content::NotificationDetails& details) { | 245 const content::NotificationDetails& details) { |
| 171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 246 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 172 | 247 |
| 173 const Extension* extension = | 248 const Extension* extension = |
| 174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 249 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 175 | 250 |
| 176 if (extension_ == extension) | 251 if (extension_ == extension) |
| 177 extension_ = NULL; | 252 extension_ = NULL; |
| 178 } | 253 } |
| 179 | 254 |
| 180 } // namespace extensions | 255 } // namespace extensions |
| OLD | NEW |