| 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 "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 79 |
| 80 class IconImage::Source : public gfx::ImageSkiaSource { | 80 class IconImage::Source : public gfx::ImageSkiaSource { |
| 81 public: | 81 public: |
| 82 Source(IconImage* host, const gfx::Size& size_in_dip); | 82 Source(IconImage* host, const gfx::Size& size_in_dip); |
| 83 virtual ~Source(); | 83 virtual ~Source(); |
| 84 | 84 |
| 85 void ResetHost(); | 85 void ResetHost(); |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 // gfx::ImageSkiaSource overrides: | 88 // gfx::ImageSkiaSource overrides: |
| 89 virtual gfx::ImageSkiaRep GetImageForScale( | 89 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE; |
| 90 ui::ScaleFactor scale_factor) OVERRIDE; | |
| 91 | 90 |
| 92 // Used to load images, possibly asynchronously. NULLed out when the IconImage | 91 // Used to load images, possibly asynchronously. NULLed out when the IconImage |
| 93 // is destroyed. | 92 // is destroyed. |
| 94 IconImage* host_; | 93 IconImage* host_; |
| 95 | 94 |
| 96 // Image whose representations will be used until |host_| loads the real | 95 // Image whose representations will be used until |host_| loads the real |
| 97 // representations for the image. | 96 // representations for the image. |
| 98 gfx::ImageSkia blank_image_; | 97 gfx::ImageSkia blank_image_; |
| 99 | 98 |
| 100 DISALLOW_COPY_AND_ASSIGN(Source); | 99 DISALLOW_COPY_AND_ASSIGN(Source); |
| 101 }; | 100 }; |
| 102 | 101 |
| 103 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) | 102 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) |
| 104 : host_(host), | 103 : host_(host), |
| 105 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { | 104 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { |
| 106 } | 105 } |
| 107 | 106 |
| 108 IconImage::Source::~Source() { | 107 IconImage::Source::~Source() { |
| 109 } | 108 } |
| 110 | 109 |
| 111 void IconImage::Source::ResetHost() { | 110 void IconImage::Source::ResetHost() { |
| 112 host_ = NULL; | 111 host_ = NULL; |
| 113 } | 112 } |
| 114 | 113 |
| 115 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( | 114 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(float scale) { |
| 116 ui::ScaleFactor scale_factor) { | |
| 117 gfx::ImageSkiaRep representation; | 115 gfx::ImageSkiaRep representation; |
| 118 if (host_) | 116 if (host_) { |
| 119 representation = host_->LoadImageForScaleFactor(scale_factor); | 117 representation = |
| 118 host_->LoadImageForScaleFactor(ui::GetScaleFactorFromScale(scale)); |
| 119 } |
| 120 | 120 |
| 121 if (!representation.is_null()) | 121 if (!representation.is_null()) |
| 122 return representation; | 122 return representation; |
| 123 | 123 |
| 124 return blank_image_.GetRepresentation(scale_factor); | 124 return blank_image_.GetRepresentation(scale); |
| 125 } | 125 } |
| 126 | 126 |
| 127 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
| 128 // IconImage | 128 // IconImage |
| 129 | 129 |
| 130 IconImage::IconImage( | 130 IconImage::IconImage( |
| 131 Profile* profile, | 131 Profile* profile, |
| 132 const Extension* extension, | 132 const Extension* extension, |
| 133 const ExtensionIconSet& icon_set, | 133 const ExtensionIconSet& icon_set, |
| 134 int resource_size_in_dip, | 134 int resource_size_in_dip, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 177 } |
| 178 | 178 |
| 179 // If resource is not found by now, try matching smaller one. | 179 // If resource is not found by now, try matching smaller one. |
| 180 if (resource.empty()) { | 180 if (resource.empty()) { |
| 181 resource = GetExtensionIconResource(extension_, icon_set_, | 181 resource = GetExtensionIconResource(extension_, icon_set_, |
| 182 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); | 182 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); |
| 183 } | 183 } |
| 184 | 184 |
| 185 // If there is no resource found, return default icon. | 185 // If there is no resource found, return default icon. |
| 186 if (resource.empty()) | 186 if (resource.empty()) |
| 187 return default_icon_.GetRepresentation(scale_factor); | 187 return default_icon_.GetRepresentation(scale); |
| 188 | 188 |
| 189 std::vector<ImageLoader::ImageRepresentation> info_list; | 189 std::vector<ImageLoader::ImageRepresentation> info_list; |
| 190 info_list.push_back(ImageLoader::ImageRepresentation( | 190 info_list.push_back(ImageLoader::ImageRepresentation( |
| 191 resource, | 191 resource, |
| 192 ImageLoader::ImageRepresentation::ALWAYS_RESIZE, | 192 ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| 193 gfx::ToFlooredSize(gfx::ScaleSize( | 193 gfx::ToFlooredSize(gfx::ScaleSize( |
| 194 gfx::Size(resource_size_in_dip_, resource_size_in_dip_), scale)), | 194 gfx::Size(resource_size_in_dip_, resource_size_in_dip_), scale)), |
| 195 scale_factor)); | 195 scale_factor)); |
| 196 | 196 |
| 197 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile_); | 197 extensions::ImageLoader* loader = extensions::ImageLoader::Get(profile_); |
| 198 loader->LoadImagesAsync(extension_, info_list, | 198 loader->LoadImagesAsync(extension_, info_list, |
| 199 base::Bind(&IconImage::OnImageLoaded, | 199 base::Bind(&IconImage::OnImageLoaded, |
| 200 weak_ptr_factory_.GetWeakPtr(), | 200 weak_ptr_factory_.GetWeakPtr(), |
| 201 scale_factor)); | 201 scale)); |
| 202 | 202 |
| 203 return gfx::ImageSkiaRep(); | 203 return gfx::ImageSkiaRep(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void IconImage::OnImageLoaded(ui::ScaleFactor scale_factor, | 206 void IconImage::OnImageLoaded(float scale, const gfx::Image& image_in) { |
| 207 const gfx::Image& image_in) { | |
| 208 const gfx::ImageSkia* image = | 207 const gfx::ImageSkia* image = |
| 209 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); | 208 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); |
| 210 | 209 |
| 211 // Maybe default icon was not set. | 210 // Maybe default icon was not set. |
| 212 if (image->isNull()) | 211 if (image->isNull()) |
| 213 return; | 212 return; |
| 214 | 213 |
| 215 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor); | 214 gfx::ImageSkiaRep rep = image->GetRepresentation(scale); |
| 216 DCHECK(!rep.is_null()); | 215 DCHECK(!rep.is_null()); |
| 217 DCHECK_EQ(scale_factor, rep.scale_factor()); | 216 DCHECK_EQ(scale, rep.scale()); |
| 218 | 217 |
| 219 // Remove old representation if there is one. | 218 // Remove old representation if there is one. |
| 220 image_skia_.RemoveRepresentation(rep.scale_factor()); | 219 image_skia_.RemoveRepresentation(scale); |
| 221 image_skia_.AddRepresentation(rep); | 220 image_skia_.AddRepresentation(rep); |
| 222 | 221 |
| 223 if (observer_) | 222 if (observer_) |
| 224 observer_->OnExtensionIconImageChanged(this); | 223 observer_->OnExtensionIconImageChanged(this); |
| 225 } | 224 } |
| 226 | 225 |
| 227 void IconImage::Observe(int type, | 226 void IconImage::Observe(int type, |
| 228 const content::NotificationSource& source, | 227 const content::NotificationSource& source, |
| 229 const content::NotificationDetails& details) { | 228 const content::NotificationDetails& details) { |
| 230 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 229 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 231 | 230 |
| 232 const Extension* extension = | 231 const Extension* extension = |
| 233 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 232 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 234 | 233 |
| 235 if (extension_ == extension) | 234 if (extension_ == extension) |
| 236 extension_ = NULL; | 235 extension_ = NULL; |
| 237 } | 236 } |
| 238 | 237 |
| 239 } // namespace extensions | 238 } // namespace extensions |
| OLD | NEW |