diff options
Diffstat (limited to 'webkit/glue')
-rw-r--r-- | webkit/glue/webworker_impl.cc | 44 | ||||
-rw-r--r-- | webkit/glue/webworker_impl.h | 14 | ||||
-rw-r--r-- | webkit/glue/webworkerclient_impl.cc | 32 | ||||
-rw-r--r-- | webkit/glue/webworkerclient_impl.h | 3 |
4 files changed, 82 insertions, 11 deletions
diff --git a/webkit/glue/webworker_impl.cc b/webkit/glue/webworker_impl.cc index 3bb5d20..0d6529c 100644 --- a/webkit/glue/webworker_impl.cc +++ b/webkit/glue/webworker_impl.cc @@ -5,12 +5,22 @@ #include "config.h" #include "base/compiler_specific.h" + +#include "GenericWorkerTask.h" +#include "ScriptExecutionContext.h" +#include "WorkerContext.h" +#include "WorkerThread.h" +#include <wtf/Threading.h> + +#undef LOG + +#include "base/logging.h" #include "webkit/glue/glue_util.h" #include "webkit/glue/webworkerclient.h" #include "webkit/glue/webworker_impl.h" -#if ENABLE(WORKERS) +#if ENABLE(WORKERS) WebWorker* WebWorker::Create(WebWorkerClient* client) { return new WebWorkerImpl(client); @@ -23,21 +33,47 @@ WebWorkerImpl::WebWorkerImpl(WebWorkerClient* client) : client_(client) { WebWorkerImpl::~WebWorkerImpl() { } +void WebWorkerImpl::PostMessageToWorkerContextTask( + WebCore::ScriptExecutionContext* context, + WebWorkerImpl* this_ptr, + const WebCore::String& message) { + DCHECK(context->isWorkerContext()); + WebCore::WorkerContext* worker_context = + static_cast<WebCore::WorkerContext*>(context); + worker_context->dispatchMessage(message); + + this_ptr->client_->ConfirmMessageFromWorkerObject( + worker_context->hasPendingActivity()); +} + void WebWorkerImpl::StartWorkerContext(const GURL& script_url, const string16& user_agent, const string16& source_code) { - // TODO(jianli): implement WorkerContextProxy here (i.e. create WorkerThread - // etc). The WebKit code uses worker_object_proxy_ when it wants to talk to - // code running in the renderer process. + worker_thread_ = WebCore::WorkerThread::create( + webkit_glue::GURLToKURL(script_url), + webkit_glue::String16ToString(user_agent), + webkit_glue::String16ToString(source_code), + this); + + // Worker initialization means a pending activity. + reportPendingActivity(true); + + worker_thread_->start(); } void WebWorkerImpl::TerminateWorkerContext() { + worker_thread_->stop(); } void WebWorkerImpl::PostMessageToWorkerContext(const string16& message) { + worker_thread_->runLoop().postTask(WebCore::createCallbackTask( + &PostMessageToWorkerContextTask, + this, + webkit_glue::String16ToString(message))); } void WebWorkerImpl::WorkerObjectDestroyed() { + TerminateWorkerContext(); } void WebWorkerImpl::postMessageToWorkerObject(const WebCore::String& message) { diff --git a/webkit/glue/webworker_impl.h b/webkit/glue/webworker_impl.h index 9903cea..4af1f64 100644 --- a/webkit/glue/webworker_impl.h +++ b/webkit/glue/webworker_impl.h @@ -9,8 +9,16 @@ #if ENABLE(WORKERS) +#include <vector> #include "ScriptExecutionContext.h" #include "WorkerObjectProxy.h" +#include <wtf/RefPtr.h> + +namespace WebCore { +class ScriptExecutionContext; +class Strng; +class WorkerThread; +}; // This class is used by the worker process code to talk to the WebCore::Worker // implementation. It can't use it directly since it uses WebKit types, so this @@ -47,7 +55,13 @@ class WebWorkerImpl: public WebCore::WorkerObjectProxy, void WorkerObjectDestroyed(); private: + static void PostMessageToWorkerContextTask( + WebCore::ScriptExecutionContext* context, + WebWorkerImpl* this_ptr, + const WebCore::String& message); + WebWorkerClient* client_; + WTF::RefPtr<WebCore::WorkerThread> worker_thread_; DISALLOW_COPY_AND_ASSIGN(WebWorkerImpl); }; diff --git a/webkit/glue/webworkerclient_impl.cc b/webkit/glue/webworkerclient_impl.cc index d140681..c75bff4 100644 --- a/webkit/glue/webworkerclient_impl.cc +++ b/webkit/glue/webworkerclient_impl.cc @@ -55,7 +55,10 @@ WebCore::WorkerContextProxy* WebCore::WorkerContextProxy::create( WebWorkerClientImpl::WebWorkerClientImpl(WebCore::Worker* worker) - : worker_(worker) { + : worker_(worker), + asked_to_terminate_(false), + unconfirmed_message_count_(0), + worker_context_had_pending_activity_(false) { } WebWorkerClientImpl::~WebWorkerClientImpl() { @@ -75,20 +78,23 @@ void WebWorkerClientImpl::startWorkerContext( } void WebWorkerClientImpl::terminateWorkerContext() { + if (asked_to_terminate_) + return; + asked_to_terminate_ = true; + webworker_->TerminateWorkerContext(); } void WebWorkerClientImpl::postMessageToWorkerContext( const WebCore::String& message) { + ++unconfirmed_message_count_; webworker_->PostMessageToWorkerContext( webkit_glue::StringToString16(message)); } bool WebWorkerClientImpl::hasPendingActivity() const { - // TODO(jianli): we should use the same logic from WorkerMessagingProxy - // here, so that we don't do a synchronous IPC. - // Until then, always return true. - return true; + return !asked_to_terminate_ && + (unconfirmed_message_count_ || worker_context_had_pending_activity_); } void WebWorkerClientImpl::workerObjectDestroyed() { @@ -96,14 +102,17 @@ void WebWorkerClientImpl::workerObjectDestroyed() { } void WebWorkerClientImpl::PostMessageToWorkerObject(const string16& message) { - // TODO(jianli): this method, and the ones below, need to implement - // WorkerObjectProxy. + worker_->dispatchMessage(webkit_glue::String16ToString(message)); } void WebWorkerClientImpl::PostExceptionToWorkerObject( const string16& error_message, int line_number, const string16& source_url) { + worker_->scriptExecutionContext()->reportException( + webkit_glue::String16ToString(error_message), + line_number, + webkit_glue::String16ToString(source_url)); } void WebWorkerClientImpl::PostConsoleMessageToWorkerObject( @@ -113,12 +122,21 @@ void WebWorkerClientImpl::PostConsoleMessageToWorkerObject( const string16& message, int line_number, const string16& source_url) { + worker_->scriptExecutionContext()->addMessage( + static_cast<WebCore::MessageDestination>(destination), + static_cast<WebCore::MessageSource>(source), + static_cast<WebCore::MessageLevel>(level), + webkit_glue::String16ToString(message), + line_number, + webkit_glue::String16ToString(source_url)); } void WebWorkerClientImpl::ConfirmMessageFromWorkerObject(bool has_pending_activity) { + --unconfirmed_message_count_; } void WebWorkerClientImpl::ReportPendingActivity(bool has_pending_activity) { + worker_context_had_pending_activity_ = has_pending_activity; } void WebWorkerClientImpl::WorkerContextDestroyed() { diff --git a/webkit/glue/webworkerclient_impl.h b/webkit/glue/webworkerclient_impl.h index fc93a3c..4277f4b 100644 --- a/webkit/glue/webworkerclient_impl.h +++ b/webkit/glue/webworkerclient_impl.h @@ -54,6 +54,9 @@ class WebWorkerClientImpl : public WebCore::WorkerContextProxy, WebCore::Worker* worker_; scoped_ptr<WebWorker> webworker_; + bool asked_to_terminate_; + uint32 unconfirmed_message_count_; + bool worker_context_had_pending_activity_; }; #endif |