Sanitize NetworkServiceTestHelper instantiation.

This makes NetworkServiceTestHelper instantiation better:
1. Unify the instantiation condition to the class
2. Make sure there is up to one instance in the utility process of
the network service.
3. Simplify caller by returning a callback.

Bug: 1377734
Change-Id: Ib5349f548a0fd932e14f5fd5e4b066c4f8ace786
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3970337
Reviewed-by: John Abd-El-Malek <[email protected]>
Reviewed-by: Yaron Friedman <[email protected]>
Reviewed-by: Eric Seckler <[email protected]>
Auto-Submit: Yoichi Osato <[email protected]>
Reviewed-by: Ben Wells <[email protected]>
Reviewed-by: danakj <[email protected]>
Commit-Queue: Ben Wells <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1081809}
diff --git a/chrome/browser/android/chrome_entry_point_for_test.cc b/chrome/browser/android/chrome_entry_point_for_test.cc
index fc759ec..258aead 100644
--- a/chrome/browser/android/chrome_entry_point_for_test.cc
+++ b/chrome/browser/android/chrome_entry_point_for_test.cc
@@ -6,40 +6,20 @@
 #include "base/android/jni_utils.h"
 #include "base/android/library_loader/library_loader_hooks.h"
 #include "base/bind.h"
-#include "base/command_line.h"
-#include "base/no_destructor.h"
 #include "base/test/test_support_android.h"
 #include "chrome/android/chrome_jni_for_test_registration_generated.h"
 #include "chrome/app/android/chrome_jni_onload.h"
 #include "chrome/utility/chrome_content_utility_client.h"
-#include "content/public/common/content_switches.h"
 #include "content/public/test/network_service_test_helper.h"
