Introduce task_runner() accessors for both base::Thread and base::MessageLoop

This is so that callers can code against a TaskRunner interface
directly, rather than converting message_loop_proxy() into a TaskRunner.

BUG=391045

Review URL: https://codereview.chromium.org/389653005

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


CrOS-Libchrome-Original-Commit: 54e0612d52deedf7a0f0a0f9a65aa5ab0bc67947
diff --git a/base/message_loop/message_loop.h b/base/message_loop/message_loop.h
index a65fbf5..330bde6 100644
--- a/base/message_loop/message_loop.h
+++ b/base/message_loop/message_loop.h
@@ -295,10 +295,17 @@
   const std::string& thread_name() const { return thread_name_; }
 
   // Gets the message loop proxy associated with this message loop.
+  //
+  // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces
   scoped_refptr<MessageLoopProxy> message_loop_proxy() {
     return message_loop_proxy_;
   }
 
+  // Gets the TaskRunner associated with this message loop.
+  scoped_refptr<SingleThreadTaskRunner> task_runner() {
+    return message_loop_proxy_;
+  }
+
   // Enables or disables the recursive task processing. This happens in the case
   // of recursive message loops. Some unwanted message loop may occurs when
   // using common controls or printer functions. By default, recursive task
diff --git a/base/threading/thread.h b/base/threading/thread.h
index 9d446dd..a0a3005 100644
--- a/base/threading/thread.h
+++ b/base/threading/thread.h
@@ -138,15 +138,27 @@
   //
   MessageLoop* message_loop() const { return message_loop_; }
 
-  // Returns a MessageLoopProxy for this thread.  Use the MessageLoopProxy's
-  // PostTask methods to execute code on the thread.  This only returns
-  // non-NULL after a successful call to Start. After Stop has been called,
-  // this will return NULL. Callers can hold on to this even after the thread
-  // is gone.
+  // Returns a MessageLoopProxy for this thread. Use the MessageLoopProxy's
+  // PostTask methods to execute code on the thread. Returns NULL if the thread
+  // is not running (e.g. before Start or after Stop have been called). Callers
+  // can hold on to this even after the thread is gone; in this situation,
+  // attempts to PostTask() will fail.
+  //
+  // Note: This method is deprecated. Callers should call task_runner() instead
+  // and use the TaskRunner interfaces for safely interfacing with the Thread.
   scoped_refptr<MessageLoopProxy> message_loop_proxy() const {
     return message_loop_ ? message_loop_->message_loop_proxy() : NULL;
   }
 
+  // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask
+  // methods to execute code on the thread. Returns NULL if the thread is not
+  // running (e.g. before Start or after Stop have been called). Callers can
+  // hold on to this even after the thread is gone; in this situation, attempts
+  // to PostTask() will fail.
+  scoped_refptr<SingleThreadTaskRunner> task_runner() const {
+    return message_loop_proxy();
+  }
+
   // Returns the name of this thread (for display in debugger too).
   const std::string& thread_name() const { return name_; }