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 "V8ServiceWorker.h"
35 #include "bindings/v8/CallbackPromiseAdapter.h"
36 #include "bindings/v8/ScriptPromiseResolver.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/dom/ExecutionContext.h"
40 #include "core/frame/Frame.h"
41 #include "core/frame/Navigator.h"
42 #include "core/loader/DocumentLoader.h"
43 #include "core/loader/FrameLoaderClient.h"
44 #include "core/workers/SharedWorker.h"
45 #include "modules/serviceworkers/ServiceWorker.h"
46 #include "public/platform/WebServiceWorkerProvider.h"
47 #include "public/platform/WebServiceWorkerProviderClient.h"
48 #include "public/platform/WebString.h"
49 #include "public/platform/WebURL.h"
51 using WebKit::WebServiceWorkerProvider;
52 using WebKit::WebString;
56 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator)
57 : DOMWindowProperty(navigator->frame())
58 , m_navigator(navigator)
62 NavigatorServiceWorker::~NavigatorServiceWorker()
66 const char* NavigatorServiceWorker::supplementName()
68 return "NavigatorServiceWorker";
71 WebServiceWorkerProvider* NavigatorServiceWorker::ensureProvider()
73 ASSERT(m_navigator->frame());
75 Frame* frame = m_navigator->frame();
77 FrameLoaderClient* client = frame->loader().client();
78 // FIXME: This is temporarily hooked up here until we hook up to the loading process.
79 m_provider = client->createServiceWorkerProvider(nullptr);
81 return m_provider.get();
84 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator)
86 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator);
88 supplement = new NavigatorServiceWorker(navigator);
89 provideTo(navigator, supplementName(), adoptPtr(supplement));
94 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, const String& url, ExceptionState& es)
96 return from(navigator)->registerServiceWorker(context, pattern, url, es);
99 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* executionContext, const String& pattern, const String& scriptSrc, ExceptionState& es)
101 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
102 Frame* frame = m_navigator->frame();
105 es.throwDOMException(InvalidStateError, "No document available.");
106 return ScriptPromise();
109 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
111 KURL patternURL = executionContext->completeURL(pattern);
112 if (documentOrigin->canRequest(patternURL)) {
113 es.throwSecurityError("Can only register for patterns in the document's origin.");
114 return ScriptPromise();
117 KURL scriptURL = executionContext->completeURL(scriptSrc);
118 if (documentOrigin->canRequest(scriptURL)) {
119 es.throwSecurityError("Script must be in document's origin.");
120 return ScriptPromise();
123 ScriptPromise promise = ScriptPromise::createPending(executionContext);
124 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise, executionContext);
125 ensureProvider()->registerServiceWorker(patternURL, scriptURL, new CallbackPromiseAdapter<ServiceWorker, ServiceWorker>(resolver, executionContext));
129 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, ExceptionState& es)
131 return from(navigator)->unregisterServiceWorker(context, pattern, es);
134 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* executionContext, const String& pattern, ExceptionState& es)
136 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
137 Frame* frame = m_navigator->frame();
139 es.throwDOMException(InvalidStateError, "No document available.");
140 return ScriptPromise();
143 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
145 KURL patternURL = executionContext->completeURL(pattern);
146 if (documentOrigin->canRequest(patternURL)) {
147 es.throwSecurityError("Can only unregister for patterns in the document's origin.");
148 return ScriptPromise();
151 ScriptPromise promise = ScriptPromise::createPending(executionContext);
152 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise, executionContext);
153 ensureProvider()->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<ServiceWorker, ServiceWorker>(resolver, executionContext));
157 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame()
159 m_provider = nullptr;
160 DOMWindowProperty::willDetachGlobalObjectFromFrame();
164 } // namespace WebCore