Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: Source/web/WebEmbeddedWorkerImpl.cpp

Issue 78233002: Implement initial part of WebEmbeddedWorker (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: updated Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "WebDataSourceImpl.h"
35 #include "WebFrameImpl.h"
36 #include "WebServiceWorkerContextClient.h"
37 #include "WebView.h"
38 #include "WebWorkerPermissionClientProxy.h"
39 #include "WorkerPermissionClient.h"
40 #include "core/dom/Document.h"
41 #include "core/frame/ContentSecurityPolicyResponseHeaders.h"
42 #include "core/loader/FrameLoadRequest.h"
43 #include "core/loader/SubstituteData.h"
44 #include "core/workers/WorkerClients.h"
45 #include "core/workers/WorkerScriptLoader.h"
46 #include "core/workers/WorkerScriptLoaderClient.h"
47 #include "core/workers/WorkerThreadStartupData.h"
48 #include "platform/NotImplemented.h"
49 #include "platform/SharedBuffer.h"
50 #include "wtf/Functional.h"
51
52 using namespace WebCore;
53
54 namespace blink {
55
56 // A thin wrapper for one-off script loading.
57 class WebEmbeddedWorkerImpl::Loader : public WorkerScriptLoaderClient {
58 public:
59 static PassOwnPtr<Loader> create(ExecutionContext* loadingContext, const KUR L& scriptURL, const Closure& callback)
60 {
61 return adoptPtr(new Loader(loadingContext, scriptURL, callback));
62 }
63
64 virtual ~Loader()
65 {
66 m_scriptLoader->setClient(0);
michaeln 2013/11/21 22:05:35 was wondering where the that setter was used
67 }
68
69 virtual void didReceiveResponse(unsigned long, const WebCore::ResourceRespon se& response)
michaeln 2013/11/21 22:05:35 OVERRIDE keyword?
kinuko 2013/11/22 02:23:33 Done.
70 {
71 m_cspHeader = ContentSecurityPolicyResponseHeaders(response);
72 }
73
74 virtual void notifyFinished() OVERRIDE
75 {
76 m_callback();
77 }
78
79 void cancel()
80 {
81 m_scriptLoader->cancel();
82 }
83
84 bool failed() const { return m_scriptLoader->failed(); }
85 String script() const { return m_scriptLoader->script(); }
86 const ContentSecurityPolicyResponseHeaders& cspHeader() const { return m_csp Header; }
87
88 private:
89 Loader(ExecutionContext* loadingContext, const KURL& scriptURL, const Closur e& callback)
90 : m_scriptLoader(WorkerScriptLoader::create())
91 , m_callback(callback)
92 {
93 m_scriptLoader->setTargetType(ResourceRequest::TargetIsServiceWorker);
94 m_scriptLoader->loadAsynchronously(
95 loadingContext, scriptURL, DenyCrossOriginRequests, this);
96 }
97
98 RefPtr<WorkerScriptLoader> m_scriptLoader;
99 Closure m_callback;
100 ContentSecurityPolicyResponseHeaders m_cspHeader;
101 };
102
103 WebEmbeddedWorker* WebEmbeddedWorker::create(
104 WebServiceWorkerContextClient* client,
105 WebWorkerPermissionClientProxy* permissionClient)
106 {
107 return new WebEmbeddedWorkerImpl(adoptPtr(client), adoptPtr(permissionClient ));
108 }
109
110 WebEmbeddedWorkerImpl::WebEmbeddedWorkerImpl(
111 PassOwnPtr<WebServiceWorkerContextClient> client,
112 PassOwnPtr<WebWorkerPermissionClientProxy> permissionClient)
113 : m_workerContextClient(client)
114 , m_permissionClient(permissionClient)
115 , m_askedToTerminate(false)
116 {
117 }
118
119 WebEmbeddedWorkerImpl::~WebEmbeddedWorkerImpl()
120 {
121 ASSERT(m_webView);
122
123 // Detach the client before closing the view to avoid getting called back.
124 toWebFrameImpl(m_mainFrame)->setClient(0);
125
126 m_webView->close();
127 m_mainFrame->close();
128 }
129
130 void WebEmbeddedWorkerImpl::startWorkerContext(
131 const WebEmbeddedWorkerStartData& data)
132 {
133 ASSERT(!m_askedToTerminate);
134 ASSERT(!m_mainScriptLoader);
135 m_workerStartData = data;
136
137 prepareShadowPageForLoader();
138
139 m_mainScriptLoader = Loader::create(
140 m_loadingDocument.get(),
141 data.scriptURL,
142 bind(&WebEmbeddedWorkerImpl::didLoadScriptAndStartWorkerContext, this));
michaeln 2013/11/21 22:05:35 i have to remind myself that despite looking like
143 }
144
145 void WebEmbeddedWorkerImpl::terminateWorkerContext()
146 {
147 if (m_askedToTerminate)
148 return;
149 m_askedToTerminate = true;
150 if (m_mainScriptLoader)
151 m_mainScriptLoader->cancel();
kinuko 2013/11/21 06:36:05 note: this will call didLoadScriptAndStartWorkerCo
152 if (m_workerThread)
153 m_workerThread->stop();
154 }
155
156 void WebEmbeddedWorkerImpl::postTaskToLoader(PassOwnPtr<WebCore::ExecutionContex tTask> task)
157 {
158 m_loadingDocument->postTask(task);
159 }
160
161 bool WebEmbeddedWorkerImpl::postTaskForModeToWorkerGlobalScope(PassOwnPtr<WebCor e::ExecutionContextTask> task, const String& mode)
162 {
163 m_workerThread->runLoop().postTaskForMode(task, mode);
164 return true;
165 }
166
167 void WebEmbeddedWorkerImpl::prepareShadowPageForLoader()
168 {
169 // Create 'shadow page', which is never displayed and is used mainly to
170 // provide a context for loading on the main thread.
171 ASSERT(!m_webView);
172 m_webView = WebView::create(0);
173 m_mainFrame = WebFrame::create(this);
174 m_webView->setMainFrame(m_mainFrame);
175
176 WebFrameImpl* webFrame = toWebFrameImpl(m_webView->mainFrame());
177
178 // Construct substitute data source for the 'shadow page'. We only need it
179 // to have same origin as the worker so the loading checks work correctly.
180 CString content("");
181 int length = static_cast<int>(content.length());
182 RefPtr<SharedBuffer> buffer(SharedBuffer::create(content.data(), length));
183 webFrame->frame()->loader().load(FrameLoadRequest(0, ResourceRequest(m_worke rStartData.scriptURL), SubstituteData(buffer, "text/html", "UTF-8", KURL())));
184
185 // This document will be used as 'loading context' for the worker.
186 m_loadingDocument = webFrame->frame()->document();
187 }
188
189 void WebEmbeddedWorkerImpl::didCreateDataSource(WebFrame*, WebDataSource* ds)
190 {
191 // Tell the loader to load the data into the 'shadow page' synchronously,
192 // so we can grab the resulting Document right after load.
193 static_cast<WebDataSourceImpl*>(ds)->setDeferMainResourceDataLoad(false);
194 }
195
196 void WebEmbeddedWorkerImpl::didLoadScriptAndStartWorkerContext()
michaeln 2013/11/21 22:05:35 nit: method naming, its a combo of a notification
kinuko 2013/11/22 02:23:33 Actually I suspected this name would be bad for th
197 {
198 ASSERT(m_mainScriptLoader);
199
200 if (m_mainScriptLoader->failed() || m_askedToTerminate) {
201 m_workerContextClient->workerContextFailedToStart();
202 m_mainScriptLoader.clear();
203 return;
204 }
205
206 // Setting up worker startup data.
207 WorkerThreadStartMode startMode =
208 (m_workerStartData.startMode == WebEmbeddedWorkerStartModePauseOnStart)
209 ? PauseWorkerGlobalScopeOnStart : DontPauseWorkerGlobalScopeOnStart;
210
211 const ContentSecurityPolicyResponseHeaders& header = m_mainScriptLoader->csp Header();
212 String cspHeader = header.contentSecurityPolicy();
213 ContentSecurityPolicy::HeaderType cspType = ContentSecurityPolicy::Enforce;
214 if (!header.contentSecurityPolicyReportOnly().isEmpty())
215 cspType = ContentSecurityPolicy::Report;
michaeln 2013/11/21 22:05:35 I'm trying to figure out how the ContentSecurityPo
kinuko 2013/11/22 02:23:33 I'm thinking about passing ContentSecurityPolicyRe
216
217 OwnPtr<WorkerClients> workerClients = WorkerClients::create();
218 providePermissionClientToWorker(workerClients.get(), m_permissionClient.rele ase());
219
220 OwnPtr<WorkerThreadStartupData> startupData =
221 WorkerThreadStartupData::create(
222 m_workerStartData.scriptURL,
michaeln 2013/11/21 22:05:35 If there is a same-origin redirect, i think we nee
kinuko 2013/11/22 02:23:33 I was going to do so, but existing worker code is
michaeln 2013/11/22 20:01:15 If SharedWorkers get the baseurl wrong, that's no
223 m_workerStartData.userAgent,
224 m_mainScriptLoader->script(),
225 startMode,
226 cspHeader,
227 cspType,
228 workerClients.release());
229
230 m_mainScriptLoader.clear();
231
232 // FIXME: Create WorkerReportingProxy, create ServiceWorkerThread and
233 // start it with m_scripLoader->script().
234 notImplemented();
235 }
236
237 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698