[Autofill] Pull ScopedAutofillManagersObservation out of content/

This CL moves ScopedAutofillManagersObservation from
//components/autofill/content/browser to
//components/autofill/core/browser/foundations and thus makes it
available on iOS.

It doesn't move the unit test because we don't have a
TestAutofillDriverFactory yet that'd suffice for the test.

Bypass-Check-License: Renamed files.
Bug: 445023368, 40178290
Change-Id: I0193c0a1cdf8335cce8a1e3420f2649ed5832ecd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6943178
Reviewed-by: Jan Keitel <jkeitel@google.com>
Commit-Queue: Christoph Schwering <schwering@google.com>
Cr-Commit-Position: refs/heads/main@{#1515378}
diff --git a/chrome/browser/autofill/autofill_captured_sites_interactive_uitest.cc b/chrome/browser/autofill/autofill_captured_sites_interactive_uitest.cc
index 5ee23d0..44d4c9b 100644
--- a/chrome/browser/autofill/autofill_captured_sites_interactive_uitest.cc
+++ b/chrome/browser/autofill/autofill_captured_sites_interactive_uitest.cc
@@ -42,11 +42,11 @@
 #include "chrome/test/base/interactive_test_utils.h"
 #include "chrome/test/base/ui_test_utils.h"
 #include "components/autofill/content/browser/content_autofill_driver.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/field_types.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
 #include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
 #include "components/autofill/core/browser/foundations/browser_autofill_manager_test_delegate.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/geo/state_names.h"
 #include "components/autofill/core/browser/proto/server.pb.h"
 #include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
@@ -97,8 +97,8 @@
   }
 }
 
-autofill::ElementExpr GetElementByXpath(const std::string& xpath) {
-  return autofill::ElementExpr(base::StringPrintf(
+ElementExpr GetElementByXpath(const std::string& xpath) {
+  return ElementExpr(base::StringPrintf(
       "automation_helper.getElementByXpath(`%s`)", xpath.c_str()));
 }
 
@@ -130,20 +130,21 @@
 
 // Used to verify that the expected form signatures are submitted during the
 // test.
-class FormSubmissionCounter : public autofill::AutofillManager::Observer {
+class FormSubmissionCounter : public AutofillManager::Observer {
  public:
   explicit FormSubmissionCounter(content::WebContents* web_contents) {
     autofill_managers_observation_.Observe(
-        web_contents, autofill::ScopedAutofillManagersObservation::
-                          InitializationPolicy::kObservePreexistingManagers);
+        ContentAutofillClient::FromWebContents(web_contents),
+        ScopedAutofillManagersObservation::InitializationPolicy::
+            kObservePreexistingManagers);
   }
   ~FormSubmissionCounter() override = default;
 
   // AutofillManager::Observer:
-  void OnBeforeFormSubmitted(autofill::AutofillManager& manager,
-                             const autofill::FormData& form_data) override {
-    actual_form_signatures_submitted_.insert(base::NumberToString(
-        autofill::CalculateFormSignature(form_data).value()));
+  void OnBeforeFormSubmitted(AutofillManager& manager,
+                             const FormData& form_data) override {
+    actual_form_signatures_submitted_.insert(
+        base::NumberToString(CalculateFormSignature(form_data).value()));
   }
 
   void VerifyFormSubmissions(
@@ -161,8 +162,7 @@
 
  private:
   std::set<std::string> actual_form_signatures_submitted_;
-  autofill::ScopedAutofillManagersObservation autofill_managers_observation_{
-      this};
+  ScopedAutofillManagersObservation autofill_managers_observation_{this};
 };
 
 // Implements the `kAutofillCapturedSiteTestsMetricsScraper` testing feature.
@@ -266,7 +266,7 @@
       TryToCloseAllPrompts(web_contents);
 
       autofill_manager.client().HideAutofillSuggestions(
-          autofill::SuggestionHidingReason::kViewDestroyed);
+          SuggestionHidingReason::kViewDestroyed);
 
       testing::AssertionResult suggestions_shown = ShowAutofillSuggestion(
           focus_element_css_selector, iframe_path, frame);
@@ -319,7 +319,7 @@
     }
 
     autofill_manager.client().HideAutofillSuggestions(
-        autofill::SuggestionHidingReason::kViewDestroyed);
+        SuggestionHidingReason::kViewDestroyed);
     ADD_FAILURE() << "Failed to autofill the form!";
     return false;
   }
diff --git a/chrome/browser/compose/chrome_compose_client.cc b/chrome/browser/compose/chrome_compose_client.cc
index e4a486f4..c8c2ae92 100644
--- a/chrome/browser/compose/chrome_compose_client.cc
+++ b/chrome/browser/compose/chrome_compose_client.cc
@@ -104,8 +104,9 @@
     content::WebContents* web_contents)
     : web_contents_(web_contents) {
   autofill_managers_observation_.Observe(
-      web_contents, autofill::ScopedAutofillManagersObservation::
-                        InitializationPolicy::kObservePreexistingManagers);
+      autofill::ContentAutofillClient::FromWebContents(web_contents),
+      autofill::ScopedAutofillManagersObservation::InitializationPolicy::
+          kObservePreexistingManagers);
 }
 
 ChromeComposeClient::FieldChangeObserver::~FieldChangeObserver() = default;
