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 "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 8 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/permissions_updater.h" | 10 #include "chrome/browser/extensions/permissions_updater.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 bool ContainsPermissionsFunction::RunImpl() { | 54 bool ContainsPermissionsFunction::RunImpl() { |
55 scoped_ptr<Contains::Params> params(Contains::Params::Create(*args_)); | 55 scoped_ptr<Contains::Params> params(Contains::Params::Create(*args_)); |
56 | 56 |
57 scoped_refptr<PermissionSet> permissions = | 57 scoped_refptr<PermissionSet> permissions = |
58 helpers::UnpackPermissionSet(params->permissions, &error_); | 58 helpers::UnpackPermissionSet(params->permissions, &error_); |
59 if (!permissions.get()) | 59 if (!permissions.get()) |
60 return false; | 60 return false; |
61 | 61 |
62 result_.reset(Contains::Result::Create( | 62 SetResult(Contains::Result::Create( |
63 GetExtension()->GetActivePermissions()->Contains(*permissions))); | 63 GetExtension()->GetActivePermissions()->Contains(*permissions))); |
64 return true; | 64 return true; |
65 } | 65 } |
66 | 66 |
67 bool GetAllPermissionsFunction::RunImpl() { | 67 bool GetAllPermissionsFunction::RunImpl() { |
68 scoped_ptr<Permissions> permissions = | 68 scoped_ptr<Permissions> permissions = |
69 helpers::PackPermissionSet(GetExtension()->GetActivePermissions()); | 69 helpers::PackPermissionSet(GetExtension()->GetActivePermissions()); |
70 result_.reset(GetAll::Result::Create(*permissions)); | 70 SetResult(GetAll::Result::Create(*permissions)); |
71 return true; | 71 return true; |
72 } | 72 } |
73 | 73 |
74 bool RemovePermissionsFunction::RunImpl() { | 74 bool RemovePermissionsFunction::RunImpl() { |
75 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); | 75 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
76 EXTENSION_FUNCTION_VALIDATE(params.get()); | 76 EXTENSION_FUNCTION_VALIDATE(params.get()); |
77 | 77 |
78 scoped_refptr<PermissionSet> permissions = | 78 scoped_refptr<PermissionSet> permissions = |
79 helpers::UnpackPermissionSet(params->permissions, &error_); | 79 helpers::UnpackPermissionSet(params->permissions, &error_); |
80 if (!permissions.get()) | 80 if (!permissions.get()) |
(...skipping 13 matching lines...) Expand all Loading... |
94 return false; | 94 return false; |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 // Make sure we don't remove any required pemissions. | 98 // Make sure we don't remove any required pemissions. |
99 const PermissionSet* required = extension->required_permission_set(); | 99 const PermissionSet* required = extension->required_permission_set(); |
100 scoped_refptr<PermissionSet> intersection( | 100 scoped_refptr<PermissionSet> intersection( |
101 PermissionSet::CreateIntersection(permissions.get(), required)); | 101 PermissionSet::CreateIntersection(permissions.get(), required)); |
102 if (!intersection->IsEmpty()) { | 102 if (!intersection->IsEmpty()) { |
103 error_ = kCantRemoveRequiredPermissionsError; | 103 error_ = kCantRemoveRequiredPermissionsError; |
104 result_.reset(Remove::Result::Create(false)); | 104 SetResult(Remove::Result::Create(false)); |
105 return false; | 105 return false; |
106 } | 106 } |
107 | 107 |
108 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get()); | 108 PermissionsUpdater(profile()).RemovePermissions(extension, permissions.get()); |
109 result_.reset(Remove::Result::Create(true)); | 109 SetResult(Remove::Result::Create(true)); |
110 return true; | 110 return true; |
111 } | 111 } |
112 | 112 |
113 // static | 113 // static |
114 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { | 114 void RequestPermissionsFunction::SetAutoConfirmForTests(bool should_proceed) { |
115 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; | 115 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; |
116 } | 116 } |
117 | 117 |
118 // static | 118 // static |
119 void RequestPermissionsFunction::SetIgnoreUserGestureForTests( | 119 void RequestPermissionsFunction::SetIgnoreUserGestureForTests( |
120 bool ignore) { | 120 bool ignore) { |
121 ignore_user_gesture_for_tests = ignore; | 121 ignore_user_gesture_for_tests = ignore; |
122 } | 122 } |
123 | 123 |
124 RequestPermissionsFunction::RequestPermissionsFunction() {} | 124 RequestPermissionsFunction::RequestPermissionsFunction() {} |
125 | 125 |
126 void RequestPermissionsFunction::InstallUIProceed() { | 126 void RequestPermissionsFunction::InstallUIProceed() { |
127 PermissionsUpdater perms_updater(profile()); | 127 PermissionsUpdater perms_updater(profile()); |
128 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); | 128 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); |
129 | 129 |
130 result_.reset(Request::Result::Create(true)); | 130 SetResult(Request::Result::Create(true)); |
131 SendResponse(true); | 131 SendResponse(true); |
132 | 132 |
133 Release(); // Balanced in RunImpl(). | 133 Release(); // Balanced in RunImpl(). |
134 } | 134 } |
135 | 135 |
136 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { | 136 void RequestPermissionsFunction::InstallUIAbort(bool user_initiated) { |
137 result_.reset(Request::Result::Create(false)); | 137 SetResult(Request::Result::Create(false)); |
138 SendResponse(true); | 138 SendResponse(true); |
139 | 139 |
140 Release(); // Balanced in RunImpl(). | 140 Release(); // Balanced in RunImpl(). |
141 } | 141 } |
142 | 142 |
143 RequestPermissionsFunction::~RequestPermissionsFunction() {} | 143 RequestPermissionsFunction::~RequestPermissionsFunction() {} |
144 | 144 |
145 bool RequestPermissionsFunction::RunImpl() { | 145 bool RequestPermissionsFunction::RunImpl() { |
146 if (!user_gesture() && !ignore_user_gesture_for_tests) { | 146 if (!user_gesture() && !ignore_user_gesture_for_tests) { |
147 error_ = kUserGestureRequiredError; | 147 error_ = kUserGestureRequiredError; |
(...skipping 21 matching lines...) Expand all Loading... |
169 error_ = ExtensionErrorUtils::FormatErrorMessage( | 169 error_ = ExtensionErrorUtils::FormatErrorMessage( |
170 kNotWhitelistedError, api->name()); | 170 kNotWhitelistedError, api->name()); |
171 return false; | 171 return false; |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 // The requested permissions must be defined as optional in the manifest. | 175 // The requested permissions must be defined as optional in the manifest. |
176 if (!GetExtension()->optional_permission_set()->Contains( | 176 if (!GetExtension()->optional_permission_set()->Contains( |
177 *requested_permissions_)) { | 177 *requested_permissions_)) { |
178 error_ = kNotInOptionalPermissionsError; | 178 error_ = kNotInOptionalPermissionsError; |
179 result_.reset(Request::Result::Create(false)); | 179 SetResult(Request::Result::Create(false)); |
180 return false; | 180 return false; |
181 } | 181 } |
182 | 182 |
183 // We don't need to prompt the user if the requested permissions are a subset | 183 // We don't need to prompt the user if the requested permissions are a subset |
184 // of the granted permissions set. | 184 // of the granted permissions set. |
185 scoped_refptr<const PermissionSet> granted = | 185 scoped_refptr<const PermissionSet> granted = |
186 prefs->GetGrantedPermissions(GetExtension()->id()); | 186 prefs->GetGrantedPermissions(GetExtension()->id()); |
187 if (granted.get() && granted->Contains(*requested_permissions_)) { | 187 if (granted.get() && granted->Contains(*requested_permissions_)) { |
188 PermissionsUpdater perms_updater(profile()); | 188 PermissionsUpdater perms_updater(profile()); |
189 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); | 189 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); |
190 result_.reset(Request::Result::Create(true)); | 190 SetResult(Request::Result::Create(true)); |
191 SendResponse(true); | 191 SendResponse(true); |
192 return true; | 192 return true; |
193 } | 193 } |
194 | 194 |
195 // Filter out the granted permissions so we only prompt for new ones. | 195 // Filter out the granted permissions so we only prompt for new ones. |
196 requested_permissions_ = PermissionSet::CreateDifference( | 196 requested_permissions_ = PermissionSet::CreateDifference( |
197 requested_permissions_.get(), granted.get()); | 197 requested_permissions_.get(), granted.get()); |
198 | 198 |
199 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). | 199 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). |
200 | 200 |
201 // We don't need to show the prompt if there are no new warnings, or if | 201 // We don't need to show the prompt if there are no new warnings, or if |
202 // we're skipping the confirmation UI. All extension types but INTERNAL | 202 // we're skipping the confirmation UI. All extension types but INTERNAL |
203 // are allowed to silently increase their permission level. | 203 // are allowed to silently increase their permission level. |
204 if (auto_confirm_for_tests == PROCEED || | 204 if (auto_confirm_for_tests == PROCEED || |
205 requested_permissions_->GetWarningMessages().size() == 0) { | 205 requested_permissions_->GetWarningMessages().size() == 0) { |
206 InstallUIProceed(); | 206 InstallUIProceed(); |
207 } else if (auto_confirm_for_tests == ABORT) { | 207 } else if (auto_confirm_for_tests == ABORT) { |
208 // Pretend the user clicked cancel. | 208 // Pretend the user clicked cancel. |
209 InstallUIAbort(true); | 209 InstallUIAbort(true); |
210 } else { | 210 } else { |
211 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); | 211 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); |
212 install_ui_.reset( | 212 install_ui_.reset( |
213 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); | 213 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
214 install_ui_->ConfirmPermissions( | 214 install_ui_->ConfirmPermissions( |
215 this, GetExtension(), requested_permissions_.get()); | 215 this, GetExtension(), requested_permissions_.get()); |
216 } | 216 } |
217 | 217 |
218 return true; | 218 return true; |
219 } | 219 } |
OLD | NEW |