diff options
author | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-02 19:41:12 +0000 |
---|---|---|
committer | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-02 19:41:12 +0000 |
commit | cf178fb755d0fda06fd770c1a94eb8696063c001 (patch) | |
tree | 6e78f195f4731ec0231c538fc8377a3a41e8482b /ipc | |
parent | 7d1c92a65a91100f4cae59626107b7b74aba536b (diff) | |
download | chromium_src-cf178fb755d0fda06fd770c1a94eb8696063c001.zip chromium_src-cf178fb755d0fda06fd770c1a94eb8696063c001.tar.gz chromium_src-cf178fb755d0fda06fd770c1a94eb8696063c001.tar.bz2 |
Introduce IPC::ChannelProxy::Create*() and IPC::SynChannel::Create*()
This change hides constructors of these classes so that we can turn
them polymorphic classes.
Note that having almost identical ChannelProxy::Init*() isn't great
and they will be replaced by a factory-like abstraction in coming
changes.
TEST=none
R=darin,cpu
BUG=377980
Review URL: https://codereview.chromium.org/301973003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274310 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ipc')
-rw-r--r-- | ipc/ipc_channel_proxy.cc | 90 | ||||
-rw-r--r-- | ipc/ipc_channel_proxy.h | 46 | ||||
-rw-r--r-- | ipc/ipc_sync_channel.cc | 64 | ||||
-rw-r--r-- | ipc/ipc_sync_channel.h | 45 | ||||
-rw-r--r-- | ipc/ipc_sync_channel_unittest.cc | 65 | ||||
-rw-r--r-- | ipc/ipc_test_base.cc | 8 |
6 files changed, 247 insertions, 71 deletions
diff --git a/ipc/ipc_channel_proxy.cc b/ipc/ipc_channel_proxy.cc index e88bb5d..cf28ed6 100644 --- a/ipc/ipc_channel_proxy.cc +++ b/ipc/ipc_channel_proxy.cc @@ -304,13 +304,56 @@ void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { //----------------------------------------------------------------------------- -ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, - Channel::Mode mode, - Listener* listener, - base::SingleThreadTaskRunner* ipc_task_runner) - : context_(new Context(listener, ipc_task_runner)), - did_init_(false) { - Init(channel_handle, mode, true); +// static +scoped_ptr<ChannelProxy> ChannelProxy::Create( + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) { + return make_scoped_ptr(new ChannelProxy( + listener, ipc_task_runner)); +} + +// static +scoped_ptr<ChannelProxy> ChannelProxy::CreateClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) { + scoped_ptr<ChannelProxy> channel = Create( + listener, ipc_task_runner); + channel->InitClient(channel_handle, true); + return channel.Pass(); +} + +// static +scoped_ptr<ChannelProxy> ChannelProxy::CreateServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) { + scoped_ptr<ChannelProxy> channel = Create( + listener, ipc_task_runner); + channel->InitServer(channel_handle, true); + return channel.Pass(); +} + +// static +scoped_ptr<ChannelProxy> ChannelProxy::CreateNamedClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) { + scoped_ptr<ChannelProxy> channel = Create( + listener, ipc_task_runner); + channel->InitNamedClient(channel_handle, true); + return channel.Pass(); +} + +// static +scoped_ptr<ChannelProxy> ChannelProxy::CreateNamedServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) { + scoped_ptr<ChannelProxy> channel = Create( + listener, ipc_task_runner); + channel->InitNamedServer(channel_handle, true); + return channel.Pass(); } ChannelProxy::ChannelProxy(Context* context) @@ -318,15 +361,22 @@ ChannelProxy::ChannelProxy(Context* context) did_init_(false) { } +ChannelProxy::ChannelProxy(Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner) + : context_(new Context(listener, ipc_task_runner)), + did_init_(false) { +} + ChannelProxy::~ChannelProxy() { DCHECK(CalledOnValidThread()); Close(); } -void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, - Channel::Mode mode, - bool create_pipe_now) { +void ChannelProxy::InitByMode( + const IPC::ChannelHandle& channel_handle, + Channel::Mode mode, + bool create_pipe_now) { DCHECK(CalledOnValidThread()); DCHECK(!did_init_); #if defined(OS_POSIX) @@ -358,6 +408,26 @@ void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, did_init_ = true; } +void ChannelProxy::InitClient(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now) { + InitByMode(channel_handle, Channel::MODE_CLIENT, create_pipe_now); +} + +void ChannelProxy::InitServer(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now) { + InitByMode(channel_handle, Channel::MODE_SERVER, create_pipe_now); +} + +void ChannelProxy::InitNamedClient(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now) { + InitByMode(channel_handle, Channel::MODE_NAMED_CLIENT, create_pipe_now); +} + +void ChannelProxy::InitNamedServer(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now) { + InitByMode(channel_handle, Channel::MODE_NAMED_SERVER, create_pipe_now); +} + void ChannelProxy::Close() { DCHECK(CalledOnValidThread()); diff --git a/ipc/ipc_channel_proxy.h b/ipc/ipc_channel_proxy.h index 0a3a5d2..d1f270d 100644 --- a/ipc/ipc_channel_proxy.h +++ b/ipc/ipc_channel_proxy.h @@ -64,10 +64,31 @@ class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe { // on the background thread. Any message not handled by the filter will be // dispatched to the listener. The given task runner correspond to a thread // on which IPC::Channel is created and used (e.g. IO thread). - ChannelProxy(const IPC::ChannelHandle& channel_handle, - Channel::Mode mode, - Listener* listener, - base::SingleThreadTaskRunner* ipc_task_runner); + // The naming pattern follows IPC::Channel. The Create() does not create + // underlying channel and let Init*() do it. + static scoped_ptr<ChannelProxy> Create( + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); + + static scoped_ptr<ChannelProxy> CreateClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); + + static scoped_ptr<ChannelProxy> CreateServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); + + static scoped_ptr<ChannelProxy> CreateNamedClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); + + static scoped_ptr<ChannelProxy> CreateNamedServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); virtual ~ChannelProxy(); @@ -75,8 +96,17 @@ class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe { // proxy that was not initialized in its constructor. If create_pipe_now is // true, the pipe is created synchronously. Otherwise it's created on the IO // thread. - void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, - bool create_pipe_now); + // The naming pattern follows IPC::Channel::Create*(). + void InitByMode(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, + bool create_pipe_now); + void InitClient(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now); + void InitServer(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now); + void InitNamedClient(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now); + void InitNamedServer(const IPC::ChannelHandle& channel_handle, + bool create_pipe_now); // Close the IPC::Channel. This operation completes asynchronously, once the // background thread processes the command to close the channel. It is ok to @@ -124,6 +154,10 @@ class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe { // to the internal state. ChannelProxy(Context* context); + ChannelProxy(Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner); + + // Used internally to hold state that is referenced on the IPC thread. class Context : public base::RefCountedThreadSafe<Context>, public Listener { diff --git a/ipc/ipc_sync_channel.cc b/ipc/ipc_sync_channel.cc index 0e0018c..32f8619 100644 --- a/ipc/ipc_sync_channel.cc +++ b/ipc/ipc_sync_channel.cc @@ -404,19 +404,65 @@ base::WaitableEventWatcher::EventCallback return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this); } -SyncChannel::SyncChannel( +// static +scoped_ptr<SyncChannel> SyncChannel::CreateClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event) { + scoped_ptr<SyncChannel> channel = Create( + listener, ipc_task_runner, shutdown_event); + channel->InitClient(channel_handle, create_pipe_now); + return channel.Pass(); +} + +// static +scoped_ptr<SyncChannel> SyncChannel::CreateServer( const IPC::ChannelHandle& channel_handle, - Channel::Mode mode, Listener* listener, base::SingleThreadTaskRunner* ipc_task_runner, bool create_pipe_now, - WaitableEvent* shutdown_event) - : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)) { - // The current (listener) thread must be distinct from the IPC thread, or else - // sending synchronous messages will deadlock. - DCHECK_NE(ipc_task_runner, base::ThreadTaskRunnerHandle::Get()); - ChannelProxy::Init(channel_handle, mode, create_pipe_now); - StartWatching(); + base::WaitableEvent* shutdown_event) { + scoped_ptr<SyncChannel> channel = Create( + listener, ipc_task_runner, shutdown_event); + channel->InitServer(channel_handle, create_pipe_now); + return channel.Pass(); +} + +// static +scoped_ptr<SyncChannel> SyncChannel::CreateNamedClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event) { + scoped_ptr<SyncChannel> channel = Create( + listener, ipc_task_runner, shutdown_event); + channel->InitNamedClient(channel_handle, create_pipe_now); + return channel.Pass(); +} + +// static +scoped_ptr<SyncChannel> SyncChannel::CreateNamedServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event) { + scoped_ptr<SyncChannel> channel = Create( + listener, ipc_task_runner, shutdown_event); + channel->InitNamedServer(channel_handle, create_pipe_now); + return channel.Pass(); +} + +// static +scoped_ptr<SyncChannel> SyncChannel::Create( + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + WaitableEvent* shutdown_event) { + return make_scoped_ptr(new SyncChannel( + listener, ipc_task_runner, shutdown_event)); } SyncChannel::SyncChannel( diff --git a/ipc/ipc_sync_channel.h b/ipc/ipc_sync_channel.h index f18fcff9..9ae3fd9 100644 --- a/ipc/ipc_sync_channel.h +++ b/ipc/ipc_sync_channel.h @@ -66,19 +66,42 @@ class IPC_EXPORT SyncChannel : public ChannelProxy { // Creates and initializes a sync channel. If create_pipe_now is specified, // the channel will be initialized synchronously. - SyncChannel(const IPC::ChannelHandle& channel_handle, - Channel::Mode mode, - Listener* listener, - base::SingleThreadTaskRunner* ipc_task_runner, - bool create_pipe_now, - base::WaitableEvent* shutdown_event); + // The naming pattern follows IPC::Channel. + static scoped_ptr<SyncChannel> CreateClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event); + + static scoped_ptr<SyncChannel> CreateServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event); + + static scoped_ptr<SyncChannel> CreateNamedClient( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event); + + static scoped_ptr<SyncChannel> CreateNamedServer( + const IPC::ChannelHandle& channel_handle, + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + bool create_pipe_now, + base::WaitableEvent* shutdown_event); // Creates an uninitialized sync channel. Call ChannelProxy::Init to // initialize the channel. This two-step setup allows message filters to be // added before any messages are sent or received. - SyncChannel(Listener* listener, - base::SingleThreadTaskRunner* ipc_task_runner, - base::WaitableEvent* shutdown_event); + static scoped_ptr<SyncChannel> Create( + Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + base::WaitableEvent* shutdown_event); virtual ~SyncChannel(); @@ -188,6 +211,10 @@ class IPC_EXPORT SyncChannel : public ChannelProxy { }; private: + SyncChannel(Listener* listener, + base::SingleThreadTaskRunner* ipc_task_runner, + base::WaitableEvent* shutdown_event); + void OnWaitableEventSignaled(base::WaitableEvent* arg); SyncContext* sync_context() { diff --git a/ipc/ipc_sync_channel_unittest.cc b/ipc/ipc_sync_channel_unittest.cc index ca8d5d7..33653c7 100644 --- a/ipc/ipc_sync_channel_unittest.cc +++ b/ipc/ipc_sync_channel_unittest.cc @@ -151,12 +151,12 @@ class Worker : public Listener, public Sender { } virtual SyncChannel* CreateChannel() { - return new SyncChannel(channel_name_, - mode_, - this, - ipc_thread_.message_loop_proxy().get(), - true, - &shutdown_event_); + scoped_ptr<SyncChannel> channel = SyncChannel::Create( + this, + ipc_thread_.message_loop_proxy().get(), + &shutdown_event_); + channel->InitByMode(channel_name_, mode_, true); + return channel.release(); } base::Thread* ListenerThread() { @@ -324,9 +324,10 @@ class TwoStepServer : public Worker { } virtual SyncChannel* CreateChannel() OVERRIDE { - SyncChannel* channel = new SyncChannel( - this, ipc_thread().message_loop_proxy().get(), shutdown_event()); - channel->Init(channel_name(), mode(), create_pipe_now_); + SyncChannel* channel = SyncChannel::Create( + this, ipc_thread().message_loop_proxy().get(), + shutdown_event()).release(); + channel->InitByMode(channel_name(), mode(), create_pipe_now_); return channel; } @@ -345,9 +346,10 @@ class TwoStepClient : public Worker { } virtual SyncChannel* CreateChannel() OVERRIDE { - SyncChannel* channel = new SyncChannel( - this, ipc_thread().message_loop_proxy().get(), shutdown_event()); - channel->Init(channel_name(), mode(), create_pipe_now_); + SyncChannel* channel = SyncChannel::Create( + this, ipc_thread().message_loop_proxy().get(), + shutdown_event()).release(); + channel->InitByMode(channel_name(), mode(), create_pipe_now_); return channel; } @@ -1135,13 +1137,12 @@ class RestrictedDispatchClient : public Worker { else LOG(ERROR) << "Send failed to dispatch incoming message on same channel"; - non_restricted_channel_.reset( - new SyncChannel("non_restricted_channel", - Channel::MODE_CLIENT, - this, - ipc_thread().message_loop_proxy().get(), - true, - shutdown_event())); + non_restricted_channel_ = + SyncChannel::CreateClient("non_restricted_channel", + this, + ipc_thread().message_loop_proxy().get(), + true, + shutdown_event()); server_->ListenerThread()->message_loop()->PostTask( FROM_HERE, base::Bind(&RestrictedDispatchServer::OnDoPing, server_, 2)); @@ -1526,13 +1527,12 @@ class RestrictedDispatchPipeWorker : public Worker { if (is_first()) event1_->Signal(); event2_->Wait(); - other_channel_.reset( - new SyncChannel(other_channel_name_, - Channel::MODE_CLIENT, - this, - ipc_thread().message_loop_proxy().get(), - true, - shutdown_event())); + other_channel_ = SyncChannel::CreateClient( + other_channel_name_, + this, + ipc_thread().message_loop_proxy().get(), + true, + shutdown_event()); other_channel_->SetRestrictDispatchChannelGroup(group_); if (!is_first()) { event1_->Signal(); @@ -1606,13 +1606,12 @@ class ReentrantReplyServer1 : public Worker { server_ready_(server_ready) { } virtual void Run() OVERRIDE { - server2_channel_.reset( - new SyncChannel("reentrant_reply2", - Channel::MODE_CLIENT, - this, - ipc_thread().message_loop_proxy().get(), - true, - shutdown_event())); + server2_channel_ = SyncChannel::CreateClient( + "reentrant_reply2", + this, + ipc_thread().message_loop_proxy().get(), + true, + shutdown_event()); server_ready_->Signal(); Message* msg = new SyncChannelTestMsg_Reentrant1(); server2_channel_->Send(msg); diff --git a/ipc/ipc_test_base.cc b/ipc/ipc_test_base.cc index ca45d16..3d4ca88 100644 --- a/ipc/ipc_test_base.cc +++ b/ipc/ipc_test_base.cc @@ -77,10 +77,10 @@ void IPCTestBase::CreateChannelProxy( base::SingleThreadTaskRunner* ipc_task_runner) { CHECK(!channel_.get()); CHECK(!channel_proxy_.get()); - channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_), - IPC::Channel::MODE_SERVER, - listener, - ipc_task_runner)); + channel_proxy_ = IPC::ChannelProxy::CreateServer( + GetChannelName(test_client_name_), + listener, + ipc_task_runner); } void IPCTestBase::DestroyChannelProxy() { |