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/manifest_handlers/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/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/extension_file_util.h"
15 #include "chrome/common/extensions/manifest_handler_helpers.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/manifest_constants.h"
18 #include "grit/generated_resources.h"
19 #include "grit/theme_resources.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/size.h"
23
24 namespace extensions {
25
26 namespace keys = manifest_keys;
27
28 static base::LazyInstance<ExtensionIconSet> g_empty_icon_set =
29 LAZY_INSTANCE_INITIALIZER;
30
31 const int IconsInfo::kPageActionIconMaxSize = 19;
32 const int IconsInfo::kBrowserActionIconMaxSize = 19;
33
34 // static
GetIcons(const Extension * extension)35 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) {
36 IconsInfo* info = static_cast<IconsInfo*>(
37 extension->GetManifestData(keys::kIcons));
38 return info ? info->icons : g_empty_icon_set.Get();
39 }
40
41 // static
GetDefaultAppIcon()42 const gfx::ImageSkia& IconsInfo::GetDefaultAppIcon() {
43 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
44 IDR_APP_DEFAULT_ICON);
45 }
46
47 // static
GetDefaultExtensionIcon()48 const gfx::ImageSkia& IconsInfo::GetDefaultExtensionIcon() {
49 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
50 IDR_EXTENSION_DEFAULT_ICON);
51 }
52
53 // static
GetIconResource(const Extension * extension,int size,ExtensionIconSet::MatchType match_type)54 ExtensionResource IconsInfo::GetIconResource(
55 const Extension* extension,
56 int size,
57 ExtensionIconSet::MatchType match_type) {
58 std::string path = GetIcons(extension).Get(size, match_type);
59 return path.empty() ? ExtensionResource() : extension->GetResource(path);
60 }
61
62 // static
GetIconURL(const Extension * extension,int size,ExtensionIconSet::MatchType match_type)63 GURL IconsInfo::GetIconURL(const Extension* extension,
64 int size,
65 ExtensionIconSet::MatchType match_type) {
66 std::string path = GetIcons(extension).Get(size, match_type);
67 return path.empty() ? GURL() : extension->GetResourceURL(path);
68 }
69
IconsHandler()70 IconsHandler::IconsHandler() {
71 }
72
~IconsHandler()73 IconsHandler::~IconsHandler() {
74 }
75
Parse(Extension * extension,base::string16 * error)76 bool IconsHandler::Parse(Extension* extension, base::string16* error) {
77 scoped_ptr<IconsInfo> icons_info(new IconsInfo);
78 const base::DictionaryValue* icons_dict = NULL;
79 if (!extension->manifest()->GetDictionary(keys::kIcons, &icons_dict)) {
80 *error = ASCIIToUTF16(manifest_errors::kInvalidIcons);
81 return false;
82 }
83
84 if (!manifest_handler_helpers::LoadIconsFromDictionary(
85 icons_dict,
86 extension_misc::kExtensionIconSizes,
87 extension_misc::kNumExtensionIconSizes,
88 &icons_info->icons,
89 error)) {
90 return false;
91 }
92
93 extension->SetManifestData(keys::kIcons, icons_info.release());
94 return true;
95 }
96
Validate(const Extension * extension,std::string * error,std::vector<InstallWarning> * warnings) const97 bool IconsHandler::Validate(const Extension* extension,
98 std::string* error,
99 std::vector<InstallWarning>* warnings) const {
100 return extension_file_util::ValidateExtensionIconSet(
101 IconsInfo::GetIcons(extension),
102 extension,
103 IDS_EXTENSION_LOAD_ICON_FAILED,
104 error);
105 }
106
Keys() const107 const std::vector<std::string> IconsHandler::Keys() const {
108 return SingleKey(keys::kIcons);
109 }
110
111 } // namespace extensions
112