@@ -181,8 +182,9 @@
   }
 
   autofill_managers_observation_.Observe(
-      web_contents, autofill::ScopedAutofillManagersObservation::
-                        InitializationPolicy::kObservePreexistingManagers);
+      autofill::ContentAutofillDriverFactory::FromWebContents(web_contents),
+      autofill::ScopedAutofillManagersObservation::InitializationPolicy::
+          kObservePreexistingManagers);
   nudge_tracker_.StartObserving(web_contents);
 }
 
diff --git a/chrome/browser/compose/chrome_compose_client.h b/chrome/browser/compose/chrome_compose_client.h
index edb33ba..bb04dc4 100644
--- a/chrome/browser/compose/chrome_compose_client.h
+++ b/chrome/browser/compose/chrome_compose_client.h
@@ -17,8 +17,8 @@
 #include "chrome/browser/compose/proactive_nudge_tracker.h"
 #include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
 #include "chrome/common/compose/compose.mojom.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/common/unique_ids.h"
 #include "components/compose/core/browser/compose_client.h"
 #include "components/compose/core/browser/compose_dialog_controller.h"
diff --git a/chrome/browser/compose/proactive_nudge_tracker.cc b/chrome/browser/compose/proactive_nudge_tracker.cc
index b29897b..3175109 100644
--- a/chrome/browser/compose/proactive_nudge_tracker.cc
+++ b/chrome/browser/compose/proactive_nudge_tracker.cc
@@ -15,6 +15,7 @@
 #include "base/time/time.h"
 #include "base/types/cxx23_to_underlying.h"
 #include "chrome/browser/compose/proto/compose_optimization_guide.pb.h"
+#include "components/autofill/content/browser/content_autofill_client.h"
 #include "components/autofill/core/common/signatures.h"
 #include "components/compose/core/browser/compose_metrics.h"
 #include "components/compose/core/browser/config.h"
