Remove OptimizationGuideFetchingEnabled policy in favor of ComponentUpdatesEnabled policy

This guards ML model downloads behind the ComponentUpdatesEnabled policy as instructed by Chrome Enterprise team and also removes fetching of page load metadata (which is already gated by the Make Searches and Browsing Better) from a specific enterprise policy

Bug: 1320421
Change-Id: I12ca5ef91181a0fbc99db6dcd7cc68a50147c144
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3612141
Reviewed-by: Tarun Bansal <[email protected]>
Reviewed-by: Owen Min <[email protected]>
Reviewed-by: Sorin Jianu <[email protected]>
Reviewed-by: Gabriel Charette <[email protected]>
Reviewed-by: Sylvain Defresne <[email protected]>
Commit-Queue: Sophie Chang <[email protected]>
Cr-Commit-Position: refs/heads/main@{#997870}
diff --git a/chrome/browser/optimization_guide/optimization_guide_keyed_service.cc b/chrome/browser/optimization_guide/optimization_guide_keyed_service.cc
index ad5908e1..771bf04 100644
--- a/chrome/browser/optimization_guide/optimization_guide_keyed_service.cc
+++ b/chrome/browser/optimization_guide/optimization_guide_keyed_service.cc
@@ -24,6 +24,7 @@
 #include "chrome/browser/profiles/profile_key.h"
 #include "chrome/browser/profiles/profile_manager.h"
 #include "chrome/common/chrome_paths.h"
+#include "components/component_updater/pref_names.h"
 #include "components/leveldb_proto/public/proto_database_provider.h"
 #include "components/optimization_guide/core/command_line_top_host_provider.h"
 #include "components/optimization_guide/core/hints_processing_util.h"
@@ -40,6 +41,7 @@
 #include "components/optimization_guide/core/tab_url_provider.h"
 #include "components/optimization_guide/core/top_host_provider.h"
 #include "components/optimization_guide/proto/models.pb.h"
+#include "components/prefs/pref_service.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/navigation_handle.h"
@@ -107,8 +109,7 @@
     OPTIMIZATION_GUIDE_LOG(optimization_guide_logger,
                            "FEATURE_FLAG Hints component disabled");
   }
-  if (!optimization_guide::features::IsRemoteFetchingEnabled(
-          profile->GetPrefs())) {
+  if (!optimization_guide::features::IsRemoteFetchingEnabled()) {
     OPTIMIZATION_GUIDE_LOG(optimization_guide_logger,
                            "FEATURE_FLAG remote fetching feature disabled");
   }
@@ -165,6 +166,11 @@
   return BackgroundDownloadServiceFactory::GetForKey(profile->GetProfileKey());
 }
 
+bool OptimizationGuideKeyedService::ComponentUpdatesEnabledProvider() const {
+  return g_browser_process->local_state()->GetBoolean(
+      ::prefs::kComponentUpdatesEnabled);
+}
+
 void OptimizationGuideKeyedService::Initialize() {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
 
@@ -263,6 +269,11 @@
           &OptimizationGuideKeyedService::BackgroundDownloadServiceProvider,
           // It's safe to use |base::Unretained(this)| here because
           // |this| owns |prediction_manager_|.
+          base::Unretained(this)),
+      base::BindRepeating(
+          &OptimizationGuideKeyedService::ComponentUpdatesEnabledProvider,
+          // It's safe to use |base::Unretained(this)| here because
+          // |this| owns |prediction_manager_|.
           base::Unretained(this)));
 
   // The previous store paths were written in incorrect locations. Delete the
diff --git a/chrome/browser/optimization_guide/optimization_guide_keyed_service.h b/chrome/browser/optimization_guide/optimization_guide_keyed_service.h
index 74a92ad..78de30c 100644
--- a/chrome/browser/optimization_guide/optimization_guide_keyed_service.h
+++ b/chrome/browser/optimization_guide/optimization_guide_keyed_service.h
@@ -187,6 +187,8 @@
 
   download::BackgroundDownloadService* BackgroundDownloadServiceProvider();
 
+  bool ComponentUpdatesEnabledProvider() const;
+
   raw_ptr<content::BrowserContext> browser_context_;
 
   // The store of hints.
