[Chromoting] Introducing refcount-based life time management of the message loops in the service (daemon) and me2me host (network) processes.

This CL introduces AutoMessageLoop wrapper that provides control over life time of a message loop via scoped_refptr references. This scheme is useful in the cases when shutdown code has to run on a particular thread or when the OS requires resources (such as windows) to be freed before exiting a message loop.

The CL switches threads, owned by remoting::HostService, remoting::HostProcess and remoting::ChromotingHostContext, to refcount-based lifetime management. This change required updating tear-down sequences in remoting_me2me_host and the host plugin code.

BUG=134694


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@154827 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/remoting/host/chromoting_host_context.h b/remoting/host/chromoting_host_context.h
index 149444a..497b8e3 100644
--- a/remoting/host/chromoting_host_context.h
+++ b/remoting/host/chromoting_host_context.h
@@ -5,22 +5,17 @@
 #ifndef REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
 #define REMOTING_HOST_CHROMOTING_HOST_CONTEXT_H_
 
-#include <string>
-
 #include "base/gtest_prod_util.h"
 #include "base/memory/ref_counted.h"
-#include "base/threading/platform_thread.h"
+#include "base/single_thread_task_runner.h"
 #include "base/threading/thread.h"
 
-namespace base {
-class SingleThreadTaskRunner;
-}  // namespace base
-
 namespace net {
 class URLRequestContextGetter;
 }  // namespace net
 
 namespace remoting {
+class AutoThreadTaskRunner;
 
 // A class that manages threads and running context for the chromoting host
 // process.  This class is virtual only for testing purposes (see below).
@@ -28,9 +23,11 @@
  public:
   // Create a context.
   ChromotingHostContext(
-      scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
+      scoped_refptr<AutoThreadTaskRunner> ui_task_runner);
   virtual ~ChromotingHostContext();
 
+  void ReleaseTaskRunners();
+
   // TODO(ajwong): Move the Start method out of this class. Then
   // create a static factory for construction, and destruction.  We
   // should be able to remove the need for virtual functions below
@@ -38,61 +35,70 @@
   // this API.
   virtual bool Start();
 
-  // Task runner for the thread that is used for the UI. In the NPAPI
-  // plugin this corresponds to the main plugin thread.
-  virtual base::SingleThreadTaskRunner* ui_task_runner();
+  // Task runner for the thread used for audio capture and encoding.
+  virtual base::SingleThreadTaskRunner* audio_task_runner();
 
   // Task runner for the thread used by the ScreenRecorder to capture
   // the screen.
   virtual base::SingleThreadTaskRunner* capture_task_runner();
 
-  // Task runner for the thread used to encode video streams.
-  virtual base::SingleThreadTaskRunner* encode_task_runner();
-
-  // Task runner for the thread used for audio capture and encoding.
-  virtual base::SingleThreadTaskRunner* audio_task_runner();
-
-  // Task runner for the thread used for network IO. This thread runs
-  // a libjingle message loop, and is the only thread on which
-  // libjingle code may be run.
-  virtual base::SingleThreadTaskRunner* network_task_runner();
-
   // Task runner for the thread that is used by the EventExecutor.
   //
   // TODO(sergeyu): Do we need a separate thread for EventExecutor?
   // Can we use some other thread instead?
   virtual base::SingleThreadTaskRunner* desktop_task_runner();
 
+  // Task runner for the thread used to encode video streams.
+  virtual base::SingleThreadTaskRunner* encode_task_runner();
+
   // Task runner for the thread that is used for blocking file
   // IO. This thread is used by the URLRequestContext to read proxy
   // configuration and by NatConfig to read policy configs.
   virtual base::SingleThreadTaskRunner* file_task_runner();
 
+  // Task runner for the thread used for network IO. This thread runs
+  // a libjingle message loop, and is the only thread on which
+  // libjingle code may be run.
+  virtual base::SingleThreadTaskRunner* network_task_runner();
+
+  // Task runner for the thread that is used for the UI. In the NPAPI
+  // plugin this corresponds to the main plugin thread.
+  virtual base::SingleThreadTaskRunner* ui_task_runner();
+
   const scoped_refptr<net::URLRequestContextGetter>&
       url_request_context_getter();
 
  private:
   FRIEND_TEST_ALL_PREFIXES(ChromotingHostContextTest, StartAndStop);
 
-  // A thread that hosts all network operations.
-  base::Thread network_thread_;
+  // A thread that hosts audio capture and encoding.
+  base::Thread audio_thread_;
 
   // A thread that hosts screen capture.
   base::Thread capture_thread_;
 
-  // A thread that hosts all encode operations.
-  base::Thread encode_thread_;
-
-  // A thread that hosts audio capture and encoding.
-  base::Thread audio_thread_;
-
   // A thread that hosts input injection.
   base::Thread desktop_thread_;
 
+  // A thread that hosts all encode operations.
+  base::Thread encode_thread_;
+
   // Thread for blocking IO operations.
   base::Thread file_thread_;
 
-  scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
+  // A thread that hosts all network operations.
+  base::Thread network_thread_;
+
+  // Task runners wrapping the above threads. These should be declared after
+  // the corresponding threads to guarantee proper order of destruction.
+  scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
+  scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_;
+  scoped_refptr<base::SingleThreadTaskRunner> desktop_task_runner_;
+  scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_;
+  scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
+  scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
+
+  scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
 
   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;