-#include "sandbox/policy/switches.h"
-#include "services/network/public/mojom/network_service.mojom.h"
 
 namespace {
 
-content::NetworkServiceTestHelper* GetNetworkServiceTestHelper() {
-  static base::NoDestructor<content::NetworkServiceTestHelper> instance;
-  return instance.get();
-}
-
 bool NativeInit(base::android::LibraryProcessType) {
   // Setup a working test environment for the network service in case it's used.
   // Only create this object in the utility process, so that its members don't
   // interfere with other test objects in the browser process.
-  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
-  if (command_line->GetSwitchValueASCII(switches::kProcessType) ==
-          switches::kUtilityProcess &&
-      command_line->GetSwitchValueASCII(switches::kUtilitySubType) ==
-          network::mojom::NetworkService::Name_) {
-    ChromeContentUtilityClient::SetNetworkBinderCreationCallback(base::BindOnce(
-        [](content::NetworkServiceTestHelper* helper,
-           service_manager::BinderRegistry* registry) {
-          helper->RegisterNetworkBinders(registry);
-        },
-        GetNetworkServiceTestHelper()));
-  }
+  static std::unique_ptr<content::NetworkServiceTestHelper>
+      network_service_test_helper = content::NetworkServiceTestHelper::Create();
 
   return android::OnJNIOnLoadInit();
 }
diff --git a/chrome/test/android/browsertests_apk/android_browsertests_jni_onload.cc b/chrome/test/android/browsertests_apk/android_browsertests_jni_onload.cc
index 94d7e6c..99dd993c 100644
--- a/chrome/test/android/browsertests_apk/android_browsertests_jni_onload.cc
+++ b/chrome/test/android/browsertests_apk/android_browsertests_jni_onload.cc
@@ -5,41 +5,21 @@
 #include <memory>
 
 #include "base/android/jni_android.h"
-#include "base/android/jni_utils.h"
 #include "base/android/library_loader/library_loader_hooks.h"
-#include "base/bind.h"
-#include "base/command_line.h"
-#include "base/no_destructor.h"
 #include "chrome/app/android/chrome_jni_onload.h"
 #include "chrome/test/base/chrome_test_launcher.h"
-#include "chrome/utility/chrome_content_utility_client.h"
 #include "content/public/app/content_jni_onload.h"
 #include "content/public/app/content_main.h"
-#include "content/public/common/content_switches.h"
 #include "content/public/test/nested_message_pump_android.h"
 #include "content/public/test/network_service_test_helper.h"
-#include "services/network/public/mojom/network_service.mojom.h"
 
 namespace {
 bool NativeInit(base::android::LibraryProcessType) {
-  static base::NoDestructor<content::NetworkServiceTestHelper>
-      network_service_test_helper;
-
   // Setup a working test environment for the network service in case it's used.
   // Only create this object in the utility process, so that its members don't
   // interfere with other test objects in the browser process.
-  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
-  if (command_line->GetSwitchValueASCII(switches::kProcessType) ==
-          switches::kUtilityProcess &&
-      command_line->GetSwitchValueASCII(switches::kUtilitySubType) ==
-          network::mojom::NetworkService::Name_) {
-    ChromeContentUtilityClient::SetNetworkBinderCreationCallback(base::BindOnce(
-        [](content::NetworkServiceTestHelper* helper,
-           service_manager::BinderRegistry* registry) {
-          helper->RegisterNetworkBinders(registry);
-        },
-        network_service_test_helper.get()));
-  }
+  static std::unique_ptr<content::NetworkServiceTestHelper>
+      network_service_test_helper = content::NetworkServiceTestHelper::Create();
   return true;
 }
 }  // namespace
diff --git a/chrome/test/base/chrome_test_launcher.cc b/chrome/test/base/chrome_test_launcher.cc
index 0a59bd8f..b42ccac6 100644
--- a/chrome/test/base/chrome_test_launcher.cc
+++ b/chrome/test/base/chrome_test_launcher.cc
@@ -287,18 +287,7 @@
   // Only create this object in the utility process, so that its members don't
   // interfere with other test objects in the browser process.
   std::unique_ptr<content::NetworkServiceTestHelper>
-      network_service_test_helper;
-  if (command_line.GetSwitchValueASCII(switches::kProcessType) ==
-      switches::kUtilityProcess) {
-    network_service_test_helper =
-        std::make_unique<content::NetworkServiceTestHelper>();
-    ChromeContentUtilityClient::SetNetworkBinderCreationCallback(base::BindOnce(
-        [](content::NetworkServiceTestHelper* helper,
-           service_manager::BinderRegistry* registry) {
-          helper->RegisterNetworkBinders(registry);
-        },
-        network_service_test_helper.get()));
-  }
+      network_service_test_helper = content::NetworkServiceTestHelper::Create();
 
 // TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
 // of lacros-chrome is complete.
diff --git a/chrome/utility/chrome_content_utility_client.cc b/chrome/utility/chrome_content_utility_client.cc
index c55bfb8..f779813 100644
--- a/chrome/utility/chrome_content_utility_client.cc
+++ b/chrome/utility/chrome_content_utility_client.cc
@@ -10,7 +10,6 @@
 
 #include "base/command_line.h"
 #include "base/files/file_path.h"
-#include "base/lazy_instance.h"
 #include "base/path_service.h"
 #include "build/build_config.h"
 #include "chrome/common/chrome_paths.h"
@@ -25,13 +24,6 @@
 #include "sandbox/policy/mojom/sandbox.mojom.h"
 #include "sandbox/policy/sandbox_type.h"
 
-namespace {
-
-base::LazyInstance<ChromeContentUtilityClient::NetworkBinderCreationCallback>::
-    Leaky g_network_binder_creation_callback = LAZY_INSTANCE_INITIALIZER;
-
-}  // namespace
-
 ChromeContentUtilityClient::ChromeContentUtilityClient() = default;
 
 ChromeContentUtilityClient::~ChromeContentUtilityClient() = default;
@@ -55,12 +47,6 @@
     ExposeElevatedChromeUtilityInterfacesToBrowser(binders);
 }
 
-void ChromeContentUtilityClient::RegisterNetworkBinders(
-    service_manager::BinderRegistry* registry) {
-  if (g_network_binder_creation_callback.Get())
-    std::move(g_network_binder_creation_callback.Get()).Run(registry);
-}
-
 void ChromeContentUtilityClient::UtilityThreadStarted() {
   // Only builds message pipes for utility processes which enable sampling
   // profilers.
@@ -106,9 +92,3 @@
     mojo::ServiceFactory& services) {
   return ::RegisterIOThreadServices(services);
 }
