Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / ServiceWorkerContainer.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "config.h"
31 #include "modules/serviceworkers/ServiceWorkerContainer.h"
32
33 #include "bindings/core/v8/CallbackPromiseAdapter.h"
34 #include "bindings/core/v8/ScriptPromise.h"
35 #include "bindings/core/v8/ScriptPromiseResolver.h"
36 #include "bindings/core/v8/ScriptState.h"
37 #include "bindings/core/v8/SerializedScriptValue.h"
38 #include "core/dom/DOMException.h"
39 #include "core/dom/ExceptionCode.h"
40 #include "core/dom/ExecutionContext.h"
41 #include "core/dom/MessagePort.h"
42 #include "core/events/MessageEvent.h"
43 #include "modules/serviceworkers/RegistrationOptionList.h"
44 #include "modules/serviceworkers/ServiceWorker.h"
45 #include "modules/serviceworkers/ServiceWorkerContainerClient.h"
46 #include "modules/serviceworkers/ServiceWorkerError.h"
47 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
48 #include "platform/RuntimeEnabledFeatures.h"
49 #include "public/platform/WebServiceWorker.h"
50 #include "public/platform/WebServiceWorkerProvider.h"
51 #include "public/platform/WebString.h"
52 #include "public/platform/WebURL.h"
53
54 using blink::WebServiceWorker;
55 using blink::WebServiceWorkerProvider;
56
57 namespace blink {
58
59 PassRefPtrWillBeRawPtr<ServiceWorkerContainer> ServiceWorkerContainer::create(ExecutionContext* executionContext)
60 {
61     return adoptRefWillBeNoop(new ServiceWorkerContainer(executionContext));
62 }
63
64 ServiceWorkerContainer::~ServiceWorkerContainer()
65 {
66     ASSERT(!m_provider);
67 }
68
69 void ServiceWorkerContainer::willBeDetachedFromFrame()
70 {
71     if (m_provider) {
72         m_provider->setClient(0);
73         m_provider = nullptr;
74     }
75 }
76
77 void ServiceWorkerContainer::trace(Visitor* visitor)
78 {
79     visitor->trace(m_active);
80     visitor->trace(m_controller);
81     visitor->trace(m_installing);
82     visitor->trace(m_waiting);
83     visitor->trace(m_ready);
84 }
85
86 ScriptPromise ServiceWorkerContainer::registerServiceWorker(ScriptState* scriptState, const String& url, const Dictionary& dictionary)
87 {
88     RegistrationOptionList options(dictionary);
89     ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
90     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
91     ScriptPromise promise = resolver->promise();
92
93     if (!m_provider) {
94         resolver->reject(DOMException::create(InvalidStateError, "No associated provider is available"));
95         return promise;
96     }
97
98     // FIXME: This should use the container's execution context, not
99     // the callers.
100     ExecutionContext* executionContext = scriptState->executionContext();
101     RefPtr<SecurityOrigin> documentOrigin = executionContext->securityOrigin();
102     String errorMessage;
103     if (!documentOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) {
104         resolver->reject(DOMException::create(NotSupportedError, errorMessage));
105         return promise;
106     }
107
108     KURL patternURL = executionContext->completeURL(options.scope);
109     patternURL.removeFragmentIdentifier();
110     if (!documentOrigin->canRequest(patternURL)) {
111         resolver->reject(DOMException::create(SecurityError, "The scope must match the current origin."));
112         return promise;
113     }
114
115     KURL scriptURL = executionContext->completeURL(url);
116     scriptURL.removeFragmentIdentifier();
117     if (!documentOrigin->canRequest(scriptURL)) {
118         resolver->reject(DOMException::create(SecurityError, "The origin of the script must match the current origin."));
119         return promise;
120     }
121
122     m_provider->registerServiceWorker(patternURL, scriptURL, new CallbackPromiseAdapter<ServiceWorkerRegistration, ServiceWorkerError>(resolver));
123
124     return promise;
125 }
126
127 class UndefinedValue {
128 public:
129     typedef WebServiceWorkerRegistration WebType;
130     static V8UndefinedType take(ScriptPromiseResolver* resolver, WebType* registration)
131     {
132         ASSERT(!registration); // Anything passed here will be leaked.
133         return V8UndefinedType();
134     }
135     static void dispose(WebType* registration)
136     {
137         ASSERT(!registration); // Anything passed here will be leaked.
138     }
139
140 private:
141     UndefinedValue();
142 };
143
144 ScriptPromise ServiceWorkerContainer::unregisterServiceWorker(ScriptState* scriptState, const String& pattern)
145 {
146     ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
147     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
148     ScriptPromise promise = resolver->promise();
149
150     if (!m_provider) {
151         resolver->reject(DOMException::create(InvalidStateError, "No associated provider is available"));
152         return promise;
153     }
154
155     // FIXME: This should use the container's execution context, not
156     // the callers.
157     RefPtr<SecurityOrigin> documentOrigin = scriptState->executionContext()->securityOrigin();
158     String errorMessage;
159     if (!documentOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) {
160         resolver->reject(DOMException::create(NotSupportedError, errorMessage));
161         return promise;
162     }
163
164     KURL patternURL = scriptState->executionContext()->completeURL(pattern);
165     patternURL.removeFragmentIdentifier();
166     if (!pattern.isEmpty() && !documentOrigin->canRequest(patternURL)) {
167         resolver->reject(DOMException::create(SecurityError, "The scope must match the current origin."));
168         return promise;
169     }
170
171     m_provider->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<UndefinedValue, ServiceWorkerError>(resolver));
172     return promise;
173 }
174
175 ServiceWorkerContainer::ReadyProperty* ServiceWorkerContainer::createReadyProperty()
176 {
177     return new ReadyProperty(executionContext(), this, ReadyProperty::Ready);
178 }
179
180 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState)
181 {
182     if (!executionContext())
183         return ScriptPromise();
184
185     if (!callerState->world().isMainWorld()) {
186         // FIXME: Support .ready from isolated worlds when
187         // ScriptPromiseProperty can vend Promises in isolated worlds.
188         return ScriptPromise::rejectWithDOMException(callerState, DOMException::create(NotSupportedError, "'ready' is only supported in pages."));
189     }
190
191     return m_ready->promise(callerState->world());
192 }
193
194 // If the WebServiceWorker is up for adoption (does not have a
195 // WebServiceWorkerProxy owner), rejects the adoption by deleting the
196 // WebServiceWorker.
197 static void deleteIfNoExistingOwner(WebServiceWorker* serviceWorker)
198 {
199     if (serviceWorker && !serviceWorker->proxy())
200         delete serviceWorker;
201 }
202
203 void ServiceWorkerContainer::setActive(WebServiceWorker* serviceWorker)
204 {
205     if (!executionContext()) {
206         deleteIfNoExistingOwner(serviceWorker);
207         return;
208     }
209     RefPtrWillBeRawPtr<ServiceWorker> previousReadyWorker = m_active;
210     m_active = ServiceWorker::from(executionContext(), serviceWorker);
211     checkReadyChanged(previousReadyWorker.release());
212 }
213
214 void ServiceWorkerContainer::checkReadyChanged(PassRefPtrWillBeRawPtr<ServiceWorker> previousReadyWorker)
215 {
216     ServiceWorker* currentReadyWorker = m_active.get();
217
218     if (previousReadyWorker == currentReadyWorker)
219         return;
220
221     if (m_ready->state() != ReadyProperty::Pending) {
222         // Already resolved Promises are now stale because the
223         // ready worker changed
224         m_ready = createReadyProperty();
225     }
226
227     if (currentReadyWorker)
228         m_ready->resolve(currentReadyWorker);
229 }
230
231 void ServiceWorkerContainer::setController(WebServiceWorker* serviceWorker)
232 {
233     if (!executionContext()) {
234         deleteIfNoExistingOwner(serviceWorker);
235         return;
236     }
237     m_controller = ServiceWorker::from(executionContext(), serviceWorker);
238 }
239
240 void ServiceWorkerContainer::setInstalling(WebServiceWorker* serviceWorker)
241 {
242     if (!executionContext()) {
243         deleteIfNoExistingOwner(serviceWorker);
244         return;
245     }
246     m_installing = ServiceWorker::from(executionContext(), serviceWorker);
247 }
248
249 void ServiceWorkerContainer::setWaiting(WebServiceWorker* serviceWorker)
250 {
251     if (!executionContext()) {
252         deleteIfNoExistingOwner(serviceWorker);
253         return;
254     }
255     m_waiting = ServiceWorker::from(executionContext(), serviceWorker);
256 }
257
258 void ServiceWorkerContainer::dispatchMessageEvent(const WebString& message, const WebMessagePortChannelArray& webChannels)
259 {
260     if (!executionContext() || !executionContext()->executingWindow())
261         return;
262
263     OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::toMessagePortArray(executionContext(), webChannels);
264     RefPtr<SerializedScriptValue> value = SerializedScriptValue::createFromWire(message);
265     executionContext()->executingWindow()->dispatchEvent(MessageEvent::create(ports.release(), value));
266 }
267
268 ServiceWorkerContainer::ServiceWorkerContainer(ExecutionContext* executionContext)
269     : ContextLifecycleObserver(executionContext)
270     , m_provider(0)
271 {
272     ScriptWrappable::init(this);
273
274     if (!executionContext)
275         return;
276
277     m_ready = createReadyProperty();
278
279     if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(executionContext)) {
280         m_provider = client->provider();
281         if (m_provider)
282             m_provider->setClient(this);
283     }
284 }
285
286 } // namespace blink