blob: 2ea0d6f81b59d39f64ff40a3ab32cb7a96030cdd [file] [log] [blame]
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: fgei <fgei@gmail.com>
Date: Tue, 21 Feb 2023 01:48:15 +0000
Subject: [PATCH] Support for both browser and android autofill functionality
---
.../browser/android_autofill_manager.cc | 15 ++++++
.../browser/android_autofill_manager.h | 15 ++++++
.../browser/content_autofill_driver.cc | 52 +++++++++++++++++++
.../content/browser/content_autofill_driver.h | 8 +++
.../autofill/core/browser/autofill_driver.h | 6 +++
5 files changed, 96 insertions(+)
diff --git a/components/android_autofill/browser/android_autofill_manager.cc b/components/android_autofill/browser/android_autofill_manager.cc
index ed7f0f073b734..df84f20ca4ffa 100644
--- a/components/android_autofill/browser/android_autofill_manager.cc
+++ b/components/android_autofill/browser/android_autofill_manager.cc
@@ -14,6 +14,7 @@
#include "components/android_autofill/browser/autofill_provider.h"
#include "components/android_autofill/browser/form_event_logger_weblayer_android.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
+#include "components/autofill/core/browser/browser_autofill_manager.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
@@ -32,6 +33,20 @@ void AndroidDriverInitHook(AutofillClient* client,
driver->GetAutofillAgent()->SetQueryPasswordSuggestion(true);
}
+void AndroidAndBrowserDriverInitHook(
+ AutofillClient* client,
+ const std::string& app_locale,
+ ContentAutofillDriver* driver) {
+ driver->set_autofill_manager(std::make_unique<BrowserAutofillManager>(
+ driver, client, app_locale));
+ driver->set_secondary_autofill_manager(base::WrapUnique(
+ new AndroidAutofillManager(driver, client)));
+ driver->GetAutofillAgent()->SetUserGestureRequired(false);
+ driver->GetAutofillAgent()->SetSecureContextRequired(true);
+ driver->GetAutofillAgent()->SetFocusRequiresScroll(false);
+ driver->GetAutofillAgent()->SetQueryPasswordSuggestion(true);
+}
+
AndroidAutofillManager::AndroidAutofillManager(AutofillDriver* driver,
AutofillClient* client)
: AutofillManager(driver, client) {
diff --git a/components/android_autofill/browser/android_autofill_manager.h b/components/android_autofill/browser/android_autofill_manager.h
index 3fd9a9c2cd46e..e610c101c52e0 100644
--- a/components/android_autofill/browser/android_autofill_manager.h
+++ b/components/android_autofill/browser/android_autofill_manager.h
@@ -21,6 +21,16 @@ class AutofillProvider;
class ContentAutofillDriver;
class FormEventLoggerWeblayerAndroid;
+// Creates an AndroidAutofillManager and attaches it to the `driver`.
+//
+// This hook is to be passed to CreateForWebContentsAndDelegate().
+// It is the glue between ContentAutofillDriver[Factory] and
+// AndroidAutofillManager, BrowserAutofillManager.
+void AndroidAndBrowserDriverInitHook(
+ AutofillClient* client,
+ const std::string& app_locale,
+ ContentAutofillDriver* driver);
+
// Creates an AndroidAutofillManager and attaches it to the `driver`.
//
// This hook is to be passed to CreateForWebContentsAndDelegate().
@@ -96,6 +106,11 @@ class AndroidAutofillManager : public AutofillManager,
const url::Origin& triggered_origin);
protected:
+ friend void AndroidAndBrowserDriverInitHook(
+ AutofillClient* client,
+ const std::string& app_locale,
+ ContentAutofillDriver* driver);
+
friend void AndroidDriverInitHook(AutofillClient* client,
ContentAutofillDriver* driver);
diff --git a/components/autofill/content/browser/content_autofill_driver.cc b/components/autofill/content/browser/content_autofill_driver.cc
index 83625554d4fd7..b66f3f3cb1f7f 100644
--- a/components/autofill/content/browser/content_autofill_driver.cc
+++ b/components/autofill/content/browser/content_autofill_driver.cc
@@ -145,6 +145,18 @@ AutofillManager& ContentAutofillDriver::GetAutofillManager() {
return *autofill_manager_;
}
+AutofillManager& ContentAutofillDriver::GetSecondaryAutofillManager() {
+ return *secondary_autofill_manager_;
+}
+
+bool ContentAutofillDriver::HasSecondaryAutofillManager() {
+ if (secondary_autofill_manager_) {
+ return true;
+ }
+
+ return false;
+}
+
absl::optional<LocalFrameToken> ContentAutofillDriver::Resolve(
FrameToken query) {
if (absl::holds_alternative<LocalFrameToken>(query)) {
@@ -412,6 +424,10 @@ void ContentAutofillDriver::FormsSeen(
const std::vector<FormGlobalId>& removed_forms) {
target->GetAutofillManager().OnFormsSeen(
WithNewVersion(updated_forms), removed_forms);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnFormsSeen(
+ WithNewVersion(updated_forms), removed_forms);
+ }
});
}
@@ -439,6 +455,10 @@ void ContentAutofillDriver::FormSubmitted(
}
target->GetAutofillManager().OnFormSubmitted(
WithNewVersion(form), known_success, submission_source);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnFormSubmitted(
+ WithNewVersion(form), known_success, submission_source);
+ }
});
}
@@ -460,6 +480,10 @@ void ContentAutofillDriver::TextFieldDidChange(const FormData& raw_form,
base::TimeTicks timestamp) {
target->GetAutofillManager().OnTextFieldDidChange(
WithNewVersion(form), field, bounding_box, timestamp);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnTextFieldDidChange(
+ WithNewVersion(form), field, bounding_box, timestamp);
+ }
});
}
@@ -479,6 +503,10 @@ void ContentAutofillDriver::TextFieldDidScroll(const FormData& raw_form,
const FormFieldData& field, const gfx::RectF& bounding_box) {
target->GetAutofillManager().OnTextFieldDidScroll(WithNewVersion(form),
field, bounding_box);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnTextFieldDidScroll(WithNewVersion(form),
+ field, bounding_box);
+ }
});
}
@@ -499,6 +527,10 @@ void ContentAutofillDriver::SelectControlDidChange(
const FormFieldData& field, const gfx::RectF& bounding_box) {
target->GetAutofillManager().OnSelectControlDidChange(
WithNewVersion(form), field, bounding_box);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnSelectControlDidChange(
+ WithNewVersion(form), field, bounding_box);
+ }
});
}
@@ -521,6 +553,10 @@ void ContentAutofillDriver::AskForValuesToFill(
AutofillSuggestionTriggerSource trigger_source) {
target->GetAutofillManager().OnAskForValuesToFill(
WithNewVersion(form), field, bounding_box, trigger_source);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnAskForValuesToFill(
+ WithNewVersion(form), field, bounding_box, trigger_source);
+ }
});
}
@@ -532,6 +568,9 @@ void ContentAutofillDriver::HidePopup() {
DCHECK(!target->IsPrerendering())
<< "We should never affect UI while prerendering";
target->GetAutofillManager().OnHidePopup();
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnHidePopup();
+ }
});
}
@@ -543,6 +582,9 @@ void ContentAutofillDriver::FocusNoLongerOnForm(bool had_interacted_form) {
this, had_interacted_form,
[](autofill::AutofillDriver* target, bool had_interacted_form) {
target->GetAutofillManager().OnFocusNoLongerOnForm(had_interacted_form);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnFocusNoLongerOnForm(had_interacted_form);
+ }
});
}
@@ -565,6 +607,9 @@ void ContentAutofillDriver::FocusOnFormField(const FormData& raw_form,
},
[](autofill::AutofillDriver* target) {
target->GetAutofillManager().OnFocusNoLongerOnForm(true);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnFocusNoLongerOnForm(true);
+ }
});
}
@@ -579,6 +624,10 @@ void ContentAutofillDriver::DidFillAutofillFormData(const FormData& raw_form,
base::TimeTicks timestamp) {
target->GetAutofillManager().OnDidFillAutofillFormData(
WithNewVersion(form), timestamp);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnDidFillAutofillFormData(
+ WithNewVersion(form), timestamp);
+ }
});
}
@@ -643,6 +692,9 @@ void ContentAutofillDriver::Reset() {
owner_->router().UnregisterDriver(this,
/*driver_is_dying=*/false);
autofill_manager_->Reset();
+ if (secondary_autofill_manager_) {
+ secondary_autofill_manager_->Reset();
+ }
}
const mojo::AssociatedRemote<mojom::AutofillAgent>&
diff --git a/components/autofill/content/browser/content_autofill_driver.h b/components/autofill/content/browser/content_autofill_driver.h
index 39c13f8e77500..ac7d4dba61a09 100644
--- a/components/autofill/content/browser/content_autofill_driver.h
+++ b/components/autofill/content/browser/content_autofill_driver.h
@@ -131,6 +131,10 @@ class ContentAutofillDriver : public AutofillDriver,
autofill_manager_ = std::move(autofill_manager);
}
+ void set_secondary_autofill_manager(std::unique_ptr<AutofillManager> secondary_autofill_manager) {
+ secondary_autofill_manager_ = std::move(secondary_autofill_manager);
+ }
+
content::RenderFrameHost* render_frame_host() { return &*render_frame_host_; }
const content::RenderFrameHost* render_frame_host() const {
return &*render_frame_host_;
@@ -159,6 +163,8 @@ class ContentAutofillDriver : public AutofillDriver,
absl::optional<LocalFrameToken> Resolve(FrameToken query) override;
ContentAutofillDriver* GetParent() override;
AutofillManager& GetAutofillManager() override;
+ AutofillManager& GetSecondaryAutofillManager() override;
+ bool HasSecondaryAutofillManager() override;
bool IsInActiveFrame() const override;
bool IsInAnyMainFrame() const override;
bool IsPrerendering() const override;
@@ -319,6 +325,8 @@ class ContentAutofillDriver : public AutofillDriver,
std::unique_ptr<AutofillManager> autofill_manager_ = nullptr;
+ std::unique_ptr<AutofillManager> secondary_autofill_manager_ = nullptr;
+
mojo::AssociatedReceiver<mojom::AutofillDriver> receiver_{this};
mojo::AssociatedRemote<mojom::AutofillAgent> autofill_agent_;
diff --git a/components/autofill/core/browser/autofill_driver.h b/components/autofill/core/browser/autofill_driver.h
index 9426b8cd4106f..d80e4a55b022a 100644
--- a/components/autofill/core/browser/autofill_driver.h
+++ b/components/autofill/core/browser/autofill_driver.h
@@ -69,6 +69,12 @@ class AutofillDriver {
// Returns the AutofillManager owned by the AutofillDriver.
virtual AutofillManager& GetAutofillManager() = 0;
+ // Returns the secondary AutofillManager owned by the AutofillDriver.
+ virtual AutofillManager& GetSecondaryAutofillManager() = 0;
+
+ // Returns if there is a secondary AutofillManager owned by the AutofillDriver.
+ virtual bool HasSecondaryAutofillManager() = 0;
+
// Returns whether the AutofillDriver instance is associated with an active
// frame in the MPArch sense.
virtual bool IsInActiveFrame() const = 0;