| 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/serviceworkers/ServiceWorkerClients.h" | 5 #include "modules/serviceworkers/ServiceWorkerClients.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 9 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/dom/ExecutionContext.h" |
| 13 #include "core/workers/WorkerGlobalScope.h" | 14 #include "core/workers/WorkerGlobalScope.h" |
| 14 #include "core/workers/WorkerLocation.h" | 15 #include "core/workers/WorkerLocation.h" |
| 15 #include "modules/serviceworkers/ServiceWorkerError.h" | 16 #include "modules/serviceworkers/ServiceWorkerError.h" |
| 16 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 17 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 17 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" | 18 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
| 18 #include "modules/serviceworkers/ServiceWorkerWindowClientCallback.h" | 19 #include "modules/serviceworkers/ServiceWorkerWindowClientCallback.h" |
| 19 #include "platform/wtf/PtrUtil.h" | 20 #include "platform/wtf/PtrUtil.h" |
| 20 #include "platform/wtf/RefPtr.h" | 21 #include "platform/wtf/RefPtr.h" |
| 21 #include "platform/wtf/Vector.h" | 22 #include "platform/wtf/Vector.h" |
| 22 #include "public/platform/modules/serviceworker/WebServiceWorkerClientQueryOptio
ns.h" | 23 #include "public/platform/modules/serviceworker/WebServiceWorkerClientQueryOptio
ns.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } // namespace | 97 } // namespace |
| 97 | 98 |
| 98 ServiceWorkerClients* ServiceWorkerClients::Create() { | 99 ServiceWorkerClients* ServiceWorkerClients::Create() { |
| 99 return new ServiceWorkerClients(); | 100 return new ServiceWorkerClients(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 ServiceWorkerClients::ServiceWorkerClients() {} | 103 ServiceWorkerClients::ServiceWorkerClients() {} |
| 103 | 104 |
| 104 ScriptPromise ServiceWorkerClients::get(ScriptState* script_state, | 105 ScriptPromise ServiceWorkerClients::get(ScriptState* script_state, |
| 105 const String& id) { | 106 const String& id) { |
| 106 ExecutionContext* execution_context = script_state->GetExecutionContext(); | 107 ExecutionContext* execution_context = ExecutionContext::From(script_state); |
| 107 // TODO(jungkees): May be null due to worker termination: | 108 // TODO(jungkees): May be null due to worker termination: |
| 108 // http://crbug.com/413518. | 109 // http://crbug.com/413518. |
| 109 if (!execution_context) | 110 if (!execution_context) |
| 110 return ScriptPromise(); | 111 return ScriptPromise(); |
| 111 | 112 |
| 112 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 113 ScriptPromise promise = resolver->Promise(); | 114 ScriptPromise promise = resolver->Promise(); |
| 114 | 115 |
| 115 ServiceWorkerGlobalScopeClient::From(execution_context) | 116 ServiceWorkerGlobalScopeClient::From(execution_context) |
| 116 ->GetClient(id, WTF::MakeUnique<GetCallback>(resolver)); | 117 ->GetClient(id, WTF::MakeUnique<GetCallback>(resolver)); |
| 117 return promise; | 118 return promise; |
| 118 } | 119 } |
| 119 | 120 |
| 120 ScriptPromise ServiceWorkerClients::matchAll( | 121 ScriptPromise ServiceWorkerClients::matchAll( |
| 121 ScriptState* script_state, | 122 ScriptState* script_state, |
| 122 const ClientQueryOptions& options) { | 123 const ClientQueryOptions& options) { |
| 123 ExecutionContext* execution_context = script_state->GetExecutionContext(); | 124 ExecutionContext* execution_context = ExecutionContext::From(script_state); |
| 124 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 125 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
| 125 if (!execution_context) | 126 if (!execution_context) |
| 126 return ScriptPromise(); | 127 return ScriptPromise(); |
| 127 | 128 |
| 128 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 129 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 129 ScriptPromise promise = resolver->Promise(); | 130 ScriptPromise promise = resolver->Promise(); |
| 130 | 131 |
| 131 WebServiceWorkerClientQueryOptions web_options; | 132 WebServiceWorkerClientQueryOptions web_options; |
| 132 web_options.client_type = GetClientType(options.type()); | 133 web_options.client_type = GetClientType(options.type()); |
| 133 web_options.include_uncontrolled = options.includeUncontrolled(); | 134 web_options.include_uncontrolled = options.includeUncontrolled(); |
| 134 ServiceWorkerGlobalScopeClient::From(execution_context) | 135 ServiceWorkerGlobalScopeClient::From(execution_context) |
| 135 ->GetClients(web_options, | 136 ->GetClients(web_options, |
| 136 WTF::MakeUnique< | 137 WTF::MakeUnique< |
| 137 CallbackPromiseAdapter<ClientArray, ServiceWorkerError>>( | 138 CallbackPromiseAdapter<ClientArray, ServiceWorkerError>>( |
| 138 resolver)); | 139 resolver)); |
| 139 return promise; | 140 return promise; |
| 140 } | 141 } |
| 141 | 142 |
| 142 ScriptPromise ServiceWorkerClients::claim(ScriptState* script_state) { | 143 ScriptPromise ServiceWorkerClients::claim(ScriptState* script_state) { |
| 143 ExecutionContext* execution_context = script_state->GetExecutionContext(); | 144 ExecutionContext* execution_context = ExecutionContext::From(script_state); |
| 144 | 145 |
| 145 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 146 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
| 146 if (!execution_context) | 147 if (!execution_context) |
| 147 return ScriptPromise(); | 148 return ScriptPromise(); |
| 148 | 149 |
| 149 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 150 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 150 ScriptPromise promise = resolver->Promise(); | 151 ScriptPromise promise = resolver->Promise(); |
| 151 | 152 |
| 152 auto callbacks = | 153 auto callbacks = |
| 153 WTF::MakeUnique<CallbackPromiseAdapter<void, ServiceWorkerError>>( | 154 WTF::MakeUnique<CallbackPromiseAdapter<void, ServiceWorkerError>>( |
| 154 resolver); | 155 resolver); |
| 155 ServiceWorkerGlobalScopeClient::From(execution_context) | 156 ServiceWorkerGlobalScopeClient::From(execution_context) |
| 156 ->Claim(std::move(callbacks)); | 157 ->Claim(std::move(callbacks)); |
| 157 return promise; | 158 return promise; |
| 158 } | 159 } |
| 159 | 160 |
| 160 ScriptPromise ServiceWorkerClients::openWindow(ScriptState* script_state, | 161 ScriptPromise ServiceWorkerClients::openWindow(ScriptState* script_state, |
| 161 const String& url) { | 162 const String& url) { |
| 162 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 163 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 163 ScriptPromise promise = resolver->Promise(); | 164 ScriptPromise promise = resolver->Promise(); |
| 164 ExecutionContext* context = script_state->GetExecutionContext(); | 165 ExecutionContext* context = ExecutionContext::From(script_state); |
| 165 | 166 |
| 166 KURL parsed_url = KURL(ToWorkerGlobalScope(context)->location()->Url(), url); | 167 KURL parsed_url = KURL(ToWorkerGlobalScope(context)->location()->Url(), url); |
| 167 if (!parsed_url.IsValid()) { | 168 if (!parsed_url.IsValid()) { |
| 168 resolver->Reject(V8ThrowException::CreateTypeError( | 169 resolver->Reject(V8ThrowException::CreateTypeError( |
| 169 script_state->GetIsolate(), "'" + url + "' is not a valid URL.")); | 170 script_state->GetIsolate(), "'" + url + "' is not a valid URL.")); |
| 170 return promise; | 171 return promise; |
| 171 } | 172 } |
| 172 | 173 |
| 173 if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) { | 174 if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) { |
| 174 resolver->Reject(V8ThrowException::CreateTypeError( | 175 resolver->Reject(V8ThrowException::CreateTypeError( |
| 175 script_state->GetIsolate(), | 176 script_state->GetIsolate(), |
| 176 "'" + parsed_url.ElidedString() + "' cannot be opened.")); | 177 "'" + parsed_url.ElidedString() + "' cannot be opened.")); |
| 177 return promise; | 178 return promise; |
| 178 } | 179 } |
| 179 | 180 |
| 180 if (!context->IsWindowInteractionAllowed()) { | 181 if (!context->IsWindowInteractionAllowed()) { |
| 181 resolver->Reject(DOMException::Create(kInvalidAccessError, | 182 resolver->Reject(DOMException::Create(kInvalidAccessError, |
| 182 "Not allowed to open a window.")); | 183 "Not allowed to open a window.")); |
| 183 return promise; | 184 return promise; |
| 184 } | 185 } |
| 185 context->ConsumeWindowInteraction(); | 186 context->ConsumeWindowInteraction(); |
| 186 | 187 |
| 187 ServiceWorkerGlobalScopeClient::From(context)->OpenWindow( | 188 ServiceWorkerGlobalScopeClient::From(context)->OpenWindow( |
| 188 parsed_url, WTF::MakeUnique<NavigateClientCallback>(resolver)); | 189 parsed_url, WTF::MakeUnique<NavigateClientCallback>(resolver)); |
| 189 return promise; | 190 return promise; |
| 190 } | 191 } |
| 191 | 192 |
| 192 } // namespace blink | 193 } // namespace blink |
| OLD | NEW |