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