-
-// static
-void ChromeContentUtilityClient::SetNetworkBinderCreationCallback(
-    NetworkBinderCreationCallback callback) {
-  g_network_binder_creation_callback.Get() = std::move(callback);
-}
diff --git a/chrome/utility/chrome_content_utility_client.h b/chrome/utility/chrome_content_utility_client.h
index 5545970..683d8794 100644
--- a/chrome/utility/chrome_content_utility_client.h
+++ b/chrome/utility/chrome_content_utility_client.h
@@ -10,9 +10,6 @@
 
 class ChromeContentUtilityClient : public content::ContentUtilityClient {
  public:
-  using NetworkBinderCreationCallback =
-      base::OnceCallback<void(service_manager::BinderRegistry*)>;
-
   ChromeContentUtilityClient();
 
   ChromeContentUtilityClient(const ChromeContentUtilityClient&) = delete;
@@ -25,16 +22,10 @@
   void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
   void PostIOThreadCreated(
       base::SingleThreadTaskRunner* io_thread_task_runner) override;
-  void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) override;
   void UtilityThreadStarted() override;
   void RegisterMainThreadServices(mojo::ServiceFactory& services) override;
   void RegisterIOThreadServices(mojo::ServiceFactory& services) override;
 
-  // See NetworkBinderProvider above.
-  static void SetNetworkBinderCreationCallback(
-      NetworkBinderCreationCallback callback);
-
  private:
   // True if the utility process runs with elevated privileges.
   bool utility_process_running_elevated_ = false;
diff --git a/content/public/test/network_service_test_helper.cc b/content/public/test/network_service_test_helper.cc
index c1a1bf21..eb767ee 100644
--- a/content/public/test/network_service_test_helper.cc
+++ b/content/public/test/network_service_test_helper.cc
@@ -23,7 +23,9 @@
 #include "content/public/common/content_features.h"
 #include "content/public/common/content_switches.h"
 #include "content/public/test/test_host_resolver.h"
+#include "content/utility/services.h"
 #include "mojo/public/cpp/bindings/callback_helpers.h"
+#include "mojo/public/cpp/bindings/pending_receiver.h"
 #include "mojo/public/cpp/bindings/pending_remote.h"
 #include "mojo/public/cpp/bindings/receiver_set.h"
 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
@@ -51,6 +53,7 @@
 #include "services/network/public/cpp/features.h"
 #include "services/network/public/mojom/network_change_manager.mojom.h"
 #include "services/network/public/mojom/network_service.mojom.h"
+#include "services/network/public/mojom/network_service_test.mojom.h"
 #include "services/network/sct_auditing/sct_auditing_cache.h"
 #include "services/network/sct_auditing/sct_auditing_reporter.h"
 
@@ -789,20 +792,35 @@
 };
 
 NetworkServiceTestHelper::NetworkServiceTestHelper()
-    : network_service_test_impl_(new NetworkServiceTestImpl) {}
+    : network_service_test_impl_(new NetworkServiceTestImpl) {
+  static bool is_created = false;
+  DCHECK(!is_created) << "NetworkServiceTestHelper shouldn't be created twice.";
+  is_created = true;
+}
 
 NetworkServiceTestHelper::~NetworkServiceTestHelper() = default;
 
+std::unique_ptr<NetworkServiceTestHelper> NetworkServiceTestHelper::Create() {
+  if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+          switches::kUtilitySubType) == network::mojom::NetworkService::Name_) {
+    std::unique_ptr<NetworkServiceTestHelper> helper(
+        new NetworkServiceTestHelper());
+    SetNetworkBinderCreationCallbackForTesting(
+        base::BindOnce(&NetworkServiceTestHelper::RegisterNetworkBinders,
+                       base::Unretained(helper.get())));
+    return helper;
+  }
+  return nullptr;
+}
+
 void NetworkServiceTestHelper::RegisterNetworkBinders(
     service_manager::BinderRegistry* registry) {
   registry->AddInterface(base::BindRepeating(
-      &NetworkServiceTestHelper::BindNetworkServiceTestReceiver,
+      [](NetworkServiceTestHelper* helper,
+         mojo::PendingReceiver<network::mojom::NetworkServiceTest> receiver) {
+        helper->network_service_test_impl_->BindReceiver(std::move(receiver));
+      },
       base::Unretained(this)));
 }
 
-void NetworkServiceTestHelper::BindNetworkServiceTestReceiver(
-    mojo::PendingReceiver<network::mojom::NetworkServiceTest> receiver) {
-  network_service_test_impl_->BindReceiver(std::move(receiver));
-}
-
 }  // namespace content