@@ -187,8 +188,9 @@
     return;
   }
   autofill_managers_observation_.Observe(
-      web_contents, autofill::ScopedAutofillManagersObservation::
-                        InitializationPolicy::kObservePreexistingManagers);
+      autofill::ContentAutofillClient::FromWebContents(web_contents),
+      autofill::ScopedAutofillManagersObservation::InitializationPolicy::
+          kObservePreexistingManagers);
 }
 
 ProactiveNudgeTracker::~ProactiveNudgeTracker() {
diff --git a/chrome/browser/compose/proactive_nudge_tracker.h b/chrome/browser/compose/proactive_nudge_tracker.h
index a4495ff..c676f38a 100644
--- a/chrome/browser/compose/proactive_nudge_tracker.h
+++ b/chrome/browser/compose/proactive_nudge_tracker.h
@@ -15,12 +15,16 @@
 #include "base/memory/raw_ref.h"
 #include "base/memory/weak_ptr.h"
 #include "chrome/browser/compose/proto/compose_optimization_guide.pb.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/suggestions/suggestion.h"
 #include "components/autofill/core/common/unique_ids.h"
 #include "components/compose/core/browser/compose_metrics.h"
 #include "components/segmentation_platform/public/segmentation_platform_service.h"
 
+namespace content {
+class WebContents;
+}
+
 namespace compose {
 
 // This class is a state machine tracking whether the proactive nudge should
diff --git a/chrome/browser/devtools/protocol/autofill_handler.cc b/chrome/browser/devtools/protocol/autofill_handler.cc
index 2f0802b..e36d14ea 100644
--- a/chrome/browser/devtools/protocol/autofill_handler.cc
+++ b/chrome/browser/devtools/protocol/autofill_handler.cc
@@ -17,7 +17,6 @@
 #include "components/autofill/content/browser/content_autofill_client.h"
 #include "components/autofill/content/browser/content_autofill_driver.h"
 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
 #include "components/autofill/core/browser/data_model/payments/credit_card.h"
 #include "components/autofill/core/browser/field_type_utils.h"
@@ -26,6 +25,7 @@
 #include "components/autofill/core/browser/form_structure.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
 #include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/manual_testing_import.h"
 #include "components/autofill/core/browser/ui/addresses/autofill_address_util.h"
 #include "components/autofill/core/common/autofill_features.h"
diff --git a/chrome/browser/password_manager/chrome_password_manager_client.cc b/chrome/browser/password_manager/chrome_password_manager_client.cc
index 326b9783..8bcfa90 100644
--- a/chrome/browser/password_manager/chrome_password_manager_client.cc
+++ b/chrome/browser/password_manager/chrome_password_manager_client.cc
@@ -56,7 +56,7 @@
 #include "chrome/grit/generated_resources.h"
 #include "components/autofill/content/browser/content_autofill_client.h"
 #include "components/autofill/content/browser/renderer_forms_from_browser_form.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/logging/log_manager.h"
 #include "components/autofill/core/browser/logging/log_router.h"
 #include "components/autofill/core/common/autofill_util.h"
@@ -1831,8 +1831,9 @@
   ContentPasswordManagerDriverFactory::CreateForWebContents(web_contents, this);
 
   autofill_managers_observation_.Observe(
-      web_contents, autofill::ScopedAutofillManagersObservation::
-                        InitializationPolicy::kObservePreexistingManagers);
+      autofill::ContentAutofillClient::FromWebContents(web_contents),
+      autofill::ScopedAutofillManagersObservation::InitializationPolicy::
+          kObservePreexistingManagers);
 }
 
 void ChromePasswordManagerClient::RenderFrameDeleted(
diff --git a/chrome/browser/password_manager/chrome_password_manager_client.h b/chrome/browser/password_manager/chrome_password_manager_client.h
index 39ac059..70effd1 100644
--- a/chrome/browser/password_manager/chrome_password_manager_client.h
+++ b/chrome/browser/password_manager/chrome_password_manager_client.h
@@ -15,8 +15,8 @@
 #include "base/memory/raw_ptr.h"
 #include "build/build_config.h"
 #include "build/buildflag.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/common/password_generation_util.h"
 #include "components/autofill/core/common/unique_ids.h"
 #include "components/credential_management/content_credential_manager.h"
diff --git a/components/autofill/content/browser/BUILD.gn b/components/autofill/content/browser/BUILD.gn
index 2564be7..95ee3734 100644
--- a/components/autofill/content/browser/BUILD.gn
+++ b/components/autofill/content/browser/BUILD.gn
@@ -28,8 +28,6 @@
     "renderer_forms_from_browser_form.h",
     "risk/fingerprint.cc",
     "risk/fingerprint.h",
-    "scoped_autofill_managers_observation.cc",
-    "scoped_autofill_managers_observation.h",
     "suggestions/identity_credential_suggestion_generator.cc",
     "suggestions/identity_credential_suggestion_generator.h",
   ]
diff --git a/components/autofill/content/browser/content_autofill_driver_factory.cc b/components/autofill/content/browser/content_autofill_driver_factory.cc
index 0382e5fc..b9c7b67a 100644
--- a/components/autofill/content/browser/content_autofill_driver_factory.cc
+++ b/components/autofill/content/browser/content_autofill_driver_factory.cc
@@ -8,6 +8,7 @@
 #include <utility>
 #include <vector>
 
+#include "base/containers/to_vector.h"
 #include "base/feature_list.h"
 #include "base/metrics/histogram_functions.h"
 #include "components/autofill/content/browser/content_autofill_client.h"
@@ -242,15 +243,11 @@
                                   : AutofillDriver::LifecycleState::kInactive);
 }
 
