Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_permissions_api_helpers.h" | 9 #include "chrome/browser/extensions/extension_permissions_api_helpers.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/permissions_updater.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/url_pattern_set.h" | 16 #include "chrome/common/extensions/url_pattern_set.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 | 18 |
| 19 #include "chrome/common/extensions/api/permissions_api.h" | |
|
not at google - send to devlin
2012/01/12 06:01:05
move this up with the other #includes
calamity
2012/01/12 22:59:20
Done.
| |
| 20 | |
| 19 using extensions::PermissionsUpdater; | 21 using extensions::PermissionsUpdater; |
| 22 using extensions::permissions_api::Contains; | |
| 20 using extensions::permissions_api::PackPermissionsToValue; | 23 using extensions::permissions_api::PackPermissionsToValue; |
| 21 using extensions::permissions_api::UnpackPermissionsFromValue; | 24 using extensions::permissions_api::Remove; |
| 25 using extensions::permissions_api::Request; | |
| 26 using extensions::permissions_api::UnpackPermissionSet; | |
| 22 | 27 |
| 23 namespace { | 28 namespace { |
| 24 | 29 |
| 25 const char kCantRemoveRequiredPermissionsError[] = | 30 const char kCantRemoveRequiredPermissionsError[] = |
| 26 "You cannot remove required permissions."; | 31 "You cannot remove required permissions."; |
| 27 const char kNotInOptionalPermissionsError[] = | 32 const char kNotInOptionalPermissionsError[] = |
| 28 "Optional permissions must be listed in extension manifest."; | 33 "Optional permissions must be listed in extension manifest."; |
| 29 const char kNotWhitelistedError[] = | 34 const char kNotWhitelistedError[] = |
| 30 "The optional permissions API does not support '*'."; | 35 "The optional permissions API does not support '*'."; |
| 31 const char kUserGestureRequiredError[] = | 36 const char kUserGestureRequiredError[] = |
| 32 "This function must be called during a user gesture"; | 37 "This function must be called during a user gesture"; |
| 33 | 38 |
| 34 enum AutoConfirmForTest { | 39 enum AutoConfirmForTest { |
| 35 DO_NOT_SKIP = 0, | 40 DO_NOT_SKIP = 0, |
| 36 PROCEED, | 41 PROCEED, |
| 37 ABORT | 42 ABORT |
| 38 }; | 43 }; |
| 39 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; | 44 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; |
| 40 bool ignore_user_gesture_for_tests = false; | 45 bool ignore_user_gesture_for_tests = false; |
| 41 | 46 |
| 42 } // namespace | 47 } // namespace |
| 43 | 48 |
| 44 bool ContainsPermissionsFunction::RunImpl() { | 49 bool ContainsPermissionsFunction::RunImpl() { |
| 45 DictionaryValue* args = NULL; | 50 Contains::Params params; |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 51 EXTENSION_FUNCTION_VALIDATE(Contains::Params::Populate(*args_, ¶ms)); |
| 47 std::string error; | 52 |
| 48 if (!args) | 53 scoped_refptr<ExtensionPermissionSet> permissions = |
| 54 UnpackPermissionSet(params.permissions, &error_); | |
| 55 if (!permissions.get()) | |
| 49 return false; | 56 return false; |
| 50 | 57 |
| 51 scoped_refptr<ExtensionPermissionSet> permissions; | |
| 52 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) | |
| 53 return false; | |
| 54 CHECK(permissions.get()); | |
| 55 | |
| 56 result_.reset(Value::CreateBooleanValue( | 58 result_.reset(Value::CreateBooleanValue( |
| 57 GetExtension()->GetActivePermissions()->Contains(*permissions))); | 59 GetExtension()->GetActivePermissions()->Contains(*permissions))); |
| 58 return true; | 60 return true; |
| 59 } | 61 } |
| 60 | 62 |
| 61 bool GetAllPermissionsFunction::RunImpl() { | 63 bool GetAllPermissionsFunction::RunImpl() { |
| 62 result_.reset(PackPermissionsToValue( | 64 result_.reset(PackPermissionsToValue( |
| 63 GetExtension()->GetActivePermissions())); | 65 GetExtension()->GetActivePermissions())); |
| 64 return true; | 66 return true; |
| 65 } | 67 } |
| 66 | 68 |
| 67 bool RemovePermissionsFunction::RunImpl() { | 69 bool RemovePermissionsFunction::RunImpl() { |
| 68 DictionaryValue* args = NULL; | 70 Remove::Params params; |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 71 EXTENSION_FUNCTION_VALIDATE(Remove::Params::Populate(*args_, ¶ms)); |
| 70 if (!args) | 72 |
| 73 scoped_refptr<ExtensionPermissionSet> permissions = | |
| 74 UnpackPermissionSet(params.permissions, &error_); | |
| 75 if (!permissions.get()) | |
| 71 return false; | 76 return false; |
| 72 | 77 |
| 73 scoped_refptr<ExtensionPermissionSet> permissions; | |
| 74 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) | |
| 75 return false; | |
| 76 CHECK(permissions.get()); | |
| 77 | |
| 78 const Extension* extension = GetExtension(); | 78 const Extension* extension = GetExtension(); |
| 79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | 79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
| 80 | 80 |
| 81 // 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. |
| 82 ExtensionAPIPermissionSet apis = permissions->apis(); | 82 ExtensionAPIPermissionSet apis = permissions->apis(); |
| 83 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); | 83 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); |
| 84 i != apis.end(); ++i) { | 84 i != apis.end(); ++i) { |
| 85 const ExtensionAPIPermission* api = info->GetByID(*i); | 85 const ExtensionAPIPermission* api = info->GetByID(*i); |
| 86 if (!api->supports_optional()) { | 86 if (!api->supports_optional()) { |
| 87 error_ = ExtensionErrorUtils::FormatErrorMessage( | 87 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 118 | 118 |
| 119 RequestPermissionsFunction::RequestPermissionsFunction() {} | 119 RequestPermissionsFunction::RequestPermissionsFunction() {} |
| 120 RequestPermissionsFunction::~RequestPermissionsFunction() {} | 120 RequestPermissionsFunction::~RequestPermissionsFunction() {} |
| 121 | 121 |
| 122 bool RequestPermissionsFunction::RunImpl() { | 122 bool RequestPermissionsFunction::RunImpl() { |
| 123 if (!user_gesture() && !ignore_user_gesture_for_tests) { | 123 if (!user_gesture() && !ignore_user_gesture_for_tests) { |
| 124 error_ = kUserGestureRequiredError; | 124 error_ = kUserGestureRequiredError; |
| 125 return false; | 125 return false; |
| 126 } | 126 } |
| 127 | 127 |
| 128 DictionaryValue* args = NULL; | 128 Request::Params params; |
| 129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 129 EXTENSION_FUNCTION_VALIDATE(Request::Params::Populate(*args_.get(), ¶ms)); |
| 130 if (!args) | 130 |
| 131 requested_permissions_ = UnpackPermissionSet(params.permissions, &error_); | |
| 132 if (!requested_permissions_.get()) | |
| 131 return false; | 133 return false; |
| 132 | 134 |
| 133 if (!UnpackPermissionsFromValue( | |
| 134 args, &requested_permissions_, &bad_message_, &error_)) | |
| 135 return false; | |
| 136 CHECK(requested_permissions_.get()); | |
| 137 | |
| 138 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | 135 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
| 139 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); | 136 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); |
| 140 | 137 |
| 141 // Make sure they're only requesting permissions supported by this API. | 138 // Make sure they're only requesting permissions supported by this API. |
| 142 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); | 139 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); |
| 143 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); | 140 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); |
| 144 i != apis.end(); ++i) { | 141 i != apis.end(); ++i) { |
| 145 const ExtensionAPIPermission* api = info->GetByID(*i); | 142 const ExtensionAPIPermission* api = info->GetByID(*i); |
| 146 if (!api->supports_optional()) { | 143 if (!api->supports_optional()) { |
| 147 error_ = ExtensionErrorUtils::FormatErrorMessage( | 144 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 | 201 |
| 205 Release(); // Balanced in RunImpl(). | 202 Release(); // Balanced in RunImpl(). |
| 206 } | 203 } |
| 207 | 204 |
| 208 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { | 205 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { |
| 209 result_.reset(Value::CreateBooleanValue(false)); | 206 result_.reset(Value::CreateBooleanValue(false)); |
| 210 SendResponse(true); | 207 SendResponse(true); |
| 211 | 208 |
| 212 Release(); // Balanced in RunImpl(). | 209 Release(); // Balanced in RunImpl(). |
| 213 } | 210 } |
| OLD | NEW |