diff --git a/content/public/test/network_service_test_helper.h b/content/public/test/network_service_test_helper.h
index 618d95e..d6e6abf 100644
--- a/content/public/test/network_service_test_helper.h
+++ b/content/public/test/network_service_test_helper.h
@@ -7,34 +7,33 @@
 
 #include <memory>
 
-#include "mojo/public/cpp/bindings/pending_receiver.h"
-#include "services/network/public/mojom/network_service_test.mojom.h"
 #include "services/service_manager/public/cpp/binder_registry.h"
 
 namespace content {
 
 // Used by testing environments to inject test-only interface binders into an
 // embedded network service instance. Test suites should create a long-lived
-// instance of this class and call RegisterNetworkBinders() on a BinderRegistry
-// which will be used to fulfill interface requests within the network service.
+// instance of this class.
 class NetworkServiceTestHelper {
  public:
-  NetworkServiceTestHelper();
-
+  // Returns the instance if the current process is running the network service
+  // and call this function first.
+  // If this returns an instance, it is ready to fulfill NetworkServiceTest
+  // mojo interface requests within the network service.
+  static std::unique_ptr<NetworkServiceTestHelper> Create();
   NetworkServiceTestHelper(const NetworkServiceTestHelper&) = delete;
   NetworkServiceTestHelper& operator=(const NetworkServiceTestHelper&) = delete;
 
   ~NetworkServiceTestHelper();
 
-  // Registers the helper's interfaces on |registry|. Note that this object
-  // must outlive |registry|.
-  void RegisterNetworkBinders(service_manager::BinderRegistry* registry);
-
  private:
   class NetworkServiceTestImpl;
 
-  void BindNetworkServiceTestReceiver(
-      mojo::PendingReceiver<network::mojom::NetworkServiceTest> receiver);
+  NetworkServiceTestHelper();
+
+  // Registers the helper's interfaces on |registry|. Note that this object
+  // must outlive |registry|.
+  void RegisterNetworkBinders(service_manager::BinderRegistry* registry);
 
   std::unique_ptr<NetworkServiceTestImpl> network_service_test_impl_;
 };
diff --git a/content/public/utility/content_utility_client.h b/content/public/utility/content_utility_client.h
index 59b5240f..d5aa03a 100644
--- a/content/public/utility/content_utility_client.h
+++ b/content/public/utility/content_utility_client.h
@@ -17,7 +17,7 @@
 namespace mojo {
 class BinderMap;
 class ServiceFactory;
-}
+}  // namespace mojo
 
 namespace content {
 
@@ -64,9 +64,6 @@
   //
   // Only called from the main thread.
   virtual void RegisterMainThreadServices(mojo::ServiceFactory& services) {}
-
-  virtual void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) {}
 };
 
 }  // namespace content
diff --git a/content/shell/utility/shell_content_utility_client.cc b/content/shell/utility/shell_content_utility_client.cc
index 136c5fa..dfbe40f 100644
--- a/content/shell/utility/shell_content_utility_client.cc
+++ b/content/shell/utility/shell_content_utility_client.cc
@@ -141,7 +141,7 @@
   if (is_browsertest &&
       base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
           switches::kProcessType) == switches::kUtilityProcess) {
-    network_service_test_helper_ = std::make_unique<NetworkServiceTestHelper>();
+    network_service_test_helper_ = NetworkServiceTestHelper::Create();
     audio_service_test_helper_ = std::make_unique<AudioServiceTestHelper>();
     storage::InjectTestApiImplementation();
     register_sandbox_status_helper_ = true;
@@ -171,10 +171,4 @@
   services.Add(RunEchoService);
 }
 
-void ShellContentUtilityClient::RegisterNetworkBinders(
-    service_manager::BinderRegistry* registry) {
-  if (network_service_test_helper_)
-    network_service_test_helper_->RegisterNetworkBinders(registry);
-}
-
 }  // namespace content
diff --git a/content/shell/utility/shell_content_utility_client.h b/content/shell/utility/shell_content_utility_client.h
index 24e1a44..16befe4 100644
--- a/content/shell/utility/shell_content_utility_client.h
+++ b/content/shell/utility/shell_content_utility_client.h
@@ -24,8 +24,6 @@
   // ContentUtilityClient:
   void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
   void RegisterIOThreadServices(mojo::ServiceFactory& services) override;
