OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/manifest_handlers/automation.h" | 5 #include "chrome/common/extensions/manifest_handlers/automation.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/common/extensions/api/manifest_types.h" | 8 #include "chrome/common/extensions/api/manifest_types.h" |
10 #include "extensions/common/error_utils.h" | 9 #include "extensions/common/error_utils.h" |
| 10 #include "extensions/common/extensions_client.h" |
11 #include "extensions/common/manifest_constants.h" | 11 #include "extensions/common/manifest_constants.h" |
12 #include "extensions/common/permissions/api_permission_set.h" | 12 #include "extensions/common/permissions/api_permission_set.h" |
| 13 #include "extensions/common/permissions/manifest_permission.h" |
| 14 #include "extensions/common/permissions/permission_message.h" |
| 15 #include "extensions/common/permissions/permission_message_util.h" |
13 #include "extensions/common/permissions/permissions_data.h" | 16 #include "extensions/common/permissions/permissions_data.h" |
14 #include "extensions/common/url_pattern.h" | 17 #include "extensions/common/url_pattern.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "ipc/ipc_message.h" |
| 20 #include "ipc/ipc_message_utils.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
15 | 22 |
16 namespace extensions { | 23 namespace extensions { |
17 | 24 |
18 namespace automation_errors { | 25 namespace automation_errors { |
19 const char kErrorDesktopTrueInteractFalse[] = | 26 const char kErrorDesktopTrueInteractFalse[] = |
20 "Cannot specify interactive=false if desktop=true is specified; " | 27 "Cannot specify interactive=false if desktop=true is specified; " |
21 "interactive=false will be ignored."; | 28 "interactive=false will be ignored."; |
22 const char kErrorDesktopTrueMatchesSpecified[] = | 29 const char kErrorDesktopTrueMatchesSpecified[] = |
23 "Cannot specify matches for Automation if desktop=true is specified; " | 30 "Cannot specify matches for Automation if desktop=true is specified; " |
24 "matches will be ignored."; | 31 "matches will be ignored."; |
25 const char kErrorInvalidMatch[] = "Invalid match pattern '*': *"; | 32 const char kErrorInvalidMatch[] = "Invalid match pattern '*': *"; |
26 const char kErrorNoMatchesProvided[] = "No valid match patterns provided."; | 33 const char kErrorNoMatchesProvided[] = "No valid match patterns provided."; |
27 } | 34 } |
28 | 35 |
29 namespace errors = manifest_errors; | 36 namespace errors = manifest_errors; |
30 namespace keys = extensions::manifest_keys; | 37 namespace keys = extensions::manifest_keys; |
31 using api::manifest_types::Automation; | 38 using api::manifest_types::Automation; |
32 | 39 |
| 40 class AutomationManifestPermission : public ManifestPermission { |
| 41 public: |
| 42 explicit AutomationManifestPermission( |
| 43 scoped_ptr<const AutomationInfo> automation_info) |
| 44 : automation_info_(automation_info.Pass()) {} |
| 45 |
| 46 // extensions::ManifestPermission overrides. |
| 47 virtual std::string name() const OVERRIDE { return keys::kAutomation; } |
| 48 |
| 49 virtual std::string id() const OVERRIDE { return keys::kAutomation; } |
| 50 |
| 51 virtual bool HasMessages() const OVERRIDE { return GetMessages().size() > 0; } |
| 52 |
| 53 virtual PermissionMessages GetMessages() const OVERRIDE { |
| 54 PermissionMessages messages; |
| 55 if (automation_info_->desktop) { |
| 56 messages.push_back(PermissionMessage( |
| 57 PermissionMessage::kFullAccess, |
| 58 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS))); |
| 59 } else if (automation_info_->matches.MatchesAllURLs()) { |
| 60 messages.push_back(PermissionMessage( |
| 61 PermissionMessage::kHostsAll, |
| 62 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS))); |
| 63 } else { |
| 64 URLPatternSet regular_hosts; |
| 65 std::set<PermissionMessage> message_set; |
| 66 ExtensionsClient::Get()->FilterHostPermissions( |
| 67 automation_info_->matches, ®ular_hosts, &message_set); |
| 68 messages.insert(messages.end(), message_set.begin(), message_set.end()); |
| 69 |
| 70 std::set<std::string> hosts = |
| 71 permission_message_util::GetDistinctHosts(regular_hosts, true, true); |
| 72 if (!hosts.empty()) |
| 73 messages.push_back(permission_message_util::CreateFromHostList(hosts)); |
| 74 } |
| 75 |
| 76 return messages; |
| 77 } |
| 78 |
| 79 virtual bool FromValue(const base::Value* value) OVERRIDE { |
| 80 base::string16 error; |
| 81 automation_info_.reset( |
| 82 AutomationInfo::FromValue(*value, NULL /* install_warnings */, &error) |
| 83 .release()); |
| 84 return error.empty(); |
| 85 } |
| 86 |
| 87 virtual scoped_ptr<base::Value> ToValue() const OVERRIDE { |
| 88 return AutomationInfo::ToValue(*automation_info_).Pass(); |
| 89 } |
| 90 |
| 91 virtual ManifestPermission* Diff( |
| 92 const ManifestPermission* rhs) const OVERRIDE { |
| 93 const AutomationManifestPermission* other = |
| 94 static_cast<const AutomationManifestPermission*>(rhs); |
| 95 |
| 96 bool desktop = |
| 97 automation_info_->desktop && !other->automation_info_->desktop; |
| 98 bool interact = |
| 99 automation_info_->interact && !other->automation_info_->interact; |
| 100 URLPatternSet matches; |
| 101 URLPatternSet::CreateDifference( |
| 102 automation_info_->matches, other->automation_info_->matches, &matches); |
| 103 return new AutomationManifestPermission( |
| 104 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); |
| 105 } |
| 106 |
| 107 virtual ManifestPermission* Union( |
| 108 const ManifestPermission* rhs) const OVERRIDE { |
| 109 const AutomationManifestPermission* other = |
| 110 static_cast<const AutomationManifestPermission*>(rhs); |
| 111 |
| 112 bool desktop = |
| 113 automation_info_->desktop || other->automation_info_->desktop; |
| 114 bool interact = |
| 115 automation_info_->interact || other->automation_info_->interact; |
| 116 URLPatternSet matches; |
| 117 URLPatternSet::CreateUnion( |
| 118 automation_info_->matches, other->automation_info_->matches, &matches); |
| 119 return new AutomationManifestPermission( |
| 120 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); |
| 121 } |
| 122 |
| 123 virtual ManifestPermission* Intersect( |
| 124 const ManifestPermission* rhs) const OVERRIDE { |
| 125 const AutomationManifestPermission* other = |
| 126 static_cast<const AutomationManifestPermission*>(rhs); |
| 127 |
| 128 bool desktop = |
| 129 automation_info_->desktop && other->automation_info_->desktop; |
| 130 bool interact = |
| 131 automation_info_->interact && other->automation_info_->interact; |
| 132 URLPatternSet matches; |
| 133 URLPatternSet::CreateIntersection( |
| 134 automation_info_->matches, other->automation_info_->matches, &matches); |
| 135 return new AutomationManifestPermission( |
| 136 make_scoped_ptr(new const AutomationInfo(desktop, matches, interact))); |
| 137 } |
| 138 |
| 139 private: |
| 140 scoped_ptr<const AutomationInfo> automation_info_; |
| 141 }; |
| 142 |
33 AutomationHandler::AutomationHandler() { | 143 AutomationHandler::AutomationHandler() { |
34 } | 144 } |
35 | 145 |
36 AutomationHandler::~AutomationHandler() { | 146 AutomationHandler::~AutomationHandler() { |
37 } | 147 } |
38 | 148 |
39 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { | 149 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { |
40 const base::Value* automation = NULL; | 150 const base::Value* automation = NULL; |
41 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); | 151 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); |
42 std::vector<InstallWarning> install_warnings; | 152 std::vector<InstallWarning> install_warnings; |
43 scoped_ptr<AutomationInfo> info = | 153 scoped_ptr<AutomationInfo> info = |
44 AutomationInfo::FromValue(*automation, &install_warnings, error); | 154 AutomationInfo::FromValue(*automation, &install_warnings, error); |
45 if (!error->empty()) | 155 if (!error->empty()) |
46 return false; | 156 return false; |
47 | 157 |
48 extension->AddInstallWarnings(install_warnings); | 158 extension->AddInstallWarnings(install_warnings); |
49 | 159 |
50 if (!info) | 160 if (!info) |
51 return true; | 161 return true; |
52 | 162 |
53 extension->SetManifestData(keys::kAutomation, info.release()); | 163 extension->SetManifestData(keys::kAutomation, info.release()); |
54 return true; | 164 return true; |
55 } | 165 } |
56 | 166 |
57 const std::vector<std::string> AutomationHandler::Keys() const { | 167 const std::vector<std::string> AutomationHandler::Keys() const { |
58 return SingleKey(keys::kAutomation); | 168 return SingleKey(keys::kAutomation); |
59 } | 169 } |
60 | 170 |
| 171 ManifestPermission* AutomationHandler::CreatePermission() { |
| 172 return new AutomationManifestPermission( |
| 173 make_scoped_ptr(new const AutomationInfo)); |
| 174 } |
| 175 |
| 176 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission( |
| 177 const Extension* extension) { |
| 178 const AutomationInfo* info = AutomationInfo::Get(extension); |
| 179 if (info) { |
| 180 return new AutomationManifestPermission( |
| 181 make_scoped_ptr(new const AutomationInfo( |
| 182 info->desktop, info->matches, info->interact))); |
| 183 } |
| 184 return NULL; |
| 185 } |
| 186 |
61 // static | 187 // static |
62 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { | 188 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { |
63 return static_cast<AutomationInfo*>( | 189 return static_cast<AutomationInfo*>( |
64 extension->GetManifestData(keys::kAutomation)); | 190 extension->GetManifestData(keys::kAutomation)); |
65 } | 191 } |
66 | 192 |
67 // static | 193 // static |
68 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( | 194 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( |
69 const base::Value& value, | 195 const base::Value& value, |
70 std::vector<InstallWarning>* install_warnings, | 196 std::vector<InstallWarning>* install_warnings, |
71 base::string16* error) { | 197 base::string16* error) { |
72 scoped_ptr<Automation> automation = Automation::FromValue(value, error); | 198 scoped_ptr<Automation> automation = Automation::FromValue(value, error); |
73 if (!automation) | 199 if (!automation) |
74 return scoped_ptr<AutomationInfo>(); | 200 return scoped_ptr<AutomationInfo>(); |
75 | 201 |
76 if (automation->as_boolean) { | 202 if (automation->as_boolean) { |
77 if (*automation->as_boolean) | 203 if (*automation->as_boolean) |
78 return make_scoped_ptr(new AutomationInfo()); | 204 return make_scoped_ptr(new AutomationInfo()); |
79 return scoped_ptr<AutomationInfo>(); | 205 return scoped_ptr<AutomationInfo>(); |
80 } | 206 } |
81 const Automation::Object& automation_object = *automation->as_object; | 207 Automation::Object& automation_object = *automation->as_object; |
82 | 208 |
83 bool desktop = false; | 209 bool desktop = false; |
84 bool interact = false; | 210 bool interact = false; |
85 if (automation_object.desktop && *automation_object.desktop) { | 211 if (automation_object.desktop && *automation_object.desktop) { |
86 desktop = true; | 212 desktop = true; |
87 interact = true; | 213 interact = true; |
88 if (automation_object.interact && !*automation_object.interact) { | 214 if (automation_object.interact && !*automation_object.interact) { |
89 // TODO(aboxhall): Do we want to allow this? | 215 // TODO(aboxhall): Do we want to allow this? |
90 install_warnings->push_back( | 216 install_warnings->push_back( |
91 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); | 217 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); |
92 } | 218 } |
93 } else if (automation_object.interact && *automation_object.interact) { | 219 } else if (automation_object.interact && *automation_object.interact) { |
94 interact = true; | 220 interact = true; |
95 } | 221 } |
96 | 222 |
97 URLPatternSet matches; | 223 URLPatternSet matches; |
98 bool specified_matches = false; | 224 bool specified_matches = false; |
99 if (automation_object.matches) { | 225 if (automation_object.matches) { |
100 if (desktop) { | 226 if (desktop) { |
101 install_warnings->push_back( | 227 install_warnings->push_back( |
102 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); | 228 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); |
103 } else { | 229 } else { |
104 specified_matches = true; | 230 specified_matches = true; |
| 231 |
105 for (std::vector<std::string>::iterator it = | 232 for (std::vector<std::string>::iterator it = |
106 automation_object.matches->begin(); | 233 automation_object.matches->begin(); |
107 it != automation_object.matches->end(); | 234 it != automation_object.matches->end(); |
108 ++it) { | 235 ++it) { |
109 // TODO(aboxhall): Refactor common logic from content_scripts_handler, | 236 // TODO(aboxhall): Refactor common logic from content_scripts_handler, |
110 // manifest_url_handler and user_script.cc into a single location and | 237 // manifest_url_handler and user_script.cc into a single location and |
111 // re-use here. | 238 // re-use here. |
112 URLPattern pattern(URLPattern::SCHEME_ALL & | 239 URLPattern pattern(URLPattern::SCHEME_ALL & |
113 ~URLPattern::SCHEME_CHROMEUI); | 240 ~URLPattern::SCHEME_CHROMEUI); |
114 URLPattern::ParseResult parse_result = pattern.Parse(*it); | 241 URLPattern::ParseResult parse_result = pattern.Parse(*it); |
| 242 |
115 if (parse_result != URLPattern::PARSE_SUCCESS) { | 243 if (parse_result != URLPattern::PARSE_SUCCESS) { |
116 install_warnings->push_back( | 244 install_warnings->push_back( |
117 InstallWarning(ErrorUtils::FormatErrorMessage( | 245 InstallWarning(ErrorUtils::FormatErrorMessage( |
118 automation_errors::kErrorInvalidMatch, | 246 automation_errors::kErrorInvalidMatch, |
119 *it, | 247 *it, |
120 URLPattern::GetParseResultString(parse_result)))); | 248 URLPattern::GetParseResultString(parse_result)))); |
121 continue; | 249 continue; |
122 } | 250 } |
123 | 251 |
124 matches.AddPattern(pattern); | 252 matches.AddPattern(pattern); |
125 } | 253 } |
126 } | 254 } |
127 } | 255 } |
128 if (specified_matches && matches.is_empty()) | 256 if (specified_matches && matches.is_empty()) { |
129 install_warnings->push_back( | 257 install_warnings->push_back( |
130 InstallWarning(automation_errors::kErrorNoMatchesProvided)); | 258 InstallWarning(automation_errors::kErrorNoMatchesProvided)); |
| 259 } |
131 | 260 |
132 return make_scoped_ptr( | 261 return make_scoped_ptr(new AutomationInfo(desktop, matches, interact)); |
133 new AutomationInfo(desktop, matches, interact, specified_matches)); | |
134 } | 262 } |
135 | 263 |
136 AutomationInfo::AutomationInfo() | 264 // static |
137 : desktop(false), interact(false), specified_matches(false) { | 265 scoped_ptr<base::Value> AutomationInfo::ToValue(const AutomationInfo& info) { |
| 266 return AsManifestType(info)->ToValue().Pass(); |
138 } | 267 } |
| 268 |
| 269 // static |
| 270 scoped_ptr<Automation> AutomationInfo::AsManifestType( |
| 271 const AutomationInfo& info) { |
| 272 scoped_ptr<Automation> automation(new Automation); |
| 273 if (!info.desktop && !info.interact && info.matches.size() == 0) { |
| 274 automation->as_boolean.reset(new bool(true)); |
| 275 return automation.Pass(); |
| 276 } |
| 277 |
| 278 Automation::Object* as_object = new Automation::Object; |
| 279 as_object->desktop.reset(new bool(info.desktop)); |
| 280 as_object->interact.reset(new bool(info.interact)); |
| 281 if (info.matches.size() > 0) { |
| 282 as_object->matches.reset(info.matches.ToStringVector().release()); |
| 283 } |
| 284 automation->as_object.reset(as_object); |
| 285 return automation.Pass(); |
| 286 } |
| 287 |
| 288 AutomationInfo::AutomationInfo() : desktop(false), interact(false) { |
| 289 } |
| 290 |
139 AutomationInfo::AutomationInfo(bool desktop, | 291 AutomationInfo::AutomationInfo(bool desktop, |
140 const URLPatternSet& matches, | 292 const URLPatternSet matches, |
141 bool interact, | 293 bool interact) |
142 bool specified_matches) | 294 : desktop(desktop), matches(matches), interact(interact) { |
143 : desktop(desktop), | |
144 matches(matches), | |
145 interact(interact), | |
146 specified_matches(specified_matches) { | |
147 } | 295 } |
148 | 296 |
149 AutomationInfo::~AutomationInfo() { | 297 AutomationInfo::~AutomationInfo() { |
150 } | 298 } |
151 | 299 |
152 } // namespace extensions | 300 } // namespace extensions |
OLD | NEW |