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

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: merge Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 extensions::PermissionsUpdater;
20 using extensions::permissions_api::PackPermissionsToValue;
21 using extensions::permissions_api::UnpackPermissionsFromValue;
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(Extension::kValidHostPermissionSchemes);
119 URLPattern::ParseResult parse_result = origin.Parse(pattern);
120 if (URLPattern::PARSE_SUCCESS != parse_result) {
121 *error = ExtensionErrorUtils::FormatErrorMessage(
122 kInvalidOrigin,
123 pattern,
124 URLPattern::GetParseResultString(parse_result));
125 return false;
126 }
127 origins.AddPattern(origin);
128 }
129 }
130
131 *ptr = new ExtensionPermissionSet(apis, origins, URLPatternSet());
132 return true;
133 }
134
135 } // namespace 42 } // namespace
136 43
137 ExtensionPermissionsManager::ExtensionPermissionsManager(
138 ExtensionService* extension_service)
139 : extension_service_(extension_service) {}
140
141 ExtensionPermissionsManager::~ExtensionPermissionsManager() {}
142
143 void ExtensionPermissionsManager::AddPermissions(
144 const Extension* extension, const ExtensionPermissionSet* permissions) {
145 scoped_refptr<const ExtensionPermissionSet> existing(
146 extension->GetActivePermissions());
147 scoped_refptr<ExtensionPermissionSet> total(
148 ExtensionPermissionSet::CreateUnion(existing, permissions));
149 scoped_refptr<ExtensionPermissionSet> added(
150 ExtensionPermissionSet::CreateDifference(total.get(), existing));
151
152 extension_service_->UpdateActivePermissions(extension, total.get());
153
154 // Update the granted permissions so we don't auto-disable the extension.
155 extension_service_->GrantPermissions(extension);
156
157 NotifyPermissionsUpdated(ADDED, extension, added.get());
158 }
159
160 void ExtensionPermissionsManager::RemovePermissions(
161 const Extension* extension, const ExtensionPermissionSet* permissions) {
162 scoped_refptr<const ExtensionPermissionSet> existing(
163 extension->GetActivePermissions());
164 scoped_refptr<ExtensionPermissionSet> total(
165 ExtensionPermissionSet::CreateDifference(existing, permissions));
166 scoped_refptr<ExtensionPermissionSet> removed(
167 ExtensionPermissionSet::CreateDifference(existing, total.get()));
168
169 // We update the active permissions, and not the granted permissions, because
170 // the extension, not the user, removed the permissions. This allows the
171 // extension to add them again without prompting the user.
172 extension_service_->UpdateActivePermissions(extension, total.get());
173
174 NotifyPermissionsUpdated(REMOVED, extension, removed.get());
175 }
176
177 void ExtensionPermissionsManager::DispatchEvent(
178 const std::string& extension_id,
179 const char* event_name,
180 const ExtensionPermissionSet* changed_permissions) {
181 Profile* profile = extension_service_->profile();
182 if (profile && profile->GetExtensionEventRouter()) {
183 ListValue value;
184 value.Append(PackPermissionsToValue(changed_permissions));
185 std::string json_value;
186 base::JSONWriter::Write(&value, false, &json_value);
187 profile->GetExtensionEventRouter()->DispatchEventToExtension(
188 extension_id, event_name, json_value, profile, GURL());
189 }
190 }
191
192 void ExtensionPermissionsManager::NotifyPermissionsUpdated(
193 EventType event_type,
194 const Extension* extension,
195 const ExtensionPermissionSet* changed) {
196 if (!changed || changed->IsEmpty())
197 return;
198
199 UpdatedExtensionPermissionsInfo::Reason reason;
200 const char* event_name = NULL;
201
202 if (event_type == REMOVED) {
203 reason = UpdatedExtensionPermissionsInfo::REMOVED;
204 event_name = kOnRemoved;
205 } else {
206 CHECK_EQ(ADDED, event_type);
207 reason = UpdatedExtensionPermissionsInfo::ADDED;
208 event_name = kOnAdded;
209 }
210
211 // Notify other APIs or interested parties.
212 UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo(
213 extension, changed, reason);
214 content::NotificationService::current()->Notify(
215 chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
216 content::Source<Profile>(extension_service_->profile()),
217 content::Details<UpdatedExtensionPermissionsInfo>(&info));
218
219 // Send the new permissions to the renderers.
220 for (content::RenderProcessHost::iterator i(
221 content::RenderProcessHost::AllHostsIterator());
222 !i.IsAtEnd(); i.Advance()) {
223 content::RenderProcessHost* host = i.GetCurrentValue();
224 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
225 if (extension_service_->profile()->IsSameProfile(profile))
226 host->Send(new ExtensionMsg_UpdatePermissions(
227 static_cast<int>(reason),
228 extension->id(),
229 changed->apis(),
230 changed->explicit_hosts(),
231 changed->scriptable_hosts()));
232 }
233
234 // Trigger the onAdded and onRemoved events in the extension.
235 DispatchEvent(extension->id(), event_name, changed);
236 }
237
238 bool ContainsPermissionsFunction::RunImpl() { 44 bool ContainsPermissionsFunction::RunImpl() {
239 DictionaryValue* args = NULL; 45 DictionaryValue* args = NULL;
240 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
241 std::string error; 47 std::string error;
242 if (!args) 48 if (!args)
243 return false; 49 return false;
244 50
245 scoped_refptr<ExtensionPermissionSet> permissions; 51 scoped_refptr<ExtensionPermissionSet> permissions;
246 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) 52 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_))
247 return false; 53 return false;
(...skipping 15 matching lines...) Expand all
263 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
264 if (!args) 70 if (!args)
265 return false; 71 return false;
266 72
267 scoped_refptr<ExtensionPermissionSet> permissions; 73 scoped_refptr<ExtensionPermissionSet> permissions;
268 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) 74 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_))
269 return false; 75 return false;
270 CHECK(permissions.get()); 76 CHECK(permissions.get());
271 77
272 const Extension* extension = GetExtension(); 78 const Extension* extension = GetExtension();
273 ExtensionPermissionsManager* perms_manager =
274 profile()->GetExtensionService()->permissions_manager();
275 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); 79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
276 80
277 // 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.
278 ExtensionAPIPermissionSet apis = permissions->apis(); 82 ExtensionAPIPermissionSet apis = permissions->apis();
279 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); 83 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin();
280 i != apis.end(); ++i) { 84 i != apis.end(); ++i) {
281 const ExtensionAPIPermission* api = info->GetByID(*i); 85 const ExtensionAPIPermission* api = info->GetByID(*i);
282 if (!api->supports_optional()) { 86 if (!api->supports_optional()) {
283 error_ = ExtensionErrorUtils::FormatErrorMessage( 87 error_ = ExtensionErrorUtils::FormatErrorMessage(
284 kNotWhitelistedError, api->name()); 88 kNotWhitelistedError, api->name());
285 return false; 89 return false;
286 } 90 }
287 } 91 }
288 92
289 // Make sure we don't remove any required pemissions. 93 // Make sure we don't remove any required pemissions.
290 const ExtensionPermissionSet* required = extension->required_permission_set(); 94 const ExtensionPermissionSet* required = extension->required_permission_set();
291 scoped_refptr<ExtensionPermissionSet> intersection( 95 scoped_refptr<ExtensionPermissionSet> intersection(
292 ExtensionPermissionSet::CreateIntersection(permissions.get(), required)); 96 ExtensionPermissionSet::CreateIntersection(permissions.get(), required));
293 if (!intersection->IsEmpty()) { 97 if (!intersection->IsEmpty()) {
294 error_ = kCantRemoveRequiredPermissionsError; 98 error_ = kCantRemoveRequiredPermissionsError;
295 result_.reset(Value::CreateBooleanValue(false)); 99 result_.reset(Value::CreateBooleanValue(false));
296 return false; 100 return false;
297 } 101 }
298 102
299 perms_manager->RemovePermissions(extension, permissions.get()); 103 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get());
300 result_.reset(Value::CreateBooleanValue(true)); 104 result_.reset(Value::CreateBooleanValue(true));
301 return true; 105 return true;
302 } 106 }
303 107
304 // static 108 // static
305 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { 109 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) {
306 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; 110 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT;
307 } 111 }
308 112
309 // static 113 // static
(...skipping 14 matching lines...) Expand all
324 DictionaryValue* args = NULL; 128 DictionaryValue* args = NULL;
325 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
326 if (!args) 130 if (!args)
327 return false; 131 return false;
328 132
329 if (!UnpackPermissionsFromValue( 133 if (!UnpackPermissionsFromValue(
330 args, &requested_permissions_, &bad_message_, &error_)) 134 args, &requested_permissions_, &bad_message_, &error_))
331 return false; 135 return false;
332 CHECK(requested_permissions_.get()); 136 CHECK(requested_permissions_.get());
333 137
334 extension_ = GetExtension();
335 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); 138 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
336 ExtensionPermissionsManager* perms_manager =
337 profile()->GetExtensionService()->permissions_manager();
338 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); 139 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs();
339 140
340 // Make sure they're only requesting permissions supported by this API. 141 // Make sure they're only requesting permissions supported by this API.
341 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); 142 ExtensionAPIPermissionSet apis = requested_permissions_->apis();
342 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); 143 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin();
343 i != apis.end(); ++i) { 144 i != apis.end(); ++i) {
344 const ExtensionAPIPermission* api = info->GetByID(*i); 145 const ExtensionAPIPermission* api = info->GetByID(*i);
345 if (!api->supports_optional()) { 146 if (!api->supports_optional()) {
346 error_ = ExtensionErrorUtils::FormatErrorMessage( 147 error_ = ExtensionErrorUtils::FormatErrorMessage(
347 kNotWhitelistedError, api->name()); 148 kNotWhitelistedError, api->name());
348 return false; 149 return false;
349 } 150 }
350 } 151 }
351 152
352 // The requested permissions must be defined as optional in the manifest. 153 // The requested permissions must be defined as optional in the manifest.
353 if (!extension_->optional_permission_set()->Contains( 154 if (!GetExtension()->optional_permission_set()->Contains(
354 *requested_permissions_)) { 155 *requested_permissions_)) {
355 error_ = kNotInOptionalPermissionsError; 156 error_ = kNotInOptionalPermissionsError;
356 result_.reset(Value::CreateBooleanValue(false)); 157 result_.reset(Value::CreateBooleanValue(false));
357 return false; 158 return false;
358 } 159 }
359 160
360 // 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
361 // of the granted permissions set. 162 // of the granted permissions set.
362 const ExtensionPermissionSet* granted = 163 const ExtensionPermissionSet* granted =
363 prefs->GetGrantedPermissions(extension_->id()); 164 prefs->GetGrantedPermissions(GetExtension()->id());
364 if (granted && granted->Contains(*requested_permissions_)) { 165 if (granted && granted->Contains(*requested_permissions_)) {
365 perms_manager->AddPermissions(extension_, requested_permissions_.get()); 166 PermissionsUpdater perms_updater(profile());
167 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get());
366 result_.reset(Value::CreateBooleanValue(true)); 168 result_.reset(Value::CreateBooleanValue(true));
367 SendResponse(true); 169 SendResponse(true);
368 return true; 170 return true;
369 } 171 }
370 172
371 // 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.
372 requested_permissions_ = ExtensionPermissionSet::CreateDifference( 174 requested_permissions_ = ExtensionPermissionSet::CreateDifference(
373 requested_permissions_.get(), granted); 175 requested_permissions_.get(), granted);
374 176
375 // Balanced with Release() in InstallUIProceed() and InstallUIAbort(). 177 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort().
376 AddRef();
377 178
378 // 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
379 // we're skipping the confirmation UI. All extension types but INTERNAL 180 // we're skipping the confirmation UI. All extension types but INTERNAL
380 // are allowed to silently increase their permission level. 181 // are allowed to silently increase their permission level.
381 if (auto_confirm_for_tests == PROCEED || 182 if (auto_confirm_for_tests == PROCEED ||
382 requested_permissions_->GetWarningMessages().size() == 0) { 183 requested_permissions_->GetWarningMessages().size() == 0) {
383 InstallUIProceed(); 184 InstallUIProceed();
384 } else if (auto_confirm_for_tests == ABORT) { 185 } else if (auto_confirm_for_tests == ABORT) {
385 // Pretend the user clicked cancel. 186 // Pretend the user clicked cancel.
386 InstallUIAbort(true); 187 InstallUIAbort(true);
387 } else { 188 } else {
388 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); 189 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests);
389 install_ui_.reset(new ExtensionInstallUI(profile())); 190 install_ui_.reset(new ExtensionInstallUI(profile()));
390 install_ui_->ConfirmPermissions( 191 install_ui_->ConfirmPermissions(
391 this, extension_, requested_permissions_.get()); 192 this, GetExtension(), requested_permissions_.get());
392 } 193 }
393 194
394 return true; 195 return true;
395 } 196 }
396 197
397 void RequestPermissionsFunction::InstallUIProceed() { 198 void RequestPermissionsFunction::InstallUIProceed() {
398 ExtensionPermissionsManager* perms_manager = 199 PermissionsUpdater perms_updater(profile());
399 profile()->GetExtensionService()->permissions_manager(); 200 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get());
400 201
401 install_ui_.reset();
402 result_.reset(Value::CreateBooleanValue(true)); 202 result_.reset(Value::CreateBooleanValue(true));
403 perms_manager->AddPermissions(extension_, requested_permissions_.get());
404
405 SendResponse(true); 203 SendResponse(true);
406 204
407 Release(); 205 Release(); // Balanced in RunImpl().
408 } 206 }
409 207
410 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { 208 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) {
411 install_ui_.reset();
412 result_.reset(Value::CreateBooleanValue(false)); 209 result_.reset(Value::CreateBooleanValue(false));
413 requested_permissions_ = NULL; 210 SendResponse(true);
414 211
415 SendResponse(true); 212 Release(); // Balanced in RunImpl().
416 Release();
417 } 213 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_permissions_api.h ('k') | chrome/browser/extensions/extension_permissions_api_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698