bruschetta: Pass local_state to BruschettaInstallerImpl

This CL lets obtain local_state as arguments for BruschettaInstallerImpl
so that BruschettaDownload and BruschettaNetworkContext can obtain
local_state without accessing g_browser_process.

This is a part of ash refactoring by removing g_browser_process access.

Bug: b:369689752
Change-Id: I3faf5a4d3a1d5bcc4b479e4a9a63ef468e6c8221
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6211358
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Eriko Kurimoto <elkurin@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1413340}
diff --git a/chrome/browser/ash/bruschetta/BUILD.gn b/chrome/browser/ash/bruschetta/BUILD.gn
index 9d0ffee..97ccb15 100644
--- a/chrome/browser/ash/bruschetta/BUILD.gn
+++ b/chrome/browser/ash/bruschetta/BUILD.gn
@@ -58,7 +58,6 @@
   deps = [
     "//ash/constants",
     "//chrome/app:generated_resources",
-    "//chrome/browser:browser_process",
     "//chrome/browser/ash/app_list",
     "//chrome/browser/ash/crostini",
     "//chrome/browser/ash/guest_os/public",
@@ -72,6 +71,7 @@
     "//components/policy:generated",
     "//components/policy:policy_code_generate",
     "//components/policy/core/common",
+    "//components/prefs",
     "//components/strings:components_strings",
     "//content/public/browser",
     "//crypto",
@@ -110,6 +110,7 @@
     "//base",
     "//base/test:test_support",
     "//chrome/browser",
+    "//chrome/browser:browser_process",
     "//chrome/browser/content_settings:content_settings_factory",
     "//chrome/browser/profiles:profile",
     "//chrome/browser/ui",
diff --git a/chrome/browser/ash/bruschetta/bruschetta_download.cc b/chrome/browser/ash/bruschetta/bruschetta_download.cc
index 6d02ef4f..8ee1a37 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_download.cc
+++ b/chrome/browser/ash/bruschetta/bruschetta_download.cc
@@ -16,6 +16,7 @@
 #include "chrome/browser/ash/bruschetta/bruschetta_network_context.h"
 #include "chrome/browser/extensions/cws_info_service.h"
 #include "chrome/browser/profiles/profile.h"
+#include "components/prefs/pref_service.h"
 #include "content/public/browser/storage_partition.h"
 #include "crypto/secure_hash.h"
 #include "crypto/sha2.h"
@@ -99,7 +100,8 @@
   return Sha256File(path);
 }
 
-SimpleURLLoaderDownload::SimpleURLLoaderDownload() = default;
+SimpleURLLoaderDownload::SimpleURLLoaderDownload(PrefService& local_state)
+    : local_state_(local_state) {}
 
 SimpleURLLoaderDownload::~SimpleURLLoaderDownload() {
   auto seq = base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()});
@@ -136,7 +138,8 @@
   req->site_for_cookies = net::SiteForCookies::FromUrl(url_);
   loader_ = network::SimpleURLLoader::Create(std::move(req),
                                              kBruschettaTrafficAnnotation);
