diff options
author | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-14 13:07:55 +0000 |
---|---|---|
committer | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-14 13:07:55 +0000 |
commit | 1ef98de512072aef57b96ad1a61d37cdf703c370 (patch) | |
tree | d6ae5bb2b6a85ce906186352c446a5541ec7b579 /content/browser | |
parent | 26a2ca5593e43255868d4147e0db836a17c2562c (diff) | |
download | chromium_src-1ef98de512072aef57b96ad1a61d37cdf703c370.zip chromium_src-1ef98de512072aef57b96ad1a61d37cdf703c370.tar.gz chromium_src-1ef98de512072aef57b96ad1a61d37cdf703c370.tar.bz2 |
Refactoring: Move MessagePort-related IPCs and handling them away from Worker-related classes.
MessagePorts can also be used by non-workers.
This is part of a series of changes to fix cross-process MessagePorts. This CL
contains no functional changes.
BUG=278336
Review URL: https://codereview.chromium.org/25299002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@228448 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
-rw-r--r-- | content/browser/message_port_message_filter.cc | 62 | ||||
-rw-r--r-- | content/browser/message_port_message_filter.h | 44 | ||||
-rw-r--r-- | content/browser/message_port_service.cc (renamed from content/browser/worker_host/message_port_service.cc) | 24 | ||||
-rw-r--r-- | content/browser/message_port_service.h (renamed from content/browser/worker_host/message_port_service.h) | 18 | ||||
-rw-r--r-- | content/browser/renderer_host/render_process_host_impl.cc | 11 | ||||
-rw-r--r-- | content/browser/renderer_host/render_process_host_impl.h | 4 | ||||
-rw-r--r-- | content/browser/worker_host/worker_message_filter.cc | 37 | ||||
-rw-r--r-- | content/browser/worker_host/worker_message_filter.h | 16 | ||||
-rw-r--r-- | content/browser/worker_host/worker_process_host.cc | 20 |
9 files changed, 168 insertions, 68 deletions
diff --git a/content/browser/message_port_message_filter.cc b/content/browser/message_port_message_filter.cc new file mode 100644 index 0000000..1c08497 --- /dev/null +++ b/content/browser/message_port_message_filter.cc @@ -0,0 +1,62 @@ +// Copyright 2013 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 "content/browser/message_port_message_filter.h" + +#include "content/browser/message_port_service.h" +#include "content/common/message_port_messages.h" + +namespace content { + +MessagePortMessageFilter::MessagePortMessageFilter( + const NextRoutingIDCallback& callback) + : next_routing_id_(callback) { +} + +MessagePortMessageFilter::~MessagePortMessageFilter() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); +} + +void MessagePortMessageFilter::OnChannelClosing() { + MessagePortService::GetInstance()->OnMessagePortMessageFilterClosing(this); +} + +bool MessagePortMessageFilter::OnMessageReceived(const IPC::Message& message, + bool* message_was_ok) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP_EX(MessagePortMessageFilter, message, *message_was_ok) + IPC_MESSAGE_HANDLER(MessagePortHostMsg_CreateMessagePort, + OnCreateMessagePort) + IPC_MESSAGE_FORWARD(MessagePortHostMsg_DestroyMessagePort, + MessagePortService::GetInstance(), + MessagePortService::Destroy) + IPC_MESSAGE_FORWARD(MessagePortHostMsg_Entangle, + MessagePortService::GetInstance(), + MessagePortService::Entangle) + IPC_MESSAGE_FORWARD(MessagePortHostMsg_PostMessage, + MessagePortService::GetInstance(), + MessagePortService::PostMessage) + IPC_MESSAGE_FORWARD(MessagePortHostMsg_QueueMessages, + MessagePortService::GetInstance(), + MessagePortService::QueueMessages) + IPC_MESSAGE_FORWARD(MessagePortHostMsg_SendQueuedMessages, + MessagePortService::GetInstance(), + MessagePortService::SendQueuedMessages) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP_EX() + + return handled; +} + +int MessagePortMessageFilter::GetNextRoutingID() { + return next_routing_id_.Run(); +} + +void MessagePortMessageFilter::OnCreateMessagePort(int *route_id, + int* message_port_id) { + *route_id = next_routing_id_.Run(); + MessagePortService::GetInstance()->Create(*route_id, this, message_port_id); +} + +} // namespace content diff --git a/content/browser/message_port_message_filter.h b/content/browser/message_port_message_filter.h new file mode 100644 index 0000000..592eba6 --- /dev/null +++ b/content/browser/message_port_message_filter.h @@ -0,0 +1,44 @@ +// Copyright 2013 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 CONTENT_BROWSER_MESSAGE_PORT_MESSAGE_FILTER_H_ +#define CONTENT_BROWSER_MESSAGE_PORT_MESSAGE_FILTER_H_ + +#include "base/callback.h" +#include "content/public/browser/browser_message_filter.h" + +namespace content { + +// Filter for MessagePort related IPC messages (creating and destroying a +// MessagePort, sending a message via a MessagePort etc). +class MessagePortMessageFilter : public BrowserMessageFilter { + public: + typedef base::Callback<int(void)> NextRoutingIDCallback; + + // |next_routing_id| is owned by this object. It can be used up until + // OnChannelClosing. + explicit MessagePortMessageFilter(const NextRoutingIDCallback& callback); + + // BrowserMessageFilter implementation. + virtual void OnChannelClosing() OVERRIDE; + virtual bool OnMessageReceived(const IPC::Message& message, + bool* message_was_ok) OVERRIDE; + int GetNextRoutingID(); + + private: + virtual ~MessagePortMessageFilter(); + + // Message handlers. + void OnCreateMessagePort(int* route_id, int* message_port_id); + + // This is guaranteed to be valid until OnChannelClosing is invoked, and it's + // not used after. + NextRoutingIDCallback next_routing_id_; + + DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePortMessageFilter); +}; + +} // namespace content + +#endif // CONTENT_BROWSER_WORKER_MESSAGE_FILTER_H_ diff --git a/content/browser/worker_host/message_port_service.cc b/content/browser/message_port_service.cc index 3bc8f5a..bc2a1ac 100644 --- a/content/browser/worker_host/message_port_service.cc +++ b/content/browser/message_port_service.cc @@ -1,11 +1,11 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright 2013 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 "content/browser/worker_host/message_port_service.h" +#include "content/browser/message_port_service.h" -#include "content/browser/worker_host/worker_message_filter.h" -#include "content/common/worker_messages.h" +#include "content/browser/message_port_message_filter.h" +#include "content/common/message_port_messages.h" namespace content { @@ -13,7 +13,7 @@ struct MessagePortService::MessagePort { // |filter| and |route_id| are what we need to send messages to the port. // |filter| is just a weak pointer since we get notified when its process has // gone away and remove it. - WorkerMessageFilter* filter; + MessagePortMessageFilter* filter; int route_id; // A globally unique id for this message port. int message_port_id; @@ -26,7 +26,7 @@ struct MessagePortService::MessagePort { // in-flight. This flag ensures that the latter type get flushed through the // system. // This flag should only be set to true in response to - // WorkerProcessHostMsg_QueueMessages. + // MessagePortHostMsg_QueueMessages. bool queue_messages; QueuedMessages queued_messages; }; @@ -44,7 +44,7 @@ MessagePortService::~MessagePortService() { void MessagePortService::UpdateMessagePort( int message_port_id, - WorkerMessageFilter* filter, + MessagePortMessageFilter* filter, int routing_id) { if (!message_ports_.count(message_port_id)) { NOTREACHED(); @@ -56,8 +56,8 @@ void MessagePortService::UpdateMessagePort( port.route_id = routing_id; } -void MessagePortService::OnWorkerMessageFilterClosing( - WorkerMessageFilter* filter) { +void MessagePortService::OnMessagePortMessageFilterClosing( + MessagePortMessageFilter* filter) { // Check if the (possibly) crashed process had any message ports. for (MessagePorts::iterator iter = message_ports_.begin(); iter != message_ports_.end();) { @@ -69,7 +69,7 @@ void MessagePortService::OnWorkerMessageFilterClosing( } void MessagePortService::Create(int route_id, - WorkerMessageFilter* filter, + MessagePortMessageFilter* filter, int* message_port_id) { *message_port_id = ++next_message_port_id_; @@ -173,7 +173,7 @@ void MessagePortService::PostMessageTo( } // Now send the message to the entangled port. - entangled_port.filter->Send(new WorkerProcessMsg_Message( + entangled_port.filter->Send(new MessagePortMsg_Message( entangled_port.route_id, message, sent_message_port_ids, new_routing_ids)); } @@ -186,7 +186,7 @@ void MessagePortService::QueueMessages(int message_port_id) { MessagePort& port = message_ports_[message_port_id]; if (port.filter) { - port.filter->Send(new WorkerProcessMsg_MessagesQueued(port.route_id)); + port.filter->Send(new MessagePortMsg_MessagesQueued(port.route_id)); port.queue_messages = true; port.filter = NULL; } diff --git a/content/browser/worker_host/message_port_service.h b/content/browser/message_port_service.h index b85e76b..55e536c 100644 --- a/content/browser/worker_host/message_port_service.h +++ b/content/browser/message_port_service.h @@ -1,9 +1,9 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright 2013 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 CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ -#define CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ +#ifndef CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ +#define CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ #include <map> #include <utility> @@ -15,7 +15,7 @@ #include "ipc/ipc_message.h" namespace content { -class WorkerMessageFilter; +class MessagePortMessageFilter; class MessagePortService { public: @@ -25,7 +25,9 @@ class MessagePortService { static MessagePortService* GetInstance(); // These methods correspond to the message port related IPCs. - void Create(int route_id, WorkerMessageFilter* filter, int* message_port_id); + void Create(int route_id, + MessagePortMessageFilter* filter, + int* message_port_id); void Destroy(int message_port_id); void Entangle(int local_message_port_id, int remote_message_port_id); void PostMessage(int sender_message_port_id, @@ -39,10 +41,10 @@ class MessagePortService { // (possibly different) process. void UpdateMessagePort( int message_port_id, - WorkerMessageFilter* filter, + MessagePortMessageFilter* filter, int routing_id); - void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); + void OnMessagePortMessageFilterClosing(MessagePortMessageFilter* filter); // Attempts to send the queued messages for a message port. void SendQueuedMessagesIfPossible(int message_port_id); @@ -73,4 +75,4 @@ class MessagePortService { } // namespace content -#endif // CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ +#endif // CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc index 49ed5df..f6e3ed0 100644 --- a/content/browser/renderer_host/render_process_host_impl.cc +++ b/content/browser/renderer_host/render_process_host_impl.cc @@ -63,6 +63,7 @@ #include "content/browser/loader/resource_scheduler_filter.h" #include "content/browser/media/android/browser_demuxer_android.h" #include "content/browser/media/media_internals.h" +#include "content/browser/message_port_message_filter.h" #include "content/browser/mime_registry_message_filter.h" #include "content/browser/plugin_service_impl.h" #include "content/browser/profiler_message_filter.h" @@ -673,6 +674,11 @@ void RenderProcessHostImpl::CreateMessageFilters() { GetID(), request_context_callback, resource_context); AddFilter(socket_stream_dispatcher_host); + message_port_message_filter_ = new MessagePortMessageFilter( + base::Bind(&RenderWidgetHelper::GetNextRoutingID, + base::Unretained(widget_helper_.get()))); + AddFilter(message_port_message_filter_); + AddFilter(new WorkerMessageFilter( GetID(), resource_context, @@ -684,8 +690,7 @@ void RenderProcessHostImpl::CreateMessageFilters() { storage_partition_impl_->GetFileSystemContext(), storage_partition_impl_->GetDatabaseTracker(), storage_partition_impl_->GetIndexedDBContext()), - base::Bind(&RenderWidgetHelper::GetNextRoutingID, - base::Unretained(widget_helper_.get())))); + message_port_message_filter_)); #if defined(ENABLE_WEBRTC) AddFilter(new P2PSocketDispatcherHost( @@ -1340,6 +1345,7 @@ void RenderProcessHostImpl::Cleanup() { // OnChannelClosed() to IPC::ChannelProxy::Context on the IO thread. channel_.reset(); gpu_message_filter_ = NULL; + message_port_message_filter_ = NULL; // Remove ourself from the list of renderer processes so that we can't be // reused in between now and when the Delete task runs. @@ -1638,6 +1644,7 @@ void RenderProcessHostImpl::ProcessDied(bool already_dead) { child_process_launcher_.reset(); channel_.reset(); gpu_message_filter_ = NULL; + message_port_message_filter_ = NULL; IDMap<IPC::Listener>::iterator iter(&listeners_); while (!iter.IsAtEnd()) { diff --git a/content/browser/renderer_host/render_process_host_impl.h b/content/browser/renderer_host/render_process_host_impl.h index 567b629b..1287b2e 100644 --- a/content/browser/renderer_host/render_process_host_impl.h +++ b/content/browser/renderer_host/render_process_host_impl.h @@ -35,6 +35,7 @@ class Size; namespace content { class BrowserDemuxerAndroid; class GpuMessageFilter; +class MessagePortMessageFilter; class PeerConnectionTrackerHost; class RendererMainThread; class RenderWidgetHelper; @@ -260,6 +261,9 @@ class CONTENT_EXPORT RenderProcessHostImpl // it's valid if non-NULL. GpuMessageFilter* gpu_message_filter_; + // The filter for MessagePort messages coming from the renderer. + scoped_refptr<MessagePortMessageFilter> message_port_message_filter_; + // A map of transport DIB ids to cached TransportDIBs std::map<TransportDIB::Id, TransportDIB*> cached_dibs_; diff --git a/content/browser/worker_host/worker_message_filter.cc b/content/browser/worker_host/worker_message_filter.cc index 6631de3..49fe7f6 100644 --- a/content/browser/worker_host/worker_message_filter.cc +++ b/content/browser/worker_host/worker_message_filter.cc @@ -4,7 +4,7 @@ #include "content/browser/worker_host/worker_message_filter.h" -#include "content/browser/worker_host/message_port_service.h" +#include "content/browser/message_port_message_filter.h" #include "content/browser/worker_host/worker_service_impl.h" #include "content/common/view_messages.h" #include "content/common/worker_messages.h" @@ -16,11 +16,11 @@ WorkerMessageFilter::WorkerMessageFilter( int render_process_id, ResourceContext* resource_context, const WorkerStoragePartition& partition, - const NextRoutingIDCallback& callback) + MessagePortMessageFilter* message_port_message_filter) : render_process_id_(render_process_id), resource_context_(resource_context), partition_(partition), - next_routing_id_(callback) { + message_port_message_filter_(message_port_message_filter) { // Note: This constructor is called on both IO or UI thread. DCHECK(resource_context); } @@ -30,7 +30,6 @@ WorkerMessageFilter::~WorkerMessageFilter() { } void WorkerMessageFilter::OnChannelClosing() { - MessagePortService::GetInstance()->OnWorkerMessageFilterClosing(this); WorkerServiceImpl::GetInstance()->OnWorkerMessageFilterClosing(this); } @@ -46,24 +45,6 @@ bool WorkerMessageFilter::OnMessageReceived(const IPC::Message& message, IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToWorker, OnForwardToWorker) // Only sent from renderer. IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentDetached, OnDocumentDetached) - // Message Port related messages. - IPC_MESSAGE_HANDLER(WorkerProcessHostMsg_CreateMessagePort, - OnCreateMessagePort) - IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_DestroyMessagePort, - MessagePortService::GetInstance(), - MessagePortService::Destroy) - IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_Entangle, - MessagePortService::GetInstance(), - MessagePortService::Entangle) - IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_PostMessage, - MessagePortService::GetInstance(), - MessagePortService::PostMessage) - IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_QueueMessages, - MessagePortService::GetInstance(), - MessagePortService::QueueMessages) - IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_SendQueuedMessages, - MessagePortService::GetInstance(), - MessagePortService::SendQueuedMessages) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP_EX() @@ -71,14 +52,14 @@ bool WorkerMessageFilter::OnMessageReceived(const IPC::Message& message, } int WorkerMessageFilter::GetNextRoutingID() { - return next_routing_id_.Run(); + return message_port_message_filter_->GetNextRoutingID(); } void WorkerMessageFilter::OnCreateWorker( const ViewHostMsg_CreateWorker_Params& params, int* route_id) { *route_id = params.route_id != MSG_ROUTING_NONE ? - params.route_id : next_routing_id_.Run(); + params.route_id : GetNextRoutingID(); WorkerServiceImpl::GetInstance()->CreateWorker( params, *route_id, this, resource_context_, partition_); } @@ -88,7 +69,7 @@ void WorkerMessageFilter::OnLookupSharedWorker( bool* exists, int* route_id, bool* url_error) { - *route_id = next_routing_id_.Run(); + *route_id = GetNextRoutingID(); WorkerServiceImpl::GetInstance()->LookupSharedWorker( params, *route_id, this, resource_context_, partition_, exists, @@ -103,10 +84,4 @@ void WorkerMessageFilter::OnDocumentDetached(unsigned long long document_id) { WorkerServiceImpl::GetInstance()->DocumentDetached(document_id, this); } -void WorkerMessageFilter::OnCreateMessagePort(int *route_id, - int* message_port_id) { - *route_id = next_routing_id_.Run(); - MessagePortService::GetInstance()->Create(*route_id, this, message_port_id); -} - } // namespace content diff --git a/content/browser/worker_host/worker_message_filter.h b/content/browser/worker_host/worker_message_filter.h index 47f96ef..0d495fa 100644 --- a/content/browser/worker_host/worker_message_filter.h +++ b/content/browser/worker_host/worker_message_filter.h @@ -13,18 +13,15 @@ class ResourceDispatcherHost; struct ViewHostMsg_CreateWorker_Params; namespace content { +class MessagePortMessageFilter; class ResourceContext; class WorkerMessageFilter : public BrowserMessageFilter { public: - typedef base::Callback<int(void)> NextRoutingIDCallback; - - // |next_routing_id| is owned by this object. It can be used up until - // OnChannelClosing. WorkerMessageFilter(int render_process_id, ResourceContext* resource_context, const WorkerStoragePartition& partition, - const NextRoutingIDCallback& callback); + MessagePortMessageFilter* message_port_filter); // BrowserMessageFilter implementation. virtual void OnChannelClosing() OVERRIDE; @@ -34,6 +31,10 @@ class WorkerMessageFilter : public BrowserMessageFilter { int GetNextRoutingID(); int render_process_id() const { return render_process_id_; } + MessagePortMessageFilter* message_port_message_filter() const { + return message_port_message_filter_; + } + private: virtual ~WorkerMessageFilter(); @@ -52,10 +53,7 @@ class WorkerMessageFilter : public BrowserMessageFilter { ResourceContext* const resource_context_; WorkerStoragePartition partition_; - // This is guaranteed to be valid until OnChannelClosing is closed, and it's - // not used after. - NextRoutingIDCallback next_routing_id_; - + MessagePortMessageFilter* message_port_message_filter_; DISALLOW_IMPLICIT_CONSTRUCTORS(WorkerMessageFilter); }; diff --git a/content/browser/worker_host/worker_process_host.cc b/content/browser/worker_host/worker_process_host.cc index f0324d7..b132767 100644 --- a/content/browser/worker_host/worker_process_host.cc +++ b/content/browser/worker_host/worker_process_host.cc @@ -25,6 +25,8 @@ #include "content/browser/fileapi/fileapi_message_filter.h" #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" #include "content/browser/loader/resource_message_filter.h" +#include "content/browser/message_port_message_filter.h" +#include "content/browser/message_port_service.h" #include "content/browser/mime_registry_message_filter.h" #include "content/browser/quota_dispatcher_host.h" #include "content/browser/renderer_host/database_message_filter.h" @@ -33,7 +35,6 @@ #include "content/browser/renderer_host/render_view_host_impl.h" #include "content/browser/renderer_host/socket_stream_dispatcher_host.h" #include "content/browser/resource_context_impl.h" -#include "content/browser/worker_host/message_port_service.h" #include "content/browser/worker_host/worker_message_filter.h" #include "content/browser/worker_host/worker_service_impl.h" #include "content/common/child_process_host_impl.h" @@ -223,10 +224,15 @@ void WorkerProcessHost::CreateMessageFilters(int render_process_id) { get_contexts_callback); process_->AddFilter(resource_message_filter); - worker_message_filter_ = new WorkerMessageFilter( - render_process_id, resource_context_, partition_, - base::Bind(&WorkerServiceImpl::next_worker_route_id, - base::Unretained(WorkerServiceImpl::GetInstance()))); + MessagePortMessageFilter* message_port_message_filter = + new MessagePortMessageFilter( + base::Bind(&WorkerServiceImpl::next_worker_route_id, + base::Unretained(WorkerServiceImpl::GetInstance()))); + process_->AddFilter(message_port_message_filter); + worker_message_filter_ = new WorkerMessageFilter(render_process_id, + resource_context_, + partition_, + message_port_message_filter); process_->AddFilter(worker_message_filter_.get()); process_->AddFilter(new AppCacheDispatcherHost( partition_.appcache_service(), process_->GetData().id)); @@ -400,7 +406,9 @@ void WorkerProcessHost::RelayMessage( } new_routing_id = filter->GetNextRoutingID(); MessagePortService::GetInstance()->UpdateMessagePort( - sent_message_port_id, filter, new_routing_id); + sent_message_port_id, + filter->message_port_message_filter(), + new_routing_id); // Resend the message with the new routing id. filter->Send(new WorkerMsg_Connect( |