| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/extensions/api/icons/icons_handler.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 15 #include "chrome/common/extensions/manifest_handler_helpers.h" |
| 16 #include "grit/theme_resources.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "webkit/glue/image_decoder.h" |
| 20 |
| 21 namespace keys = extension_manifest_keys; |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 static base::LazyInstance<ExtensionIconSet> g_empty_icon_set = |
| 26 LAZY_INSTANCE_INITIALIZER; |
| 27 |
| 28 const int IconsInfo::kPageActionIconMaxSize = 19; |
| 29 const int IconsInfo::kBrowserActionIconMaxSize = 19; |
| 30 |
| 31 // static |
| 32 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) { |
| 33 IconsInfo* info = static_cast<IconsInfo*>( |
| 34 extension->GetManifestData(keys::kIcons)); |
| 35 return info ? info->icons : g_empty_icon_set.Get(); |
| 36 } |
| 37 |
| 38 // static |
| 39 void IconsInfo::DecodeIcon(const Extension* extension, |
| 40 int preferred_icon_size, |
| 41 ExtensionIconSet::MatchType match_type, |
| 42 scoped_ptr<SkBitmap>* result) { |
| 43 std::string path = GetIcons(extension).Get(preferred_icon_size, match_type); |
| 44 int size = GetIcons(extension).GetIconSizeFromPath(path); |
| 45 ExtensionResource icon_resource = extension->GetResource(path); |
| 46 DecodeIconFromPath(icon_resource.GetFilePath(), size, result); |
| 47 } |
| 48 |
| 49 // static |
| 50 void IconsInfo::DecodeIcon(const Extension* extension, |
| 51 int icon_size, |
| 52 scoped_ptr<SkBitmap>* result) { |
| 53 DecodeIcon(extension, icon_size, ExtensionIconSet::MATCH_EXACTLY, result); |
| 54 } |
| 55 |
| 56 // static |
| 57 void IconsInfo::DecodeIconFromPath(const base::FilePath& icon_path, |
| 58 int icon_size, |
| 59 scoped_ptr<SkBitmap>* result) { |
| 60 if (icon_path.empty()) |
| 61 return; |
| 62 |
| 63 std::string file_contents; |
| 64 if (!file_util::ReadFileToString(icon_path, &file_contents)) { |
| 65 DLOG(ERROR) << "Could not read icon file: " << icon_path.LossyDisplayName(); |
| 66 return; |
| 67 } |
| 68 |
| 69 // Decode the image using WebKit's image decoder. |
| 70 const unsigned char* data = |
| 71 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 72 webkit_glue::ImageDecoder decoder; |
| 73 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
| 74 *decoded = decoder.Decode(data, file_contents.length()); |
| 75 if (decoded->empty()) { |
| 76 DLOG(ERROR) << "Could not decode icon file: " |
| 77 << icon_path.LossyDisplayName(); |
| 78 return; |
| 79 } |
| 80 |
| 81 if (decoded->width() != icon_size || decoded->height() != icon_size) { |
| 82 DLOG(ERROR) << "Icon file has unexpected size: " |
| 83 << base::IntToString(decoded->width()) << "x" |
| 84 << base::IntToString(decoded->height()); |
| 85 return; |
| 86 } |
| 87 |
| 88 result->swap(decoded); |
| 89 } |
| 90 |
| 91 // static |
| 92 const gfx::ImageSkia& IconsInfo::GetDefaultAppIcon() { |
| 93 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 94 IDR_APP_DEFAULT_ICON); |
| 95 } |
| 96 |
| 97 // static |
| 98 const gfx::ImageSkia& IconsInfo::GetDefaultExtensionIcon() { |
| 99 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 100 IDR_EXTENSION_DEFAULT_ICON); |
| 101 } |
| 102 |
| 103 // static |
| 104 ExtensionResource IconsInfo::GetIconResource( |
| 105 const Extension* extension, |
| 106 int size, |
| 107 ExtensionIconSet::MatchType match_type) { |
| 108 std::string path = GetIcons(extension).Get(size, match_type); |
| 109 return path.empty() ? ExtensionResource() : extension->GetResource(path); |
| 110 } |
| 111 |
| 112 // static |
| 113 GURL IconsInfo::GetIconURL(const Extension* extension, |
| 114 int size, |
| 115 ExtensionIconSet::MatchType match_type) { |
| 116 std::string path = GetIcons(extension).Get(size, match_type); |
| 117 return path.empty() ? GURL() : extension->GetResourceURL(path); |
| 118 } |
| 119 |
| 120 IconsHandler::IconsHandler() { |
| 121 } |
| 122 |
| 123 IconsHandler::~IconsHandler() { |
| 124 } |
| 125 |
| 126 bool IconsHandler::Parse(Extension* extension, string16* error) { |
| 127 scoped_ptr<IconsInfo> icons_info(new IconsInfo); |
| 128 const DictionaryValue* icons_dict = NULL; |
| 129 if (!extension->manifest()->GetDictionary(keys::kIcons, &icons_dict)) { |
| 130 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidIcons); |
| 131 return false; |
| 132 } |
| 133 |
| 134 if (!manifest_handler_helpers::LoadIconsFromDictionary( |
| 135 icons_dict, |
| 136 extension_misc::kExtensionIconSizes, |
| 137 extension_misc::kNumExtensionIconSizes, |
| 138 &icons_info->icons, |
| 139 error)) { |
| 140 return false; |
| 141 } |
| 142 |
| 143 extension->SetManifestData(keys::kIcons, icons_info.release()); |
| 144 return true; |
| 145 } |
| 146 |
| 147 } // namespace extensions |
| OLD | NEW |