- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / NavigatorServiceWorker.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/NavigatorServiceWorker.h"
32
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"
50
51 using WebKit::WebServiceWorkerProvider;
52 using WebKit::WebString;
53
54 namespace WebCore {
55
56 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator)
57     : DOMWindowProperty(navigator->frame())
58     , m_navigator(navigator)
59 {
60 }
61
62 NavigatorServiceWorker::~NavigatorServiceWorker()
63 {
64 }
65
66 const char* NavigatorServiceWorker::supplementName()
67 {
68     return "NavigatorServiceWorker";
69 }
70
71 WebServiceWorkerProvider* NavigatorServiceWorker::ensureProvider()
72 {
73     ASSERT(m_navigator->frame());
74     if (!m_provider) {
75         Frame* frame = m_navigator->frame();
76
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);
80     }
81     return m_provider.get();
82 }
83
84 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator)
85 {
86     NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator);
87     if (!supplement) {
88         supplement = new NavigatorServiceWorker(navigator);
89         provideTo(navigator, supplementName(), adoptPtr(supplement));
90     }
91     return supplement;
92 }
93
94 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, const String& url, ExceptionState& es)
95 {
96     return from(navigator)->registerServiceWorker(context, pattern, url, es);
97 }
98
99 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* executionContext, const String& pattern, const String& scriptSrc, ExceptionState& es)
100 {
101     ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
102     Frame* frame = m_navigator->frame();
103
104     if (!frame) {
105         es.throwDOMException(InvalidStateError, "No document available.");
106         return ScriptPromise();
107     }
108
109     RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
110
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();
115     }
116
117     KURL scriptURL = executionContext->completeURL(scriptSrc);
118     if (documentOrigin->canRequest(scriptURL)) {
119         es.throwSecurityError("Script must be in document's origin.");
120         return ScriptPromise();
121     }
122
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));
126     return promise;
127 }
128
129 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* context, Navigator* navigator, const String& pattern, ExceptionState& es)
130 {
131     return from(navigator)->unregisterServiceWorker(context, pattern, es);
132 }
133
134 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext* executionContext, const String& pattern, ExceptionState& es)
135 {
136     ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled());
137     Frame* frame = m_navigator->frame();
138     if (!frame) {
139         es.throwDOMException(InvalidStateError, "No document available.");
140         return ScriptPromise();
141     }
142
143     RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin();
144
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();
149     }
150
151     ScriptPromise promise = ScriptPromise::createPending(executionContext);
152     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise, executionContext);
153     ensureProvider()->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<ServiceWorker, ServiceWorker>(resolver, executionContext));
154     return promise;
155 }
156
157 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame()
158 {
159     m_provider = nullptr;
160     DOMWindowProperty::willDetachGlobalObjectFromFrame();
161 }
162
163
164 } // namespace WebCore