Commit bc6327c3 authored by Michael Bai's avatar Michael Bai Committed by Chromium LUCI CQ

Autofill: Update prediction after session starts

Update the prediction after the session starts and notify
AutofillProvider the server prediction available.

Bug: 1151542
Change-Id: Id41b025f3a99e95167e6a75ee0eea796a4146786
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2585960
Commit-Queue: Michael Bai <michaelbai@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836242}
parent cceea30b
...@@ -94,6 +94,7 @@ public class AwAutofillTest { ...@@ -94,6 +94,7 @@ public class AwAutofillTest {
public static final int AUTOFILL_COMMIT = 4; public static final int AUTOFILL_COMMIT = 4;
public static final int AUTOFILL_CANCEL = 5; public static final int AUTOFILL_CANCEL = 5;
public static final int AUTOFILL_SESSION_STARTED = 6; public static final int AUTOFILL_SESSION_STARTED = 6;
public static final int AUTOFILL_QUERY_DONE = 7;
/** /**
* This class only implements the necessary methods of ViewStructure for testing. * This class only implements the necessary methods of ViewStructure for testing.
...@@ -470,6 +471,7 @@ public class AwAutofillTest { ...@@ -470,6 +471,7 @@ public class AwAutofillTest {
private class TestAutofillManagerWrapper extends AutofillManagerWrapper { private class TestAutofillManagerWrapper extends AutofillManagerWrapper {
private boolean mDisabled; private boolean mDisabled;
private boolean mQuerySucceed;
public TestAutofillManagerWrapper(Context context) { public TestAutofillManagerWrapper(Context context) {
super(context); super(context);
...@@ -484,6 +486,9 @@ public class AwAutofillTest { ...@@ -484,6 +486,9 @@ public class AwAutofillTest {
return mDisabled; return mDisabled;
} }
public boolean isQuerySucceed() {
return mQuerySucceed;
}
@Override @Override
public void notifyVirtualViewEntered(View parent, int childId, Rect absBounds) { public void notifyVirtualViewEntered(View parent, int childId, Rect absBounds) {
if (DEBUG) Log.i(TAG, "notifyVirtualViewEntered"); if (DEBUG) Log.i(TAG, "notifyVirtualViewEntered");
...@@ -530,6 +535,14 @@ public class AwAutofillTest { ...@@ -530,6 +535,14 @@ public class AwAutofillTest {
mEventQueue.add(AUTOFILL_SESSION_STARTED); mEventQueue.add(AUTOFILL_SESSION_STARTED);
mCallbackHelper.notifyCalled(); mCallbackHelper.notifyCalled();
} }
@Override
public void onQueryDone(boolean success) {
mQuerySucceed = success;
if (DEBUG) Log.i(TAG, "onQueryDone " + success);
mEventQueue.add(AUTOFILL_QUERY_DONE);
mCallbackHelper.notifyCalled();
}
} }
private static class AwAutofillTestClient extends TestAwContentsClient { private static class AwAutofillTestClient extends TestAwContentsClient {
...@@ -2133,7 +2146,18 @@ public class AwAutofillTest { ...@@ -2133,7 +2146,18 @@ public class AwAutofillTest {
assertEquals("NO_SERVER_DATA", assertEquals("NO_SERVER_DATA",
viewStructure.getChild(1).getHtmlInfo().getAttribute("computed-autofill-hints")); viewStructure.getChild(1).getHtmlInfo().getAttribute("computed-autofill-hints"));
// TODO(crbug.com/1151542): Complete the test once the prediction update is implemented. TestThreadUtils.runOnUiThreadBlocking(
()
-> AutofillProviderTestHelper
.simulateMainFrameAutofillServerResponseForTesting(
mAwContents.getWebContents(),
new String[] {"text1", "text2"},
new int[] {/*EMAIL_ADDRESS, USERNAME*/ 9, 86}));
cnt += waitForCallbackAndVerifyTypes(cnt, new Integer[] {AUTOFILL_QUERY_DONE});
assertTrue(mTestAutofillManagerWrapper.isQuerySucceed());
// TODO(crbug.com/1151542): Verify the field types once they are sent from service.
} }
private void pollJavascriptResult(String script, String expectedResult) throws Throwable { private void pollJavascriptResult(String script, String expectedResult) throws Throwable {
......
...@@ -358,6 +358,20 @@ void AutofillProviderAndroid::OnHidePopup(AutofillHandlerProxy* handler) { ...@@ -358,6 +358,20 @@ void AutofillProviderAndroid::OnHidePopup(AutofillHandlerProxy* handler) {
void AutofillProviderAndroid::OnServerPredictionsAvailable( void AutofillProviderAndroid::OnServerPredictionsAvailable(
AutofillHandlerProxy* handler) { AutofillHandlerProxy* handler) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (handler != handler_.get() || !form_.get())
return;
if (auto* form_structure = handler_->FindCachedFormByRendererId(
form_->form().unique_renderer_id)) {
form_->UpdateFieldTypes(*form_structure);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
Java_AutofillProvider_onQueryDone(env, obj, /*success=*/true);
}
} }
void AutofillProviderAndroid::Reset(AutofillHandlerProxy* handler) { void AutofillProviderAndroid::Reset(AutofillHandlerProxy* handler) {
......
...@@ -52,13 +52,10 @@ ScopedJavaLocalRef<jobject> FormFieldDataAndroid::GetJavaPeer() { ...@@ -52,13 +52,10 @@ ScopedJavaLocalRef<jobject> FormFieldDataAndroid::GetJavaPeer() {
ConvertUTF8ToJavaString(env, heuristic_type_.ToString()); ConvertUTF8ToJavaString(env, heuristic_type_.ToString());
} }
ScopedJavaLocalRef<jstring> jserver_type; ScopedJavaLocalRef<jstring> jserver_type;
if (!server_type_.IsUnknown()) { jserver_type = ConvertUTF8ToJavaString(env, server_type_.ToString());
jserver_type = ConvertUTF8ToJavaString(env, server_type_.ToString());
}
ScopedJavaLocalRef<jstring> jcomputed_type; ScopedJavaLocalRef<jstring> jcomputed_type;
if (!computed_type_.IsUnknown()) { jcomputed_type = ConvertUTF8ToJavaString(env, computed_type_.ToString());
jcomputed_type = ConvertUTF8ToJavaString(env, computed_type_.ToString());
}
ScopedJavaLocalRef<jobjectArray> jdatalist_values = ScopedJavaLocalRef<jobjectArray> jdatalist_values =
ToJavaArrayOfStrings(env, field_ptr_->datalist_values); ToJavaArrayOfStrings(env, field_ptr_->datalist_values);
ScopedJavaLocalRef<jobjectArray> jdatalist_labels = ScopedJavaLocalRef<jobjectArray> jdatalist_labels =
...@@ -120,6 +117,20 @@ void FormFieldDataAndroid::UpdateAutofillTypes( ...@@ -120,6 +117,20 @@ void FormFieldDataAndroid::UpdateAutofillTypes(
heuristic_type_ = heuristic_type; heuristic_type_ = heuristic_type;
server_type_ = server_type; server_type_ = server_type;
computed_type_ = computed_type; computed_type_ = computed_type;
// Java peer isn't available when this object is instantiated, update to
// Java peer if the prediction arrives later.
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null())
return;
ScopedJavaLocalRef<jstring> jserver_type;
jserver_type = ConvertUTF8ToJavaString(env, server_type_.ToString());
ScopedJavaLocalRef<jstring> jcomputed_type;
jcomputed_type = ConvertUTF8ToJavaString(env, computed_type_.ToString());
Java_FormFieldData_updateFieldTypes(env, obj, jserver_type, jcomputed_type);
} }
} // namespace autofill } // namespace autofill
...@@ -170,6 +170,10 @@ public class AutofillManagerWrapper { ...@@ -170,6 +170,10 @@ public class AutofillManagerWrapper {
if (isLoggable()) log("Session starts"); if (isLoggable()) log("Session starts");
} }
public void onQueryDone(boolean success) {
if (isLoggable()) log("Query " + (success ? "succeed" : "failed"));
}
/** /**
* Always check isLoggable() before call this method. * Always check isLoggable() before call this method.
*/ */
......
...@@ -709,6 +709,11 @@ public class AutofillProvider { ...@@ -709,6 +709,11 @@ public class AutofillProvider {
forceNotifyFormValues(); forceNotifyFormValues();
} }
@CalledByNative
private void onQueryDone(boolean success) {
mAutofillManager.onQueryDone(success);
}
private void forceNotifyFormValues() { private void forceNotifyFormValues() {
if (mRequest == null) return; if (mRequest == null) return;
for (int i = 0; i < mRequest.getFieldCount(); ++i) { for (int i = 0; i < mRequest.getFieldCount(); ++i) {
......
...@@ -144,6 +144,12 @@ public class FormFieldData { ...@@ -144,6 +144,12 @@ public class FormFieldData {
updateAutofillState(false); updateAutofillState(false);
} }
@CalledByNative
private void updateFieldTypes(String serverType, String computedType) {
mServerType = serverType;
mComputedType = computedType;
}
public String getServerType() { public String getServerType() {
return mServerType; return mServerType;
} }
......
...@@ -146,6 +146,10 @@ class AutofillHandler : public AutofillDownloadManager::Observer { ...@@ -146,6 +146,10 @@ class AutofillHandler : public AutofillDownloadManager::Observer {
FormStructure** form_structure, FormStructure** form_structure,
AutofillField** autofill_field) WARN_UNUSED_RESULT; AutofillField** autofill_field) WARN_UNUSED_RESULT;
// Returns nullptr if no cached form structure is found with a matching
// |renderer_id|. Runs in logarithmic time.
FormStructure* FindCachedFormByRendererId(FormRendererId renderer_id) const;
// Returns the number of forms this Autofill handler is aware of. // Returns the number of forms this Autofill handler is aware of.
size_t NumFormsDetected() const { return form_structures_.size(); } size_t NumFormsDetected() const { return form_structures_.size(); }
...@@ -260,10 +264,6 @@ class AutofillHandler : public AutofillDownloadManager::Observer { ...@@ -260,10 +264,6 @@ class AutofillHandler : public AutofillDownloadManager::Observer {
FormSignature form_signature, FormSignature form_signature,
std::vector<FormStructure*>* form_structures) const; std::vector<FormStructure*>* form_structures) const;
// Returns nullptr if no cached form structure is found with a matching
// |renderer_id|. Runs in logarithmic time.
FormStructure* FindCachedFormByRendererId(FormRendererId renderer_id) const;
// Parses the |form| with the server data retrieved from the |cached_form| // Parses the |form| with the server data retrieved from the |cached_form|
// (if any). Returns nullptr if the form should not be parsed. Otherwise, adds // (if any). Returns nullptr if the form should not be parsed. Otherwise, adds
// the returned form structure to the |form_structures_|. // the returned form structure to the |form_structures_|.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment