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