summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-05 22:06:43 +0000
committerjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-05 22:06:43 +0000
commitd9056166ac600fd4c261d81a9da1f5ba855e6bd8 (patch)
tree148e4e04c47a1c0b65dbd3f022040259e85acfb9
parent1a616d92fc1bdafcceda50939e6a5b72f4885a98 (diff)
downloadchromium_src-d9056166ac600fd4c261d81a9da1f5ba855e6bd8.zip
chromium_src-d9056166ac600fd4c261d81a9da1f5ba855e6bd8.tar.gz
chromium_src-d9056166ac600fd4c261d81a9da1f5ba855e6bd8.tar.bz2
Hook up WebKit worker with Chromium.
Review URL: http://codereview.chromium.org/39147 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11046 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/renderer/webworker_proxy.cc10
-rw-r--r--chrome/renderer/webworker_proxy.h5
-rw-r--r--chrome/worker/webworkerclient_proxy.cc15
-rw-r--r--chrome/worker/webworkerclient_proxy.h8
-rw-r--r--chrome/worker/worker.scons3
-rw-r--r--chrome/worker/worker.vcproj12
-rw-r--r--chrome/worker/worker_thread.cc20
-rw-r--r--chrome/worker/worker_thread.h7
-rw-r--r--chrome/worker/worker_webkitclient_impl.cc29
-rw-r--r--chrome/worker/worker_webkitclient_impl.h23
-rw-r--r--webkit/glue/webworker_impl.cc44
-rw-r--r--webkit/glue/webworker_impl.h14
-rw-r--r--webkit/glue/webworkerclient_impl.cc32
-rw-r--r--webkit/glue/webworkerclient_impl.h3
14 files changed, 187 insertions, 38 deletions
diff --git a/chrome/renderer/webworker_proxy.cc b/chrome/renderer/webworker_proxy.cc
index cf6477a..0da55f3 100644
--- a/chrome/renderer/webworker_proxy.cc
+++ b/chrome/renderer/webworker_proxy.cc
@@ -28,6 +28,12 @@ void WebWorkerProxy::StartWorkerContext(
RenderThread::current()->AddRoute(route_id_, this);
Send(new WorkerMsg_StartWorkerContext(
route_id_, script_url, user_agent, source_code));
+
+ for (size_t i = 0; i < queued_messages_.size(); ++i) {
+ queued_messages_[i]->set_routing_id(route_id_);
+ Send(queued_messages_[i]);
+ }
+ queued_messages_.clear();
}
void WebWorkerProxy::TerminateWorkerContext() {
@@ -50,8 +56,8 @@ void WebWorkerProxy::WorkerObjectDestroyed() {
bool WebWorkerProxy::Send(IPC::Message* message) {
if (route_id_ == MSG_ROUTING_NONE) {
- delete message;
- return false;
+ queued_messages_.push_back(message);
+ return true;
}
// For now we proxy all messages to the worker process through the browser.
diff --git a/chrome/renderer/webworker_proxy.h b/chrome/renderer/webworker_proxy.h
index 97a6bb3..e068e8e 100644
--- a/chrome/renderer/webworker_proxy.h
+++ b/chrome/renderer/webworker_proxy.h
@@ -5,6 +5,8 @@
#ifndef CHROME_RENDERER_WEBWORKER_PROXY_H_
#define CHROME_RENDERER_WEBWORKER_PROXY_H_
+#include <vector>
+
#include "base/basictypes.h"
#include "chrome/common/ipc_channel.h"
#include "webkit/glue/webworker.h"
@@ -49,6 +51,9 @@ class WebWorkerProxy : public WebWorker,
// messages.
WebWorkerClient* client_;
+ // Stores messages that were sent before the StartWorkerContext message.
+ std::vector<IPC::Message*> queued_messages_;
+
DISALLOW_COPY_AND_ASSIGN(WebWorkerProxy);
};
diff --git a/chrome/worker/webworkerclient_proxy.cc b/chrome/worker/webworkerclient_proxy.cc
index fc8c2d3..9e3917c0 100644
--- a/chrome/worker/webworkerclient_proxy.cc
+++ b/chrome/worker/webworkerclient_proxy.cc
@@ -14,7 +14,6 @@
WebWorkerClientProxy::WebWorkerClientProxy(const GURL& url, int route_id)
: url_(url),
route_id_(route_id),
- started_worker_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(impl_(WebWorker::Create(this))) {
WorkerThread::current()->AddRoute(route_id_, this);
WorkerProcess::current()->AddRefProcess();
@@ -67,12 +66,6 @@ bool WebWorkerClientProxy::Send(IPC::Message* message) {
}
void WebWorkerClientProxy::OnMessageReceived(const IPC::Message& message) {
- if (!started_worker_ &&
- message.type() != WorkerMsg_StartWorkerContext::ID) {
- queued_messages_.push_back(new IPC::Message(message));
- return;
- }
-
WebWorker* worker = impl_.get();
IPC_BEGIN_MESSAGE_MAP(WebWorkerClientProxy, message)
IPC_MESSAGE_FORWARD(WorkerMsg_StartWorkerContext, worker,
@@ -84,12 +77,4 @@ void WebWorkerClientProxy::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_FORWARD(WorkerMsg_WorkerObjectDestroyed, worker,
WebWorker::WorkerObjectDestroyed)
IPC_END_MESSAGE_MAP()
-
- if (message.type() == WorkerMsg_StartWorkerContext::ID) {
- started_worker_ = true;
- for (size_t i = 0; i < queued_messages_.size(); ++i)
- OnMessageReceived(*queued_messages_[i]);
-
- queued_messages_.clear();
- }
}
diff --git a/chrome/worker/webworkerclient_proxy.h b/chrome/worker/webworkerclient_proxy.h
index c744023..094bdeb 100644
--- a/chrome/worker/webworkerclient_proxy.h
+++ b/chrome/worker/webworkerclient_proxy.h
@@ -5,8 +5,6 @@
#ifndef CHROME_WORKER_WEBWORKERCLIENT_PROXY_H_
#define CHROME_WORKER_WEBWORKERCLIENT_PROXY_H_
-#include <vector>
-
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
#include "chrome/common/ipc_channel.h"
@@ -54,14 +52,8 @@ class WebWorkerClientProxy : public WebWorkerClient,
int route_id_;
- // Whether we've received StartWorkerContext message.
- bool started_worker_;
-
scoped_ptr<WebWorker> impl_;
- // Stores messages that arrived before the StartWorkerContext message.
- std::vector<IPC::Message*> queued_messages_;
-
DISALLOW_COPY_AND_ASSIGN(WebWorkerClientProxy);
};
diff --git a/chrome/worker/worker.scons b/chrome/worker/worker.scons
index e00c413..6b194c4 100644
--- a/chrome/worker/worker.scons
+++ b/chrome/worker/worker.scons
@@ -32,6 +32,8 @@ input_files = ChromeFileList([
'worker_process.h',
'worker_thread.cc',
'worker_thread.h',
+ 'worker_webkitclient_impl.cc',
+ 'worker_webkitclient_impl.h',
'$CHROME_DIR/tools/build/win/precompiled$OBJSUFFIX',
'$CHROME_DIR/tools/build/win/precompiled.h',
])
@@ -48,6 +50,7 @@ if env.Bit('posix'):
'worker_main.cc',
'worker_process.cc',
'worker_thread.cc',
+ 'worker_webkitclient_impl.cc',
)
env.ChromeLibrary('worker', input_files)
diff --git a/chrome/worker/worker.vcproj b/chrome/worker/worker.vcproj
index ba82a39..08b43d1 100644
--- a/chrome/worker/worker.vcproj
+++ b/chrome/worker/worker.vcproj
@@ -18,7 +18,7 @@
<Configuration
Name="Debug|Win32"
ConfigurationType="4"
- InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\build\debug.vsprops;..\tools\build\win\precompiled.vsprops"
+ InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\build\debug.vsprops;..\tools\build\win\precompiled.vsprops;..\..\webkit\build\WebKit\using_WebKit.vsprops"
>
<Tool
Name="VCPreBuildEventTool"
@@ -69,7 +69,7 @@
<Configuration
Name="Release|Win32"
ConfigurationType="4"
- InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\build\release.vsprops"
+ InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\build\release.vsprops;..\..\webkit\build\WebKit\using_WebKit.vsprops"
>
<Tool
Name="VCPreBuildEventTool"
@@ -165,6 +165,14 @@
RelativePath=".\worker_thread.h"
>
</File>
+ <File
+ RelativePath=".\worker_webkitclient_impl.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\worker_webkitclient_impl.h"
+ >
+ </File>
</Files>
<Globals>
</Globals>
diff --git a/chrome/worker/worker_thread.cc b/chrome/worker/worker_thread.cc
index daba38b..298f9db 100644
--- a/chrome/worker/worker_thread.cc
+++ b/chrome/worker/worker_thread.cc
@@ -7,6 +7,9 @@
#include "chrome/common/worker_messages.h"
#include "chrome/worker/webworkerclient_proxy.h"
#include "chrome/worker/worker_process.h"
+#include "chrome/worker/worker_webkitclient_impl.h"
+
+#include "WebKit.h"
WorkerThread::WorkerThread()
: ChildThread(base::Thread::Options(MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
@@ -15,6 +18,23 @@ WorkerThread::WorkerThread()
WorkerThread::~WorkerThread() {
}
+void WorkerThread::Init() {
+ ChildThread::Init();
+ webkit_client_.reset(new WorkerWebKitClientImpl);
+ WebKit::initialize(webkit_client_.get());
+}
+
+void WorkerThread::CleanUp() {
+ // Shutdown in reverse of the initialization order.
+
+ if (webkit_client_.get()) {
+ WebKit::shutdown();
+ webkit_client_.reset();
+ }
+
+ ChildThread::CleanUp();
+}
+
void WorkerThread::OnControlMessageReceived(const IPC::Message& msg) {
IPC_BEGIN_MESSAGE_MAP(WorkerThread, msg)
IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateWorker)
diff --git a/chrome/worker/worker_thread.h b/chrome/worker/worker_thread.h
index 25ae2f2..c7f9a09 100644
--- a/chrome/worker/worker_thread.h
+++ b/chrome/worker/worker_thread.h
@@ -9,6 +9,7 @@
#include "chrome/common/child_thread.h"
class GURL;
+class WorkerWebKitClientImpl;
class WorkerThread : public ChildThread {
public:
@@ -23,8 +24,14 @@ class WorkerThread : public ChildThread {
private:
virtual void OnControlMessageReceived(const IPC::Message& msg);
+ // Called by the thread base class
+ virtual void Init();
+ virtual void CleanUp();
+
void OnCreateWorker(const GURL& url, int route_id);
+ scoped_ptr<WorkerWebKitClientImpl> webkit_client_;
+
DISALLOW_COPY_AND_ASSIGN(WorkerThread);
};
diff --git a/chrome/worker/worker_webkitclient_impl.cc b/chrome/worker/worker_webkitclient_impl.cc
new file mode 100644
index 0000000..8bd147c
--- /dev/null
+++ b/chrome/worker/worker_webkitclient_impl.cc
@@ -0,0 +1,29 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
+// source code is governed by a BSD-style license that can be found in the
+// LICENSE file.
+
+#include "chrome/worker/worker_webkitclient_impl.h"
+
+#include "WebString.h"
+#include "WebURL.h"
+
+WebKit::WebMimeRegistry* WorkerWebKitClientImpl::mimeRegistry() {
+ return NULL;
+}
+
+void WorkerWebKitClientImpl::setCookies(const WebKit::WebURL& url,
+ const WebKit::WebURL& policy_url,
+ const WebKit::WebString& value) {
+}
+
+WebKit::WebString WorkerWebKitClientImpl::cookies(
+ const WebKit::WebURL& url, const WebKit::WebURL& policy_url) {
+ return WebKit::WebString();
+}
+
+void WorkerWebKitClientImpl::prefetchHostName(const WebKit::WebString&) {
+}
+
+WebKit::WebString WorkerWebKitClientImpl::defaultLocale() {
+ return WebKit::WebString();
+}
diff --git a/chrome/worker/worker_webkitclient_impl.h b/chrome/worker/worker_webkitclient_impl.h
new file mode 100644
index 0000000..e8b9a73
--- /dev/null
+++ b/chrome/worker/worker_webkitclient_impl.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
+// source code is governed by a BSD-style license that can be found in the
+// LICENSE file.
+
+#ifndef CHROME_WORKER_WORKER_WEBKIT_CLIENT_IMPL_H_
+#define CHROME_WORKER_WORKER_WEBKIT_CLIENT_IMPL_H_
+
+#include "webkit/glue/webkitclient_impl.h"
+
+class WorkerWebKitClientImpl : public webkit_glue::WebKitClientImpl {
+ public:
+ // WebKitClient methods:
+ virtual WebKit::WebMimeRegistry* mimeRegistry();
+ virtual void setCookies(const WebKit::WebURL& url,
+ const WebKit::WebURL& policy_url,
+ const WebKit::WebString& value);
+ virtual WebKit::WebString cookies(const WebKit::WebURL& url,
+ const WebKit::WebURL& policy_url);
+ virtual void prefetchHostName(const WebKit::WebString&);
+ virtual WebKit::WebString defaultLocale();
+};
+
+#endif // CHROME_WORKER_WORKER_WEBKIT_CLIENT_IMPL_H_
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