Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/common/extensions/manifest_handlers/automation.cc

Issue 408493002: Make Clone, Contains, Equal and IPC function non-virtual and implement in manifest_permission.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src@warnings
Patch Set: Rebase to master Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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, &regular_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 scoped_ptr<const AutomationInfo> info(
104 new AutomationInfo(desktop, matches, interact));
105 return new AutomationManifestPermission(info.Pass());
106 }
107
108 virtual ManifestPermission* Union(
109 const ManifestPermission* rhs) const OVERRIDE {
110 const AutomationManifestPermission* other =
111 static_cast<const AutomationManifestPermission*>(rhs);
112
113 bool desktop =
114 automation_info_->desktop || other->automation_info_->desktop;
115 bool interact =
116 automation_info_->interact || other->automation_info_->interact;
117 URLPatternSet matches;
118 URLPatternSet::CreateUnion(
119 automation_info_->matches, other->automation_info_->matches, &matches);
120 scoped_ptr<const AutomationInfo> info(
121 new AutomationInfo(desktop, matches, interact));
122 return new AutomationManifestPermission(info.Pass());
123 }
124
125 virtual ManifestPermission* Intersect(
126 const ManifestPermission* rhs) const OVERRIDE {
127 const AutomationManifestPermission* other =
128 static_cast<const AutomationManifestPermission*>(rhs);
129
130 bool desktop =
131 automation_info_->desktop && other->automation_info_->desktop;
132 bool interact =
133 automation_info_->interact && other->automation_info_->interact;
134 URLPatternSet matches;
135 URLPatternSet::CreateIntersection(
136 automation_info_->matches, other->automation_info_->matches, &matches);
137 scoped_ptr<const AutomationInfo> info(
138 new AutomationInfo(desktop, matches, interact));
139 return new AutomationManifestPermission(info.Pass());
140 }
141
142 virtual bool Equal(const ManifestPermission* rhs) const OVERRIDE {
143 const AutomationManifestPermission* other =
144 static_cast<const AutomationManifestPermission*>(rhs);
145
146 bool same_desktop =
147 automation_info_->desktop == other->automation_info_->desktop;
148 bool same_interact =
149 automation_info_->interact == other->automation_info_->interact;
150 bool same_matches =
151 automation_info_->matches == other->automation_info_->matches;
152
153 return same_desktop && same_interact && same_matches;
154 }
155
156 private:
157 scoped_ptr<const AutomationInfo> automation_info_;
158 };
159
33 AutomationHandler::AutomationHandler() { 160 AutomationHandler::AutomationHandler() {
34 } 161 }
35 162
36 AutomationHandler::~AutomationHandler() { 163 AutomationHandler::~AutomationHandler() {
37 } 164 }
38 165
39 bool AutomationHandler::Parse(Extension* extension, base::string16* error) { 166 bool AutomationHandler::Parse(Extension* extension, base::string16* error) {
40 const base::Value* automation = NULL; 167 const base::Value* automation = NULL;
41 CHECK(extension->manifest()->Get(keys::kAutomation, &automation)); 168 CHECK(extension->manifest()->Get(keys::kAutomation, &automation));
42 std::vector<InstallWarning> install_warnings; 169 std::vector<InstallWarning> install_warnings;
43 scoped_ptr<AutomationInfo> info = 170 scoped_ptr<AutomationInfo> info =
44 AutomationInfo::FromValue(*automation, &install_warnings, error); 171 AutomationInfo::FromValue(*automation, &install_warnings, error);
45 if (!error->empty()) 172 if (!error->empty())
46 return false; 173 return false;
47 174
48 extension->AddInstallWarnings(install_warnings); 175 extension->AddInstallWarnings(install_warnings);
49 176
50 if (!info) 177 if (!info)
51 return true; 178 return true;
52 179
53 extension->SetManifestData(keys::kAutomation, info.release()); 180 extension->SetManifestData(keys::kAutomation, info.release());
54 return true; 181 return true;
55 } 182 }
56 183
57 const std::vector<std::string> AutomationHandler::Keys() const { 184 const std::vector<std::string> AutomationHandler::Keys() const {
58 return SingleKey(keys::kAutomation); 185 return SingleKey(keys::kAutomation);
59 } 186 }
60 187
188 ManifestPermission* AutomationHandler::CreatePermission() {
189 scoped_ptr<const AutomationInfo> info(new AutomationInfo);
190 return new AutomationManifestPermission(info.Pass());
191 }
192
193 ManifestPermission* AutomationHandler::CreateInitialRequiredPermission(
194 const Extension* extension) {
195 const AutomationInfo* info = AutomationInfo::Get(extension);
196 if (info)
197 return new AutomationManifestPermission(AutomationInfo::Clone(*info));
198 return NULL;
199 }
200
61 // static 201 // static
62 const AutomationInfo* AutomationInfo::Get(const Extension* extension) { 202 const AutomationInfo* AutomationInfo::Get(const Extension* extension) {
63 return static_cast<AutomationInfo*>( 203 return static_cast<AutomationInfo*>(
64 extension->GetManifestData(keys::kAutomation)); 204 extension->GetManifestData(keys::kAutomation));
65 } 205 }
66 206
67 // static 207 // static
208 scoped_ptr<AutomationInfo> AutomationInfo::FromValue(const base::Value& value) {
209 std::vector<InstallWarning> install_warnings;
210 base::string16 error;
211 return AutomationInfo::FromValue(value, &install_warnings, &error);
212 }
213
214 // static
68 scoped_ptr<AutomationInfo> AutomationInfo::FromValue( 215 scoped_ptr<AutomationInfo> AutomationInfo::FromValue(
69 const base::Value& value, 216 const base::Value& value,
70 std::vector<InstallWarning>* install_warnings, 217 std::vector<InstallWarning>* install_warnings,
71 base::string16* error) { 218 base::string16* error) {
72 scoped_ptr<Automation> automation = Automation::FromValue(value, error); 219 scoped_ptr<Automation> automation = Automation::FromValue(value, error);
73 if (!automation) 220 if (!automation)
74 return scoped_ptr<AutomationInfo>(); 221 return scoped_ptr<AutomationInfo>();
75 222
76 if (automation->as_boolean) { 223 if (automation->as_boolean) {
77 if (*automation->as_boolean) 224 if (*automation->as_boolean)
78 return make_scoped_ptr(new AutomationInfo()); 225 return make_scoped_ptr(new AutomationInfo());
79 return scoped_ptr<AutomationInfo>(); 226 return scoped_ptr<AutomationInfo>();
80 } 227 }
81 const Automation::Object& automation_object = *automation->as_object; 228 Automation::Object& automation_object = *automation->as_object;
82 229
83 bool desktop = false; 230 bool desktop = false;
84 bool interact = false; 231 bool interact = false;
85 if (automation_object.desktop && *automation_object.desktop) { 232 if (automation_object.desktop && *automation_object.desktop) {
86 desktop = true; 233 desktop = true;
87 interact = true; 234 interact = true;
88 if (automation_object.interact && !*automation_object.interact) { 235 if (automation_object.interact && !*automation_object.interact) {
89 // TODO(aboxhall): Do we want to allow this? 236 // TODO(aboxhall): Do we want to allow this?
90 install_warnings->push_back( 237 install_warnings->push_back(
91 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse)); 238 InstallWarning(automation_errors::kErrorDesktopTrueInteractFalse));
92 } 239 }
93 } else if (automation_object.interact && *automation_object.interact) { 240 } else if (automation_object.interact && *automation_object.interact) {
94 interact = true; 241 interact = true;
95 } 242 }
96 243
97 URLPatternSet matches; 244 URLPatternSet matches;
98 bool specified_matches = false; 245 bool specified_matches = false;
99 if (automation_object.matches) { 246 if (automation_object.matches) {
100 if (desktop) { 247 if (desktop) {
101 install_warnings->push_back( 248 install_warnings->push_back(
102 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified)); 249 InstallWarning(automation_errors::kErrorDesktopTrueMatchesSpecified));
103 } else { 250 } else {
104 specified_matches = true; 251 specified_matches = true;
252
105 for (std::vector<std::string>::iterator it = 253 for (std::vector<std::string>::iterator it =
106 automation_object.matches->begin(); 254 automation_object.matches->begin();
107 it != automation_object.matches->end(); 255 it != automation_object.matches->end();
108 ++it) { 256 ++it) {
109 // TODO(aboxhall): Refactor common logic from content_scripts_handler, 257 // TODO(aboxhall): Refactor common logic from content_scripts_handler,
110 // manifest_url_handler and user_script.cc into a single location and 258 // manifest_url_handler and user_script.cc into a single location and
111 // re-use here. 259 // re-use here.
112 URLPattern pattern(URLPattern::SCHEME_ALL & 260 URLPattern pattern(URLPattern::SCHEME_ALL &
113 ~URLPattern::SCHEME_CHROMEUI); 261 ~URLPattern::SCHEME_CHROMEUI);
114 URLPattern::ParseResult parse_result = pattern.Parse(*it); 262 URLPattern::ParseResult parse_result = pattern.Parse(*it);
263
115 if (parse_result != URLPattern::PARSE_SUCCESS) { 264 if (parse_result != URLPattern::PARSE_SUCCESS) {
116 install_warnings->push_back( 265 install_warnings->push_back(
117 InstallWarning(ErrorUtils::FormatErrorMessage( 266 InstallWarning(ErrorUtils::FormatErrorMessage(
118 automation_errors::kErrorInvalidMatch, 267 automation_errors::kErrorInvalidMatch,
119 *it, 268 *it,
120 URLPattern::GetParseResultString(parse_result)))); 269 URLPattern::GetParseResultString(parse_result))));
121 continue; 270 continue;
122 } 271 }
123 272
124 matches.AddPattern(pattern); 273 matches.AddPattern(pattern);
125 } 274 }
126 } 275 }
127 } 276 }
128 if (specified_matches && matches.is_empty()) 277 if (specified_matches && matches.is_empty()) {
129 install_warnings->push_back( 278 install_warnings->push_back(
130 InstallWarning(automation_errors::kErrorNoMatchesProvided)); 279 InstallWarning(automation_errors::kErrorNoMatchesProvided));
280 }
131 281
132 return make_scoped_ptr( 282 return make_scoped_ptr(new AutomationInfo(desktop, matches, interact));
133 new AutomationInfo(desktop, matches, interact, specified_matches));
134 } 283 }
135 284
136 AutomationInfo::AutomationInfo() 285 // static
137 : desktop(false), interact(false), specified_matches(false) { 286 scoped_ptr<base::Value> AutomationInfo::ToValue(const AutomationInfo& info) {
287 return AsManifestType(info)->ToValue().Pass();
138 } 288 }
289
290 // static
291 scoped_ptr<Automation> AutomationInfo::AsManifestType(
292 const AutomationInfo& info) {
293 Automation* automation = new Automation;
294 if (!info.desktop && !info.interact && info.matches.size() == 0) {
295 automation->as_boolean.reset(new bool(true));
296 return make_scoped_ptr(automation).Pass();
297 }
298
299 Automation::Object* as_object = new Automation::Object;
300 as_object->desktop.reset(new bool(info.desktop));
301 as_object->interact.reset(new bool(info.interact));
302 if (info.matches.size() > 0) {
303 as_object->matches.reset(info.matches.ToStringVector().release());
304 }
305 automation->as_object.reset(as_object);
306 return make_scoped_ptr(automation).Pass();
307 }
308
309 // static
310 scoped_ptr<const AutomationInfo> AutomationInfo::Clone(
311 const AutomationInfo& info) {
312 const AutomationInfo* result =
313 new AutomationInfo(info.desktop, info.matches, info.interact);
314 return make_scoped_ptr(result);
315 }
316
317 AutomationInfo::AutomationInfo() : desktop(false), interact(false) {
318 }
319
139 AutomationInfo::AutomationInfo(bool desktop, 320 AutomationInfo::AutomationInfo(bool desktop,
140 const URLPatternSet& matches, 321 const URLPatternSet matches,
141 bool interact, 322 bool interact)
142 bool specified_matches) 323 : desktop(desktop), matches(matches), interact(interact) {
143 : desktop(desktop),
144 matches(matches),
145 interact(interact),
146 specified_matches(specified_matches) {
147 } 324 }
148 325
149 AutomationInfo::~AutomationInfo() { 326 AutomationInfo::~AutomationInfo() {
150 } 327 }
151 328
152 } // namespace extensions 329 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698