-  void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) override;
 
  private:
   std::unique_ptr<NetworkServiceTestHelper> network_service_test_helper_;
diff --git a/content/utility/services.cc b/content/utility/services.cc
index 3db6825..5daa04d8 100644
--- a/content/utility/services.cc
+++ b/content/utility/services.cc
@@ -28,7 +28,6 @@
 #include "services/audio/service_factory.h"
 #include "services/data_decoder/data_decoder_service.h"
 #include "services/network/network_service.h"
-#include "services/service_manager/public/cpp/binder_registry.h"
 #include "services/tracing/public/mojom/tracing_service.mojom.h"
 #include "services/tracing/tracing_service.h"
 #include "services/video_capture/public/mojom/video_capture_service.mojom.h"
@@ -104,6 +103,8 @@
 #endif  // BUILDFLAG(ENABLE_ACCESSIBILITY_SERVICE)
 
 namespace content {
+base::LazyInstance<NetworkBinderCreationCallback>::Leaky
+    g_network_binder_creation_callback_for_testing = LAZY_INSTANCE_INITIALIZER;
 
 namespace {
 
@@ -167,7 +168,10 @@
 auto RunNetworkService(
     mojo::PendingReceiver<network::mojom::NetworkService> receiver) {
   auto binders = std::make_unique<service_manager::BinderRegistry>();
-  GetContentClient()->utility()->RegisterNetworkBinders(binders.get());
+  if (g_network_binder_creation_callback_for_testing.Get()) {
+    std::move(g_network_binder_creation_callback_for_testing.Get())
+        .Run(binders.get());
+  }
   return std::make_unique<network::NetworkService>(
       std::move(binders), std::move(receiver),
       /*delay_initialization_until_set_client=*/true);
@@ -337,6 +341,11 @@
 
 }  // namespace
 
+void SetNetworkBinderCreationCallbackForTesting(  // IN-TEST
+    NetworkBinderCreationCallback callback) {
+  g_network_binder_creation_callback_for_testing.Get() = std::move(callback);
+}
+
 void RegisterIOThreadServices(mojo::ServiceFactory& services) {
   // The network service runs on the IO thread because it needs a message
   // loop of type IO that can get notified when pipes have data.
diff --git a/content/utility/services.h b/content/utility/services.h
index c838747..3e2a9f4f 100644
--- a/content/utility/services.h
+++ b/content/utility/services.h
@@ -5,11 +5,18 @@
 #ifndef CONTENT_UTILITY_SERVICES_H_
 #define CONTENT_UTILITY_SERVICES_H_
 
+#include "content/common/content_export.h"
+#include "services/service_manager/public/cpp/binder_registry.h"
+
 namespace mojo {
 class ServiceFactory;
 }
 
 namespace content {
+using NetworkBinderCreationCallback =
+    base::OnceCallback<void(service_manager::BinderRegistry*)>;
+CONTENT_EXPORT void SetNetworkBinderCreationCallbackForTesting(  // IN-TEST
+    NetworkBinderCreationCallback callback);
 
 void RegisterIOThreadServices(mojo::ServiceFactory& services);
 void RegisterMainThreadServices(mojo::ServiceFactory& services);
diff --git a/extensions/shell/test/test_shell_main_delegate.cc b/extensions/shell/test/test_shell_main_delegate.cc
index 2c18434..38ea1b9a 100644
--- a/extensions/shell/test/test_shell_main_delegate.cc
+++ b/extensions/shell/test/test_shell_main_delegate.cc
@@ -9,7 +9,6 @@
 #include "content/public/common/content_switches.h"
 #include "content/public/test/network_service_test_helper.h"
 #include "content/public/utility/content_utility_client.h"
-#include "content/shell/common/shell_switches.h"
 #include "third_party/abseil-cpp/absl/types/variant.h"
 
 namespace {
@@ -17,11 +16,7 @@
 class TestShellContentUtilityClient : public content::ContentUtilityClient {
  public:
   TestShellContentUtilityClient() {
-    if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
-            switches::kProcessType) == switches::kUtilityProcess) {
-      network_service_test_helper_ =
-          std::make_unique<content::NetworkServiceTestHelper>();
-    }
+    network_service_test_helper_ = content::NetworkServiceTestHelper::Create();
   }
 
   TestShellContentUtilityClient(const TestShellContentUtilityClient&) = delete;
@@ -30,12 +25,6 @@
 
   ~TestShellContentUtilityClient() override {}
 
-  // content::ContentUtilityClient implementation.
-  void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) override {
-    network_service_test_helper_->RegisterNetworkBinders(registry);
-  }
-
  private:
   std::unique_ptr<content::NetworkServiceTestHelper>
       network_service_test_helper_;
diff --git a/headless/lib/utility/headless_content_utility_client.cc b/headless/lib/utility/headless_content_utility_client.cc
index d348209f..7039a15 100644
--- a/headless/lib/utility/headless_content_utility_client.cc
+++ b/headless/lib/utility/headless_content_utility_client.cc
@@ -5,7 +5,6 @@
 #include "headless/lib/utility/headless_content_utility_client.h"
 
 #include "base/bind.h"
-#include "base/lazy_instance.h"
 #include "content/public/utility/utility_thread.h"
 #include "mojo/public/cpp/bindings/pending_receiver.h"
 #include "mojo/public/cpp/bindings/service_factory.h"
@@ -20,10 +19,6 @@
 
 namespace {
 
-base::LazyInstance<
-    HeadlessContentUtilityClient::NetworkBinderCreationCallback>::Leaky
-    g_network_binder_creation_callback = LAZY_INSTANCE_INITIALIZER;
-
 #if BUILDFLAG(ENABLE_PRINTING)
 auto RunPrintCompositor(
     mojo::PendingReceiver<printing::mojom::PrintCompositor> receiver) {
@@ -35,12 +30,6 @@
 
 }  // namespace
 
-// static
-void HeadlessContentUtilityClient::SetNetworkBinderCreationCallbackForTests(
-    NetworkBinderCreationCallback callback) {
-  g_network_binder_creation_callback.Get() = std::move(callback);
-}
-
 HeadlessContentUtilityClient::HeadlessContentUtilityClient(
     const std::string& user_agent)
     : user_agent_(user_agent) {}
@@ -54,10 +43,4 @@
 #endif
 }
 
-void HeadlessContentUtilityClient::RegisterNetworkBinders(
-    service_manager::BinderRegistry* registry) {
-  if (g_network_binder_creation_callback.Get())
-    g_network_binder_creation_callback.Get().Run(registry);
-}
-
 }  // namespace headless
diff --git a/headless/lib/utility/headless_content_utility_client.h b/headless/lib/utility/headless_content_utility_client.h
index bfffad9..783d8b40 100644
--- a/headless/lib/utility/headless_content_utility_client.h
+++ b/headless/lib/utility/headless_content_utility_client.h
@@ -16,12 +16,6 @@
 class HEADLESS_EXPORT HeadlessContentUtilityClient
     : public content::ContentUtilityClient {
  public:
-  using NetworkBinderCreationCallback =
-      base::RepeatingCallback<void(service_manager::BinderRegistry*)>;
-
-  static void SetNetworkBinderCreationCallbackForTests(
-      NetworkBinderCreationCallback callback);
-
   explicit HeadlessContentUtilityClient(const std::string& user_agent);
 
   HeadlessContentUtilityClient(const HeadlessContentUtilityClient&) = delete;
@@ -32,8 +26,6 @@
 
   // content::ContentUtilityClient:
   void RegisterMainThreadServices(mojo::ServiceFactory& services) override;
-  void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) override;
 
  private:
   const std::string user_agent_;
diff --git a/headless/test/headless_test_launcher.cc b/headless/test/headless_test_launcher.cc
index 1282ebb..f1fa8859 100644
--- a/headless/test/headless_test_launcher.cc
+++ b/headless/test/headless_test_launcher.cc
@@ -8,13 +8,11 @@
 #include "base/command_line.h"
 #include "base/test/launcher/test_launcher.h"
 #include "build/build_config.h"
-#include "content/public/common/content_switches.h"
 #include "content/public/test/content_test_suite_base.h"
 #include "content/public/test/network_service_test_helper.h"
 #include "content/public/test/test_launcher.h"
 #include "headless/lib/browser/headless_browser_impl.h"
 #include "headless/lib/headless_content_main_delegate.h"
-#include "headless/lib/utility/headless_content_utility_client.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 #if BUILDFLAG(IS_WIN)
@@ -86,19 +84,7 @@
   // Only create this object in the utility process, so that its members don't
   // interfere with other test objects in the browser process.
   std::unique_ptr<content::NetworkServiceTestHelper>
-      network_service_test_helper;
-  if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
-          switches::kProcessType) == switches::kUtilityProcess) {
-    network_service_test_helper =
-        std::make_unique<content::NetworkServiceTestHelper>();
-    headless::HeadlessContentUtilityClient::
-        SetNetworkBinderCreationCallbackForTests(base::BindRepeating(
-            [](content::NetworkServiceTestHelper* helper,
-               service_manager::BinderRegistry* registry) {
-              helper->RegisterNetworkBinders(registry);
-            },
-            network_service_test_helper.get()));
-  }
+      network_service_test_helper = content::NetworkServiceTestHelper::Create();
 
   headless::HeadlessTestLauncherDelegate launcher_delegate;
   return LaunchTests(&launcher_delegate, parallel_jobs, argc, argv);
