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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 const ExtensionIconSet& icons, | 22 const ExtensionIconSet& icons, |
| 23 int size, | 23 int size, |
| 24 ExtensionIconSet::MatchType match_type) { | 24 ExtensionIconSet::MatchType match_type) { |
| 25 std::string path = icons.Get(size, match_type); | 25 std::string path = icons.Get(size, match_type); |
| 26 if (path.empty()) | 26 if (path.empty()) |
| 27 return ExtensionResource(); | 27 return ExtensionResource(); |
| 28 | 28 |
| 29 return extension->GetResource(path); | 29 return extension->GetResource(path); |
| 30 } | 30 } |
| 31 | 31 |
| 32 class BlankImageSource : public gfx::ImageSkiaSource { | |
| 33 public: | |
| 34 explicit BlankImageSource(const gfx::Size& size_in_dip) | |
| 35 : size_in_dip_(size_in_dip) { | |
| 36 } | |
| 37 virtual ~BlankImageSource() {} | |
| 38 | |
| 39 private: | |
| 40 // gfx::ImageSkiaSource overrides: | |
| 41 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 42 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 43 SkBitmap bitmap; | |
| 44 const float scale = ui::GetScaleFactorScale(scale_factor); | |
| 45 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.
| |
| 46 static_cast<int>(size_in_dip_.width() * scale), | |
| 47 static_cast<int>(size_in_dip_.height() * scale)); | |
| 48 bitmap.allocPixels(); | |
| 49 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); | |
| 50 return gfx::ImageSkiaRep(bitmap, scale_factor); | |
| 51 } | |
| 52 | |
| 53 const gfx::Size size_in_dip_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(BlankImageSource); | |
| 56 }; | |
| 57 | |
| 32 } // namespace | 58 } // namespace |
| 33 | 59 |
| 34 namespace extensions { | 60 namespace extensions { |
| 35 | 61 |
| 36 //////////////////////////////////////////////////////////////////////////////// | 62 //////////////////////////////////////////////////////////////////////////////// |
| 37 // ExtensionIconImage::Source | 63 // ExtensionIconImage::Source |
| 38 | 64 |
| 39 class IconImage::Source : public gfx::ImageSkiaSource { | 65 class IconImage::Source : public gfx::ImageSkiaSource { |
| 40 public: | 66 public: |
| 41 explicit Source(IconImage* host); | 67 Source(IconImage* host, const gfx::Size& size_in_dip); |
| 42 virtual ~Source(); | 68 virtual ~Source(); |
| 43 | 69 |
| 44 void ResetHost(); | 70 void ResetHost(); |
| 45 | 71 |
| 46 private: | 72 private: |
| 47 // gfx::ImageSkiaSource overrides: | 73 // gfx::ImageSkiaSource overrides: |
| 48 virtual gfx::ImageSkiaRep GetImageForScale( | 74 virtual gfx::ImageSkiaRep GetImageForScale( |
| 49 ui::ScaleFactor scale_factor) OVERRIDE; | 75 ui::ScaleFactor scale_factor) OVERRIDE; |
| 50 | 76 |
| 51 IconImage* host_; | 77 IconImage* host_; |
| 52 | 78 |
| 79 // Image whose representations will be used until |host_| load the real | |
| 80 // representations for the image. | |
| 81 gfx::ImageSkia blank_image_; | |
| 82 | |
| 53 DISALLOW_COPY_AND_ASSIGN(Source); | 83 DISALLOW_COPY_AND_ASSIGN(Source); |
| 54 }; | 84 }; |
| 55 | 85 |
| 56 IconImage::Source::Source(IconImage* host) : host_(host) { | 86 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) |
| 87 : host_(host), | |
| 88 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
| |
| 57 } | 89 } |
| 58 | 90 |
| 59 IconImage::Source::~Source() { | 91 IconImage::Source::~Source() { |
| 60 } | 92 } |
| 61 | 93 |
| 62 void IconImage::Source::ResetHost() { | 94 void IconImage::Source::ResetHost() { |
| 63 host_ = NULL; | 95 host_ = NULL; |
| 64 } | 96 } |
| 65 | 97 |
| 66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( | 98 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( |
| 67 ui::ScaleFactor scale_factor) { | 99 ui::ScaleFactor scale_factor) { |
| 100 gfx::ImageSkiaRep representation; | |
| 68 if (host_) | 101 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.
| |
| 69 host_->LoadImageForScaleFactor(scale_factor); | 102 representation = host_->LoadImageForScaleFactor(scale_factor); |
| 70 return gfx::ImageSkiaRep(); | 103 |
| 104 if (!representation.is_null()) | |
| 105 return representation; | |
| 106 | |
| 107 return blank_image_.GetRepresentation(scale_factor); | |
| 71 } | 108 } |
| 72 | 109 |
| 73 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |
| 74 // ExtensionIconImage | 111 // ExtensionIconImage |
| 75 | 112 |
| 76 IconImage::IconImage( | 113 IconImage::IconImage( |
| 77 const Extension* extension, | 114 const Extension* extension, |
| 78 const ExtensionIconSet& icon_set, | 115 const ExtensionIconSet& icon_set, |
| 79 int resource_size_in_dip, | 116 int resource_size_in_dip, |
| 117 const gfx::ImageSkia& default_icon, | |
| 80 Observer* observer) | 118 Observer* observer) |
| 81 : extension_(extension), | 119 : extension_(extension), |
| 82 icon_set_(icon_set), | 120 icon_set_(icon_set), |
| 83 resource_size_in_dip_(resource_size_in_dip), | 121 resource_size_in_dip_(resource_size_in_dip), |
| 84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), | |
| 85 observer_(observer), | 122 observer_(observer), |
| 86 source_(NULL), | 123 source_(NULL), |
| 124 default_icon_(default_icon), | |
| 87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | 125 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 88 source_ = new Source(this); | 126 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); |
| 89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); | 127 source_ = new Source(this, resource_size); |
| 128 image_skia_ = gfx::ImageSkia(source_, resource_size); | |
| 90 | 129 |
| 91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 130 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 92 content::NotificationService::AllSources()); | 131 content::NotificationService::AllSources()); |
| 93 } | 132 } |
| 94 | 133 |
| 95 IconImage::~IconImage() { | 134 IconImage::~IconImage() { |
| 96 // |source_| could be NULL if resource does not exist. | 135 // |source_| could be NULL if resource does not exist. |
|
Jeffrey Yasskin
2012/08/31 18:43:14
Could it? I see source_ initialized unconditionall
tbarzic
2012/08/31 22:29:44
yeah, that's a leftover from the initial represent
| |
| 97 if (source_) | 136 if (source_) |
| 98 source_->ResetHost(); | 137 source_->ResetHost(); |
| 99 } | 138 } |
| 100 | 139 |
| 101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { | 140 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( |
| 141 ui::ScaleFactor scale_factor) { | |
| 102 // Do nothing if extension is unloaded. | 142 // Do nothing if extension is unloaded. |
| 103 if (!extension_) | 143 if (!extension_) |
| 104 return; | 144 return gfx::ImageSkiaRep(); |
| 105 | 145 |
| 106 const float scale = ui::GetScaleFactorScale(scale_factor); | 146 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 107 const int resource_size_in_pixel = | 147 const int resource_size_in_pixel = |
| 108 static_cast<int>(resource_size_in_dip_ * scale); | 148 static_cast<int>(resource_size_in_dip_ * scale); |
| 109 | 149 |
| 110 ExtensionResource resource; | 150 ExtensionResource resource; |
| 111 // We try loading bigger image only if resource size is >= 32. | 151 // We try loading bigger image only if resource size is >= 32. |
| 112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { | 152 if (resource_size_in_pixel >= kMatchBiggerTreshold) { |
| 113 resource = GetExtensionIconResource(extension_, icon_set_, | 153 resource = GetExtensionIconResource(extension_, icon_set_, |
| 114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); | 154 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); |
| 115 } | 155 } |
| 116 | 156 |
| 117 // If resource is not found by now, try matching smaller one. | 157 // If resource is not found by now, try matching smaller one. |
| 118 if (resource.empty()) { | 158 if (resource.empty()) { |
| 119 resource = GetExtensionIconResource(extension_, icon_set_, | 159 resource = GetExtensionIconResource(extension_, icon_set_, |
| 120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); | 160 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); |
| 121 } | 161 } |
| 122 | 162 |
| 123 // If there is no resource found, bail out and notify observer of failure. | 163 // If there is no resource found, return default icon. |
| 124 if (resource.empty()) { | 164 if (resource.empty()) |
| 125 if (observer_) | 165 return default_icon_.GetRepresentation(scale_factor); |
| 126 observer_->OnIconImageLoadFailed(this, scale_factor); | |
| 127 return; | |
| 128 } | |
| 129 | 166 |
| 130 int id = tracker_.next_id(); | 167 int id = tracker_.next_id(); |
| 131 load_map_[id] = scale_factor; | 168 load_map_[id].scale_factor = scale_factor; |
| 169 load_map_[id].is_async = false; | |
| 132 | 170 |
| 133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 171 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| 134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( | 172 info_list.push_back(ImageLoadingTracker::ImageRepresentation( |
| 135 resource, | 173 resource, |
| 136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, | 174 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, |
| 137 desired_size_in_dip_.Scale(scale), | 175 gfx::Size(resource_size_in_dip_, resource_size_in_dip_).Scale(scale), |
| 138 scale_factor)); | 176 scale_factor)); |
| 139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); | 177 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); |
| 178 | |
| 179 // If we have not received |OnImageLoaded|, image load request is | |
| 180 // asynchronous. | |
| 181 if (load_map_.find(id) != load_map_.end()) | |
| 182 load_map_[id].is_async = true; | |
| 183 | |
| 184 // If LoadImages returned synchronously and the requested image rep is cached | |
| 185 // in the extension, return the cached image rep. | |
| 186 if (image_skia_.HasRepresentation(scale_factor)) | |
| 187 return image_skia_.GetRepresentation(scale_factor); | |
| 188 | |
| 189 return gfx::ImageSkiaRep(); | |
| 140 } | 190 } |
| 141 | 191 |
| 142 void IconImage::OnImageLoaded(const gfx::Image& image, | 192 void IconImage::OnImageLoaded(const gfx::Image& image_in, |
| 143 const std::string& extension_id, | 193 const std::string& extension_id, |
| 144 int index) { | 194 int index) { |
| 145 LoadMap::iterator load_map_it = load_map_.find(index); | 195 LoadMap::iterator load_map_it = load_map_.find(index); |
| 146 DCHECK(load_map_it != load_map_.end()); | 196 DCHECK(load_map_it != load_map_.end()); |
| 147 | 197 |
| 148 ui::ScaleFactor scale_factor = load_map_it->second; | 198 ui::ScaleFactor scale_factor = load_map_it->second.scale_factor; |
| 199 bool is_async = load_map_it->second.is_async; | |
| 149 | 200 |
| 150 load_map_.erase(load_map_it); | 201 load_map_.erase(load_map_it); |
| 151 | 202 |
| 152 if (image.IsEmpty()) { | 203 const gfx::ImageSkia* image = |
| 153 // There waas an error loading the image. | 204 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); |
| 154 if (observer_) | 205 |
| 155 observer_->OnIconImageLoadFailed(this, scale_factor); | 206 // Maybe default icon was not set. |
| 207 if (image->isNull()) | |
| 156 return; | 208 return; |
| 157 } | |
| 158 | 209 |
| 159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); | 210 // TODO(tbarzic): add DCHECK(image->HasRepresentation(scale_factor)) once |
| 160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); | 211 // 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
| |
| 212 // factors. | |
| 213 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor); | |
| 161 DCHECK(!rep.is_null()); | 214 DCHECK(!rep.is_null()); |
| 215 | |
| 216 // Remove old representation if there is one. | |
| 217 image_skia_.RemoveRepresentation(rep.scale_factor()); | |
| 162 image_skia_.AddRepresentation(rep); | 218 image_skia_.AddRepresentation(rep); |
| 163 | 219 |
| 164 if (observer_) | 220 // If |tracker_| called us synchronously the image did not really change from |
| 221 // the observers perspective, since the initial image representation is | |
| 222 // returned synchronously. | |
| 223 if (is_async && observer_) | |
| 165 observer_->OnExtensionIconImageChanged(this); | 224 observer_->OnExtensionIconImageChanged(this); |
| 166 } | 225 } |
| 167 | 226 |
| 168 void IconImage::Observe(int type, | 227 void IconImage::Observe(int type, |
| 169 const content::NotificationSource& source, | 228 const content::NotificationSource& source, |
| 170 const content::NotificationDetails& details) { | 229 const content::NotificationDetails& details) { |
| 171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 230 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 172 | 231 |
| 173 const Extension* extension = | 232 const Extension* extension = |
| 174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 233 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 175 | 234 |
| 176 if (extension_ == extension) | 235 if (extension_ == extension) |
| 177 extension_ = NULL; | 236 extension_ = NULL; |
| 178 } | 237 } |
| 179 | 238 |
| 180 } // namespace extensions | 239 } // namespace extensions |
| OLD | NEW |