| 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_manager.h" | 5 #include "chrome/browser/extensions/extension_icon_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "extensions/browser/image_loader.h" | 10 #include "extensions/browser/image_loader.h" |
| 11 #include "extensions/common/constants.h" | 11 #include "extensions/common/constants.h" |
| 12 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
| 13 #include "extensions/common/extension_icon_set.h" | 13 #include "extensions/common/extension_icon_set.h" |
| 14 #include "extensions/common/extension_resource.h" | 14 #include "extensions/common/extension_resource.h" |
| 15 #include "extensions/common/manifest_handlers/icons_handler.h" | 15 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 16 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/color_palette.h" | 19 #include "ui/gfx/color_palette.h" |
| 20 #include "ui/gfx/color_utils.h" | 20 #include "ui/gfx/color_utils.h" |
| 21 #include "ui/gfx/favicon_size.h" | 21 #include "ui/gfx/favicon_size.h" |
| 22 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 24 #include "ui/gfx/image/image_skia_operations.h" |
| 24 #include "ui/gfx/paint_vector_icon.h" | 25 #include "ui/gfx/paint_vector_icon.h" |
| 25 #include "ui/gfx/skbitmap_operations.h" | |
| 26 #include "ui/gfx/vector_icons_public.h" | 26 #include "ui/gfx/vector_icons_public.h" |
| 27 #include "ui/native_theme/common_theme.h" | 27 #include "ui/native_theme/common_theme.h" |
| 28 #include "ui/native_theme/native_theme.h" | 28 #include "ui/native_theme/native_theme.h" |
| 29 | 29 |
| 30 namespace { | |
| 31 | |
| 32 // Helper function to create a new bitmap with |padding| amount of empty space | |
| 33 // around the original bitmap. | |
| 34 static SkBitmap ApplyPadding(const SkBitmap& source, | |
| 35 const gfx::Insets& padding) { | |
| 36 std::unique_ptr<gfx::Canvas> result( | |
| 37 new gfx::Canvas(gfx::Size(source.width() + padding.width(), | |
| 38 source.height() + padding.height()), | |
| 39 1.0f, false)); | |
| 40 result->DrawImageInt( | |
| 41 gfx::ImageSkia::CreateFrom1xBitmap(source), | |
| 42 0, 0, source.width(), source.height(), | |
| 43 padding.left(), padding.top(), source.width(), source.height(), | |
| 44 false); | |
| 45 return result->ExtractImageRep().sk_bitmap(); | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 ExtensionIconManager::ExtensionIconManager() | 30 ExtensionIconManager::ExtensionIconManager() |
| 51 : monochrome_(false), | 31 : monochrome_(false), |
| 52 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) { |
| 53 } | 33 } |
| 54 | 34 |
| 55 ExtensionIconManager::~ExtensionIconManager() { | 35 ExtensionIconManager::~ExtensionIconManager() { |
| 56 } | 36 } |
| 57 | 37 |
| 58 void ExtensionIconManager::LoadIcon(content::BrowserContext* context, | 38 void ExtensionIconManager::LoadIcon(content::BrowserContext* context, |
| 59 const extensions::Extension* extension) { | 39 const extensions::Extension* extension) { |
| 60 extensions::ExtensionResource icon_resource = | 40 // Insert into pending_icons_ first because LoadImage can call us back |
| 61 extensions::IconsInfo::GetIconResource( | 41 // synchronously if the image is already cached. |
| 62 extension, | 42 pending_icons_.insert(extension->id()); |
| 63 extension_misc::EXTENSION_ICON_BITTY, | 43 extensions::ImageLoader* loader = extensions::ImageLoader::Get(context); |
| 64 ExtensionIconSet::MATCH_BIGGER); | 44 loader->LoadImageAtEveryScaleFactorAsync( |
| 65 if (!icon_resource.extension_root().empty()) { | 45 extension, gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize), |
| 66 // Insert into pending_icons_ first because LoadImage can call us back | 46 base::Bind(&ExtensionIconManager::OnImageLoaded, |
| 67 // synchronously if the image is already cached. | 47 weak_ptr_factory_.GetWeakPtr(), extension->id())); |
| 68 pending_icons_.insert(extension->id()); | |
| 69 extensions::ImageLoader* loader = extensions::ImageLoader::Get(context); | |
| 70 loader->LoadImageAsync(extension, icon_resource, | |
| 71 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize), | |
| 72 base::Bind( | |
| 73 &ExtensionIconManager::OnImageLoaded, | |
| 74 weak_ptr_factory_.GetWeakPtr(), | |
| 75 extension->id())); | |
| 76 } | |
| 77 } | 48 } |
| 78 | 49 |
| 79 const SkBitmap& ExtensionIconManager::GetIcon(const std::string& extension_id) { | 50 gfx::Image ExtensionIconManager::GetIcon(const std::string& extension_id) { |
| 80 const SkBitmap* result = NULL; | 51 auto iter = icons_.find(extension_id); |
| 81 if (base::ContainsKey(icons_, extension_id)) { | 52 gfx::Image* result = nullptr; |
| 82 result = &icons_[extension_id]; | 53 if (iter == icons_.end()) { |
| 83 } else { | |
| 84 EnsureDefaultIcon(); | 54 EnsureDefaultIcon(); |
| 85 result = &default_icon_; | 55 result = &default_icon_; |
| 56 } else { |
| 57 result = &iter->second; |
| 86 } | 58 } |
| 59 |
| 87 DCHECK(result); | 60 DCHECK(result); |
| 88 DCHECK_EQ(gfx::kFaviconSize + padding_.width(), result->width()); | 61 DCHECK_EQ(gfx::kFaviconSize, result->Width()); |
| 89 DCHECK_EQ(gfx::kFaviconSize + padding_.height(), result->height()); | 62 DCHECK_EQ(gfx::kFaviconSize, result->Height()); |
| 90 return *result; | 63 return *result; |
| 91 } | 64 } |
| 92 | 65 |
| 93 void ExtensionIconManager::RemoveIcon(const std::string& extension_id) { | 66 void ExtensionIconManager::RemoveIcon(const std::string& extension_id) { |
| 94 icons_.erase(extension_id); | 67 icons_.erase(extension_id); |
| 95 pending_icons_.erase(extension_id); | 68 pending_icons_.erase(extension_id); |
| 96 } | 69 } |
| 97 | 70 |
| 98 void ExtensionIconManager::OnImageLoaded(const std::string& extension_id, | 71 void ExtensionIconManager::OnImageLoaded(const std::string& extension_id, |
| 99 const gfx::Image& image) { | 72 const gfx::Image& image) { |
| 100 if (image.IsEmpty()) | 73 if (image.IsEmpty()) |
| 101 return; | 74 return; |
| 102 | 75 |
| 103 // We may have removed the icon while waiting for it to load. In that case, | 76 // We may have removed the icon while waiting for it to load. In that case, |
| 104 // do nothing. | 77 // do nothing. |
| 105 if (!base::ContainsKey(pending_icons_, extension_id)) | 78 if (pending_icons_.erase(extension_id) == 0) |
| 106 return; | 79 return; |
| 107 | 80 |
| 108 pending_icons_.erase(extension_id); | 81 gfx::Image modified_image = image; |
| 109 icons_[extension_id] = ApplyTransforms(*image.ToSkBitmap()); | 82 if (monochrome_) { |
| 83 color_utils::HSL shift = {-1, 0, 0.6}; |
| 84 modified_image = gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage( |
| 85 image.AsImageSkia(), shift)); |
| 86 } |
| 87 icons_[extension_id] = modified_image; |
| 110 } | 88 } |
| 111 | 89 |
| 112 void ExtensionIconManager::EnsureDefaultIcon() { | 90 void ExtensionIconManager::EnsureDefaultIcon() { |
| 113 if (default_icon_.empty()) { | 91 if (default_icon_.IsEmpty()) { |
| 114 // TODO(estade): use correct scale factor instead of 1x. | 92 default_icon_ = gfx::Image(gfx::CreateVectorIcon( |
| 115 default_icon_ = ApplyPadding( | 93 gfx::VectorIconId::EXTENSION, gfx::kFaviconSize, gfx::kChromeIconGrey)); |
| 116 *gfx::CreateVectorIcon(gfx::VectorIconId::EXTENSION, gfx::kFaviconSize, | |
| 117 gfx::kChromeIconGrey) | |
| 118 .bitmap(), | |
| 119 padding_); | |
| 120 } | 94 } |
| 121 } | 95 } |
| 122 | |
| 123 SkBitmap ExtensionIconManager::ApplyTransforms(const SkBitmap& source) { | |
| 124 SkBitmap result = source; | |
| 125 | |
| 126 if (result.width() != gfx::kFaviconSize || | |
| 127 result.height() != gfx::kFaviconSize) { | |
| 128 result = skia::ImageOperations::Resize( | |
| 129 result, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 130 gfx::kFaviconSize, gfx::kFaviconSize); | |
| 131 } | |
| 132 | |
| 133 if (monochrome_) { | |
| 134 color_utils::HSL shift = {-1, 0, 0.6}; | |
| 135 result = SkBitmapOperations::CreateHSLShiftedBitmap(result, shift); | |
| 136 } | |
| 137 | |
| 138 if (!padding_.IsEmpty()) | |
| 139 result = ApplyPadding(result, padding_); | |
| 140 | |
| 141 return result; | |
| 142 } | |
| OLD | NEW |