Fix thread usage in chromoting host

There are several things done in this patch:
1. Isloate thread start and stop to ChromotingHostContext
2. SessionManager now doesn't own capturer and encoder, ownership moved to ChromotingHost
3. Fix up the sequence of actions when ChromotingHost shuts down

TEST=remoting_unittests
BUG=none

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51050 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/remoting/host/simple_host_process.cc b/remoting/host/simple_host_process.cc
index 0f12c43..8697b3d 100644
--- a/remoting/host/simple_host_process.cc
+++ b/remoting/host/simple_host_process.cc
@@ -27,6 +27,7 @@
 #include "base/waitable_event.h"
 #include "remoting/host/capturer_fake.h"
 #include "remoting/host/chromoting_host.h"
+#include "remoting/host/chromoting_host_context.h"
 #include "remoting/host/encoder_verbatim.h"
 #include "remoting/host/json_host_config.h"
 
@@ -52,6 +53,10 @@
 static char* GetEnvironmentVar(const char* x) { return getenv(x); }
 #endif
 
+void ShutdownTask(MessageLoop* message_loop) {
+  message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+}
+
 const std::string kFakeSwitchName = "fake";
 const std::string kConfigSwitchName = "config";
 
@@ -96,6 +101,7 @@
 
   if (fake) {
     // Inject a fake capturer.
+    LOG(INFO) << "Usage a fake capturer.";
     capturer.reset(new remoting::CapturerFake());
   }
 
@@ -111,16 +117,25 @@
     return 1;
   }
 
-  base::WaitableEvent host_done(false, false);
+  // Allocate a chromoting context and starts it.
+  remoting::ChromotingHostContext context;
+  context.Start();
+
+  // Construct a chromoting host.
   scoped_refptr<remoting::ChromotingHost> host =
-      new remoting::ChromotingHost(config,
+      new remoting::ChromotingHost(&context,
+                                   config,
                                    capturer.release(),
                                    encoder.release(),
-                                   executor.release(),
-                                   &host_done);
-  host->Run();
-  host_done.Wait();
+                                   executor.release());
 
+  // Let the chromoting host runs until the shutdown task is executed.
+  MessageLoop message_loop;
+  host->Start(NewRunnableFunction(&ShutdownTask, &message_loop));
+  message_loop.Run();
+
+  // And then stop the chromoting context.
+  context.Stop();
   file_io_thread.Stop();
   return 0;
 }