LeOSium_webview/LeOS/patches/Enable-native-Android-autof...

815 lines
40 KiB
Diff
Raw Permalink Normal View History

2023-11-18 11:46:19 +01:00
From: uazo <uazo@users.noreply.github.com>
Date: Sun, 24 Oct 2021 16:54:04 +0000
Subject: Enable native Android autofill
There are 2 different types of autofill: one managed via GCM and the
native Android one that uses the provider assigned by the user
(which can be any user installed app like Bitwarden for example).
In chromium GCM is active while in the WebView the latter.
This patch uses WebView code to enable native Android autofill
along with browser-managed autofill.
A separate toggle is introduced to enable autofill in incognito mode.
See also: https://github.com/bromite/bromite/issues/547
Original License: GPL-2.0-or-later - https://spdx.org/licenses/GPL-2.0-or-later.html
License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html
---
android_webview/browser/aw_contents.cc | 3 +-
chrome/android/BUILD.gn | 1 +
.../settings/PasswordSettings.java | 81 ++++++++++++++++++-
.../chromium/chrome/browser/tab/TabImpl.java | 51 ++++++++++++
.../browser/tab/TabViewAndroidDelegate.java | 14 ++++
chrome/browser/BUILD.gn | 7 ++
chrome/browser/android/tab_android.cc | 15 ++++
chrome/browser/android/tab_android.h | 2 +
.../strings/android_chrome_strings.grd | 6 ++
.../ui/autofill/chrome_autofill_client.cc | 8 +-
.../browser/android_autofill_manager.cc | 2 +-
.../browser/android_autofill_manager.h | 4 +-
.../browser/content_autofill_driver.cc | 53 ++++++++++++
.../content/browser/content_autofill_driver.h | 9 ++-
.../content_autofill_driver_factory.cc | 11 ++-
.../browser/content_autofill_driver_factory.h | 1 +
.../renderer/password_autofill_agent.cc | 5 +-
.../autofill/core/common/autofill_prefs.cc | 6 ++
.../autofill/core/common/autofill_prefs.h | 2 +
.../embedder_support/view/ContentView.java | 46 +++++++++++
.../chromium/ui/base/ViewAndroidDelegate.java | 8 ++
21 files changed, 326 insertions(+), 9 deletions(-)
diff --git a/android_webview/browser/aw_contents.cc b/android_webview/browser/aw_contents.cc
--- a/android_webview/browser/aw_contents.cc
+++ b/android_webview/browser/aw_contents.cc
@@ -216,7 +216,8 @@ AwBrowserPermissionRequestDelegate* AwBrowserPermissionRequestDelegate::FromID(
AwSafeBrowsingUIManager::UIManagerClient*
AwSafeBrowsingUIManager::UIManagerClient::FromWebContents(
WebContents* web_contents) {
- return AwContents::FromWebContents(web_contents);
+ return AwContents::FromWebContents(web_contents,
+ /*enable_secondary_autofill_manager*/ false);
}
// static
diff --git a/chrome/android/BUILD.gn b/chrome/android/BUILD.gn
--- a/chrome/android/BUILD.gn
+++ b/chrome/android/BUILD.gn
@@ -424,6 +424,7 @@ if (current_toolchain == default_toolchain) {
"//chrome/browser/ui/android/hats:message_ui_delegate_java",
"//chrome/browser/ui/android/layouts:java",
"//chrome/browser/ui/android/layouts/glue:java",
+ "//components/android_autofill/browser:java",
"//chrome/browser/ui/android/logo:java",
"//chrome/browser/ui/android/management:java",
"//chrome/browser/ui/android/multiwindow:java",
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/password_manager/settings/PasswordSettings.java b/chrome/android/java/src/org/chromium/chrome/browser/password_manager/settings/PasswordSettings.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/password_manager/settings/PasswordSettings.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/password_manager/settings/PasswordSettings.java
@@ -59,13 +59,18 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
+import org.chromium.chrome.browser.ui.messages.snackbar.SnackbarManager;
+import org.chromium.chrome.browser.ui.messages.snackbar.INeedSnackbarManager;
+import org.chromium.chrome.browser.ui.messages.snackbar.Snackbar;
+import org.chromium.chrome.browser.ApplicationLifetime;
+
/**
* The "Passwords" screen in Settings, which allows the user to enable or disable password saving,
* to view saved passwords (just the username and URL), and to delete saved passwords.
*/
public class PasswordSettings extends ChromeBaseSettingsFragment
implements PasswordListObserver, Preference.OnPreferenceClickListener,
- SyncService.SyncStateChangedListener {
+ SyncService.SyncStateChangedListener, INeedSnackbarManager {
@IntDef({TrustedVaultBannerState.NOT_SHOWN, TrustedVaultBannerState.OFFER_OPT_IN,
TrustedVaultBannerState.OPTED_IN})
@Retention(RetentionPolicy.SOURCE)
@@ -92,6 +97,12 @@ public class PasswordSettings extends ChromeBaseSettingsFragment
public static final String PREF_TRUSTED_VAULT_BANNER = "trusted_vault_banner";
public static final String PREF_KEY_MANAGE_ACCOUNT_LINK = "manage_account_link";
+ public static final String PREF_ANDROID_AUTOFILL_SWITCH = "android_autofill_switch";
+ public static final String PREF_ANDROID_AUTOFILL_INCOGNITO_SWITCH = "android_autofill_incognito_switch";
+
+ private SnackbarManager mSnackbarManager;
+ private Snackbar mSnackbar;
+
private static final String PREF_KEY_CATEGORY_SAVED_PASSWORDS = "saved_passwords";
private static final String PREF_KEY_CATEGORY_EXCEPTIONS = "exceptions";
private static final String PREF_KEY_SAVED_PASSWORDS_NO_TEXT = "saved_passwords_no_text";
@@ -124,6 +135,8 @@ public class PasswordSettings extends ChromeBaseSettingsFragment
private Preference mLinkPref;
private Menu mMenu;
+ private ChromeSwitchPreference mEnableAndroidAutofillSwitch;
+ private ChromeSwitchPreference mEnableAndroidAutofillIncognitoSwitch;
private @ManagePasswordsReferrer int mManagePasswordsReferrer;
private BottomSheetController mBottomSheetController;
@@ -298,6 +311,7 @@ public class PasswordSettings extends ChromeBaseSettingsFragment
}
createSavePasswordsSwitch();
+ createEnableAndroidAutofillSwitch();
if (shouldShowAutoSigninOption()) {
createAutoSignInCheckbox();
}
@@ -561,6 +575,71 @@ public class PasswordSettings extends ChromeBaseSettingsFragment
getPrefService().getBoolean(Pref.CREDENTIALS_ENABLE_SERVICE));
}
+ private void createEnableAndroidAutofillSwitch() {
+ if (mSnackbar == null) {
+ mSnackbar = Snackbar.make(getActivity().getString(R.string.ui_relaunch_notice),
+ new SnackbarManager.SnackbarController() {
+ @Override
+ public void onDismissNoAction(Object actionData) { }
+
+ @Override
+ public void onAction(Object actionData) {
+ ApplicationLifetime.terminate(true);
+ }
+ }, Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_UNKNOWN)
+ .setSingleLine(false)
+ .setAction(getActivity().getString(R.string.relaunch),
+ /*actionData*/null)
+ .setDuration(/*durationMs*/70000);
+ }
+
+ mEnableAndroidAutofillSwitch = new ChromeSwitchPreference(getStyledContext(), null);
+ mEnableAndroidAutofillSwitch.setKey(PREF_ANDROID_AUTOFILL_SWITCH);
+ mEnableAndroidAutofillSwitch.setTitle(R.string.enable_android_autofill);
+ mEnableAndroidAutofillSwitch.setOrder(ORDER_SWITCH);
+ mEnableAndroidAutofillSwitch.setSummaryOn(R.string.text_on);
+ mEnableAndroidAutofillSwitch.setSummaryOff(R.string.text_off);
+
+ try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
+ getPreferenceScreen().addPreference(mEnableAndroidAutofillSwitch);
+ }
+
+ mEnableAndroidAutofillSwitch.setChecked(
+ getPrefService().getBoolean(Pref.AUTOFILL_ANDROID_ENABLED));
+
+ mEnableAndroidAutofillSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
+ getPrefService().setBoolean(Pref.AUTOFILL_ANDROID_ENABLED, (boolean) newValue);
+ if (!mSnackbarManager.isShowing())
+ mSnackbarManager.showSnackbar(mSnackbar);
+ return true;
+ });
+
+ mEnableAndroidAutofillIncognitoSwitch = new ChromeSwitchPreference(getStyledContext(), null);
+ mEnableAndroidAutofillIncognitoSwitch.setKey(PREF_ANDROID_AUTOFILL_INCOGNITO_SWITCH);
+ mEnableAndroidAutofillIncognitoSwitch.setTitle(R.string.enable_android_autofill_incognito);
+ mEnableAndroidAutofillIncognitoSwitch.setOrder(ORDER_SWITCH);
+ mEnableAndroidAutofillIncognitoSwitch.setSummaryOn(R.string.text_on);
+ mEnableAndroidAutofillIncognitoSwitch.setSummaryOff(R.string.text_off);
+
+ try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
+ getPreferenceScreen().addPreference(mEnableAndroidAutofillIncognitoSwitch);
+ }
+
+ mEnableAndroidAutofillIncognitoSwitch.setChecked(
+ getPrefService().getBoolean(Pref.AUTOFILL_ANDROID_INCOGNITO_ENABLED));
+
+ mEnableAndroidAutofillIncognitoSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
+ getPrefService().setBoolean(Pref.AUTOFILL_ANDROID_INCOGNITO_ENABLED, (boolean) newValue);
+ if (!mSnackbarManager.isShowing())
+ mSnackbarManager.showSnackbar(mSnackbar);
+ return true;
+ });
+ }
+
+ public void setSnackbarManager(SnackbarManager manager) {
+ mSnackbarManager = manager;
+ }
+
private void createAutoSignInCheckbox() {
ChromeSwitchPreference autoSignInSwitch =
new ChromeSwitchPreference(getStyledContext(), null);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java
@@ -75,6 +75,17 @@ import org.chromium.url.GURL;
import java.nio.ByteBuffer;
+import android.os.Build;
+import android.util.SparseArray;
+import org.chromium.ui.base.EventOffsetHandler;
+import android.view.ViewStructure;
+import android.view.autofill.AutofillValue;
+import org.chromium.components.autofill.AutofillProvider;
+import org.chromium.components.autofill.AutofillSelectionMenuItemProvider;
+import org.chromium.content_public.browser.SelectionPopupController;
+import org.chromium.chrome.browser.preferences.Pref;
+import org.chromium.components.user_prefs.UserPrefs;
+
/**
* Implementation of the interface {@link Tab}. Contains and manages a {@link ContentView}.
* This class is not intended to be extended.
@@ -236,6 +247,8 @@ public class TabImpl implements Tab {
*/
private @Nullable @TabLaunchType Integer mTabLaunchTypeAtCreation;
+ AutofillProvider mAutofillProvider;
+
/**
* Creates an instance of a {@link TabImpl}.
*
@@ -817,6 +830,11 @@ public class TabImpl implements Tab {
for (TabObserver observer : mObservers) observer.onDestroyed(this);
mObservers.clear();
+ if (mAutofillProvider != null) {
+ mAutofillProvider.destroy();
+ mAutofillProvider = null;
+ }
+
mUserDataHost.destroy();
mTabViewManager.destroy();
hideNativePage(false, null);
@@ -1395,6 +1413,16 @@ public class TabImpl implements Tab {
return mWebContentsState == null ? -1 : mWebContentsState.version();
}
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
+ if (mAutofillProvider != null)
+ mAutofillProvider.onProvideAutoFillVirtualStructure(structure, flags);
+ }
+
+ public void autofill(final SparseArray<AutofillValue> values) {
+ if (mAutofillProvider != null)
+ mAutofillProvider.autofill(values);
+ }
+
/**
* Initializes the {@link WebContents}. Completes the browser content components initialization
* around a native WebContents pointer.
@@ -1444,6 +1472,28 @@ public class TabImpl implements Tab {
mDelegateFactory.createContextMenuPopulatorFactory(this), this));
mWebContents.notifyRendererPreferenceUpdate();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ boolean autofillEnabled = false;
+ if (isIncognito()) {
+ autofillEnabled = UserPrefs.get(Profile.getLastUsedRegularProfile())
+ .getBoolean(Pref.AUTOFILL_ANDROID_INCOGNITO_ENABLED);
+ } else {
+ autofillEnabled = UserPrefs.get(Profile.getLastUsedRegularProfile())
+ .getBoolean(Pref.AUTOFILL_ANDROID_ENABLED);
+ }
+
+ if (autofillEnabled) {
+ SelectionPopupController selectionController =
+ SelectionPopupController.fromWebContents(mWebContents);
+ mAutofillProvider = new AutofillProvider(getContext(), cv, webContents, "bromite");
+ TabImplJni.get().initializeAutofillIfNecessary(mNativeTabAndroid);
+ mAutofillProvider.setWebContents(webContents);
+ cv.setWebContents(webContents);
+ selectionController.setNonSelectionAdditionalMenuItemProvider(
+ new AutofillSelectionMenuItemProvider(
+ getContext(), mAutofillProvider));
+ }
+ }
TabHelpers.initWebContentsHelpers(this);
notifyContentChanged();
} finally {
@@ -1873,6 +1923,7 @@ public class TabImpl implements Tab {
void setActiveNavigationEntryTitleForUrl(long nativeTabAndroid, String url, String title);
void loadOriginalImage(long nativeTabAndroid);
boolean handleNonNavigationAboutURL(GURL url);
+ void initializeAutofillIfNecessary(long nativeTabAndroid);
void onShow(long nativeTabAndroid);
}
}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java
@@ -21,6 +21,10 @@ import org.chromium.ui.dragdrop.DragAndDropBrowserDelegate;
import org.chromium.ui.dragdrop.DragAndDropDelegate;
import org.chromium.ui.dragdrop.DragStateTracker;
+import android.util.SparseArray;
+import android.view.autofill.AutofillValue;
+import android.view.ViewStructure;
+
/**
* Implementation of the abstract class {@link ViewAndroidDelegate} for Chrome.
*/
@@ -86,6 +90,16 @@ public class TabViewAndroidDelegate extends ViewAndroidDelegate {
mTab.onBackgroundColorChanged(color);
}
+ @Override
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
+ mTab.onProvideAutofillVirtualStructure(structure, flags);
+ }
+
+ @Override
+ public void autofill(final SparseArray<AutofillValue> values) {
+ mTab.autofill(values);
+ }
+
@Override
public void onTopControlsChanged(
int topControlsOffsetY, int contentOffsetY, int topControlsMinHeightOffsetY) {
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -2677,6 +2677,13 @@ static_library("browser") {
deps += [ "//chrome/browser/error_reporting" ]
}
+ if (is_android) {
+ deps += [
+ "//components/android_autofill/browser",
+ "//components/android_autofill/browser:android"
+ ]
+ }
+
if (use_ozone) {
deps += [
"//ui/events/ozone",
diff --git a/chrome/browser/android/tab_android.cc b/chrome/browser/android/tab_android.cc
--- a/chrome/browser/android/tab_android.cc
+++ b/chrome/browser/android/tab_android.cc
@@ -52,6 +52,13 @@
#include "url/android/gurl_android.h"
#include "url/gurl.h"
+#include "components/android_autofill/browser/android_autofill_manager.h"
+#include "components/android_autofill/browser/autofill_provider.h"
+#include "components/android_autofill/browser/autofill_provider_android.h"
+#include "components/autofill/content/browser/content_autofill_driver_factory.h"
+#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
+#include "chrome/browser/browser_process.h"
+
using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
@@ -513,3 +520,11 @@ static void JNI_TabImpl_Init(JNIEnv* env,
// This will automatically bind to the Java object and pass ownership there.
new TabAndroid(env, obj, profile, id);
}
+
+void TabAndroid::InitializeAutofillIfNecessary(JNIEnv* env) {
+ if (!autofill::ContentAutofillDriverFactory::FromWebContents(
+ web_contents_.get())) {
+ content::WebContents* web_contents = web_contents_.get();
+ autofill::ChromeAutofillClient::CreateForWebContents(web_contents);
+ }
+}
diff --git a/chrome/browser/android/tab_android.h b/chrome/browser/android/tab_android.h
--- a/chrome/browser/android/tab_android.h
+++ b/chrome/browser/android/tab_android.h
@@ -173,6 +173,8 @@ class TabAndroid : public base::SupportsUserData {
void SetDevToolsAgentHost(scoped_refptr<content::DevToolsAgentHost> host);
+ void InitializeAutofillIfNecessary(JNIEnv* env);
+
// This should never return null, unless it is called in a state where no
// tabs exist (such as on FRE), which should never happen. If it is called
// then, a nullptr will be returned and must be handled accordingly.
diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd
--- a/chrome/browser/ui/android/strings/android_chrome_strings.grd
+++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd
@@ -642,6 +642,12 @@ CHAR_LIMIT guidelines:
<message name="IDS_PASSWORD_SETTINGS_SAVE_PASSWORDS" desc="Title for the checkbox toggling whether passwords are saved or not. [CHAR_LIMIT=32]">
Save passwords
</message>
+ <message name="IDS_ENABLE_ANDROID_AUTOFILL" desc="Title for the checkbox toggling whether enable Android native autofill or not. [CHAR_LIMIT=32]">
+ Enable native Android autofill
+ </message>
+ <message name="IDS_ENABLE_ANDROID_AUTOFILL_INCOGNITO" desc="Title for the checkbox toggling whether enable Android native autofill or not in incognito mode. [CHAR_LIMIT=32]">
+ Enable native Android autofill in incognito
+ </message>
<message name="IDS_PASSWORDS_AUTO_SIGNIN_TITLE" desc="Title for checkbox to enable automatically signing the user in to websites">
Auto Sign-in
</message>
diff --git a/chrome/browser/ui/autofill/chrome_autofill_client.cc b/chrome/browser/ui/autofill/chrome_autofill_client.cc
--- a/chrome/browser/ui/autofill/chrome_autofill_client.cc
+++ b/chrome/browser/ui/autofill/chrome_autofill_client.cc
@@ -1320,7 +1320,13 @@ ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
web_contents,
base::BindRepeating(&BrowserDriverInitHook,
this,
- g_browser_process->GetApplicationLocale())),
+ g_browser_process->GetApplicationLocale(),
+#if BUILDFLAG(IS_ANDROID)
+ /*enable_secondary_autofill_manager*/ true
+#else
+ /*enable_secondary_autofill_manager*/ false
+#endif
+ )),
content::WebContentsObserver(web_contents),
log_manager_(
// TODO(crbug.com/928595): Replace the closure with a callback to the
diff --git a/components/android_autofill/browser/android_autofill_manager.cc b/components/android_autofill/browser/android_autofill_manager.cc
--- a/components/android_autofill/browser/android_autofill_manager.cc
+++ b/components/android_autofill/browser/android_autofill_manager.cc
@@ -25,7 +25,7 @@ using base::TimeTicks;
void AndroidDriverInitHook(AutofillClient* client,
ContentAutofillDriver* driver) {
driver->set_autofill_manager(
- base::WrapUnique(new AndroidAutofillManager(driver, client)));
+ base::WrapUnique(new AndroidAutofillManager(driver, client)), nullptr);
driver->GetAutofillAgent()->SetUserGestureRequired(false);
driver->GetAutofillAgent()->SetSecureContextRequired(true);
driver->GetAutofillAgent()->SetFocusRequiresScroll(false);
diff --git a/components/android_autofill/browser/android_autofill_manager.h b/components/android_autofill/browser/android_autofill_manager.h
--- a/components/android_autofill/browser/android_autofill_manager.h
+++ b/components/android_autofill/browser/android_autofill_manager.h
@@ -95,12 +95,12 @@ class AndroidAutofillManager : public AutofillManager,
const FieldTypeGroup field_type_group,
const url::Origin& triggered_origin);
+ AndroidAutofillManager(AutofillDriver* driver, AutofillClient* client);
+
protected:
friend void AndroidDriverInitHook(AutofillClient* client,
ContentAutofillDriver* driver);
- AndroidAutofillManager(AutofillDriver* driver, AutofillClient* client);
-
void OnFormSubmittedImpl(const FormData& form,
bool known_success,
mojom::SubmissionSource source) override;
diff --git a/components/autofill/content/browser/content_autofill_driver.cc b/components/autofill/content/browser/content_autofill_driver.cc
--- a/components/autofill/content/browser/content_autofill_driver.cc
+++ b/components/autofill/content/browser/content_autofill_driver.cc
@@ -412,6 +412,10 @@ void ContentAutofillDriver::FormsSeen(
const std::vector<FormGlobalId>& removed_forms) {
target->GetAutofillManager().OnFormsSeen(
WithNewVersion(updated_forms), removed_forms);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnFormsSeen(
+ WithNewVersion(updated_forms), removed_forms);
+ }
});
}
@@ -439,6 +443,10 @@ void ContentAutofillDriver::FormSubmitted(
}
target->GetAutofillManager().OnFormSubmitted(
WithNewVersion(form), known_success, submission_source);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnFormSubmitted(
+ WithNewVersion(form), known_success, submission_source);
+ }
});
}
@@ -460,6 +468,10 @@ void ContentAutofillDriver::TextFieldDidChange(const FormData& raw_form,
base::TimeTicks timestamp) {
target->GetAutofillManager().OnTextFieldDidChange(
WithNewVersion(form), field, bounding_box, timestamp);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnTextFieldDidChange(
+ WithNewVersion(form), field, bounding_box, timestamp);
+ }
});
}
@@ -479,6 +491,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->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnTextFieldDidScroll(
+ WithNewVersion(form), field, bounding_box);
+ }
});
}
@@ -499,6 +515,10 @@ void ContentAutofillDriver::SelectControlDidChange(
const FormFieldData& field, const gfx::RectF& bounding_box) {
target->GetAutofillManager().OnSelectControlDidChange(
WithNewVersion(form), field, bounding_box);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnSelectControlDidChange(
+ WithNewVersion(form), field, bounding_box);
+ }
});
}
@@ -521,6 +541,10 @@ void ContentAutofillDriver::AskForValuesToFill(
AutofillSuggestionTriggerSource trigger_source) {
target->GetAutofillManager().OnAskForValuesToFill(
WithNewVersion(form), field, bounding_box, trigger_source);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnAskForValuesToFill(
+ WithNewVersion(form), field, bounding_box, trigger_source);
+ }
});
}
@@ -532,6 +556,8 @@ void ContentAutofillDriver::HidePopup() {
DCHECK(!target->IsPrerendering())
<< "We should never affect UI while prerendering";
target->GetAutofillManager().OnHidePopup();
+ if (target->secondary_autofill_manager_)
+ target->secondary_autofill_manager_->OnHidePopup();
});
}
@@ -543,6 +569,8 @@ 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->secondary_autofill_manager_)
+ target->secondary_autofill_manager_->OnFocusNoLongerOnForm(had_interacted_form);
});
}
@@ -562,9 +590,16 @@ void ContentAutofillDriver::FocusOnFormField(const FormData& raw_form,
const FormFieldData& field, const gfx::RectF& bounding_box) {
target->GetAutofillManager().OnFocusOnFormField(WithNewVersion(form),
field, bounding_box);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnFocusOnFormField(
+ WithNewVersion(form), field, bounding_box);
+ }
},
[](autofill::AutofillDriver* target) {
target->GetAutofillManager().OnFocusNoLongerOnForm(true);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnFocusNoLongerOnForm(true);
+ }
});
}
@@ -579,6 +614,10 @@ void ContentAutofillDriver::DidFillAutofillFormData(const FormData& raw_form,
base::TimeTicks timestamp) {
target->GetAutofillManager().OnDidFillAutofillFormData(
WithNewVersion(form), timestamp);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnDidFillAutofillFormData(
+ WithNewVersion(form), timestamp);
+ }
});
}
@@ -588,6 +627,8 @@ void ContentAutofillDriver::DidEndTextFieldEditing() {
}
router().DidEndTextFieldEditing(this, [](autofill::AutofillDriver* target) {
target->GetAutofillManager().OnDidEndTextFieldEditing();
+ if (target->secondary_autofill_manager_)
+ target->secondary_autofill_manager_->OnDidEndTextFieldEditing();
});
}
@@ -602,6 +643,10 @@ void ContentAutofillDriver::SelectOrSelectListFieldOptionsDidChange(
cast(target)
->GetAutofillManager()
.OnSelectOrSelectListFieldOptionsDidChange(WithNewVersion(form));
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnSelectOrSelectMenuFieldOptionsDidChange(
+ WithNewVersion(form));
+ }
});
}
@@ -621,6 +666,10 @@ void ContentAutofillDriver::JavaScriptChangedAutofilledValue(
const FormFieldData& field, const std::u16string& old_value) {
target->GetAutofillManager().OnJavaScriptChangedAutofilledValue(
WithNewVersion(form), field, old_value);
+ if (target->secondary_autofill_manager_) {
+ target->secondary_autofill_manager_->OnJavaScriptChangedAutofilledValue(
+ WithNewVersion(form), field, old_value);
+ }
});
}
@@ -633,6 +682,8 @@ void ContentAutofillDriver::OnContextMenuShownInField(
const FieldGlobalId& field_global_id) {
target->GetAutofillManager().OnContextMenuShownInField(form_global_id,
field_global_id);
+ if (target->secondary_autofill_manager_)
+ target->secondary_autofill_manager_->OnContextMenuShownInField(form_global_id, field_global_id);
});
}
@@ -643,6 +694,8 @@ 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
--- a/components/autofill/content/browser/content_autofill_driver.h
+++ b/components/autofill/content/browser/content_autofill_driver.h
@@ -127,9 +127,12 @@ class ContentAutofillDriver : public AutofillDriver,
ContentAutofillDriver& operator=(const ContentAutofillDriver&) = delete;
~ContentAutofillDriver() override;
- void set_autofill_manager(std::unique_ptr<AutofillManager> autofill_manager) {
+ void set_autofill_manager(std::unique_ptr<AutofillManager> autofill_manager,
+ std::unique_ptr<AutofillManager> secondary_autofill_manager) {
autofill_manager_ = std::move(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_; }
const content::RenderFrameHost* render_frame_host() const {
@@ -319,6 +322,10 @@ class ContentAutofillDriver : public AutofillDriver,
std::unique_ptr<AutofillManager> autofill_manager_ = nullptr;
+ // adds a reference for AndroidAutofillManager, since native autofill works in
+ // conjunction with browser autofill in Bromite
+ 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/content/browser/content_autofill_driver_factory.cc b/components/autofill/content/browser/content_autofill_driver_factory.cc
--- a/components/autofill/content/browser/content_autofill_driver_factory.cc
+++ b/components/autofill/content/browser/content_autofill_driver_factory.cc
@@ -13,6 +13,7 @@
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/browser_autofill_manager.h"
#include "components/autofill/core/common/autofill_features.h"
+#include "components/android_autofill/browser/android_autofill_manager.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
@@ -44,9 +45,17 @@ bool ShouldEnableHeavyFormDataScraping(const version_info::Channel channel) {
void BrowserDriverInitHook(AutofillClient* client,
const std::string& app_locale,
+ bool enable_secondary_autofill_manager,
ContentAutofillDriver* driver) {
+#if BUILDFLAG(IS_ANDROID)
driver->set_autofill_manager(
- std::make_unique<BrowserAutofillManager>(driver, client, app_locale));
+ std::make_unique<BrowserAutofillManager>(driver, client, app_locale),
+ enable_secondary_autofill_manager == false ? nullptr :
+ base::WrapUnique(new AndroidAutofillManager(driver, client)));
+#else
+ driver->set_autofill_manager(std::make_unique<BrowserAutofillManager>(
+ driver, client, app_locale), nullptr);
+#endif
if (client && ShouldEnableHeavyFormDataScraping(client->GetChannel()))
driver->GetAutofillAgent()->EnableHeavyFormDataScraping();
}
diff --git a/components/autofill/content/browser/content_autofill_driver_factory.h b/components/autofill/content/browser/content_autofill_driver_factory.h
--- a/components/autofill/content/browser/content_autofill_driver_factory.h
+++ b/components/autofill/content/browser/content_autofill_driver_factory.h
@@ -36,6 +36,7 @@ class ScopedAutofillManagersObservation;
// other implementations.
void BrowserDriverInitHook(AutofillClient* client,
const std::string& app_locale,
+ bool enable_secondary_autofill_manager,
ContentAutofillDriver* driver);
// Manages lifetime of ContentAutofillDriver. Owned by ContentAutofillClient,
diff --git a/components/autofill/content/renderer/password_autofill_agent.cc b/components/autofill/content/renderer/password_autofill_agent.cc
--- a/components/autofill/content/renderer/password_autofill_agent.cc
+++ b/components/autofill/content/renderer/password_autofill_agent.cc
@@ -838,7 +838,10 @@ void PasswordAutofillAgent::UpdateStateForTextChange(
void PasswordAutofillAgent::TrackAutofilledElement(
const blink::WebFormControlElement& element) {
- autofill_agent_->TrackAutofilledElement(element);
+ // fix for https://github.com/bromite/bromite/issues/1570
+ AutofillAgent* agent = autofill_agent_.get();
+ if (agent)
+ agent->TrackAutofilledElement(element);
}
void PasswordAutofillAgent::FillPasswordSuggestion(
diff --git a/components/autofill/core/common/autofill_prefs.cc b/components/autofill/core/common/autofill_prefs.cc
--- a/components/autofill/core/common/autofill_prefs.cc
+++ b/components/autofill/core/common/autofill_prefs.cc
@@ -141,6 +141,10 @@ const char kAutofillUsingVirtualViewStructure[] =
"autofill.using_virtual_view_structure";
#endif // BUILDFLAG(IS_ANDROID)
+// Boolean that is true to enable native Android Autofill
+const char kAutofillAndroidEnabled[] = "autofill.android_autofill_enabled";
+const char kAutofillAndroidIncognitoEnabled[] = "autofill.android_autofill_incognito_enabled";
+
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
// Synced prefs. Used for cross-device choices, e.g., credit card Autofill.
registry->RegisterBooleanPref(
@@ -169,6 +173,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kAutofillCreditCardFidoAuthOfferCheckboxState, true);
#endif
+ registry->RegisterBooleanPref(prefs::kAutofillAndroidEnabled, true);
+ registry->RegisterBooleanPref(prefs::kAutofillAndroidIncognitoEnabled, false);
registry->RegisterIntegerPref(
prefs::kAutofillLastVersionDisusedCreditCardsDeleted, 0);
registry->RegisterIntegerPref(prefs::kAutocompleteLastVersionRetentionPolicy,
diff --git a/components/autofill/core/common/autofill_prefs.h b/components/autofill/core/common/autofill_prefs.h
--- a/components/autofill/core/common/autofill_prefs.h
+++ b/components/autofill/core/common/autofill_prefs.h
@@ -42,6 +42,8 @@ extern const char kAutofillUploadEncodingSeed[];
extern const char kAutofillUploadEvents[];
extern const char kAutofillUploadEventsLastResetTimestamp[];
extern const char kAutocompleteLastVersionRetentionPolicy[];
+extern const char kAutofillAndroidEnabled[];
+extern const char kAutofillAndroidIncognitoEnabled[];
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID) || \
BUILDFLAG(IS_IOS)
extern const char kAutofillPaymentMethodsMandatoryReauth[];
diff --git a/components/embedder_support/android/java/src/org/chromium/components/embedder_support/view/ContentView.java b/components/embedder_support/android/java/src/org/chromium/components/embedder_support/view/ContentView.java
--- a/components/embedder_support/android/java/src/org/chromium/components/embedder_support/view/ContentView.java
+++ b/components/embedder_support/android/java/src/org/chromium/components/embedder_support/view/ContentView.java
@@ -41,6 +41,11 @@ import org.chromium.ui.dragdrop.DragEventDispatchHelper.DragEventDispatchDestina
import java.util.function.Supplier;
+import org.chromium.base.Log;
+import android.util.SparseArray;
+import android.view.autofill.AutofillValue;
+import org.chromium.ui.base.ViewAndroidDelegate;
+
/**
* The containing view for {@link WebContents} that exists in the Android UI hierarchy and exposes
* the various {@link View} functionality to it.
@@ -92,6 +97,8 @@ public class ContentView extends FrameLayout
*/
public static ContentView createContentView(Context context,
@Nullable EventOffsetHandler eventOffsetHandler, @Nullable WebContents webContents) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+ return new ContentViewWithAutofill(context, eventOffsetHandler, webContents);
return new ContentView(context, eventOffsetHandler, webContents);
}
@@ -638,4 +645,43 @@ public class ContentView extends FrameLayout
mDragDropEventOffsetHandler.onPostDispatchDragEvent(event.getAction());
return ret;
}
+
+ /**
+ * API level 26 implementation that includes autofill.
+ */
+ public static class ContentViewWithAutofill extends ContentView {
+ private ViewAndroidDelegate viewAndroidDelegate;
+
+ private ContentViewWithAutofill(Context context, EventOffsetHandler eventOffsetHandler, WebContents webContents) {
+ super(context, eventOffsetHandler, webContents);
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ // The Autofill system-level infrastructure has heuristics for which Views it considers
+ // important for autofill; only these Views will be queried for their autofill
+ // structure on notifications that a new (virtual) View was entered. By default,
+ // FrameLayout is not considered important for autofill. Thus, for ContentView to be
+ // queried for its autofill structure, we must explicitly inform the autofill system
+ // that this View is important for autofill.
+ setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_YES);
+ }
+ }
+
+ @Override
+ public void setWebContents(WebContents webContents) {
+ viewAndroidDelegate = webContents.getViewAndroidDelegate();
+ super.setWebContents(webContents);
+ }
+
+ @Override
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
+ if (viewAndroidDelegate != null)
+ viewAndroidDelegate.onProvideAutofillVirtualStructure(structure, flags);
+ }
+
+ @Override
+ public void autofill(final SparseArray<AutofillValue> values) {
+ if (viewAndroidDelegate != null)
+ viewAndroidDelegate.autofill(values);
+ }
+ }
}
diff --git a/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java b/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java
--- a/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java
+++ b/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java
@@ -31,6 +31,10 @@ import org.chromium.ui.dragdrop.DragStateTracker;
import org.chromium.ui.dragdrop.DropDataAndroid;
import org.chromium.ui.mojom.CursorType;
+import android.util.SparseArray;
+import android.view.autofill.AutofillValue;
+import android.view.ViewStructure;
+
/**
* Class to acquire, position, and remove anchor views from the implementing View.
*/
@@ -578,4 +582,8 @@ public class ViewAndroidDelegate {
sDragAndDropDelegateForTesting = testDelegate;
ResettersForTesting.register(() -> sDragAndDropDelegateForTesting = null);
}
+
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {}
+
+ public void autofill(final SparseArray<AutofillValue> values) {}
}
--
2.25.1