-std::vector<ContentAutofillDriver*>
-ContentAutofillDriverFactory::GetExistingDrivers(
+std::vector<AutofillDriver*> ContentAutofillDriverFactory::GetExistingDrivers(
     base::PassKey<ScopedAutofillManagersObservation>) {
-  std::vector<ContentAutofillDriver*> drivers;
-  drivers.reserve(driver_map_.size());
-  for (const auto& [rfh, driver] : driver_map_) {
-    drivers.push_back(driver.get());
-  }
-  return drivers;
+  return base::ToVector(driver_map_, [](const auto& p) -> AutofillDriver* {
+    return p.second.get();
+  });
 }
 
 }  // namespace autofill
diff --git a/components/autofill/content/browser/content_autofill_driver_factory.h b/components/autofill/content/browser/content_autofill_driver_factory.h
index 3ef30a64..dbbb8b7 100644
--- a/components/autofill/content/browser/content_autofill_driver_factory.h
+++ b/components/autofill/content/browser/content_autofill_driver_factory.h
@@ -91,9 +91,8 @@
 
   size_t num_drivers() const { return driver_map_.size(); }
 
-  // Returns raw pointers to all drivers that the factory currently owns.
-  std::vector<ContentAutofillDriver*> GetExistingDrivers(
-      base::PassKey<ScopedAutofillManagersObservation>);
+  std::vector<AutofillDriver*> GetExistingDrivers(
+      base::PassKey<ScopedAutofillManagersObservation>) override;
 
   ContentAutofillDriver* DriverForFrame(
       content::RenderFrameHost* render_frame_host,
diff --git a/components/autofill/content/browser/scoped_autofill_managers_observation_unittest.cc b/components/autofill/content/browser/scoped_autofill_managers_observation_unittest.cc
index 806bfb66..b5a0c1f7 100644
--- a/components/autofill/content/browser/scoped_autofill_managers_observation_unittest.cc
+++ b/components/autofill/content/browser/scoped_autofill_managers_observation_unittest.cc
@@ -2,15 +2,15 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 
 #include "base/test/gtest_util.h"
-#include "components/autofill/content/browser/content_autofill_driver_factory.h"
 #include "components/autofill/content/browser/test_autofill_client_injector.h"
 #include "components/autofill/content/browser/test_autofill_driver_injector.h"
 #include "components/autofill/content/browser/test_autofill_manager_injector.h"
 #include "components/autofill/content/browser/test_content_autofill_client.h"
 #include "components/autofill/core/browser/foundations/autofill_driver.h"
+#include "components/autofill/core/browser/foundations/autofill_driver_factory.h"
 #include "components/autofill/core/browser/foundations/mock_autofill_manager_observer.h"
 #include "components/autofill/core/browser/foundations/test_browser_autofill_manager.h"
 #include "content/public/test/navigation_simulator.h"
@@ -24,11 +24,10 @@
 using ::testing::NiceMock;
 using ::testing::Ref;
 
+// TODO(crbug.com/40714201): Add a TestAutofillDriverFactory and then move this
+// test to //components/autofill/core/browser.
 class ScopedAutofillManagersObservationTest
     : public content::RenderViewHostTestHarness {
- public:
-  void SetUp() override { content::RenderViewHostTestHarness::SetUp(); }
-
  protected:
   // TODO(crbug.com/40276395): Move this code (and the nearly identical function
   // in `FormForest`'s unittest) into a common helper function.
@@ -53,6 +52,8 @@
     return simulator->GetFinalRenderFrameHost();
   }
 
+  AutofillClient* client() { return autofill_client_injector_[web_contents()]; }
+
   AutofillManager* manager(content::RenderFrameHost* rfh) {
     ContentAutofillDriver* driver = autofill_driver_injector_[rfh];
     return driver ? &driver->GetAutofillManager() : nullptr;
@@ -69,7 +70,7 @@
 TEST_F(ScopedAutofillManagersObservationTest, SingleFrameObservation) {
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents());
+  observation.Observe(client());
   NavigateAndCommit(GURL("https://a.com/"));
 
   EXPECT_CALL(observer, OnBeforeLanguageDetermined(Ref(*manager(main_rfh()))));
@@ -84,8 +85,8 @@
   ScopedAutofillManagersObservation observation(&observer);
 
   EXPECT_CHECK_DEATH(observation.Observe(
-      web_contents(), ScopedAutofillManagersObservation::InitializationPolicy::
-                          kExpectNoPreexistingManagers));
+      client(), ScopedAutofillManagersObservation::InitializationPolicy::
+                    kExpectNoPreexistingManagers));
 }
 
 TEST_F(ScopedAutofillManagersObservationTest,
@@ -94,7 +95,7 @@
 
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents(),
+  observation.Observe(client(),
                       ScopedAutofillManagersObservation::InitializationPolicy::
                           kObservePreexistingManagers);
 
@@ -107,7 +108,7 @@
        SingleFrameObservationWithNavigation) {
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents());
+  observation.Observe(client());
   NavigateAndCommit(GURL("https://a.com/"));
 
   EXPECT_CALL(observer, OnBeforeLanguageDetermined(Ref(*manager(main_rfh()))));
