[FPF] Create Fingerprinting Protection ruleset service.

Hides it behind the `kEnableFingerprintingProtectionBlocklist` flag.
This flag is currently disabled by default.

Bug: 5419756
Change-Id: I36809b3f17d3811ee278b1c12434e00bf565339e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5420158
Reviewed-by: Alex Turner <[email protected]>
Commit-Queue: Zainab Rizvi <[email protected]>
Reviewed-by: Marc Treib <[email protected]>
Reviewed-by: Patricia Alfonso <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1284445}
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index b5d6fbd..79ffad74 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -2280,6 +2280,7 @@
     "//components/feed/mojom:mojo_bindings",
     "//components/filename_generation",
     "//components/find_in_page",
+    "//components/fingerprinting_protection_filter/browser",
     "//components/flags_ui",
     "//components/gcm_driver",
     "//components/handoff",
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS
index 17fb05ee..fb850f0 100644
--- a/chrome/browser/DEPS
+++ b/chrome/browser/DEPS
@@ -346,6 +346,7 @@
   "+components/subresource_filter/android",
   "+components/subresource_filter/content/shared/browser",
   "+components/subresource_filter/content/browser",
+  "+components/fingerprinting_protection_filter/browser",
   "+components/subresource_filter/content/mojom",
   "+components/subresource_filter/core/browser",
   "+components/subresource_filter/core/common",
diff --git a/chrome/browser/browser_process.h b/chrome/browser/browser_process.h
index 58b498d..0a7910b 100644
--- a/chrome/browser/browser_process.h
+++ b/chrome/browser/browser_process.h
@@ -230,6 +230,11 @@
   virtual subresource_filter::RulesetService*
   subresource_filter_ruleset_service() = 0;
 
+  // Returns the service providing versioned storage for rules used by the
+  // Fingerprinting Protection subresource filter.
+  virtual subresource_filter::RulesetService*
+  fingerprinting_protection_ruleset_service() = 0;
+
   // Returns the StartupData which owns any pre-created objects in //chrome
   // before the full browser starts.
   virtual StartupData* startup_data() = 0;
diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc
index 6034388..f55eeb4 100644
--- a/chrome/browser/browser_process_impl.cc
+++ b/chrome/browser/browser_process_impl.cc
@@ -99,6 +99,7 @@
 #include "components/component_updater/timer_update_scheduler.h"
 #include "components/crash/core/common/crash_key.h"
 #include "components/embedder_support/origin_trials/origin_trials_settings_storage.h"
+#include "components/fingerprinting_protection_filter/browser/fingerprinting_protection_filter_constants.h"
 #include "components/gcm_driver/gcm_driver.h"
 #include "components/language/core/browser/pref_names.h"
 #include "components/metrics/metrics_pref_names.h"
@@ -1134,6 +1135,17 @@
   return subresource_filter_ruleset_service_.get();
 }
 
+subresource_filter::RulesetService*
+BrowserProcessImpl::fingerprinting_protection_ruleset_service() {
+  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+  if (!created_fingerprinting_protection_ruleset_service_ &&
+      base::FeatureList::IsEnabled(
+          features::kEnableFingerprintingProtectionBlocklist)) {
+    CreateFingerprintingProtectionRulesetService();
+  }
+  return fingerprinting_protection_ruleset_service_.get();
+}
+
 StartupData* BrowserProcessImpl::startup_data() {
   return startup_data_;
 }
@@ -1437,6 +1449,21 @@
           user_data_dir);
 }
 
+void BrowserProcessImpl::CreateFingerprintingProtectionRulesetService() {
+  CHECK(!fingerprinting_protection_ruleset_service_);
+  // Set this to true so that we don't retry indefinitely to
+  // create the service if there was an error.
+  created_fingerprinting_protection_ruleset_service_ = true;
+
+  base::FilePath user_data_dir;
+  base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
+  fingerprinting_protection_ruleset_service_ =
+      subresource_filter::RulesetService::Create(
+          fingerprinting_protection_filter::
+              kFingerprintingProtectionRulesetConfig,
+          local_state(), user_data_dir);
+}
+
 #if !BUILDFLAG(IS_ANDROID)
 // Android's GCMDriver currently makes the assumption that it's a singleton.
 // Until this gets fixed, instantiating multiple Java GCMDrivers will throw an
