[Chromoting] The desktop process now connects back to the daemon process.

BUG=134694


Review URL: https://chromiumcodereview.appspot.com/11048056

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162667 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/remoting/host/desktop_process.cc b/remoting/host/desktop_process.cc
index 4a4ab6d3..e333e71 100644
--- a/remoting/host/desktop_process.cc
+++ b/remoting/host/desktop_process.cc
@@ -10,10 +10,16 @@
 #include "base/at_exit.h"
 #include "base/command_line.h"
 #include "base/file_path.h"
+#include "base/memory/ref_counted.h"
+#include "base/message_loop.h"
+#include "base/run_loop.h"
 #include "base/scoped_native_library.h"
 #include "base/stringprintf.h"
 #include "base/utf_string_conversions.h"
 #include "base/win/windows_version.h"
+#include "ipc/ipc_channel_proxy.h"
+#include "remoting/base/auto_thread.h"
+#include "remoting/base/auto_thread_task_runner.h"
 #include "remoting/host/host_exit_codes.h"
 #include "remoting/host/logging.h"
 #include "remoting/host/usage_stats_consent.h"
@@ -28,6 +34,11 @@
 
 namespace {
 
+// The command line switch specifying the name of the daemon IPC endpoint.
+const char kDaemonIpcSwitchName[] = "daemon-pipe";
+
+const char kIoThreadName[] = "I/O thread";
+
 // "--help" or "--?" prints the usage message.
 const char kHelpSwitchName[] = "help";
 const char kQuestionSwitchName[] = "?";
@@ -48,14 +59,50 @@
 
 namespace remoting {
 
-DesktopProcess::DesktopProcess() {
+DesktopProcess::DesktopProcess(const std::string& daemon_channel_name)
+    : daemon_channel_name_(daemon_channel_name) {
 }
 
 DesktopProcess::~DesktopProcess() {
 }
 
-int DesktopProcess::Run() {
+bool DesktopProcess::OnMessageReceived(const IPC::Message& message) {
+  return false;
+}
+
+void DesktopProcess::OnChannelConnected(int32 peer_pid) {
   NOTIMPLEMENTED();
+}
+
+void DesktopProcess::OnChannelError() {
+  LOG(ERROR) << "Failed to connect to '" << daemon_channel_name_ << "'";
+  daemon_channel_.reset();
+}
+
+int DesktopProcess::Run() {
+  // Create the UI message loop.
+  MessageLoop message_loop(MessageLoop::TYPE_UI);
+
+  {
+    scoped_refptr<AutoThreadTaskRunner> ui_task_runner =
+        new remoting::AutoThreadTaskRunner(message_loop.message_loop_proxy(),
+                                           MessageLoop::QuitClosure());
+
+    // Launch the I/O thread.
+    scoped_refptr<AutoThreadTaskRunner> io_task_runner =
+        AutoThread::CreateWithType(kIoThreadName, ui_task_runner,
+                                   MessageLoop::TYPE_IO);
+
+    // Connect to the daemon.
+    daemon_channel_.reset(new IPC::ChannelProxy(daemon_channel_name_,
+                                                IPC::Channel::MODE_CLIENT,
+                                                this,
+                                                io_task_runner));
+  }
+
+  // Run the UI message loop.
+  base::RunLoop run_loop;
+  run_loop.Run();
   return 0;
 }
 
@@ -82,7 +129,15 @@
     return remoting::kSuccessExitCode;
   }
 
-  remoting::DesktopProcess desktop_process;
+  std::string channel_name =
+      command_line->GetSwitchValueASCII(kDaemonIpcSwitchName);
+
+  if (channel_name.empty()) {
+    usage(command_line->GetProgram());
+    return remoting::kUsageExitCode;
+  }
+
+  remoting::DesktopProcess desktop_process(channel_name);
   return desktop_process.Run();
 }