Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/extension_icon_image.h" | 5 #include "extensions/browser/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 "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "extensions/browser/image_loader.h" | 12 #include "extensions/browser/image_loader.h" |
| 13 #include "extensions/browser/notification_types.h" | 13 #include "extensions/browser/notification_types.h" |
| 14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
| 15 #include "ui/base/layout.h" | 15 #include "ui/base/layout.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 18 #include "ui/gfx/geometry/size_conversions.h" | 18 #include "ui/gfx/geometry/size_conversions.h" |
| 19 #include "ui/gfx/image/canvas_image_source.h" | 19 #include "ui/gfx/image/canvas_image_source.h" |
| 20 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
| 21 #include "ui/gfx/image/image_skia_operations.h" | 21 #include "ui/gfx/image/image_skia_operations.h" |
| 22 #include "ui/gfx/image/image_skia_source.h" | 22 #include "ui/gfx/image/image_skia_source.h" |
| 23 | 23 |
| 24 // The ImageSkia provided by extensions::IconImage contains ImageSkiaReps that | 24 // The ImageSkia provided by extensions::IconImage contains ImageSkiaReps that |
| 25 // are computed and updated using the following algorithm (if no default icon | 25 // are computed and updated using the following algorithm (if no default icon |
| 26 // was supplied, transparent icon is considered the default): | 26 // was supplied, transparent icon is considered the default): |
| 27 // - |LoadImageForScaleFactors()| searches the extension for an icon of an | 27 // - |LoadImageForScale()| searches the extension for an icon of an |
| 28 // appropriate size. If the extension doesn't have a icon resource needed for | 28 // appropriate size. If the extension doesn't have a icon resource needed for |
| 29 // the image representation, the default icon's representation for the | 29 // the image representation, the default icon's representation for the |
| 30 // requested scale factor is returned by ImageSkiaSource. | 30 // requested scale factor is returned by ImageSkiaSource. |
| 31 // - If the extension has the resource, IconImage tries to load it using | 31 // - If the extension has the resource, IconImage tries to load it using |
| 32 // ImageLoader. | 32 // ImageLoader. |
| 33 // - |ImageLoader| is asynchronous. | 33 // - |ImageLoader| is asynchronous. |
| 34 // - ImageSkiaSource will initially return transparent image resource of the | 34 // - ImageSkiaSource will initially return transparent image resource of the |
| 35 // desired size. | 35 // desired size. |
| 36 // - The image will be updated with an appropriate image representation when | 36 // - The image will be updated with an appropriate image representation when |
| 37 // the |ImageLoader| finishes. The image representation is chosen the same | 37 // the |ImageLoader| finishes. The image representation is chosen the same |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 | 104 |
| 105 IconImage::Source::~Source() { | 105 IconImage::Source::~Source() { |
| 106 } | 106 } |
| 107 | 107 |
| 108 void IconImage::Source::ResetHost() { | 108 void IconImage::Source::ResetHost() { |
| 109 host_ = NULL; | 109 host_ = NULL; |
| 110 } | 110 } |
| 111 | 111 |
| 112 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(float scale) { | 112 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(float scale) { |
| 113 gfx::ImageSkiaRep representation; | 113 gfx::ImageSkiaRep representation; |
| 114 if (host_) { | 114 if (host_) |
| 115 representation = | 115 representation = host_->LoadImageForScale(scale); |
| 116 host_->LoadImageForScaleFactor(ui::GetSupportedScaleFactor(scale)); | |
| 117 } | |
| 118 | 116 |
| 119 if (!representation.is_null()) | 117 if (!representation.is_null()) |
| 120 return representation; | 118 return representation; |
| 121 | 119 |
| 122 return blank_image_.GetRepresentation(scale); | 120 return blank_image_.GetRepresentation(scale); |
| 123 } | 121 } |
| 124 | 122 |
| 125 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
| 126 // IconImage | 124 // IconImage |
| 127 | 125 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 void IconImage::RemoveObserver(Observer* observer) { | 159 void IconImage::RemoveObserver(Observer* observer) { |
| 162 observers_.RemoveObserver(observer); | 160 observers_.RemoveObserver(observer); |
| 163 } | 161 } |
| 164 | 162 |
| 165 IconImage::~IconImage() { | 163 IconImage::~IconImage() { |
| 166 for (auto& observer : observers_) | 164 for (auto& observer : observers_) |
| 167 observer.OnExtensionIconImageDestroyed(this); | 165 observer.OnExtensionIconImageDestroyed(this); |
| 168 source_->ResetHost(); | 166 source_->ResetHost(); |
| 169 } | 167 } |
| 170 | 168 |
| 171 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor( | 169 gfx::ImageSkiaRep IconImage::LoadImageForScale(float scale) { |
| 172 ui::ScaleFactor scale_factor) { | |
| 173 // Do nothing if extension is unloaded. | 170 // Do nothing if extension is unloaded. |
| 174 if (!extension_) | 171 if (!extension_) |
| 175 return gfx::ImageSkiaRep(); | 172 return gfx::ImageSkiaRep(); |
| 176 | 173 |
| 177 const float scale = ui::GetScaleForScaleFactor(scale_factor); | |
| 178 const int resource_size_in_pixel = | 174 const int resource_size_in_pixel = |
| 179 static_cast<int>(resource_size_in_dip_ * scale); | 175 static_cast<int>(resource_size_in_dip_ * scale); |
| 180 | 176 |
| 181 extensions::ExtensionResource resource; | 177 extensions::ExtensionResource resource; |
| 182 | 178 |
| 183 // Find extension resource for non bundled component extensions. | 179 // Find extension resource for non bundled component extensions. |
| 184 resource = | 180 resource = |
| 185 GetExtensionIconResource(*extension_, icon_set_, resource_size_in_pixel, | 181 GetExtensionIconResource(*extension_, icon_set_, resource_size_in_pixel, |
| 186 ExtensionIconSet::MATCH_BIGGER); | 182 ExtensionIconSet::MATCH_BIGGER); |
| 187 | 183 |
| 188 // If resource is not found by now, try matching smaller one. | 184 // If resource is not found by now, try matching smaller one. |
| 189 if (resource.empty()) { | 185 if (resource.empty()) { |
| 190 resource = | 186 resource = |
| 191 GetExtensionIconResource(*extension_, icon_set_, resource_size_in_pixel, | 187 GetExtensionIconResource(*extension_, icon_set_, resource_size_in_pixel, |
| 192 ExtensionIconSet::MATCH_SMALLER); | 188 ExtensionIconSet::MATCH_SMALLER); |
| 193 } | 189 } |
| 194 | 190 |
| 195 // If there is no resource found, return default icon. | 191 // If there is no resource found, return default icon. |
| 196 if (resource.empty()) | 192 if (resource.empty()) |
| 197 return default_icon_.GetRepresentation(scale); | 193 return default_icon_.GetRepresentation(scale); |
| 198 | 194 |
| 199 std::vector<ImageLoader::ImageRepresentation> info_list; | 195 std::vector<ImageLoader::ImageRepresentation> info_list; |
| 200 info_list.push_back(ImageLoader::ImageRepresentation( | 196 info_list.push_back(ImageLoader::ImageRepresentation( |
| 201 resource, ImageLoader::ImageRepresentation::ALWAYS_RESIZE, | 197 resource, ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| 202 gfx::ScaleToFlooredSize( | 198 gfx::Size(resource_size_in_pixel, resource_size_in_pixel), scale)); |
| 203 gfx::Size(resource_size_in_dip_, resource_size_in_dip_), scale), | |
| 204 scale_factor)); | |
|
Evan Stade
2017/01/07 01:17:38
the failure was due to scale_factor being an enum
Devlin
2017/01/09 21:58:33
As far as I know, this is fine, but oshima@ might
| |
| 205 | 199 |
| 206 extensions::ImageLoader* loader = | 200 extensions::ImageLoader* loader = |
| 207 extensions::ImageLoader::Get(browser_context_); | 201 extensions::ImageLoader::Get(browser_context_); |
| 208 loader->LoadImagesAsync(extension_.get(), info_list, | 202 loader->LoadImagesAsync(extension_.get(), info_list, |
| 209 base::Bind(&IconImage::OnImageLoaded, | 203 base::Bind(&IconImage::OnImageLoaded, |
| 210 weak_ptr_factory_.GetWeakPtr(), scale)); | 204 weak_ptr_factory_.GetWeakPtr(), scale)); |
| 211 | 205 |
| 212 return gfx::ImageSkiaRep(); | 206 return gfx::ImageSkiaRep(); |
| 213 } | 207 } |
| 214 | 208 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 const content::NotificationDetails& details) { | 251 const content::NotificationDetails& details) { |
| 258 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_REMOVED); | 252 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_REMOVED); |
| 259 | 253 |
| 260 const Extension* extension = content::Details<const Extension>(details).ptr(); | 254 const Extension* extension = content::Details<const Extension>(details).ptr(); |
| 261 | 255 |
| 262 if (extension_.get() == extension) | 256 if (extension_.get() == extension) |
| 263 extension_ = nullptr; | 257 extension_ = nullptr; |
| 264 } | 258 } |
| 265 | 259 |
| 266 } // namespace extensions | 260 } // namespace extensions |
| OLD | NEW |