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

Side by Side Diff: webkit/glue/webworker_impl.cc

Issue 337057: Move webworker{client}_impl.{h,cc} into webkit/api/src (Closed)
Patch Set: mulitple workers per process comment fixed Created 11 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
« no previous file with comments | « webkit/glue/webworker_impl.h ('k') | webkit/glue/webworkerclient_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6
7 #include "DedicatedWorkerContext.h"
8 #include "DedicatedWorkerThread.h"
9 #include "GenericWorkerTask.h"
10 #include "KURL.h"
11 #include "MessageEvent.h"
12 #include "MessagePort.h"
13 #include "MessagePortChannel.h"
14 #include "ScriptExecutionContext.h"
15 #include "SecurityOrigin.h"
16 #include "SerializedScriptValue.h"
17 #include "SubstituteData.h"
18 #include <wtf/MainThread.h>
19 #include <wtf/Threading.h>
20
21 #undef LOG
22
23 #include "webkit/api/public/WebFrameClient.h"
24 #include "webkit/api/public/WebMessagePortChannel.h"
25 #include "webkit/api/public/WebScreenInfo.h"
26 #include "webkit/api/public/WebString.h"
27 #include "webkit/api/public/WebURL.h"
28 #include "webkit/api/public/WebView.h"
29 #include "webkit/api/public/WebWorkerClient.h"
30 #include "webkit/api/src/PlatformMessagePortChannel.h"
31 #include "webkit/api/src/WebDataSourceImpl.h"
32 #include "webkit/glue/empty_webframeclient.h"
33 #include "webkit/glue/glue_util.h"
34 #include "webkit/glue/webframe_impl.h"
35 #include "webkit/glue/webpreferences.h"
36 #include "webkit/glue/webworker_impl.h"
37
38 using WebKit::WebCursorInfo;
39 using WebKit::WebDataSource;
40 using WebKit::WebDataSourceImpl;
41 using WebKit::WebFrame;
42 using WebKit::WebFrameClient;
43 using WebKit::WebMessagePortChannel;
44 using WebKit::WebMessagePortChannelArray;
45 using WebKit::WebNavigationPolicy;
46 using WebKit::WebRect;
47 using WebKit::WebScreenInfo;
48 using WebKit::WebString;
49 using WebKit::WebURL;
50 using WebKit::WebWorker;
51 using WebKit::WebWorkerClient;
52 using WebKit::WebView;
53
54 #if ENABLE(WORKERS)
55
56 // Dummy WebViewDelegate - we only need it in Worker process to load a
57 // 'shadow page' which will initialize WebCore loader.
58 class WorkerWebFrameClient : public webkit_glue::EmptyWebFrameClient {
59 public:
60 // Tell the loader to load the data into the 'shadow page' synchronously,
61 // so we can grab the resulting Document right after load.
62 virtual void didCreateDataSource(WebFrame* frame, WebDataSource* ds) {
63 static_cast<WebDataSourceImpl*>(ds)->setDeferMainResourceDataLoad(false);
64 }
65
66 // Lazy allocate and leak this instance.
67 static WorkerWebFrameClient* GetSharedInstance() {
68 static WorkerWebFrameClient client;
69 return &client;
70 }
71
72 private:
73 WorkerWebFrameClient() {}
74 };
75
76 namespace WebKit {
77
78 WebWorker* WebWorker::create(WebWorkerClient* client) {
79 return new WebWorkerImpl(client);
80 }
81
82 }
83
84 // This function is called on the main thread to force to initialize some static
85 // values used in WebKit before any worker thread is started. This is because in
86 // our worker processs, we do not run any WebKit code in main thread and thus
87 // when multiple workers try to start at the same time, we might hit crash due
88 // to contention for initializing static values.
89 void InitializeWebKitStaticValues() {
90 static bool initialized = false;
91 if (!initialized) {
92 initialized= true;
93 // Note that we have to pass a URL with valid protocol in order to follow
94 // the path to do static value initializations.
95 WTF::RefPtr<WebCore::SecurityOrigin> origin =
96 WebCore::SecurityOrigin::create(WebCore::KURL(WebCore::ParsedURLString,
97 "http://localhost"));
98 origin.release();
99 }
100 }
101
102 WebWorkerImpl::WebWorkerImpl(WebWorkerClient* client)
103 : client_(client),
104 web_view_(NULL),
105 asked_to_terminate_(false) {
106 InitializeWebKitStaticValues();
107 }
108
109 WebWorkerImpl::~WebWorkerImpl() {
110 web_view_->close();
111 }
112
113 void WebWorkerImpl::PostMessageToWorkerContextTask(
114 WebCore::ScriptExecutionContext* context,
115 WebWorkerImpl* this_ptr,
116 const WebCore::String& message,
117 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) {
118 ASSERT(context->isWorkerContext());
119 WebCore::DedicatedWorkerContext* worker_context =
120 static_cast<WebCore::DedicatedWorkerContext*>(context);
121
122 WTF::OwnPtr<WebCore::MessagePortArray> ports =
123 WebCore::MessagePort::entanglePorts(*context, channels.release());
124 WTF::RefPtr<WebCore::SerializedScriptValue> serialized_message =
125 WebCore::SerializedScriptValue::create(message);
126 worker_context->dispatchEvent(
127 WebCore::MessageEvent::create(ports.release(),
128 serialized_message.release()));
129
130 this_ptr->confirmMessageFromWorkerObject(
131 worker_context->hasPendingActivity());
132 }
133
134 // WebWorker -------------------------------------------------------------------
135
136 void WebWorkerImpl::startWorkerContext(const WebURL& script_url,
137 const WebString& user_agent,
138 const WebString& source_code) {
139 // Create 'shadow page'. This page is never displayed, it is used to proxy the
140 // loading requests from the worker context to the rest of WebKit and Chromium
141 // infrastructure.
142 ASSERT(!web_view_);
143 web_view_ = WebView::create(NULL);
144 WebPreferences().Apply(web_view_);
145 web_view_->initializeMainFrame(WorkerWebFrameClient::GetSharedInstance());
146
147 WebFrameImpl* web_frame = static_cast<WebFrameImpl*>(web_view_->mainFrame());
148
149 // Construct substitute data source for the 'shadow page'. We only need it
150 // to have same origin as the worker so the loading checks work correctly.
151 WebCore::CString content("");
152 int len = static_cast<int>(content.length());
153 RefPtr<WebCore::SharedBuffer> buf(
154 WebCore::SharedBuffer::create(content.data(), len));
155 WebCore::SubstituteData subst_data(buf,
156 WebCore::String("text/html"),
157 WebCore::String("UTF-8"),
158 WebCore::KURL());
159 WebCore::ResourceRequest request(webkit_glue::GURLToKURL(script_url),
160 WebCore::CString());
161 web_frame->frame()->loader()->load(request, subst_data, false);
162
163 // This document will be used as 'loading context' for the worker.
164 loading_document_ = web_frame->frame()->document();
165
166 worker_thread_ = WebCore::DedicatedWorkerThread::create(
167 webkit_glue::WebURLToKURL(script_url),
168 webkit_glue::WebStringToString(user_agent),
169 webkit_glue::WebStringToString(source_code),
170 *this,
171 *this);
172
173 // Worker initialization means a pending activity.
174 reportPendingActivity(true);
175
176 worker_thread_->start();
177 }
178
179 void WebWorkerImpl::terminateWorkerContext() {
180 if (asked_to_terminate_)
181 return;
182 asked_to_terminate_ = true;
183
184 if (worker_thread_)
185 worker_thread_->stop();
186 }
187
188 void WebWorkerImpl::postMessageToWorkerContext(
189 const WebString& message,
190 const WebMessagePortChannelArray& webchannels) {
191
192 WTF::OwnPtr<WebCore::MessagePortChannelArray> channels;
193 if (webchannels.size()) {
194 channels = new WebCore::MessagePortChannelArray(webchannels.size());
195 for (size_t i = 0; i < webchannels.size(); ++i) {
196 RefPtr<WebCore::PlatformMessagePortChannel> platform_channel =
197 WebCore::PlatformMessagePortChannel::create(webchannels[i]);
198 webchannels[i]->setClient(platform_channel.get());
199 (*channels)[i] = WebCore::MessagePortChannel::create(platform_channel);
200 }
201 }
202
203 worker_thread_->runLoop().postTask(WebCore::createCallbackTask(
204 &PostMessageToWorkerContextTask,
205 this,
206 webkit_glue::WebStringToString(message),
207 channels.release()));
208 }
209
210 void WebWorkerImpl::workerObjectDestroyed() {
211 // Worker object in the renderer was destroyed, perhaps a result of GC.
212 // For us, it's a signal to start terminating the WorkerContext too.
213 // TODO(dimich): when 'kill a worker' html5 spec algorithm is implemented, it
214 // should be used here instead of 'terminate a worker'.
215 terminateWorkerContext();
216 }
217
218 void WebWorkerImpl::clientDestroyed() {
219 client_ = NULL;
220 }
221
222 void WebWorkerImpl::DispatchTaskToMainThread(
223 PassRefPtr<WebCore::ScriptExecutionContext::Task> task) {
224 return WTF::callOnMainThread(InvokeTaskMethod, task.releaseRef());
225 }
226
227 void WebWorkerImpl::InvokeTaskMethod(void* param) {
228 WebCore::ScriptExecutionContext::Task* task =
229 static_cast<WebCore::ScriptExecutionContext::Task*>(param);
230 task->performTask(NULL);
231 task->deref();
232 }
233
234 // WorkerObjectProxy -----------------------------------------------------------
235
236 void WebWorkerImpl::postMessageToWorkerObject(
237 WTF::PassRefPtr<WebCore::SerializedScriptValue> message,
238 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) {
239 DispatchTaskToMainThread(WebCore::createCallbackTask(
240 &PostMessageTask,
241 this,
242 message->toString(),
243 channels));
244 }
245
246 void WebWorkerImpl::PostMessageTask(
247 WebCore::ScriptExecutionContext* context,
248 WebWorkerImpl* this_ptr,
249 WebCore::String message,
250 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) {
251 if (!this_ptr->client_)
252 return;
253
254 WebMessagePortChannelArray web_channels(
255 channels.get() ? channels->size() : 0);
256 for (size_t i = 0; i < web_channels.size(); ++i) {
257 web_channels[i] = (*channels)[i]->channel()->webChannelRelease();
258 web_channels[i]->setClient(0);
259 }
260
261 this_ptr->client_->postMessageToWorkerObject(
262 webkit_glue::StringToWebString(message), web_channels);
263 }
264
265 void WebWorkerImpl::postExceptionToWorkerObject(
266 const WebCore::String& error_message,
267 int line_number,
268 const WebCore::String& source_url) {
269 DispatchTaskToMainThread(WebCore::createCallbackTask(
270 &PostExceptionTask,
271 this,
272 error_message,
273 line_number,
274 source_url));
275 }
276
277 void WebWorkerImpl::PostExceptionTask(
278 WebCore::ScriptExecutionContext* context,
279 WebWorkerImpl* this_ptr,
280 const WebCore::String& error_message,
281 int line_number,
282 const WebCore::String& source_url) {
283 if (!this_ptr->client_)
284 return;
285
286 this_ptr->client_->postExceptionToWorkerObject(
287 webkit_glue::StringToWebString(error_message),
288 line_number,
289 webkit_glue::StringToWebString(source_url));
290 }
291
292 void WebWorkerImpl::postConsoleMessageToWorkerObject(
293 WebCore::MessageDestination destination,
294 WebCore::MessageSource source,
295 WebCore::MessageType type,
296 WebCore::MessageLevel level,
297 const WebCore::String& message,
298 int line_number,
299 const WebCore::String& source_url) {
300 DispatchTaskToMainThread(WebCore::createCallbackTask(
301 &PostConsoleMessageTask,
302 this,
303 static_cast<int>(destination),
304 static_cast<int>(source),
305 static_cast<int>(type),
306 static_cast<int>(level),
307 message,
308 line_number,
309 source_url));
310 }
311
312 void WebWorkerImpl::PostConsoleMessageTask(
313 WebCore::ScriptExecutionContext* context,
314 WebWorkerImpl* this_ptr,
315 int destination,
316 int source,
317 int type,
318 int level,
319 const WebCore::String& message,
320 int line_number,
321 const WebCore::String& source_url) {
322 if (!this_ptr->client_)
323 return;
324
325 this_ptr->client_->postConsoleMessageToWorkerObject(
326 destination,
327 source,
328 type,
329 level,
330 webkit_glue::StringToWebString(message),
331 line_number,
332 webkit_glue::StringToWebString(source_url));
333 }
334
335 void WebWorkerImpl::confirmMessageFromWorkerObject(bool has_pending_activity) {
336 DispatchTaskToMainThread(WebCore::createCallbackTask(
337 &ConfirmMessageTask,
338 this,
339 has_pending_activity));
340 }
341
342 void WebWorkerImpl::ConfirmMessageTask(
343 WebCore::ScriptExecutionContext* context,
344 WebWorkerImpl* this_ptr,
345 bool has_pending_activity) {
346 if (!this_ptr->client_)
347 return;
348
349 this_ptr->client_->confirmMessageFromWorkerObject(has_pending_activity);
350 }
351
352 void WebWorkerImpl::reportPendingActivity(bool has_pending_activity) {
353 DispatchTaskToMainThread(WebCore::createCallbackTask(
354 &ReportPendingActivityTask,
355 this,
356 has_pending_activity));
357 }
358
359 void WebWorkerImpl::ReportPendingActivityTask(
360 WebCore::ScriptExecutionContext* context,
361 WebWorkerImpl* this_ptr,
362 bool has_pending_activity) {
363 if (!this_ptr->client_)
364 return;
365
366 this_ptr->client_->reportPendingActivity(has_pending_activity);
367 }
368
369 void WebWorkerImpl::workerContextDestroyed() {
370 DispatchTaskToMainThread(WebCore::createCallbackTask(
371 &WorkerContextDestroyedTask,
372 this));
373 }
374
375 // WorkerLoaderProxy -----------------------------------------------------------
376
377 void WebWorkerImpl::postTaskToLoader(
378 PassRefPtr<WebCore::ScriptExecutionContext::Task> task) {
379 ASSERT(loading_document_->isDocument());
380 loading_document_->postTask(task);
381 }
382
383 void WebWorkerImpl::postTaskForModeToWorkerContext(
384 PassRefPtr<WebCore::ScriptExecutionContext::Task> task,
385 const WebCore::String& mode) {
386 worker_thread_->runLoop().postTaskForMode(task, mode);
387 }
388
389 void WebWorkerImpl::WorkerContextDestroyedTask(
390 WebCore::ScriptExecutionContext* context,
391 WebWorkerImpl* this_ptr) {
392 if (this_ptr->client_)
393 this_ptr->client_->workerContextDestroyed();
394
395 // The lifetime of this proxy is controlled by the worker context.
396 delete this_ptr;
397 }
398
399 #else
400
401 namespace WebKit {
402
403 WebWorker* WebWorker::create(WebWorkerClient* client) {
404 return NULL;
405 }
406
407 }
408
409 #endif
OLDNEW
« no previous file with comments | « webkit/glue/webworker_impl.h ('k') | webkit/glue/webworkerclient_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698