diff --git a/chrome/browser/optimization_guide/prediction/prediction_manager_browsertest.cc b/chrome/browser/optimization_guide/prediction/prediction_manager_browsertest.cc
index dc060b7..b639677 100644
--- a/chrome/browser/optimization_guide/prediction/prediction_manager_browsertest.cc
+++ b/chrome/browser/optimization_guide/prediction/prediction_manager_browsertest.cc
@@ -28,6 +28,7 @@
 #include "chrome/common/chrome_paths.h"
 #include "chrome/test/base/in_process_browser_test.h"
 #include "chrome/test/base/ui_test_utils.h"
+#include "components/component_updater/pref_names.h"
 #include "components/metrics/content/subprocess_metrics_provider.h"
 #include "components/optimization_guide/core/model_util.h"
 #include "components/optimization_guide/core/optimization_guide_constants.h"
@@ -315,11 +316,11 @@
 };
 
 IN_PROC_BROWSER_TEST_F(PredictionManagerBrowserTest,
-                       RemoteFetchingPrefDisabled) {
+                       ComponentUpdatesPrefDisabled) {
   ModelFileObserver model_file_observer;
   SetResponseType(PredictionModelsFetcherRemoteResponseType::kUnsuccessful);
-  browser()->profile()->GetPrefs()->SetBoolean(
-      optimization_guide::prefs::kOptimizationGuideFetchingEnabled, false);
+  g_browser_process->local_state()->SetBoolean(
+      ::prefs::kComponentUpdatesEnabled, false);
   base::HistogramTester histogram_tester;
 
   RegisterWithKeyedService(&model_file_observer);
diff --git a/chrome/browser/policy/configuration_policy_handler_list_factory.cc b/chrome/browser/policy/configuration_policy_handler_list_factory.cc
index aa6f218..e0308ec 100644
--- a/chrome/browser/policy/configuration_policy_handler_list_factory.cc
+++ b/chrome/browser/policy/configuration_policy_handler_list_factory.cc
@@ -78,7 +78,6 @@
 #include "components/metrics/metrics_pref_names.h"
 #include "components/network_time/network_time_pref_names.h"
 #include "components/omnibox/browser/omnibox_prefs.h"
-#include "components/optimization_guide/core/optimization_guide_prefs.h"
 #include "components/password_manager/core/common/password_manager_pref_names.h"
 #include "components/payments/core/payment_prefs.h"
 #include "components/policy/core/browser/configuration_policy_handler.h"
@@ -212,9 +211,6 @@
   { key::kEditBookmarksEnabled,
     bookmarks::prefs::kEditBookmarksEnabled,
     base::Value::Type::BOOLEAN },
-    { key::kOptimizationGuideFetchingEnabled,
-    optimization_guide::prefs::kOptimizationGuideFetchingEnabled,
-    base::Value::Type::BOOLEAN },
   { key::kPasswordManagerEnabled,
     password_manager::prefs::kCredentialsEnableService,
     base::Value::Type::BOOLEAN },
diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc
index a2f2393..6a49e31 100644
--- a/chrome/browser/prefs/browser_prefs.cc
+++ b/chrome/browser/prefs/browser_prefs.cc
@@ -770,6 +770,10 @@
 const char kTimeOnOobe[] = "settings.time_on_oobe";
 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
 
+// Deprecated 04/2022.
+const char kOptimizationGuideRemoteFetchingEnabled[] =
+    "optimization_guide.remote_fetching_enabled";
+
 // Register local state used only for migration (clearing or moving to a new
 // key).
 void RegisterLocalStatePrefsForMigration(PrefRegistrySimple* registry) {
@@ -1012,6 +1016,8 @@
 #if BUILDFLAG(IS_CHROMEOS_ASH)
   registry->RegisterIntegerPref(kTimeOnOobe, 0);
 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
+
+  registry->RegisterBooleanPref(kOptimizationGuideRemoteFetchingEnabled, true);
 }
 
 }  // namespace
@@ -1969,6 +1975,9 @@
   profile_prefs->ClearPref(kTimeOnOobe);
 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
 
+  // Added 04/2022
+  profile_prefs->ClearPref(kOptimizationGuideRemoteFetchingEnabled);
+
   // Please don't delete the following line. It is used by PRESUBMIT.py.
   // END_MIGRATE_OBSOLETE_PROFILE_PREFS