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


CrOS-Libchrome-Original-Commit: e482111a87c5415af8aaf33636f00c650c19b61e
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 @@
   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 @@
 
   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 @@
   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 @@
   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 @@
   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 @@
   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 @@
                              "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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   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 @@
   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());