diff --git a/weblayer/BUILD.gn b/weblayer/BUILD.gn
index b5c2edc..5e5c763 100644
--- a/weblayer/BUILD.gn
+++ b/weblayer/BUILD.gn
@@ -387,8 +387,6 @@
     "renderer/weblayer_render_frame_observer.h",
     "renderer/weblayer_render_thread_observer.cc",
     "renderer/weblayer_render_thread_observer.h",
-    "utility/content_utility_client_impl.cc",
-    "utility/content_utility_client_impl.h",
   ]
 
   configs += [ "//build/config:precompiled_headers" ]
diff --git a/weblayer/app/content_main_delegate_impl.cc b/weblayer/app/content_main_delegate_impl.cc
index 86094aa..a081277 100644
--- a/weblayer/app/content_main_delegate_impl.cc
+++ b/weblayer/app/content_main_delegate_impl.cc
@@ -42,7 +42,6 @@
 #include "weblayer/common/weblayer_paths.h"
 #include "weblayer/public/common/switches.h"
 #include "weblayer/renderer/content_renderer_client_impl.h"
-#include "weblayer/utility/content_utility_client_impl.h"
 
 #if BUILDFLAG(IS_ANDROID)
 #include "base/android/apk_assets.h"
@@ -452,10 +451,4 @@
   return renderer_client_.get();
 }
 
