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/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
11 #include "chrome/common/extensions/extension_constants.h" | 11 #include "chrome/common/extensions/extension_constants.h" |
12 #include "chrome/common/extensions/extension_manifest_constants.h" | 12 #include "chrome/common/extensions/extension_manifest_constants.h" |
13 #include "chrome/common/extensions/feature_switch.h" | 13 #include "chrome/common/extensions/feature_switch.h" |
| 14 #include "chrome/common/extensions/manifest.h" |
14 #include "chrome/common/extensions/manifest_handler_helpers.h" | 15 #include "chrome/common/extensions/manifest_handler_helpers.h" |
15 | 16 |
16 namespace errors = extension_manifest_errors; | 17 namespace errors = extension_manifest_errors; |
17 | 18 |
18 namespace extensions { | 19 namespace extensions { |
19 | 20 |
20 ScriptBadgeHandler::ScriptBadgeHandler() { | 21 ScriptBadgeHandler::ScriptBadgeHandler() { |
21 } | 22 } |
22 | 23 |
23 ScriptBadgeHandler::~ScriptBadgeHandler() { | 24 ScriptBadgeHandler::~ScriptBadgeHandler() { |
24 } | 25 } |
25 | 26 |
26 bool ScriptBadgeHandler::Parse(const base::Value* value, | 27 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { |
27 Extension* extension, | |
28 string16* error) { | |
29 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 28 scoped_ptr<ActionInfo> action_info(new ActionInfo); |
30 | 29 |
31 // So as to not confuse developers if they specify a script badge section | 30 // So as to not confuse developers if they specify a script badge section |
32 // in the manifest, show a warning if the script badge declaration isn't | 31 // in the manifest, show a warning if the script badge declaration isn't |
33 // going to have any effect. | 32 // going to have any effect. |
34 if (!FeatureSwitch::script_badges()->IsEnabled()) { | 33 if (!FeatureSwitch::script_badges()->IsEnabled()) { |
35 extension->AddInstallWarning( | 34 extension->AddInstallWarning( |
36 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 35 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, |
37 errors::kScriptBadgeRequiresFlag)); | 36 errors::kScriptBadgeRequiresFlag)); |
38 } | 37 } |
39 | 38 |
| 39 // Provide a default script badge if one isn't declared in the manifest. |
| 40 if (!extension->manifest()->HasKey(extension_manifest_keys::kScriptBadge)) { |
| 41 scoped_ptr<ActionInfo> action_info(new ActionInfo); |
| 42 SetActionInfoDefaults(extension, action_info.get()); |
| 43 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
| 44 return true; |
| 45 } |
| 46 |
40 const DictionaryValue* dict = NULL; | 47 const DictionaryValue* dict = NULL; |
41 if (!value->GetAsDictionary(&dict)) { | 48 if (!extension->manifest()->GetDictionary( |
42 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidScriptBadge); | 49 extension_manifest_keys::kScriptBadge, &dict)) { |
| 50 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); |
43 return false; | 51 return false; |
44 } | 52 } |
45 | 53 |
46 action_info = | 54 action_info = |
47 manifest_handler_helpers::LoadActionInfo(extension, dict, error); | 55 manifest_handler_helpers::LoadActionInfo(extension, dict, error); |
48 | 56 |
49 if (!action_info.get()) | 57 if (!action_info.get()) |
50 return false; // Failed to parse script badge definition. | 58 return false; // Failed to parse script badge definition. |
51 | 59 |
52 // Script badges always use their extension's title and icon so users can rely | 60 // Script badges always use their extension's title and icon so users can rely |
(...skipping 12 matching lines...) Expand all Loading... |
65 extension->AddInstallWarning( | 73 extension->AddInstallWarning( |
66 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 74 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, |
67 errors::kScriptBadgeIconIgnored)); | 75 errors::kScriptBadgeIconIgnored)); |
68 } | 76 } |
69 | 77 |
70 SetActionInfoDefaults(extension, action_info.get()); | 78 SetActionInfoDefaults(extension, action_info.get()); |
71 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 79 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
72 return true; | 80 return true; |
73 } | 81 } |
74 | 82 |
75 bool ScriptBadgeHandler::HasNoKey(Extension* extension, string16* error) { | 83 bool ScriptBadgeHandler::AlwaysParseForType(Extension::Type type) { |
76 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 84 return type == Extension::TYPE_EXTENSION; |
77 SetActionInfoDefaults(extension, action_info.get()); | |
78 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | |
79 return true; | |
80 } | 85 } |
81 | 86 |
82 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, | 87 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, |
83 ActionInfo* info) { | 88 ActionInfo* info) { |
84 info->default_title = extension->name(); | 89 info->default_title = extension->name(); |
85 info->default_icon.Clear(); | 90 info->default_icon.Clear(); |
86 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { | 91 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { |
87 std::string path = extension->icons().Get( | 92 std::string path = extension->icons().Get( |
88 extension_misc::kScriptBadgeIconSizes[i], | 93 extension_misc::kScriptBadgeIconSizes[i], |
89 ExtensionIconSet::MATCH_BIGGER); | 94 ExtensionIconSet::MATCH_BIGGER); |
90 if (!path.empty()) { | 95 if (!path.empty()) { |
91 info->default_icon.Add( | 96 info->default_icon.Add( |
92 extension_misc::kScriptBadgeIconSizes[i], path); | 97 extension_misc::kScriptBadgeIconSizes[i], path); |
93 } | 98 } |
94 } | 99 } |
95 } | 100 } |
96 | 101 |
97 } // namespace extensions | 102 } // namespace extensions |
OLD | NEW |