Remove calls to MessageLoop::current() in tools.

Why?
The fact that there's a MessageLoop on the thread is an
unnecessary implementation detail. When browser threads
are migrated to base/task_scheduler, tasks will no longer
have access to a MessageLoop.

These changes were generated manually.

BUG=616447

Review-Url: https://codereview.chromium.org/2130443002
Cr-Original-Commit-Position: refs/heads/master@{#406006}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: f0544fc6ed9a3477926b3640fae9706836f211ea
diff --git a/tools/gn/loader.cc b/tools/gn/loader.cc
index 1899df7..0ede3c9 100644
--- a/tools/gn/loader.cc
+++ b/tools/gn/loader.cc
@@ -6,8 +6,7 @@
 
 #include "base/bind.h"
 #include "base/memory/ptr_util.h"
-#include "base/message_loop/message_loop.h"
-#include "base/single_thread_task_runner.h"
+#include "base/threading/thread_task_runner_handle.h"
 #include "tools/gn/build_settings.h"
 #include "tools/gn/err.h"
 #include "tools/gn/filesystem_utils.h"
@@ -100,9 +99,11 @@
 // -----------------------------------------------------------------------------
 
 LoaderImpl::LoaderImpl(const BuildSettings* build_settings)
-    : main_loop_(base::MessageLoop::current()),
-      pending_loads_(0),
-      build_settings_(build_settings) {
+    : pending_loads_(0), build_settings_(build_settings) {
+  // There may not be an active TaskRunner at this point. When that's the case,
+  // the calling code is expected to call set_task_runner().
+  if (base::ThreadTaskRunnerHandle::IsSet())
+    task_runner_ = base::ThreadTaskRunnerHandle::Get();
 }
 
 LoaderImpl::~LoaderImpl() {
@@ -239,7 +240,7 @@
                                     const LocationRange& origin,
                                     const ParseNode* root) {
   if (!root) {
-    main_loop_->task_runner()->PostTask(
+    task_runner_->PostTask(
         FROM_HERE, base::Bind(&LoaderImpl::DecrementPendingLoads, this));
     return;
   }
@@ -280,8 +281,7 @@
 
   trace.Done();
 
-  main_loop_->task_runner()->PostTask(
-      FROM_HERE, base::Bind(&LoaderImpl::DidLoadFile, this));
+  task_runner_->PostTask(FROM_HERE, base::Bind(&LoaderImpl::DidLoadFile, this));
 }
 
 void LoaderImpl::BackgroundLoadBuildConfig(
@@ -289,7 +289,7 @@
     const Scope::KeyValueMap& toolchain_overrides,
     const ParseNode* root) {
   if (!root) {
-    main_loop_->task_runner()->PostTask(
+    task_runner_->PostTask(
         FROM_HERE, base::Bind(&LoaderImpl::DecrementPendingLoads, this));
     return;
   }
@@ -339,9 +339,9 @@
     }
   }
 
-  main_loop_->task_runner()->PostTask(
-      FROM_HERE, base::Bind(&LoaderImpl::DidLoadBuildConfig, this,
-                            settings->toolchain_label()));
+  task_runner_->PostTask(FROM_HERE,
+                         base::Bind(&LoaderImpl::DidLoadBuildConfig, this,
+                                    settings->toolchain_label()));
 }
 
 void LoaderImpl::DidLoadFile() {
diff --git a/tools/gn/loader.h b/tools/gn/loader.h
index a610835..17f7110 100644
--- a/tools/gn/loader.h
+++ b/tools/gn/loader.h
@@ -11,13 +11,10 @@
 
 #include "base/callback.h"
 #include "base/memory/ref_counted.h"
+#include "base/single_thread_task_runner.h"
 #include "tools/gn/label.h"
 #include "tools/gn/scope.h"
 
-namespace base {
-class MessageLoop;
-}
-
 class BuildSettings;
 class LocationRange;
 class Settings;
@@ -90,10 +87,13 @@
   Label GetDefaultToolchain() const override;
   const Settings* GetToolchainSettings(const Label& label) const override;
 
-  // Sets the message loop corresponding to the main thread. By default this
+  // Sets the task runner corresponding to the main thread. By default this
   // class will use the thread active during construction, but there is not
-  // a message loop active during construction all the time.
-  void set_main_loop(base::MessageLoop* loop) { main_loop_ = loop; }
+  // a task runner active during construction all the time.
+  void set_task_runner(
+      scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
+    task_runner_ = task_runner;
+  }
 
   // The complete callback is called whenever there are no more pending loads.
   // Called on the main thread only. This may be called more than once if the
@@ -159,7 +159,7 @@
                      const base::Callback<void(const ParseNode*)>& callback,
                      Err* err);
 
-  base::MessageLoop* main_loop_;
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
 
   int pending_loads_;
   base::Closure complete_callback_;
diff --git a/tools/gn/loader_unittest.cc b/tools/gn/loader_unittest.cc
index e8d809d..c6cdbdb 100644
--- a/tools/gn/loader_unittest.cc
+++ b/tools/gn/loader_unittest.cc
@@ -7,7 +7,6 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/message_loop/message_loop.h"
 #include "base/run_loop.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "tools/gn/build_settings.h"
diff --git a/tools/gn/scheduler.cc b/tools/gn/scheduler.cc
index b5f3dd0..7364a02 100644
--- a/tools/gn/scheduler.cc
+++ b/tools/gn/scheduler.cc
@@ -97,14 +97,14 @@
 }
 
 void Scheduler::Log(const std::string& verb, const std::string& msg) {
-  if (base::MessageLoop::current() == &main_loop_) {
+  if (task_runner()->BelongsToCurrentThread()) {
     LogOnMainThread(verb, msg);
   } else {
     // The run loop always joins on the sub threads, so the lifetime of this
     // object outlives the invocations of this function, hence "unretained".
-    main_loop_.task_runner()->PostTask(
-        FROM_HERE, base::Bind(&Scheduler::LogOnMainThread,
-                              base::Unretained(this), verb, msg));
+    task_runner()->PostTask(FROM_HERE,
+                            base::Bind(&Scheduler::LogOnMainThread,
+                                       base::Unretained(this), verb, msg));
   }
 }
 
@@ -118,14 +118,14 @@
     is_failed_ = true;
   }
 
-  if (base::MessageLoop::current() == &main_loop_) {
+  if (task_runner()->BelongsToCurrentThread()) {
     FailWithErrorOnMainThread(err);
   } else {
     // The run loop always joins on the sub threads, so the lifetime of this
     // object outlives the invocations of this function, hence "unretained".
-    main_loop_.task_runner()->PostTask(
-        FROM_HERE, base::Bind(&Scheduler::FailWithErrorOnMainThread,
-                              base::Unretained(this), err));
+    task_runner()->PostTask(FROM_HERE,
+                            base::Bind(&Scheduler::FailWithErrorOnMainThread,
+                                       base::Unretained(this), err));
   }
 }
 
@@ -208,12 +208,11 @@
 
 void Scheduler::DecrementWorkCount() {
   if (!base::AtomicRefCountDec(&work_count_)) {
-    if (base::MessageLoop::current() == &main_loop_) {
+    if (task_runner()->BelongsToCurrentThread()) {
       OnComplete();
     } else {
-      main_loop_.task_runner()->PostTask(
-          FROM_HERE,
-          base::Bind(&Scheduler::OnComplete, base::Unretained(this)));
+      task_runner()->PostTask(FROM_HERE, base::Bind(&Scheduler::OnComplete,
+                                                    base::Unretained(this)));
     }
   }
 }
@@ -236,6 +235,6 @@
 
 void Scheduler::OnComplete() {
   // Should be called on the main thread.
-  DCHECK(base::MessageLoop::current() == main_loop());
+  DCHECK(task_runner()->BelongsToCurrentThread());
   runner_.Quit();
 }
diff --git a/tools/gn/scheduler.h b/tools/gn/scheduler.h
index a9bb036..67650d8 100644
--- a/tools/gn/scheduler.h
+++ b/tools/gn/scheduler.h
@@ -12,6 +12,7 @@
 #include "base/macros.h"
 #include "base/message_loop/message_loop.h"
 #include "base/run_loop.h"
+#include "base/single_thread_task_runner.h"
 #include "base/synchronization/lock.h"
 #include "base/threading/sequenced_worker_pool.h"
 #include "tools/gn/input_file_manager.h"
@@ -29,7 +30,9 @@
 
   bool Run();
 
-  base::MessageLoop* main_loop() { return &main_loop_; }
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
+    return main_loop_.task_runner();
+  }
   base::SequencedWorkerPool* pool() { return pool_.get(); }
 
   InputFileManager* input_file_manager() { return input_file_manager_.get(); }
diff --git a/tools/gn/setup.cc b/tools/gn/setup.cc
index eceb4f0..60aeaa8 100644
--- a/tools/gn/setup.cc
+++ b/tools/gn/setup.cc
@@ -13,6 +13,7 @@
 #include "base/command_line.h"
 #include "base/files/file_path.h"
 #include "base/files/file_util.h"
+#include "base/memory/ref_counted.h"
 #include "base/process/launch.h"
 #include "base/single_thread_task_runner.h"
 #include "base/strings/string_split.h"
@@ -139,13 +140,13 @@
 }
 
 // Called on any thread. Post the item to the builder on the main thread.