diff --git a/chrome/browser/browser_process_impl.h b/chrome/browser/browser_process_impl.h
index 865b088..61101b6 100644
--- a/chrome/browser/browser_process_impl.h
+++ b/chrome/browser/browser_process_impl.h
@@ -198,6 +198,8 @@
   safe_browsing::SafeBrowsingService* safe_browsing_service() override;
   subresource_filter::RulesetService* subresource_filter_ruleset_service()
       override;
+  subresource_filter::RulesetService*
+  fingerprinting_protection_ruleset_service() override;
 
   StartupData* startup_data() override;
 
@@ -249,6 +251,7 @@
   void CreateBackgroundPrintingManager();
   void CreateSafeBrowsingService();
   void CreateSubresourceFilterRulesetService();
+  void CreateFingerprintingProtectionRulesetService();
   void CreateOptimizationGuideService();
   void CreateStatusTray();
   void CreateBackgroundModeManager();
@@ -362,6 +365,10 @@
   std::unique_ptr<subresource_filter::RulesetService>
       subresource_filter_ruleset_service_;
 
+  bool created_fingerprinting_protection_ruleset_service_ = false;
+  std::unique_ptr<subresource_filter::RulesetService>
+      fingerprinting_protection_ruleset_service_;
+
   bool shutting_down_ = false;
 
   bool tearing_down_ = false;
diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc
index c92ecce..f129b4b 100644
--- a/chrome/browser/chrome_browser_main.cc
+++ b/chrome/browser/chrome_browser_main.cc
@@ -1396,6 +1396,8 @@
   // available at no cost in an indexed format. This enables activating
   // subresource filtering, if needed, also for page loads on start-up.
   g_browser_process->subresource_filter_ruleset_service();
+  // Also enable subresource filtering for fingerprinting protection.
+  g_browser_process->fingerprinting_protection_ruleset_service();
 }
 
 void ChromeBrowserMainParts::PostBrowserStart() {
diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc
index 9037275..ee3440a 100644
--- a/chrome/browser/prefs/browser_prefs.cc
+++ b/chrome/browser/prefs/browser_prefs.cc
@@ -116,6 +116,7 @@
 #include "components/embedder_support/origin_trials/origin_trial_prefs.h"
 #include "components/enterprise/browser/identifiers/identifiers_prefs.h"
 #include "components/enterprise/buildflags/buildflags.h"
+#include "components/fingerprinting_protection_filter/browser/fingerprinting_protection_filter_constants.h"
 #include "components/flags_ui/pref_service_flags_storage.h"
 #include "components/history_clusters/core/history_clusters_prefs.h"
 #include "components/image_fetcher/core/cache/image_cache.h"
@@ -1554,6 +1555,10 @@
   SSLConfigServiceManager::RegisterPrefs(registry);
   subresource_filter::IndexedRulesetVersion::RegisterPrefs(
       registry, subresource_filter::kSafeBrowsingRulesetConfig.filter_tag);
+  subresource_filter::IndexedRulesetVersion::RegisterPrefs(
+      registry,
+      fingerprinting_protection_filter::kFingerprintingProtectionRulesetConfig
+          .filter_tag);
   SystemNetworkContextManager::RegisterPrefs(registry);
   tpcd::experiment::RegisterLocalStatePrefs(registry);
   tracing::RegisterPrefs(registry);
diff --git a/chrome/browser/subresource_filter/ruleset_browsertest.cc b/chrome/browser/subresource_filter/ruleset_browsertest.cc
index 5ee5482..514a763 100644
--- a/chrome/browser/subresource_filter/ruleset_browsertest.cc
+++ b/chrome/browser/subresource_filter/ruleset_browsertest.cc
@@ -7,6 +7,7 @@
 #include "base/test/metrics/histogram_tester.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
+#include "chrome/common/chrome_features.h"
 #include "chrome/test/base/testing_browser_process.h"
 #include "components/subresource_filter/content/shared/browser/ruleset_service.h"
 #include "components/subresource_filter/core/browser/async_document_subresource_filter.h"
@@ -226,4 +227,44 @@
   EXPECT_EQ(RulesetVerificationStatus::kIntact, dealer_status);
 }
 
