OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "base/values.h" | |
10 #include "chrome/common/extensions/api/extension_action/action_handler_helpers.h " | |
11 #include "chrome/common/extensions/extension.h" | |
12 #include "chrome/common/extensions/extension_constants.h" | |
13 #include "chrome/common/extensions/extension_manifest_constants.h" | |
14 #include "chrome/common/extensions/feature_switch.h" | |
15 | |
16 namespace errors = extension_manifest_errors; | |
17 | |
18 namespace extensions { | |
19 | |
20 ScriptBadgeInfo::ScriptBadgeInfo(ActionInfo* action_info) | |
21 : script_badge_info(action_info) { | |
22 } | |
23 | |
24 ScriptBadgeInfo::~ScriptBadgeInfo() { | |
25 } | |
26 | |
27 // static | |
28 const ActionInfo* ScriptBadgeInfo::GetScriptBadge( | |
29 const Extension* extension) { | |
30 ScriptBadgeInfo* info = static_cast<ScriptBadgeInfo*>( | |
31 extension->GetManifestData(extension_manifest_keys::kScriptBadge)); | |
32 return info ? info->script_badge_info.get() : NULL; | |
33 } | |
34 | |
35 ScriptBadgeHandler::ScriptBadgeHandler() { | |
36 } | |
37 | |
38 ScriptBadgeHandler::~ScriptBadgeHandler() { | |
39 } | |
40 | |
41 bool ScriptBadgeHandler::Parse(const base::Value* value, | |
42 Extension* extension, | |
43 string16* error) { | |
44 scoped_ptr<ActionInfo> action_info(new ActionInfo); | |
45 | |
46 // So as to not confuse developers if they specify a script badge section | |
47 // in the manifest, show a warning if the script badge declaration isn't | |
48 // going to have any effect. | |
49 if (!FeatureSwitch::script_badges()->IsEnabled()) { | |
50 extension->AddInstallWarning( | |
51 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | |
52 errors::kScriptBadgeRequiresFlag)); | |
53 } | |
54 | |
55 const DictionaryValue* dict = NULL; | |
56 if (!value->GetAsDictionary(&dict)) { | |
57 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidScriptBadge); | |
58 return false; | |
59 } | |
60 | |
61 action_info = LoadActionInfo(extension, dict, error); | |
62 | |
63 if (!action_info.get()) | |
64 return false; // Failed to parse script badge definition. | |
65 | |
66 // Script badges always use their extension's title and icon so users can rely | |
67 // on the visual appearance to know which extension is running. This isn't | |
68 // bulletproof since an malicious extension could use a different 16x16 icon | |
69 // that matches the icon of a trusted extension, and users wouldn't be warned | |
70 // during installation. | |
71 | |
72 if (!action_info->default_title.empty()) { | |
73 extension->AddInstallWarning( | |
74 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | |
75 errors::kScriptBadgeTitleIgnored)); | |
76 } | |
77 | |
78 if (!action_info->default_icon.empty()) { | |
79 extension->AddInstallWarning( | |
80 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | |
81 errors::kScriptBadgeIconIgnored)); | |
82 } | |
83 | |
84 SetActionInfoDefaults(extension, action_info.get()); | |
85 extension->SetManifestData(extension_manifest_keys::kScriptBadge, | |
86 new ScriptBadgeInfo(action_info.release())); | |
87 return true; | |
88 } | |
89 | |
90 bool ScriptBadgeHandler::HasNoKey(Extension* extension, string16* error) { | |
91 scoped_ptr<ActionInfo> action_info(new ActionInfo); | |
92 SetActionInfoDefaults(extension, action_info.get()); | |
93 extension->SetManifestData(extension_manifest_keys::kScriptBadge, | |
94 new ScriptBadgeInfo(action_info.release())); | |
95 return true; | |
96 } | |
97 | |
98 void ScriptBadgeHandler::SetActionInfoDefaults(Extension* extension, | |
Yoyo Zhou
2012/12/19 01:38:50
should be const Extension* here
Devlin
2012/12/19 19:38:17
Done.
| |
99 ActionInfo* info) { | |
100 info->default_title = extension->name(); | |
101 info->default_icon.Clear(); | |
102 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { | |
103 std::string path = extension->icons().Get( | |
104 extension_misc::kScriptBadgeIconSizes[i], | |
105 ExtensionIconSet::MATCH_BIGGER); | |
106 if (!path.empty()) { | |
107 info->default_icon.Add( | |
108 extension_misc::kScriptBadgeIconSizes[i], path); | |
109 } | |
110 } | |
111 } | |
112 | |
113 } // namespace extensions | |
OLD | NEW |