diff options
author | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-30 03:58:59 +0000 |
---|---|---|
committer | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-30 03:58:59 +0000 |
commit | e482111a87c5415af8aaf33636f00c650c19b61e (patch) | |
tree | 2aaac1052209eb2ff1d1e3be3dac7f9010bef8b7 | |
parent | 1df3479c2fff2122dd136c8eb2838034324fae9e (diff) | |
download | chromium_src-e482111a87c5415af8aaf33636f00c650c19b61e.zip chromium_src-e482111a87c5415af8aaf33636f00c650c19b61e.tar.gz chromium_src-e482111a87c5415af8aaf33636f00c650c19b61e.tar.bz2 |
Introduce IPC::Channel::Create*() to ensure it being heap-allocated.
This change introduces IPC::Channel::Create*() API to turn
IPC::Channel into a heap allocated object. This will allow us to
make Channel a polymorphic class.
This change also tries to hide Channel::Mode from public API
so that we can simplify channel creation code paths cleaner in
following changes. ChannelProxy has to follow same pattern to
finish this cleanup. Such changes will follow.
TEST=none
BUG=377980
R=darin@chromium.org,cpu@chromium.org
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=273575
Review URL: https://codereview.chromium.org/307653003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273713 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 235 insertions, 142 deletions
diff --git a/chrome/browser/pepper_flash_settings_manager.cc b/chrome/browser/pepper_flash_settings_manager.cc index 1945d67..7a3230f 100644 --- a/chrome/browser/pepper_flash_settings_manager.cc +++ b/chrome/browser/pepper_flash_settings_manager.cc @@ -353,7 +353,7 @@ void PepperFlashSettingsManager::Core::ConnectToChannel( return; } - channel_.reset(new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this)); + channel_ = IPC::Channel::CreateClient(handle, this); if (!channel_->Connect()) { DLOG(ERROR) << "Couldn't connect to plugin"; NotifyErrorFromIOThread(); diff --git a/chrome/utility/importer/firefox_importer_unittest_utils_mac.cc b/chrome/utility/importer/firefox_importer_unittest_utils_mac.cc index 5c385de..9608f63 100644 --- a/chrome/utility/importer/firefox_importer_unittest_utils_mac.cc +++ b/chrome/utility/importer/firefox_importer_unittest_utils_mac.cc @@ -130,9 +130,7 @@ bool FFUnitTestDecryptorProxy::Setup(const base::FilePath& nss_path) { message_loop_.reset(new base::MessageLoopForIO()); listener_.reset(new FFDecryptorServerChannelListener()); - channel_.reset(new IPC::Channel(kTestChannelID, - IPC::Channel::MODE_SERVER, - listener_.get())); + channel_ = IPC::Channel::CreateServer(kTestChannelID, listener_.get()); CHECK(channel_->Connect()); listener_->SetSender(channel_.get()); @@ -264,9 +262,10 @@ MULTIPROCESS_IPC_TEST_MAIN(NSSDecrypterChildProcess) { base::MessageLoopForIO main_message_loop; FFDecryptorClientChannelListener listener; - IPC::Channel channel(kTestChannelID, IPC::Channel::MODE_CLIENT, &listener); - CHECK(channel.Connect()); - listener.SetSender(&channel); + scoped_ptr<IPC::Channel> channel = IPC::Channel::CreateClient( + kTestChannelID, &listener); + CHECK(channel->Connect()); + listener.SetSender(channel.get()); // run message loop base::MessageLoop::current()->Run(); diff --git a/cloud_print/service/win/service_listener.cc b/cloud_print/service/win/service_listener.cc index 53622a8..bdb2217 100644 --- a/cloud_print/service/win/service_listener.cc +++ b/cloud_print/service/win/service_listener.cc @@ -91,8 +91,8 @@ void ServiceListener::Connect() { SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | FILE_FLAG_OVERLAPPED, NULL)); if (handle.IsValid()) { - channel_.reset(new IPC::Channel(IPC::ChannelHandle(handle), - IPC::Channel::MODE_CLIENT, this)); + channel_ = IPC::Channel::CreateClient(IPC::ChannelHandle(handle), + this); channel_->Connect(); } else { ipc_thread_->message_loop()->PostDelayedTask( diff --git a/cloud_print/service/win/setup_listener.cc b/cloud_print/service/win/setup_listener.cc index dd1cb37..c0b6d1b 100644 --- a/cloud_print/service/win/setup_listener.cc +++ b/cloud_print/service/win/setup_listener.cc @@ -117,8 +117,8 @@ void SetupListener::Connect(const base::string16& user) { IPC::Channel::kReadBufferSize, IPC::Channel::kReadBufferSize, 5000, &attribs)); if (pipe.IsValid()) { - channel_.reset(new IPC::Channel(IPC::ChannelHandle(pipe), - IPC::Channel::MODE_SERVER, this)); + channel_ = IPC::Channel::CreateServer(IPC::ChannelHandle(pipe), + this); channel_->Connect(); } } diff --git a/components/nacl/broker/nacl_broker_listener.cc b/components/nacl/broker/nacl_broker_listener.cc index c4f268da..bceed22 100644 --- a/components/nacl/broker/nacl_broker_listener.cc +++ b/components/nacl/broker/nacl_broker_listener.cc @@ -40,8 +40,7 @@ void NaClBrokerListener::Listen() { std::string channel_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( switches::kProcessChannelID); - channel_.reset(new IPC::Channel( - channel_name, IPC::Channel::MODE_CLIENT, this)); + channel_ = IPC::Channel::CreateClient(channel_name, this); CHECK(channel_->Connect()); base::MessageLoop::current()->Run(); } diff --git a/components/nacl/loader/nacl_ipc_adapter.cc b/components/nacl/loader/nacl_ipc_adapter.cc index 1398b22..d3160e4 100644 --- a/components/nacl/loader/nacl_ipc_adapter.cc +++ b/components/nacl/loader/nacl_ipc_adapter.cc @@ -329,8 +329,7 @@ NaClIPCAdapter::NaClIPCAdapter(const IPC::ChannelHandle& handle, cond_var_(&lock_), task_runner_(runner), locked_data_() { - io_thread_data_.channel_.reset( - new IPC::Channel(handle, IPC::Channel::MODE_SERVER, this)); + io_thread_data_.channel_ = IPC::Channel::CreateServer(handle, this); // Note, we can not PostTask for ConnectChannelOnIOThread here. If we did, // and that task ran before this constructor completes, the reference count // would go to 1 and then to 0 because of the Task, before we've been returned diff --git a/content/browser/plugin_data_remover_impl.cc b/content/browser/plugin_data_remover_impl.cc index 9133e59..a318e72 100644 --- a/content/browser/plugin_data_remover_impl.cc +++ b/content/browser/plugin_data_remover_impl.cc @@ -226,7 +226,7 @@ class PluginDataRemoverImpl::Context return; DCHECK(!channel_.get()); - channel_.reset(new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this)); + channel_ = IPC::Channel::CreateClient(handle, this); if (!channel_->Connect()) { NOTREACHED() << "Couldn't connect to plugin"; SignalDone(); diff --git a/content/browser/plugin_service_impl_browsertest.cc b/content/browser/plugin_service_impl_browsertest.cc index 44f4754..7a3271e 100644 --- a/content/browser/plugin_service_impl_browsertest.cc +++ b/content/browser/plugin_service_impl_browsertest.cc @@ -60,7 +60,7 @@ class MockPluginProcessHostClient : public PluginProcessHost::Client, ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); ASSERT_TRUE(set_plugin_info_called_); ASSERT_TRUE(!channel_); - channel_ = new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this); + channel_ = IPC::Channel::CreateClient(handle, this).release(); ASSERT_TRUE(channel_->Connect()); } diff --git a/content/common/child_process_host_impl.cc b/content/common/child_process_host_impl.cc index 9ae7c82..1dccab0 100644 --- a/content/common/child_process_host_impl.cc +++ b/content/common/child_process_host_impl.cc @@ -164,8 +164,7 @@ void ChildProcessHostImpl::ForceShutdown() { std::string ChildProcessHostImpl::CreateChannel() { channel_id_ = IPC::Channel::GenerateVerifiedChannelID(std::string()); - channel_.reset(new IPC::Channel( - channel_id_, IPC::Channel::MODE_SERVER, this)); + channel_ = IPC::Channel::CreateServer(channel_id_, this); if (!channel_->Connect()) return std::string(); diff --git a/content/renderer/render_thread_impl_browsertest.cc b/content/renderer/render_thread_impl_browsertest.cc index 6715ef3..11d486e 100644 --- a/content/renderer/render_thread_impl_browsertest.cc +++ b/content/renderer/render_thread_impl_browsertest.cc @@ -57,8 +57,9 @@ TEST_F(RenderThreadImplBrowserTest, std::string channel_id = IPC::Channel::GenerateVerifiedChannelID( std::string()); DummyListener dummy_listener; - IPC::Channel channel(channel_id, IPC::Channel::MODE_SERVER, &dummy_listener); - ASSERT_TRUE(channel.Connect()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateServer(channel_id, &dummy_listener)); + ASSERT_TRUE(channel->Connect()); scoped_ptr<MockRenderProcess> mock_process(new MockRenderProcess); // Owned by mock_process. diff --git a/ipc/BUILD.gn b/ipc/BUILD.gn index 9d9e49a..3bff55e 100644 --- a/ipc/BUILD.gn +++ b/ipc/BUILD.gn @@ -8,6 +8,7 @@ component("ipc") { "file_descriptor_set_posix.h", "ipc_channel.cc", "ipc_channel.h", + "ipc_channel_common.cc", "ipc_channel_factory.cc", "ipc_channel_factory.h", "ipc_channel_handle.h", diff --git a/ipc/ipc.gypi b/ipc/ipc.gypi index fd23fb0..e7d5cc6 100644 --- a/ipc/ipc.gypi +++ b/ipc/ipc.gypi @@ -15,6 +15,7 @@ 'file_descriptor_set_posix.h', 'ipc_channel.cc', 'ipc_channel.h', + 'ipc_channel_common.cc', 'ipc_channel_factory.cc', 'ipc_channel_factory.h', 'ipc_channel_handle.h', diff --git a/ipc/ipc_channel.h b/ipc/ipc_channel.h index 4426e5d..be8f83c 100644 --- a/ipc/ipc_channel.h +++ b/ipc/ipc_channel.h @@ -55,21 +55,15 @@ class IPC_EXPORT Channel : public Sender { }; // Some Standard Modes + // TODO(morrita): These are under deprecation work. You should use Create*() + // functions instead. enum Mode { MODE_NONE = MODE_NO_FLAG, MODE_SERVER = MODE_SERVER_FLAG, MODE_CLIENT = MODE_CLIENT_FLAG, - // Channels on Windows are named by default and accessible from other - // processes. On POSIX channels are anonymous by default and not accessible - // from other processes. Named channels work via named unix domain sockets. - // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and - // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG, MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG, #if defined(OS_POSIX) - // An "open" named server accepts connections from ANY client. - // The caller must then implement their own access-control based on the - // client process' user Id. MODE_OPEN_NAMED_SERVER = MODE_OPEN_ACCESS_FLAG | MODE_SERVER_FLAG | MODE_NAMED_FLAG #endif @@ -106,15 +100,47 @@ class IPC_EXPORT Channel : public Sender { // the file descriptor in the channel handle is != -1, the channel takes // ownership of the file descriptor and will close it appropriately, otherwise // it will create a new descriptor internally. - // |mode| specifies whether this Channel is to operate in server mode or - // client mode. In server mode, the Channel is responsible for setting up the - // IPC object, whereas in client mode, the Channel merely connects to the - // already established IPC object. // |listener| receives a callback on the current thread for each newly // received message. // - Channel(const IPC::ChannelHandle &channel_handle, Mode mode, - Listener* listener); + // There are four type of modes how channels operate: + // + // - Server and named server: In these modes, the Channel is + // responsible for settingb up the IPC object + // - An "open" named server: It accepts connections from ANY client. + // The caller must then implement their own access-control based on the + // client process' user Id. + // - Client and named client: In these mode, the Channel merely + // connects to the already established IPC object. + // + // Each mode has its own Create*() API to create the Channel object. + // + // TODO(morrita): Replace CreateByModeForProxy() with one of above Create*(). + // + static scoped_ptr<Channel> CreateByModeForProxy( + const IPC::ChannelHandle &channel_handle, Mode mode,Listener* listener); + static scoped_ptr<Channel> CreateClient( + const IPC::ChannelHandle &channel_handle, Listener* listener); + + // Channels on Windows are named by default and accessible from other + // processes. On POSIX channels are anonymous by default and not accessible + // from other processes. Named channels work via named unix domain sockets. + // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and + // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. + static scoped_ptr<Channel> CreateNamedServer( + const IPC::ChannelHandle &channel_handle, Listener* listener); + static scoped_ptr<Channel> CreateNamedClient( + const IPC::ChannelHandle &channel_handle, Listener* listener); +#if defined(OS_POSIX) + // An "open" named server accepts connections from ANY client. + // The caller must then implement their own access-control based on the + // client process' user Id. + static scoped_ptr<Channel> CreateOpenNamedServer( + const IPC::ChannelHandle &channel_handle, Listener* listener); +#endif + static scoped_ptr<Channel> CreateServer( + const IPC::ChannelHandle &channel_handle, Listener* listener); + virtual ~Channel(); @@ -220,6 +246,9 @@ class IPC_EXPORT Channel : public Sender { Channel() : channel_impl_(0) { } private: + Channel(const IPC::ChannelHandle &channel_handle, Mode mode, + Listener* listener); + // PIMPL to which all channel calls are delegated. class ChannelImpl; ChannelImpl *channel_impl_; diff --git a/ipc/ipc_channel_common.cc b/ipc/ipc_channel_common.cc new file mode 100644 index 0000000..c5ceba3e --- /dev/null +++ b/ipc/ipc_channel_common.cc @@ -0,0 +1,55 @@ +// Copyright 2014 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 "ipc/ipc_channel.h" + +namespace IPC { + +// static +scoped_ptr<Channel> Channel::CreateByModeForProxy( + const IPC::ChannelHandle &channel_handle, Mode mode, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, mode, listener)); +} + +// static +scoped_ptr<Channel> Channel::CreateClient( + const IPC::ChannelHandle &channel_handle, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, Channel::MODE_CLIENT, listener)); +} + +// static +scoped_ptr<Channel> Channel::CreateNamedServer( + const IPC::ChannelHandle &channel_handle, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, Channel::MODE_NAMED_SERVER, listener)); +} + +// static +scoped_ptr<Channel> Channel::CreateNamedClient( + const IPC::ChannelHandle &channel_handle, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, Channel::MODE_NAMED_CLIENT, listener)); +} + +#if defined(OS_POSIX) +// static +scoped_ptr<Channel> Channel::CreateOpenNamedServer( + const IPC::ChannelHandle &channel_handle, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, Channel::MODE_OPEN_NAMED_SERVER, listener)); +} +#endif + +// static +scoped_ptr<Channel> Channel::CreateServer( + const IPC::ChannelHandle &channel_handle, Listener* listener) { + return make_scoped_ptr( + new Channel(channel_handle, Channel::MODE_SERVER, listener)); +} + + +} // namespace IPC + diff --git a/ipc/ipc_channel_posix_unittest.cc b/ipc/ipc_channel_posix_unittest.cc index 6f3962b..d001920 100644 --- a/ipc/ipc_channel_posix_unittest.cc +++ b/ipc/ipc_channel_posix_unittest.cc @@ -204,12 +204,13 @@ TEST_F(IPCChannelPosixTest, BasicListen) { IPC::ChannelHandle handle(kChannelName); SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); unlink(handle.name.c_str()); - IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); - ASSERT_TRUE(channel.Connect()); - ASSERT_TRUE(channel.AcceptsConnections()); - ASSERT_FALSE(channel.HasAcceptedConnection()); - channel.ResetToAcceptingConnectionState(); - ASSERT_FALSE(channel.HasAcceptedConnection()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(handle, NULL)); + ASSERT_TRUE(channel->Connect()); + ASSERT_TRUE(channel->AcceptsConnections()); + ASSERT_FALSE(channel->HasAcceptedConnection()); + channel->ResetToAcceptingConnectionState(); + ASSERT_FALSE(channel->HasAcceptedConnection()); } TEST_F(IPCChannelPosixTest, BasicConnected) { @@ -221,17 +222,18 @@ TEST_F(IPCChannelPosixTest, BasicConnected) { base::FileDescriptor fd(pipe_fds[0], false); IPC::ChannelHandle handle(socket_name, fd); - IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL); - ASSERT_TRUE(channel.Connect()); - ASSERT_FALSE(channel.AcceptsConnections()); - channel.Close(); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateServer(handle, NULL)); + ASSERT_TRUE(channel->Connect()); + ASSERT_FALSE(channel->AcceptsConnections()); + channel->Close(); ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); // Make sure that we can use the socket that is created for us by // a standard channel. - IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL); - ASSERT_TRUE(channel2.Connect()); - ASSERT_FALSE(channel2.AcceptsConnections()); + scoped_ptr<IPC::Channel> channel2( + IPC::Channel::CreateServer(socket_name, NULL)); + ASSERT_TRUE(channel2->Connect()); + ASSERT_FALSE(channel2->AcceptsConnections()); } // If a connection closes right before a Send() call, we may end up closing @@ -241,15 +243,17 @@ TEST_F(IPCChannelPosixTest, SendHangTest) { IPCChannelPosixTestListener out_listener(true); IPCChannelPosixTestListener in_listener(true); IPC::ChannelHandle in_handle("IN"); - IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener); - base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false); + scoped_ptr<IPC::Channel> in_chan( + IPC::Channel::CreateServer(in_handle, &in_listener)); + base::FileDescriptor out_fd(in_chan->TakeClientFileDescriptor(), false); IPC::ChannelHandle out_handle("OUT", out_fd); - IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener); - ASSERT_TRUE(in_chan.Connect()); - ASSERT_TRUE(out_chan.Connect()); - in_chan.Close(); // simulate remote process dying at an unfortunate time. + scoped_ptr<IPC::Channel> out_chan( + IPC::Channel::CreateClient(out_handle, &out_listener)); + ASSERT_TRUE(in_chan->Connect()); + ASSERT_TRUE(out_chan->Connect()); + in_chan->Close(); // simulate remote process dying at an unfortunate time. // Send will fail, because it cannot write the message. - ASSERT_FALSE(out_chan.Send(new IPC::Message( + ASSERT_FALSE(out_chan->Send(new IPC::Message( 0, // routing_id kQuitMessage, // message type IPC::Message::PRIORITY_NORMAL))); @@ -264,13 +268,15 @@ TEST_F(IPCChannelPosixTest, AcceptHangTest) { IPCChannelPosixTestListener out_listener(true); IPCChannelPosixTestListener in_listener(true); IPC::ChannelHandle in_handle("IN"); - IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener); - base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false); + scoped_ptr<IPC::Channel> in_chan( + IPC::Channel::CreateServer(in_handle, &in_listener)); + base::FileDescriptor out_fd(in_chan->TakeClientFileDescriptor(), false); IPC::ChannelHandle out_handle("OUT", out_fd); - IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener); - ASSERT_TRUE(in_chan.Connect()); - in_chan.Close(); // simulate remote process dying at an unfortunate time. - ASSERT_FALSE(out_chan.Connect()); + scoped_ptr<IPC::Channel> out_chan( + IPC::Channel::CreateClient(out_handle, &out_listener)); + ASSERT_TRUE(in_chan->Connect()); + in_chan->Close(); // simulate remote process dying at an unfortunate time. + ASSERT_FALSE(out_chan->Connect()); SpinRunLoop(TestTimeouts::action_max_timeout()); ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); } @@ -280,26 +286,27 @@ TEST_F(IPCChannelPosixTest, AdvancedConnected) { IPCChannelPosixTestListener listener(false); IPC::ChannelHandle chan_handle(GetConnectionSocketName()); SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); - ASSERT_TRUE(channel.Connect()); - ASSERT_TRUE(channel.AcceptsConnections()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(chan_handle, &listener)); + ASSERT_TRUE(channel->Connect()); + ASSERT_TRUE(channel->AcceptsConnections()); + ASSERT_FALSE(channel->HasAcceptedConnection()); base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); ASSERT_TRUE(handle); SpinRunLoop(TestTimeouts::action_max_timeout()); ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); - ASSERT_TRUE(channel.HasAcceptedConnection()); + ASSERT_TRUE(channel->HasAcceptedConnection()); IPC::Message* message = new IPC::Message(0, // routing_id kQuitMessage, // message type IPC::Message::PRIORITY_NORMAL); - channel.Send(message); + channel->Send(message); SpinRunLoop(TestTimeouts::action_timeout()); int exit_code = 0; EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); EXPECT_EQ(0, exit_code); ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + ASSERT_FALSE(channel->HasAcceptedConnection()); } TEST_F(IPCChannelPosixTest, ResetState) { @@ -309,42 +316,44 @@ TEST_F(IPCChannelPosixTest, ResetState) { IPCChannelPosixTestListener listener(false); IPC::ChannelHandle chan_handle(GetConnectionSocketName()); SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); - ASSERT_TRUE(channel.Connect()); - ASSERT_TRUE(channel.AcceptsConnections()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(chan_handle, &listener)); + ASSERT_TRUE(channel->Connect()); + ASSERT_TRUE(channel->AcceptsConnections()); + ASSERT_FALSE(channel->HasAcceptedConnection()); base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); ASSERT_TRUE(handle); SpinRunLoop(TestTimeouts::action_max_timeout()); ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); - ASSERT_TRUE(channel.HasAcceptedConnection()); - channel.ResetToAcceptingConnectionState(); - ASSERT_FALSE(channel.HasAcceptedConnection()); + ASSERT_TRUE(channel->HasAcceptedConnection()); + channel->ResetToAcceptingConnectionState(); + ASSERT_FALSE(channel->HasAcceptedConnection()); base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc"); ASSERT_TRUE(handle2); SpinRunLoop(TestTimeouts::action_max_timeout()); ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); - ASSERT_TRUE(channel.HasAcceptedConnection()); + ASSERT_TRUE(channel->HasAcceptedConnection()); IPC::Message* message = new IPC::Message(0, // routing_id kQuitMessage, // message type IPC::Message::PRIORITY_NORMAL); - channel.Send(message); + channel->Send(message); SpinRunLoop(TestTimeouts::action_timeout()); EXPECT_TRUE(base::KillProcess(handle, 0, false)); int exit_code = 0; EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); EXPECT_EQ(0, exit_code); ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + ASSERT_FALSE(channel->HasAcceptedConnection()); } TEST_F(IPCChannelPosixTest, BadChannelName) { // Test empty name IPC::ChannelHandle handle(""); - IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); - ASSERT_FALSE(channel.Connect()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(handle, NULL)); + ASSERT_FALSE(channel->Connect()); // Test name that is too long. const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" @@ -356,8 +365,9 @@ TEST_F(IPCChannelPosixTest, BadChannelName) { "leading-edge_processes"; EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); IPC::ChannelHandle handle2(kTooLongName); - IPC::Channel channel2(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL); - EXPECT_FALSE(channel2.Connect()); + scoped_ptr<IPC::Channel> channel2( + IPC::Channel::CreateNamedServer(handle2, NULL)); + EXPECT_FALSE(channel2->Connect()); } TEST_F(IPCChannelPosixTest, MultiConnection) { @@ -366,16 +376,17 @@ TEST_F(IPCChannelPosixTest, MultiConnection) { IPCChannelPosixTestListener listener(false); IPC::ChannelHandle chan_handle(GetConnectionSocketName()); SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); - ASSERT_TRUE(channel.Connect()); - ASSERT_TRUE(channel.AcceptsConnections()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(chan_handle, &listener)); + ASSERT_TRUE(channel->Connect()); + ASSERT_TRUE(channel->AcceptsConnections()); + ASSERT_FALSE(channel->HasAcceptedConnection()); base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); ASSERT_TRUE(handle); SpinRunLoop(TestTimeouts::action_max_timeout()); ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); - ASSERT_TRUE(channel.HasAcceptedConnection()); + ASSERT_TRUE(channel->HasAcceptedConnection()); base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc"); ASSERT_TRUE(handle2); SpinRunLoop(TestTimeouts::action_max_timeout()); @@ -383,16 +394,16 @@ TEST_F(IPCChannelPosixTest, MultiConnection) { EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); EXPECT_EQ(exit_code, 0); ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); - ASSERT_TRUE(channel.HasAcceptedConnection()); + ASSERT_TRUE(channel->HasAcceptedConnection()); IPC::Message* message = new IPC::Message(0, // routing_id kQuitMessage, // message type IPC::Message::PRIORITY_NORMAL); - channel.Send(message); + channel->Send(message); SpinRunLoop(TestTimeouts::action_timeout()); EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); EXPECT_EQ(exit_code, 0); ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); - ASSERT_FALSE(channel.HasAcceptedConnection()); + ASSERT_FALSE(channel->HasAcceptedConnection()); } TEST_F(IPCChannelPosixTest, DoubleServer) { @@ -400,18 +411,21 @@ TEST_F(IPCChannelPosixTest, DoubleServer) { IPCChannelPosixTestListener listener(false); IPCChannelPosixTestListener listener2(false); IPC::ChannelHandle chan_handle(GetConnectionSocketName()); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_SERVER, &listener); - IPC::Channel channel2(chan_handle, IPC::Channel::MODE_SERVER, &listener2); - ASSERT_TRUE(channel.Connect()); - ASSERT_FALSE(channel2.Connect()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateServer(chan_handle, &listener)); + scoped_ptr<IPC::Channel> channel2( + IPC::Channel::CreateServer(chan_handle, &listener2)); + ASSERT_TRUE(channel->Connect()); + ASSERT_FALSE(channel2->Connect()); } TEST_F(IPCChannelPosixTest, BadMode) { // Test setting up two servers with a bad mode. IPCChannelPosixTestListener listener(false); IPC::ChannelHandle chan_handle(GetConnectionSocketName()); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener); - ASSERT_FALSE(channel.Connect()); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateByModeForProxy( + chan_handle, IPC::Channel::MODE_NONE, &listener)); + ASSERT_FALSE(channel->Connect()); } TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { @@ -421,10 +435,11 @@ TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( connection_socket_name)); - IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedServer(chan_handle, &listener)); ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( connection_socket_name)); - channel.Close(); + channel->Close(); ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( connection_socket_name)); } @@ -435,8 +450,9 @@ MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { IPCChannelPosixTestListener listener(true); IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); - IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); - EXPECT_TRUE(channel.Connect()); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedClient(handle, &listener)); + EXPECT_TRUE(channel->Connect()); IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); return 0; @@ -448,14 +464,15 @@ MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { IPCChannelPosixTestListener listener(false); IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); - IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); + scoped_ptr<IPC::Channel> channel( + IPC::Channel::CreateNamedClient(handle, &listener)); // In this case connect may succeed or fail depending on if the packet // actually gets sent at sendmsg. Since we never delay on send, we may not // see the error. However even if connect succeeds, eventually we will get an // error back since the channel will be closed when we attempt to read from // it. - bool connected = channel.Connect(); + bool connected = channel->Connect(); if (connected) { IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); diff --git a/ipc/ipc_channel_proxy.cc b/ipc/ipc_channel_proxy.cc index 7e32018..e88bb5d 100644 --- a/ipc/ipc_channel_proxy.cc +++ b/ipc/ipc_channel_proxy.cc @@ -52,7 +52,7 @@ void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, const Channel::Mode& mode) { DCHECK(!channel_); channel_id_ = handle.name; - channel_.reset(new Channel(handle, mode, this)); + channel_ = Channel::CreateByModeForProxy(handle, mode, this); } bool ChannelProxy::Context::TryFilters(const Message& message) { diff --git a/ipc/ipc_channel_proxy_unittest.cc b/ipc/ipc_channel_proxy_unittest.cc index bd2381c..20e42e3 100644 --- a/ipc/ipc_channel_proxy_unittest.cc +++ b/ipc/ipc_channel_proxy_unittest.cc @@ -428,11 +428,11 @@ TEST_F(IPCChannelBadMessageTest, BadMessage) { MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ChannelProxyClient) { base::MessageLoopForIO main_message_loop; ChannelReflectorListener listener; - IPC::Channel channel(IPCTestBase::GetChannelName("ChannelProxyClient"), - IPC::Channel::MODE_CLIENT, - &listener); - CHECK(channel.Connect()); - listener.Init(&channel); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName("ChannelProxyClient"), + &listener)); + CHECK(channel->Connect()); + listener.Init(channel.get()); base::MessageLoop::current()->Run(); return 0; diff --git a/ipc/ipc_channel_unittest.cc b/ipc/ipc_channel_unittest.cc index eea432a..b9665db 100644 --- a/ipc/ipc_channel_unittest.cc +++ b/ipc/ipc_channel_unittest.cc @@ -254,12 +254,12 @@ MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) { GenericChannelListener listener; // Set up IPC channel. - IPC::Channel channel(IPCTestBase::GetChannelName("GenericClient"), - IPC::Channel::MODE_CLIENT, - &listener); - CHECK(channel.Connect()); - listener.Init(&channel); - Send(&channel, "hello from child"); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName("GenericClient"), + &listener)); + CHECK(channel->Connect()); + listener.Init(channel.get()); + Send(channel.get(), "hello from child"); base::MessageLoop::current()->Run(); return 0; diff --git a/ipc/ipc_fuzzing_tests.cc b/ipc/ipc_fuzzing_tests.cc index 1ed9acd..4abcc4c 100644 --- a/ipc/ipc_fuzzing_tests.cc +++ b/ipc/ipc_fuzzing_tests.cc @@ -247,11 +247,11 @@ class FuzzerClientListener : public SimpleListener { MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) { base::MessageLoopForIO main_message_loop; FuzzerServerListener listener; - IPC::Channel channel(IPCTestBase::GetChannelName("FuzzServerClient"), - IPC::Channel::MODE_CLIENT, - &listener); - CHECK(channel.Connect()); - listener.Init(&channel); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName("FuzzServerClient"), + &listener)); + CHECK(channel->Connect()); + listener.Init(channel.get()); base::MessageLoop::current()->Run(); return 0; } diff --git a/ipc/ipc_perftests.cc b/ipc/ipc_perftests.cc index d4b1abd..3feabda 100644 --- a/ipc/ipc_perftests.cc +++ b/ipc/ipc_perftests.cc @@ -265,11 +265,10 @@ TEST_F(IPCChannelPerfTest, Performance) { MULTIPROCESS_IPC_TEST_CLIENT_MAIN(PerformanceClient) { base::MessageLoopForIO main_message_loop; ChannelReflectorListener listener; - IPC::Channel channel(IPCTestBase::GetChannelName("PerformanceClient"), - IPC::Channel::MODE_CLIENT, - &listener); - listener.Init(&channel); - CHECK(channel.Connect()); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName("PerformanceClient"), &listener)); + listener.Init(channel.get()); + CHECK(channel->Connect()); base::MessageLoop::current()->Run(); return 0; diff --git a/ipc/ipc_send_fds_test.cc b/ipc/ipc_send_fds_test.cc index aeec890..7e6a4e4 100644 --- a/ipc/ipc_send_fds_test.cc +++ b/ipc/ipc_send_fds_test.cc @@ -134,10 +134,10 @@ int SendFdsClientCommon(const std::string& test_client_name, MyChannelDescriptorListener listener(expected_inode_num); // Set up IPC channel. - IPC::Channel channel(IPCTestBase::GetChannelName(test_client_name), - IPC::Channel::MODE_CLIENT, - &listener); - CHECK(channel.Connect()); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName(test_client_name), + &listener)); + CHECK(channel->Connect()); // Run message loop. base::MessageLoop::current()->Run(); @@ -233,14 +233,10 @@ class PipeChannelHelper { void Init() { IPC::ChannelHandle in_handle("IN"); - in.reset(new IPC::Channel(in_handle, - IPC::Channel::MODE_SERVER, - &null_listener_)); + in = IPC::Channel::CreateServer(in_handle, &null_listener_); base::FileDescriptor out_fd(in->TakeClientFileDescriptor(), false); IPC::ChannelHandle out_handle("OUT", out_fd); - out.reset(new IPC::Channel(out_handle, - IPC::Channel::MODE_CLIENT, - &cb_listener_)); + out = IPC::Channel::CreateClient(out_handle, &cb_listener_); // PostTask the connect calls to make sure the callbacks happens // on the right threads. in_thread_->message_loop()->PostTask( diff --git a/ipc/ipc_test_base.cc b/ipc/ipc_test_base.cc index b609e8c..ca45d16 100644 --- a/ipc/ipc_test_base.cc +++ b/ipc/ipc_test_base.cc @@ -69,9 +69,7 @@ void IPCTestBase::CreateChannelFromChannelHandle( IPC::Listener* listener) { CHECK(!channel_.get()); CHECK(!channel_proxy_.get()); - channel_.reset(new IPC::Channel(channel_handle, - IPC::Channel::MODE_SERVER, - listener)); + channel_ = IPC::Channel::CreateServer(channel_handle, listener); } void IPCTestBase::CreateChannelProxy( diff --git a/ipc/sync_socket_unittest.cc b/ipc/sync_socket_unittest.cc index 2888607..5527abc 100644 --- a/ipc/sync_socket_unittest.cc +++ b/ipc/sync_socket_unittest.cc @@ -108,11 +108,11 @@ class SyncSocketServerListener : public IPC::Listener { MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SyncSocketServerClient) { base::MessageLoopForIO main_message_loop; SyncSocketServerListener listener; - IPC::Channel channel(IPCTestBase::GetChannelName("SyncSocketServerClient"), - IPC::Channel::MODE_CLIENT, - &listener); - EXPECT_TRUE(channel.Connect()); - listener.Init(&channel); + scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( + IPCTestBase::GetChannelName("SyncSocketServerClient"), + &listener)); + EXPECT_TRUE(channel->Connect()); + listener.Init(channel.get()); base::MessageLoop::current()->Run(); return 0; } |