Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / api / extension_action / script_badge_handler.cc
blob9f5552a08ab070badcedb91ae8acbd84489fbb8f
1 // Copyright (c) 2012 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/api/extension_action/script_badge_handler.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/extension_constants.h"
11 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/feature_switch.h"
14 #include "extensions/common/install_warning.h"
15 #include "extensions/common/manifest.h"
16 #include "extensions/common/manifest_constants.h"
18 namespace extensions {
20 namespace errors = manifest_errors;
21 namespace keys = manifest_keys;
23 ScriptBadgeHandler::ScriptBadgeHandler() {
26 ScriptBadgeHandler::~ScriptBadgeHandler() {
29 const std::vector<std::string> ScriptBadgeHandler::PrerequisiteKeys() const {
30 return SingleKey(keys::kIcons);
33 bool ScriptBadgeHandler::Parse(Extension* extension, base::string16* error) {
34 scoped_ptr<ActionInfo> action_info(new ActionInfo);
36 // Provide a default script badge if one isn't declared in the manifest.
37 if (!extension->manifest()->HasKey(keys::kScriptBadge)) {
38 SetActionInfoDefaults(extension, action_info.get());
39 ActionInfo::SetScriptBadgeInfo(extension, action_info.release());
40 return true;
43 // So as to not confuse developers if they specify a script badge section
44 // in the manifest, show a warning if the script badge declaration isn't
45 // going to have any effect.
46 if (!FeatureSwitch::script_badges()->IsEnabled()) {
47 extension->AddInstallWarning(
48 InstallWarning(errors::kScriptBadgeRequiresFlag, keys::kScriptBadge));
51 const base::DictionaryValue* dict = NULL;
52 if (!extension->manifest()->GetDictionary(keys::kScriptBadge, &dict)) {
53 *error = base::ASCIIToUTF16(errors::kInvalidScriptBadge);
54 return false;
57 action_info = ActionInfo::Load(extension, dict, error);
59 if (!action_info.get())
60 return false; // Failed to parse script badge definition.
62 // Script badges always use their extension's title and icon so users can rely
63 // on the visual appearance to know which extension is running. This isn't
64 // bulletproof since an malicious extension could use a different 16x16 icon
65 // that matches the icon of a trusted extension, and users wouldn't be warned
66 // during installation.
68 if (!action_info->default_title.empty()) {
69 extension->AddInstallWarning(
70 InstallWarning(errors::kScriptBadgeTitleIgnored,
71 keys::kScriptBadge,
72 keys::kPageActionDefaultTitle));
75 if (!action_info->default_icon.empty()) {
76 extension->AddInstallWarning(
77 InstallWarning(errors::kScriptBadgeIconIgnored,
78 keys::kScriptBadge,
79 keys::kPageActionDefaultIcon));
82 SetActionInfoDefaults(extension, action_info.get());
83 ActionInfo::SetScriptBadgeInfo(extension, action_info.release());
84 return true;
87 bool ScriptBadgeHandler::AlwaysParseForType(Manifest::Type type) const {
88 return type == Manifest::TYPE_EXTENSION;
91 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension,
92 ActionInfo* info) {
93 info->default_title = extension->name();
94 info->default_icon.Clear();
95 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) {
96 std::string path = IconsInfo::GetIcons(extension).Get(
97 extension_misc::kScriptBadgeIconSizes[i],
98 ExtensionIconSet::MATCH_BIGGER);
99 if (!path.empty()) {
100 info->default_icon.Add(
101 extension_misc::kScriptBadgeIconSizes[i], path);
106 const std::vector<std::string> ScriptBadgeHandler::Keys() const {
107 return SingleKey(keys::kScriptBadge);
110 } // namespace extensions