@@ -123,7 +124,7 @@
 TEST_F(ScopedAutofillManagersObservationTest, NoObservationsAfterReset) {
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents());
+  observation.Observe(client());
   NavigateAndCommit(GURL("https://a.com/"));
 
   EXPECT_CALL(observer, OnBeforeLanguageDetermined(Ref(*manager(main_rfh()))));
@@ -139,7 +140,7 @@
 TEST_F(ScopedAutofillManagersObservationTest, MultipleFrameObservation) {
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents());
+  observation.Observe(client());
   NavigateAndCommit(GURL("https://a.com/"));
 
   content::RenderFrameHost* child_rfh =
@@ -159,7 +160,7 @@
        StateChangedToPendingDeletionNotifiesObserver) {
   MockAutofillManagerObserver observer;
   ScopedAutofillManagersObservation observation(&observer);
-  observation.Observe(web_contents());
+  observation.Observe(client());
   NavigateAndCommit(GURL("https://a.com/"));
 
   EXPECT_CALL(observer, OnAutofillManagerStateChanged(
diff --git a/components/autofill/core/browser/BUILD.gn b/components/autofill/core/browser/BUILD.gn
index 73039c7..85090cec 100644
--- a/components/autofill/core/browser/BUILD.gn
+++ b/components/autofill/core/browser/BUILD.gn
@@ -349,6 +349,8 @@
     "foundations/form_forest.cc",
     "foundations/form_forest.h",
     "foundations/form_forest_util_inl.h",
+    "foundations/scoped_autofill_managers_observation.cc",
+    "foundations/scoped_autofill_managers_observation.h",
     "geo/address_i18n.cc",
     "geo/address_i18n.h",
     "geo/address_rewriter.cc",
diff --git a/components/autofill/core/browser/foundations/autofill_driver_factory.cc b/components/autofill/core/browser/foundations/autofill_driver_factory.cc
index 1615254..a19ef0d1 100644
--- a/components/autofill/core/browser/foundations/autofill_driver_factory.cc
+++ b/components/autofill/core/browser/foundations/autofill_driver_factory.cc
@@ -27,4 +27,9 @@
       old_state, new_state, /*pass_key=*/{});
 }
 
+std::vector<AutofillDriver*> AutofillDriverFactory::GetExistingDrivers(
+    base::PassKey<ScopedAutofillManagersObservation>) {
+  return {};
+}
+
 }  // namespace autofill
diff --git a/components/autofill/core/browser/foundations/autofill_driver_factory.h b/components/autofill/core/browser/foundations/autofill_driver_factory.h
index 45e8c634..05c85de5 100644
--- a/components/autofill/core/browser/foundations/autofill_driver_factory.h
+++ b/components/autofill/core/browser/foundations/autofill_driver_factory.h
@@ -10,6 +10,8 @@
 
 namespace autofill {
 
+class ScopedAutofillManagersObservation;
+
 // The common interface for platform-dependent AutofillDriver factories:
 // - ContentAutofillDriverFactory
 // - AutofillDriverIOSFactory
@@ -62,6 +64,12 @@
     observers_.RemoveObserver(observer);
   }
 
+  // Returns raw pointers to all drivers that the factory currently owns.
+  // TODO(crbug.com/40178290): Make this pure virtual once tests use a
+  // TestAutofillDriverFactory.
+  virtual std::vector<AutofillDriver*> GetExistingDrivers(
+      base::PassKey<ScopedAutofillManagersObservation>);
+
  protected:
   friend class AutofillDriverFactoryTestApi;
 
