OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/chromeos/extensions/public_session_permission_helper.h" | 5 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 // This class is the internal implementation of HandlePermissionRequest(). It | 37 // This class is the internal implementation of HandlePermissionRequest(). It |
38 // contains the actual prompt showing and resolving logic, and it caches the | 38 // contains the actual prompt showing and resolving logic, and it caches the |
39 // user choices. | 39 // user choices. |
40 class PublicSessionPermissionHelper { | 40 class PublicSessionPermissionHelper { |
41 public: | 41 public: |
42 PublicSessionPermissionHelper(); | 42 PublicSessionPermissionHelper(); |
43 PublicSessionPermissionHelper(PublicSessionPermissionHelper&& other); | 43 PublicSessionPermissionHelper(PublicSessionPermissionHelper&& other); |
44 ~PublicSessionPermissionHelper(); | 44 ~PublicSessionPermissionHelper(); |
45 | 45 |
46 void HandlePermissionRequestImpl(const Extension& extension, | 46 bool HandlePermissionRequestImpl(const Extension& extension, |
47 const PermissionIDSet& requested_permissions, | 47 const PermissionIDSet& requested_permissions, |
48 content::WebContents* web_contents, | 48 content::WebContents* web_contents, |
49 const RequestResolvedCallback& callback, | 49 const RequestResolvedCallback& callback, |
50 const PromptFactory& prompt_factory); | 50 const PromptFactory& prompt_factory); |
51 | 51 |
| 52 bool PermissionAllowedImpl(APIPermission::ID permission); |
| 53 |
52 private: | 54 private: |
53 void ResolvePermissionPrompt(const ExtensionInstallPrompt* prompt, | 55 void ResolvePermissionPrompt(const ExtensionInstallPrompt* prompt, |
54 const PermissionIDSet& unprompted_permissions, | 56 const PermissionIDSet& unprompted_permissions, |
55 ExtensionInstallPrompt::Result prompt_result); | 57 ExtensionInstallPrompt::Result prompt_result); |
56 | 58 |
57 PermissionIDSet FilterAllowedPermissions(const PermissionIDSet& permissions); | 59 PermissionIDSet FilterAllowedPermissions(const PermissionIDSet& permissions); |
58 | 60 |
59 struct RequestCallback { | 61 struct RequestCallback { |
60 RequestCallback(const RequestResolvedCallback& callback, | 62 RequestCallback(const RequestResolvedCallback& callback, |
61 const PermissionIDSet& permission_list); | 63 const PermissionIDSet& permission_list); |
(...skipping 13 matching lines...) Expand all Loading... |
75 DISALLOW_COPY_AND_ASSIGN(PublicSessionPermissionHelper); | 77 DISALLOW_COPY_AND_ASSIGN(PublicSessionPermissionHelper); |
76 }; | 78 }; |
77 | 79 |
78 PublicSessionPermissionHelper::PublicSessionPermissionHelper() {} | 80 PublicSessionPermissionHelper::PublicSessionPermissionHelper() {} |
79 | 81 |
80 PublicSessionPermissionHelper::PublicSessionPermissionHelper( | 82 PublicSessionPermissionHelper::PublicSessionPermissionHelper( |
81 PublicSessionPermissionHelper&& other) = default; | 83 PublicSessionPermissionHelper&& other) = default; |
82 | 84 |
83 PublicSessionPermissionHelper::~PublicSessionPermissionHelper() {} | 85 PublicSessionPermissionHelper::~PublicSessionPermissionHelper() {} |
84 | 86 |
85 void PublicSessionPermissionHelper::HandlePermissionRequestImpl( | 87 bool PublicSessionPermissionHelper::HandlePermissionRequestImpl( |
86 const Extension& extension, | 88 const Extension& extension, |
87 const PermissionIDSet& requested_permissions, | 89 const PermissionIDSet& requested_permissions, |
88 content::WebContents* web_contents, | 90 content::WebContents* web_contents, |
89 const RequestResolvedCallback& callback, | 91 const RequestResolvedCallback& callback, |
90 const PromptFactory& prompt_factory) { | 92 const PromptFactory& prompt_factory) { |
91 CHECK(web_contents); | 93 CHECK(web_contents); |
92 | 94 |
93 PermissionIDSet unresolved_permissions = PermissionIDSet::Difference( | 95 PermissionIDSet unresolved_permissions = PermissionIDSet::Difference( |
94 requested_permissions, allowed_permission_set_); | 96 requested_permissions, allowed_permission_set_); |
95 unresolved_permissions = PermissionIDSet::Difference( | 97 unresolved_permissions = PermissionIDSet::Difference( |
96 unresolved_permissions, denied_permission_set_); | 98 unresolved_permissions, denied_permission_set_); |
97 if (unresolved_permissions.empty()) { | 99 if (unresolved_permissions.empty()) { |
98 // All requested permissions are already resolved. | 100 // All requested permissions are already resolved. |
99 callback.Run(FilterAllowedPermissions(requested_permissions)); | 101 if (!callback.is_null()) |
100 return; | 102 callback.Run(FilterAllowedPermissions(requested_permissions)); |
| 103 return true; |
101 } | 104 } |
102 | 105 |
103 // Since not all permissions are resolved yet, queue the callback to be called | 106 // Since not all permissions are resolved yet, queue the callback to be called |
104 // when all of them are resolved. | 107 // when all of them are resolved. |
105 callbacks_.push_back(RequestCallback(callback, requested_permissions)); | 108 if (!callback.is_null()) |
| 109 callbacks_.push_back(RequestCallback(callback, requested_permissions)); |
106 | 110 |
107 PermissionIDSet unprompted_permissions = PermissionIDSet::Difference( | 111 PermissionIDSet unprompted_permissions = PermissionIDSet::Difference( |
108 unresolved_permissions, prompted_permission_set_); | 112 unresolved_permissions, prompted_permission_set_); |
109 if (unprompted_permissions.empty()) { | 113 if (unprompted_permissions.empty()) { |
110 // Some permissions aren't resolved yet, but they are currently being | 114 // Some permissions aren't resolved yet, but they are currently being |
111 // prompted for, so no need to show a prompt. | 115 // prompted for, so no need to show a prompt. |
112 return; | 116 return false; |
113 } | 117 } |
114 | 118 |
115 // Some permissions need prompting, setup the prompt and show it. | 119 // Some permissions need prompting, setup the prompt and show it. |
116 APIPermissionSet new_apis; | 120 APIPermissionSet new_apis; |
117 for (const auto& permission : unprompted_permissions) { | 121 for (const auto& permission : unprompted_permissions) { |
118 prompted_permission_set_.insert(permission.id()); | 122 prompted_permission_set_.insert(permission.id()); |
119 new_apis.insert(permission.id()); | 123 new_apis.insert(permission.id()); |
120 } | 124 } |
121 auto permission_set = base::MakeUnique<PermissionSet>( | 125 auto permission_set = base::MakeUnique<PermissionSet>( |
122 new_apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet()); | 126 new_apis, ManifestPermissionSet(), URLPatternSet(), URLPatternSet()); |
123 auto prompt = prompt_factory.Run(web_contents); | 127 auto prompt = prompt_factory.Run(web_contents); |
124 // This Unretained is safe because the lifetime of this object is until | 128 // This Unretained is safe because the lifetime of this object is until |
125 // process exit. | 129 // process exit. |
126 prompt->ShowDialog( | 130 prompt->ShowDialog( |
127 base::Bind(&PublicSessionPermissionHelper::ResolvePermissionPrompt, | 131 base::Bind(&PublicSessionPermissionHelper::ResolvePermissionPrompt, |
128 base::Unretained(this), prompt.get(), | 132 base::Unretained(this), prompt.get(), |
129 std::move(unprompted_permissions)), | 133 std::move(unprompted_permissions)), |
130 &extension, | 134 &extension, |
131 nullptr, // Use the extension icon. | 135 nullptr, // Use the extension icon. |
132 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | 136 base::MakeUnique<ExtensionInstallPrompt::Prompt>( |
133 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | 137 ExtensionInstallPrompt::PERMISSIONS_PROMPT), |
134 std::move(permission_set), | 138 std::move(permission_set), |
135 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | 139 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
136 prompts_.insert(std::move(prompt)); | 140 prompts_.insert(std::move(prompt)); |
| 141 |
| 142 return false; |
| 143 } |
| 144 |
| 145 bool PublicSessionPermissionHelper::PermissionAllowedImpl( |
| 146 APIPermission::ID permission) { |
| 147 return allowed_permission_set_.ContainsID(permission); |
137 } | 148 } |
138 | 149 |
139 void PublicSessionPermissionHelper::ResolvePermissionPrompt( | 150 void PublicSessionPermissionHelper::ResolvePermissionPrompt( |
140 const ExtensionInstallPrompt* prompt, | 151 const ExtensionInstallPrompt* prompt, |
141 const PermissionIDSet& unprompted_permissions, | 152 const PermissionIDSet& unprompted_permissions, |
142 ExtensionInstallPrompt::Result prompt_result) { | 153 ExtensionInstallPrompt::Result prompt_result) { |
143 PermissionIDSet& add_to_set = | 154 PermissionIDSet& add_to_set = |
144 prompt_result == ExtensionInstallPrompt::Result::ACCEPTED ? | 155 prompt_result == ExtensionInstallPrompt::Result::ACCEPTED ? |
145 allowed_permission_set_ : denied_permission_set_; | 156 allowed_permission_set_ : denied_permission_set_; |
146 for (const auto& permission : unprompted_permissions) { | 157 for (const auto& permission : unprompted_permissions) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 PublicSessionPermissionHelper::RequestCallback::RequestCallback( | 207 PublicSessionPermissionHelper::RequestCallback::RequestCallback( |
197 const RequestCallback& other) = default; | 208 const RequestCallback& other) = default; |
198 | 209 |
199 PublicSessionPermissionHelper::RequestCallback::~RequestCallback() {} | 210 PublicSessionPermissionHelper::RequestCallback::~RequestCallback() {} |
200 | 211 |
201 base::LazyInstance<std::map<ExtensionId, PublicSessionPermissionHelper>>::Leaky | 212 base::LazyInstance<std::map<ExtensionId, PublicSessionPermissionHelper>>::Leaky |
202 g_helpers = LAZY_INSTANCE_INITIALIZER; | 213 g_helpers = LAZY_INSTANCE_INITIALIZER; |
203 | 214 |
204 } // namespace | 215 } // namespace |
205 | 216 |
206 void HandlePermissionRequest(const Extension& extension, | 217 bool HandlePermissionRequest(const Extension& extension, |
207 const PermissionIDSet& requested_permissions, | 218 const PermissionIDSet& requested_permissions, |
208 content::WebContents* web_contents, | 219 content::WebContents* web_contents, |
209 const RequestResolvedCallback& callback, | 220 const RequestResolvedCallback& callback, |
210 const PromptFactory& prompt_factory) { | 221 const PromptFactory& prompt_factory) { |
211 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 222 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
212 const PromptFactory& factory = prompt_factory.is_null() | 223 const PromptFactory& factory = prompt_factory.is_null() |
213 ? base::Bind(&CreateExtensionInstallPrompt) | 224 ? base::Bind(&CreateExtensionInstallPrompt) |
214 : prompt_factory; | 225 : prompt_factory; |
215 return g_helpers.Get()[extension.id()].HandlePermissionRequestImpl( | 226 return g_helpers.Get()[extension.id()].HandlePermissionRequestImpl( |
216 extension, requested_permissions, web_contents, callback, factory); | 227 extension, requested_permissions, web_contents, callback, factory); |
217 } | 228 } |
218 | 229 |
| 230 bool PermissionAllowed(const Extension* extension, |
| 231 APIPermission::ID permission) { |
| 232 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 233 return g_helpers.Get()[extension->id()].PermissionAllowedImpl(permission); |
| 234 } |
| 235 |
219 void ResetPermissionsForTesting() { | 236 void ResetPermissionsForTesting() { |
220 // Clear out the std::map between tests. Just setting g_helpers to | 237 // Clear out the std::map between tests. Just setting g_helpers to |
221 // LAZY_INSTANCE_INITIALIZER again causes a memory leak (because of the | 238 // LAZY_INSTANCE_INITIALIZER again causes a memory leak (because of the |
222 // LazyInstance::Leaky). | 239 // LazyInstance::Leaky). |
223 g_helpers.Get().clear(); | 240 g_helpers.Get().clear(); |
224 } | 241 } |
225 | 242 |
226 } // namespace permission_helper | 243 } // namespace permission_helper |
227 } // namespace extensions | 244 } // namespace extensions |
OLD | NEW |