Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(394)

Side by Side Diff: chrome/browser/extensions/api/permissions/permissions_api.cc

Issue 2399423004: [Extensions] Convert some ChromeSyncExtensionFunctions (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 enum AutoConfirmForTest { 47 enum AutoConfirmForTest {
48 DO_NOT_SKIP = 0, 48 DO_NOT_SKIP = 0,
49 PROCEED, 49 PROCEED,
50 ABORT 50 ABORT
51 }; 51 };
52 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP; 52 AutoConfirmForTest auto_confirm_for_tests = DO_NOT_SKIP;
53 bool ignore_user_gesture_for_tests = false; 53 bool ignore_user_gesture_for_tests = false;
54 54
55 } // namespace 55 } // namespace
56 56
57 bool PermissionsContainsFunction::RunSync() { 57 ExtensionFunction::ResponseAction PermissionsContainsFunction::Run() {
58 std::unique_ptr<Contains::Params> params(Contains::Params::Create(*args_)); 58 std::unique_ptr<Contains::Params> params(Contains::Params::Create(*args_));
59 EXTENSION_FUNCTION_VALIDATE(params); 59 EXTENSION_FUNCTION_VALIDATE(params);
60 60
61 // NOTE: |permissions| is not used to make any security decisions. Therefore, 61 // NOTE: |permissions| is not used to make any security decisions. Therefore,
62 // it is entirely fine to set |allow_file_access| to true below. This will 62 // it is entirely fine to set |allow_file_access| to true below. This will
63 // avoid throwing error when extension() doesn't have access to file://. 63 // avoid throwing error when extension() doesn't have access to file://.
64 std::string error;
64 std::unique_ptr<const PermissionSet> permissions = 65 std::unique_ptr<const PermissionSet> permissions =
65 helpers::UnpackPermissionSet(params->permissions, 66 helpers::UnpackPermissionSet(params->permissions,
66 true /* allow_file_access */, &error_); 67 true /* allow_file_access */, &error);
67 if (!permissions.get()) 68 if (!permissions.get())
68 return false; 69 return RespondNow(Error(error));
69 70
70 results_ = Contains::Results::Create( 71 return RespondNow(ArgumentList(Contains::Results::Create(
71 extension()->permissions_data()->active_permissions().Contains( 72 extension()->permissions_data()->active_permissions().Contains(
72 *permissions)); 73 *permissions))));
73 return true;
74 } 74 }
75 75
76 bool PermissionsGetAllFunction::RunSync() { 76 ExtensionFunction::ResponseAction PermissionsGetAllFunction::Run() {
77 std::unique_ptr<Permissions> permissions = helpers::PackPermissionSet( 77 std::unique_ptr<Permissions> permissions = helpers::PackPermissionSet(
78 extension()->permissions_data()->active_permissions()); 78 extension()->permissions_data()->active_permissions());
79 results_ = GetAll::Results::Create(*permissions); 79 return RespondNow(ArgumentList(GetAll::Results::Create(*permissions)));
80 return true;
81 } 80 }
82 81
83 bool PermissionsRemoveFunction::RunSync() { 82 ExtensionFunction::ResponseAction PermissionsRemoveFunction::Run() {
84 std::unique_ptr<Remove::Params> params(Remove::Params::Create(*args_)); 83 std::unique_ptr<Remove::Params> params(Remove::Params::Create(*args_));
85 EXTENSION_FUNCTION_VALIDATE(params); 84 EXTENSION_FUNCTION_VALIDATE(params);
86 85
86 std::string error;
87 std::unique_ptr<const PermissionSet> permissions = 87 std::unique_ptr<const PermissionSet> permissions =
88 helpers::UnpackPermissionSet( 88 helpers::UnpackPermissionSet(params->permissions,
89 params->permissions, 89 ExtensionPrefs::Get(browser_context())
90 ExtensionPrefs::Get(GetProfile())->AllowFileAccess(extension_->id()), 90 ->AllowFileAccess(extension_->id()),
91 &error_); 91 &error);
92
92 if (!permissions.get()) 93 if (!permissions.get())
93 return false; 94 return RespondNow(Error(error));
94 95
95 // Make sure they're only trying to remove permissions supported by this API. 96 // Make sure they're only trying to remove permissions supported by this API.
96 APIPermissionSet apis = permissions->apis(); 97 APIPermissionSet apis = permissions->apis();
97 for (APIPermissionSet::const_iterator i = apis.begin(); 98 for (const APIPermission* permission : apis) {
98 i != apis.end(); ++i) { 99 if (!permission->info()->supports_optional())
99 if (!i->info()->supports_optional()) { 100 return RespondNow(Error(kNotWhitelistedError, permission->name()));
100 error_ = ErrorUtils::FormatErrorMessage(
101 kNotWhitelistedError, i->name());
102 return false;
103 }
104 } 101 }
105 102
106 // Make sure we only remove optional permissions, and not required 103 // Make sure we only remove optional permissions, and not required
107 // permissions. Sadly, for some reason we support having a permission be both 104 // permissions. Sadly, for some reason we support having a permission be both
108 // optional and required (and should assume its required), so we need both of 105 // optional and required (and should assume its required), so we need both of
109 // these checks. 106 // these checks.
110 // TODO(devlin): *Why* do we support that? Should be a load error. 107 // TODO(devlin): *Why* do we support that? Should be a load error.
111 const PermissionSet& optional = 108 const PermissionSet& optional =
112 PermissionsParser::GetOptionalPermissions(extension()); 109 PermissionsParser::GetOptionalPermissions(extension());
113 const PermissionSet& required = 110 const PermissionSet& required =
114 PermissionsParser::GetRequiredPermissions(extension()); 111 PermissionsParser::GetRequiredPermissions(extension());
115 if (!optional.Contains(*permissions) || 112 if (!optional.Contains(*permissions) ||
116 !std::unique_ptr<const PermissionSet>( 113 !std::unique_ptr<const PermissionSet>(
117 PermissionSet::CreateIntersection(*permissions, required)) 114 PermissionSet::CreateIntersection(*permissions, required))
118 ->IsEmpty()) { 115 ->IsEmpty()) {
119 error_ = kCantRemoveRequiredPermissionsError; 116 return RespondNow(Error(kCantRemoveRequiredPermissionsError));
120 return false;
121 } 117 }
122 118
123 // Only try and remove those permissions that are active on the extension. 119 // Only try and remove those permissions that are active on the extension.
124 // For backwards compatability with behavior before this check was added, just 120 // For backwards compatability with behavior before this check was added, just
125 // silently remove any that aren't present. 121 // silently remove any that aren't present.
126 permissions = PermissionSet::CreateIntersection( 122 permissions = PermissionSet::CreateIntersection(
127 *permissions, extension()->permissions_data()->active_permissions()); 123 *permissions, extension()->permissions_data()->active_permissions());
128 124
129 PermissionsUpdater(GetProfile()) 125 PermissionsUpdater(browser_context())
130 .RemovePermissions(extension(), *permissions, 126 .RemovePermissions(extension(), *permissions,
131 PermissionsUpdater::REMOVE_SOFT); 127 PermissionsUpdater::REMOVE_SOFT);
132 results_ = Remove::Results::Create(true); 128 return RespondNow(ArgumentList(Remove::Results::Create(true)));
133 return true;
134 } 129 }
135 130
136 // static 131 // static
137 void PermissionsRequestFunction::SetAutoConfirmForTests(bool should_proceed) { 132 void PermissionsRequestFunction::SetAutoConfirmForTests(bool should_proceed) {
138 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT; 133 auto_confirm_for_tests = should_proceed ? PROCEED : ABORT;
139 } 134 }
140 135
141 // static 136 // static
142 void PermissionsRequestFunction::SetIgnoreUserGestureForTests( 137 void PermissionsRequestFunction::SetIgnoreUserGestureForTests(
143 bool ignore) { 138 bool ignore) {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 perms_updater.AddPermissions(extension(), *requested_permissions_); 250 perms_updater.AddPermissions(extension(), *requested_permissions_);
256 251
257 results_ = Request::Results::Create(true); 252 results_ = Request::Results::Create(true);
258 } 253 }
259 254
260 SendResponse(true); 255 SendResponse(true);
261 Release(); // Balanced in RunAsync(). 256 Release(); // Balanced in RunAsync().
262 } 257 }
263 258
264 } // namespace extensions 259 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698