diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-05 16:27:06 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-05 16:27:06 +0000 |
commit | 6898bbedb15bfddf91ea125e3ffde4b1789d9bf3 (patch) | |
tree | e3950c1d597f704af541572cfc39e9874131d667 | |
parent | 1bbe5f61a331b2fb3cd1c0e18cf3aefa7105e635 (diff) | |
download | chromium_src-6898bbedb15bfddf91ea125e3ffde4b1789d9bf3.zip chromium_src-6898bbedb15bfddf91ea125e3ffde4b1789d9bf3.tar.gz chromium_src-6898bbedb15bfddf91ea125e3ffde4b1789d9bf3.tar.bz2 |
Added beginnings of browser-process support for shared workers.
Refactored WebWorkerClientProxy into two classes - WebWorkerDispatcher which dispatches incoming IPCs for the worker, and WebWorkerClientProxy, which handles outgoing API calls from WebWorkerImpl. This allows WebWorkerClientProxy to be reused by WebSharedWorkerDispatcher.
BUG=26233
TEST=none (will enable layout tests when basic functionality available)
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=31077
Review URL: http://codereview.chromium.org/351004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31096 0039d316-1c4b-4281-b951-d872f2087c98
33 files changed, 648 insertions, 217 deletions
diff --git a/chrome/browser/renderer_host/resource_message_filter.cc b/chrome/browser/renderer_host/resource_message_filter.cc index f817a9b..e386edc 100644 --- a/chrome/browser/renderer_host/resource_message_filter.cc +++ b/chrome/browser/renderer_host/resource_message_filter.cc @@ -308,8 +308,7 @@ bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_OpenChannelToPlugin, OnOpenChannelToPlugin) IPC_MESSAGE_HANDLER(ViewHostMsg_LaunchNaCl, OnLaunchNaCl) - IPC_MESSAGE_HANDLER(ViewHostMsg_CreateDedicatedWorker, - OnCreateDedicatedWorker) + IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWorker, OnCreateWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_CancelCreateDedicatedWorker, OnCancelCreateDedicatedWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToWorker, @@ -647,12 +646,14 @@ void ResourceMessageFilter::OnLaunchNaCl(const std::wstring& url, nacl_process_id); } -void ResourceMessageFilter::OnCreateDedicatedWorker(const GURL& url, - int render_view_route_id, - int* route_id) { +void ResourceMessageFilter::OnCreateWorker(const GURL& url, + bool is_shared, + const string16& name, + int render_view_route_id, + int* route_id) { *route_id = render_widget_helper_->GetNextRoutingID(); - WorkerService::GetInstance()->CreateDedicatedWorker( - url, id(), render_view_route_id, this, id(), *route_id); + WorkerService::GetInstance()->CreateWorker( + url, is_shared, name, id(), render_view_route_id, this, id(), *route_id); } void ResourceMessageFilter::OnCancelCreateDedicatedWorker(int route_id) { diff --git a/chrome/browser/renderer_host/resource_message_filter.h b/chrome/browser/renderer_host/resource_message_filter.h index 82a9383..9d21a4f 100644 --- a/chrome/browser/renderer_host/resource_message_filter.h +++ b/chrome/browser/renderer_host/resource_message_filter.h @@ -166,9 +166,11 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter, nacl::FileDescriptor* handle, base::ProcessHandle* nacl_process_handle, base::ProcessId* nacl_process_id); - void OnCreateDedicatedWorker(const GURL& url, - int render_view_route_id, - int* route_id); + void OnCreateWorker(const GURL& url, + bool is_shared, + const string16& name, + int render_view_route_id, + int* route_id); void OnCancelCreateDedicatedWorker(int route_id); void OnForwardToWorker(const IPC::Message& msg); void OnDownloadUrl(const IPC::Message& message, diff --git a/chrome/browser/worker_host/worker_process_host.cc b/chrome/browser/worker_host/worker_process_host.cc index eb6d2fe..02e3cc6 100644 --- a/chrome/browser/worker_host/worker_process_host.cc +++ b/chrome/browser/worker_host/worker_process_host.cc @@ -139,8 +139,10 @@ void WorkerProcessHost::CreateWorker(const WorkerInstance& instance) { id(), instance.url); instances_.push_back(instance); - Send(new WorkerProcessMsg_CreateWorker( - instance.url, instance.worker_route_id)); + Send(new WorkerProcessMsg_CreateWorker(instance.url, + instance.is_shared, + instance.name, + instance.worker_route_id)); UpdateTitle(); instances_.back().sender->Send( @@ -175,8 +177,7 @@ void WorkerProcessHost::OnMessageReceived(const IPC::Message& message) { if (!handled) { handled = true; IPC_BEGIN_MESSAGE_MAP_EX(WorkerProcessHost, message, msg_is_ok) - IPC_MESSAGE_HANDLER(ViewHostMsg_CreateDedicatedWorker, - OnCreateDedicatedWorker) + IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWorker, OnCreateWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_CancelCreateDedicatedWorker, OnCancelCreateDedicatedWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToWorker, @@ -321,13 +322,15 @@ void WorkerProcessHost::UpdateTitle() { set_name(ASCIIToWide(display_title)); } -void WorkerProcessHost::OnCreateDedicatedWorker(const GURL& url, - int render_view_route_id, - int* route_id) { +void WorkerProcessHost::OnCreateWorker(const GURL& url, + bool is_shared, + const string16& name, + int render_view_route_id, + int* route_id) { DCHECK(instances_.size() == 1); // Only called when one process per worker. *route_id = WorkerService::GetInstance()->next_worker_route_id(); - WorkerService::GetInstance()->CreateDedicatedWorker( - url, instances_.front().renderer_id, + WorkerService::GetInstance()->CreateWorker( + url, is_shared, name, instances_.front().renderer_id, instances_.front().render_view_route_id, this, id(), *route_id); } diff --git a/chrome/browser/worker_host/worker_process_host.h b/chrome/browser/worker_host/worker_process_host.h index 65f4999..1fae373 100644 --- a/chrome/browser/worker_host/worker_process_host.h +++ b/chrome/browser/worker_host/worker_process_host.h @@ -19,6 +19,8 @@ class WorkerProcessHost : public ChildProcessHost { // between the renderer and worker processes. struct WorkerInstance { GURL url; + bool is_shared; + string16 name; int renderer_id; int render_view_route_id; int worker_route_id; @@ -73,9 +75,11 @@ class WorkerProcessHost : public ChildProcessHost { // Updates the title shown in the task manager. void UpdateTitle(); - void OnCreateDedicatedWorker(const GURL& url, - int render_view_route_id, - int* route_id); + void OnCreateWorker(const GURL& url, + bool is_shared, + const string16& name, + int render_view_route_id, + int* route_id); void OnCancelCreateDedicatedWorker(int route_id); void OnForwardToWorker(const IPC::Message& message); diff --git a/chrome/browser/worker_host/worker_service.cc b/chrome/browser/worker_host/worker_service.cc index a54bb2a..334917f 100644 --- a/chrome/browser/worker_host/worker_service.cc +++ b/chrome/browser/worker_host/worker_service.cc @@ -44,24 +44,28 @@ void WorkerService::Initialize(ResourceDispatcherHost* rdh) { WorkerService::~WorkerService() { } -bool WorkerService::CreateDedicatedWorker(const GURL &url, - int renderer_id, - int render_view_route_id, - IPC::Message::Sender* sender, - int sender_id, - int sender_route_id) { +bool WorkerService::CreateWorker(const GURL &url, + bool is_shared, + const string16& name, + int renderer_id, + int render_view_route_id, + IPC::Message::Sender* sender, + int sender_id, + int sender_route_id) { // Generate a unique route id for the browser-worker communication that's // unique among all worker processes. That way when the worker process sends // a wrapped IPC message through us, we know which WorkerProcessHost to give // it to. WorkerProcessHost::WorkerInstance instance; instance.url = url; + instance.name = name; instance.renderer_id = renderer_id; instance.render_view_route_id = render_view_route_id; instance.worker_route_id = next_worker_route_id(); instance.sender = sender; instance.sender_id = sender_id; instance.sender_route_id = sender_route_id; + instance.is_shared = is_shared; WorkerProcessHost* worker = NULL; if (CommandLine::ForCurrentProcess()->HasSwitch( diff --git a/chrome/browser/worker_host/worker_service.h b/chrome/browser/worker_host/worker_service.h index 0bd6f58..40267aa 100644 --- a/chrome/browser/worker_host/worker_service.h +++ b/chrome/browser/worker_host/worker_service.h @@ -26,12 +26,14 @@ class WorkerService : public NotificationObserver { void Initialize(ResourceDispatcherHost* rdh); // Creates a dedicated worker. Returns true on success. - bool CreateDedicatedWorker(const GURL &url, - int renderer_pid, - int render_view_route_id, - IPC::Message::Sender* sender, - int sender_id, - int sender_route_id); + bool CreateWorker(const GURL &url, + bool is_shared, + const string16& name, + int renderer_pid, + int render_view_route_id, + IPC::Message::Sender* sender, + int sender_id, + int sender_route_id); // Cancel creation of a dedicated worker that hasn't started yet. void CancelCreateDedicatedWorker(int sender_id, int sender_route_id); diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index dde6002..013a584 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -3330,8 +3330,8 @@ 'renderer/webplugin_delegate_proxy.h', 'renderer/webplugin_delegate_pepper.cc', 'renderer/webplugin_delegate_pepper.h', - 'renderer/websharedworker_impl.cc', - 'renderer/websharedworker_impl.h', + 'renderer/websharedworker_proxy.cc', + 'renderer/websharedworker_proxy.h', 'renderer/websharedworkerrepository_impl.cc', 'renderer/websharedworkerrepository_impl.h', 'renderer/webworker_base.cc', @@ -3420,6 +3420,12 @@ 'worker/nativewebworker_impl.h', 'worker/nativewebworker_stub.cc', 'worker/nativewebworker_stub.h', + 'worker/websharedworker_stub.cc', + 'worker/websharedworker_stub.h', + 'worker/webworker_stub_base.cc', + 'worker/webworker_stub_base.h', + 'worker/webworker_stub.cc', + 'worker/webworker_stub.h', 'worker/webworkerclient_proxy.cc', 'worker/webworkerclient_proxy.h', 'worker/worker_main.cc', diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h index d568d02..e111866 100644 --- a/chrome/common/render_messages_internal.h +++ b/chrome/common/render_messages_internal.h @@ -1708,17 +1708,9 @@ IPC_BEGIN_MESSAGES(ViewHost) // A renderer sends this to the browser process when it wants to create a // worker. The browser will create the worker process if necessary, and // will return the route id on success. On error returns MSG_ROUTING_NONE. - IPC_SYNC_MESSAGE_CONTROL2_1(ViewHostMsg_CreateDedicatedWorker, - GURL /* url */, - int /* render_view_route_id */, - int /* route_id */) - - // A renderer sends this to the browser process when it wants to create a - // shared worker. The browser will create the worker process if necessary, - // and will return the route id on success. On error returns - // MSG_ROUTING_NONE. - IPC_SYNC_MESSAGE_CONTROL3_1(ViewHostMsg_CreateSharedWorker, + IPC_SYNC_MESSAGE_CONTROL4_1(ViewHostMsg_CreateWorker, GURL /* url */, + bool /* is_shared */, string16 /* name */, int /* render_view_route_id */, int /* route_id */) diff --git a/chrome/common/worker_messages_internal.h b/chrome/common/worker_messages_internal.h index 6ed4d09..04c983c 100644 --- a/chrome/common/worker_messages_internal.h +++ b/chrome/common/worker_messages_internal.h @@ -13,8 +13,10 @@ // WorkerProcess messages // These are messages sent from the browser to the worker process. IPC_BEGIN_MESSAGES(WorkerProcess) - IPC_MESSAGE_CONTROL2(WorkerProcessMsg_CreateWorker, + IPC_MESSAGE_CONTROL4(WorkerProcessMsg_CreateWorker, GURL /* url */, + bool /* is_shared */, + string16 /* name */, int /* route_id */) // Note: these Message Port related messages can also be sent to the diff --git a/chrome/renderer/websharedworker_impl.cc b/chrome/renderer/websharedworker_proxy.cc index f5e20ce..ee08901 100644 --- a/chrome/renderer/websharedworker_impl.cc +++ b/chrome/renderer/websharedworker_proxy.cc @@ -2,38 +2,32 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/renderer/websharedworker_impl.h" +#include "chrome/renderer/websharedworker_proxy.h" #include "chrome/common/render_messages.h" #include "chrome/common/webmessageportchannel_impl.h" #include "chrome/common/worker_messages.h" #include "webkit/api/public/WebURL.h" -WebSharedWorkerImpl::WebSharedWorkerImpl(const GURL& url, - const string16& name, - ChildThread* child_thread, +WebSharedWorkerProxy::WebSharedWorkerProxy(ChildThread* child_thread, int route_id, int render_view_route_id) - : WebWorkerBase(child_thread, route_id, render_view_route_id), - url_(url), - name_(name) { + : WebWorkerBase(child_thread, route_id, render_view_route_id) { } -bool WebSharedWorkerImpl::isStarted() { +bool WebSharedWorkerProxy::isStarted() { return IsStarted(); } -void WebSharedWorkerImpl::startWorkerContext( +void WebSharedWorkerProxy::startWorkerContext( const WebKit::WebURL& script_url, + const WebKit::WebString& name, const WebKit::WebString& user_agent, const WebKit::WebString& source_code) { - DCHECK(url_ == script_url); - IPC::Message* create_message = new ViewHostMsg_CreateSharedWorker( - url_, name_, render_view_route_id_, &route_id_); - CreateWorkerContext(create_message, script_url, user_agent, source_code); + CreateWorkerContext(script_url, true, name, user_agent, source_code); } -void WebSharedWorkerImpl::connect(WebKit::WebMessagePortChannel* channel) { +void WebSharedWorkerProxy::connect(WebKit::WebMessagePortChannel* channel) { WebMessagePortChannelImpl* webchannel = static_cast<WebMessagePortChannelImpl*>(channel); @@ -44,13 +38,13 @@ void WebSharedWorkerImpl::connect(WebKit::WebMessagePortChannel* channel) { Send(new WorkerMsg_Connect(route_id_, message_port_id, MSG_ROUTING_NONE)); } -void WebSharedWorkerImpl::OnMessageReceived(const IPC::Message& message) { - IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerImpl, message) +void WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { + IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) IPC_END_MESSAGE_MAP() } -void WebSharedWorkerImpl::OnWorkerCreated() { +void WebSharedWorkerProxy::OnWorkerCreated() { // The worker is created - now send off the CreateWorkerContext message and // any other queued messages SendQueuedMessages(); diff --git a/chrome/renderer/websharedworker_impl.h b/chrome/renderer/websharedworker_proxy.h index 14d122d89..8ffe63c 100644 --- a/chrome/renderer/websharedworker_impl.h +++ b/chrome/renderer/websharedworker_proxy.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_RENDERER_WEBSHAREDWORKER_IMPL_H_ -#define CHROME_RENDERER_WEBSHAREDWORKER_IMPL_H_ +#ifndef CHROME_RENDERER_WEBSHAREDWORKER_PROXY_H_ +#define CHROME_RENDERER_WEBSHAREDWORKER_PROXY_H_ #include "base/basictypes.h" #include "chrome/renderer/webworker_base.h" @@ -17,12 +17,10 @@ class ChildThread; // thread. Once the connect event has been sent, all future communication will // happen via the WebMessagePortChannel, and the WebSharedWorker instance will // be freed. -class WebSharedWorkerImpl : public WebKit::WebSharedWorker, - private WebWorkerBase { +class WebSharedWorkerProxy : public WebKit::WebSharedWorker, + private WebWorkerBase { public: - WebSharedWorkerImpl(const GURL& url, - const string16& name, - ChildThread* child_thread, + WebSharedWorkerProxy(ChildThread* child_thread, int route_id, int render_view_route_id); @@ -30,20 +28,17 @@ class WebSharedWorkerImpl : public WebKit::WebSharedWorker, virtual bool isStarted(); virtual void connect(WebKit::WebMessagePortChannel* channel); virtual void startWorkerContext(const WebKit::WebURL& script_url, + const WebKit::WebString& name, const WebKit::WebString& user_agent, const WebKit::WebString& source_code); - // IPC::Channel::Listener implementation. + // IPC::Channel::Listener proxyementation. void OnMessageReceived(const IPC::Message& message); private: void OnWorkerCreated(); - // The name and URL that uniquely identify this worker. - GURL url_; - string16 name_; - - DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerImpl); + DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerProxy); }; -#endif // CHROME_RENDERER_WEBSHAREDWORKER_IMPL_H_ +#endif // CHROME_RENDERER_WEBSHAREDWORKER_PROXY_H_ diff --git a/chrome/renderer/webworker_base.cc b/chrome/renderer/webworker_base.cc index 98365cd..1364e7a 100644 --- a/chrome/renderer/webworker_base.cc +++ b/chrome/renderer/webworker_base.cc @@ -48,12 +48,14 @@ void WebWorkerBase::Disconnect() { route_id_ = MSG_ROUTING_NONE; } -void WebWorkerBase::CreateWorkerContext(IPC::Message* create_message, - const GURL& script_url, +void WebWorkerBase::CreateWorkerContext(const GURL& script_url, + bool is_shared, + const string16& name, const string16& user_agent, const string16& source_code) { DCHECK(route_id_ == MSG_ROUTING_NONE); - // create_message is a sync message that sets route_id_ + IPC::Message* create_message = new ViewHostMsg_CreateWorker( + script_url, is_shared, name, render_view_route_id_, &route_id_); child_thread_->Send(create_message); if (route_id_ == MSG_ROUTING_NONE) return; diff --git a/chrome/renderer/webworker_base.h b/chrome/renderer/webworker_base.h index c7c6c62..12ee84a 100644 --- a/chrome/renderer/webworker_base.h +++ b/chrome/renderer/webworker_base.h @@ -26,8 +26,9 @@ class WebWorkerBase : public IPC::Channel::Listener { virtual ~WebWorkerBase(); // Creates and initializes a new worker context. - void CreateWorkerContext(IPC::Message* create_message, - const GURL& script_url, + void CreateWorkerContext(const GURL& script_url, + bool is_shared, + const string16& name, const string16& user_agent, const string16& source_code); diff --git a/chrome/renderer/webworker_proxy.cc b/chrome/renderer/webworker_proxy.cc index 5a24c41..e8a5b9d 100644 --- a/chrome/renderer/webworker_proxy.cc +++ b/chrome/renderer/webworker_proxy.cc @@ -11,6 +11,7 @@ #include "webkit/api/public/WebURL.h" #include "webkit/api/public/WebWorkerClient.h" +using WebKit::WebCommonWorkerClient; using WebKit::WebMessagePortChannel; using WebKit::WebMessagePortChannelArray; using WebKit::WebString; @@ -41,9 +42,7 @@ void WebWorkerProxy::startWorkerContext( const WebURL& script_url, const WebString& user_agent, const WebString& source_code) { - IPC::Message* create_message = new ViewHostMsg_CreateDedicatedWorker( - script_url, render_view_route_id_, &route_id_); - CreateWorkerContext(create_message, script_url, user_agent, source_code); + CreateWorkerContext(script_url, false, string16(), user_agent, source_code); } void WebWorkerProxy::terminateWorkerContext() { @@ -97,8 +96,8 @@ void WebWorkerProxy::OnMessageReceived(const IPC::Message& message) { client_, WebWorkerClient::reportPendingActivity) IPC_MESSAGE_FORWARD(WorkerHostMsg_WorkerContextDestroyed, - client_, - WebWorkerClient::workerContextDestroyed) + static_cast<WebCommonWorkerClient*>(client_), + WebCommonWorkerClient::workerContextDestroyed) IPC_END_MESSAGE_MAP() } diff --git a/chrome/renderer/webworker_proxy.h b/chrome/renderer/webworker_proxy.h index 686669a..48487c0 100644 --- a/chrome/renderer/webworker_proxy.h +++ b/chrome/renderer/webworker_proxy.h @@ -46,6 +46,7 @@ class WebWorkerProxy : public WebKit::WebWorker, private WebWorkerBase { virtual void Disconnect(); void OnWorkerCreated(); + void OnWorkerContextDestroyed(); void OnPostMessage(const string16& message, const std::vector<int>& sent_message_port_ids, const std::vector<int>& new_routing_ids); diff --git a/chrome/worker/websharedworker_stub.cc b/chrome/worker/websharedworker_stub.cc new file mode 100644 index 0000000..902f393 --- /dev/null +++ b/chrome/worker/websharedworker_stub.cc @@ -0,0 +1,52 @@ +// 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/websharedworker_stub.h" + +#include "chrome/common/webmessageportchannel_impl.h" +#include "chrome/common/worker_messages.h" +#include "webkit/api/public/WebSharedWorker.h" +#include "webkit/api/public/WebString.h" +#include "webkit/api/public/WebURL.h" + +WebSharedWorkerStub::WebSharedWorkerStub( + const string16& name, int route_id) + : WebWorkerStubBase(route_id), + name_(name) { + + // TODO(atwilson): Add support for NaCl when they support MessagePorts. + impl_ = WebKit::WebSharedWorker::create(client()); + +} + +WebSharedWorkerStub::~WebSharedWorkerStub() { + impl_->clientDestroyed(); +} + +void WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { + IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) + IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) + IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, + OnTerminateWorkerContext) + IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) + IPC_END_MESSAGE_MAP() +} + +void WebSharedWorkerStub::OnStartWorkerContext( + const GURL& url, const string16& user_agent, const string16& source_code) { + impl_->startWorkerContext(url, name_, user_agent, source_code); +} + +void WebSharedWorkerStub::OnConnect(int sent_message_port_id, int routing_id) { + WebKit::WebMessagePortChannel* channel = + new WebMessagePortChannelImpl(routing_id, sent_message_port_id); + impl_->connect(channel); +} + +void WebSharedWorkerStub::OnTerminateWorkerContext() { + impl_->terminateWorkerContext(); + + // Call the client to make sure context exits. + EnsureWorkerContextTerminates(); +} diff --git a/chrome/worker/websharedworker_stub.h b/chrome/worker/websharedworker_stub.h new file mode 100644 index 0000000..f7a26f3 --- /dev/null +++ b/chrome/worker/websharedworker_stub.h @@ -0,0 +1,40 @@ +// 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_WEB_SHARED_WORKER_STUB_H_ +#define CHROME_WORKER_WEB_SHARED_WORKER_STUB_H_ + +#include "chrome/worker/webworker_stub_base.h" +#include "chrome/worker/webworkerclient_proxy.h" +#include "googleurl/src/gurl.h" + +namespace WebKit { +class WebSharedWorker; +} + +// This class creates a WebSharedWorker, and translates incoming IPCs to the +// appropriate WebSharedWorker APIs. +class WebSharedWorkerStub : public WebWorkerStubBase { + public: + WebSharedWorkerStub(const string16& name, int route_id); + + // IPC::Channel::Listener implementation. + virtual void OnMessageReceived(const IPC::Message& message); + + private: + virtual ~WebSharedWorkerStub(); + + // Invoked when the WebWorkerClientProxy is shutting down. + void OnConnect(int sent_message_port_id, int routing_id); + void OnStartWorkerContext( + const GURL& url, const string16& user_agent, const string16& source_code); + void OnTerminateWorkerContext(); + + WebKit::WebSharedWorker* impl_; + string16 name_; + + DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerStub); +}; + +#endif // CHROME_WORKER_WEB_SHARED_WORKER_STUB_H_ diff --git a/chrome/worker/webworker_stub.cc b/chrome/worker/webworker_stub.cc new file mode 100644 index 0000000..37be7ab --- /dev/null +++ b/chrome/worker/webworker_stub.cc @@ -0,0 +1,84 @@ +// 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/webworker_stub.h" + +#include "base/command_line.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/webmessageportchannel_impl.h" +#include "chrome/common/worker_messages.h" +#include "chrome/worker/nativewebworker_impl.h" +#include "webkit/api/public/WebString.h" +#include "webkit/api/public/WebURL.h" +#include "webkit/api/public/WebWorker.h" + +using WebKit::WebWorker; + +static bool UrlIsNativeWorker(const GURL& url) { + // If the renderer was not passed the switch to enable native workers, + // then the URL should be treated as a JavaScript worker. + if (!CommandLine::ForCurrentProcess()->HasSwitch( + switches::kEnableNativeWebWorkers)) { + return false; + } + // Based on the suffix, decide whether the url should be considered + // a NativeWebWorker (for .nexe) or a WebWorker (for anything else). + const std::string kNativeSuffix(".nexe"); + std::string worker_url = url.path(); + // Compute the start index of the suffix. + std::string::size_type suffix_index = + worker_url.length() - kNativeSuffix.length(); + std::string::size_type pos = worker_url.find(kNativeSuffix, suffix_index); + return (suffix_index == pos); +} + +WebWorkerStub::WebWorkerStub(const GURL& url, int route_id) + : WebWorkerStubBase(route_id) { + if (UrlIsNativeWorker(url)) { + // Launch a native worker. + impl_ = NativeWebWorkerImpl::create(client()); + } else { + // Launch a JavaScript worker. + impl_ = WebKit::WebWorker::create(client()); + } +} + +WebWorkerStub::~WebWorkerStub() { + impl_->clientDestroyed(); +} + +void WebWorkerStub::OnMessageReceived(const IPC::Message& message) { + if (!impl_) + return; + + IPC_BEGIN_MESSAGE_MAP(WebWorkerStub, message) + IPC_MESSAGE_FORWARD(WorkerMsg_StartWorkerContext, impl_, + WebWorker::startWorkerContext) + IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, + OnTerminateWorkerContext) + IPC_MESSAGE_HANDLER(WorkerMsg_PostMessage, OnPostMessage) + IPC_MESSAGE_FORWARD(WorkerMsg_WorkerObjectDestroyed, impl_, + WebWorker::workerObjectDestroyed) + IPC_END_MESSAGE_MAP() +} + +void WebWorkerStub::OnTerminateWorkerContext() { + impl_->terminateWorkerContext(); + + // Call the client to make sure context exits. + EnsureWorkerContextTerminates(); +} + +void WebWorkerStub::OnPostMessage( + const string16& message, + const std::vector<int>& sent_message_port_ids, + const std::vector<int>& new_routing_ids) { + WebKit::WebMessagePortChannelArray channels(sent_message_port_ids.size()); + for (size_t i = 0; i < sent_message_port_ids.size(); i++) { + channels[i] = new WebMessagePortChannelImpl( + new_routing_ids[i], sent_message_port_ids[i]); + } + + impl_->postMessageToWorkerContext(message, channels); +} diff --git a/chrome/worker/webworker_stub.h b/chrome/worker/webworker_stub.h new file mode 100644 index 0000000..d83a4b7 --- /dev/null +++ b/chrome/worker/webworker_stub.h @@ -0,0 +1,38 @@ +// 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_WEB_WORKER_STUB_H_ +#define CHROME_WORKER_WEB_WORKER_STUB_H_ + +#include "chrome/worker/webworker_stub_base.h" +#include "chrome/worker/webworkerclient_proxy.h" +#include "googleurl/src/gurl.h" + +namespace WebKit { +class WebWorker; +} + +// This class creates a WebWorker, and translates incoming IPCs to the +// appropriate WebWorker APIs. +class WebWorkerStub : public WebWorkerStubBase { + public: + WebWorkerStub(const GURL& url, int route_id); + + // IPC::Channel::Listener implementation. + virtual void OnMessageReceived(const IPC::Message& message); + + private: + virtual ~WebWorkerStub(); + + void OnTerminateWorkerContext(); + void OnPostMessage(const string16& message, + const std::vector<int>& sent_message_port_ids, + const std::vector<int>& new_routing_ids); + + WebKit::WebWorker* impl_; + + DISALLOW_COPY_AND_ASSIGN(WebWorkerStub); +}; + +#endif // CHROME_WORKER_WEB_WORKER_STUB_H_ diff --git a/chrome/worker/webworker_stub_base.cc b/chrome/worker/webworker_stub_base.cc new file mode 100644 index 0000000..8ea2a15 --- /dev/null +++ b/chrome/worker/webworker_stub_base.cc @@ -0,0 +1,32 @@ +// 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/webworker_stub_base.h" + +#include "base/compiler_specific.h" +#include "chrome/common/child_process.h" +#include "chrome/worker/worker_thread.h" + +WebWorkerStubBase::WebWorkerStubBase(int route_id) + : route_id_(route_id), + ALLOW_THIS_IN_INITIALIZER_LIST(client_(route_id, this)) { + + // Start processing incoming IPCs for this worker. + WorkerThread::current()->AddRoute(route_id_, this); + ChildProcess::current()->AddRefProcess(); +} + +WebWorkerStubBase::~WebWorkerStubBase() { + WorkerThread::current()->RemoveRoute(route_id_); + ChildProcess::current()->ReleaseProcess(); +} + +void WebWorkerStubBase::Shutdown() { + // The worker has exited - free ourselves and the client. + delete this; +} + +void WebWorkerStubBase::EnsureWorkerContextTerminates() { + client_.EnsureWorkerContextTerminates(); +} diff --git a/chrome/worker/webworker_stub_base.h b/chrome/worker/webworker_stub_base.h new file mode 100644 index 0000000..1e5ec12 --- /dev/null +++ b/chrome/worker/webworker_stub_base.h @@ -0,0 +1,36 @@ +// 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_WEB_WORKER_STUB_BASE_H_ +#define CHROME_WORKER_WEB_WORKER_STUB_BASE_H_ + +#include "chrome/worker/webworkerclient_proxy.h" +#include "ipc/ipc_channel.h" + +// This class is the common base class for both WebWorkerStub and +// WebSharedWorkerStub and contains common setup/teardown functionality. +class WebWorkerStubBase : public IPC::Channel::Listener { + public: + WebWorkerStubBase(int route_id); + virtual ~WebWorkerStubBase(); + + // Invoked when the WebWorkerClientProxy is shutting down. + void Shutdown(); + + // Called after terminating the worker context to make sure that the worker + // actually terminates (is not stuck in an infinite loop). + void EnsureWorkerContextTerminates(); + + WebWorkerClientProxy* client() { return &client_; } + + private: + int route_id_; + + // WebWorkerClient that responds to outgoing API calls from the worker object. + WebWorkerClientProxy client_; + + DISALLOW_COPY_AND_ASSIGN(WebWorkerStubBase); +}; + +#endif // CHROME_WORKER_WEB_WORKER_STUB_BASE_H_ diff --git a/chrome/worker/webworkerclient_proxy.cc b/chrome/worker/webworkerclient_proxy.cc index 9fe0870..baff23c 100644 --- a/chrome/worker/webworkerclient_proxy.cc +++ b/chrome/worker/webworkerclient_proxy.cc @@ -5,13 +5,12 @@ #include "chrome/worker/webworkerclient_proxy.h" #include "base/command_line.h" -#include "chrome/common/child_process.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/webmessageportchannel_impl.h" #include "chrome/common/worker_messages.h" #include "chrome/renderer/webworker_proxy.h" +#include "chrome/worker/webworker_stub_base.h" #include "chrome/worker/worker_thread.h" -#include "chrome/worker/nativewebworker_impl.h" #include "ipc/ipc_logging.h" #include "webkit/api/public/WebString.h" #include "webkit/api/public/WebURL.h" @@ -26,43 +25,14 @@ using WebKit::WebWorkerClient; // How long to wait for worker to finish after it's been told to terminate. #define kMaxTimeForRunawayWorkerMs 3000 -static bool UrlIsNativeWorker(const GURL& url) { - // If the renderer was not passed the switch to enable native workers, - // then the URL should be treated as a JavaScript worker. - if (!CommandLine::ForCurrentProcess()->HasSwitch( - switches::kEnableNativeWebWorkers)) { - return false; - } - // Based on the suffix, decide whether the url should be considered - // a NativeWebWorker (for .nexe) or a WebWorker (for anything else). - const std::string kNativeSuffix(".nexe"); - std::string worker_url = url.path(); - // Compute the start index of the suffix. - std::string::size_type suffix_index = - worker_url.length() - kNativeSuffix.length(); - std::string::size_type pos = worker_url.find(kNativeSuffix, suffix_index); - return (suffix_index == pos); -} - -WebWorkerClientProxy::WebWorkerClientProxy(const GURL& url, int route_id) - : url_(url), - route_id_(route_id), +WebWorkerClientProxy::WebWorkerClientProxy(int route_id, + WebWorkerStubBase* stub) + : route_id_(route_id), + stub_(stub), ALLOW_THIS_IN_INITIALIZER_LIST(kill_process_factory_(this)) { - if (UrlIsNativeWorker(url)) { - // Launch a native worker. - impl_ = NativeWebWorkerImpl::create(this); - } else { - // Launch a JavaScript worker. - impl_ = WebWorker::create(this); - } - WorkerThread::current()->AddRoute(route_id_, this); - ChildProcess::current()->AddRefProcess(); } WebWorkerClientProxy::~WebWorkerClientProxy() { - impl_->clientDestroyed(); - WorkerThread::current()->RemoveRoute(route_id_); - ChildProcess::current()->ReleaseProcess(); } void WebWorkerClientProxy::postMessageToWorkerObject( @@ -123,8 +93,9 @@ void WebWorkerClientProxy::reportPendingActivity(bool has_pending_activity) { void WebWorkerClientProxy::workerContextDestroyed() { Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_)); - - delete this; + // Tell the stub that the worker has shutdown - frees this object. + if (stub_) + stub_->Shutdown(); } WebKit::WebWorker* WebWorkerClientProxy::createWorker( @@ -136,24 +107,7 @@ bool WebWorkerClientProxy::Send(IPC::Message* message) { return WorkerThread::current()->Send(message); } -void WebWorkerClientProxy::OnMessageReceived(const IPC::Message& message) { - if (!impl_) - return; - - IPC_BEGIN_MESSAGE_MAP(WebWorkerClientProxy, message) - IPC_MESSAGE_FORWARD(WorkerMsg_StartWorkerContext, impl_, - WebWorker::startWorkerContext) - IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, - OnTerminateWorkerContext) - IPC_MESSAGE_HANDLER(WorkerMsg_PostMessage, OnPostMessage) - IPC_MESSAGE_FORWARD(WorkerMsg_WorkerObjectDestroyed, impl_, - WebWorker::workerObjectDestroyed) - IPC_END_MESSAGE_MAP() -} - -void WebWorkerClientProxy::OnTerminateWorkerContext() { - impl_->terminateWorkerContext(); - +void WebWorkerClientProxy::EnsureWorkerContextTerminates() { // Avoid a worker doing a while(1) from never exiting. if (CommandLine::ForCurrentProcess()->HasSwitch( switches::kWebWorkerShareProcesses)) { @@ -172,15 +126,3 @@ void WebWorkerClientProxy::OnTerminateWorkerContext() { kMaxTimeForRunawayWorkerMs); } -void WebWorkerClientProxy::OnPostMessage( - const string16& message, - const std::vector<int>& sent_message_port_ids, - const std::vector<int>& new_routing_ids) { - WebMessagePortChannelArray channels(sent_message_port_ids.size()); - for (size_t i = 0; i < sent_message_port_ids.size(); i++) { - channels[i] = new WebMessagePortChannelImpl( - new_routing_ids[i], sent_message_port_ids[i]); - } - - impl_->postMessageToWorkerContext(message, channels); -} diff --git a/chrome/worker/webworkerclient_proxy.h b/chrome/worker/webworkerclient_proxy.h index 456ecb0..b422912 100644 --- a/chrome/worker/webworkerclient_proxy.h +++ b/chrome/worker/webworkerclient_proxy.h @@ -9,7 +9,6 @@ #include "base/basictypes.h" #include "base/task.h" -#include "googleurl/src/gurl.h" #include "ipc/ipc_channel.h" #include "webkit/api/public/WebWorkerClient.h" @@ -17,15 +16,17 @@ namespace WebKit { class WebWorker; } +class WebWorkerStubBase; + // This class receives IPCs from the renderer and calls the WebCore::Worker // implementation (after the data types have been converted by glue code). It // is also called by the worker code and converts these function calls into // IPCs that are sent to the renderer, where they're converted back to function // calls by WebWorkerProxy. -class WebWorkerClientProxy : public WebKit::WebWorkerClient, - public IPC::Channel::Listener { +class WebWorkerClientProxy : public WebKit::WebWorkerClient { public: - WebWorkerClientProxy(const GURL& url, int route_id); + WebWorkerClientProxy(int route_id, WebWorkerStubBase* stub); + ~WebWorkerClientProxy(); // WebWorkerClient implementation. virtual void postMessageToWorkerObject( @@ -55,26 +56,13 @@ class WebWorkerClientProxy : public WebKit::WebWorkerClient, return NULL; } - // IPC::Channel::Listener implementation. - virtual void OnMessageReceived(const IPC::Message& message); + void EnsureWorkerContextTerminates(); private: - ~WebWorkerClientProxy(); - bool Send(IPC::Message* message); - void OnTerminateWorkerContext(); - void OnPostMessage(const string16& message, - const std::vector<int>& sent_message_port_ids, - const std::vector<int>& new_routing_ids); - - // The source url for this worker. - GURL url_; - int route_id_; - - WebKit::WebWorker* impl_; - + WebWorkerStubBase* stub_; ScopedRunnableMethodFactory<WebWorkerClientProxy> kill_process_factory_; DISALLOW_COPY_AND_ASSIGN(WebWorkerClientProxy); diff --git a/chrome/worker/worker_thread.cc b/chrome/worker/worker_thread.cc index e4be0b9..8ffc92b 100644 --- a/chrome/worker/worker_thread.cc +++ b/chrome/worker/worker_thread.cc @@ -7,7 +7,8 @@ #include "base/lazy_instance.h" #include "base/thread_local.h" #include "chrome/common/worker_messages.h" -#include "chrome/worker/webworkerclient_proxy.h" +#include "chrome/worker/webworker_stub.h" +#include "chrome/worker/websharedworker_stub.h" #include "chrome/worker/worker_webkitclient_impl.h" #include "webkit/api/public/WebKit.h" @@ -37,7 +38,13 @@ void WorkerThread::OnControlMessageReceived(const IPC::Message& msg) { IPC_END_MESSAGE_MAP() } -void WorkerThread::OnCreateWorker(const GURL& url, int route_id) { - // WebWorkerClientProxy owns itself. - new WebWorkerClientProxy(url, route_id); +void WorkerThread::OnCreateWorker(const GURL& url, + bool is_shared, + const string16& name, + int route_id) { + // WebWorkerStub and WebSharedWorkerStub own themselves. + if (is_shared) + new WebSharedWorkerStub(name, route_id); + else + new WebWorkerStub(url, route_id); } diff --git a/chrome/worker/worker_thread.h b/chrome/worker/worker_thread.h index 27d0abf..040f0db 100644 --- a/chrome/worker/worker_thread.h +++ b/chrome/worker/worker_thread.h @@ -21,7 +21,8 @@ class WorkerThread : public ChildThread { private: virtual void OnControlMessageReceived(const IPC::Message& msg); - void OnCreateWorker(const GURL& url, int route_id); + void OnCreateWorker( + const GURL& url, bool is_shared, const string16& name, int route_id); scoped_ptr<WorkerWebKitClientImpl> webkit_client_; diff --git a/tools/valgrind/memcheck/suppressions.txt b/tools/valgrind/memcheck/suppressions.txt index c8e41e3..4c4c9c6 100644 --- a/tools/valgrind/memcheck/suppressions.txt +++ b/tools/valgrind/memcheck/suppressions.txt @@ -954,9 +954,9 @@ Memcheck:Leak fun:_Znw* ... - fun:_ZN13WorkerService21CreateDedicatedWorkerERK4GURLiiPN3IPC7Message6SenderEii - fun:_ZN21ResourceMessageFilter23OnCreateDedicatedWorkerERK4GURLiPi - fun:_Z16DispatchToMethodI21ResourceMessageFilterMS0_FvRK4GURLiPiES1_iiEvPT_T0_RK6Tuple2IT1_T2_EP6Tuple1IT3_E + fun:_ZN13WorkerService12CreateWorkerERK4GURLbRKSbItN4base20string16_char_traitsESaItEEiiPN3IPC7Message6SenderEii + fun:_ZN21ResourceMessageFilter14OnCreateWorkerERK4GURLbRKSbItN4base20string16_char_traitsESaItEEiPi + fun:_Z16DispatchToMethodI21ResourceMessageFilterMS0_FvRK4GURLbRKSbItN4base20string16_char_traitsESaItEEiPiES1_bS7_iiEvPT_T0_RK6Tuple4IT1_T2_T3_T4_EP6Tuple1IT5_E } { bug_22932 diff --git a/webkit/api/WebKit.gyp b/webkit/api/WebKit.gyp index 49c8966..1efd323 100644 --- a/webkit/api/WebKit.gyp +++ b/webkit/api/WebKit.gyp @@ -76,6 +76,7 @@ 'public/WebColor.h', 'public/WebColorName.h', 'public/WebCommon.h', + 'public/WebCommonWorkerClient.h', 'public/WebCompositionCommand.h', 'public/WebConsoleMessage.h', 'public/WebContextMenuData.h', @@ -134,6 +135,8 @@ 'public/WebSecurityOrigin.h', 'public/WebSecurityPolicy.h', 'public/WebSettings.h', + 'public/WebSharedWorker.h' + 'public/WebSharedWorkerRepository.h', 'public/WebSize.h', 'public/WebSocketStreamError.h', 'public/WebSocketStreamHandle.h', @@ -259,6 +262,8 @@ 'src/WebSecurityPolicy.cpp', 'src/WebSettingsImpl.cpp', 'src/WebSettingsImpl.h', + 'src/WebSharedWorkerImpl.cpp', + 'src/WebSharedWorkerImpl.h', 'src/WebStorageAreaImpl.cpp', 'src/WebStorageAreaImpl.h', 'src/WebStorageEventDispatcherImpl.cpp', diff --git a/webkit/api/public/WebCommonWorkerClient.h b/webkit/api/public/WebCommonWorkerClient.h new file mode 100644 index 0000000..fd7e399 --- /dev/null +++ b/webkit/api/public/WebCommonWorkerClient.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef WebCommonWorkerClient_h +#define WebCommonWorkerClient_h + +namespace WebKit { + class WebNotificationPresenter; + class WebString; + class WebWorker; + class WebWorkerClient; + + // Provides an interface back to the in-page script object for a worker. + // This interface contains common APIs used by both shared and dedicated + // workers. + // All functions are expected to be called back on the thread that created + // the Worker object, unless noted. + class WebCommonWorkerClient { + public: + virtual void postExceptionToWorkerObject( + const WebString& errorString, int lineNumber, + const WebString& sourceURL) = 0; + + virtual void postConsoleMessageToWorkerObject( + int destinationIdentifier, + int sourceIdentifier, + int messageType, + int messageLevel, + const WebString& message, + int lineNumber, + const WebString& sourceURL) = 0; + + virtual void workerContextDestroyed() = 0; + + // Returns the notification presenter for this worker context. Pointer + // is owned by the object implementing WebCommonWorkerClient. + virtual WebNotificationPresenter* notificationPresenter() = 0; + + // This can be called on any thread to create a nested WebWorker. + // WebSharedWorkers are not instantiated via this API - instead + // they are created via the WebSharedWorkerRepository. + virtual WebWorker* createWorker(WebWorkerClient* client) = 0; + + protected: + ~WebCommonWorkerClient() { } + }; + +} // namespace WebKit + +#endif diff --git a/webkit/api/public/WebSharedWorker.h b/webkit/api/public/WebSharedWorker.h index 37f0ad6..a580882 100644 --- a/webkit/api/public/WebSharedWorker.h +++ b/webkit/api/public/WebSharedWorker.h @@ -37,24 +37,35 @@ namespace WebKit { class ScriptExecutionContext; class WebString; class WebMessagePortChannel; + class WebCommonWorkerClient; class WebURL; // This is the interface to a SharedWorker thread. // Since SharedWorkers communicate entirely through MessagePorts this interface only contains APIs for starting up a SharedWorker. class WebSharedWorker { public: - virtual ~WebSharedWorker() {} + // Invoked from the worker thread to instantiate a WebSharedWorker that interacts with the WebKit worker components. + WEBKIT_API static WebSharedWorker* create(WebCommonWorkerClient*); + + virtual ~WebSharedWorker() {}; // Returns false if the thread hasn't been started yet (script loading has not taken place). // FIXME(atwilson): Remove this when we move the initial script loading into the worker process. virtual bool isStarted() = 0; virtual void startWorkerContext(const WebURL& scriptURL, + const WebString& name, const WebString& userAgent, const WebString& sourceCode) = 0; - // Sends a connect event to the SharedWorker thread. + // Sends a connect event to the SharedWorker context. virtual void connect(WebMessagePortChannel*) = 0; + + // Invoked to shutdown the worker when there are no more associated documents. + virtual void terminateWorkerContext() = 0; + + // Notification when the WebCommonWorkerClient is destroyed. + virtual void clientDestroyed() = 0; }; } // namespace WebKit diff --git a/webkit/api/public/WebWorkerClient.h b/webkit/api/public/WebWorkerClient.h index c0aa1bd..044ba52 100644 --- a/webkit/api/public/WebWorkerClient.h +++ b/webkit/api/public/WebWorkerClient.h @@ -32,6 +32,7 @@ #define WebWorkerClient_h #include "WebMessagePortChannel.h" +#include "WebCommonWorkerClient.h" namespace WebKit { class WebNotificationPresenter; @@ -41,37 +42,15 @@ namespace WebKit { // Provides an interface back to the in-page script object for a worker. // All functions are expected to be called back on the thread that created // the Worker object, unless noted. - class WebWorkerClient { + class WebWorkerClient : public WebCommonWorkerClient { public: virtual void postMessageToWorkerObject( const WebString&, const WebMessagePortChannelArray&) = 0; - virtual void postExceptionToWorkerObject( - const WebString& errorString, int lineNumber, - const WebString& sourceURL) = 0; - - virtual void postConsoleMessageToWorkerObject( - int destinationIdentifier, - int sourceIdentifier, - int messageType, - int messageLevel, - const WebString& message, - int lineNumber, - const WebString& sourceURL) = 0; - virtual void confirmMessageFromWorkerObject(bool hasPendingActivity) = 0; virtual void reportPendingActivity(bool hasPendingActivity) = 0; - virtual void workerContextDestroyed() = 0; - - // Returns the notification presenter for this worker context. Pointer - // is owned by the object implementing WebWorkerClient. - virtual WebNotificationPresenter* notificationPresenter() = 0; - - // This can be called on any thread to create a nested worker. - virtual WebWorker* createWorker(WebWorkerClient* client) = 0; - protected: ~WebWorkerClient() { } }; diff --git a/webkit/api/src/SharedWorkerRepository.cpp b/webkit/api/src/SharedWorkerRepository.cpp index ddc6238..32e8fd7 100644 --- a/webkit/api/src/SharedWorkerRepository.cpp +++ b/webkit/api/src/SharedWorkerRepository.cpp @@ -60,28 +60,32 @@ using WebKit::WebSharedWorkerRepository; // Callback class that keeps the Worker object alive while loads are potentially happening, and also translates load errors into error events on the worker. class SharedWorkerScriptLoader : public RefCounted<SharedWorkerScriptLoader>, private WorkerScriptLoaderClient { public: - SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, PassOwnPtr<MessagePortChannel> port, PassOwnPtr<WebSharedWorker> webWorker) - : m_worker(worker) - , m_webWorker(webWorker) - , m_port(port) + SharedWorkerScriptLoader(PassRefPtr<SharedWorker> worker, const KURL& url, const String& name, PassOwnPtr<MessagePortChannel> port, PassOwnPtr<WebSharedWorker> webWorker) + : m_worker(worker), + m_url(url), + m_name(name), + m_webWorker(webWorker), + m_port(port) { } - void load(const KURL&); + void load(); private: // WorkerScriptLoaderClient callback virtual void notifyFinished(); RefPtr<SharedWorker> m_worker; + KURL m_url; + String m_name; OwnPtr<WebSharedWorker> m_webWorker; OwnPtr<MessagePortChannel> m_port; WorkerScriptLoader m_scriptLoader; }; -void SharedWorkerScriptLoader::load(const KURL& url) +void SharedWorkerScriptLoader::load() { - m_scriptLoader.loadAsynchronously(m_worker->scriptExecutionContext(), url, DenyCrossOriginRequests, this); + m_scriptLoader.loadAsynchronously(m_worker->scriptExecutionContext(), m_url, DenyCrossOriginRequests, this); } // Extracts a WebMessagePortChannel from a MessagePortChannel. @@ -99,7 +103,7 @@ void SharedWorkerScriptLoader::notifyFinished() if (m_scriptLoader.failed()) m_worker->dispatchEvent(Event::create(eventNames().errorEvent, false, true)); else { - m_webWorker->startWorkerContext(m_scriptLoader.url(), m_worker->scriptExecutionContext()->userAgent(m_scriptLoader.url()), m_scriptLoader.script()); + m_webWorker->startWorkerContext(m_url, m_name, m_worker->scriptExecutionContext()->userAgent(m_url), m_scriptLoader.script()); m_webWorker->connect(getWebPort(m_port.release())); } @@ -138,8 +142,8 @@ void SharedWorkerRepository::connect(PassRefPtr<SharedWorker> worker, PassOwnPtr if (!webWorker->isStarted()) { // Need to kick off a load for the worker. The loader will connect to the worker once the script has been loaded, then free itself. - SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, port.release(), webWorker.release()); - loader->load(url); + SharedWorkerScriptLoader* loader = new SharedWorkerScriptLoader(worker, url, name, port.release(), webWorker.release()); + loader->load(); } else webWorker->connect(getWebPort(port.release())); } diff --git a/webkit/api/src/WebSharedWorkerImpl.cpp b/webkit/api/src/WebSharedWorkerImpl.cpp new file mode 100644 index 0000000..e5c9fc5 --- /dev/null +++ b/webkit/api/src/WebSharedWorkerImpl.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebSharedWorkerImpl.h" + +using namespace WebCore; + +namespace WebKit { + +#if ENABLE(SHARED_WORKERS) + +WebSharedWorker* WebSharedWorker::create(WebCommonWorkerClient* client) +{ + // FIXME: Return an instance of WebSharedWorkerImpl once the implementation is complete. + ASSERT_NOT_REACHED(); + return NULL; +} + +#endif // ENABLE(SHARED_WORKERS) + +} // namespace WebKit diff --git a/webkit/api/src/WebSharedWorkerImpl.h b/webkit/api/src/WebSharedWorkerImpl.h new file mode 100644 index 0000000..fad0f5a --- /dev/null +++ b/webkit/api/src/WebSharedWorkerImpl.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef WebSharedWorkerImpl_h +#define WebSharedWorkerImpl_h + +#include "WebSharedWorker.h" + +#if ENABLE(SHARED_WORKERS) + +#include "ScriptExecutionContext.h" +#include "WorkerLoaderProxy.h" +#include "WorkerObjectProxy.h" +#include <wtf/PassOwnPtr.h> +#include <wtf/RefPtr.h> + +namespace WebCore { +class SharedWorkerThread; +} + +namespace WebKit { +class WebView; + +// This class is used by the worker process code to talk to the WebCore::SharedWorker implementation. +// It can't use it directly since it uses WebKit types, so this class converts the data types. +// When the WebCore::SharedWorker object wants to call WebCore::WorkerReportingProxy, this class will +// convert to Chrome data types first and then call the supplied WebCommonWorkerClient. +class WebSharedWorkerImpl : public WebCore::WorkerLoaderProxy { +public: + explicit WebSharedWorkerImpl(WebCommonWorkerClient* client); + + // WebSharedWorker methods: + virtual bool isStarted(); + virtual void startWorkerContext(const WebURL&, const WebString& name, const WebString& userAgent, const WebString& sourceCode); + virtual void connect(WebMessagePortChannel*); + + WebCommonWorkerClient* client() { return m_client; } + +private: + virtual ~WebSharedWorkerImpl(); + + WebCommonWorkerClient* m_client; + + RefPtr<WebCore::SharedWorkerThread> m_workerThread; +}; + +} // namespace WebKit + +#endif // ENABLE(SHARED_WORKERS) + +#endif |