diagnostics: remove usages of base::MessageLoop

BUG=chromium:1094927
TEST=CQ

Change-Id: I9ddad09b4ebe428d3fbabf9c5fa284430f57c61a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2334502
Tested-by: Qijiang Fan <fqj@google.com>
Commit-Queue: Qijiang Fan <fqj@google.com>
Reviewed-by: Maksim Ivanov <emaxx@chromium.org>
diff --git a/diagnostics/common/system/bluetooth_client_impl_test.cc b/diagnostics/common/system/bluetooth_client_impl_test.cc
index aa484d6..b0fae11 100644
--- a/diagnostics/common/system/bluetooth_client_impl_test.cc
+++ b/diagnostics/common/system/bluetooth_client_impl_test.cc
@@ -8,8 +8,8 @@
 
 #include <base/bind.h>
 #include <base/memory/ref_counted.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
+#include <base/test/task_environment.h>
 #include <base/threading/thread_task_runner_handle.h>
 #include <dbus/mock_bus.h>
 #include <dbus/mock_object_manager.h>
@@ -180,7 +180,8 @@
   BluetoothClient* bluetooth_client() const { return bluetooth_client_.get(); }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
 
   StrictMock<MockBluetoothClientObserver> observer_;
 
diff --git a/diagnostics/cros_health_tool/diag/diag.cc b/diagnostics/cros_health_tool/diag/diag.cc
index ac698eb..6f82f32 100644
--- a/diagnostics/cros_health_tool/diag/diag.cc
+++ b/diagnostics/cros_health_tool/diag/diag.cc
@@ -13,7 +13,7 @@
 
 #include <base/at_exit.h>
 #include <base/logging.h>
-#include <base/message_loop/message_loop.h>
+#include <base/task/single_thread_task_executor.h>
 #include <base/time/time.h>
 #include <brillo/flag_helper.h>
 
@@ -104,7 +104,7 @@
 
   base::AtExitManager at_exit_manager;
 
-  base::MessageLoopForIO message_loop;
+  base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
 
   if (FLAGS_crosh_help) {
     std::cout << "Usage: [list|routine]" << std::endl;
diff --git a/diagnostics/cros_health_tool/telem/telem.cc b/diagnostics/cros_health_tool/telem/telem.cc
index 642f414..89b392c 100644
--- a/diagnostics/cros_health_tool/telem/telem.cc
+++ b/diagnostics/cros_health_tool/telem/telem.cc
@@ -17,11 +17,11 @@
 
 #include <base/at_exit.h>
 #include <base/logging.h>
-#include <base/message_loop/message_loop.h>
 #include <base/optional.h>
 #include <base/strings/stringprintf.h>
 #include <base/strings/string_split.h>
 #include <base/strings/string_util.h>
+#include <base/task/single_thread_task_executor.h>
 #include <brillo/flag_helper.h>
 #include <brillo/syslog_logging.h>
 
@@ -451,7 +451,7 @@
 
   logging::InitLogging(logging::LoggingSettings());
 
-  base::MessageLoopForIO message_loop;
+  base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
 
   std::unique_ptr<CrosHealthdMojoAdapter> adapter =
       CrosHealthdMojoAdapter::Create();
diff --git a/diagnostics/cros_healthd/cros_healthd_mojo_service_test.cc b/diagnostics/cros_healthd/cros_healthd_mojo_service_test.cc
index 9a1b762..5b1dd54 100644
--- a/diagnostics/cros_healthd/cros_healthd_mojo_service_test.cc
+++ b/diagnostics/cros_healthd/cros_healthd_mojo_service_test.cc
@@ -8,9 +8,9 @@
 #include <vector>
 
 #include <base/bind.h>
-#include <base/message_loop/message_loop.h>
 #include <base/optional.h>
 #include <base/run_loop.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <mojo/core/embedder/embedder.h>
@@ -129,7 +129,8 @@
   MockCrosHealthdRoutineService* routine_service() { return &routine_service_; }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
   StrictMock<MockCrosHealthdRoutineService> routine_service_;
   MockContext mock_context_;
   FetchAggregator fetch_aggregator_{&mock_context_};
diff --git a/diagnostics/cros_healthd/fetchers/fan_fetcher_test.cc b/diagnostics/cros_healthd/fetchers/fan_fetcher_test.cc
index 7af56a9..9849d40 100644
--- a/diagnostics/cros_healthd/fetchers/fan_fetcher_test.cc
+++ b/diagnostics/cros_healthd/fetchers/fan_fetcher_test.cc
@@ -12,9 +12,9 @@
 #include <base/files/file_path.h>
 #include <base/files/file_util.h>
 #include <base/files/scoped_temp_dir.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
 #include <base/strings/stringprintf.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
@@ -84,7 +84,8 @@
   }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
   MockContext mock_context_;
   FanFetcher fan_fetcher_{&mock_context_};
   base::ScopedTempDir temp_dir_;
