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