| 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/ServiceWorkerWindowClient.h" | 5 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/dom/ExecutionContext.h" |
| 12 #include "core/page/PageVisibilityState.h" | 13 #include "core/page/PageVisibilityState.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/ServiceWorkerWindowClientCallback.h" | 18 #include "modules/serviceworkers/ServiceWorkerWindowClientCallback.h" |
| 18 #include "platform/wtf/RefPtr.h" | 19 #include "platform/wtf/RefPtr.h" |
| 19 #include "public/platform/WebString.h" | 20 #include "public/platform/WebString.h" |
| 20 | 21 |
| 21 namespace blink { | 22 namespace blink { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 String ServiceWorkerWindowClient::visibilityState() const { | 43 String ServiceWorkerWindowClient::visibilityState() const { |
| 43 return PageVisibilityStateString( | 44 return PageVisibilityStateString( |
| 44 static_cast<PageVisibilityState>(page_visibility_state_)); | 45 static_cast<PageVisibilityState>(page_visibility_state_)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 ScriptPromise ServiceWorkerWindowClient::focus(ScriptState* script_state) { | 48 ScriptPromise ServiceWorkerWindowClient::focus(ScriptState* script_state) { |
| 48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 49 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 49 ScriptPromise promise = resolver->Promise(); | 50 ScriptPromise promise = resolver->Promise(); |
| 50 | 51 |
| 51 if (!script_state->GetExecutionContext()->IsWindowInteractionAllowed()) { | 52 if (!ExecutionContext::From(script_state)->IsWindowInteractionAllowed()) { |
| 52 resolver->Reject(DOMException::Create(kInvalidAccessError, | 53 resolver->Reject(DOMException::Create(kInvalidAccessError, |
| 53 "Not allowed to focus a window.")); | 54 "Not allowed to focus a window.")); |
| 54 return promise; | 55 return promise; |
| 55 } | 56 } |
| 56 script_state->GetExecutionContext()->ConsumeWindowInteraction(); | 57 ExecutionContext::From(script_state)->ConsumeWindowInteraction(); |
| 57 | 58 |
| 58 ServiceWorkerGlobalScopeClient::From(script_state->GetExecutionContext()) | 59 ServiceWorkerGlobalScopeClient::From(ExecutionContext::From(script_state)) |
| 59 ->Focus(Uuid(), | 60 ->Focus(Uuid(), |
| 60 WTF::MakeUnique<CallbackPromiseAdapter<ServiceWorkerWindowClient, | 61 WTF::MakeUnique<CallbackPromiseAdapter<ServiceWorkerWindowClient, |
| 61 ServiceWorkerError>>( | 62 ServiceWorkerError>>( |
| 62 resolver)); | 63 resolver)); |
| 63 return promise; | 64 return promise; |
| 64 } | 65 } |
| 65 | 66 |
| 66 ScriptPromise ServiceWorkerWindowClient::navigate(ScriptState* script_state, | 67 ScriptPromise ServiceWorkerWindowClient::navigate(ScriptState* script_state, |
| 67 const String& url) { | 68 const String& url) { |
| 68 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 69 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 69 ScriptPromise promise = resolver->Promise(); | 70 ScriptPromise promise = resolver->Promise(); |
| 70 ExecutionContext* context = script_state->GetExecutionContext(); | 71 ExecutionContext* context = ExecutionContext::From(script_state); |
| 71 | 72 |
| 72 KURL parsed_url = KURL(ToWorkerGlobalScope(context)->location()->Url(), url); | 73 KURL parsed_url = KURL(ToWorkerGlobalScope(context)->location()->Url(), url); |
| 73 if (!parsed_url.IsValid() || parsed_url.ProtocolIsAbout()) { | 74 if (!parsed_url.IsValid() || parsed_url.ProtocolIsAbout()) { |
| 74 resolver->Reject(V8ThrowException::CreateTypeError( | 75 resolver->Reject(V8ThrowException::CreateTypeError( |
| 75 script_state->GetIsolate(), "'" + url + "' is not a valid URL.")); | 76 script_state->GetIsolate(), "'" + url + "' is not a valid URL.")); |
| 76 return promise; | 77 return promise; |
| 77 } | 78 } |
| 78 if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) { | 79 if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) { |
| 79 resolver->Reject(V8ThrowException::CreateTypeError( | 80 resolver->Reject(V8ThrowException::CreateTypeError( |
| 80 script_state->GetIsolate(), | 81 script_state->GetIsolate(), |
| 81 "'" + parsed_url.ElidedString() + "' cannot navigate.")); | 82 "'" + parsed_url.ElidedString() + "' cannot navigate.")); |
| 82 return promise; | 83 return promise; |
| 83 } | 84 } |
| 84 | 85 |
| 85 ServiceWorkerGlobalScopeClient::From(context)->Navigate( | 86 ServiceWorkerGlobalScopeClient::From(context)->Navigate( |
| 86 Uuid(), parsed_url, WTF::MakeUnique<NavigateClientCallback>(resolver)); | 87 Uuid(), parsed_url, WTF::MakeUnique<NavigateClientCallback>(resolver)); |
| 87 return promise; | 88 return promise; |
| 88 } | 89 } |
| 89 | 90 |
| 90 DEFINE_TRACE(ServiceWorkerWindowClient) { | 91 DEFINE_TRACE(ServiceWorkerWindowClient) { |
| 91 ServiceWorkerClient::Trace(visitor); | 92 ServiceWorkerClient::Trace(visitor); |
| 92 } | 93 } |
| 93 | 94 |
| 94 } // namespace blink | 95 } // namespace blink |
| OLD | NEW |