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