-void ItemDefinedCallback(base::MessageLoop* main_loop,
-                         scoped_refptr<Builder> builder,
-                         std::unique_ptr<Item> item) {
+void ItemDefinedCallback(
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+    scoped_refptr<Builder> builder,
+    std::unique_ptr<Item> item) {
   DCHECK(item);
-  main_loop->task_runner()->PostTask(
-      FROM_HERE,
-      base::Bind(&Builder::ItemDefined, builder, base::Passed(&item)));
+  task_runner->PostTask(FROM_HERE, base::Bind(&Builder::ItemDefined, builder,
+                                              base::Passed(&item)));
 }
 
 void DecrementWorkCount() {
@@ -258,12 +259,12 @@
       fill_arguments_(true) {
   dotfile_settings_.set_toolchain_label(Label());
   build_settings_.set_item_defined_callback(
-      base::Bind(&ItemDefinedCallback, scheduler_.main_loop(), builder_));
+      base::Bind(&ItemDefinedCallback, scheduler_.task_runner(), builder_));
 
   loader_->set_complete_callback(base::Bind(&DecrementWorkCount));
-  // The scheduler's main loop wasn't created when the Loader was created, so
+  // The scheduler's task runner wasn't created when the Loader was created, so
   // we need to set it now.
-  loader_->set_main_loop(scheduler_.main_loop());
+  loader_->set_task_runner(scheduler_.task_runner());
 }
 
 Setup::~Setup() {