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 "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 class GetCallback : public WebServiceWorkerClientCallbacks { | 63 class GetCallback : public WebServiceWorkerClientCallbacks { |
64 public: | 64 public: |
65 explicit GetCallback(ScriptPromiseResolver* resolver) | 65 explicit GetCallback(ScriptPromiseResolver* resolver) |
66 : m_resolver(resolver) { } | 66 : m_resolver(resolver) { } |
67 ~GetCallback() override { } | 67 ~GetCallback() override { } |
68 | 68 |
69 void onSuccess(WebPassOwnPtr<WebServiceWorkerClientInfo> webClient) override | 69 void onSuccess(WebPassOwnPtr<WebServiceWorkerClientInfo> webClient) override |
70 { | 70 { |
71 OwnPtr<WebServiceWorkerClientInfo> client = webClient.release(); | 71 OwnPtr<WebServiceWorkerClientInfo> client = webClient.release(); |
72 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) | 72 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
73 return; | 73 return; |
74 if (!client) { | 74 if (!client) { |
75 // Resolve the promise with undefined. | 75 // Resolve the promise with undefined. |
76 m_resolver->resolve(); | 76 m_resolver->resolve(); |
77 return; | 77 return; |
78 } | 78 } |
79 m_resolver->resolve(ServiceWorkerClient::take(m_resolver, client.release
())); | 79 m_resolver->resolve(ServiceWorkerClient::take(m_resolver, client.release
())); |
80 } | 80 } |
81 | 81 |
82 void onError(const WebServiceWorkerError& error) override | 82 void onError(const WebServiceWorkerError& error) override |
83 { | 83 { |
84 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) | 84 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
85 return; | 85 return; |
86 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error)); | 86 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error)); |
87 } | 87 } |
88 | 88 |
89 private: | 89 private: |
90 Persistent<ScriptPromiseResolver> m_resolver; | 90 Persistent<ScriptPromiseResolver> m_resolver; |
91 WTF_MAKE_NONCOPYABLE(GetCallback); | 91 WTF_MAKE_NONCOPYABLE(GetCallback); |
92 }; | 92 }; |
93 | 93 |
94 } // namespace | 94 } // namespace |
95 | 95 |
96 ServiceWorkerClients* ServiceWorkerClients::create() | 96 ServiceWorkerClients* ServiceWorkerClients::create() |
97 { | 97 { |
98 return new ServiceWorkerClients(); | 98 return new ServiceWorkerClients(); |
99 } | 99 } |
100 | 100 |
101 ServiceWorkerClients::ServiceWorkerClients() | 101 ServiceWorkerClients::ServiceWorkerClients() |
102 { | 102 { |
103 } | 103 } |
104 | 104 |
105 ScriptPromise ServiceWorkerClients::get(ScriptState* scriptState, const String&
id) | 105 ScriptPromise ServiceWorkerClients::get(ScriptState* scriptState, const String&
id) |
106 { | 106 { |
107 ExecutionContext* executionContext = scriptState->executionContext(); | 107 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
108 // TODO(jungkees): May be null due to worker termination: http://crbug.com/4
13518. | 108 // TODO(jungkees): May be null due to worker termination: http://crbug.com/4
13518. |
109 if (!executionContext) | 109 if (!executionContext) |
110 return ScriptPromise(); | 110 return ScriptPromise(); |
111 | 111 |
112 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 112 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
113 ScriptPromise promise = resolver->promise(); | 113 ScriptPromise promise = resolver->promise(); |
114 | 114 |
115 ServiceWorkerGlobalScopeClient::from(executionContext)->getClient(id, new Ge
tCallback(resolver)); | 115 ServiceWorkerGlobalScopeClient::from(executionContext)->getClient(id, new Ge
tCallback(resolver)); |
116 return promise; | 116 return promise; |
117 } | 117 } |
118 | 118 |
119 ScriptPromise ServiceWorkerClients::matchAll(ScriptState* scriptState, const Cli
entQueryOptions& options) | 119 ScriptPromise ServiceWorkerClients::matchAll(ScriptState* scriptState, const Cli
entQueryOptions& options) |
120 { | 120 { |
121 ExecutionContext* executionContext = scriptState->executionContext(); | 121 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
122 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 122 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
123 if (!executionContext) | 123 if (!executionContext) |
124 return ScriptPromise(); | 124 return ScriptPromise(); |
125 | 125 |
126 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 126 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
127 ScriptPromise promise = resolver->promise(); | 127 ScriptPromise promise = resolver->promise(); |
128 | 128 |
129 WebServiceWorkerClientQueryOptions webOptions; | 129 WebServiceWorkerClientQueryOptions webOptions; |
130 webOptions.clientType = getClientType(options.type()); | 130 webOptions.clientType = getClientType(options.type()); |
131 webOptions.includeUncontrolled = options.includeUncontrolled(); | 131 webOptions.includeUncontrolled = options.includeUncontrolled(); |
132 ServiceWorkerGlobalScopeClient::from(executionContext)->getClients(webOption
s, new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); | 132 ServiceWorkerGlobalScopeClient::from(executionContext)->getClients(webOption
s, new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); |
133 return promise; | 133 return promise; |
134 } | 134 } |
135 | 135 |
136 ScriptPromise ServiceWorkerClients::claim(ScriptState* scriptState) | 136 ScriptPromise ServiceWorkerClients::claim(ScriptState* scriptState) |
137 { | 137 { |
138 ExecutionContext* executionContext = scriptState->executionContext(); | 138 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
139 | 139 |
140 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 140 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
141 if (!executionContext) | 141 if (!executionContext) |
142 return ScriptPromise(); | 142 return ScriptPromise(); |
143 | 143 |
144 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 144 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
145 ScriptPromise promise = resolver->promise(); | 145 ScriptPromise promise = resolver->promise(); |
146 | 146 |
147 WebServiceWorkerClientsClaimCallbacks* callbacks = new CallbackPromiseAdapte
r<void, ServiceWorkerError>(resolver); | 147 WebServiceWorkerClientsClaimCallbacks* callbacks = new CallbackPromiseAdapte
r<void, ServiceWorkerError>(resolver); |
148 ServiceWorkerGlobalScopeClient::from(executionContext)->claim(callbacks); | 148 ServiceWorkerGlobalScopeClient::from(executionContext)->claim(callbacks); |
149 return promise; | 149 return promise; |
150 } | 150 } |
151 | 151 |
152 ScriptPromise ServiceWorkerClients::openWindow(ScriptState* scriptState, const S
tring& url) | 152 ScriptPromise ServiceWorkerClients::openWindow(ScriptState* scriptState, const S
tring& url) |
153 { | 153 { |
154 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 154 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
155 ScriptPromise promise = resolver->promise(); | 155 ScriptPromise promise = resolver->promise(); |
156 ExecutionContext* context = scriptState->executionContext(); | 156 ExecutionContext* context = scriptState->getExecutionContext(); |
157 | 157 |
158 KURL parsedUrl = KURL(toWorkerGlobalScope(context)->location()->url(), url); | 158 KURL parsedUrl = KURL(toWorkerGlobalScope(context)->location()->url(), url); |
159 if (!parsedUrl.isValid()) { | 159 if (!parsedUrl.isValid()) { |
160 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(
), "'" + url + "' is not a valid URL.")); | 160 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(
), "'" + url + "' is not a valid URL.")); |
161 return promise; | 161 return promise; |
162 } | 162 } |
163 | 163 |
164 if (!context->securityOrigin()->canDisplay(parsedUrl)) { | 164 if (!context->getSecurityOrigin()->canDisplay(parsedUrl)) { |
165 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(
), "'" + parsedUrl.elidedString() + "' cannot be opened.")); | 165 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(
), "'" + parsedUrl.elidedString() + "' cannot be opened.")); |
166 return promise; | 166 return promise; |
167 } | 167 } |
168 | 168 |
169 if (!context->isWindowInteractionAllowed()) { | 169 if (!context->isWindowInteractionAllowed()) { |
170 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t
o open a window.")); | 170 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t
o open a window.")); |
171 return promise; | 171 return promise; |
172 } | 172 } |
173 context->consumeWindowInteraction(); | 173 context->consumeWindowInteraction(); |
174 | 174 |
175 ServiceWorkerGlobalScopeClient::from(context)->openWindow(parsedUrl, new Nav
igateClientCallback(resolver)); | 175 ServiceWorkerGlobalScopeClient::from(context)->openWindow(parsedUrl, new Nav
igateClientCallback(resolver)); |
176 return promise; | 176 return promise; |
177 } | 177 } |
178 | 178 |
179 } // namespace blink | 179 } // namespace blink |
OLD | NEW |