Reland "[Autofill] Simplify ContentAutofillDriver initialization"

This is a reland of commit 3b90869541da848d1791a4ca8b44bc0fd4af8347

The CL did not cause the breakage. The tests seem to be
failing quite consistently on ci/mac12-arm64-rel-tests. The
first failure [1] does include the reverted CL, but the
failures continue after the revert [2].

I suspect [3] is the culprit.

[1] https://ci.chromium.org/ui/p/chromium/builders/ci/mac12-arm64-rel-tests/18253/blamelist
[2] https://ci.chromium.org/ui/p/chromium/builders/ci/mac12-arm64-rel-tests/18262/blamelist
[3] https://chromium-review.googlesource.com/c/chromium/src/+/5224247

Original change's description:
> [Autofill] Simplify ContentAutofillDriver initialization
>
> The DriverInitHooks have two jobs:
> (1) create a {Browser,Android}AutofillManager;
> (2) call some setters on AutofillAgent.
>
> Since the introduction of ContentAutofillClient, the DriverInitHook
> can be conveniently turned into a member function of the client.
>
> This CL introduces *two* new methods in CAC:
> (1) CreateManager(), to be called by CAD's constructor.
> (2) InitAgent(), to be called by the CAD factory.
>
> The point in time they're called is important:
> - CreateManager() is called before OnContentAutofillDriverCreated(),
>   which is necessary for TestAutofillManagerInjector;
> - InitAgent() is called after  OnContentAutofillDriverCreated(),
>   which avoids trouble with double binding mojo remotes when
>   TestAutofillDriverInjector creates a new driver.
>
> Alternatives I considered are:
> - Calling CreateManager() from the factory. This would avoid the
>   potential problem of the manager calling into the driver during
>   the driver's construction. Rejected because this requires
>   CAD::set_autofill_manager(), and since driver and manager
>   have to point to each other, there will always be some kind of
>   chicken-egg problem.
> - Calling InitAgent() from CAD's constructor. Rejected because
>   - tests that mock an AutofillAgent would attempt to bind *two*
>     remotes, which isn't allowed;
>   - unbinding and rebinding remotes
>     - makes writing tests harder;
>     - seems not to work for the renderer -> browser remote.
> - Calling CreateManager() and InitAgent() or only InitAgent() from
>   a virtual CAD::Init() function, which is be called by the CAD
>   factory. Tests would override Init() and only call CreateDriver(),
>   not InitAgent(). Rejected because that'd make writing tests harder.
>
>   The DriverInitHook model worked similarly, but it probably worked
>   out of luck: the DriverInitHooks didn't actually call
>   GetAutofillAgent() in most tests, which happened to avoid the
>   double bind in all relevant tests.
>
> Bug: 1200511
> Change-Id: I339a8b0e7531a76395e5e50dba16f77e3385f56a
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5224630
> Reviewed-by: Jan Keitel <[email protected]>
> Commit-Queue: Christoph Schwering <[email protected]>
> Cr-Commit-Position: refs/heads/main@{#1252040}

Bug: 1200511
Change-Id: Ib923916a0bd31c920ee089569584c1728289731d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5237331
Commit-Queue: Christoph Schwering <[email protected]>
Auto-Submit: Christoph Schwering <[email protected]>
Reviewed-by: Muyao Xu <[email protected]>
Owners-Override: Muyao Xu <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1252350}
diff --git a/chrome/browser/autofill/mock_autofill_agent.cc b/chrome/browser/autofill/mock_autofill_agent.cc
new file mode 100644
index 0000000..afe0878
--- /dev/null
+++ b/chrome/browser/autofill/mock_autofill_agent.cc
@@ -0,0 +1,30 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/autofill/mock_autofill_agent.h"
+
+#include "content/public/browser/render_frame_host.h"
+#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
+
+namespace autofill {
+
+MockAutofillAgent::MockAutofillAgent() = default;
+MockAutofillAgent::~MockAutofillAgent() = default;
+
+void MockAutofillAgent::BindForTesting(content::RenderFrameHost* rfh) {
+  blink::AssociatedInterfaceProvider* remote_interfaces =
+      rfh->GetRemoteAssociatedInterfaces();
+  remote_interfaces->OverrideBinderForTesting(
+      mojom::AutofillAgent::Name_,
+      base::BindRepeating(&MockAutofillAgent::BindPendingReceiver,
+                          weak_ptr_factory_.GetWeakPtr()));
+}
+
+void MockAutofillAgent::BindPendingReceiver(
+    mojo::ScopedInterfaceEndpointHandle handle) {
+  receivers_.Add(this, mojo::PendingAssociatedReceiver<mojom::AutofillAgent>(
+                           std::move(handle)));
+}
+
+}  // namespace autofill