Chromium Code Reviews| 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 scoped_ptr<const AutomationInfo> info(new AutomationInfo); | |
|
Devlin
2014/07/21 19:45:35
inline |info|
aboxhall
2014/07/21 22:44:11
Done.
| |
| 173 return new AutomationManifestPermission(info.Pass()); | |
| 174 } | |
| 175 | |
| 176 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission( | |
| 177 const Extension* extension) { | |
| 178 const AutomationInfo* info = AutomationInfo::Get(extension); | |
| 179 if (info) | |
| 180 return new AutomationManifestPermission(AutomationInfo::Clone(*info)); | |
| 181 return NULL; | |
| 182 } | |
| 183 | |
| 61 // static | 184 // static |
| 62 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { | 185 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { |
| 63 return static_cast<AutomationInfo*>( | 186 return static_cast<AutomationInfo*>( |
| 64 extension->GetManifestData(keys::kAutomation)); | 187 extension->GetManifestData(keys::kAutomation)); |
| 65 } | 188 } |
| 66 | 189 |
| 67 // static | 190 // static |
| 191 scoped_ptr<AutomationInfo> AutomationInfo::FromValue(const base::Value& value) { | |
|
Devlin
2014/07/21 19:45:35
Is this used?
aboxhall
2014/07/21 22:44:11
Not any more! Removed.
| |
| 192 std::vector<InstallWarning> install_warnings; | |
| 193 base::string16 error; | |
| 194 return AutomationInfo::FromValue(value, &install_warnings, &error); | |
| 195 } | |
| 196 | |
| 197 // static | |
| 68 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( | 198 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( |
| 69 const base::Value& value, | 199 const base::Value& value, |
| 70 std::vector<InstallWarning>* install_warnings, | 200 std::vector<InstallWarning>* install_warnings, |
| 71 base::string16* error) { | 201 base::string16* error) { |
| 72 scoped_ptr<Automation> automation = Automation::FromValue(value, error); | 202 scoped_ptr<Automation> automation = Automation::FromValue(value, error); |
| 73 if (!automation) | 203 if (!automation) |
| 74 return scoped_ptr<AutomationInfo>(); | 204 return scoped_ptr<AutomationInfo>(); |
| 75 | 205 |
| 76 if (automation->as_boolean) { | 206 if (automation->as_boolean) { |
| 77 if (*automation->as_boolean) | 207 if (*automation->as_boolean) |
| 78 return make_scoped_ptr(new AutomationInfo()); | 208 return make_scoped_ptr(new AutomationInfo()); |
| 79 return scoped_ptr<AutomationInfo>(); | 209 return scoped_ptr<AutomationInfo>(); |
| 80 } | 210 } |
| 81 const Automation::Object& automation_object = *automation->as_object; | 211 Automation::Object& automation_object = *automation->as_object; |
| 82 | 212 |
| 83 bool desktop = false; | 213 bool desktop = false; |
| 84 bool interact = false; | 214 bool interact = false; |
| 85 if (automation_object.desktop && *automation_object.desktop) { | 215 if (automation_object.desktop && *automation_object.desktop) { |
| 86 desktop = true; | 216 desktop = true; |
| 87 interact = true; | 217 interact = true; |
| 88 if (automation_object.interact && !*automation_object.interact) { | 218 if (automation_object.interact && !*automation_object.interact) { |
| 89 // TODO(aboxhall): Do we want to allow this? | 219 // TODO(aboxhall): Do we want to allow this? |
| 90 install_warnings->push_back( | 220 install_warnings->push_back( |
| 91 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); | 221 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); |
| 92 } | 222 } |
| 93 } else if (automation_object.interact && *automation_object.interact) { | 223 } else if (automation_object.interact && *automation_object.interact) { |
| 94 interact = true; | 224 interact = true; |
| 95 } | 225 } |
| 96 | 226 |
| 97 URLPatternSet matches; | 227 URLPatternSet matches; |
| 98 bool specified_matches = false; | 228 bool specified_matches = false; |
| 99 if (automation_object.matches) { | 229 if (automation_object.matches) { |
| 100 if (desktop) { | 230 if (desktop) { |
| 101 install_warnings->push_back( | 231 install_warnings->push_back( |
| 102 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); | 232 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); |
| 103 } else { | 233 } else { |
| 104 specified_matches = true; | 234 specified_matches = true; |
| 235 | |
| 105 for (std::vector<std::string>::iterator it = | 236 for (std::vector<std::string>::iterator it = |
| 106 automation_object.matches->begin(); | 237 automation_object.matches->begin(); |
| 107 it != automation_object.matches->end(); | 238 it != automation_object.matches->end(); |
| 108 ++it) { | 239 ++it) { |
| 109 // TODO(aboxhall): Refactor common logic from content_scripts_handler, | 240 // TODO(aboxhall): Refactor common logic from content_scripts_handler, |
| 110 // manifest_url_handler and user_script.cc into a single location and | 241 // manifest_url_handler and user_script.cc into a single location and |
| 111 // re-use here. | 242 // re-use here. |
| 112 URLPattern pattern(URLPattern::SCHEME_ALL & | 243 URLPattern pattern(URLPattern::SCHEME_ALL & |
| 113 ~URLPattern::SCHEME_CHROMEUI); | 244 ~URLPattern::SCHEME_CHROMEUI); |
| 114 URLPattern::ParseResult parse_result = pattern.Parse(*it); | 245 URLPattern::ParseResult parse_result = pattern.Parse(*it); |
| 246 | |
| 115 if (parse_result != URLPattern::PARSE_SUCCESS) { | 247 if (parse_result != URLPattern::PARSE_SUCCESS) { |
| 116 install_warnings->push_back( | 248 install_warnings->push_back( |
| 117 InstallWarning(ErrorUtils::FormatErrorMessage( | 249 InstallWarning(ErrorUtils::FormatErrorMessage( |
| 118 automation_errors::kErrorInvalidMatch, | 250 automation_errors::kErrorInvalidMatch, |
| 119 *it, | 251 *it, |
| 120 URLPattern::GetParseResultString(parse_result)))); | 252 URLPattern::GetParseResultString(parse_result)))); |
| 121 continue; | 253 continue; |
| 122 } | 254 } |
| 123 | 255 |
| 124 matches.AddPattern(pattern); | 256 matches.AddPattern(pattern); |
| 125 } | 257 } |
| 126 } | 258 } |
| 127 } | 259 } |
| 128 if (specified_matches && matches.is_empty()) | 260 if (specified_matches && matches.is_empty()) { |
| 129 install_warnings->push_back( | 261 install_warnings->push_back( |
| 130 InstallWarning(automation_errors::kErrorNoMatchesProvided)); | 262 InstallWarning(automation_errors::kErrorNoMatchesProvided)); |
| 263 } | |
| 131 | 264 |
| 132 return make_scoped_ptr( | 265 return make_scoped_ptr(new AutomationInfo(desktop, matches, interact)); |
| 133 new AutomationInfo(desktop, matches, interact, specified_matches)); | |
| 134 } | 266 } |
| 135 | 267 |
| 136 AutomationInfo::AutomationInfo() | 268 // static |
| 137 : desktop(false), interact(false), specified_matches(false) { | 269 scoped_ptr<base::Value> AutomationInfo::ToValue(const AutomationInfo& info) { |
| 270 return AsManifestType(info)->ToValue().Pass(); | |
|
Devlin
2014/07/21 19:45:35
Are you anticipating using AsManifestType more lat
aboxhall
2014/07/21 22:44:11
I'm unlikely to use it again, but I think it's mor
Devlin
2014/07/21 23:43:53
Not worried about performance; just like to keep t
| |
| 138 } | 271 } |
| 272 | |
| 273 // static | |
| 274 scoped_ptr<Automation> AutomationInfo::AsManifestType( | |
| 275 const AutomationInfo& info) { | |
| 276 scoped_ptr<Automation> automation(new Automation); | |
| 277 if (!info.desktop && !info.interact && info.matches.size() == 0) { | |
| 278 automation->as_boolean.reset(new bool(true)); | |
| 279 return automation.Pass(); | |
| 280 } | |
| 281 | |
| 282 Automation::Object* as_object = new Automation::Object; | |
| 283 as_object->desktop.reset(new bool(info.desktop)); | |
| 284 as_object->interact.reset(new bool(info.interact)); | |
| 285 if (info.matches.size() > 0) { | |
| 286 as_object->matches.reset(info.matches.ToStringVector().release()); | |
| 287 } | |
| 288 automation->as_object.reset(as_object); | |
| 289 return automation.Pass(); | |
| 290 } | |
| 291 | |
| 292 // static | |
| 293 scoped_ptr<const AutomationInfo> AutomationInfo::Clone( | |
|
Devlin
2014/07/21 19:45:35
If you don't plan on using Clone() elsewhere, can
aboxhall
2014/07/21 22:44:11
Done.
| |
| 294 const AutomationInfo& info) { | |
| 295 const AutomationInfo* result = | |
| 296 new AutomationInfo(info.desktop, info.matches, info.interact); | |
| 297 return make_scoped_ptr(result); | |
|
Devlin
2014/07/21 19:45:35
inline?
aboxhall
2014/07/21 22:44:11
Acknowledged.
| |
| 298 } | |
| 299 | |
| 300 AutomationInfo::AutomationInfo() : desktop(false), interact(false) { | |
| 301 } | |
| 302 | |
| 139 AutomationInfo::AutomationInfo(bool desktop, | 303 AutomationInfo::AutomationInfo(bool desktop, |
| 140 const URLPatternSet& matches, | 304 const URLPatternSet matches, |
| 141 bool interact, | 305 bool interact) |
| 142 bool specified_matches) | 306 : desktop(desktop), matches(matches), interact(interact) { |
| 143 : desktop(desktop), | |
| 144 matches(matches), | |
| 145 interact(interact), | |
| 146 specified_matches(specified_matches) { | |
| 147 } | 307 } |
| 148 | 308 |
| 149 AutomationInfo::~AutomationInfo() { | 309 AutomationInfo::~AutomationInfo() { |
| 150 } | 310 } |
| 151 | 311 |
| 152 } // namespace extensions | 312 } // namespace extensions |
| OLD | NEW |