diff --git a/diagnostics/cros_healthd_mojo_adapter/cros_healthd_mojo_adapter_delegate_impl.cc b/diagnostics/cros_healthd_mojo_adapter/cros_healthd_mojo_adapter_delegate_impl.cc
index f87d3e9..4b10edd 100644
--- a/diagnostics/cros_healthd_mojo_adapter/cros_healthd_mojo_adapter_delegate_impl.cc
+++ b/diagnostics/cros_healthd_mojo_adapter/cros_healthd_mojo_adapter_delegate_impl.cc
@@ -62,11 +62,11 @@
 
 CrosHealthdMojoAdapterDelegateImpl::CrosHealthdMojoAdapterDelegateImpl() {
   CHECK(mojo_thread_.StartWithOptions(
-      base::Thread::Options(base::MessageLoop::TYPE_IO, 0)))
+      base::Thread::Options(base::MessagePumpType::IO, 0)))
       << "Failed starting the mojo thread.";
 
   CHECK(dbus_thread_.StartWithOptions(
-      base::Thread::Options(base::MessageLoop::TYPE_IO, 0)))
+      base::Thread::Options(base::MessagePumpType::IO, 0)))
       << "Failed starting the D-Bus thread.";
 
   mojo::core::Init();
diff --git a/diagnostics/dpsl/internal/dpsl_thread_context_impl.cc b/diagnostics/dpsl/internal/dpsl_thread_context_impl.cc
index 8bd68e2..e187145 100644
--- a/diagnostics/dpsl/internal/dpsl_thread_context_impl.cc
+++ b/diagnostics/dpsl/internal/dpsl_thread_context_impl.cc
@@ -33,12 +33,14 @@
 
 DpslThreadContextImpl::DpslThreadContextImpl()
     : thread_id_(base::PlatformThread::CurrentId()),