-  network_context_ = std::make_unique<BruschettaNetworkContext>(profile);
+  network_context_ =
+      std::make_unique<BruschettaNetworkContext>(profile, local_state_.get());
   loader_->DownloadToFile(network_context_->GetURLLoaderFactory(),
                           base::BindOnce(&SimpleURLLoaderDownload::Finished,
                                          weak_ptr_factory_.GetWeakPtr()),
diff --git a/chrome/browser/ash/bruschetta/bruschetta_download.h b/chrome/browser/ash/bruschetta/bruschetta_download.h
index 8a3cf14b..1af15e3 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_download.h
+++ b/chrome/browser/ash/bruschetta/bruschetta_download.h
@@ -11,6 +11,7 @@
 #include "services/network/public/cpp/simple_url_loader.h"
 #include "url/gurl.h"
 
+class PrefService;
 class Profile;
 namespace network {
 class SimpleURLLoader;
@@ -47,7 +48,7 @@
 // downloading files.
 class SimpleURLLoaderDownload : public BruschettaDownload {
  public:
-  SimpleURLLoaderDownload();
+  explicit SimpleURLLoaderDownload(PrefService& local_state);
   // Starts downloading the file at `url`, will invoke `callback` upon
   // completion. Either with the path to the downloaded file and a sha256 hash
   // of its contents, or in case of error will run `callback` with an empty
@@ -66,14 +67,10 @@
   ~SimpleURLLoaderDownload() override;
 
  private:
-  SimpleURLLoaderDownload(
-      Profile* profile,
-      GURL url,
-      base::OnceCallback<void(base::FilePath path, std::string sha256)>
-          callback);
   void Download(Profile* profile, std::unique_ptr<base::ScopedTempDir> dir);
   void Finished(base::FilePath path);
 
+  const raw_ref<PrefService> local_state_;
   GURL url_;
   std::unique_ptr<base::ScopedTempDir> scoped_temp_dir_;
   base::OnceCallback<void(base::FilePath path, std::string sha256)> callback_;
diff --git a/chrome/browser/ash/bruschetta/bruschetta_download_browsertest.cc b/chrome/browser/ash/bruschetta/bruschetta_download_browsertest.cc
index e4e73c3..d782c73 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_download_browsertest.cc
+++ b/chrome/browser/ash/bruschetta/bruschetta_download_browsertest.cc
@@ -9,6 +9,7 @@
 #include "base/strings/string_util.h"
 #include "base/test/test_future.h"
 #include "base/threading/thread_restrictions.h"
+#include "chrome/browser/browser_process.h"
 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
 #include "chrome/browser/net/profile_network_context_service.h"
 #include "chrome/browser/net/profile_network_context_service_factory.h"
@@ -130,7 +131,8 @@
 IN_PROC_BROWSER_TEST_F(BruschettaHttpsDownloadBrowserTest,
                        TestDownloadUrlNotFound) {
   base::test::TestFuture<base::FilePath, std::string> future;
-  auto download = std::make_unique<SimpleURLLoaderDownload>();
+  auto download = std::make_unique<SimpleURLLoaderDownload>(
+      *g_browser_process->local_state());
   download->StartDownload(browser()->profile(), GURL("bad url"),
                           future.GetCallback());
 
@@ -155,7 +157,8 @@
           base::BindRepeating(&CreateStubClientCertStore));
 
   base::test::TestFuture<base::FilePath, std::string> future;
-  auto download = std::make_unique<SimpleURLLoaderDownload>();
+  auto download = std::make_unique<SimpleURLLoaderDownload>(
+      *g_browser_process->local_state());
   download->StartDownload(browser()->profile(), url_, future.GetCallback());
 
   auto path = future.Get<base::FilePath>();
@@ -185,7 +188,8 @@
           base::BindRepeating(&CreateEmptyClientCertStore));
 
   base::test::TestFuture<base::FilePath, std::string> future;
-  auto download = std::make_unique<SimpleURLLoaderDownload>();
+  auto download = std::make_unique<SimpleURLLoaderDownload>(
+      *g_browser_process->local_state());
   download->StartDownload(browser()->profile(), url_, future.GetCallback());
 
   auto path = future.Get<base::FilePath>();
diff --git a/chrome/browser/ash/bruschetta/bruschetta_installer_impl.cc b/chrome/browser/ash/bruschetta/bruschetta_installer_impl.cc
index 6c7cf5eb..3b2f89c 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_installer_impl.cc
+++ b/chrome/browser/ash/bruschetta/bruschetta_installer_impl.cc
@@ -64,8 +64,15 @@
 
 BruschettaInstallerImpl::BruschettaInstallerImpl(
     Profile* profile,
+    PrefService& local_state,
     base::OnceClosure close_closure)
-    : profile_(profile), close_closure_(std::move(close_closure)) {}
+    : profile_(profile),
+      download_factory_(base::BindRepeating(
+          [](PrefService& local_state) -> std::unique_ptr<BruschettaDownload> {
+            return std::make_unique<SimpleURLLoaderDownload>(local_state);
+          },
+          std::ref(local_state))),
+      close_closure_(std::move(close_closure)) {}
 
 BruschettaInstallerImpl::~BruschettaInstallerImpl() = default;
 
diff --git a/chrome/browser/ash/bruschetta/bruschetta_installer_impl.h b/chrome/browser/ash/bruschetta/bruschetta_installer_impl.h
index 2722f00..7ab7a99 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_installer_impl.h
+++ b/chrome/browser/ash/bruschetta/bruschetta_installer_impl.h
@@ -19,6 +19,7 @@
 #include "components/download/public/background_service/download_metadata.h"
 #include "url/gurl.h"
 
+class PrefService;
 class Profile;
 
 namespace bruschetta {
@@ -29,7 +30,9 @@
   // Public for a free function in the .cc file, not actually part of the public
   // interface.
   struct Fds;
-  BruschettaInstallerImpl(Profile* profile, base::OnceClosure close_callback);
+  BruschettaInstallerImpl(Profile* profile,
+                          PrefService& local_state,
+                          base::OnceClosure close_callback);
 
   BruschettaInstallerImpl(const BruschettaInstallerImpl&) = delete;
   BruschettaInstallerImpl& operator=(const BruschettaInstallerImpl&) = delete;
@@ -104,11 +107,7 @@
   std::unique_ptr<BruschettaDownload> boot_disk_download_;
   std::unique_ptr<BruschettaDownload> pflash_download_;
   base::RepeatingCallback<std::unique_ptr<BruschettaDownload>(void)>
-      download_factory_ = base::BindRepeating([]() {
-        std::unique_ptr<BruschettaDownload> d =
-            std::make_unique<SimpleURLLoaderDownload>();
-        return d;
-      });
+      download_factory_;
 
   base::OnceClosure close_closure_;
 
diff --git a/chrome/browser/ash/bruschetta/bruschetta_installer_impl_unittest.cc b/chrome/browser/ash/bruschetta/bruschetta_installer_impl_unittest.cc
index 37b5f4a..c6031538 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_installer_impl_unittest.cc
+++ b/chrome/browser/ash/bruschetta/bruschetta_installer_impl_unittest.cc
@@ -22,6 +22,8 @@
 #include "chrome/browser/ash/bruschetta/bruschetta_service_factory.h"
 #include "chrome/browser/ash/guest_os/dbus_test_helper.h"
 #include "chrome/browser/profiles/profile_key.h"
+#include "chrome/test/base/scoped_testing_local_state.h"
+#include "chrome/test/base/testing_browser_process.h"
 #include "chrome/test/base/testing_profile.h"
 #include "chromeos/ash/components/dbus/attestation/attestation_client.h"
 #include "chromeos/ash/components/dbus/concierge/fake_concierge_client.h"
@@ -144,8 +146,9 @@
     ash::disks::DiskMountManager::InitializeForTesting(&*disk_mount_manager_);
 
     installer_ = std::make_unique<BruschettaInstallerImpl>(
-        &profile_, base::BindOnce(&BruschettaInstallerTest::CloseCallback,
-                                  base::Unretained(this)));
+        &profile_, *local_state_.Get(),
+        base::BindOnce(&BruschettaInstallerTest::CloseCallback,
+                       base::Unretained(this)));
 
     installer_->AddObserver(&observer_);
     ConfigureDownloadFactory(base::FilePath(), "");
@@ -600,6 +603,8 @@
 
   content::BrowserTaskEnvironment task_environment_{
       base::test::TaskEnvironment::TimeSource::MOCK_TIME};
+  ScopedTestingLocalState local_state_{TestingBrowserProcess::GetGlobal()};
+
   base::RunLoop run_loop_, run_loop_2_;
 
   base::Value::Dict prefs_installable_no_pflash_, prefs_installable_,
diff --git a/chrome/browser/ash/bruschetta/bruschetta_network_context.cc b/chrome/browser/ash/bruschetta/bruschetta_network_context.cc
index cea9c343..031bb61 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_network_context.cc
+++ b/chrome/browser/ash/bruschetta/bruschetta_network_context.cc
@@ -22,6 +22,7 @@
 #include "chrome/browser/net/proxy_config_monitor.h"
 #include "chrome/browser/net/system_network_context_manager.h"
 #include "chrome/browser/profiles/profile.h"
+#include "components/prefs/pref_service.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/content_browser_client.h"
 #include "content/public/browser/network_service_instance.h"
@@ -81,9 +82,9 @@
   base::WeakPtrFactory<SSLPrivateKeyBridge> weak_ptr_factory_{this};
 };
 
-BruschettaNetworkContext::BruschettaNetworkContext(Profile* profile)
-    : profile_(profile),
-      proxy_config_monitor_(g_browser_process->local_state()) {
+BruschettaNetworkContext::BruschettaNetworkContext(Profile* profile,
+                                                   PrefService& local_state)
+    : profile_(profile), proxy_config_monitor_(&local_state) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
 }
 
diff --git a/chrome/browser/ash/bruschetta/bruschetta_network_context.h b/chrome/browser/ash/bruschetta/bruschetta_network_context.h
index d3b6ce3d..fd3d5f1 100644
--- a/chrome/browser/ash/bruschetta/bruschetta_network_context.h
+++ b/chrome/browser/ash/bruschetta/bruschetta_network_context.h
@@ -27,6 +27,8 @@
 #include "services/network/public/mojom/url_loader_network_service_observer.mojom.h"
 #include "url/gurl.h"
 
+class PrefService;
+
 namespace bruschetta {
 
 // Provides an isolated NetworkContext which uses the client certificate store
@@ -36,7 +38,7 @@
     : public network::mojom::URLLoaderNetworkServiceObserver {
  public:
   // Class should not outlive the passed-in profile.
-  explicit BruschettaNetworkContext(Profile* profile);
+  BruschettaNetworkContext(Profile* profile, PrefService& local_state);
 
   BruschettaNetworkContext(const BruschettaNetworkContext&) = delete;
   BruschettaNetworkContext& operator=(const BruschettaNetworkContext&) = delete;
diff --git a/chrome/browser/ash/extensions/autotest_private/autotest_private_api.cc b/chrome/browser/ash/extensions/autotest_private/autotest_private_api.cc
index d633e23..97c123c0 100644
--- a/chrome/browser/ash/extensions/autotest_private/autotest_private_api.cc
+++ b/chrome/browser/ash/extensions/autotest_private/autotest_private_api.cc
@@ -54,6 +54,7 @@
 #include "ash/wm/overview/overview_controller.h"
 #include "ash/wm/wm_event.h"
 #include "base/base64.h"
+#include "base/check_deref.h"
 #include "base/command_line.h"
 #include "base/compiler_specific.h"
 #include "base/feature_list.h"
@@ -6745,6 +6746,7 @@
   Profile* profile = Profile::FromBrowserContext(browser_context());
 
   BruschettaInstallerView::Show(profile,
+                                CHECK_DEREF(g_browser_process->local_state()),
                                 bruschetta::MakeBruschettaId(params->vm_name));
 
   auto* view = BruschettaInstallerView::GetActiveViewForTesting();
diff --git a/chrome/browser/ui/views/bruschetta/BUILD.gn b/chrome/browser/ui/views/bruschetta/BUILD.gn
index c08d247..c309d11a 100644
--- a/chrome/browser/ui/views/bruschetta/BUILD.gn
+++ b/chrome/browser/ui/views/bruschetta/BUILD.gn
@@ -35,6 +35,7 @@
 
   deps = [
     ":bruschetta",
+    "//chrome/browser:browser_process",
     "//chrome/browser/ash/bruschetta",
     "//chrome/browser/ui",
     "//chrome/test:test_support",
diff --git a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
index 3b732d1e..39ee0c6 100644
--- a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
+++ b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
@@ -20,6 +20,7 @@
 #include "chrome/browser/ash/bruschetta/bruschetta_util.h"
 #include "chrome/browser/ui/views/chrome_typography.h"
 #include "chrome/grit/generated_resources.h"
+#include "components/prefs/pref_service.h"
 #include "components/strings/grit/components_strings.h"
 #include "content/public/browser/browser_thread.h"
 #include "ui/accessibility/ax_node_data.h"
@@ -115,6 +116,7 @@
 
 // static
 void BruschettaInstallerView::Show(Profile* profile,
+                                   PrefService& local_state,
                                    const guest_os::GuestId& guest_id) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
   if (bruschetta::GetInstallableConfigs(profile).empty()) {
@@ -124,7 +126,7 @@
   }
   if (!g_bruschetta_installer_view) {
     g_bruschetta_installer_view =
-        new BruschettaInstallerView(profile, guest_id);
+        new BruschettaInstallerView(profile, local_state, guest_id);
     views::DialogDelegate::CreateDialogWidget(g_bruschetta_installer_view,
                                               nullptr, nullptr);
   }
@@ -154,6 +156,7 @@
 END_METADATA
 
 BruschettaInstallerView::BruschettaInstallerView(Profile* profile,
+                                                 PrefService& local_state,
                                                  guest_os::GuestId guest_id)
     : profile_(profile), observation_(this), guest_id_(guest_id) {
   // Layout constants from the spec used for the plugin vm installer.
@@ -256,12 +259,14 @@
   if (dark_light_controller) {
     dark_light_controller->AddObserver(this);
   }
-  installer_factory_ =
-      base::BindRepeating([](Profile* profile, base::OnceClosure closure) {
+  installer_factory_ = base::BindRepeating(
+      [](PrefService& local_state, Profile* profile, base::OnceClosure closure)
+          -> std::unique_ptr<bruschetta::BruschettaInstaller> {
         return static_cast<std::unique_ptr<bruschetta::BruschettaInstaller>>(
             std::make_unique<bruschetta::BruschettaInstallerImpl>(
-                profile, std::move(closure)));
-      });
+                profile, local_state, std::move(closure)));
+      },
+      std::ref(local_state));
 }
 
 BruschettaInstallerView::~BruschettaInstallerView() {
diff --git a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.h b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.h
index c0a4a66..b60c2a66 100644
--- a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.h
+++ b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.h
@@ -18,6 +18,7 @@
 #include "ui/views/controls/progress_bar.h"
 #include "ui/views/window/dialog_delegate.h"
 
+class PrefService;
 class Profile;
 
 namespace views {
@@ -42,9 +43,12 @@
   using InstallResultCallback =
       base::OnceCallback<void(bruschetta::BruschettaInstallResult)>;
 
-  static void Show(Profile* profile, const guest_os::GuestId& guest_id);
+  static void Show(Profile* profile,
+                   PrefService& local_state,
+                   const guest_os::GuestId& guest_id);
 
   explicit BruschettaInstallerView(Profile* profile,
+                                   PrefService& local_state,
                                    guest_os::GuestId guest_id);
 
   // Disallow copy and assign.
diff --git a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view_browsertest.cc b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view_browsertest.cc
index b2f5be60d..f43a476a 100644
--- a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view_browsertest.cc
+++ b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view_browsertest.cc
@@ -13,6 +13,7 @@
 #include "chrome/browser/ash/bruschetta/bruschetta_installer.h"
 #include "chrome/browser/ash/bruschetta/bruschetta_pref_names.h"
 #include "chrome/browser/ash/bruschetta/bruschetta_util.h"
+#include "chrome/browser/browser_process.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/browser/ui/test/test_browser_dialog.h"
@@ -79,7 +80,9 @@
   }
 
   void ShowUi(const std::string& name) override {
-    BruschettaInstallerView::Show(browser()->profile(), GetBruschettaAlphaId());
+    BruschettaInstallerView::Show(browser()->profile(),
+                                  *g_browser_process->local_state(),
+                                  GetBruschettaAlphaId());
     view_ = BruschettaInstallerView::GetActiveViewForTesting();
 
     ASSERT_NE(nullptr, view_);
diff --git a/chrome/browser/ui/webui/ash/settings/pages/crostini/crostini_handler.cc b/chrome/browser/ui/webui/ash/settings/pages/crostini/crostini_handler.cc
index 07865d0..8599007b 100644
--- a/chrome/browser/ui/webui/ash/settings/pages/crostini/crostini_handler.cc
+++ b/chrome/browser/ui/webui/ash/settings/pages/crostini/crostini_handler.cc
@@ -7,6 +7,7 @@
 #include <string>
 #include <utility>
 
+#include "base/check_deref.h"
 #include "base/functional/bind.h"
 #include "base/functional/callback_helpers.h"
 #include "base/metrics/histogram_functions.h"
@@ -1027,6 +1028,7 @@
     const base::Value::List& args) {
   AllowJavascript();
   BruschettaInstallerView::Show(Profile::FromWebUI(web_ui()),
+                                CHECK_DEREF(g_browser_process->local_state()),
                                 bruschetta::GetBruschettaAlphaId());
 }