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" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/common/extensions/api/manifest_types.h" | 9 #include "chrome/common/extensions/api/manifest_types.h" |
| 10 #include "extensions/common/error_utils.h" | 10 #include "extensions/common/error_utils.h" |
| 11 #include "extensions/common/extensions_client.h" | |
| 11 #include "extensions/common/manifest_constants.h" | 12 #include "extensions/common/manifest_constants.h" |
| 12 #include "extensions/common/permissions/api_permission_set.h" | 13 #include "extensions/common/permissions/api_permission_set.h" |
| 14 #include "extensions/common/permissions/manifest_permission.h" | |
| 15 #include "extensions/common/permissions/permission_message.h" | |
| 16 #include "extensions/common/permissions/permission_message_util.h" | |
| 13 #include "extensions/common/permissions/permissions_data.h" | 17 #include "extensions/common/permissions/permissions_data.h" |
| 14 #include "extensions/common/url_pattern.h" | 18 #include "extensions/common/url_pattern.h" |
| 19 #include "grit/generated_resources.h" | |
| 20 #include "ipc/ipc_message.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 { | |
|
David Tseng
2014/07/08 18:37:39
I personally would prefer if this class lived in i
aboxhall
2014/07/18 15:55:15
Yeah, I'm in two minds about this, and there are e
| |
| 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* Clone() const OVERRIDE { | |
| 92 scoped_ptr<const AutomationInfo> info( | |
| 93 AutomationInfo::Clone(*automation_info_.get())); | |
| 94 return new AutomationManifestPermission(info.Pass()); | |
| 95 } | |
| 96 | |
| 97 virtual ManifestPermission* Diff( | |
| 98 const ManifestPermission* rhs) const OVERRIDE { | |
| 99 const AutomationManifestPermission* other = | |
| 100 static_cast<const AutomationManifestPermission*>(rhs); | |
| 101 | |
| 102 bool desktop = | |
| 103 automation_info_->desktop && !other->automation_info_->desktop; | |
| 104 bool interact = | |
| 105 automation_info_->interact && !other->automation_info_->interact; | |
| 106 URLPatternSet matches; | |
| 107 URLPatternSet::CreateDifference( | |
| 108 automation_info_->matches, other->automation_info_->matches, &matches); | |
| 109 scoped_ptr<const AutomationInfo> info( | |
| 110 new AutomationInfo(desktop, matches, interact)); | |
| 111 return new AutomationManifestPermission(info.Pass()); | |
| 112 } | |
| 113 | |
| 114 virtual ManifestPermission* Union( | |
| 115 const ManifestPermission* rhs) const OVERRIDE { | |
| 116 const AutomationManifestPermission* other = | |
| 117 static_cast<const AutomationManifestPermission*>(rhs); | |
| 118 | |
| 119 bool desktop = | |
| 120 automation_info_->desktop || other->automation_info_->desktop; | |
| 121 bool interact = | |
| 122 automation_info_->interact || other->automation_info_->interact; | |
| 123 URLPatternSet matches; | |
| 124 URLPatternSet::CreateUnion( | |
| 125 automation_info_->matches, other->automation_info_->matches, &matches); | |
| 126 scoped_ptr<const AutomationInfo> info( | |
| 127 new AutomationInfo(desktop, matches, interact)); | |
| 128 return new AutomationManifestPermission(info.Pass()); | |
| 129 } | |
| 130 | |
| 131 virtual ManifestPermission* Intersect( | |
| 132 const ManifestPermission* rhs) const OVERRIDE { | |
| 133 const AutomationManifestPermission* other = | |
| 134 static_cast<const AutomationManifestPermission*>(rhs); | |
| 135 | |
| 136 bool desktop = | |
| 137 automation_info_->desktop && other->automation_info_->desktop; | |
| 138 bool interact = | |
| 139 automation_info_->interact && other->automation_info_->interact; | |
| 140 URLPatternSet matches; | |
| 141 URLPatternSet::CreateIntersection( | |
| 142 automation_info_->matches, other->automation_info_->matches, &matches); | |
| 143 scoped_ptr<const AutomationInfo> info( | |
| 144 new AutomationInfo(desktop, matches, interact)); | |
| 145 return new AutomationManifestPermission(info.Pass()); | |
| 146 } | |
| 147 | |
| 148 virtual bool Contains(const ManifestPermission* rhs) const OVERRIDE { | |
| 149 const AutomationManifestPermission* other = | |
| 150 static_cast<const AutomationManifestPermission*>(rhs); | |
| 151 | |
| 152 bool contains_desktop = | |
| 153 automation_info_->desktop || !other->automation_info_->desktop; | |
| 154 bool contains_interact = | |
| 155 automation_info_->interact || !other->automation_info_->interact; | |
| 156 bool contains_matches = | |
| 157 automation_info_->matches.Contains(other->automation_info_->matches); | |
| 158 | |
| 159 return contains_desktop && contains_interact && contains_matches; | |
| 160 } | |
| 161 | |
| 162 virtual bool Equal(const ManifestPermission* rhs) const OVERRIDE { | |
| 163 const AutomationManifestPermission* other = | |
| 164 static_cast<const AutomationManifestPermission*>(rhs); | |
| 165 | |
| 166 bool same_desktop = | |
| 167 automation_info_->desktop == other->automation_info_->desktop; | |
| 168 bool same_interact = | |
| 169 automation_info_->interact == other->automation_info_->interact; | |
| 170 bool same_matches = | |
| 171 automation_info_->matches == other->automation_info_->matches; | |
| 172 | |
| 173 return same_desktop && same_interact && same_matches; | |
| 174 } | |
| 175 | |
| 176 virtual void Write(IPC::Message* m) const OVERRIDE { | |
| 177 // IPC::WriteParam(m); | |
| 178 } | |
| 179 | |
| 180 virtual bool Read(const IPC::Message* m, PickleIterator* iter) OVERRIDE { | |
| 181 // return IPC::ReadParam(m, iter, &override_bookmarks_ui_permission_); | |
| 182 return false; | |
| 183 } | |
| 184 | |
| 185 virtual void Log(std::string* log) const OVERRIDE { | |
| 186 // IPC::LogParam(override_bookmarks_ui_permission_, log); | |
| 187 } | |
| 188 | |
| 189 private: | |
| 190 scoped_ptr<const AutomationInfo> automation_info_; | |
| 191 }; | |
| 192 | |
| 33 AutomationHandler::AutomationHandler() { | 193 AutomationHandler::AutomationHandler() { |
| 34 } | 194 } |
| 35 | 195 |
| 36 AutomationHandler::~AutomationHandler() { | 196 AutomationHandler::~AutomationHandler() { |
| 37 } | 197 } |
| 38 | 198 |
| 39 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { | 199 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { |
| 40 const base::Value* automation = NULL; | 200 const base::Value* automation = NULL; |
| 41 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); | 201 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); |
| 42 std::vector<InstallWarning> install_warnings; | 202 std::vector<InstallWarning> install_warnings; |
| 43 scoped_ptr<AutomationInfo> info = | 203 scoped_ptr<AutomationInfo> info = |
| 44 AutomationInfo::FromValue(*automation, &install_warnings, error); | 204 AutomationInfo::FromValue(*automation, &install_warnings, error); |
| 45 if (!error->empty()) | 205 if (!error->empty()) |
| 46 return false; | 206 return false; |
| 47 | 207 |
| 48 extension->AddInstallWarnings(install_warnings); | 208 extension->AddInstallWarnings(install_warnings); |
| 49 | 209 |
| 50 if (!info) | 210 if (!info) |
| 51 return true; | 211 return true; |
| 52 | 212 |
| 53 extension->SetManifestData(keys::kAutomation, info.release()); | 213 extension->SetManifestData(keys::kAutomation, info.release()); |
| 54 return true; | 214 return true; |
| 55 } | 215 } |
| 56 | 216 |
| 57 const std::vector<std::string> AutomationHandler::Keys() const { | 217 const std::vector<std::string> AutomationHandler::Keys() const { |
| 58 return SingleKey(keys::kAutomation); | 218 return SingleKey(keys::kAutomation); |
| 59 } | 219 } |
| 60 | 220 |
| 221 ManifestPermission* AutomationHandler::CreatePermission() { | |
| 222 scoped_ptr<const AutomationInfo> info(new AutomationInfo); | |
| 223 return new AutomationManifestPermission(info.Pass()); | |
| 224 } | |
| 225 | |
| 226 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission( | |
| 227 const Extension* extension) { | |
| 228 const AutomationInfo* info = AutomationInfo::Get(extension); | |
| 229 if (info) | |
| 230 return new AutomationManifestPermission(AutomationInfo::Clone(*info)); | |
| 231 return NULL; | |
| 232 } | |
| 233 | |
| 61 // static | 234 // static |
| 62 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { | 235 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { |
| 63 return static_cast<AutomationInfo*>( | 236 return static_cast<AutomationInfo*>( |
| 64 extension->GetManifestData(keys::kAutomation)); | 237 extension->GetManifestData(keys::kAutomation)); |
| 65 } | 238 } |
| 66 | 239 |
| 67 // static | 240 // static |
| 241 scoped_ptr<AutomationInfo> AutomationInfo::FromValue(const base::Value& value) { | |
| 242 std::vector<InstallWarning> install_warnings; | |
| 243 base::string16 error; | |
| 244 return AutomationInfo::FromValue(value, &install_warnings, &error); | |
| 245 } | |
| 246 | |
| 247 // static | |
| 68 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( | 248 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( |
| 69 const base::Value& value, | 249 const base::Value& value, |
| 70 std::vector<InstallWarning>* install_warnings, | 250 std::vector<InstallWarning>* install_warnings, |
| 71 base::string16* error) { | 251 base::string16* error) { |
| 72 scoped_ptr<Automation> automation = Automation::FromValue(value, error); | 252 scoped_ptr<Automation> automation = Automation::FromValue(value, error); |
| 73 if (!automation) | 253 if (!automation) |
| 74 return scoped_ptr<AutomationInfo>(); | 254 return scoped_ptr<AutomationInfo>(); |
| 75 | 255 |
| 76 if (automation->as_boolean) { | 256 if (automation->as_boolean) { |
| 77 if (*automation->as_boolean) | 257 if (*automation->as_boolean) |
| 78 return make_scoped_ptr(new AutomationInfo()); | 258 return make_scoped_ptr(new AutomationInfo()); |
| 79 return scoped_ptr<AutomationInfo>(); | 259 return scoped_ptr<AutomationInfo>(); |
| 80 } | 260 } |
| 81 const Automation::Object& automation_object = *automation->as_object; | 261 Automation::Object& automation_object = *automation->as_object; |
| 82 | 262 |
| 83 bool desktop = false; | 263 bool desktop = false; |
| 84 bool interact = false; | 264 bool interact = false; |
| 85 if (automation_object.desktop && *automation_object.desktop) { | 265 if (automation_object.desktop && *automation_object.desktop) { |
| 86 desktop = true; | 266 desktop = true; |
| 87 interact = true; | 267 interact = true; |
| 88 if (automation_object.interact && !*automation_object.interact) { | 268 if (automation_object.interact && !*automation_object.interact) { |
| 89 // TODO(aboxhall): Do we want to allow this? | 269 // TODO(aboxhall): Do we want to allow this? |
| 90 install_warnings->push_back( | 270 install_warnings->push_back( |
| 91 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); | 271 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); |
| 92 } | 272 } |
| 93 } else if (automation_object.interact && *automation_object.interact) { | 273 } else if (automation_object.interact && *automation_object.interact) { |
| 94 interact = true; | 274 interact = true; |
| 95 } | 275 } |
| 96 | 276 |
| 97 URLPatternSet matches; | 277 URLPatternSet matches; |
| 98 bool specified_matches = false; | 278 bool specified_matches = false; |
| 99 if (automation_object.matches) { | 279 if (automation_object.matches) { |
| 100 if (desktop) { | 280 if (desktop) { |
| 101 install_warnings->push_back( | 281 install_warnings->push_back( |
| 102 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); | 282 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); |
| 103 } else { | 283 } else { |
| 104 specified_matches = true; | 284 specified_matches = true; |
| 285 | |
| 105 for (std::vector<std::string>::iterator it = | 286 for (std::vector<std::string>::iterator it = |
| 106 automation_object.matches->begin(); | 287 automation_object.matches->begin(); |
| 107 it != automation_object.matches->end(); | 288 it != automation_object.matches->end(); |
| 108 ++it) { | 289 ++it) { |
| 109 // TODO(aboxhall): Refactor common logic from content_scripts_handler, | 290 // TODO(aboxhall): Refactor common logic from content_scripts_handler, |
| 110 // manifest_url_handler and user_script.cc into a single location and | 291 // manifest_url_handler and user_script.cc into a single location and |
| 111 // re-use here. | 292 // re-use here. |
| 112 URLPattern pattern(URLPattern::SCHEME_ALL & | 293 URLPattern pattern(URLPattern::SCHEME_ALL & |
| 113 ~URLPattern::SCHEME_CHROMEUI); | 294 ~URLPattern::SCHEME_CHROMEUI); |
| 114 URLPattern::ParseResult parse_result = pattern.Parse(*it); | 295 URLPattern::ParseResult parse_result = pattern.Parse(*it); |
| 296 | |
| 115 if (parse_result != URLPattern::PARSE_SUCCESS) { | 297 if (parse_result != URLPattern::PARSE_SUCCESS) { |
| 116 install_warnings->push_back( | 298 install_warnings->push_back( |
| 117 InstallWarning(ErrorUtils::FormatErrorMessage( | 299 InstallWarning(ErrorUtils::FormatErrorMessage( |
| 118 automation_errors::kErrorInvalidMatch, | 300 automation_errors::kErrorInvalidMatch, |
| 119 *it, | 301 *it, |
| 120 URLPattern::GetParseResultString(parse_result)))); | 302 URLPattern::GetParseResultString(parse_result)))); |
| 121 continue; | 303 continue; |
| 122 } | 304 } |
| 123 | 305 |
| 124 matches.AddPattern(pattern); | 306 matches.AddPattern(pattern); |
| 125 } | 307 } |
| 126 } | 308 } |
| 127 } | 309 } |
| 128 if (specified_matches && matches.is_empty()) | 310 if (specified_matches && matches.is_empty()) { |
| 129 install_warnings->push_back( | 311 install_warnings->push_back( |
| 130 InstallWarning(automation_errors::kErrorNoMatchesProvided)); | 312 InstallWarning(automation_errors::kErrorNoMatchesProvided)); |
| 313 } | |
| 131 | 314 |
| 132 return make_scoped_ptr( | 315 return make_scoped_ptr(new AutomationInfo(desktop, matches, interact)); |
| 133 new AutomationInfo(desktop, matches, interact, specified_matches)); | |
| 134 } | 316 } |
| 135 | 317 |
| 136 AutomationInfo::AutomationInfo() | 318 // static |
| 137 : desktop(false), interact(false), specified_matches(false) { | 319 scoped_ptr<base::Value> AutomationInfo::ToValue(const AutomationInfo& info) { |
| 320 return AsManifestType(info)->ToValue().Pass(); | |
| 138 } | 321 } |
| 322 | |
| 323 // static | |
| 324 scoped_ptr<Automation> AutomationInfo::AsManifestType( | |
| 325 const AutomationInfo& info) { | |
| 326 Automation* automation = new Automation; | |
| 327 if (!info.desktop && !info.interact && info.matches.size() == 0) { | |
| 328 automation->as_boolean.reset(new bool(true)); | |
| 329 return make_scoped_ptr(automation).Pass(); | |
| 330 } | |
| 331 | |
| 332 Automation::Object* as_object = new Automation::Object; | |
| 333 as_object->desktop.reset(new bool(info.desktop)); | |
| 334 as_object->interact.reset(new bool(info.interact)); | |
| 335 if (info.matches.size() > 0) { | |
| 336 as_object->matches.reset(info.matches.ToStringVector().release()); | |
| 337 } | |
| 338 automation->as_object.reset(as_object); | |
| 339 return make_scoped_ptr(automation).Pass(); | |
| 340 } | |
| 341 | |
| 342 // static | |
| 343 scoped_ptr<const AutomationInfo> AutomationInfo::Clone( | |
| 344 const AutomationInfo& info) { | |
| 345 scoped_ptr<base::Value> value = AutomationInfo::ToValue(info); | |
| 346 scoped_ptr<const AutomationInfo> result(AutomationInfo::FromValue(*value)); | |
| 347 return result.Pass(); | |
| 348 } | |
| 349 | |
| 350 AutomationInfo::AutomationInfo() : desktop(false), interact(false) { | |
| 351 } | |
| 352 | |
| 139 AutomationInfo::AutomationInfo(bool desktop, | 353 AutomationInfo::AutomationInfo(bool desktop, |
| 140 const URLPatternSet& matches, | 354 const URLPatternSet matches, |
| 141 bool interact, | 355 bool interact) |
| 142 bool specified_matches) | 356 : desktop(desktop), matches(matches), interact(interact) { |
| 143 : desktop(desktop), | |
| 144 matches(matches), | |
| 145 interact(interact), | |
| 146 specified_matches(specified_matches) { | |
| 147 } | 357 } |
| 148 | 358 |
| 149 AutomationInfo::~AutomationInfo() { | 359 AutomationInfo::~AutomationInfo() { |
| 150 } | 360 } |
| 151 | 361 |
| 152 } // namespace extensions | 362 } // namespace extensions |
| OLD | NEW |