| 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(float scale) OVERRIDE; | 89 virtual gfx::ImageSkiaRep GetImageForScale( |
| 90 ui::ScaleFactor scale_factor) OVERRIDE; |
| 90 | 91 |
| 91 // Used to load images, possibly asynchronously. NULLed out when the IconImage | 92 // Used to load images, possibly asynchronously. NULLed out when the IconImage |
| 92 // is destroyed. | 93 // is destroyed. |
| 93 IconImage* host_; | 94 IconImage* host_; |
| 94 | 95 |
| 95 // Image whose representations will be used until |host_| loads the real | 96 // Image whose representations will be used until |host_| loads the real |
| 96 // representations for the image. | 97 // representations for the image. |
| 97 gfx::ImageSkia blank_image_; | 98 gfx::ImageSkia blank_image_; |
| 98 | 99 |
| 99 DISALLOW_COPY_AND_ASSIGN(Source); | 100 DISALLOW_COPY_AND_ASSIGN(Source); |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) | 103 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip) |
| 103 : host_(host), | 104 : host_(host), |
| 104 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { | 105 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) { |
| 105 } | 106 } |
| 106 | 107 |
| 107 IconImage::Source::~Source() { | 108 IconImage::Source::~Source() { |
| 108 } | 109 } |
| 109 | 110 |
| 110 void IconImage::Source::ResetHost() { | 111 void IconImage::Source::ResetHost() { |
| 111 host_ = NULL; | 112 host_ = NULL; |
| 112 } | 113 } |
| 113 | 114 |
| 114 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(float scale) { | 115 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( |
| 116 ui::ScaleFactor scale_factor) { |
| 115 gfx::ImageSkiaRep representation; | 117 gfx::ImageSkiaRep representation; |
| 116 if (host_) { | 118 if (host_) |
| 117 representation = | 119 representation = host_->LoadImageForScaleFactor(scale_factor); |
| 118 host_->LoadImageForScaleFactor(ui::GetSupportedScaleFactor(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); | 124 return blank_image_.GetRepresentation(scale_factor); |
| 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 21 matching lines...) Expand all Loading... |
| 156 IconImage::~IconImage() { | 156 IconImage::~IconImage() { |
| 157 source_->ResetHost(); | 157 source_->ResetHost(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( | 160 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( |
| 161 ui::ScaleFactor scale_factor) { | 161 ui::ScaleFactor scale_factor) { |
| 162 // Do nothing if extension is unloaded. | 162 // Do nothing if extension is unloaded. |
| 163 if (!extension_) | 163 if (!extension_) |
| 164 return gfx::ImageSkiaRep(); | 164 return gfx::ImageSkiaRep(); |
| 165 | 165 |
| 166 const float scale = ui::GetImageScale(scale_factor); | 166 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 167 const int resource_size_in_pixel = | 167 const int resource_size_in_pixel = |
| 168 static_cast<int>(resource_size_in_dip_ * scale); | 168 static_cast<int>(resource_size_in_dip_ * scale); |
| 169 | 169 |
| 170 extensions::ExtensionResource resource; | 170 extensions::ExtensionResource resource; |
| 171 | 171 |
| 172 // Find extension resource for non bundled component extensions. | 172 // Find extension resource for non bundled component extensions. |
| 173 // We try loading bigger image only if resource size is >= 32. | 173 // We try loading bigger image only if resource size is >= 32. |
| 174 if (resource_size_in_pixel >= kMatchBiggerTreshold) { | 174 if (resource_size_in_pixel >= kMatchBiggerTreshold) { |
| 175 resource = GetExtensionIconResource(extension_, icon_set_, | 175 resource = GetExtensionIconResource(extension_, icon_set_, |
| 176 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); | 176 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); |
| 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); | 187 return default_icon_.GetRepresentation(scale_factor); |
| 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)); | 201 scale_factor)); |
| 202 | 202 |
| 203 return gfx::ImageSkiaRep(); | 203 return gfx::ImageSkiaRep(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void IconImage::OnImageLoaded(float scale, const gfx::Image& image_in) { | 206 void IconImage::OnImageLoaded(ui::ScaleFactor scale_factor, |
| 207 const gfx::Image& image_in) { |
| 207 const gfx::ImageSkia* image = | 208 const gfx::ImageSkia* image = |
| 208 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); | 209 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia(); |
| 209 | 210 |
| 210 // Maybe default icon was not set. | 211 // Maybe default icon was not set. |
| 211 if (image->isNull()) | 212 if (image->isNull()) |
| 212 return; | 213 return; |
| 213 | 214 |
| 214 gfx::ImageSkiaRep rep = image->GetRepresentation(scale); | 215 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor); |
| 215 DCHECK(!rep.is_null()); | 216 DCHECK(!rep.is_null()); |
| 216 DCHECK_EQ(scale, rep.scale()); | 217 DCHECK_EQ(scale_factor, rep.scale_factor()); |
| 217 | 218 |
| 218 // Remove old representation if there is one. | 219 // Remove old representation if there is one. |
| 219 image_skia_.RemoveRepresentation(scale); | 220 image_skia_.RemoveRepresentation(rep.scale_factor()); |
| 220 image_skia_.AddRepresentation(rep); | 221 image_skia_.AddRepresentation(rep); |
| 221 | 222 |
| 222 if (observer_) | 223 if (observer_) |
| 223 observer_->OnExtensionIconImageChanged(this); | 224 observer_->OnExtensionIconImageChanged(this); |
| 224 } | 225 } |
| 225 | 226 |
| 226 void IconImage::Observe(int type, | 227 void IconImage::Observe(int type, |
| 227 const content::NotificationSource& source, | 228 const content::NotificationSource& source, |
| 228 const content::NotificationDetails& details) { | 229 const content::NotificationDetails& details) { |
| 229 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 230 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 230 | 231 |
| 231 const Extension* extension = | 232 const Extension* extension = |
| 232 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 233 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 233 | 234 |
| 234 if (extension_ == extension) | 235 if (extension_ == extension) |
| 235 extension_ = NULL; | 236 extension_ = NULL; |
| 236 } | 237 } |
| 237 | 238 |
| 238 } // namespace extensions | 239 } // namespace extensions |
| OLD | NEW |