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
3 // found in the LICENSE file.
5 #include "extensions/common/manifest_handlers/icons_handler.h"
7 #include "base/files/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 "extensions/common/constants.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/file_util.h"
16 #include "extensions/common/manifest_constants.h"
17 #include "extensions/common/manifest_handler_helpers.h"
18 #include "grit/extensions_strings.h"
19 #include "ui/gfx/geometry/size.h"
21 namespace extensions
{
23 namespace keys
= manifest_keys
;
25 static base::LazyInstance
<ExtensionIconSet
> g_empty_icon_set
=
26 LAZY_INSTANCE_INITIALIZER
;
29 const ExtensionIconSet
& IconsInfo::GetIcons(const Extension
* extension
) {
30 IconsInfo
* info
= static_cast<IconsInfo
*>(
31 extension
->GetManifestData(keys::kIcons
));
32 return info
? info
->icons
: g_empty_icon_set
.Get();
36 ExtensionResource
IconsInfo::GetIconResource(
37 const Extension
* extension
,
39 ExtensionIconSet::MatchType match_type
) {
40 const std::string
& path
= GetIcons(extension
).Get(size
, match_type
);
41 return path
.empty() ? ExtensionResource() : extension
->GetResource(path
);
45 GURL
IconsInfo::GetIconURL(const Extension
* extension
,
47 ExtensionIconSet::MatchType match_type
) {
48 const std::string
& path
= GetIcons(extension
).Get(size
, match_type
);
49 return path
.empty() ? GURL() : extension
->GetResourceURL(path
);
52 IconsHandler::IconsHandler() {
55 IconsHandler::~IconsHandler() {
58 bool IconsHandler::Parse(Extension
* extension
, base::string16
* error
) {
59 scoped_ptr
<IconsInfo
> icons_info(new IconsInfo
);
60 const base::DictionaryValue
* icons_dict
= NULL
;
61 if (!extension
->manifest()->GetDictionary(keys::kIcons
, &icons_dict
)) {
62 *error
= base::ASCIIToUTF16(manifest_errors::kInvalidIcons
);
66 if (!manifest_handler_helpers::LoadIconsFromDictionary(
68 extension_misc::kExtensionIconSizes
,
69 extension_misc::kNumExtensionIconSizes
,
75 extension
->SetManifestData(keys::kIcons
, icons_info
.release());
79 bool IconsHandler::Validate(const Extension
* extension
,
81 std::vector
<InstallWarning
>* warnings
) const {
82 return file_util::ValidateExtensionIconSet(IconsInfo::GetIcons(extension
),
84 IDS_EXTENSION_LOAD_ICON_FAILED
,
88 const std::vector
<std::string
> IconsHandler::Keys() const {
89 return SingleKey(keys::kIcons
);
92 } // namespace extensions