Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(783)

Unified Diff: components/autofill/core/browser/autofill_provider_android.cc

Issue 2839023003: WebView autofill implementation (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/autofill/core/browser/autofill_provider_android.cc
diff --git a/components/autofill/core/browser/autofill_provider_android.cc b/components/autofill/core/browser/autofill_provider_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4bee7c7fb38f3a6f5183c2b77cc5b9c1290f7872
--- /dev/null
+++ b/components/autofill/core/browser/autofill_provider_android.cc
@@ -0,0 +1,188 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/autofill/core/browser/autofill_provider_android.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "components/autofill/core/browser/autofill_manager.h"
+#include "components/autofill/core/browser/form_data_android.h"
+#include "content/public/browser/browser_thread.h"
+#include "jni/AutofillProvider_jni.h"
+#include "ui/gfx/geometry/rect_f.h"
+
+using base::android::AttachCurrentThread;
+using base::android::ConvertUTF16ToJavaString;
+using base::android::ConvertUTF8ToJavaString;
+using base::android::JavaParamRef;
+using base::android::ScopedJavaLocalRef;
+using content::BrowserThread;
+using gfx::RectF;
+
+namespace autofill {
+
+const int kInvalidRequestId = -1;
+
+static jlong CreateNativePeer(JNIEnv* env,
+ const JavaParamRef<jobject>& jcaller) {
+ return reinterpret_cast<jlong>(
+ new autofill::AutofillProviderAndroid(jcaller));
+}
+
+AutofillProviderAndroid::AutofillProviderAndroid(
+ const JavaParamRef<jobject>& jcaller)
+ : id_(kInvalidRequestId) {
+ JNIEnv* env = AttachCurrentThread();
+ java_ref_ = JavaObjectWeakGlobalRef(env, jcaller);
+}
+
+AutofillProviderAndroid::~AutofillProviderAndroid() {
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
+ if (obj.is_null())
+ return;
+
+ Java_AutofillProvider_onNativeDestroyed(env, obj);
+}
+
+void AutofillProviderAndroid::OnQueryFormFieldAutofill(
+ AutofillManager* manager,
+ int32_t id,
+ const FormData& form,
+ const FormFieldData& field,
+ const gfx::RectF& bounding_box) {
+ // The id isn't passed to Java side because Android API guaranttes the
sgurun-gerrit only 2017/04/28 01:46:39 nit: s/guaranttes/guarantees/
michaelbai 2017/05/03 04:43:59 Done.
+ // response is always for current session, so we just use the current id
+ // in response, see OnAutofillAvailable.
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ id_ = id;
+ if (manager_.get() == manager && form_ && form_->SimilarFormAs(form)) {
+ size_t index;
+ if (form_->GetSimilarFieldIndex(field, &index)) {
+ OnFocusChanged(true, index, bounding_box);
+ }
+ return;
+ }
+
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
+ if (obj.is_null())
+ return;
+
+ form_.reset(new FormDataAndroid(form));
sgurun-gerrit only 2017/04/28 01:46:39 MakeUnique?
michaelbai 2017/05/03 04:43:59 Done.
+
+ size_t index;
+ if (!form_->GetFieldIndex(field, &index))
sgurun-gerrit only 2017/04/28 01:46:40 I don't understand why we need to do the similarit
michaelbai 2017/05/03 04:43:59 This uses to get index of field.
+ return;
+
+ ScopedJavaLocalRef<jobject> formObj = form_->GetJavaPeer();
+ manager_ = manager->GetWeakPtr();
+ Java_AutofillProvider_queryFormFieldAutofill(
sgurun-gerrit only 2017/04/28 01:46:40 keep the same name, i.e. onQueryFormFieldAutofill
michaelbai 2017/05/03 04:43:59 I intentionally used different name, OnQueryFormFi
+ env, obj, formObj, index, bounding_box.x(), bounding_box.y(),
+ bounding_box.width(), bounding_box.height());
sgurun-gerrit only 2017/04/28 01:46:40 you don't seem to be converting x,y coordinates to
michaelbai 2017/05/03 04:43:59 The bound still wasn't correct, after use the code
+}
+
+void AutofillProviderAndroid::OnAutofillAvailable(JNIEnv* env,
+ jobject jcaller,
+ jobject formData) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (manager_) {
+ const FormData& form = form_->PullAutofillValues();
+ SendFormDataToRenderer(manager_.get(), id_, form);
+ }
+}
+
+void AutofillProviderAndroid::OnTextFieldDidChange(
+ AutofillManager* manager,
+ const FormData& form,
+ const FormFieldData& field,
+ const base::TimeTicks& timestamp) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (!ValidateManager(manager))
+ return;
+
+ if (!form_->SimilarFormAs(form))
+ return;
+
+ size_t index;
+ if (!form_->GetSimilarFieldIndex(field, &index))
+ return;
+
+ form_->OnTextFieldDidChange(index, field.value);
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
+ if (obj.is_null())
+ return;
+
+ Java_AutofillProvider_onTextFieldDidChange(env, obj, index);
+}
+
+void AutofillProviderAndroid::OnWillSubmitForm(
+ AutofillManager* manager,
+ const FormData& form,
+ const base::TimeTicks& timestamp) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (!ValidateManager(manager))
+ return;
+
+ if (!form_->SimilarFormAs(form))
+ return;
+
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
+ if (obj.is_null())
+ return;
+ Java_AutofillProvider_onWillSubmitForm(env, obj);
+}
+
+void AutofillProviderAndroid::OnFocusNoLongerOnForm(AutofillManager* manager) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (!ValidateManager(manager))
+ return;
+
+ OnFocusChanged(false, 0, RectF());
+}
+
+void AutofillProviderAndroid::OnFocusChanged(bool focus_on_form,
+ size_t index,
+ const gfx::RectF& bounding_box) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
+ if (obj.is_null())
+ return;
+
+ Java_AutofillProvider_onFocusChanged(
+ env, obj, focus_on_form, index, bounding_box.x(), bounding_box.y(),
+ bounding_box.width(), bounding_box.height());
+}
+
+void AutofillProviderAndroid::OnDidFillAutofillFormData(
+ AutofillManager* manager,
+ const FormData& form,
+ base::TimeTicks timestamp) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (manager != manager_.get() || !form_ || !form_->SimilarFormAs(form))
+ return;
+
+ for (auto field : form.fields) {
+ if (!field.is_autofilled)
+ continue;
+ OnTextFieldDidChange(manager, form, field, timestamp);
+ }
+}
+
+bool AutofillProviderAndroid::ValidateManager(AutofillManager* manager) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ bool ret = manager == manager_.get();
+ if (!ret)
+ manager_.reset();
+ return ret;
+}
+
+bool RegisterAutofillProvider(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace autofil

Powered by Google App Engine
This is Rietveld 408576698