OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" | 5 #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/common/extensions/api/icons/icons_handler.h" | 10 #include "chrome/common/extensions/api/icons/icons_handler.h" |
11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
12 #include "chrome/common/extensions/extension_constants.h" | 12 #include "chrome/common/extensions/extension_constants.h" |
13 #include "chrome/common/extensions/extension_manifest_constants.h" | 13 #include "chrome/common/extensions/extension_manifest_constants.h" |
14 #include "chrome/common/extensions/feature_switch.h" | 14 #include "chrome/common/extensions/feature_switch.h" |
15 #include "chrome/common/extensions/manifest.h" | 15 #include "chrome/common/extensions/manifest.h" |
16 #include "chrome/common/extensions/manifest_handler_helpers.h" | 16 #include "chrome/common/extensions/manifest_handler_helpers.h" |
17 #include "extensions/common/install_warning.h" | 17 #include "extensions/common/install_warning.h" |
18 | 18 |
19 namespace errors = extension_manifest_errors; | 19 namespace errors = extension_manifest_errors; |
| 20 namespace keys = extension_manifest_keys; |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 ScriptBadgeHandler::ScriptBadgeHandler() { | 24 ScriptBadgeHandler::ScriptBadgeHandler() { |
24 prerequisite_keys_.push_back(extension_manifest_keys::kIcons); | |
25 } | 25 } |
26 | 26 |
27 ScriptBadgeHandler::~ScriptBadgeHandler() { | 27 ScriptBadgeHandler::~ScriptBadgeHandler() { |
28 } | 28 } |
29 | 29 |
30 const std::vector<std::string>& ScriptBadgeHandler::PrerequisiteKeys() { | 30 const std::vector<std::string> ScriptBadgeHandler::PrerequisiteKeys() const { |
31 return prerequisite_keys_; | 31 return SingleKey(keys::kIcons); |
32 } | 32 } |
33 | 33 |
34 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { | 34 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { |
35 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 35 scoped_ptr<ActionInfo> action_info(new ActionInfo); |
36 | 36 |
37 // Provide a default script badge if one isn't declared in the manifest. | 37 // Provide a default script badge if one isn't declared in the manifest. |
38 if (!extension->manifest()->HasKey(extension_manifest_keys::kScriptBadge)) { | 38 if (!extension->manifest()->HasKey(keys::kScriptBadge)) { |
39 SetActionInfoDefaults(extension, action_info.get()); | 39 SetActionInfoDefaults(extension, action_info.get()); |
40 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 40 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
41 return true; | 41 return true; |
42 } | 42 } |
43 | 43 |
44 // So as to not confuse developers if they specify a script badge section | 44 // So as to not confuse developers if they specify a script badge section |
45 // in the manifest, show a warning if the script badge declaration isn't | 45 // in the manifest, show a warning if the script badge declaration isn't |
46 // going to have any effect. | 46 // going to have any effect. |
47 if (!FeatureSwitch::script_badges()->IsEnabled()) { | 47 if (!FeatureSwitch::script_badges()->IsEnabled()) { |
48 extension->AddInstallWarning( | 48 extension->AddInstallWarning( |
49 InstallWarning(InstallWarning::FORMAT_TEXT, | 49 InstallWarning(InstallWarning::FORMAT_TEXT, |
50 errors::kScriptBadgeRequiresFlag)); | 50 errors::kScriptBadgeRequiresFlag)); |
51 } | 51 } |
52 | 52 |
53 const DictionaryValue* dict = NULL; | 53 const DictionaryValue* dict = NULL; |
54 if (!extension->manifest()->GetDictionary( | 54 if (!extension->manifest()->GetDictionary(keys::kScriptBadge, &dict)) { |
55 extension_manifest_keys::kScriptBadge, &dict)) { | |
56 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); | 55 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); |
57 return false; | 56 return false; |
58 } | 57 } |
59 | 58 |
60 action_info = | 59 action_info = |
61 manifest_handler_helpers::LoadActionInfo(extension, dict, error); | 60 manifest_handler_helpers::LoadActionInfo(extension, dict, error); |
62 | 61 |
63 if (!action_info.get()) | 62 if (!action_info.get()) |
64 return false; // Failed to parse script badge definition. | 63 return false; // Failed to parse script badge definition. |
65 | 64 |
(...skipping 13 matching lines...) Expand all Loading... |
79 extension->AddInstallWarning( | 78 extension->AddInstallWarning( |
80 InstallWarning(InstallWarning::FORMAT_TEXT, | 79 InstallWarning(InstallWarning::FORMAT_TEXT, |
81 errors::kScriptBadgeIconIgnored)); | 80 errors::kScriptBadgeIconIgnored)); |
82 } | 81 } |
83 | 82 |
84 SetActionInfoDefaults(extension, action_info.get()); | 83 SetActionInfoDefaults(extension, action_info.get()); |
85 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 84 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
86 return true; | 85 return true; |
87 } | 86 } |
88 | 87 |
89 bool ScriptBadgeHandler::AlwaysParseForType(Manifest::Type type) { | 88 bool ScriptBadgeHandler::AlwaysParseForType(Manifest::Type type) const { |
90 return type == Manifest::TYPE_EXTENSION; | 89 return type == Manifest::TYPE_EXTENSION; |
91 } | 90 } |
92 | 91 |
93 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, | 92 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, |
94 ActionInfo* info) { | 93 ActionInfo* info) { |
95 info->default_title = extension->name(); | 94 info->default_title = extension->name(); |
96 info->default_icon.Clear(); | 95 info->default_icon.Clear(); |
97 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { | 96 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { |
98 std::string path = IconsInfo::GetIcons(extension).Get( | 97 std::string path = IconsInfo::GetIcons(extension).Get( |
99 extension_misc::kScriptBadgeIconSizes[i], | 98 extension_misc::kScriptBadgeIconSizes[i], |
100 ExtensionIconSet::MATCH_BIGGER); | 99 ExtensionIconSet::MATCH_BIGGER); |
101 if (!path.empty()) { | 100 if (!path.empty()) { |
102 info->default_icon.Add( | 101 info->default_icon.Add( |
103 extension_misc::kScriptBadgeIconSizes[i], path); | 102 extension_misc::kScriptBadgeIconSizes[i], path); |
104 } | 103 } |
105 } | 104 } |
106 } | 105 } |
107 | 106 |
| 107 const std::vector<std::string> ScriptBadgeHandler::Keys() const { |
| 108 return SingleKey(keys::kScriptBadge); |
| 109 } |
| 110 |
108 } // namespace extensions | 111 } // namespace extensions |
OLD | NEW |