diff --git a/components/autofill/content/browser/scoped_autofill_managers_observation.cc b/components/autofill/core/browser/foundations/scoped_autofill_managers_observation.cc
similarity index 65%
rename from components/autofill/content/browser/scoped_autofill_managers_observation.cc
rename to components/autofill/core/browser/foundations/scoped_autofill_managers_observation.cc
index 1f89f54..42fea0a 100644
--- a/components/autofill/content/browser/scoped_autofill_managers_observation.cc
+++ b/components/autofill/core/browser/foundations/scoped_autofill_managers_observation.cc
@@ -2,15 +2,14 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 
 #include "base/check_op.h"
 #include "base/scoped_multi_source_observation.h"
 #include "base/scoped_observation.h"
-#include "components/autofill/content/browser/content_autofill_driver.h"
-#include "components/autofill/content/browser/content_autofill_driver_factory.h"
+#include "components/autofill/core/browser/foundations/autofill_driver.h"
+#include "components/autofill/core/browser/foundations/autofill_driver_factory.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
-#include "content/public/browser/web_contents.h"
 
 namespace autofill {
 
@@ -23,15 +22,21 @@
 }
 
 void ScopedAutofillManagersObservation::Observe(
-    ContentAutofillDriverFactory* factory,
+    AutofillClient* client,
+    InitializationPolicy initialization_policy) {
+  return Observe(&client->GetAutofillDriverFactory(), initialization_policy);
+}
+
+void ScopedAutofillManagersObservation::Observe(
+    AutofillDriverFactory* factory,
     InitializationPolicy initialization_policy) {
   factory_observation_.Observe(factory);
   switch (initialization_policy) {
     case InitializationPolicy::kExpectNoPreexistingManagers:
-      CHECK_EQ(factory->num_drivers(), 0u);
+      CHECK_EQ(factory->GetExistingDrivers({}).size(), 0u);
       break;
     case InitializationPolicy::kObservePreexistingManagers:
-      for (ContentAutofillDriver* driver : factory->GetExistingDrivers({})) {
+      for (AutofillDriver* driver : factory->GetExistingDrivers({})) {
         autofill_manager_observations_.AddObservation(
             &driver->GetAutofillManager());
       }
@@ -39,37 +44,30 @@
   }
 }
 
-void ScopedAutofillManagersObservation::Observe(
-    content::WebContents* contents,
-    InitializationPolicy initialization_policy) {
-  Observe(ContentAutofillDriverFactory::FromWebContents(contents),
-          initialization_policy);
-}
-
 void ScopedAutofillManagersObservation::Reset() {
   factory_observation_.Reset();
   autofill_manager_observations_.RemoveAllObservations();
 }
 
-content::WebContents* ScopedAutofillManagersObservation::web_contents() {
-  ContentAutofillDriverFactory* factory = factory_observation_.GetSource();
-  return factory ? factory->web_contents() : nullptr;
+AutofillDriverFactory*
+ScopedAutofillManagersObservation::autofill_driver_factory() {
+  return factory_observation_.GetSource();
 }
 
-void ScopedAutofillManagersObservation::OnContentAutofillDriverFactoryDestroyed(
-    ContentAutofillDriverFactory& factory) {
+void ScopedAutofillManagersObservation::OnAutofillDriverFactoryDestroyed(
+    AutofillDriverFactory& factory) {
   Reset();
 }
 
-void ScopedAutofillManagersObservation::OnContentAutofillDriverCreated(
-    ContentAutofillDriverFactory& factory,
-    ContentAutofillDriver& driver) {
+void ScopedAutofillManagersObservation::OnAutofillDriverCreated(
+    AutofillDriverFactory& factory,
+    AutofillDriver& driver) {
   autofill_manager_observations_.AddObservation(&driver.GetAutofillManager());
 }
 
-void ScopedAutofillManagersObservation::OnContentAutofillDriverStateChanged(
-    ContentAutofillDriverFactory& factory,
-    ContentAutofillDriver& driver,
+void ScopedAutofillManagersObservation::OnAutofillDriverStateChanged(
+    AutofillDriverFactory& factory,
+    AutofillDriver& driver,
     AutofillDriver::LifecycleState old_state,
     AutofillDriver::LifecycleState new_state) {
   switch (new_state) {
diff --git a/components/autofill/content/browser/scoped_autofill_managers_observation.h b/components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h
similarity index 62%
rename from components/autofill/content/browser/scoped_autofill_managers_observation.h
rename to components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h
index 1e7c8a4f..e7930a64 100644
--- a/components/autofill/content/browser/scoped_autofill_managers_observation.h
+++ b/components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h
@@ -2,25 +2,21 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
-#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
+#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
+#define COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
 
 #include "base/scoped_multi_source_observation.h"
 #include "base/scoped_observation.h"
-#include "components/autofill/content/browser/content_autofill_driver_factory.h"
+#include "components/autofill/core/browser/foundations/autofill_driver_factory.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
 
-namespace content {
-class WebContents;
-}
-
 namespace autofill {
 
-class ContentAutofillDriver;
+class AutofillDriver;
 
 // `ScopedAutofillManagersObservation` is a helper to reduce boilerplate when
-// observing all `AutofillManagers` of a `WebContents`. Instead of having to
-// implement both `ContentAutofillDriverFactory`'s and `AutofillManager`'s
+// observing all `AutofillManagers` of an `AutofillClient`. Instead of having to
+// implement both `AutofillDriverFactory`'s and `AutofillManager`'s
 // observer interfaces, the consumer now only needs to implement
 // `AutofillManager::Observer` and add a member variable of this type.
 //
@@ -28,7 +24,8 @@
 // class AutofillInformationConsumer : public AutofillManager::Observer {
 //  public:
 //   AutofillInformationConsumer(content::WebContents* contents) {
-//     autofill_managers_observation.Observe(contents);
+//     auto* client = ContentAutofillClient::FromWebContents(contents);
+//     autofill_managers_observation.Observe(client);
 //   }
 //
 //   // AutofillManager::Observer:
@@ -39,14 +36,18 @@
 //  private:
 //   ScopedAutofillManagersObservation autofill_managers_observation_{this};
 // };
+//
+// The unit test lives in //components/autofill/content/browser because there is
+// currently no TestAutofillDriverFactory.
+// TODO(crbug.com/40714201): Add TestAutofillDriverFactory and move the test.
 class ScopedAutofillManagersObservation final
-    : public ContentAutofillDriverFactory::Observer {
+    : public AutofillDriverFactory::Observer {
  public:
   explicit ScopedAutofillManagersObservation(AutofillManager::Observer*);
   ~ScopedAutofillManagersObservation() override;
 
-  // Starts observing `factory` or the `contents`' factory and the managers of
-  // all of its owned drivers.
+  // Starts observing a client's AutofillManagers.
+  //
   // The `initialization_policy` defines how to handle `AutofillManager`s that
   // already exist at the time the observation starts.
   // - If the policy is `kExpectNoPreexistingManagers`, then `this` CHECKS that
@@ -61,34 +62,33 @@
     kExpectNoPreexistingManagers,
     kObservePreexistingManagers,
   };
-  void Observe(ContentAutofillDriverFactory* factory,
+  void Observe(AutofillClient* client,
                InitializationPolicy initialization_policy =
                    InitializationPolicy::kExpectNoPreexistingManagers);
-  void Observe(content::WebContents* contents,
+  void Observe(AutofillDriverFactory* factory,
                InitializationPolicy initialization_policy =
                    InitializationPolicy::kExpectNoPreexistingManagers);
 
   // Resets all observations.
   void Reset();
 
-  // Returns the `WebContents` whose factory it is observing.
-  content::WebContents* web_contents();
+  AutofillDriverFactory* autofill_driver_factory();
 
  private:
-  // ContentAutofillDriverFactory::Observer:
-  void OnContentAutofillDriverFactoryDestroyed(
-      ContentAutofillDriverFactory& factory) override;
-  void OnContentAutofillDriverCreated(ContentAutofillDriverFactory& factory,
-                                      ContentAutofillDriver& driver) override;
-  void OnContentAutofillDriverStateChanged(
-      ContentAutofillDriverFactory& factory,
-      ContentAutofillDriver& driver,
+  // AutofillDriverFactory::Observer:
+  void OnAutofillDriverFactoryDestroyed(
+      AutofillDriverFactory& factory) override;
+  void OnAutofillDriverCreated(AutofillDriverFactory& factory,
+                               AutofillDriver& driver) override;
+  void OnAutofillDriverStateChanged(
+      AutofillDriverFactory& factory,
+      AutofillDriver& driver,
       AutofillDriver::LifecycleState old_state,
       AutofillDriver::LifecycleState new_state) override;
 
   // The observation used to track driver creation and destruction.
-  base::ScopedObservation<ContentAutofillDriverFactory,
-                          ContentAutofillDriverFactory::Observer>
+  base::ScopedObservation<AutofillDriverFactory,
+                          AutofillDriverFactory::Observer>
       factory_observation_{this};
   // The observation used to forward events to `observer_`.
   base::ScopedMultiSourceObservation<AutofillManager, AutofillManager::Observer>
@@ -97,4 +97,4 @@
 
 }  // namespace autofill
 
-#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
+#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_SCOPED_AUTOFILL_MANAGERS_OBSERVATION_H_
diff --git a/components/autofill/ios/browser/autofill_driver_ios_factory.h b/components/autofill/ios/browser/autofill_driver_ios_factory.h
index 80e254e..c164fc5 100644
--- a/components/autofill/ios/browser/autofill_driver_ios_factory.h
+++ b/components/autofill/ios/browser/autofill_driver_ios_factory.h
@@ -73,6 +73,9 @@
 
   AutofillDriverRouter& router() { return router_; }
 
+  std::vector<AutofillDriver*> GetExistingDrivers(
+      base::PassKey<ScopedAutofillManagersObservation>) override;
+
  private:
   friend class AutofillDriverIOSFactoryTestApi;
 
diff --git a/components/autofill/ios/browser/autofill_driver_ios_factory.mm b/components/autofill/ios/browser/autofill_driver_ios_factory.mm
index e4507e0d..957f38e 100644
--- a/components/autofill/ios/browser/autofill_driver_ios_factory.mm
+++ b/components/autofill/ios/browser/autofill_driver_ios_factory.mm
@@ -149,4 +149,16 @@
   return driver.get();
 }
 
+std::vector<AutofillDriver*> AutofillDriverIOSFactory::GetExistingDrivers(
+    base::PassKey<ScopedAutofillManagersObservation>) {
+  std::vector<AutofillDriver*> drivers;
+  drivers.reserve(driver_map_.size());
+  for (const auto& [frame_id, driver] : driver_map_) {
+    if (driver) {
+      drivers.push_back(driver.get());
+    }
+  }
+  return drivers;
+}
+
 }  //  namespace autofill
diff --git a/components/sensitive_content/sensitive_content_manager.cc b/components/sensitive_content/sensitive_content_manager.cc
index 6d4b8eba..2637975 100644
--- a/components/sensitive_content/sensitive_content_manager.cc
+++ b/components/sensitive_content/sensitive_content_manager.cc
@@ -7,6 +7,7 @@
 #include "base/check_deref.h"
 #include "base/metrics/histogram_functions.h"
 #include "base/strings/strcat.h"
+#include "components/autofill/content/browser/content_autofill_client.h"
 #include "components/autofill/core/browser/autofill_field.h"
 #include "components/autofill/core/browser/field_types.h"
 #include "components/autofill/core/browser/form_structure.h"
@@ -48,8 +49,9 @@
     SensitiveContentClient* client)
     : client_(CHECK_DEREF(client)) {
   autofill_managers_observation_.Observe(
-      web_contents, autofill::ScopedAutofillManagersObservation::
-                        InitializationPolicy::kObservePreexistingManagers);
+      autofill::ContentAutofillClient::FromWebContents(web_contents),
+      autofill::ScopedAutofillManagersObservation::InitializationPolicy::
+          kObservePreexistingManagers);
 }
 
 SensitiveContentManager::~SensitiveContentManager() = default;
diff --git a/components/sensitive_content/sensitive_content_manager.h b/components/sensitive_content/sensitive_content_manager.h
index d7abdcc..3045b00 100644
--- a/components/sensitive_content/sensitive_content_manager.h
+++ b/components/sensitive_content/sensitive_content_manager.h
@@ -7,9 +7,9 @@
 
 #include "base/memory/raw_ref.h"
 #include "base/time/time.h"
-#include "components/autofill/content/browser/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/browser/foundations/autofill_driver.h"
 #include "components/autofill/core/browser/foundations/autofill_manager.h"
+#include "components/autofill/core/browser/foundations/scoped_autofill_managers_observation.h"
 #include "components/autofill/core/common/unique_ids.h"
 
 namespace content {