-content::ContentUtilityClient*
-ContentMainDelegateImpl::CreateContentUtilityClient() {
-  utility_client_ = std::make_unique<ContentUtilityClientImpl>();
-  return utility_client_.get();
-}
-
 }  // namespace weblayer
diff --git a/weblayer/app/content_main_delegate_impl.h b/weblayer/app/content_main_delegate_impl.h
index 9e1d984..6a78a5b 100644
--- a/weblayer/app/content_main_delegate_impl.h
+++ b/weblayer/app/content_main_delegate_impl.h
@@ -16,7 +16,6 @@
 class ContentBrowserClientImpl;
 class ContentClientImpl;
 class ContentRendererClientImpl;
-class ContentUtilityClientImpl;
 
 class ContentMainDelegateImpl : public content::ContentMainDelegate {
  public:
@@ -40,7 +39,6 @@
   content::ContentClient* CreateContentClient() override;
   content::ContentBrowserClient* CreateContentBrowserClient() override;
   content::ContentRendererClient* CreateContentRendererClient() override;
-  content::ContentUtilityClient* CreateContentUtilityClient() override;
 
  private:
   void InitializeResourceBundle();
@@ -48,7 +46,6 @@
   MainParams params_;
   std::unique_ptr<ContentBrowserClientImpl> browser_client_;
   std::unique_ptr<ContentRendererClientImpl> renderer_client_;
-  std::unique_ptr<ContentUtilityClientImpl> utility_client_;
   std::unique_ptr<ContentClientImpl> content_client_;
 };
 
diff --git a/weblayer/test/browsertests_main.cc b/weblayer/test/browsertests_main.cc
index 4deecce..2d93ccc 100644
--- a/weblayer/test/browsertests_main.cc
+++ b/weblayer/test/browsertests_main.cc
@@ -9,7 +9,6 @@
 #include "content/public/test/network_service_test_helper.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "weblayer/test/test_launcher_delegate_impl.h"
