Chromium Code Reviews| Index: components/autofill/android/autofill_provider_android.cc |
| diff --git a/components/autofill/android/autofill_provider_android.cc b/components/autofill/android/autofill_provider_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..377565cf765349831a8500acad85eda12c3868da |
| --- /dev/null |
| +++ b/components/autofill/android/autofill_provider_android.cc |
| @@ -0,0 +1,200 @@ |
| +// 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/android/autofill_provider_android.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "components/autofill/android/form_data_android.h" |
| +#include "components/autofill/core/browser/autofill_handler_proxy.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/web_contents.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::JavaRef; |
| +using base::android::ScopedJavaLocalRef; |
| +using content::BrowserThread; |
| +using content::WebContents; |
| +using gfx::RectF; |
| + |
| +namespace autofill { |
| + |
| +const int kInvalidRequestId = -1; |
| + |
| +AutofillProviderAndroid::AutofillProviderAndroid( |
| + const JavaRef<jobject>& jcaller, |
| + content::WebContents* web_contents) |
| + : id_(kInvalidRequestId), web_contents_(web_contents) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + java_ref_ = JavaObjectWeakGlobalRef(env, jcaller); |
| + Java_AutofillProvider_setNativeAutofillProvider( |
| + env, jcaller, reinterpret_cast<jlong>(this)); |
| +} |
| + |
| +AutofillProviderAndroid::~AutofillProviderAndroid() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (obj.is_null()) |
| + return; |
| + |
|
Mathieu
2017/05/24 18:02:01
// Remove the reference to this object on the Java
michaelbai
2017/05/26 23:12:06
Done.
|
| + Java_AutofillProvider_setNativeAutofillProvider(env, obj, 0); |
| +} |
| + |
| +void AutofillProviderAndroid::OnQueryFormFieldAutofill( |
| + AutofillHandlerProxy* handler, |
| + 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 guarantees the |
| + // response is always for current session, so we just use the current id |
| + // in response, see OnAutofillAvailable. |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
|
Mathieu
2017/05/24 18:02:01
From their sample code, is it typical to query And
michaelbai
2017/05/24 20:57:54
This isn't synchronous query and pretty safe to ca
|
| + id_ = id; |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (obj.is_null()) |
| + return; |
| + |
| + form_ = base::MakeUnique<FormDataAndroid>(form); |
| + |
| + size_t index; |
| + if (!form_->GetFieldIndex(field, &index)) |
| + return; |
| + |
| + gfx::Rect client_area = web_contents_->GetContainerBounds(); |
| + gfx::RectF transformed_bounding = |
| + bounding_box + client_area.OffsetFromOrigin(); |
| + |
| + ScopedJavaLocalRef<jobject> formObj = form_->GetJavaPeer(); |
|
Mathieu
2017/05/24 18:02:01
hacker case: form_obj
michaelbai
2017/05/26 23:12:07
Done.
|
| + handler_ = handler->GetWeakPtr(); |
| + Java_AutofillProvider_startAutofillSession( |
| + env, obj, formObj, index, transformed_bounding.x(), |
| + transformed_bounding.y(), transformed_bounding.width(), |
| + transformed_bounding.height()); |
| +} |
| + |
| +void AutofillProviderAndroid::OnAutofillAvailable(JNIEnv* env, |
| + jobject jcaller, |
| + jobject formData) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (handler_) { |
| + const FormData& form = form_->PullAutofillValues(); |
| + SendFormDataToRenderer(handler_.get(), id_, form); |
| + } |
| +} |
| + |
| +void AutofillProviderAndroid::OnTextFieldDidChange( |
| + AutofillHandlerProxy* handler, |
| + const FormData& form, |
| + const FormFieldData& field, |
| + const base::TimeTicks& timestamp) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (!ValidateHandler(handler)) |
|
Mathieu
2017/05/24 18:02:01
This function would benefit from high-level commen
michaelbai
2017/05/26 23:12:06
Could you suggest the comments? from my aspect, th
|
| + return; |
| + |
| + if (!form_->SimilarFormAs(form)) |
|
Mathieu
2017/05/24 18:02:01
can you combine those in a big if statement for th
michaelbai
2017/05/26 23:12:06
Done.
|
| + 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( |
| + AutofillHandlerProxy* handler, |
| + const FormData& form, |
| + const base::TimeTicks& timestamp) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (!ValidateHandler(handler)) |
| + 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( |
| + AutofillHandlerProxy* handler) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (!ValidateHandler(handler)) |
| + 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( |
| + AutofillHandlerProxy* handler, |
| + const FormData& form, |
| + base::TimeTicks timestamp) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (handler != handler_.get() || !form_ || !form_->SimilarFormAs(form)) |
| + return; |
| + |
| + for (auto field : form.fields) { |
|
Mathieu
2017/05/24 18:02:01
const FormFieldData& ?
michaelbai
2017/05/24 20:57:54
Interesting, do we have any guideline about when a
michaelbai
2017/05/26 23:12:06
OK, I found something about auto here https://goog
|
| + if (!field.is_autofilled) |
| + continue; |
| + OnTextFieldDidChange(handler, form, field, timestamp); |
| + } |
| +} |
| + |
| +void AutofillProviderAndroid::Reset(AutofillHandlerProxy* handler) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (handler == handler_.get()) { |
| + handler_.reset(); |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| + if (obj.is_null()) |
| + return; |
| + |
| + Java_AutofillProvider_reset(env, obj); |
| + } |
| +} |
| + |
| +bool AutofillProviderAndroid::ValidateHandler(AutofillHandlerProxy* handler) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + bool ret = handler == handler_.get(); |
| + if (!ret) |
| + handler_.reset(); |
| + return ret; |
| +} |
| + |
| +bool RegisterAutofillProvider(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace autofill |