blob: 5b19e1fa086bac590cc34f79989542fce09380e0 [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 0d51b11b639b6..de3e78c4eaec8 100644
--- a/components/android_autofill/browser/android_autofill_manager.cc
+++ b/components/android_autofill/browser/android_autofill_manager.cc
@@ -15,6 +15,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"
@@ -33,6 +34,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 9ef74e9a284a6..2972c6b08a156 100644
--- a/components/android_autofill/browser/android_autofill_manager.h
+++ b/components/android_autofill/browser/android_autofill_manager.h
@@ -23,6 +23,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().
@@ -92,6 +102,11 @@ class AndroidAutofillManager : public AutofillManager,
PopupItemId popup_item_id) override;
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 98437f9b05252..83d44f5932068 100644
--- a/components/autofill/content/browser/content_autofill_driver.cc
+++ b/components/autofill/content/browser/content_autofill_driver.cc
@@ -157,6 +157,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)) {
@@ -455,6 +467,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);
+ }
});
}
@@ -482,6 +498,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);
+ }
});
}
@@ -503,6 +523,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);
+ }
});
}
@@ -522,6 +546,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);
+ }
});
}
@@ -542,6 +570,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);
+ }
});
}
@@ -564,6 +596,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);
+ }
});
}
@@ -575,6 +611,9 @@ void ContentAutofillDriver::HidePopup() {
DCHECK(!target->IsPrerendering())
<< "We should never affect UI while prerendering";
target->GetAutofillManager().OnHidePopup();
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnHidePopup();
+ }
});
}
@@ -586,6 +625,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);
+ }
});
}
@@ -608,6 +650,9 @@ void ContentAutofillDriver::FocusOnFormField(const FormData& raw_form,
},
[](autofill::AutofillDriver* target) {
target->GetAutofillManager().OnFocusNoLongerOnForm(true);
+ if (target->HasSecondaryAutofillManager()) {
+ target->GetSecondaryAutofillManager().OnFocusNoLongerOnForm(true);
+ }
});
}
@@ -622,6 +667,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);
+ }
});
}
@@ -686,6 +735,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 2bf1c751de4eb..1851045fdf3ea 100644
--- a/components/autofill/content/browser/content_autofill_driver.h
+++ b/components/autofill/content/browser/content_autofill_driver.h
@@ -132,6 +132,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_;
@@ -160,6 +164,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;
@@ -351,6 +357,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 ed61f8b928a1e..c5a2e80443223 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;