| 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/image_loader.h" | 5 #include "extensions/browser/image_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/browser/component_extension_resource_manager.h" | 18 #include "extensions/browser/component_extension_resource_manager.h" |
| 19 #include "extensions/browser/extensions_browser_client.h" | 19 #include "extensions/browser/extensions_browser_client.h" |
| 20 #include "extensions/browser/image_loader_factory.h" | 20 #include "extensions/browser/image_loader_factory.h" |
| 21 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| 22 #include "extensions/common/manifest_handlers/icons_handler.h" | 22 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 23 #include "skia/ext/image_operations.h" | 23 #include "skia/ext/image_operations.h" |
| 24 #include "ui/base/layout.h" | 24 #include "ui/base/layout.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/display/display.h" |
| 27 #include "ui/display/screen.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/image/image_family.h" | 29 #include "ui/gfx/image/image_family.h" |
| 28 #include "ui/gfx/image/image_skia.h" | 30 #include "ui/gfx/image/image_skia.h" |
| 29 | 31 |
| 30 using content::BrowserThread; | 32 using content::BrowserThread; |
| 31 | 33 |
| 32 namespace extensions { | 34 namespace extensions { |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 130 |
| 129 } // namespace | 131 } // namespace |
| 130 | 132 |
| 131 //////////////////////////////////////////////////////////////////////////////// | 133 //////////////////////////////////////////////////////////////////////////////// |
| 132 // ImageLoader::ImageRepresentation | 134 // ImageLoader::ImageRepresentation |
| 133 | 135 |
| 134 ImageLoader::ImageRepresentation::ImageRepresentation( | 136 ImageLoader::ImageRepresentation::ImageRepresentation( |
| 135 const ExtensionResource& resource, | 137 const ExtensionResource& resource, |
| 136 ResizeCondition resize_condition, | 138 ResizeCondition resize_condition, |
| 137 const gfx::Size& desired_size, | 139 const gfx::Size& desired_size, |
| 138 ui::ScaleFactor scale_factor) | 140 float scale_factor) |
| 139 : resource(resource), | 141 : resource(resource), |
| 140 resize_condition(resize_condition), | 142 resize_condition(resize_condition), |
| 141 desired_size(desired_size), | 143 desired_size(desired_size), |
| 142 scale_factor(scale_factor) { | 144 scale_factor(scale_factor) {} |
| 143 } | |
| 144 | 145 |
| 145 ImageLoader::ImageRepresentation::~ImageRepresentation() { | 146 ImageLoader::ImageRepresentation::~ImageRepresentation() { |
| 146 } | 147 } |
| 147 | 148 |
| 148 //////////////////////////////////////////////////////////////////////////////// | 149 //////////////////////////////////////////////////////////////////////////////// |
| 149 // ImageLoader::LoadResult | 150 // ImageLoader::LoadResult |
| 150 | 151 |
| 151 struct ImageLoader::LoadResult { | 152 struct ImageLoader::LoadResult { |
| 152 LoadResult(const SkBitmap& bitmap, | 153 LoadResult(const SkBitmap& bitmap, |
| 153 const gfx::Size& original_size, | 154 const gfx::Size& original_size, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { | 224 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { |
| 224 return ImageLoaderFactory::GetForBrowserContext(context); | 225 return ImageLoaderFactory::GetForBrowserContext(context); |
| 225 } | 226 } |
| 226 | 227 |
| 227 void ImageLoader::LoadImageAsync(const Extension* extension, | 228 void ImageLoader::LoadImageAsync(const Extension* extension, |
| 228 const ExtensionResource& resource, | 229 const ExtensionResource& resource, |
| 229 const gfx::Size& max_size, | 230 const gfx::Size& max_size, |
| 230 const ImageLoaderImageCallback& callback) { | 231 const ImageLoaderImageCallback& callback) { |
| 231 std::vector<ImageRepresentation> info_list; | 232 std::vector<ImageRepresentation> info_list; |
| 232 info_list.push_back(ImageRepresentation( | 233 info_list.push_back(ImageRepresentation( |
| 233 resource, | 234 resource, ImageRepresentation::RESIZE_WHEN_LARGER, max_size, 1.f)); |
| 234 ImageRepresentation::RESIZE_WHEN_LARGER, | |
| 235 max_size, | |
| 236 ui::SCALE_FACTOR_100P)); | |
| 237 LoadImagesAsync(extension, info_list, callback); | 235 LoadImagesAsync(extension, info_list, callback); |
| 238 } | 236 } |
| 239 | 237 |
| 240 void ImageLoader::LoadImageAtEveryScaleFactorAsync( | 238 void ImageLoader::LoadImageAtEveryScaleFactorAsync( |
| 241 const Extension* extension, | 239 const Extension* extension, |
| 242 const gfx::Size& dip_size, | 240 const gfx::Size& dip_size, |
| 243 const ImageLoaderImageCallback& callback) { | 241 const ImageLoaderImageCallback& callback) { |
| 244 std::vector<ImageRepresentation> info_list; | 242 std::vector<ImageRepresentation> info_list; |
| 245 for (auto scale : ui::GetSupportedScaleFactors()) { | 243 |
| 246 const float scale_factor = ui::GetScaleForScaleFactor(scale); | 244 std::set<float> scales; |
| 247 const gfx::Size px_size = gfx::ScaleToFlooredSize(dip_size, scale_factor); | 245 for (auto scale : ui::GetSupportedScaleFactors()) |
| 246 scales.insert(ui::GetScaleForScaleFactor(scale)); |
| 247 |
| 248 // There may not be a screen in unit tests. |
| 249 auto screen = display::Screen::GetScreen(); |
| 250 if (screen) { |
| 251 for (const auto& display : screen->GetAllDisplays()) |
| 252 scales.insert(display.device_scale_factor()); |
| 253 } |
| 254 |
| 255 for (auto scale : scales) { |
| 256 const gfx::Size px_size = gfx::ScaleToFlooredSize(dip_size, scale); |
| 248 ExtensionResource image = IconsInfo::GetIconResource( | 257 ExtensionResource image = IconsInfo::GetIconResource( |
| 249 extension, px_size.width(), ExtensionIconSet::MATCH_BIGGER); | 258 extension, px_size.width(), ExtensionIconSet::MATCH_BIGGER); |
| 250 info_list.push_back(ImageRepresentation( | 259 info_list.push_back(ImageRepresentation( |
| 251 image, ImageRepresentation::ALWAYS_RESIZE, px_size, scale)); | 260 image, ImageRepresentation::ALWAYS_RESIZE, px_size, scale)); |
| 252 } | 261 } |
| 253 LoadImagesAsync(extension, info_list, callback); | 262 LoadImagesAsync(extension, info_list, callback); |
| 254 } | 263 } |
| 255 | 264 |
| 256 void ImageLoader::LoadImagesAsync( | 265 void ImageLoader::LoadImagesAsync( |
| 257 const Extension* extension, | 266 const Extension* extension, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 const std::vector<LoadResult>& load_result) { | 299 const std::vector<LoadResult>& load_result) { |
| 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 300 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 292 | 301 |
| 293 gfx::ImageSkia image_skia; | 302 gfx::ImageSkia image_skia; |
| 294 | 303 |
| 295 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | 304 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); |
| 296 it != load_result.end(); ++it) { | 305 it != load_result.end(); ++it) { |
| 297 const SkBitmap& bitmap = it->bitmap; | 306 const SkBitmap& bitmap = it->bitmap; |
| 298 const ImageRepresentation& image_rep = it->image_representation; | 307 const ImageRepresentation& image_rep = it->image_representation; |
| 299 | 308 |
| 300 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 309 image_skia.AddRepresentation( |
| 301 bitmap, | 310 gfx::ImageSkiaRep(bitmap, image_rep.scale_factor)); |
| 302 ui::GetScaleForScaleFactor(image_rep.scale_factor))); | |
| 303 } | 311 } |
| 304 | 312 |
| 305 gfx::Image image; | 313 gfx::Image image; |
| 306 if (!image_skia.isNull()) { | 314 if (!image_skia.isNull()) { |
| 307 image_skia.MakeThreadSafe(); | 315 image_skia.MakeThreadSafe(); |
| 308 image = gfx::Image(image_skia); | 316 image = gfx::Image(image_skia); |
| 309 } | 317 } |
| 310 | 318 |
| 311 callback.Run(image); | 319 callback.Run(image); |
| 312 } | 320 } |
| 313 | 321 |
| 314 void ImageLoader::ReplyBackWithImageFamily( | 322 void ImageLoader::ReplyBackWithImageFamily( |
| 315 const ImageLoaderImageFamilyCallback& callback, | 323 const ImageLoaderImageFamilyCallback& callback, |
| 316 const std::vector<LoadResult>& load_result) { | 324 const std::vector<LoadResult>& load_result) { |
| 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 325 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 318 | 326 |
| 319 std::map<std::pair<int, int>, gfx::ImageSkia> image_skia_map; | 327 std::map<std::pair<int, int>, gfx::ImageSkia> image_skia_map; |
| 320 gfx::ImageFamily image_family; | 328 gfx::ImageFamily image_family; |
| 321 | 329 |
| 322 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | 330 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); |
| 323 it != load_result.end(); | 331 it != load_result.end(); |
| 324 ++it) { | 332 ++it) { |
| 325 const SkBitmap& bitmap = it->bitmap; | 333 const SkBitmap& bitmap = it->bitmap; |
| 326 const ImageRepresentation& image_rep = it->image_representation; | 334 const ImageRepresentation& image_rep = it->image_representation; |
| 327 const std::pair<int, int> key = std::make_pair( | 335 const std::pair<int, int> key = std::make_pair( |
| 328 image_rep.desired_size.width(), image_rep.desired_size.height()); | 336 image_rep.desired_size.width(), image_rep.desired_size.height()); |
| 329 // Create a new ImageSkia for this width/height, or add a representation to | 337 // Create a new ImageSkia for this width/height, or add a representation to |
| 330 // an existing ImageSkia with the same width/height. | 338 // an existing ImageSkia with the same width/height. |
| 331 image_skia_map[key].AddRepresentation( | 339 image_skia_map[key].AddRepresentation( |
| 332 gfx::ImageSkiaRep(bitmap, | 340 gfx::ImageSkiaRep(bitmap, image_rep.scale_factor)); |
| 333 ui::GetScaleForScaleFactor(image_rep.scale_factor))); | |
| 334 } | 341 } |
| 335 | 342 |
| 336 for (std::map<std::pair<int, int>, gfx::ImageSkia>::iterator it = | 343 for (std::map<std::pair<int, int>, gfx::ImageSkia>::iterator it = |
| 337 image_skia_map.begin(); | 344 image_skia_map.begin(); |
| 338 it != image_skia_map.end(); | 345 it != image_skia_map.end(); |
| 339 ++it) { | 346 ++it) { |
| 340 it->second.MakeThreadSafe(); | 347 it->second.MakeThreadSafe(); |
| 341 image_family.Add(it->second); | 348 image_family.Add(it->second); |
| 342 } | 349 } |
| 343 | 350 |
| 344 callback.Run(image_family); | 351 callback.Run(image_family); |
| 345 } | 352 } |
| 346 | 353 |
| 347 } // namespace extensions | 354 } // namespace extensions |
| OLD | NEW |