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