OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "modules/permissions/Permissions.h" | 5 #include "modules/permissions/Permissions.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
9 #include "bindings/core/v8/Nullable.h" | 9 #include "bindings/core/v8/Nullable.h" |
10 #include "bindings/core/v8/ScriptPromise.h" | 10 #include "bindings/core/v8/ScriptPromise.h" |
11 #include "bindings/core/v8/ScriptPromiseResolver.h" | 11 #include "bindings/core/v8/ScriptPromiseResolver.h" |
12 #include "bindings/modules/v8/V8MidiPermissionDescriptor.h" | 12 #include "bindings/modules/v8/V8MidiPermissionDescriptor.h" |
13 #include "bindings/modules/v8/V8PermissionDescriptor.h" | 13 #include "bindings/modules/v8/V8PermissionDescriptor.h" |
14 #include "bindings/modules/v8/V8PushPermissionDescriptor.h" | 14 #include "bindings/modules/v8/V8PushPermissionDescriptor.h" |
15 #include "core/dom/DOMException.h" | 15 #include "core/dom/DOMException.h" |
16 #include "core/dom/Document.h" | 16 #include "core/dom/Document.h" |
17 #include "core/dom/ExceptionCode.h" | 17 #include "core/dom/ExceptionCode.h" |
| 18 #include "core/dom/ExecutionContext.h" |
18 #include "core/frame/LocalFrame.h" | 19 #include "core/frame/LocalFrame.h" |
19 #include "modules/permissions/PermissionDescriptor.h" | 20 #include "modules/permissions/PermissionDescriptor.h" |
20 #include "modules/permissions/PermissionStatus.h" | 21 #include "modules/permissions/PermissionStatus.h" |
21 #include "modules/permissions/PermissionUtils.h" | 22 #include "modules/permissions/PermissionUtils.h" |
22 #include "platform/UserGestureIndicator.h" | 23 #include "platform/UserGestureIndicator.h" |
23 #include "platform/wtf/Functional.h" | 24 #include "platform/wtf/Functional.h" |
24 #include "platform/wtf/NotFound.h" | 25 #include "platform/wtf/NotFound.h" |
25 #include "platform/wtf/PtrUtil.h" | 26 #include "platform/wtf/PtrUtil.h" |
26 #include "platform/wtf/Vector.h" | 27 #include "platform/wtf/Vector.h" |
27 #include "public/platform/Platform.h" | 28 #include "public/platform/Platform.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 ExceptionState exception_state(script_state->GetIsolate(), | 102 ExceptionState exception_state(script_state->GetIsolate(), |
102 ExceptionState::kGetterContext, "Permissions", | 103 ExceptionState::kGetterContext, "Permissions", |
103 "query"); | 104 "query"); |
104 PermissionDescriptorPtr descriptor = | 105 PermissionDescriptorPtr descriptor = |
105 ParsePermission(script_state, raw_permission, exception_state); | 106 ParsePermission(script_state, raw_permission, exception_state); |
106 if (exception_state.HadException()) | 107 if (exception_state.HadException()) |
107 return exception_state.Reject(script_state); | 108 return exception_state.Reject(script_state); |
108 | 109 |
109 // This must be called after `parsePermission` because the website might | 110 // This must be called after `parsePermission` because the website might |
110 // be able to run code. | 111 // be able to run code. |
111 PermissionService* service = GetService(script_state->GetExecutionContext()); | 112 PermissionService* service = GetService(ExecutionContext::From(script_state)); |
112 if (!service) | 113 if (!service) |
113 return ScriptPromise::RejectWithDOMException( | 114 return ScriptPromise::RejectWithDOMException( |
114 script_state, | 115 script_state, |
115 DOMException::Create( | 116 DOMException::Create( |
116 kInvalidStateError, | 117 kInvalidStateError, |
117 "In its current state, the global scope can't query permissions.")); | 118 "In its current state, the global scope can't query permissions.")); |
118 | 119 |
119 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 120 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
120 ScriptPromise promise = resolver->Promise(); | 121 ScriptPromise promise = resolver->Promise(); |
121 | 122 |
122 // If the current origin is a file scheme, it will unlikely return a | 123 // If the current origin is a file scheme, it will unlikely return a |
123 // meaningful value because most APIs are broken on file scheme and no | 124 // meaningful value because most APIs are broken on file scheme and no |
124 // permission prompt will be shown even if the returned permission will most | 125 // permission prompt will be shown even if the returned permission will most |
125 // likely be "prompt". | 126 // likely be "prompt". |
126 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); | 127 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); |
127 service->HasPermission( | 128 service->HasPermission( |
128 std::move(descriptor), | 129 std::move(descriptor), |
129 script_state->GetExecutionContext()->GetSecurityOrigin(), | 130 ExecutionContext::From(script_state)->GetSecurityOrigin(), |
130 ConvertToBaseCallback(WTF::Bind( | 131 ConvertToBaseCallback(WTF::Bind( |
131 &Permissions::TaskComplete, WrapPersistent(this), | 132 &Permissions::TaskComplete, WrapPersistent(this), |
132 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); | 133 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); |
133 return promise; | 134 return promise; |
134 } | 135 } |
135 | 136 |
136 ScriptPromise Permissions::request(ScriptState* script_state, | 137 ScriptPromise Permissions::request(ScriptState* script_state, |
137 const Dictionary& raw_permission) { | 138 const Dictionary& raw_permission) { |
138 ExceptionState exception_state(script_state->GetIsolate(), | 139 ExceptionState exception_state(script_state->GetIsolate(), |
139 ExceptionState::kGetterContext, "Permissions", | 140 ExceptionState::kGetterContext, "Permissions", |
140 "request"); | 141 "request"); |
141 PermissionDescriptorPtr descriptor = | 142 PermissionDescriptorPtr descriptor = |
142 ParsePermission(script_state, raw_permission, exception_state); | 143 ParsePermission(script_state, raw_permission, exception_state); |
143 if (exception_state.HadException()) | 144 if (exception_state.HadException()) |
144 return exception_state.Reject(script_state); | 145 return exception_state.Reject(script_state); |
145 | 146 |
146 // This must be called after `parsePermission` because the website might | 147 // This must be called after `parsePermission` because the website might |
147 // be able to run code. | 148 // be able to run code. |
148 PermissionService* service = GetService(script_state->GetExecutionContext()); | 149 PermissionService* service = GetService(ExecutionContext::From(script_state)); |
149 if (!service) | 150 if (!service) |
150 return ScriptPromise::RejectWithDOMException( | 151 return ScriptPromise::RejectWithDOMException( |
151 script_state, DOMException::Create(kInvalidStateError, | 152 script_state, DOMException::Create(kInvalidStateError, |
152 "In its current state, the global " | 153 "In its current state, the global " |
153 "scope can't request permissions.")); | 154 "scope can't request permissions.")); |
154 | 155 |
155 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 156 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
156 ScriptPromise promise = resolver->Promise(); | 157 ScriptPromise promise = resolver->Promise(); |
157 | 158 |
158 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); | 159 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); |
159 service->RequestPermission( | 160 service->RequestPermission( |
160 std::move(descriptor), | 161 std::move(descriptor), |
161 script_state->GetExecutionContext()->GetSecurityOrigin(), | 162 ExecutionContext::From(script_state)->GetSecurityOrigin(), |
162 UserGestureIndicator::ProcessingUserGestureThreadSafe(), | 163 UserGestureIndicator::ProcessingUserGestureThreadSafe(), |
163 ConvertToBaseCallback(WTF::Bind( | 164 ConvertToBaseCallback(WTF::Bind( |
164 &Permissions::TaskComplete, WrapPersistent(this), | 165 &Permissions::TaskComplete, WrapPersistent(this), |
165 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); | 166 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); |
166 return promise; | 167 return promise; |
167 } | 168 } |
168 | 169 |
169 ScriptPromise Permissions::revoke(ScriptState* script_state, | 170 ScriptPromise Permissions::revoke(ScriptState* script_state, |
170 const Dictionary& raw_permission) { | 171 const Dictionary& raw_permission) { |
171 ExceptionState exception_state(script_state->GetIsolate(), | 172 ExceptionState exception_state(script_state->GetIsolate(), |
172 ExceptionState::kGetterContext, "Permissions", | 173 ExceptionState::kGetterContext, "Permissions", |
173 "revoke"); | 174 "revoke"); |
174 PermissionDescriptorPtr descriptor = | 175 PermissionDescriptorPtr descriptor = |
175 ParsePermission(script_state, raw_permission, exception_state); | 176 ParsePermission(script_state, raw_permission, exception_state); |
176 if (exception_state.HadException()) | 177 if (exception_state.HadException()) |
177 return exception_state.Reject(script_state); | 178 return exception_state.Reject(script_state); |
178 | 179 |
179 // This must be called after `parsePermission` because the website might | 180 // This must be called after `parsePermission` because the website might |
180 // be able to run code. | 181 // be able to run code. |
181 PermissionService* service = GetService(script_state->GetExecutionContext()); | 182 PermissionService* service = GetService(ExecutionContext::From(script_state)); |
182 if (!service) | 183 if (!service) |
183 return ScriptPromise::RejectWithDOMException( | 184 return ScriptPromise::RejectWithDOMException( |
184 script_state, DOMException::Create(kInvalidStateError, | 185 script_state, DOMException::Create(kInvalidStateError, |
185 "In its current state, the global " | 186 "In its current state, the global " |
186 "scope can't revoke permissions.")); | 187 "scope can't revoke permissions.")); |
187 | 188 |
188 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 189 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
189 ScriptPromise promise = resolver->Promise(); | 190 ScriptPromise promise = resolver->Promise(); |
190 | 191 |
191 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); | 192 PermissionDescriptorPtr descriptor_copy = descriptor->Clone(); |
192 service->RevokePermission( | 193 service->RevokePermission( |
193 std::move(descriptor), | 194 std::move(descriptor), |
194 script_state->GetExecutionContext()->GetSecurityOrigin(), | 195 ExecutionContext::From(script_state)->GetSecurityOrigin(), |
195 ConvertToBaseCallback(WTF::Bind( | 196 ConvertToBaseCallback(WTF::Bind( |
196 &Permissions::TaskComplete, WrapPersistent(this), | 197 &Permissions::TaskComplete, WrapPersistent(this), |
197 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); | 198 WrapPersistent(resolver), WTF::Passed(std::move(descriptor_copy))))); |
198 return promise; | 199 return promise; |
199 } | 200 } |
200 | 201 |
201 ScriptPromise Permissions::requestAll( | 202 ScriptPromise Permissions::requestAll( |
202 ScriptState* script_state, | 203 ScriptState* script_state, |
203 const Vector<Dictionary>& raw_permissions) { | 204 const Vector<Dictionary>& raw_permissions) { |
204 ExceptionState exception_state(script_state->GetIsolate(), | 205 ExceptionState exception_state(script_state->GetIsolate(), |
(...skipping 20 matching lines...) Expand all Loading... |
225 } | 226 } |
226 if (internal_index == kNotFound) { | 227 if (internal_index == kNotFound) { |
227 internal_index = internal_permissions.size(); | 228 internal_index = internal_permissions.size(); |
228 internal_permissions.push_back(std::move(descriptor)); | 229 internal_permissions.push_back(std::move(descriptor)); |
229 } | 230 } |
230 caller_index_to_internal_index[i] = internal_index; | 231 caller_index_to_internal_index[i] = internal_index; |
231 } | 232 } |
232 | 233 |
233 // This must be called after `parsePermission` because the website might | 234 // This must be called after `parsePermission` because the website might |
234 // be able to run code. | 235 // be able to run code. |
235 PermissionService* service = GetService(script_state->GetExecutionContext()); | 236 PermissionService* service = GetService(ExecutionContext::From(script_state)); |
236 if (!service) | 237 if (!service) |
237 return ScriptPromise::RejectWithDOMException( | 238 return ScriptPromise::RejectWithDOMException( |
238 script_state, DOMException::Create(kInvalidStateError, | 239 script_state, DOMException::Create(kInvalidStateError, |
239 "In its current state, the global " | 240 "In its current state, the global " |
240 "scope can't request permissions.")); | 241 "scope can't request permissions.")); |
241 | 242 |
242 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 243 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
243 ScriptPromise promise = resolver->Promise(); | 244 ScriptPromise promise = resolver->Promise(); |
244 | 245 |
245 Vector<PermissionDescriptorPtr> internal_permissions_copy; | 246 Vector<PermissionDescriptorPtr> internal_permissions_copy; |
246 internal_permissions_copy.ReserveCapacity(internal_permissions.size()); | 247 internal_permissions_copy.ReserveCapacity(internal_permissions.size()); |
247 for (const auto& descriptor : internal_permissions) | 248 for (const auto& descriptor : internal_permissions) |
248 internal_permissions_copy.push_back(descriptor->Clone()); | 249 internal_permissions_copy.push_back(descriptor->Clone()); |
249 | 250 |
250 service->RequestPermissions( | 251 service->RequestPermissions( |
251 std::move(internal_permissions), | 252 std::move(internal_permissions), |
252 script_state->GetExecutionContext()->GetSecurityOrigin(), | 253 ExecutionContext::From(script_state)->GetSecurityOrigin(), |
253 UserGestureIndicator::ProcessingUserGestureThreadSafe(), | 254 UserGestureIndicator::ProcessingUserGestureThreadSafe(), |
254 ConvertToBaseCallback( | 255 ConvertToBaseCallback( |
255 WTF::Bind(&Permissions::BatchTaskComplete, WrapPersistent(this), | 256 WTF::Bind(&Permissions::BatchTaskComplete, WrapPersistent(this), |
256 WrapPersistent(resolver), | 257 WrapPersistent(resolver), |
257 WTF::Passed(std::move(internal_permissions_copy)), | 258 WTF::Passed(std::move(internal_permissions_copy)), |
258 WTF::Passed(std::move(caller_index_to_internal_index))))); | 259 WTF::Passed(std::move(caller_index_to_internal_index))))); |
259 return promise; | 260 return promise; |
260 } | 261 } |
261 | 262 |
262 PermissionService* Permissions::GetService( | 263 PermissionService* Permissions::GetService( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 result.ReserveInitialCapacity(caller_index_to_internal_index.size()); | 299 result.ReserveInitialCapacity(caller_index_to_internal_index.size()); |
299 for (int internal_index : caller_index_to_internal_index) { | 300 for (int internal_index : caller_index_to_internal_index) { |
300 result.push_back(PermissionStatus::CreateAndListen( | 301 result.push_back(PermissionStatus::CreateAndListen( |
301 resolver->GetExecutionContext(), results[internal_index], | 302 resolver->GetExecutionContext(), results[internal_index], |
302 descriptors[internal_index]->Clone())); | 303 descriptors[internal_index]->Clone())); |
303 } | 304 } |
304 resolver->Resolve(result); | 305 resolver->Resolve(result); |
305 } | 306 } |
306 | 307 |
307 } // namespace blink | 308 } // namespace blink |
OLD | NEW |