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 "ServiceWorkerGlobalScopeProxy.h"
35 #include "WebDataSourceImpl.h"
36 #include "WebFrameImpl.h"
37 #include "WebServiceWorkerContextClient.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"
54 using namespace WebCore;
58 // A thin wrapper for one-off script loading.
59 class WebEmbeddedWorkerImpl::Loader : public WorkerScriptLoaderClient {
61 static PassOwnPtr<Loader> create()
63 return adoptPtr(new Loader());
68 m_scriptLoader->setClient(0);
71 void load(ExecutionContext* loadingContext, const KURL& scriptURL, const Closure& callback)
73 m_callback = callback;
74 m_scriptLoader->setTargetType(ResourceRequest::TargetIsServiceWorker);
75 m_scriptLoader->loadAsynchronously(
76 loadingContext, scriptURL, DenyCrossOriginRequests, this);
79 virtual void notifyFinished() OVERRIDE
86 m_scriptLoader->cancel();
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(); }
94 Loader() : m_scriptLoader(WorkerScriptLoader::create())
98 RefPtr<WorkerScriptLoader> m_scriptLoader;
102 class WebEmbeddedWorkerImpl::LoaderProxy : public WorkerLoaderProxy {
104 static PassOwnPtr<LoaderProxy> create(WebEmbeddedWorkerImpl& embeddedWorker)
106 return adoptPtr(new LoaderProxy(embeddedWorker));
109 virtual void postTaskToLoader(PassOwnPtr<ExecutionContextTask> task) OVERRIDE
111 toWebFrameImpl(m_embeddedWorker.m_mainFrame)->frame()->document()->postTask(task);
114 virtual bool postTaskForModeToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask> task, const String& mode) OVERRIDE
116 if (m_embeddedWorker.m_askedToTerminate || !m_embeddedWorker.m_workerThread)
118 return m_embeddedWorker.m_workerThread->runLoop().postTaskForMode(task, mode);
122 explicit LoaderProxy(WebEmbeddedWorkerImpl& embeddedWorker)
123 : m_embeddedWorker(embeddedWorker)
127 // Not owned, embedded worker owns this.
128 WebEmbeddedWorkerImpl& m_embeddedWorker;
131 WebEmbeddedWorker* WebEmbeddedWorker::create(
132 WebServiceWorkerContextClient* client,
133 WebWorkerPermissionClientProxy* permissionClient)
135 return new WebEmbeddedWorkerImpl(adoptPtr(client), adoptPtr(permissionClient));
138 WebEmbeddedWorkerImpl::WebEmbeddedWorkerImpl(
139 PassOwnPtr<WebServiceWorkerContextClient> client,
140 PassOwnPtr<WebWorkerPermissionClientProxy> permissionClient)
141 : m_workerContextClient(client)
142 , m_permissionClient(permissionClient)
145 , m_askedToTerminate(false)
149 WebEmbeddedWorkerImpl::~WebEmbeddedWorkerImpl()
153 // Detach the client before closing the view to avoid getting called back.
154 toWebFrameImpl(m_mainFrame)->setClient(0);
157 m_mainFrame->close();
160 void WebEmbeddedWorkerImpl::startWorkerContext(
161 const WebEmbeddedWorkerStartData& data)
163 ASSERT(!m_askedToTerminate);
164 ASSERT(!m_mainScriptLoader);
165 m_workerStartData = data;
167 prepareShadowPageForLoader();
170 void WebEmbeddedWorkerImpl::terminateWorkerContext()
172 if (m_askedToTerminate)
174 m_askedToTerminate = true;
175 if (m_mainScriptLoader)
176 m_mainScriptLoader->cancel();
178 m_workerThread->stop();
181 void WebEmbeddedWorkerImpl::prepareShadowPageForLoader()
183 // Create 'shadow page', which is never displayed and is used mainly to
184 // provide a context for loading on the main thread.
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.
190 m_webView = WebView::create(0);
191 m_mainFrame = WebFrame::create(this);
192 m_webView->setMainFrame(m_mainFrame);
194 WebFrameImpl* webFrame = toWebFrameImpl(m_webView->mainFrame());
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.
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())));
204 void WebEmbeddedWorkerImpl::didFinishDocumentLoad(WebFrame* frame)
206 ASSERT(!m_mainScriptLoader);
208 m_mainScriptLoader = Loader::create();
209 m_mainScriptLoader->load(
210 toWebFrameImpl(m_mainFrame)->frame()->document(),
211 m_workerStartData.scriptURL,
212 bind(&WebEmbeddedWorkerImpl::onScriptLoaderFinished, this));
215 void WebEmbeddedWorkerImpl::onScriptLoaderFinished()
217 ASSERT(m_mainScriptLoader);
219 if (m_mainScriptLoader->failed() || m_askedToTerminate) {
220 m_workerContextClient->workerContextFailedToStart();
221 m_mainScriptLoader.clear();
225 WorkerThreadStartMode startMode =
226 (m_workerStartData.startMode == WebEmbeddedWorkerStartModePauseOnStart)
227 ? PauseWorkerGlobalScopeOnStart : DontPauseWorkerGlobalScopeOnStart;
229 OwnPtr<WorkerClients> workerClients = WorkerClients::create();
230 providePermissionClientToWorker(workerClients.get(), m_permissionClient.release());
232 OwnPtr<WorkerThreadStartupData> startupData =
233 WorkerThreadStartupData::create(
234 m_mainScriptLoader->url(),
235 m_workerStartData.userAgent,
236 m_mainScriptLoader->script(),
238 // FIXME: fill appropriate CSP info and policy type.
240 ContentSecurityPolicy::Enforce,
241 workerClients.release());
243 m_mainScriptLoader.clear();
245 m_workerGlobalScopeProxy = ServiceWorkerGlobalScopeProxy::create(*this, *toWebFrameImpl(m_mainFrame)->frame()->document(), m_workerContextClient.release());
246 m_loaderProxy = LoaderProxy::create(*this);
248 m_workerThread = ServiceWorkerThread::create(*m_loaderProxy, *m_workerGlobalScopeProxy, startupData.release());
249 m_workerThread->start();