-#include "weblayer/utility/content_utility_client_impl.h"
 
 #if BUILDFLAG(IS_WIN)
 #include "base/win/win_util.h"
@@ -31,19 +30,8 @@
   // used. Only create this object in the utility process, so that its members
   // don't interfere with other test objects in the browser process.
   std::unique_ptr<content::NetworkServiceTestHelper>
-      network_service_test_helper;
-  if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
-          switches::kProcessType) == switches::kUtilityProcess) {
-    network_service_test_helper =
-        std::make_unique<content::NetworkServiceTestHelper>();
-    weblayer::ContentUtilityClientImpl::
-        SetNetworkBinderCreationCallbackForTests(base::BindRepeating(
-            [](content::NetworkServiceTestHelper* helper,
-               service_manager::BinderRegistry* registry) {
-              helper->RegisterNetworkBinders(registry);
-            },
-            network_service_test_helper.get()));
-  }
+      network_service_test_helper = content::NetworkServiceTestHelper::Create();
+
   weblayer::TestLauncherDelegateImpl launcher_delegate;
   return content::LaunchTests(&launcher_delegate, parallel_jobs, argc, argv);
 }
diff --git a/weblayer/utility/DEPS b/weblayer/utility/DEPS
deleted file mode 100644
index 8ad521e..0000000
--- a/weblayer/utility/DEPS
+++ /dev/null
@@ -1,3 +0,0 @@
-include_rules = [
-  "+content/public/utility",
-]
diff --git a/weblayer/utility/content_utility_client_impl.cc b/weblayer/utility/content_utility_client_impl.cc
deleted file mode 100644
index a2e69b9e..0000000
--- a/weblayer/utility/content_utility_client_impl.cc
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2019 The Chromium Authors
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "weblayer/utility/content_utility_client_impl.h"
-
-#include "base/bind.h"
-#include "base/lazy_instance.h"
-
-namespace weblayer {
-
-namespace {
-
-base::LazyInstance<ContentUtilityClientImpl::NetworkBinderCreationCallback>::
-    Leaky g_network_binder_creation_callback = LAZY_INSTANCE_INITIALIZER;
-
-}  // namespace
-
-// static
-void ContentUtilityClientImpl::SetNetworkBinderCreationCallbackForTests(
-    NetworkBinderCreationCallback callback) {
-  g_network_binder_creation_callback.Get() = std::move(callback);
-}
-
-ContentUtilityClientImpl::ContentUtilityClientImpl() = default;
-
-ContentUtilityClientImpl::~ContentUtilityClientImpl() = default;
-
-void ContentUtilityClientImpl::RegisterNetworkBinders(
-    service_manager::BinderRegistry* registry) {
-  if (g_network_binder_creation_callback.Get())
-    g_network_binder_creation_callback.Get().Run(registry);
-}
-
-}  // namespace weblayer
diff --git a/weblayer/utility/content_utility_client_impl.h b/weblayer/utility/content_utility_client_impl.h
deleted file mode 100644
index b05ecb8..0000000
--- a/weblayer/utility/content_utility_client_impl.h
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2019 The Chromium Authors
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEBLAYER_UTILITY_CONTENT_UTILITY_CLIENT_IMPL_H_
-#define WEBLAYER_UTILITY_CONTENT_UTILITY_CLIENT_IMPL_H_
-
-#include "base/callback.h"
-#include "content/public/utility/content_utility_client.h"
-
-namespace weblayer {
-
-class ContentUtilityClientImpl : public content::ContentUtilityClient {
- public:
-  using NetworkBinderCreationCallback =
-      base::RepeatingCallback<void(service_manager::BinderRegistry*)>;
-
-  static void SetNetworkBinderCreationCallbackForTests(
-      NetworkBinderCreationCallback callback);
-
-  ContentUtilityClientImpl();
-
-  ContentUtilityClientImpl(const ContentUtilityClientImpl&) = delete;
-  ContentUtilityClientImpl& operator=(const ContentUtilityClientImpl&) = delete;
-
-  ~ContentUtilityClientImpl() override;
-
-  // content::ContentUtilityClient:
-  void RegisterNetworkBinders(
-      service_manager::BinderRegistry* registry) override;
-};
-
-}  // namespace weblayer
-
-#endif  // WEBLAYER_UTILITY_CONTENT_UTILITY_CLIENT_IMPL_H_