b5da7daf9ff4e7ab8912537329e1006877532087
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebEmbeddedWorkerImpl.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
31 #include "config.h"
32 #include "WebEmbeddedWorkerImpl.h"
33
34 #include "ServiceWorkerGlobalScopeClientImpl.h"
35 #include "ServiceWorkerGlobalScopeProxy.h"
36 #include "WebDataSourceImpl.h"
37 #include "WebFrameImpl.h"
38 #include "WebServiceWorkerContextClient.h"
39 #include "WebServiceWorkerNetworkProvider.h"
40 #include "WebView.h"
41 #include "WebWorkerPermissionClientProxy.h"
42 #include "WorkerPermissionClient.h"
43 #include "core/dom/Document.h"
44 #include "core/loader/FrameLoadRequest.h"
45 #include "core/loader/SubstituteData.h"
46 #include "core/workers/WorkerClients.h"
47 #include "core/workers/WorkerLoaderProxy.h"
48 #include "core/workers/WorkerScriptLoader.h"
49 #include "core/workers/WorkerScriptLoaderClient.h"
50 #include "core/workers/WorkerThreadStartupData.h"
51 #include "heap/Handle.h"
52 #include "modules/serviceworkers/ServiceWorkerThread.h"
53 #include "platform/NotImplemented.h"
54 #include "platform/SharedBuffer.h"
55 #include "platform/network/ContentSecurityPolicyParsers.h"
56 #include "wtf/Functional.h"
57
58 using namespace WebCore;
59
60 namespace blink {
61
62 // A thin wrapper for one-off script loading.
63 class WebEmbeddedWorkerImpl::Loader : public WorkerScriptLoaderClient {
64 public:
65     static PassOwnPtr<Loader> create()
66     {
67         return adoptPtr(new Loader());
68     }
69
70     virtual ~Loader()
71     {
72         m_scriptLoader->setClient(0);
73     }
74
75     void load(ExecutionContext* loadingContext, const KURL& scriptURL, const Closure& callback)
76     {
77         m_callback = callback;
78         m_scriptLoader->setTargetType(ResourceRequest::TargetIsServiceWorker);
79         m_scriptLoader->loadAsynchronously(
80             loadingContext, scriptURL, DenyCrossOriginRequests, this);
81     }
82
83     virtual void notifyFinished() OVERRIDE
84     {
85         m_callback();
86     }
87
88     void cancel()
89     {
90         m_scriptLoader->cancel();
91     }
92
93     bool failed() const { return m_scriptLoader->failed(); }
94     const KURL& url() const { return m_scriptLoader->responseURL(); }
95     String script() const { return m_scriptLoader->script(); }
96
97 private:
98     Loader() : m_scriptLoader(WorkerScriptLoader::create())
99     {
100     }
101
102     RefPtr<WorkerScriptLoader> m_scriptLoader;
103     Closure m_callback;
104 };
105
106 class WebEmbeddedWorkerImpl::LoaderProxy : public WorkerLoaderProxy {
107 public:
108     static PassOwnPtr<LoaderProxy> create(WebEmbeddedWorkerImpl& embeddedWorker)
109     {
110         return adoptPtr(new LoaderProxy(embeddedWorker));
111     }
112
113     virtual void postTaskToLoader(PassOwnPtr<ExecutionContextTask> task) OVERRIDE
114     {
115         toWebFrameImpl(m_embeddedWorker.m_mainFrame)->frame()->document()->postTask(task);
116     }
117
118     virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask> task) OVERRIDE
119     {
120         if (m_embeddedWorker.m_askedToTerminate || !m_embeddedWorker.m_workerThread)
121             return false;
122         return m_embeddedWorker.m_workerThread->runLoop().postTask(task);
123     }
124
125 private:
126     explicit LoaderProxy(WebEmbeddedWorkerImpl& embeddedWorker)
127         : m_embeddedWorker(embeddedWorker)
128     {
129     }
130
131     // Not owned, embedded worker owns this.
132     WebEmbeddedWorkerImpl& m_embeddedWorker;
133 };
134
135 WebEmbeddedWorker* WebEmbeddedWorker::create(
136     WebServiceWorkerContextClient* client,
137     WebWorkerPermissionClientProxy* permissionClient)
138 {
139     return new WebEmbeddedWorkerImpl(adoptPtr(client), adoptPtr(permissionClient));
140 }
141
142 WebEmbeddedWorkerImpl::WebEmbeddedWorkerImpl(
143     PassOwnPtr<WebServiceWorkerContextClient> client,
144     PassOwnPtr<WebWorkerPermissionClientProxy> permissionClient)
145     : m_workerContextClient(client)
146     , m_permissionClient(permissionClient)
147     , m_webView(0)
148     , m_mainFrame(0)
149     , m_askedToTerminate(false)
150 {
151 }
152
153 WebEmbeddedWorkerImpl::~WebEmbeddedWorkerImpl()
154 {
155     ASSERT(m_webView);
156
157     // Detach the client before closing the view to avoid getting called back.
158     toWebFrameImpl(m_mainFrame)->setClient(0);
159
160     m_webView->close();
161     m_mainFrame->close();
162 }
163
164 void WebEmbeddedWorkerImpl::startWorkerContext(
165     const WebEmbeddedWorkerStartData& data)
166 {
167     ASSERT(!m_askedToTerminate);
168     ASSERT(!m_mainScriptLoader);
169     m_workerStartData = data;
170
171     prepareShadowPageForLoader();
172 }
173
174 void WebEmbeddedWorkerImpl::terminateWorkerContext()
175 {
176     if (m_askedToTerminate)
177         return;
178     m_askedToTerminate = true;
179     if (m_mainScriptLoader)
180         m_mainScriptLoader->cancel();
181     if (m_workerThread)
182         m_workerThread->stop();
183 }
184
185 void WebEmbeddedWorkerImpl::prepareShadowPageForLoader()
186 {
187     // Create 'shadow page', which is never displayed and is used mainly to
188     // provide a context for loading on the main thread.
189     //
190     // FIXME: This does mostly same as WebSharedWorkerImpl::initializeLoader.
191     // This code, and probably most of the code in this class should be shared
192     // with SharedWorker.
193     ASSERT(!m_webView);
194     m_webView = WebView::create(0);
195     m_mainFrame = WebFrame::create(this);
196     m_webView->setMainFrame(m_mainFrame);
197
198     WebFrameImpl* webFrame = toWebFrameImpl(m_webView->mainFrame());
199
200     // Construct substitute data source for the 'shadow page'. We only need it
201     // to have same origin as the worker so the loading checks work correctly.
202     CString content("");
203     int length = static_cast<int>(content.length());
204     RefPtr<SharedBuffer> buffer(SharedBuffer::create(content.data(), length));
205     webFrame->frame()->loader().load(FrameLoadRequest(0, ResourceRequest(m_workerStartData.scriptURL), SubstituteData(buffer, "text/html", "UTF-8", KURL())));
206 }
207
208 void WebEmbeddedWorkerImpl::willSendRequest(
209     WebFrame* frame, unsigned, WebURLRequest& request,
210     const WebURLResponse& redirectResponse)
211 {
212     if (m_networkProvider)
213         m_networkProvider->willSendRequest(frame->dataSource(), request);
214 }
215
216 void WebEmbeddedWorkerImpl::didFinishDocumentLoad(WebFrame* frame)
217 {
218     ASSERT(!m_mainScriptLoader);
219     ASSERT(!m_networkProvider);
220     ASSERT(m_mainFrame);
221     ASSERT(m_workerContextClient);
222     m_networkProvider = adoptPtr(m_workerContextClient->createServiceWorkerNetworkProvider(frame->dataSource()));
223     m_mainScriptLoader = Loader::create();
224     m_mainScriptLoader->load(
225         toWebFrameImpl(m_mainFrame)->frame()->document(),
226         m_workerStartData.scriptURL,
227         bind(&WebEmbeddedWorkerImpl::onScriptLoaderFinished, this));
228 }
229
230 void WebEmbeddedWorkerImpl::onScriptLoaderFinished()
231 {
232     ASSERT(m_mainScriptLoader);
233
234     if (m_mainScriptLoader->failed() || m_askedToTerminate) {
235         m_mainScriptLoader.clear();
236         // This may delete 'this'.
237         m_workerContextClient->workerContextFailedToStart();
238         return;
239     }
240
241     WorkerThreadStartMode startMode =
242         (m_workerStartData.startMode == WebEmbeddedWorkerStartModePauseOnStart)
243         ? PauseWorkerGlobalScopeOnStart : DontPauseWorkerGlobalScopeOnStart;
244
245     // This is to be owned by ServiceWorker's WorkerGlobalScope, and is
246     // guaranteed to be around while the WorkerGlobalScope is alive.
247     WebServiceWorkerContextClient* contextClient = m_workerContextClient.get();
248
249     OwnPtr<WorkerClients> workerClients = WorkerClients::create();
250     providePermissionClientToWorker(workerClients.get(), m_permissionClient.release());
251     provideServiceWorkerGlobalScopeClientToWorker(workerClients.get(), ServiceWorkerGlobalScopeClientImpl::create(m_workerContextClient.release()));
252
253     OwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData =
254         WorkerThreadStartupData::create(
255             m_mainScriptLoader->url(),
256             m_workerStartData.userAgent,
257             m_mainScriptLoader->script(),
258             startMode,
259             // FIXME: fill appropriate CSP info and policy type.
260             String(),
261             ContentSecurityPolicyHeaderTypeEnforce,
262             workerClients.release());
263
264     m_mainScriptLoader.clear();
265
266     m_workerGlobalScopeProxy = ServiceWorkerGlobalScopeProxy::create(*this, *toWebFrameImpl(m_mainFrame)->frame()->document(), *contextClient);
267     m_loaderProxy = LoaderProxy::create(*this);
268
269     m_workerThread = ServiceWorkerThread::create(*m_loaderProxy, *m_workerGlobalScopeProxy, startupData.release());
270     m_workerThread->start();
271 }
272
273 } // namespace blink