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 "config.h" | 5 #include "config.h" |
6 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" | 6 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
7 | 7 |
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/page/PageVisibilityState.h" | 12 #include "core/page/PageVisibilityState.h" |
13 #include "core/workers/WorkerGlobalScope.h" | 13 #include "core/workers/WorkerGlobalScope.h" |
14 #include "core/workers/WorkerLocation.h" | 14 #include "core/workers/WorkerLocation.h" |
15 #include "modules/serviceworkers/ServiceWorkerError.h" | 15 #include "modules/serviceworkers/ServiceWorkerError.h" |
16 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 16 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
17 #include "public/platform/WebString.h" | 17 #include "public/platform/WebString.h" |
18 #include "public/platform/modules/serviceworker/WebServiceWorkerClientsInfo.h" | |
18 #include "wtf/RefPtr.h" | 19 #include "wtf/RefPtr.h" |
19 | 20 |
20 namespace blink { | 21 namespace blink { |
21 | 22 |
22 ServiceWorkerWindowClient* ServiceWorkerWindowClient::take(ScriptPromiseResolver *, PassOwnPtr<WebServiceWorkerClientInfo> webClient) | 23 ServiceWorkerWindowClient* ServiceWorkerWindowClient::take(ScriptPromiseResolver *, PassOwnPtr<WebServiceWorkerClientInfo> webClient) |
23 { | 24 { |
24 return webClient ? ServiceWorkerWindowClient::create(*webClient) : nullptr; | 25 return webClient ? ServiceWorkerWindowClient::create(*webClient) : nullptr; |
25 } | 26 } |
26 | 27 |
27 ServiceWorkerWindowClient* ServiceWorkerWindowClient::create(const WebServiceWor kerClientInfo& info) | 28 ServiceWorkerWindowClient* ServiceWorkerWindowClient::create(const WebServiceWor kerClientInfo& info) |
(...skipping 25 matching lines...) Expand all Loading... | |
53 if (!scriptState->executionContext()->isWindowInteractionAllowed()) { | 54 if (!scriptState->executionContext()->isWindowInteractionAllowed()) { |
54 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t o focus a window.")); | 55 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t o focus a window.")); |
55 return promise; | 56 return promise; |
56 } | 57 } |
57 scriptState->executionContext()->consumeWindowInteraction(); | 58 scriptState->executionContext()->consumeWindowInteraction(); |
58 | 59 |
59 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->focus (uuid(), new CallbackPromiseAdapter<ServiceWorkerWindowClient, ServiceWorkerErro r>(resolver)); | 60 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->focus (uuid(), new CallbackPromiseAdapter<ServiceWorkerWindowClient, ServiceWorkerErro r>(resolver)); |
60 return promise; | 61 return promise; |
61 } | 62 } |
62 | 63 |
64 class NavigateClientCallback : public WebServiceWorkerClientCallbacks { | |
nhiroki
2015/11/27 07:52:01
nit: can you move this class definition to line 22
zino
2015/11/30 05:57:30
Done.
| |
65 public: | |
66 explicit NavigateClientCallback(ScriptPromiseResolver* resolver) | |
67 : m_resolver(resolver) { } | |
68 | |
69 void onSuccess(WebPassOwnPtr<WebServiceWorkerClientInfo> clientInfo) overrid e | |
70 { | |
71 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) | |
72 return; | |
73 m_resolver->resolve(ServiceWorkerWindowClient::take(m_resolver.get(), cl ientInfo.release())); | |
74 } | |
75 | |
76 void onError(const WebServiceWorkerError& error) override | |
77 { | |
78 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) | |
79 return; | |
80 | |
81 if (error.errorType == WebServiceWorkerError::ErrorTypeNavigation) { | |
82 ScriptState::Scope scope(m_resolver->scriptState()); | |
83 m_resolver->reject(V8ThrowException::createTypeError(m_resolver->scr iptState()->isolate(), error.message)); | |
84 return; | |
85 } | |
86 | |
87 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error)); | |
88 } | |
89 private: | |
nhiroki
2015/11/27 07:52:01
nit: can you add a blank line before "private:"?
zino
2015/11/30 05:57:30
Done.
| |
90 Persistent<ScriptPromiseResolver> m_resolver; | |
91 WTF_MAKE_NONCOPYABLE(NavigateClientCallback); | |
92 }; | |
93 | |
63 ScriptPromise ServiceWorkerWindowClient::navigate(ScriptState* scriptState, cons t String& url) | 94 ScriptPromise ServiceWorkerWindowClient::navigate(ScriptState* scriptState, cons t String& url) |
64 { | 95 { |
65 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 96 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
66 ScriptPromise promise = resolver->promise(); | 97 ScriptPromise promise = resolver->promise(); |
67 ExecutionContext* context = scriptState->executionContext(); | 98 ExecutionContext* context = scriptState->executionContext(); |
68 | 99 |
69 KURL parsedUrl = KURL(toWorkerGlobalScope(context)->location()->url(), url); | 100 KURL parsedUrl = KURL(toWorkerGlobalScope(context)->location()->url(), url); |
70 if (!parsedUrl.isValid() || parsedUrl.protocolIsAbout()) { | 101 if (!parsedUrl.isValid() || parsedUrl.protocolIsAbout()) { |
71 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate( ), "'" + url + "' is not a valid URL.")); | 102 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate( ), "'" + url + "' is not a valid URL.")); |
72 return promise; | 103 return promise; |
73 } | 104 } |
74 if (!context->securityOrigin()->canDisplay(parsedUrl)) { | 105 if (!context->securityOrigin()->canDisplay(parsedUrl)) { |
75 resolver->reject(DOMException::create(SecurityError, "'" + parsedUrl.eli dedString() + "' cannot navigate.")); | 106 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate( ), "'" + parsedUrl.elidedString() + "' cannot navigate.")); |
76 return promise; | 107 return promise; |
77 } | 108 } |
78 | 109 |
79 ServiceWorkerGlobalScopeClient::from(context)->navigate(uuid(), parsedUrl, n ew CallbackPromiseAdapter<ServiceWorkerWindowClient, ServiceWorkerError>(resolve r)); | 110 ServiceWorkerGlobalScopeClient::from(context)->navigate(uuid(), parsedUrl, n ew NavigateClientCallback(resolver)); |
80 return promise; | 111 return promise; |
81 } | 112 } |
82 | 113 |
83 DEFINE_TRACE(ServiceWorkerWindowClient) | 114 DEFINE_TRACE(ServiceWorkerWindowClient) |
84 { | 115 { |
85 ServiceWorkerClient::trace(visitor); | 116 ServiceWorkerClient::trace(visitor); |
86 } | 117 } |
87 | 118 |
88 } // namespace blink | 119 } // namespace blink |
OLD | NEW |