From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: fgei 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 | 42 +++++++++++++++++++ .../content/browser/content_autofill_driver.h | 12 ++++++ 4 files changed, 84 insertions(+) diff --git a/components/android_autofill/browser/android_autofill_manager.cc b/components/android_autofill/browser/android_autofill_manager.cc index 60b056e7e22d9..4101639a5649e 100644 --- a/components/android_autofill/browser/android_autofill_manager.cc +++ b/components/android_autofill/browser/android_autofill_manager.cc @@ -9,6 +9,7 @@ #include "components/android_autofill/browser/autofill_provider.h" #include "components/autofill/content/browser/content_autofill_driver.h" #include "components/autofill/core/browser/metrics/form_events/form_event_logger_weblayer_android.h" +#include "components/autofill/core/browser/browser_autofill_manager.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" @@ -26,6 +27,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( + 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 179b2b47464e0..dd8af3166841a 100644 --- a/components/android_autofill/browser/android_autofill_manager.h +++ b/components/android_autofill/browser/android_autofill_manager.h @@ -16,6 +16,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(). @@ -86,6 +96,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 00b0f300a0748..0f9287246a098 100644 --- a/components/autofill/content/browser/content_autofill_driver.cc +++ b/components/autofill/content/browser/content_autofill_driver.cc @@ -336,6 +336,10 @@ void ContentAutofillDriver::FormsSeen( const std::vector& removed_forms) { target->autofill_manager_->OnFormsSeen(WithNewVersion(updated_forms), removed_forms); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnFormsSeen(WithNewVersion(updated_forms), + removed_forms); + } }); } @@ -362,6 +366,10 @@ void ContentAutofillDriver::FormSubmitted( } target->autofill_manager_->OnFormSubmitted( WithNewVersion(form), known_success, submission_source); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnFormSubmitted( + WithNewVersion(form), known_success, submission_source); + } }); } @@ -382,6 +390,10 @@ void ContentAutofillDriver::TextFieldDidChange(const FormData& raw_form, base::TimeTicks timestamp) { target->autofill_manager_->OnTextFieldDidChange( WithNewVersion(form), field, bounding_box, timestamp); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnTextFieldDidChange( + WithNewVersion(form), field, bounding_box, timestamp); + } }); } @@ -400,6 +412,10 @@ void ContentAutofillDriver::TextFieldDidScroll(const FormData& raw_form, const FormFieldData& field, const gfx::RectF& bounding_box) { target->autofill_manager_->OnTextFieldDidScroll(WithNewVersion(form), field, bounding_box); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnTextFieldDidScroll(WithNewVersion(form), + field, bounding_box); + } }); } @@ -419,6 +435,10 @@ void ContentAutofillDriver::SelectControlDidChange( const FormFieldData& field, const gfx::RectF& bounding_box) { target->autofill_manager_->OnSelectControlDidChange( WithNewVersion(form), field, bounding_box); + if (target->secondary_autofill_manager_) { + target->autofill_manager_->OnSelectControlDidChange( + WithNewVersion(form), field, bounding_box); + } }); } @@ -444,6 +464,11 @@ void ContentAutofillDriver::AskForValuesToFill( target->autofill_manager_->OnAskForValuesToFill( WithNewVersion(form), field, bounding_box, autoselect_first_suggestion, form_element_was_clicked); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnAskForValuesToFill( + WithNewVersion(form), field, bounding_box, + autoselect_first_suggestion, form_element_was_clicked); + } }); } @@ -454,12 +479,18 @@ void ContentAutofillDriver::HidePopup() { DCHECK(!target->IsPrerendering()) << "We should never affect UI while prerendering"; target->autofill_manager_->OnHidePopup(); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnHidePopup(); + } }); } void ContentAutofillDriver::FocusNoLongerOnFormCallback( bool had_interacted_form) { autofill_manager_->OnFocusNoLongerOnForm(had_interacted_form); + if (secondary_autofill_manager_) { + secondary_autofill_manager_->OnFocusNoLongerOnForm(had_interacted_form); + } } void ContentAutofillDriver::FocusNoLongerOnForm(bool had_interacted_form) { @@ -487,6 +518,10 @@ void ContentAutofillDriver::FocusOnFormField(const FormData& raw_form, const FormFieldData& field, const gfx::RectF& bounding_box) { target->autofill_manager_->OnFocusOnFormField(WithNewVersion(form), field, bounding_box); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnFocusOnFormField(WithNewVersion(form), + field, bounding_box); + } }); } @@ -500,6 +535,10 @@ void ContentAutofillDriver::DidFillAutofillFormData(const FormData& raw_form, base::TimeTicks timestamp) { target->autofill_manager_->OnDidFillAutofillFormData( WithNewVersion(form), timestamp); + if (target->secondary_autofill_manager_) { + target->secondary_autofill_manager_->OnDidFillAutofillFormData( + WithNewVersion(form), timestamp); + } }); } @@ -575,6 +614,9 @@ void ContentAutofillDriver::Reset() { submitted_forms_.clear(); autofill_router_->UnregisterDriver(this); autofill_manager_->Reset(); + if (secondary_autofill_manager_) { + secondary_autofill_manager_->Reset(); + } } const mojo::AssociatedRemote& diff --git a/components/autofill/content/browser/content_autofill_driver.h b/components/autofill/content/browser/content_autofill_driver.h index 65d6e43b5e017..bfae54ada1006 100644 --- a/components/autofill/content/browser/content_autofill_driver.h +++ b/components/autofill/content/browser/content_autofill_driver.h @@ -130,6 +130,14 @@ class ContentAutofillDriver : public AutofillDriver, } AutofillManager* autofill_manager() { return autofill_manager_.get(); } + void set_secondary_autofill_manager( + std::unique_ptr secondary_autofill_manager) { + secondary_autofill_manager_ = std::move(secondary_autofill_manager); + } + AutofillManager* secondary_autofill_manager() { + return secondary_autofill_manager_.get(); + } + content::RenderFrameHost* render_frame_host() { return render_frame_host_; } // Expose the events that originate from the browser and renderer processes, @@ -343,6 +351,10 @@ class ContentAutofillDriver : public AutofillDriver, // code. std::unique_ptr autofill_manager_ = nullptr; + // adds a reference for AndroidAutofillManager, since native autofill works in + // conjunction with browser autofill + std::unique_ptr secondary_autofill_manager_ = nullptr; + content::RenderWidgetHost::KeyPressEventCallback key_press_handler_; mojo::AssociatedReceiver receiver_{this};