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

Side by Side Diff: chrome/browser/extensions/extension_permissions_api.cc

Issue 8493017: Cleanup extension permissions module. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/browser/extensions/extension_permissions_api.h" 5 #include "chrome/browser/extensions/extension_permissions_api.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_event_router.h" 9 #include "chrome/browser/extensions/extension_permissions_api_helpers.h"
10 #include "chrome/browser/extensions/extension_prefs.h"
11 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/permissions_updater.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/chrome_notification_types.h" 13 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/extensions/extension.h" 14 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_error_utils.h" 15 #include "chrome/common/extensions/extension_error_utils.h"
16 #include "chrome/common/extensions/extension_messages.h"
17 #include "chrome/common/extensions/extension_permission_set.h"
18 #include "chrome/common/extensions/url_pattern_set.h" 16 #include "chrome/common/extensions/url_pattern_set.h"
19 #include "content/public/browser/notification_service.h"
20 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
21 18
19 using extension_permissions_api::PackPermissionsToValue;
20 using extension_permissions_api::UnpackPermissionsFromValue;
21 using extensions::PermissionsUpdater;
22
22 namespace { 23 namespace {
23 24
24 const char kApisKey[] = "permissions";
25 const char kOriginsKey[] = "origins";
26
27 const char kCantRemoveRequiredPermissionsError[] = 25 const char kCantRemoveRequiredPermissionsError[] =
28 "You cannot remove required permissions."; 26 "You cannot remove required permissions.";
29 const char kNotInOptionalPermissionsError[] = 27 const char kNotInOptionalPermissionsError[] =
30 "Optional permissions must be listed in extension manifest."; 28 "Optional permissions must be listed in extension manifest.";
31 const char kNotWhitelistedError[] = 29 const char kNotWhitelistedError[] =
32 "The optional permissions API does not support '*'."; 30 "The optional permissions API does not support '*'.";
33 const char kUnknownPermissionError[] =
34 "'*' is not a recognized permission.";
35 const char kUserGestureRequiredError[] = 31 const char kUserGestureRequiredError[] =
36 "This function must be called during a user gesture"; 32 "This function must be called during a user gesture";
37 const char kInvalidOrigin[] =
38 "Invalid value for origin pattern *: *";
39
40 const char kOnAdded[] = "permissions.onAdded";
41 const char kOnRemoved[] = "permissions.onRemoved";
42 33
43 enum AutoConfirmForTest { 34 enum AutoConfirmForTest {
44 DO_NOT_SKIP = 0, 35 DO_NOT_SKIP = 0,
45 PROCEED, 36 PROCEED,
46 ABORT 37 ABORT
47 }; 38 };
48 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; 39 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP;
49 bool ignore_user_gesture_for_tests = false; 40 bool ignore_user_gesture_for_tests = false;
50 41
51 DictionaryValue* PackPermissionsToValue(const ExtensionPermissionSet* set) {
52 DictionaryValue* value = new DictionaryValue();
53
54 // Generate the list of API permissions.
55 ListValue* apis = new ListValue();
56 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
57 for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin();
58 i != set->apis().end(); ++i)
59 apis->Append(Value::CreateStringValue(info->GetByID(*i)->name()));
60
61 // Generate the list of origin permissions.
62 URLPatternSet hosts = set->explicit_hosts();
63 ListValue* origins = new ListValue();
64 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i)
65 origins->Append(Value::CreateStringValue(i->GetAsString()));
66
67 value->Set(kApisKey, apis);
68 value->Set(kOriginsKey, origins);
69 return value;
70 }
71
72 // Creates a new ExtensionPermissionSet from its |value| and passes ownership to
73 // the caller through |ptr|. Sets |bad_message| to true if the message is badly
74 // formed. Returns false if the method fails to unpack a permission set.
75 bool UnpackPermissionsFromValue(DictionaryValue* value,
76 scoped_refptr<ExtensionPermissionSet>* ptr,
77 bool* bad_message,
78 std::string* error) {
79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
80 ExtensionAPIPermissionSet apis;
81 if (value->HasKey(kApisKey)) {
82 ListValue* api_list = NULL;
83 if (!value->GetList(kApisKey, &api_list)) {
84 *bad_message = true;
85 return false;
86 }
87 for (size_t i = 0; i < api_list->GetSize(); ++i) {
88 std::string api_name;
89 if (!api_list->GetString(i, &api_name)) {
90 *bad_message = true;
91 return false;
92 }
93
94 ExtensionAPIPermission* permission = info->GetByName(api_name);
95 if (!permission) {
96 *error = ExtensionErrorUtils::FormatErrorMessage(
97 kUnknownPermissionError, api_name);
98 return false;
99 }
100 apis.insert(permission->id());
101 }
102 }
103
104 URLPatternSet origins;
105 if (value->HasKey(kOriginsKey)) {
106 ListValue* origin_list = NULL;
107 if (!value->GetList(kOriginsKey, &origin_list)) {
108 *bad_message = true;
109 return false;
110 }
111 for (size_t i = 0; i < origin_list->GetSize(); ++i) {
112 std::string pattern;
113 if (!origin_list->GetString(i, &pattern)) {
114 *bad_message = true;
115 return false;
116 }
117
118 URLPattern origin(URLPattern::IGNORE_PORTS,
119 Extension::kValidHostPermissionSchemes);
120 URLPattern::ParseResult parse_result = origin.Parse(pattern);
121 if (URLPattern::PARSE_SUCCESS != parse_result) {
122 *error = ExtensionErrorUtils::FormatErrorMessage(
123 kInvalidOrigin,
124 pattern,
125 URLPattern::GetParseResultString(parse_result));
126 return false;
127 }
128 origins.AddPattern(origin);
129 }
130 }
131
132 *ptr = new ExtensionPermissionSet(apis, origins, URLPatternSet());
133 return true;
134 }
135
136 } // namespace 42 } // namespace
137 43
138 ExtensionPermissionsManager::ExtensionPermissionsManager(
139 ExtensionService* extension_service)
140 : extension_service_(extension_service) {}
141
142 ExtensionPermissionsManager::~ExtensionPermissionsManager() {}
143
144 void ExtensionPermissionsManager::AddPermissions(
145 const Extension* extension, const ExtensionPermissionSet* permissions) {
146 scoped_refptr<const ExtensionPermissionSet> existing(
147 extension->GetActivePermissions());
148 scoped_refptr<ExtensionPermissionSet> total(
149 ExtensionPermissionSet::CreateUnion(existing, permissions));
150 scoped_refptr<ExtensionPermissionSet> added(
151 ExtensionPermissionSet::CreateDifference(total.get(), existing));
152
153 extension_service_->UpdateActivePermissions(extension, total.get());
154
155 // Update the granted permissions so we don't auto-disable the extension.
156 extension_service_->GrantPermissions(extension);
157
158 NotifyPermissionsUpdated(ADDED, extension, added.get());
159 }
160
161 void ExtensionPermissionsManager::RemovePermissions(
162 const Extension* extension, const ExtensionPermissionSet* permissions) {
163 scoped_refptr<const ExtensionPermissionSet> existing(
164 extension->GetActivePermissions());
165 scoped_refptr<ExtensionPermissionSet> total(
166 ExtensionPermissionSet::CreateDifference(existing, permissions));
167 scoped_refptr<ExtensionPermissionSet> removed(
168 ExtensionPermissionSet::CreateDifference(existing, total.get()));
169
170 // We update the active permissions, and not the granted permissions, because
171 // the extension, not the user, removed the permissions. This allows the
172 // extension to add them again without prompting the user.
173 extension_service_->UpdateActivePermissions(extension, total.get());
174
175 NotifyPermissionsUpdated(REMOVED, extension, removed.get());
176 }
177
178 void ExtensionPermissionsManager::DispatchEvent(
179 const std::string& extension_id,
180 const char* event_name,
181 const ExtensionPermissionSet* changed_permissions) {
182 Profile* profile = extension_service_->profile();
183 if (profile && profile->GetExtensionEventRouter()) {
184 ListValue value;
185 value.Append(PackPermissionsToValue(changed_permissions));
186 std::string json_value;
187 base::JSONWriter::Write(&value, false, &json_value);
188 profile->GetExtensionEventRouter()->DispatchEventToExtension(
189 extension_id, event_name, json_value, profile, GURL());
190 }
191 }
192
193 void ExtensionPermissionsManager::NotifyPermissionsUpdated(
194 EventType event_type,
195 const Extension* extension,
196 const ExtensionPermissionSet* changed) {
197 if (!changed || changed->IsEmpty())
198 return;
199
200 UpdatedExtensionPermissionsInfo::Reason reason;
201 const char* event_name = NULL;
202
203 if (event_type == REMOVED) {
204 reason = UpdatedExtensionPermissionsInfo::REMOVED;
205 event_name = kOnRemoved;
206 } else {
207 CHECK_EQ(ADDED, event_type);
208 reason = UpdatedExtensionPermissionsInfo::ADDED;
209 event_name = kOnAdded;
210 }
211
212 // Notify other APIs or interested parties.
213 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo(
214 extension, changed, reason);
215 content::NotificationService::current()->Notify(
216 chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
217 content::Source<Profile>(extension_service_->profile()),
218 content::Details<UpdatedExtensionPermissionsInfo>(&info));
219
220 // Send the new permissions to the renderers.
221 for (content::RenderProcessHost::iterator i(
222 content::RenderProcessHost::AllHostsIterator());
223 !i.IsAtEnd(); i.Advance()) {
224 content::RenderProcessHost* host = i.GetCurrentValue();
225 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
226 if (extension_service_->profile()->IsSameProfile(profile))
227 host->Send(new ExtensionMsg_UpdatePermissions(
228 static_cast<int>(reason),
229 extension->id(),
230 changed->apis(),
231 changed->explicit_hosts(),
232 changed->scriptable_hosts()));
233 }
234
235 // Trigger the onAdded and onRemoved events in the extension.
236 DispatchEvent(extension->id(), event_name, changed);
237 }
238
239 bool ContainsPermissionsFunction::RunImpl() { 44 bool ContainsPermissionsFunction::RunImpl() {
240 DictionaryValue* args = NULL; 45 DictionaryValue* args = NULL;
241 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
242 std::string error; 47 std::string error;
243 if (!args) 48 if (!args)
244 return false; 49 return false;
245 50
246 scoped_refptr<ExtensionPermissionSet> permissions; 51 scoped_refptr<ExtensionPermissionSet> permissions;
247 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) 52 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_))
248 return false; 53 return false;
(...skipping 15 matching lines...) Expand all
264 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
265 if (!args) 70 if (!args)
266 return false; 71 return false;
267 72
268 scoped_refptr<ExtensionPermissionSet> permissions; 73 scoped_refptr<ExtensionPermissionSet> permissions;
269 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) 74 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_))
270 return false; 75 return false;
271 CHECK(permissions.get()); 76 CHECK(permissions.get());
272 77
273 const Extension* extension = GetExtension(); 78 const Extension* extension = GetExtension();
274 ExtensionPermissionsManager* perms_manager =
275 profile()->GetExtensionService()->permissions_manager();
276 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); 79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
277 80
278 // Make sure they're only trying to remove permissions supported by this API. 81 // Make sure they're only trying to remove permissions supported by this API.
279 ExtensionAPIPermissionSet apis = permissions->apis(); 82 ExtensionAPIPermissionSet apis = permissions->apis();
280 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); 83 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin();
281 i != apis.end(); ++i) { 84 i != apis.end(); ++i) {
282 const ExtensionAPIPermission* api = info->GetByID(*i); 85 const ExtensionAPIPermission* api = info->GetByID(*i);
283 if (!api->supports_optional()) { 86 if (!api->supports_optional()) {
284 error_ = ExtensionErrorUtils::FormatErrorMessage( 87 error_ = ExtensionErrorUtils::FormatErrorMessage(
285 kNotWhitelistedError, api->name()); 88 kNotWhitelistedError, api->name());
286 return false; 89 return false;
287 } 90 }
288 } 91 }
289 92
290 // Make sure we don't remove any required pemissions. 93 // Make sure we don't remove any required pemissions.
291 const ExtensionPermissionSet* required = extension->required_permission_set(); 94 const ExtensionPermissionSet* required = extension->required_permission_set();
292 scoped_refptr<ExtensionPermissionSet> intersection( 95 scoped_refptr<ExtensionPermissionSet> intersection(
293 ExtensionPermissionSet::CreateIntersection(permissions.get(), required)); 96 ExtensionPermissionSet::CreateIntersection(permissions.get(), required));
294 if (!intersection->IsEmpty()) { 97 if (!intersection->IsEmpty()) {
295 error_ = kCantRemoveRequiredPermissionsError; 98 error_ = kCantRemoveRequiredPermissionsError;
296 result_.reset(Value::CreateBooleanValue(false)); 99 result_.reset(Value::CreateBooleanValue(false));
297 return false; 100 return false;
298 } 101 }
299 102
300 perms_manager->RemovePermissions(extension, permissions.get()); 103 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get());
301 result_.reset(Value::CreateBooleanValue(true)); 104 result_.reset(Value::CreateBooleanValue(true));
302 return true; 105 return true;
303 } 106 }
304 107
305 // static 108 // static
306 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { 109 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) {
307 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; 110 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT;
308 } 111 }
309 112
310 // static 113 // static
(...skipping 14 matching lines...) Expand all
325 DictionaryValue* args = NULL; 128 DictionaryValue* args = NULL;
326 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
327 if (!args) 130 if (!args)
328 return false; 131 return false;
329 132
330 if (!UnpackPermissionsFromValue( 133 if (!UnpackPermissionsFromValue(
331 args, &requested_permissions_, &bad_message_, &error_)) 134 args, &requested_permissions_, &bad_message_, &error_))
332 return false; 135 return false;
333 CHECK(requested_permissions_.get()); 136 CHECK(requested_permissions_.get());
334 137
335 extension_ = GetExtension();
336 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); 138 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
337 ExtensionPermissionsManager* perms_manager =
338 profile()->GetExtensionService()->permissions_manager();
339 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); 139 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs();
340 140
341 // Make sure they're only requesting permissions supported by this API. 141 // Make sure they're only requesting permissions supported by this API.
342 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); 142 ExtensionAPIPermissionSet apis = requested_permissions_->apis();
343 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); 143 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin();
344 i != apis.end(); ++i) { 144 i != apis.end(); ++i) {
345 const ExtensionAPIPermission* api = info->GetByID(*i); 145 const ExtensionAPIPermission* api = info->GetByID(*i);
346 if (!api->supports_optional()) { 146 if (!api->supports_optional()) {
347 error_ = ExtensionErrorUtils::FormatErrorMessage( 147 error_ = ExtensionErrorUtils::FormatErrorMessage(
348 kNotWhitelistedError, api->name()); 148 kNotWhitelistedError, api->name());
349 return false; 149 return false;
350 } 150 }
351 } 151 }
352 152
353 // The requested permissions must be defined as optional in the manifest. 153 // The requested permissions must be defined as optional in the manifest.
354 if (!extension_->optional_permission_set()->Contains( 154 if (!GetExtension()->optional_permission_set()->Contains(
355 *requested_permissions_)) { 155 *requested_permissions_)) {
356 error_ = kNotInOptionalPermissionsError; 156 error_ = kNotInOptionalPermissionsError;
357 result_.reset(Value::CreateBooleanValue(false)); 157 result_.reset(Value::CreateBooleanValue(false));
358 return false; 158 return false;
359 } 159 }
360 160
361 // We don't need to prompt the user if the requested permissions are a subset 161 // We don't need to prompt the user if the requested permissions are a subset
362 // of the granted permissions set. 162 // of the granted permissions set.
363 const ExtensionPermissionSet* granted = 163 const ExtensionPermissionSet* granted =
364 prefs->GetGrantedPermissions(extension_->id()); 164 prefs->GetGrantedPermissions(GetExtension()->id());
365 if (granted && granted->Contains(*requested_permissions_)) { 165 if (granted && granted->Contains(*requested_permissions_)) {
366 perms_manager->AddPermissions(extension_, requested_permissions_.get()); 166 PermissionsUpdater perms_updater(profile());
167 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get());
367 result_.reset(Value::CreateBooleanValue(true)); 168 result_.reset(Value::CreateBooleanValue(true));
368 SendResponse(true); 169 SendResponse(true);
369 return true; 170 return true;
370 } 171 }
371 172
372 // Filter out the granted permissions so we only prompt for new ones. 173 // Filter out the granted permissions so we only prompt for new ones.
373 requested_permissions_ = ExtensionPermissionSet::CreateDifference( 174 requested_permissions_ = ExtensionPermissionSet::CreateDifference(
374 requested_permissions_.get(), granted); 175 requested_permissions_.get(), granted);
375 176
376 // Balanced with Release() in InstallUIProceed() and InstallUIAbort(). 177 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort().
377 AddRef();
378 178
379 // We don't need to show the prompt if there are no new warnings, or if 179 // We don't need to show the prompt if there are no new warnings, or if
380 // we're skipping the confirmation UI. All extension types but INTERNAL 180 // we're skipping the confirmation UI. All extension types but INTERNAL
381 // are allowed to silently increase their permission level. 181 // are allowed to silently increase their permission level.
382 if (auto_confirm_for_tests == PROCEED || 182 if (auto_confirm_for_tests == PROCEED ||
383 requested_permissions_->GetWarningMessages().size() == 0) { 183 requested_permissions_->GetWarningMessages().size() == 0) {
384 InstallUIProceed(); 184 InstallUIProceed();
385 } else if (auto_confirm_for_tests == ABORT) { 185 } else if (auto_confirm_for_tests == ABORT) {
386 // Pretend the user clicked cancel. 186 // Pretend the user clicked cancel.
387 InstallUIAbort(true); 187 InstallUIAbort(true);
388 } else { 188 } else {
389 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); 189 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests);
390 install_ui_.reset(new ExtensionInstallUI(profile())); 190 install_ui_.reset(new ExtensionInstallUI(profile()));
391 install_ui_->ConfirmPermissions( 191 install_ui_->ConfirmPermissions(
392 this, extension_, requested_permissions_.get()); 192 this, GetExtension(), requested_permissions_.get());
393 } 193 }
394 194
395 return true; 195 return true;
396 } 196 }
397 197
398 void RequestPermissionsFunction::InstallUIProceed() { 198 void RequestPermissionsFunction::InstallUIProceed() {
399 ExtensionPermissionsManager* perms_manager = 199 PermissionsUpdater perms_updater(profile());
400 profile()->GetExtensionService()->permissions_manager(); 200 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get());
401 201
402 install_ui_.reset();
403 result_.reset(Value::CreateBooleanValue(true)); 202 result_.reset(Value::CreateBooleanValue(true));
404 perms_manager->AddPermissions(extension_, requested_permissions_.get());
405
406 SendResponse(true); 203 SendResponse(true);
407 204
408 Release(); 205 Release(); // Balanced in RunImpl().
409 } 206 }
410 207
411 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { 208 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) {
412 install_ui_.reset();
413 result_.reset(Value::CreateBooleanValue(false)); 209 result_.reset(Value::CreateBooleanValue(false));
414 requested_permissions_ = NULL; 210 SendResponse(true);
415 211
416 SendResponse(true); 212 Release(); // Balanced in RunImpl().
417 Release();
418 } 213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698