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 SetSingleResult(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 SetSingleResult(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 SetSingleResult(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 SetSingleResult(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 SetSingleResult(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 SetSingleResult(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 20 matching lines...) Expand all Loading... |
168 error_ = ExtensionErrorUtils::FormatErrorMessage( | 168 error_ = ExtensionErrorUtils::FormatErrorMessage( |
169 kNotWhitelistedError, api->name()); | 169 kNotWhitelistedError, api->name()); |
170 return false; | 170 return false; |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 // The requested permissions must be defined as optional in the manifest. | 174 // The requested permissions must be defined as optional in the manifest. |
175 if (!GetExtension()->optional_permission_set()->Contains( | 175 if (!GetExtension()->optional_permission_set()->Contains( |
176 *requested_permissions_)) { | 176 *requested_permissions_)) { |
177 error_ = kNotInOptionalPermissionsError; | 177 error_ = kNotInOptionalPermissionsError; |
178 result_.reset(Request::Result::Create(false)); | 178 SetSingleResult(Request::Result::Create(false)); |
179 return false; | 179 return false; |
180 } | 180 } |
181 | 181 |
182 // We don't need to prompt the user if the requested permissions are a subset | 182 // We don't need to prompt the user if the requested permissions are a subset |
183 // of the granted permissions set. | 183 // of the granted permissions set. |
184 scoped_refptr<const PermissionSet> granted = | 184 scoped_refptr<const PermissionSet> granted = |
185 prefs->GetGrantedPermissions(GetExtension()->id()); | 185 prefs->GetGrantedPermissions(GetExtension()->id()); |
186 if (granted.get() && granted->Contains(*requested_permissions_)) { | 186 if (granted.get() && granted->Contains(*requested_permissions_)) { |
187 PermissionsUpdater perms_updater(profile()); | 187 PermissionsUpdater perms_updater(profile()); |
188 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); | 188 perms_updater.AddPermissions(GetExtension(), requested_permissions_.get()); |
189 result_.reset(Request::Result::Create(true)); | 189 SetSingleResult(Request::Result::Create(true)); |
190 SendResponse(true); | 190 SendResponse(true); |
191 return true; | 191 return true; |
192 } | 192 } |
193 | 193 |
194 // Filter out the granted permissions so we only prompt for new ones. | 194 // Filter out the granted permissions so we only prompt for new ones. |
195 requested_permissions_ = PermissionSet::CreateDifference( | 195 requested_permissions_ = PermissionSet::CreateDifference( |
196 requested_permissions_.get(), granted.get()); | 196 requested_permissions_.get(), granted.get()); |
197 | 197 |
198 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). | 198 AddRef(); // Balanced in InstallUIProceed() / InstallUIAbort(). |
199 | 199 |
200 // We don't need to show the prompt if there are no new warnings, or if | 200 // We don't need to show the prompt if there are no new warnings, or if |
201 // we're skipping the confirmation UI. All extension types but INTERNAL | 201 // we're skipping the confirmation UI. All extension types but INTERNAL |
202 // are allowed to silently increase their permission level. | 202 // are allowed to silently increase their permission level. |
203 if (auto_confirm_for_tests == PROCEED || | 203 if (auto_confirm_for_tests == PROCEED || |
204 requested_permissions_->GetWarningMessages().size() == 0) { | 204 requested_permissions_->GetWarningMessages().size() == 0) { |
205 InstallUIProceed(); | 205 InstallUIProceed(); |
206 } else if (auto_confirm_for_tests == ABORT) { | 206 } else if (auto_confirm_for_tests == ABORT) { |
207 // Pretend the user clicked cancel. | 207 // Pretend the user clicked cancel. |
208 InstallUIAbort(true); | 208 InstallUIAbort(true); |
209 } else { | 209 } else { |
210 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); | 210 CHECK_EQ(DO_NOT_SKIP, auto_confirm_for_tests); |
211 install_ui_.reset( | 211 install_ui_.reset( |
212 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); | 212 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
213 install_ui_->ConfirmPermissions( | 213 install_ui_->ConfirmPermissions( |
214 this, GetExtension(), requested_permissions_.get()); | 214 this, GetExtension(), requested_permissions_.get()); |
215 } | 215 } |
216 | 216 |
217 return true; | 217 return true; |
218 } | 218 } |
OLD | NEW |