-      // Initialize the message loop only if there's no one yet (it could be
-      // already set up by the calling code via other means, e.g.,
-      // brillo::Daemon).
-      owned_message_loop_(base::ThreadTaskRunnerHandle::IsSet()
-                              ? nullptr
-                              : std::make_unique<base::MessageLoopForIO>()),
+      // Initialize the SingleThreadTaskExecutor only if there's no TaskRunner
+      // yet (it could be already set up by the calling code via other means,
+      // e.g., brillo::Daemon).
+      owned_task_executor_(
+          base::ThreadTaskRunnerHandle::IsSet()
+              ? nullptr
+              : std::make_unique<base::SingleThreadTaskExecutor>(
+                    base::MessagePumpType::IO)),
       task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
 
 DpslThreadContextImpl::~DpslThreadContextImpl() {
diff --git a/diagnostics/dpsl/internal/dpsl_thread_context_impl.h b/diagnostics/dpsl/internal/dpsl_thread_context_impl.h
index 74140d1..20ff348 100644
--- a/diagnostics/dpsl/internal/dpsl_thread_context_impl.h
+++ b/diagnostics/dpsl/internal/dpsl_thread_context_impl.h
@@ -9,10 +9,10 @@
 
 #include <base/macros.h>
 #include <base/memory/scoped_refptr.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
 #include <base/sequence_checker_impl.h>
 #include <base/single_thread_task_runner.h>
+#include <base/task/single_thread_task_executor.h>
 #include <base/threading/platform_thread.h>
 
 #include "diagnostics/dpsl/public/dpsl_thread_context.h"
@@ -41,9 +41,9 @@
  private:
   // Identifier of the thread which is associated with this instance.
   const base::PlatformThreadId thread_id_;
-  // Message loop owned by this instance. Only gets created when no previously
-  // created message loop was present at construction time.
-  std::unique_ptr<base::MessageLoopForIO> owned_message_loop_;
+  // SingleThreadTaskExecutor owned by this instance. Only gets created when no
+  // previously created task runner was present at construction time.
+  std::unique_ptr<base::SingleThreadTaskExecutor> owned_task_executor_;
   // Task runner of the thread associated with this instance.
   const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
   // The run loop which is used for the current invocation of RunEventLoop(). Is
diff --git a/diagnostics/wilco_dtc_supportd/core_test.cc b/diagnostics/wilco_dtc_supportd/core_test.cc
index 2f1936c..d74cb4c 100644
--- a/diagnostics/wilco_dtc_supportd/core_test.cc
+++ b/diagnostics/wilco_dtc_supportd/core_test.cc
@@ -26,9 +26,9 @@
 #include <base/logging.h>
 #include <base/memory/ref_counted.h>
 #include <base/memory/shared_memory.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
 #include <base/strings/stringprintf.h>
+#include <base/test/task_environment.h>
 #include <brillo/dbus/async_event_sequencer.h>
 #include <dbus/message.h>
 #include <dbus/mock_bus.h>
@@ -323,7 +323,8 @@
   // Initialize the Mojo subsystem.
   void InitializeMojo() { mojo::core::Init(); }
 
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
 
   GrpcClientManager grpc_client_manager_;
   MojoGrpcAdapter mojo_grpc_adapter_{&grpc_client_manager_};
diff --git a/diagnostics/wilco_dtc_supportd/grpc_service_test.cc b/diagnostics/wilco_dtc_supportd/grpc_service_test.cc
index 1231a54..2f1f9f9 100644
--- a/diagnostics/wilco_dtc_supportd/grpc_service_test.cc
+++ b/diagnostics/wilco_dtc_supportd/grpc_service_test.cc
@@ -14,13 +14,13 @@
 #include <base/callback.h>
 #include <base/files/file_path.h>
 #include <base/files/scoped_temp_dir.h>
-#include <base/message_loop/message_loop.h>
 #include <base/optional.h>
 #include <base/run_loop.h>
 #include <base/strings/string_piece.h>
-#include "base/strings/stringprintf.h"
-#include "base/system/sys_info.h"
-#include "base/time/time.h"
+#include <base/strings/stringprintf.h>
+#include <base/system/sys_info.h>
+#include <base/test/task_environment.h>
+#include <base/time/time.h>
 #include <gmock/gmock.h>
 #include <google/protobuf/repeated_field.h>
 #include <gtest/gtest.h>
@@ -518,7 +518,8 @@
   base::FilePath temp_dir_path() const { return temp_dir_.GetPath(); }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
   base::ScopedTempDir temp_dir_;
   StrictMock<MockGrpcServiceDelegate> delegate_;
   GrpcService service_{&delegate_};
diff --git a/diagnostics/wilco_dtc_supportd/mojo_service_test.cc b/diagnostics/wilco_dtc_supportd/mojo_service_test.cc
index 0a4a1c8..74bb197 100644
--- a/diagnostics/wilco_dtc_supportd/mojo_service_test.cc
+++ b/diagnostics/wilco_dtc_supportd/mojo_service_test.cc
@@ -9,10 +9,10 @@
 
 #include <base/bind.h>
 #include <base/callback.h>
-#include <base/message_loop/message_loop.h>
 #include <base/optional.h>
 #include <base/run_loop.h>
 #include <base/strings/string_piece.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <mojo/core/embedder/embedder.h>
@@ -72,7 +72,8 @@
   MojoService* mojo_service() { return mojo_service_.get(); }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
 
   StrictMock<MockMojoClient> mojo_client_;
   std::unique_ptr<mojo::Binding<MojomWilcoDtcSupportdClient>>
diff --git a/diagnostics/wilco_dtc_supportd/probe_service_impl_test.cc b/diagnostics/wilco_dtc_supportd/probe_service_impl_test.cc
index 7087240..6af99ba 100644
--- a/diagnostics/wilco_dtc_supportd/probe_service_impl_test.cc
+++ b/diagnostics/wilco_dtc_supportd/probe_service_impl_test.cc
@@ -7,8 +7,8 @@
 
 #include <base/bind.h>
 #include <base/callback.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <mojo/public/cpp/bindings/binding.h>
@@ -73,7 +73,8 @@
   ProbeService* service() { return &service_; }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
 
   StrictMock<MockCallback> mock_callback_;
 
diff --git a/diagnostics/wilco_dtc_supportd/routine_service_test.cc b/diagnostics/wilco_dtc_supportd/routine_service_test.cc
index 210c0d5..c0c2d5c 100644
--- a/diagnostics/wilco_dtc_supportd/routine_service_test.cc
+++ b/diagnostics/wilco_dtc_supportd/routine_service_test.cc
@@ -10,8 +10,8 @@
 
 #include <base/bind.h>
 #include <base/callback.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
@@ -204,7 +204,8 @@
   }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
   FakeDiagnosticsService diagnostics_service_;
   RoutineService service_{&diagnostics_service_};
 };
diff --git a/diagnostics/wilco_dtc_supportd/telemetry/ec_service_test.cc b/diagnostics/wilco_dtc_supportd/telemetry/ec_service_test.cc
index 77d0566..8bc23db 100644
--- a/diagnostics/wilco_dtc_supportd/telemetry/ec_service_test.cc
+++ b/diagnostics/wilco_dtc_supportd/telemetry/ec_service_test.cc
@@ -15,8 +15,8 @@
 #include <base/files/file_util.h>
 #include <base/files/scoped_file.h>
 #include <base/files/scoped_temp_dir.h>
-#include <base/message_loop/message_loop.h>
 #include <base/run_loop.h>
+#include <base/test/task_environment.h>
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
@@ -137,7 +137,8 @@
   EcService* service() { return &service_; }
 
  private:
-  base::MessageLoop message_loop_;
+  base::test::TaskEnvironment task_environment_{
+      base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
   StrictMock<MockEcServiceObserver> observer_;
   EcService service_;