LeOSium_old/patches/0001-Vanadium/0101-Support-native-Android...

380 lines
17 KiB
Diff
Raw Permalink Normal View History

2023-11-18 11:23:04 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: fgei <fgei@gmail.com>
Date: Mon, 20 Feb 2023 07:10:55 +0000
Subject: [PATCH] Support native Android autofill at browser
This enables support for Android Autofil on tabs showing fillable
entries, reusing the codebase used for webview's android autofill
support.
---
android_webview/browser/aw_autofill_client.cc | 4 ++
chrome/android/BUILD.gn | 1 +
.../chromium/chrome/browser/tab/TabImpl.java | 45 +++++++++++++++++
.../browser/tab/TabViewAndroidDelegate.java | 13 +++++
chrome/browser/BUILD.gn | 7 +++
.../ui/autofill/chrome_autofill_client.cc | 14 +++++-
.../embedder_support/view/ContentView.java | 48 +++++++++++++++++++
.../chromium/ui/base/ViewAndroidDelegate.java | 8 ++++
8 files changed, 139 insertions(+), 1 deletion(-)
diff --git a/android_webview/browser/aw_autofill_client.cc b/android_webview/browser/aw_autofill_client.cc
index 7dd21f8202f9f..889bf90b7821e 100644
--- a/android_webview/browser/aw_autofill_client.cc
+++ b/android_webview/browser/aw_autofill_client.cc
@@ -83,6 +83,7 @@ AwAutofillClient::GetURLLoaderFactory() {
}
autofill::AutofillDownloadManager* AwAutofillClient::GetDownloadManager() {
+#if defined(USE_BROWSER_AUTOFILL_ONLY)
if (autofill::AutofillProvider::is_download_manager_disabled_for_testing()) {
return nullptr;
}
@@ -92,6 +93,9 @@ autofill::AutofillDownloadManager* AwAutofillClient::GetDownloadManager() {
this, GetChannel(), GetLogManager());
}
return download_manager_.get();
+#else
+ return nullptr;
+#endif // defined(USE_BROWSER_AUTOFILL_ONLY)
}
autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() {
diff --git a/chrome/android/BUILD.gn b/chrome/android/BUILD.gn
index 4a4695c3d0119..47956c1f09e07 100644
--- a/chrome/android/BUILD.gn
+++ b/chrome/android/BUILD.gn
@@ -351,6 +351,7 @@ if (current_toolchain == default_toolchain) {
"//chrome/browser/webapps/android:java",
"//chrome/browser/webauthn/android:java",
"//chrome/browser/xsurface:java",
+ "//components/android_autofill/browser:java",
"//components/autofill/android:autofill_java",
"//components/autofill/android:prefeditor_autofill_java",
"//components/background_task_scheduler:background_task_scheduler_java",
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
index 84dee29291b42..baf0859ca7cf2 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabImpl.java
@@ -10,10 +10,14 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
+import android.os.Build;
import android.text.TextUtils;
+import android.util.SparseArray;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
+import android.view.ViewStructure;
import android.view.accessibility.AccessibilityEvent;
+import android.view.autofill.AutofillValue;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@@ -54,6 +58,8 @@ import org.chromium.chrome.browser.tab.state.CriticalPersistedTabData;
import org.chromium.chrome.browser.tab.state.SerializedCriticalPersistedTabData;
import org.chromium.chrome.browser.ui.native_page.FrozenNativePage;
import org.chromium.chrome.browser.ui.native_page.NativePage;
+import org.chromium.components.autofill.AutofillActionModeCallback;
+import org.chromium.components.autofill.AutofillProvider;
import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.embedder_support.view.ContentView;
@@ -66,9 +72,11 @@ import org.chromium.components.version_info.VersionInfo;
import org.chromium.content_public.browser.ChildProcessImportance;
import org.chromium.content_public.browser.ContentFeatureList;
import org.chromium.content_public.browser.LoadUrlParams;
+import org.chromium.content_public.browser.SelectionPopupController;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsAccessibility;
import org.chromium.content_public.browser.navigation_controller.UserAgentOverrideOption;
+import org.chromium.ui.base.EventOffsetHandler;
import org.chromium.ui.base.PageTransition;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid;
@@ -212,6 +220,7 @@ public class TabImpl implements Tab {
private int mThemeColor;
private boolean mUsedCriticalPersistedTabData;
private boolean mIsWebContentObscured;
+ AutofillProvider mAutofillProvider;
/**
* Creates an instance of a {@link TabImpl}.
@@ -255,12 +264,18 @@ public class TabImpl implements Tab {
public void onViewAttachedToWindow(View view) {
mIsViewAttachedToWindow = true;
updateInteractableState();
+ if (mAutofillProvider != null) {
+ mAutofillProvider.onContainerViewChanged(mContentView);
+ }
}
@Override
public void onViewDetachedFromWindow(View view) {
mIsViewAttachedToWindow = false;
updateInteractableState();
+ if (mAutofillProvider != null) {
+ mAutofillProvider.onContainerViewChanged(mContentView);
+ }
}
};
mTabViewManager = new TabViewManagerImpl(this);
@@ -796,6 +811,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);
@@ -1393,6 +1413,18 @@ public class TabImpl implements Tab {
return tabsPtrArray;
}
+ 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.
@@ -1436,10 +1468,23 @@ public class TabImpl implements Tab {
mWebContentsDelegate = createWebContentsDelegate();
assert mNativeTabAndroid != 0;
+ SelectionPopupController selectionController = null;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ selectionController = SelectionPopupController.fromWebContents(mWebContents);
+ mAutofillProvider = new AutofillProvider(
+ getContext(), cv, webContents, "NativeAutofillRenderer");
+ }
TabImplJni.get().initWebContents(mNativeTabAndroid, mIncognito, isDetached(this),
webContents, mWebContentsDelegate,
new TabContextMenuPopulatorFactory(
mDelegateFactory.createContextMenuPopulatorFactory(this), this));
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && selectionController != null) {
+ mAutofillProvider.setWebContents(webContents);
+ cv.setWebContents(webContents);
+ selectionController.setNonSelectionActionModeCallback(
+ new AutofillActionModeCallback(
+ mThemedApplicationContext, mAutofillProvider));
+ }
mWebContents.notifyRendererPreferenceUpdate();
TabHelpers.initWebContentsHelpers(this);
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
index aeb890d6fd459..df95505407cda 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/tab/TabViewAndroidDelegate.java
@@ -4,7 +4,10 @@
package org.chromium.chrome.browser.tab;
+import android.util.SparseArray;
import android.view.ViewGroup;
+import android.view.ViewStructure;
+import android.view.autofill.AutofillValue;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@@ -87,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
index acf48b7ffd061..ef9200037fe05 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -2484,6 +2484,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/ui/autofill/chrome_autofill_client.cc b/chrome/browser/ui/autofill/chrome_autofill_client.cc
index 07663b4b2e793..dccf3ac3163dc 100644
--- a/chrome/browser/ui/autofill/chrome_autofill_client.cc
+++ b/chrome/browser/ui/autofill/chrome_autofill_client.cc
@@ -53,6 +53,9 @@
#include "chrome/browser/web_data_service_factory.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/url_constants.h"
+#if BUILDFLAG(IS_ANDROID)
+#include "components/android_autofill/browser/android_autofill_manager.h"
+#endif // BUILDFLAG(IS_ANDROID)
#include "components/autofill/content/browser/autofill_log_router_factory.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
@@ -180,12 +183,16 @@ ChromeAutofillClient::GetURLLoaderFactory() {
}
AutofillDownloadManager* ChromeAutofillClient::GetDownloadManager() {
+#if defined(USE_BROWSER_AUTOFILL_ONLY)
if (!download_manager_) {
// Lazy initialization to avoid virtual function calls in the constructor.
download_manager_ = std::make_unique<AutofillDownloadManager>(
this, GetChannel(), GetLogManager());
}
return download_manager_.get();
+#else
+ return nullptr;
+#endif // defined(USE_BROWSER_AUTOFILL_ONLY)
}
AutofillOptimizationGuide* ChromeAutofillClient::GetAutofillOptimizationGuide()
@@ -1162,7 +1169,12 @@ void ChromeAutofillClient::OnZoomChanged(
ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
: ContentAutofillClient(
web_contents,
- base::BindRepeating(&BrowserDriverInitHook,
+ base::BindRepeating(
+#if BUILDFLAG(IS_ANDROID)
+ &AndroidAndBrowserDriverInitHook,
+#else
+ &BrowserDriverInitHook,
+#endif // BUILDFLAG(IS_ANDROID)
this,
g_browser_process->GetApplicationLocale())),
content::WebContentsObserver(web_contents),
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
index 0e909aab3760a..58de87e58e3f1 100644
--- 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
@@ -9,6 +9,7 @@ import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
+import android.util.SparseArray;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
@@ -18,6 +19,7 @@ import android.view.View.OnSystemUiVisibilityChangeListener;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.view.ViewStructure;
import android.view.accessibility.AccessibilityNodeProvider;
+import android.view.autofill.AutofillValue;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.FrameLayout;
@@ -37,6 +39,7 @@ import org.chromium.ui.accessibility.AccessibilityState;
import org.chromium.ui.base.EventForwarder;
import org.chromium.ui.base.EventOffsetHandler;
import org.chromium.ui.dragdrop.DragEventDispatchHelper.DragEventDispatchDestination;
+import org.chromium.ui.base.ViewAndroidDelegate;
/**
* The containing view for {@link WebContents} that exists in the Android UI hierarchy and exposes
@@ -89,6 +92,9 @@ 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);
}
@@ -619,4 +625,46 @@ public class ContentView extends FrameLayout
mDragDropEventOffsetHandler.onPostDispatchDragEvent(event.getAction());
return ret;
}
+
+ /**
+ * API level 26 implementation that includes autofill.
+ */
+ private 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
index 454eb50e76104..849fdd41bca62 100644
--- a/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java
+++ b/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java
@@ -29,6 +29,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.
*/
@@ -620,4 +624,8 @@ public class ViewAndroidDelegate {
public static void setDragAndDropDelegateForTest(DragAndDropDelegate testDelegate) {
sDragAndDropTestDelegate = testDelegate;
}
+
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {}
+
+ public void autofill(final SparseArray<AutofillValue> values) {}
}