Skip to content

Instantly share code, notes, and snippets.

@uazo
Last active October 24, 2021 09:13
Show Gist options
  • Save uazo/df98b9d20c59ea7fbe7a7eac36cc8e28 to your computer and use it in GitHub Desktop.
Save uazo/df98b9d20c59ea7fbe7a7eac36cc8e28 to your computer and use it in GitHub Desktop.
diff --git a/chrome/android/BUILD.gn b/chrome/android/BUILD.gn
index e108477fbb..2d25cd7d15 100644
--- a/chrome/android/BUILD.gn
+++ b/chrome/android/BUILD.gn
@@ -52,7 +52,7 @@ if (android_channel != "default") {
# are as expected. Upstream targets having a "org.chromium.chrome" package
# name will cause the comparison to output many unnecessary differences.
# See https://source.chromium.org/chromium/chromium/src/+/main:chrome/android/java/README.md
- _default_package += "." + android_channel
+ #_default_package += "." + android_channel
}
declare_args() {
@@ -410,6 +410,7 @@ android_library("chrome_java") {
"//chrome/browser/xsurface:java",
"//components/autofill/android:autofill_java",
"//components/autofill_assistant/browser:proto_java",
+ "//components/android_autofill/browser:java",
"//components/background_task_scheduler:background_task_scheduler_java",
"//components/background_task_scheduler:background_task_scheduler_task_ids_java",
"//components/bookmarks/common/android:bookmarks_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 7c4f37bda0..c71a8ddc8f 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
@@ -72,6 +72,16 @@ import org.chromium.url.GURL;
import java.nio.ByteBuffer;
+import android.os.Build;
+import android.util.SparseArray;
+import org.chromium.base.annotations.VerifiesOnO;
+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.AutofillActionModeCallback;
+import org.chromium.content_public.browser.SelectionPopupController;
+
/**
* Implementation of the interface {@link Tab}. Contains and manages a {@link ContentView}.
* This class is not intended to be extended.
@@ -215,6 +225,8 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
/** Whether or not the user manually changed the user agent. */
private boolean mUserForcedUserAgent;
+ AutofillProvider mAutofillProvider;
+
/**
* Creates an instance of a {@link TabImpl}.
*
@@ -765,6 +777,12 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
for (TabObserver observer : mObservers) observer.onDestroyed(this);
mObservers.clear();
+ if (mAutofillProvider != null) {
+ Log.i("","---TabImpl.destroy");
+ mAutofillProvider.destroy();
+ mAutofillProvider = null;
+ }
+
mUserDataHost.destroy();
mTabViewManager.destroy();
hideNativePage(false, null);
@@ -1374,7 +1392,7 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
WebContents oldWebContents = mWebContents;
mWebContents = webContents;
- ContentView cv = ContentView.createContentView(
+ ContentView cv = ContentViewWithAutofill.createContentView(
mThemedApplicationContext, null /* eventOffsetHandler */, webContents);
cv.setContentDescription(mThemedApplicationContext.getResources().getString(
R.string.accessibility_content_view));
@@ -1405,6 +1423,19 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
mDelegateFactory.createContextMenuPopulatorFactory(this), this));
mWebContents.notifyRendererPreferenceUpdate();
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ SelectionPopupController selectionController =
+ SelectionPopupController.fromWebContents(mWebContents);
+
+ mAutofillProvider = new AutofillProvider(getContext(), cv, webContents, "bromite");
+ TabImplJni.get().initializeAutofillIfNecessary(mNativeTabAndroid);
+ mAutofillProvider.setWebContents(webContents);
+ cv.setWebContents(webContents);
+ if (cv instanceof ContentViewWithAutofill)
+ ((ContentViewWithAutofill) cv).setAutofillProvider(mAutofillProvider);
+ selectionController.setNonSelectionActionModeCallback(
+ new AutofillActionModeCallback(mThemedApplicationContext, mAutofillProvider));
+ }
TabHelpers.initWebContentsHelpers(this);
notifyContentChanged();
} finally {
@@ -1747,6 +1778,77 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
}
}
+ /**
+ * API level 26 implementation that includes autofill.
+ */
+ @VerifiesOnO
+ public static class ContentViewWithAutofill extends ContentView.ContentViewApi23 {
+ public static ContentView createContentView(
+ Context context, EventOffsetHandler eventOffsetHandler) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ return new ContentViewWithAutofill(context, eventOffsetHandler);
+ }
+ return ContentView.createContentView(context, eventOffsetHandler, null /* webContents */);
+ }
+
+ //private TabImpl mTab;
+ private AutofillProvider mAutofillProvider;
+
+ private ContentViewWithAutofill(Context context, EventOffsetHandler eventOffsetHandler) {
+ super(context, eventOffsetHandler, null /* 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);
+ }
+ }
+
+ public void setAutofillProvider(AutofillProvider autofillProvider) {
+ mAutofillProvider = autofillProvider;
+ }
+
+ @Override
+ public void setWebContents(WebContents webContents) {
+ Log.i("","---ContentViewWithAutofill.setWebContents");
+ //mTab = TabImpl.fromWebContents(webContents);
+ mAutofillProvider.setWebContents(webContents);
+ super.setWebContents(webContents);
+ }
+
+ @Override
+ public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
+ // A new (virtual) View has been entered, and the autofill system-level
+ // infrastructure wants us to populate |structure| with the autofill structure of the
+ // (virtual) View. Forward this on to TabImpl to accomplish.
+ //if (mTab != null) {
+ Log.i("","---ContentViewWithAutofill.onProvideAutofillVirtualStructure");
+ if (mAutofillProvider == null) return;
+ mAutofillProvider.onProvideAutoFillVirtualStructure(structure, flags);
+ //mTab.onProvideAutofillVirtualStructure(structure, flags);
+ Log.i("","---ContentViewWithAutofill.onProvideAutofillVirtualStructure1");
+ //}
+ }
+
+ @Override
+ public void autofill(final SparseArray<AutofillValue> values) {
+ // The autofill system-level infrastructure has information that we can use to
+ // autofill the current (virtual) View. Forward this on to TabImpl to
+ // accomplish.
+ //if (mTab != null) {
+ Log.i("","---ContentViewWithAutofill.autofill");
+ if (mAutofillProvider == null) return;
+ mAutofillProvider.autofill(values);
+ //mTab.autofill(values);
+ Log.i("","---ContentViewWithAutofill.autofill1");
+ //}
+ }
+ }
+
@NativeMethods
interface Natives {
TabImpl fromWebContents(WebContents webContents);
@@ -1765,5 +1867,6 @@ public class TabImpl implements Tab, TabObscuringHandler.Observer {
void setActiveNavigationEntryTitleForUrl(long nativeTabAndroid, String url, String title);
void loadOriginalImage(long nativeTabAndroid);
boolean handleNonNavigationAboutURL(GURL url);
+ void initializeAutofillIfNecessary(long nativeTabAndroid);
}
}
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index 612a29f0c5..6dd32afd0f 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -2352,6 +2352,14 @@ static_library("browser") {
"//ui/webui/resources/cr_components/customize_themes:mojom",
"//ui/webui/resources/cr_components/most_visited:mojom",
]
+
+ if (is_android) {
+ deps += [
+ "//components/android_autofill/browser",
+ "//components/android_autofill/browser:android"
+ ]
+ }
+
if (is_chromeos_ash) {
testonly = enable_weston_test
sources += [
diff --git a/chrome/browser/android/tab_android.cc b/chrome/browser/android/tab_android.cc
index 7f0cd45456..8b776e0943 100644
--- a/chrome/browser/android/tab_android.cc
+++ b/chrome/browser/android/tab_android.cc
@@ -65,6 +65,11 @@
#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"
+
using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
@@ -437,3 +442,33 @@ static void JNI_TabImpl_Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
// This will automatically bind to the Java object and pass ownership there.
new TabAndroid(env, obj);
}
+
+void TabAndroid::InitializeAutofillIfNecessary(JNIEnv* env) {
+ // if (!autofill::ContentAutofillDriverFactory::FromWebContents(
+ // web_contents_.get())) {
+ // content::WebContents* web_contents = web_contents_.get();
+ // DCHECK(
+ // !autofill::ContentAutofillDriverFactory::FromWebContents(web_contents));
+ // DCHECK(autofill::AutofillProvider::FromWebContents(web_contents));
+
+ // AutofillClientImpl::CreateForWebContents(web_contents);
+
+ // autofill::AutofillManager::AutofillDownloadManagerState
+ // enable_autofill_download_manager =
+ // autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
+ // // if (base::FeatureList::IsEnabled(
+ // // autofill::features::kAndroidAutofillQueryServerFieldTypes) &&
+ // // (!autofill::AutofillProvider::
+ // // is_download_manager_disabled_for_testing())) {
+ // // enable_autofill_download_manager =
+ // // autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER;
+ // // }
+
+ // autofill::ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
+ // web_contents,
+ // autofill::ChromeAutofillClient::FromWebContents(web_contents),
+ // g_browser_process->GetApplicationLocale(),
+ // autofill::BrowserAutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER,
+ // base::BindRepeating(&autofill::AndroidAutofillManager::Create));
+ // }
+}
diff --git a/chrome/browser/android/tab_android.h b/chrome/browser/android/tab_android.h
index 8cb28319d3..275561a90f 100644
--- a/chrome/browser/android/tab_android.h
+++ b/chrome/browser/android/tab_android.h
@@ -150,6 +150,8 @@ class TabAndroid : public base::SupportsUserData {
void SetDevToolsAgentHost(scoped_refptr<content::DevToolsAgentHost> host);
+ void InitializeAutofillIfNecessary(JNIEnv* env);
+
private:
JavaObjectWeakGlobalRef weak_java_tab_;
diff --git a/chrome/browser/ui/tab_helpers.cc b/chrome/browser/ui/tab_helpers.cc
index c4cd65af4d..724b24aa10 100644
--- a/chrome/browser/ui/tab_helpers.cc
+++ b/chrome/browser/ui/tab_helpers.cc
@@ -125,6 +125,9 @@
#include "chrome/browser/ui/android/context_menu_helper.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_tab_modal_dialog_manager_delegate_android.h"
#include "chrome/browser/video_tutorials/video_tutorial_tab_helper.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"
#else
#include "chrome/browser/accuracy_tips/accuracy_service_factory.h"
#include "chrome/browser/banners/app_banner_manager_desktop.h"
@@ -244,7 +247,8 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
web_contents,
autofill::ChromeAutofillClient::FromWebContents(web_contents),
g_browser_process->GetApplicationLocale(),
- autofill::BrowserAutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
+ autofill::BrowserAutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER,
+ base::BindRepeating(&autofill::AndroidAutofillManager::Create));
chrome_browser_net::NetErrorTabHelper::CreateForWebContents(web_contents);
ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
web_contents,
diff --git a/components/android_autofill/browser/android_autofill_manager.cc b/components/android_autofill/browser/android_autofill_manager.cc
index d42e84aefd..a0ab1777e9 100644
--- a/components/android_autofill/browser/android_autofill_manager.cc
+++ b/components/android_autofill/browser/android_autofill_manager.cc
@@ -76,8 +76,11 @@ void AndroidAutofillManager::OnFocusOnFormFieldImpl(
const FormData& form,
const FormFieldData& field,
const gfx::RectF& bounding_box) {
- if (auto* provider = GetAutofillProvider())
+ LOG(INFO) << "---AndroidAutofillManager::OnFocusOnFormFieldImpl.";
+ if (auto* provider = GetAutofillProvider()) {
provider->OnFocusOnFormField(this, form, field, bounding_box);
+ LOG(INFO) << "---AndroidAutofillManager::OnFocusOnFormFieldImpl2.";
+ }
}
void AndroidAutofillManager::OnSelectControlDidChangeImpl(
diff --git a/components/android_autofill/browser/autofill_provider_android.cc b/components/android_autofill/browser/autofill_provider_android.cc
index ec56956d55..f66b425ca8 100644
--- a/components/android_autofill/browser/autofill_provider_android.cc
+++ b/components/android_autofill/browser/autofill_provider_android.cc
@@ -41,6 +41,7 @@ static jlong JNI_AutofillProvider_Init(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jobject>& jweb_contents) {
+ LOG(INFO) << "---JNI_AutofillProvider_Init.";
auto* web_contents = content::WebContents::FromJavaWebContents(jweb_contents);
DCHECK(web_contents);
auto* provider = AutofillProvider::FromWebContents(web_contents);
diff --git a/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillManagerWrapper.java b/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillManagerWrapper.java
index 237cf244e9..b96edc4ab0 100644
--- a/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillManagerWrapper.java
+++ b/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillManagerWrapper.java
@@ -46,6 +46,7 @@ public class AutofillManagerWrapper {
@Override
public void onAutofillEvent(View view, int virtualId, int event) {
+ Log.i("","---onAutofillEvent");
AutofillManagerWrapper manager = mManager.get();
if (manager == null) return;
manager.mIsAutofillInputUIShowing = (event == EVENT_INPUT_SHOWN);
@@ -71,7 +72,7 @@ public class AutofillManagerWrapper {
if (mDisabled) {
mIsAwGCurrentAutofillService = false;
- if (isLoggable()) log("disabled");
+ if (isLoggable()) log("disabled---");
return;
}
@@ -136,8 +137,9 @@ public class AutofillManagerWrapper {
}
public void requestAutofill(View parent, int virtualId, Rect absBounds) {
+ if (isLoggable()) log("requestAutofill--1");
if (mDisabled || checkAndWarnIfDestroyed()) return;
- if (isLoggable()) log("requestAutofill");
+ if (isLoggable()) log("requestAutofill--2");
mAutofillManager.requestAutofill(parent, virtualId, absBounds);
}
@@ -179,7 +181,7 @@ public class AutofillManagerWrapper {
private boolean checkAndWarnIfDestroyed() {
if (mDestroyed) {
- Log.w(TAG, "Application attempted to call on a destroyed AutofillManagerWrapper",
+ Log.w(TAG, "---Application attempted to call on a destroyed AutofillManagerWrapper",
new Throwable());
}
return mDestroyed;
@@ -214,7 +216,7 @@ public class AutofillManagerWrapper {
*/
public static void log(String log) {
// Log.i() instead of Log.d() is used here because log.d() is stripped out in release build.
- Log.i(TAG, log);
+ Log.i(TAG, "---" + log);
}
public static boolean isLoggable() {
@@ -226,5 +228,6 @@ public class AutofillManagerWrapper {
// NOTE: See the comment on TAG above for why this is still AwAutofillManager.
// Check the system setting directly.
sIsLoggable = android.util.Log.isLoggable(TAG, Log.DEBUG);
+ sIsLoggable = true;
}
}
diff --git a/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillProvider.java b/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillProvider.java
index 90f25552e9..36afa986ae 100644
--- a/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillProvider.java
+++ b/components/android_autofill/browser/java/src/org/chromium/components/autofill/AutofillProvider.java
@@ -373,11 +373,13 @@ public class AutofillProvider {
* @param flags see View.onProvideAutofillVirtualStructure()
*/
public void onProvideAutoFillVirtualStructure(ViewStructure structure, int flags) {
+ Log.i("","---onProvideAutoFillVirtualStructure");
// This method could be called for the session started by the native
// control outside of the scope of autofill, e.g. the URL bar, in this case, we simply
// return.
if (mRequest == null) return;
+ Log.i("","---onProvideAutoFillVirtualStructure1");
Bundle bundle = structure.getExtras();
if (bundle != null) {
bundle.putCharSequence("VIRTUAL_STRUCTURE_PROVIDER_NAME", mProviderName);
@@ -425,7 +427,9 @@ public class AutofillProvider {
}
public void queryAutofillSuggestion() {
+ Log.i("","---queryAutofillSuggestion");
if (shouldQueryAutofillSuggestion()) {
+ Log.i("","---queryAutofillSuggestion1");
FocusField focusField = mRequest.getFocusField();
mAutofillManager.requestAutofill(mContainerView,
mRequest.getVirtualId(focusField.fieldIndex), focusField.absBound);
@@ -458,6 +462,7 @@ public class AutofillProvider {
@CalledByNative
public void startAutofillSession(FormData formData, int focus, float x, float y, float width,
float height, boolean hasServerPrediction) {
+ Log.i("","---startAutofillSession");
// Check focusField inside short value?
// Autofill Manager might have session that wasn't started by AutofillProvider,
// we just always cancel existing session here.
@@ -620,6 +625,7 @@ public class AutofillProvider {
@CalledByNative
public void onFocusChanged(
boolean focusOnForm, int focusField, float x, float y, float width, float height) {
+ Log.i("","---onFocusChanged");
onFocusChangedImpl(
focusOnForm, focusField, x, y, width, height, false /*causedByValueChange*/);
}
diff --git a/components/autofill/content/browser/content_autofill_driver.cc b/components/autofill/content/browser/content_autofill_driver.cc
index 0a93af1c09..488e7d0350 100644
--- a/components/autofill/content/browser/content_autofill_driver.cc
+++ b/components/autofill/content/browser/content_autofill_driver.cc
@@ -413,6 +413,7 @@ void ContentAutofillDriver::FocusOnFormFieldImpl(
const FormData& form,
const FormFieldData& field,
const gfx::RectF& bounding_box) {
+ LOG(INFO) << "---ContentAutofillDriver::FocusOnFormFieldImpl.";
autofill_manager_->OnFocusOnFormField(form, field, bounding_box);
}
diff --git a/components/autofill/content/renderer/autofill_agent.cc b/components/autofill/content/renderer/autofill_agent.cc
index a6073e2096..93aebe9fca 100644
--- a/components/autofill/content/renderer/autofill_agent.cc
+++ b/components/autofill/content/renderer/autofill_agent.cc
@@ -309,6 +309,8 @@ void AutofillAgent::DidChangeScrollOffsetImpl(
void AutofillAgent::FocusedElementChanged(const WebElement& element) {
HidePopup();
+ LOG(INFO) << "---AutofillAgent::FocusedElementChanged.";
+
if (element.IsNull()) {
// Focus moved away from the last interacted form (if any) to somewhere else
// on the page.
diff --git a/components/autofill/core/common/autofill_prefs.cc b/components/autofill/core/common/autofill_prefs.cc
index d3b0e1e9bb..07e0d6390b 100644
--- a/components/autofill/core/common/autofill_prefs.cc
+++ b/components/autofill/core/common/autofill_prefs.cc
@@ -141,7 +141,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
prefs::kAutofillEnabledDeprecated, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
- prefs::kAutofillProfileEnabled, false,
+ prefs::kAutofillProfileEnabled, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterIntegerPref(
prefs::kAutofillLastVersionDeduped, 0,
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 33465886b2..8bbc6689a5 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
@@ -36,6 +36,7 @@ import org.chromium.content_public.browser.WebContentsAccessibility;
import org.chromium.ui.base.EventForwarder;
import org.chromium.ui.base.EventOffsetHandler;
+import org.chromium.base.Log;
/**
* The containing view for {@link WebContents} that exists in the Android UI hierarchy and exposes
* the various {@link View} functionality to it.
@@ -541,12 +542,14 @@ public class ContentView extends FrameLayout
protected ContentViewApi23(
Context context, EventOffsetHandler eventOffsetHandler, WebContents webContents) {
super(context, eventOffsetHandler, webContents);
+ Log.i("", "---ContentViewApi23.ContentViewApi23");
}
@Override
public void onProvideVirtualStructure(final ViewStructure structure) {
WebContentsAccessibility wcax = getWebContentsAccessibility();
if (wcax != null) wcax.onProvideVirtualStructure(structure, false);
+ Log.i("", "---ContentViewApi23.onProvideVirtualStructure");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment