Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / manifest_handlers / icons_handler.cc
blob8535d9b5c02fde46a689b5421dfdfce79e28cd0f
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;
28 // static
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();
35 // static
36 ExtensionResource IconsInfo::GetIconResource(
37 const Extension* extension,
38 int size,
39 ExtensionIconSet::MatchType match_type) {
40 const std::string& path = GetIcons(extension).Get(size, match_type);
41 return path.empty() ? ExtensionResource() : extension->GetResource(path);
44 // static
45 GURL IconsInfo::GetIconURL(const Extension* extension,
46 int size,
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);
63 return false;
66 if (!manifest_handler_helpers::LoadIconsFromDictionary(
67 icons_dict,
68 extension_misc::kExtensionIconSizes,
69 extension_misc::kNumExtensionIconSizes,
70 &icons_info->icons,
71 error)) {
72 return false;
75 extension->SetManifestData(keys::kIcons, icons_info.release());
76 return true;
79 bool IconsHandler::Validate(const Extension* extension,
80 std::string* error,
81 std::vector<InstallWarning>* warnings) const {
82 return file_util::ValidateExtensionIconSet(IconsInfo::GetIcons(extension),
83 extension,
84 IDS_EXTENSION_LOAD_ICON_FAILED,
85 error);
88 const std::vector<std::string> IconsHandler::Keys() const {
89 return SingleKey(keys::kIcons);
92 } // namespace extensions