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.
5 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
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"
24 namespace extensions
{
26 namespace keys
= manifest_keys
;
28 static base::LazyInstance
<ExtensionIconSet
> g_empty_icon_set
=
29 LAZY_INSTANCE_INITIALIZER
;
31 const int IconsInfo::kPageActionIconMaxSize
= 19;
32 const int IconsInfo::kBrowserActionIconMaxSize
= 19;
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();
42 const gfx::ImageSkia
& IconsInfo::GetDefaultAppIcon() {
43 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
44 IDR_APP_DEFAULT_ICON
);
48 const gfx::ImageSkia
& IconsInfo::GetDefaultExtensionIcon() {
49 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
50 IDR_EXTENSION_DEFAULT_ICON
);
54 ExtensionResource
IconsInfo::GetIconResource(
55 const Extension
* extension
,
57 ExtensionIconSet::MatchType match_type
) {
58 std::string path
= GetIcons(extension
).Get(size
, match_type
);
59 return path
.empty() ? ExtensionResource() : extension
->GetResource(path
);
63 GURL
IconsInfo::GetIconURL(const Extension
* extension
,
65 ExtensionIconSet::MatchType match_type
) {
66 std::string path
= GetIcons(extension
).Get(size
, match_type
);
67 return path
.empty() ? GURL() : extension
->GetResourceURL(path
);
70 IconsHandler::IconsHandler() {
73 IconsHandler::~IconsHandler() {
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
= base::ASCIIToUTF16(manifest_errors::kInvalidIcons
);
84 if (!manifest_handler_helpers::LoadIconsFromDictionary(
86 extension_misc::kExtensionIconSizes
,
87 extension_misc::kNumExtensionIconSizes
,
93 extension
->SetManifestData(keys::kIcons
, icons_info
.release());
97 bool IconsHandler::Validate(const Extension
* extension
,
99 std::vector
<InstallWarning
>* warnings
) const {
100 return extension_file_util::ValidateExtensionIconSet(
101 IconsInfo::GetIcons(extension
),
103 IDS_EXTENSION_LOAD_ICON_FAILED
,
107 const std::vector
<std::string
> IconsHandler::Keys() const {
108 return SingleKey(keys::kIcons
);
111 } // namespace extensions