| 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/api/permissions/permissions_api.h" | 5 #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 8 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/permissions_updater.h" | 10 #include "chrome/browser/extensions/permissions_updater.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/extensions/api/permissions.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 using extensions::PermissionsUpdater; | 19 using extensions::PermissionsUpdater; |
| 20 using extensions::permissions_api::PackPermissionsToValue; | 20 using extensions::permissions_api_helpers::PackPermissionSet; |
| 21 using extensions::permissions_api::UnpackPermissionsFromValue; | 21 using extensions::permissions_api_helpers::UnpackPermissionSet; |
| 22 using namespace extensions::api::permissions; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const char kCantRemoveRequiredPermissionsError[] = | 26 const char kCantRemoveRequiredPermissionsError[] = |
| 26 "You cannot remove required permissions."; | 27 "You cannot remove required permissions."; |
| 27 const char kNotInOptionalPermissionsError[] = | 28 const char kNotInOptionalPermissionsError[] = |
| 28 "Optional permissions must be listed in extension manifest."; | 29 "Optional permissions must be listed in extension manifest."; |
| 29 const char kNotWhitelistedError[] = | 30 const char kNotWhitelistedError[] = |
| 30 "The optional permissions API does not support '*'."; | 31 "The optional permissions API does not support '*'."; |
| 31 const char kUserGestureRequiredError[] = | 32 const char kUserGestureRequiredError[] = |
| 32 "This function must be called during a user gesture"; | 33 "This function must be called during a user gesture"; |
| 33 | 34 |
| 34 enum AutoConfirmForTest { | 35 enum AutoConfirmForTest { |
| 35 DO_NOT_SKIP = 0, | 36 DO_NOT_SKIP = 0, |
| 36 PROCEED, | 37 PROCEED, |
| 37 ABORT | 38 ABORT |
| 38 }; | 39 }; |
| 39 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; | 40 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; |
| 40 bool ignore_user_gesture_for_tests = false; | 41 bool ignore_user_gesture_for_tests = false; |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 bool ContainsPermissionsFunction::RunImpl() { | 45 bool ContainsPermissionsFunction::RunImpl() { |
| 45 DictionaryValue* args = NULL; | 46 scoped_ptr<Contains::Params> params(Contains::Params::Create(*args_)); |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 47 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 47 std::string error; | 48 |
| 48 if (!args) | 49 scoped_refptr<ExtensionPermissionSet> permissions = |
| 50 UnpackPermissionSet(params->permissions, &error_); |
| 51 if (!permissions.get()) |
| 49 return false; | 52 return false; |
| 50 | 53 |
| 51 scoped_refptr<ExtensionPermissionSet> permissions; | 54 result_.reset(Contains::Result::Create( |
| 52 if (!UnpackPermissionsFromValue(args, &permissions, &bad_message_, &error_)) | |
| 53 return false; | |
| 54 CHECK(permissions.get()); | |
| 55 | |
| 56 result_.reset(Value::CreateBooleanValue( | |
| 57 GetExtension()->GetActivePermissions()->Contains(*permissions))); | 55 GetExtension()->GetActivePermissions()->Contains(*permissions))); |
| 58 return true; | 56 return true; |
| 59 } | 57 } |
| 60 | 58 |
| 61 bool GetAllPermissionsFunction::RunImpl() { | 59 bool GetAllPermissionsFunction::RunImpl() { |
| 62 result_.reset(PackPermissionsToValue( | 60 scoped_ptr<Permissions> permissions = |
| 63 GetExtension()->GetActivePermissions())); | 61 PackPermissionSet(GetExtension()->GetActivePermissions()); |
| 62 result_.reset(GetAll::Result::Create(*permissions)); |
| 64 return true; | 63 return true; |
| 65 } | 64 } |
| 66 | 65 |
| 67 bool RemovePermissionsFunction::RunImpl() { | 66 bool RemovePermissionsFunction::RunImpl() { |
| 68 DictionaryValue* args = NULL; | 67 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 68 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 70 if (!args) | 69 |
| 70 scoped_refptr<ExtensionPermissionSet> permissions = |
| 71 UnpackPermissionSet(params->permissions, &error_); |
| 72 if (!permissions.get()) |
| 71 return false; | 73 return false; |
| 72 | 74 |
| 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(); | 75 const Extension* extension = GetExtension(); |
| 79 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | 76 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
| 80 | 77 |
| 81 // Make sure they're only trying to remove permissions supported by this API. | 78 // Make sure they're only trying to remove permissions supported by this API. |
| 82 ExtensionAPIPermissionSet apis = permissions->apis(); | 79 ExtensionAPIPermissionSet apis = permissions->apis(); |
| 83 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); | 80 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); |
| 84 i != apis.end(); ++i) { | 81 i != apis.end(); ++i) { |
| 85 const ExtensionAPIPermission* api = info->GetByID(*i); | 82 const ExtensionAPIPermission* api = info->GetByID(*i); |
| 86 if (!api->supports_optional()) { | 83 if (!api->supports_optional()) { |
| 87 error_ = ExtensionErrorUtils::FormatErrorMessage( | 84 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 88 kNotWhitelistedError, api->name()); | 85 kNotWhitelistedError, api->name()); |
| 89 return false; | 86 return false; |
| 90 } | 87 } |
| 91 } | 88 } |
| 92 | 89 |
| 93 // Make sure we don't remove any required pemissions. | 90 // Make sure we don't remove any required pemissions. |
| 94 const ExtensionPermissionSet* required = extension->required_permission_set(); | 91 const ExtensionPermissionSet* required = extension->required_permission_set(); |
| 95 scoped_refptr<ExtensionPermissionSet> intersection( | 92 scoped_refptr<ExtensionPermissionSet> intersection( |
| 96 ExtensionPermissionSet::CreateIntersection(permissions.get(), required)); | 93 ExtensionPermissionSet::CreateIntersection(permissions.get(), required)); |
| 97 if (!intersection->IsEmpty()) { | 94 if (!intersection->IsEmpty()) { |
| 98 error_ = kCantRemoveRequiredPermissionsError; | 95 error_ = kCantRemoveRequiredPermissionsError; |
| 99 result_.reset(Value::CreateBooleanValue(false)); | 96 result_.reset(Remove::Result::Create(false)); |
| 100 return false; | 97 return false; |
| 101 } | 98 } |
| 102 | 99 |
| 103 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get()); | 100 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get()); |
| 104 result_.reset(Value::CreateBooleanValue(true)); | 101 result_.reset(Remove::Result::Create(true)); |
| 105 return true; | 102 return true; |
| 106 } | 103 } |
| 107 | 104 |
| 108 // static | 105 // static |
| 109 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { | 106 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { |
| 110 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; | 107 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; |
| 111 } | 108 } |
| 112 | 109 |
| 113 // static | 110 // static |
| 114 void RequestPermissionsFunction::SetIgnoreUserGestureForTests( | 111 void RequestPermissionsFunction::SetIgnoreUserGestureForTests( |
| 115 bool ignore) { | 112 bool ignore) { |
| 116 ignore_user_gesture_for_tests = ignore; | 113 ignore_user_gesture_for_tests = ignore; |
| 117 } | 114 } |
| 118 | 115 |
| 119 RequestPermissionsFunction::RequestPermissionsFunction() {} | 116 RequestPermissionsFunction::RequestPermissionsFunction() {} |
| 120 RequestPermissionsFunction::~RequestPermissionsFunction() {} | 117 RequestPermissionsFunction::~RequestPermissionsFunction() {} |
| 121 | 118 |
| 122 bool RequestPermissionsFunction::RunImpl() { | 119 bool RequestPermissionsFunction::RunImpl() { |
| 123 if (!user_gesture() && !ignore_user_gesture_for_tests) { | 120 if (!user_gesture() && !ignore_user_gesture_for_tests) { |
| 124 error_ = kUserGestureRequiredError; | 121 error_ = kUserGestureRequiredError; |
| 125 return false; | 122 return false; |
| 126 } | 123 } |
| 127 | 124 |
| 128 DictionaryValue* args = NULL; | 125 scoped_ptr<Request::Params> params(Request::Params::Create(*args_)); |
| 129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 126 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 130 if (!args) | 127 |
| 128 requested_permissions_ = UnpackPermissionSet(params->permissions, &error_); |
| 129 if (!requested_permissions_.get()) |
| 131 return false; | 130 return false; |
| 132 | 131 |
| 133 if (!UnpackPermissionsFromValue( | |
| 134 args, &requested_permissions_, &bad_message_, &error_)) | |
| 135 return false; | |
| 136 CHECK(requested_permissions_.get()); | |
| 137 | |
| 138 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); | 132 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance(); |
| 139 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); | 133 ExtensionPrefs* prefs = profile()->GetExtensionService()->extension_prefs(); |
| 140 | 134 |
| 141 // Make sure they're only requesting permissions supported by this API. | 135 // Make sure they're only requesting permissions supported by this API. |
| 142 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); | 136 ExtensionAPIPermissionSet apis = requested_permissions_->apis(); |
| 143 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); | 137 for (ExtensionAPIPermissionSet::const_iterator i = apis.begin(); |
| 144 i != apis.end(); ++i) { | 138 i != apis.end(); ++i) { |
| 145 const ExtensionAPIPermission* api = info->GetByID(*i); | 139 const ExtensionAPIPermission* api = info->GetByID(*i); |
| 146 if (!api->supports_optional()) { | 140 if (!api->supports_optional()) { |
| 147 error_ = ExtensionErrorUtils::FormatErrorMessage( | 141 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 148 kNotWhitelistedError, api->name()); | 142 kNotWhitelistedError, api->name()); |
| 149 return false; | 143 return false; |
| 150 } | 144 } |
| 151 } | 145 } |
| 152 | 146 |
| 153 // The requested permissions must be defined as optional in the manifest. | 147 // The requested permissions must be defined as optional in the manifest. |
| 154 if (!GetExtension()->optional_permission_set()->Contains( | 148 if (!GetExtension()->optional_permission_set()->Contains( |
| 155 *requested_permissions_)) { | 149 *requested_permissions_)) { |
| 156 error_ = kNotInOptionalPermissionsError; | 150 error_ = kNotInOptionalPermissionsError; |
| 157 result_.reset(Value::CreateBooleanValue(false)); | 151 result_.reset(Request::Result::Create(false)); |
| 158 return false; | 152 return false; |
| 159 } | 153 } |
| 160 | 154 |
| 161 // We don't need to prompt the user if the requested permissions are a subset | 155 // We don't need to prompt the user if the requested permissions are a subset |
| 162 // of the granted permissions set. | 156 // of the granted permissions set. |
| 163 const ExtensionPermissionSet* granted = | 157 const ExtensionPermissionSet* granted = |
| 164 prefs->GetGrantedPermissions(GetExtension()->id()); | 158 prefs->GetGrantedPermissions(GetExtension()->id()); |
| 165 if (granted && granted->Contains(*requested_permissions_)) { | 159 if (granted && granted->Contains(*requested_permissions_)) { |
| 166 PermissionsUpdater perms_updater(profile()); | 160 PermissionsUpdater perms_updater(profile()); |
| 167 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); | 161 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); |
| 168 result_.reset(Value::CreateBooleanValue(true)); | 162 result_.reset(Request::Result::Create(true)); |
| 169 SendResponse(true); | 163 SendResponse(true); |
| 170 return true; | 164 return true; |
| 171 } | 165 } |
| 172 | 166 |
| 173 // Filter out the granted permissions so we only prompt for new ones. | 167 // Filter out the granted permissions so we only prompt for new ones. |
| 174 requested_permissions_ = ExtensionPermissionSet::CreateDifference( | 168 requested_permissions_ = ExtensionPermissionSet::CreateDifference( |
| 175 requested_permissions_.get(), granted); | 169 requested_permissions_.get(), granted); |
| 176 | 170 |
| 177 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). | 171 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). |
| 178 | 172 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 192 this, GetExtension(), requested_permissions_.get()); | 186 this, GetExtension(), requested_permissions_.get()); |
| 193 } | 187 } |
| 194 | 188 |
| 195 return true; | 189 return true; |
| 196 } | 190 } |
| 197 | 191 |
| 198 void RequestPermissionsFunction::InstallUIProceed() { | 192 void RequestPermissionsFunction::InstallUIProceed() { |
| 199 PermissionsUpdater perms_updater(profile()); | 193 PermissionsUpdater perms_updater(profile()); |
| 200 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); | 194 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); |
| 201 | 195 |
| 202 result_.reset(Value::CreateBooleanValue(true)); | 196 result_.reset(Request::Result::Create(true)); |
| 203 SendResponse(true); | 197 SendResponse(true); |
| 204 | 198 |
| 205 Release(); // Balanced in RunImpl(). | 199 Release(); // Balanced in RunImpl(). |
| 206 } | 200 } |
| 207 | 201 |
| 208 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { | 202 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { |
| 209 result_.reset(Value::CreateBooleanValue(false)); | 203 result_.reset(Request::Result::Create(false)); |
| 210 SendResponse(true); | 204 SendResponse(true); |
| 211 | 205 |
| 212 Release(); // Balanced in RunImpl(). | 206 Release(); // Balanced in RunImpl(). |
| 213 } | 207 } |
| OLD | NEW |