+class SubresourceFilterBrowserTestFingerprintingProtectionDisabled
+    : public SubresourceFilterBrowserTest {
+ public:
+  SubresourceFilterBrowserTestFingerprintingProtectionDisabled() {
+    feature_list_.InitAndDisableFeature(
+        features::kEnableFingerprintingProtectionBlocklist);
+  }
+
+ private:
+  base::test::ScopedFeatureList feature_list_;
+};
+
+IN_PROC_BROWSER_TEST_F(
+    SubresourceFilterBrowserTestFingerprintingProtectionDisabled,
+    RulesetServiceNotCreated) {
+  EXPECT_EQ(g_browser_process->fingerprinting_protection_ruleset_service(),
+            nullptr);
+}
+
+class SubresourceFilterBrowserTestFingerprintingProtectionEnabled
+    : public SubresourceFilterBrowserTest {
+ public:
+  SubresourceFilterBrowserTestFingerprintingProtectionEnabled() {
+    feature_list_.InitAndEnableFeature(
+        features::kEnableFingerprintingProtectionBlocklist);
+  }
+
+ private:
+  base::test::ScopedFeatureList feature_list_;
+};
+
+IN_PROC_BROWSER_TEST_F(
+    SubresourceFilterBrowserTestFingerprintingProtectionEnabled,
+    RulesetServiceCreated) {
+  RulesetService* service =
+      g_browser_process->fingerprinting_protection_ruleset_service();
+  ASSERT_NE(service, nullptr);
+  EXPECT_NE(service->GetRulesetDealer(), nullptr);
+}
+
 }  // namespace subresource_filter
diff --git a/chrome/test/base/testing_browser_process.cc b/chrome/test/base/testing_browser_process.cc
index 041b2c7..4222aad 100644
--- a/chrome/test/base/testing_browser_process.cc
+++ b/chrome/test/base/testing_browser_process.cc
@@ -357,6 +357,11 @@
   return subresource_filter_ruleset_service_.get();
 }
 
+subresource_filter::RulesetService*
+TestingBrowserProcess::fingerprinting_protection_ruleset_service() {
+  return fingerprinting_protection_ruleset_service_.get();
+}
+
 BrowserProcessPlatformPart* TestingBrowserProcess::platform_part() {
   return platform_part_.get();
 }
diff --git a/chrome/test/base/testing_browser_process.h b/chrome/test/base/testing_browser_process.h
index 83db31b8..43c3c87 100644
--- a/chrome/test/base/testing_browser_process.h
+++ b/chrome/test/base/testing_browser_process.h
@@ -116,6 +116,8 @@
   safe_browsing::SafeBrowsingService* safe_browsing_service() override;
   subresource_filter::RulesetService* subresource_filter_ruleset_service()
       override;
+  subresource_filter::RulesetService*
+  fingerprinting_protection_ruleset_service() override;
   BrowserProcessPlatformPart* platform_part() override;
 
   extensions::EventRouterForwarder* extension_event_router_forwarder() override;
@@ -239,6 +241,8 @@
   scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_;
   std::unique_ptr<subresource_filter::RulesetService>
       subresource_filter_ruleset_service_;
+  std::unique_ptr<subresource_filter::RulesetService>
+      fingerprinting_protection_ruleset_service_;
   std::unique_ptr<WebRtcLogUploader> webrtc_log_uploader_;
 
   std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;