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/NavigatorServiceWorker.h"
33 #include "RuntimeEnabledFeatures.h"
34 #include "bindings/v8/CallbackPromiseAdapter.h"
35 #include "bindings/v8/ScriptPromiseResolver.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/dom/ExecutionContext.h"
39 #include "core/frame/Frame.h"
40 #include "core/loader/DocumentLoader.h"
41 #include "core/loader/FrameLoaderClient.h"
42 #include "core/workers/SharedWorker.h"
43 #include "modules/serviceworkers/ServiceWorker.h"
44 #include "modules/serviceworkers/ServiceWorkerError.h"
45 #include "public/platform/WebServiceWorkerProvider.h"
46 #include "public/platform/WebServiceWorkerProviderClient.h"
47 #include "public/platform/WebString.h"
48 #include "public/platform/WebURL.h"
50 using blink::WebServiceWorkerProvider;
51 using blink::WebString;
55 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator)
56 : DOMWindowProperty(navigator->frame())
57 , m_navigator(navigator)
61 NavigatorServiceWorker::~NavigatorServiceWorker()
65 const char* NavigatorServiceWorker::supplementName()
67 return "NavigatorServiceWorker";
70 WebServiceWorkerProvider* NavigatorServiceWorker::ensureProvider()
72 ASSERT(m_navigator->frame());
74 Frame* frame = m_navigator->frame();
76 FrameLoaderClient* client = frame->loader().client();
77 // FIXME: This is temporarily hooked up here until we hook up to the loading process.
78 m_provider = client->createServiceWorkerProvider(nullptr);
80 return m_provider.get();
83 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator)
85 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator);
87 supplement = new NavigatorServiceWorker(navigator);
88 provideTo(navigator, supplementName(), adoptPtr(supplement));
93 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, const String& url, ExceptionState& exceptionState)
95 return from(navigator)->registerServiceWorker(context, pattern, url, exceptionState);
98 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* executionContext, const String& pattern, const String& scriptSrc, ExceptionState& exceptionState)
100 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
101 ScriptPromise promise = ScriptPromise::createPending(executionContext);
102 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise, executionContext);
104 Frame* frame = m_navigator->frame();
106 resolver->reject(DOMError::create(InvalidStateError, "No document available."));
110 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
112 KURL patternURL = executionContext->completeURL(pattern);
113 if (!documentOrigin->canRequest(patternURL)) {
114 resolver->reject(DOMError::create(SecurityError, "Can only register for patterns in the document's origin."));
118 KURL scriptURL = executionContext->completeURL(scriptSrc);
119 if (!documentOrigin->canRequest(scriptURL)) {
120 resolver->reject(DOMError::create(SecurityError, "Script must be in document's origin."));
124 ensureProvider()->registerServiceWorker(patternURL, scriptURL, new CallbackPromiseAdapter<ServiceWorker, ServiceWorkerError>(resolver, executionContext));
128 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, ExceptionState& exceptionState)
130 return from(navigator)->unregisterServiceWorker(context, pattern, exceptionState);
133 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* executionContext, const String& pattern, ExceptionState& exceptionState)
135 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
136 ScriptPromise promise = ScriptPromise::createPending(executionContext);
137 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise, executionContext);
139 Frame* frame = m_navigator->frame();
141 resolver->reject(DOMError::create(InvalidStateError, "No document available."));
145 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
147 KURL patternURL = executionContext->completeURL(pattern);
148 if (!documentOrigin->canRequest(patternURL)) {
149 resolver->reject(DOMError::create(SecurityError, "Can only unregister for patterns in the document's origin."));
154 ensureProvider()->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<ServiceWorker, ServiceWorkerError>(resolver, executionContext));
158 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame()
160 m_provider = nullptr;
161 DOMWindowProperty::willDetachGlobalObjectFromFrame();
165 } // namespace WebCore