| OLD | NEW |
| (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 #if ENABLE(WORKERS) | |
| 8 | |
| 9 #include "DedicatedWorkerThread.h" | |
| 10 #include "ErrorEvent.h" | |
| 11 #include "Frame.h" | |
| 12 #include "FrameLoaderClient.h" | |
| 13 #include "GenericWorkerTask.h" | |
| 14 #include "MessageEvent.h" | |
| 15 #include "MessagePort.h" | |
| 16 #include "MessagePortChannel.h" | |
| 17 #include "ScriptExecutionContext.h" | |
| 18 #include "Worker.h" | |
| 19 #include "WorkerContext.h" | |
| 20 #include "WorkerContextExecutionProxy.h" | |
| 21 #include "WorkerMessagingProxy.h" | |
| 22 #include <wtf/Threading.h> | |
| 23 | |
| 24 #undef LOG | |
| 25 | |
| 26 #include "webkit/glue/webworkerclient_impl.h" | |
| 27 | |
| 28 #include "base/command_line.h" | |
| 29 #include "webkit/api/public/WebFrameClient.h" | |
| 30 #include "webkit/api/public/WebKit.h" | |
| 31 #include "webkit/api/public/WebKitClient.h" | |
| 32 #include "webkit/api/public/WebMessagePortChannel.h" | |
| 33 #include "webkit/api/public/WebString.h" | |
| 34 #include "webkit/api/public/WebURL.h" | |
| 35 #include "webkit/api/public/WebWorker.h" | |
| 36 #include "webkit/api/src/PlatformMessagePortChannel.h" | |
| 37 #include "webkit/glue/glue_util.h" | |
| 38 #include "webkit/glue/webframeloaderclient_impl.h" | |
| 39 #include "webkit/glue/webframe_impl.h" | |
| 40 #include "webkit/glue/webkitclient_impl.h" | |
| 41 #include "webkit/glue/webview_impl.h" | |
| 42 #include "webkit/glue/webworker_impl.h" | |
| 43 | |
| 44 using WebKit::WebFrameClient; | |
| 45 using WebKit::WebMessagePortChannel; | |
| 46 using WebKit::WebMessagePortChannelArray; | |
| 47 using WebKit::WebString; | |
| 48 using WebKit::WebWorker; | |
| 49 using WebKit::WebWorkerClient; | |
| 50 | |
| 51 // When WebKit creates a WorkerContextProxy object, we check if we're in the | |
| 52 // renderer or worker process. If the latter, then we just use | |
| 53 // WebCore::WorkerMessagingProxy. | |
| 54 // | |
| 55 // If we're in the renderer process, then we need use the glue provided | |
| 56 // WebWorker object to talk to the worker process over IPC. The worker process | |
| 57 // talks to WebCore::Worker* using WorkerObjectProxy, which we implement on | |
| 58 // WebWorkerClientImpl. | |
| 59 // | |
| 60 // Note that if we're running each worker in a separate process, then nested | |
| 61 // workers end up using the same codepath as the renderer process. | |
| 62 | |
| 63 // static | |
| 64 WebCore::WorkerContextProxy* WebWorkerClientImpl::createWorkerContextProxy( | |
| 65 WebCore::Worker* worker) { | |
| 66 if (!worker->scriptExecutionContext()->isDocument() && | |
| 67 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 68 "web-worker-share-processes")) { | |
| 69 return new WebCore::WorkerMessagingProxy(worker); | |
| 70 } | |
| 71 | |
| 72 WebWorker* webworker = NULL; | |
| 73 WebWorkerClientImpl* proxy = new WebWorkerClientImpl(worker); | |
| 74 | |
| 75 if (worker->scriptExecutionContext()->isDocument()) { | |
| 76 WebCore::Document* document = static_cast<WebCore::Document*>( | |
| 77 worker->scriptExecutionContext()); | |
| 78 WebFrameImpl* webframe = WebFrameImpl::FromFrame(document->frame()); | |
| 79 webworker = webframe->client()->createWorker(webframe, proxy); | |
| 80 } else { | |
| 81 WebCore::WorkerContextExecutionProxy* current_context = | |
| 82 WebCore::WorkerContextExecutionProxy::retrieve(); | |
| 83 if (!current_context) { | |
| 84 NOTREACHED(); | |
| 85 return NULL; | |
| 86 } | |
| 87 | |
| 88 WebCore::DedicatedWorkerThread* thread = | |
| 89 static_cast<WebCore::DedicatedWorkerThread*>( | |
| 90 current_context->workerContext()->thread()); | |
| 91 WebCore::WorkerObjectProxy* worker_object_proxy = | |
| 92 &thread->workerObjectProxy(); | |
| 93 WebWorkerImpl* impl = reinterpret_cast<WebWorkerImpl*>(worker_object_proxy); | |
| 94 webworker = impl->client()->createWorker(proxy); | |
| 95 } | |
| 96 | |
| 97 proxy->set_webworker(webworker); | |
| 98 return proxy; | |
| 99 } | |
| 100 | |
| 101 WebWorkerClientImpl::WebWorkerClientImpl(WebCore::Worker* worker) | |
| 102 : script_execution_context_(worker->scriptExecutionContext()), | |
| 103 worker_(worker), | |
| 104 asked_to_terminate_(false), | |
| 105 unconfirmed_message_count_(0), | |
| 106 worker_context_had_pending_activity_(false), | |
| 107 worker_thread_id_(WTF::currentThread()) { | |
| 108 } | |
| 109 | |
| 110 WebWorkerClientImpl::~WebWorkerClientImpl() { | |
| 111 } | |
| 112 | |
| 113 void WebWorkerClientImpl::set_webworker(WebWorker* webworker) { | |
| 114 webworker_ = webworker; | |
| 115 } | |
| 116 | |
| 117 void WebWorkerClientImpl::startWorkerContext( | |
| 118 const WebCore::KURL& script_url, | |
| 119 const WebCore::String& user_agent, | |
| 120 const WebCore::String& source_code) { | |
| 121 // Worker.terminate() could be called from JS before the context is started. | |
| 122 if (asked_to_terminate_) | |
| 123 return; | |
| 124 | |
| 125 if (!WTF::isMainThread()) { | |
| 126 WebWorkerImpl::DispatchTaskToMainThread( | |
| 127 WebCore::createCallbackTask(&StartWorkerContextTask, this, | |
| 128 script_url.string(), user_agent, source_code)); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 webworker_->startWorkerContext( | |
| 133 webkit_glue::KURLToWebURL(script_url), | |
| 134 webkit_glue::StringToWebString(user_agent), | |
| 135 webkit_glue::StringToWebString(source_code)); | |
| 136 } | |
| 137 | |
| 138 void WebWorkerClientImpl::terminateWorkerContext() { | |
| 139 if (asked_to_terminate_) | |
| 140 return; | |
| 141 | |
| 142 asked_to_terminate_ = true; | |
| 143 | |
| 144 if (!WTF::isMainThread()) { | |
| 145 WebWorkerImpl::DispatchTaskToMainThread( | |
| 146 WebCore::createCallbackTask(&TerminateWorkerContextTask, this)); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 webworker_->terminateWorkerContext(); | |
| 151 } | |
| 152 | |
| 153 void WebWorkerClientImpl::postMessageToWorkerContext( | |
| 154 WTF::PassRefPtr<WebCore::SerializedScriptValue> message, | |
| 155 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) { | |
| 156 // Worker.terminate() could be called from JS before the context is started. | |
| 157 if (asked_to_terminate_) | |
| 158 return; | |
| 159 | |
| 160 ++unconfirmed_message_count_; | |
| 161 | |
| 162 if (!WTF::isMainThread()) { | |
| 163 WebWorkerImpl::DispatchTaskToMainThread( | |
| 164 WebCore::createCallbackTask( | |
| 165 &PostMessageToWorkerContextTask, | |
| 166 this, | |
| 167 message->toString(), | |
| 168 channels)); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 WebMessagePortChannelArray webchannels(channels.get() ? channels->size() : 0); | |
| 173 | |
| 174 for (size_t i = 0; i < webchannels.size(); ++i) { | |
| 175 WebMessagePortChannel* webchannel = | |
| 176 (*channels)[i]->channel()->webChannelRelease(); | |
| 177 webchannel->setClient(0); | |
| 178 webchannels[i] = webchannel; | |
| 179 } | |
| 180 | |
| 181 webworker_->postMessageToWorkerContext( | |
| 182 webkit_glue::StringToWebString(message->toString()), webchannels); | |
| 183 } | |
| 184 | |
| 185 bool WebWorkerClientImpl::hasPendingActivity() const { | |
| 186 return !asked_to_terminate_ && | |
| 187 (unconfirmed_message_count_ || worker_context_had_pending_activity_); | |
| 188 } | |
| 189 | |
| 190 void WebWorkerClientImpl::workerObjectDestroyed() { | |
| 191 if (WTF::isMainThread()) { | |
| 192 webworker_->workerObjectDestroyed(); | |
| 193 worker_ = NULL; | |
| 194 } | |
| 195 | |
| 196 // Even if this is called on the main thread, there could be a queued task for | |
| 197 // this object, so don't delete it right away. | |
| 198 WebWorkerImpl::DispatchTaskToMainThread( | |
| 199 WebCore::createCallbackTask(&WorkerObjectDestroyedTask, this)); | |
| 200 } | |
| 201 | |
| 202 void WebWorkerClientImpl::postMessageToWorkerObject( | |
| 203 const WebString& message, | |
| 204 const WebMessagePortChannelArray& channels) { | |
| 205 WebCore::String message2 = webkit_glue::WebStringToString(message); | |
| 206 OwnPtr<WebCore::MessagePortChannelArray> channels2; | |
| 207 if (channels.size()) { | |
| 208 channels2 = new WebCore::MessagePortChannelArray(channels.size()); | |
| 209 for (size_t i = 0; i < channels.size(); ++i) { | |
| 210 RefPtr<WebCore::PlatformMessagePortChannel> platform_channel = | |
| 211 WebCore::PlatformMessagePortChannel::create(channels[i]); | |
| 212 channels[i]->setClient(platform_channel.get()); | |
| 213 (*channels2)[i] = WebCore::MessagePortChannel::create(platform_channel); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 if (WTF::currentThread() != worker_thread_id_) { | |
| 218 script_execution_context_->postTask( | |
| 219 WebCore::createCallbackTask(&PostMessageToWorkerObjectTask, this, | |
| 220 message2, channels2.release())); | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 PostMessageToWorkerObjectTask( | |
| 225 script_execution_context_.get(), this, message2, channels2.release()); | |
| 226 } | |
| 227 | |
| 228 void WebWorkerClientImpl::postExceptionToWorkerObject( | |
| 229 const WebString& error_message, | |
| 230 int line_number, | |
| 231 const WebString& source_url) { | |
| 232 if (WTF::currentThread() != worker_thread_id_) { | |
| 233 script_execution_context_->postTask( | |
| 234 WebCore::createCallbackTask(&PostExceptionToWorkerObjectTask, this, | |
| 235 webkit_glue::WebStringToString(error_message), | |
| 236 line_number, | |
| 237 webkit_glue::WebStringToString(source_url))); | |
| 238 return; | |
| 239 } | |
| 240 | |
| 241 bool handled = false; | |
| 242 handled = worker_->dispatchEvent( | |
| 243 WebCore::ErrorEvent::create(webkit_glue::WebStringToString(error_message), | |
| 244 webkit_glue::WebStringToString(source_url), | |
| 245 line_number)); | |
| 246 if (!handled) | |
| 247 script_execution_context_->reportException( | |
| 248 webkit_glue::WebStringToString(error_message), | |
| 249 line_number, | |
| 250 webkit_glue::WebStringToString(source_url)); | |
| 251 } | |
| 252 | |
| 253 void WebWorkerClientImpl::postConsoleMessageToWorkerObject( | |
| 254 int destination_id, | |
| 255 int source_id, | |
| 256 int message_type, | |
| 257 int message_level, | |
| 258 const WebString& message, | |
| 259 int line_number, | |
| 260 const WebString& source_url) { | |
| 261 if (WTF::currentThread() != worker_thread_id_) { | |
| 262 script_execution_context_->postTask( | |
| 263 WebCore::createCallbackTask(&PostConsoleMessageToWorkerObjectTask, this, | |
| 264 destination_id, source_id, message_type, message_level, | |
| 265 webkit_glue::WebStringToString(message), | |
| 266 line_number, | |
| 267 webkit_glue::WebStringToString(source_url))); | |
| 268 return; | |
| 269 } | |
| 270 | |
| 271 script_execution_context_->addMessage( | |
| 272 static_cast<WebCore::MessageDestination>(destination_id), | |
| 273 static_cast<WebCore::MessageSource>(source_id), | |
| 274 static_cast<WebCore::MessageType>(message_type), | |
| 275 static_cast<WebCore::MessageLevel>(message_level), | |
| 276 webkit_glue::WebStringToString(message), | |
| 277 line_number, | |
| 278 webkit_glue::WebStringToString(source_url)); | |
| 279 } | |
| 280 | |
| 281 void WebWorkerClientImpl::confirmMessageFromWorkerObject( | |
| 282 bool has_pending_activity) { | |
| 283 // unconfirmed_message_count_ can only be updated on the thread where it's | |
| 284 // accessed. Otherwise there are race conditions with v8's garbage | |
| 285 // collection. | |
| 286 script_execution_context_->postTask( | |
| 287 WebCore::createCallbackTask(&ConfirmMessageFromWorkerObjectTask, this)); | |
| 288 } | |
| 289 | |
| 290 void WebWorkerClientImpl::reportPendingActivity(bool has_pending_activity) { | |
| 291 // See above comment in confirmMessageFromWorkerObject. | |
| 292 script_execution_context_->postTask( | |
| 293 WebCore::createCallbackTask(&ReportPendingActivityTask, this, | |
| 294 has_pending_activity)); | |
| 295 } | |
| 296 | |
| 297 void WebWorkerClientImpl::workerContextDestroyed() { | |
| 298 } | |
| 299 | |
| 300 void WebWorkerClientImpl::StartWorkerContextTask( | |
| 301 WebCore::ScriptExecutionContext* context, | |
| 302 WebWorkerClientImpl* this_ptr, | |
| 303 const WebCore::String& script_url, | |
| 304 const WebCore::String& user_agent, | |
| 305 const WebCore::String& source_code) { | |
| 306 this_ptr->webworker_->startWorkerContext( | |
| 307 webkit_glue::KURLToWebURL( | |
| 308 WebCore::KURL(WebCore::ParsedURLString, script_url)), | |
| 309 webkit_glue::StringToWebString(user_agent), | |
| 310 webkit_glue::StringToWebString(source_code)); | |
| 311 } | |
| 312 | |
| 313 void WebWorkerClientImpl::TerminateWorkerContextTask( | |
| 314 WebCore::ScriptExecutionContext* context, | |
| 315 WebWorkerClientImpl* this_ptr) { | |
| 316 this_ptr->webworker_->terminateWorkerContext(); | |
| 317 } | |
| 318 | |
| 319 void WebWorkerClientImpl::PostMessageToWorkerContextTask( | |
| 320 WebCore::ScriptExecutionContext* context, | |
| 321 WebWorkerClientImpl* this_ptr, | |
| 322 const WebCore::String& message, | |
| 323 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) { | |
| 324 WebMessagePortChannelArray web_channels(channels.get() ? channels->size() : 0)
; | |
| 325 | |
| 326 for (size_t i = 0; i < web_channels.size(); ++i) { | |
| 327 web_channels[i] = (*channels)[i]->channel()->webChannelRelease(); | |
| 328 web_channels[i]->setClient(0); | |
| 329 } | |
| 330 | |
| 331 this_ptr->webworker_->postMessageToWorkerContext( | |
| 332 webkit_glue::StringToWebString(message), web_channels); | |
| 333 } | |
| 334 | |
| 335 void WebWorkerClientImpl::WorkerObjectDestroyedTask( | |
| 336 WebCore::ScriptExecutionContext* context, | |
| 337 WebWorkerClientImpl* this_ptr) { | |
| 338 if (this_ptr->worker_) // Check we haven't alread called this. | |
| 339 this_ptr->webworker_->workerObjectDestroyed(); | |
| 340 delete this_ptr; | |
| 341 } | |
| 342 | |
| 343 void WebWorkerClientImpl::PostMessageToWorkerObjectTask( | |
| 344 WebCore::ScriptExecutionContext* context, | |
| 345 WebWorkerClientImpl* this_ptr, | |
| 346 const WebCore::String& message, | |
| 347 WTF::PassOwnPtr<WebCore::MessagePortChannelArray> channels) { | |
| 348 | |
| 349 if (this_ptr->worker_) { | |
| 350 WTF::OwnPtr<WebCore::MessagePortArray> ports = | |
| 351 WebCore::MessagePort::entanglePorts(*context, channels.release()); | |
| 352 WTF::RefPtr<WebCore::SerializedScriptValue> serialized_message = | |
| 353 WebCore::SerializedScriptValue::create(message); | |
| 354 this_ptr->worker_->dispatchEvent( | |
| 355 WebCore::MessageEvent::create(ports.release(), | |
| 356 serialized_message.release())); | |
| 357 } | |
| 358 } | |
| 359 | |
| 360 void WebWorkerClientImpl::PostExceptionToWorkerObjectTask( | |
| 361 WebCore::ScriptExecutionContext* context, | |
| 362 WebWorkerClientImpl* this_ptr, | |
| 363 const WebCore::String& error_message, | |
| 364 int line_number, | |
| 365 const WebCore::String& source_url) { | |
| 366 bool handled = false; | |
| 367 if (this_ptr->worker_) | |
| 368 handled = this_ptr->worker_->dispatchEvent( | |
| 369 WebCore::ErrorEvent::create(error_message, source_url, line_number)); | |
| 370 if (!handled) | |
| 371 this_ptr->script_execution_context_->reportException( | |
| 372 error_message, line_number, source_url); | |
| 373 } | |
| 374 | |
| 375 void WebWorkerClientImpl::PostConsoleMessageToWorkerObjectTask( | |
| 376 WebCore::ScriptExecutionContext* context, | |
| 377 WebWorkerClientImpl* this_ptr, | |
| 378 int destination_id, | |
| 379 int source_id, | |
| 380 int message_type, | |
| 381 int message_level, | |
| 382 const WebCore::String& message, | |
| 383 int line_number, | |
| 384 const WebCore::String& source_url) { | |
| 385 this_ptr->script_execution_context_->addMessage( | |
| 386 static_cast<WebCore::MessageDestination>(destination_id), | |
| 387 static_cast<WebCore::MessageSource>(source_id), | |
| 388 static_cast<WebCore::MessageType>(message_type), | |
| 389 static_cast<WebCore::MessageLevel>(message_level), | |
| 390 message, | |
| 391 line_number, | |
| 392 source_url); | |
| 393 } | |
| 394 | |
| 395 void WebWorkerClientImpl::ConfirmMessageFromWorkerObjectTask( | |
| 396 WebCore::ScriptExecutionContext* context, | |
| 397 WebWorkerClientImpl* this_ptr) { | |
| 398 this_ptr->unconfirmed_message_count_--; | |
| 399 } | |
| 400 | |
| 401 void WebWorkerClientImpl::ReportPendingActivityTask( | |
| 402 WebCore::ScriptExecutionContext* context, | |
| 403 WebWorkerClientImpl* this_ptr, | |
| 404 bool has_pending_activity) { | |
| 405 this_ptr->worker_context_had_pending_activity_ = has_pending_activity; | |
| 406 } | |
| 407 | |
| 408 #endif | |
| OLD | NEW |