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.
32 #include "WebEmbeddedWorkerImpl.h"
34 #include "ServiceWorkerGlobalScopeClientImpl.h"
35 #include "ServiceWorkerGlobalScopeProxy.h"
36 #include "WebDataSourceImpl.h"
37 #include "WebFrameImpl.h"
38 #include "WebServiceWorkerContextClient.h"
39 #include "WebServiceWorkerNetworkProvider.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"
58 using namespace WebCore;
62 // A thin wrapper for one-off script loading.
63 class WebEmbeddedWorkerImpl::Loader : public WorkerScriptLoaderClient {
65 static PassOwnPtr<Loader> create()
67 return adoptPtr(new Loader());
72 m_scriptLoader->setClient(0);
75 void load(ExecutionContext* loadingContext, const KURL& scriptURL, const Closure& callback)
77 m_callback = callback;
78 m_scriptLoader->setTargetType(ResourceRequest::TargetIsServiceWorker);
79 m_scriptLoader->loadAsynchronously(
80 loadingContext, scriptURL, DenyCrossOriginRequests, this);
83 virtual void notifyFinished() OVERRIDE
90 m_scriptLoader->cancel();
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(); }
98 Loader() : m_scriptLoader(WorkerScriptLoader::create())
102 RefPtr<WorkerScriptLoader> m_scriptLoader;
106 class WebEmbeddedWorkerImpl::LoaderProxy : public WorkerLoaderProxy {
108 static PassOwnPtr<LoaderProxy> create(WebEmbeddedWorkerImpl& embeddedWorker)
110 return adoptPtr(new LoaderProxy(embeddedWorker));
113 virtual void postTaskToLoader(PassOwnPtr<ExecutionContextTask> task) OVERRIDE
115 toWebFrameImpl(m_embeddedWorker.m_mainFrame)->frame()->document()->postTask(task);
118 virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask> task) OVERRIDE
120 if (m_embeddedWorker.m_askedToTerminate || !m_embeddedWorker.m_workerThread)
122 return m_embeddedWorker.m_workerThread->runLoop().postTask(task);
126 explicit LoaderProxy(WebEmbeddedWorkerImpl& embeddedWorker)
127 : m_embeddedWorker(embeddedWorker)
131 // Not owned, embedded worker owns this.
132 WebEmbeddedWorkerImpl& m_embeddedWorker;
135 WebEmbeddedWorker* WebEmbeddedWorker::create(
136 WebServiceWorkerContextClient* client,
137 WebWorkerPermissionClientProxy* permissionClient)
139 return new WebEmbeddedWorkerImpl(adoptPtr(client), adoptPtr(permissionClient));
142 WebEmbeddedWorkerImpl::WebEmbeddedWorkerImpl(
143 PassOwnPtr<WebServiceWorkerContextClient> client,
144 PassOwnPtr<WebWorkerPermissionClientProxy> permissionClient)
145 : m_workerContextClient(client)
146 , m_permissionClient(permissionClient)
149 , m_askedToTerminate(false)
153 WebEmbeddedWorkerImpl::~WebEmbeddedWorkerImpl()
157 // Detach the client before closing the view to avoid getting called back.
158 toWebFrameImpl(m_mainFrame)->setClient(0);
161 m_mainFrame->close();
164 void WebEmbeddedWorkerImpl::startWorkerContext(
165 const WebEmbeddedWorkerStartData& data)
167 ASSERT(!m_askedToTerminate);
168 ASSERT(!m_mainScriptLoader);
169 m_workerStartData = data;
171 prepareShadowPageForLoader();
174 void WebEmbeddedWorkerImpl::terminateWorkerContext()
176 if (m_askedToTerminate)
178 m_askedToTerminate = true;
179 if (m_mainScriptLoader)
180 m_mainScriptLoader->cancel();
182 m_workerThread->stop();
185 void WebEmbeddedWorkerImpl::prepareShadowPageForLoader()
187 // Create 'shadow page', which is never displayed and is used mainly to
188 // provide a context for loading on the main thread.
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.
194 m_webView = WebView::create(0);
195 m_mainFrame = WebFrame::create(this);
196 m_webView->setMainFrame(m_mainFrame);
198 WebFrameImpl* webFrame = toWebFrameImpl(m_webView->mainFrame());
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.
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())));
208 void WebEmbeddedWorkerImpl::willSendRequest(
209 WebFrame* frame, unsigned, WebURLRequest& request,
210 const WebURLResponse& redirectResponse)
212 if (m_networkProvider)
213 m_networkProvider->willSendRequest(frame->dataSource(), request);
216 void WebEmbeddedWorkerImpl::didFinishDocumentLoad(WebFrame* frame)
218 ASSERT(!m_mainScriptLoader);
219 ASSERT(!m_networkProvider);
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));
230 void WebEmbeddedWorkerImpl::onScriptLoaderFinished()
232 ASSERT(m_mainScriptLoader);
234 if (m_mainScriptLoader->failed() || m_askedToTerminate) {
235 m_mainScriptLoader.clear();
236 // This may delete 'this'.
237 m_workerContextClient->workerContextFailedToStart();
241 WorkerThreadStartMode startMode =
242 (m_workerStartData.startMode == WebEmbeddedWorkerStartModePauseOnStart)
243 ? PauseWorkerGlobalScopeOnStart : DontPauseWorkerGlobalScopeOnStart;
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();
249 OwnPtr<WorkerClients> workerClients = WorkerClients::create();
250 providePermissionClientToWorker(workerClients.get(), m_permissionClient.release());
251 provideServiceWorkerGlobalScopeClientToWorker(workerClients.get(), ServiceWorkerGlobalScopeClientImpl::create(m_workerContextClient.release()));
253 OwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData =
254 WorkerThreadStartupData::create(
255 m_mainScriptLoader->url(),
256 m_workerStartData.userAgent,
257 m_mainScriptLoader->script(),
259 // FIXME: fill appropriate CSP info and policy type.
261 ContentSecurityPolicyHeaderTypeEnforce,
262 workerClients.release());
264 m_mainScriptLoader.clear();
266 m_workerGlobalScopeProxy = ServiceWorkerGlobalScopeProxy::create(*this, *toWebFrameImpl(m_mainFrame)->frame()->document(), *contextClient);
267 m_loaderProxy = LoaderProxy::create(*this);
269 m_workerThread = ServiceWorkerThread::create(*m_loaderProxy, *m_workerGlobalScopeProxy, startupData.release());
270 m_workerThread->start();