2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #include "modules/serviceworkers/ServiceWorkerContainer.h"
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"
54 using blink::WebServiceWorker;
55 using blink::WebServiceWorkerProvider;
59 PassRefPtrWillBeRawPtr<ServiceWorkerContainer> ServiceWorkerContainer::create(ExecutionContext* executionContext)
61 return adoptRefWillBeNoop(new ServiceWorkerContainer(executionContext));
64 ServiceWorkerContainer::~ServiceWorkerContainer()
69 void ServiceWorkerContainer::willBeDetachedFromFrame()
72 m_provider->setClient(0);
77 void ServiceWorkerContainer::trace(Visitor* visitor)
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);
86 ScriptPromise ServiceWorkerContainer::registerServiceWorker(ScriptState* scriptState, const String& url, const Dictionary& dictionary)
88 RegistrationOptionList options(dictionary);
89 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
90 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
91 ScriptPromise promise = resolver->promise();
94 resolver->reject(DOMException::create(InvalidStateError, "No associated provider is available"));
98 // FIXME: This should use the container's execution context, not
100 ExecutionContext* executionContext = scriptState->executionContext();
101 RefPtr<SecurityOrigin> documentOrigin = executionContext->securityOrigin();
103 if (!documentOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) {
104 resolver->reject(DOMException::create(NotSupportedError, errorMessage));
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."));
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."));
122 m_provider->registerServiceWorker(patternURL, scriptURL, new CallbackPromiseAdapter<ServiceWorkerRegistration, ServiceWorkerError>(resolver));
127 class UndefinedValue {
129 typedef WebServiceWorkerRegistration WebType;
130 static V8UndefinedType take(ScriptPromiseResolver* resolver, WebType* registration)
132 ASSERT(!registration); // Anything passed here will be leaked.
133 return V8UndefinedType();
135 static void dispose(WebType* registration)
137 ASSERT(!registration); // Anything passed here will be leaked.
144 ScriptPromise ServiceWorkerContainer::unregisterServiceWorker(ScriptState* scriptState, const String& pattern)
146 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
147 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
148 ScriptPromise promise = resolver->promise();
151 resolver->reject(DOMException::create(InvalidStateError, "No associated provider is available"));
155 // FIXME: This should use the container's execution context, not
157 RefPtr<SecurityOrigin> documentOrigin = scriptState->executionContext()->securityOrigin();
159 if (!documentOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) {
160 resolver->reject(DOMException::create(NotSupportedError, errorMessage));
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."));
171 m_provider->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<UndefinedValue, ServiceWorkerError>(resolver));
175 ServiceWorkerContainer::ReadyProperty* ServiceWorkerContainer::createReadyProperty()
177 return new ReadyProperty(executionContext(), this, ReadyProperty::Ready);
180 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState)
182 if (!executionContext())
183 return ScriptPromise();
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."));
191 return m_ready->promise(callerState->world());
194 // If the WebServiceWorker is up for adoption (does not have a
195 // WebServiceWorkerProxy owner), rejects the adoption by deleting the
197 static void deleteIfNoExistingOwner(WebServiceWorker* serviceWorker)
199 if (serviceWorker && !serviceWorker->proxy())
200 delete serviceWorker;
203 void ServiceWorkerContainer::setActive(WebServiceWorker* serviceWorker)
205 if (!executionContext()) {
206 deleteIfNoExistingOwner(serviceWorker);
209 RefPtrWillBeRawPtr<ServiceWorker> previousReadyWorker = m_active;
210 m_active = ServiceWorker::from(executionContext(), serviceWorker);
211 checkReadyChanged(previousReadyWorker.release());
214 void ServiceWorkerContainer::checkReadyChanged(PassRefPtrWillBeRawPtr<ServiceWorker> previousReadyWorker)
216 ServiceWorker* currentReadyWorker = m_active.get();
218 if (previousReadyWorker == currentReadyWorker)
221 if (m_ready->state() != ReadyProperty::Pending) {
222 // Already resolved Promises are now stale because the
223 // ready worker changed
224 m_ready = createReadyProperty();
227 if (currentReadyWorker)
228 m_ready->resolve(currentReadyWorker);
231 void ServiceWorkerContainer::setController(WebServiceWorker* serviceWorker)
233 if (!executionContext()) {
234 deleteIfNoExistingOwner(serviceWorker);
237 m_controller = ServiceWorker::from(executionContext(), serviceWorker);
240 void ServiceWorkerContainer::setInstalling(WebServiceWorker* serviceWorker)
242 if (!executionContext()) {
243 deleteIfNoExistingOwner(serviceWorker);
246 m_installing = ServiceWorker::from(executionContext(), serviceWorker);
249 void ServiceWorkerContainer::setWaiting(WebServiceWorker* serviceWorker)
251 if (!executionContext()) {
252 deleteIfNoExistingOwner(serviceWorker);
255 m_waiting = ServiceWorker::from(executionContext(), serviceWorker);
258 void ServiceWorkerContainer::dispatchMessageEvent(const WebString& message, const WebMessagePortChannelArray& webChannels)
260 if (!executionContext() || !executionContext()->executingWindow())
263 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::toMessagePortArray(executionContext(), webChannels);
264 RefPtr<SerializedScriptValue> value = SerializedScriptValue::createFromWire(message);
265 executionContext()->executingWindow()->dispatchEvent(MessageEvent::create(ports.release(), value));
268 ServiceWorkerContainer::ServiceWorkerContainer(ExecutionContext* executionContext)
269 : ContextLifecycleObserver(executionContext)
272 ScriptWrappable::init(this);
274 if (!executionContext)
277 m_ready = createReadyProperty();
279 if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(executionContext)) {
280 m_provider = client->provider();
282 m_provider->setClient(this);