Allow proxy channels to be created without initializing the underlying channel.
This fixes a bug where a client needed to guarantee a message filter was in
place before any messages were received.
It also follows the style of not having constructors that do complex
initialization.
BUG=102894
TEST=none


Review URL: http://codereview.chromium.org/8417054

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110229 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 952394afc0a0dc5e3e6a1f3c2e1c0fa99b7b681f
diff --git a/ipc/ipc_sync_channel_unittest.cc b/ipc/ipc_sync_channel_unittest.cc
index 029e49c..6982b28 100644
--- a/ipc/ipc_sync_channel_unittest.cc
+++ b/ipc/ipc_sync_channel_unittest.cc
@@ -120,6 +120,7 @@
     DCHECK_EQ(answer, (succeed ? 10 : 0));
     return result;
   }
+  const std::string& channel_name() { return channel_name_; }
   Channel::Mode mode() { return mode_; }
   WaitableEvent* done_event() { return done_.get(); }
   WaitableEvent* shutdown_event() { return &shutdown_event_; }
@@ -156,6 +157,12 @@
     NOTREACHED();
   }
 
+  virtual SyncChannel* CreateChannel() {
+    return new SyncChannel(
+        channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
+        &shutdown_event_);
+  }
+
   base::Thread* ListenerThread() {
     return overrided_thread_ ? overrided_thread_ : &listener_thread_;
   }
@@ -167,9 +174,7 @@
   void OnStart() {
     // Link ipc_thread_, listener_thread_ and channel_ altogether.
     StartThread(&ipc_thread_, MessageLoop::TYPE_IO);
-    channel_.reset(new SyncChannel(
-        channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true,
-        &shutdown_event_));
+    channel_.reset(CreateChannel());
     channel_created_->Signal();
     Run();
   }
@@ -311,6 +316,74 @@
 
 namespace {
 
+// Worker classes which override how the sync channel is created to use the
+// two-step initialization (calling the lightweight constructor and then
+// ChannelProxy::Init separately) process.
+class TwoStepServer : public Worker {
+ public:
+  explicit TwoStepServer(bool create_pipe_now)
+      : Worker(Channel::MODE_SERVER, "simpler_server"),
+        create_pipe_now_(create_pipe_now) { }
+
+  void Run() {
+    SendAnswerToLife(false, base::kNoTimeout, true);
+    Done();
+  }
+
+  virtual SyncChannel* CreateChannel() {
+    SyncChannel* channel = new SyncChannel(
+        this, ipc_thread().message_loop_proxy(), shutdown_event());
+    channel->Init(channel_name(), mode(), create_pipe_now_);
+    return channel;
+  }
+
+  bool create_pipe_now_;
+};
+
+class TwoStepClient : public Worker {
+ public:
+  TwoStepClient(bool create_pipe_now)
+      : Worker(Channel::MODE_CLIENT, "simple_client"),
+        create_pipe_now_(create_pipe_now) { }
+
+  void OnAnswer(int* answer) {
+    *answer = 42;
+    Done();
+  }
+
+  virtual SyncChannel* CreateChannel() {
+    SyncChannel* channel = new SyncChannel(
+        this, ipc_thread().message_loop_proxy(), shutdown_event());
+    channel->Init(channel_name(), mode(), create_pipe_now_);
+    return channel;
+  }
+
+  bool create_pipe_now_;
+};
+
+void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) {
+  std::vector<Worker*> workers;
+  workers.push_back(new TwoStepServer(create_server_pipe_now));
+  workers.push_back(new TwoStepClient(create_client_pipe_now));
+  RunTest(workers);
+}
+
+}  // namespace
+
+// Tests basic two-step initialization, where you call the lightweight
+// constructor then Init.
+TEST_F(IPCSyncChannelTest, TwoStepInitialization) {
+  TwoStep(false, false);
+  TwoStep(false, true);
+  TwoStep(true, false);
+  TwoStep(true, true);
+}
+
+
+//-----------------------------------------------------------------------------
+
+namespace {
+
 class DelayClient : public Worker {
  public:
   DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { }