Index: components/autofill/content/browser/autofill_provider_android.cc |
diff --git a/components/autofill/content/browser/autofill_provider_android.cc b/components/autofill/content/browser/autofill_provider_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0f0b157449c39068b626e183b4c2b75c7cc27241 |
--- /dev/null |
+++ b/components/autofill/content/browser/autofill_provider_android.cc |
@@ -0,0 +1,194 @@ |
+// 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/content/browser/autofill_provider_android.h" |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "components/autofill/content/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), |
+ driver_(nullptr) { |
+ 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( |
+ ContentAutofillDriver* driver, |
+ int32_t id, |
+ const FormData& form, |
+ const FormFieldData& field, |
+ const gfx::RectF& bounding_box) { |
+ LOG(ERROR) << "bt:" << __PRETTY_FUNCTION__ << " id:" << id; |
+ // The id isn't passed to Java side because Android API guaranttes the |
+ // 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 (driver_ == driver && form_ && form_->SameFormAs(form)) { |
+ size_t index; |
+ if (form_->GetFieldIndex(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)); |
+ |
+ size_t index; |
+ if (!form_->GetFieldIndex(field, &index)) |
+ return; |
+ |
+ ScopedJavaLocalRef<jobject> formObj = form_->GetJavaPeer(); |
+ driver_ = driver; |
+ Java_AutofillProvider_queryFormFieldAutofill(env, obj, formObj, index, |
+ bounding_box.x(), bounding_box.y(), bounding_box.width(), bounding_box.height()); |
+} |
+ |
+void AutofillProviderAndroid::OnAutofillAvailable(JNIEnv*env, |
+ jobject jcaller, |
+ jobject formData) { |
+ LOG(ERROR) << "bt:" << __PRETTY_FUNCTION__; |
+ const FormData& form = form_->PullAutofillValues(); |
+ if (driver_) |
+ SendFormDataToRenderer(driver_, id_, form); |
+} |
+ |
+void AutofillProviderAndroid::OnTextFieldDidChange( |
+ ContentAutofillDriver* driver, |
+ const FormData& form, |
+ const FormFieldData& field, |
+ const base::TimeTicks& timestamp) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ LOG(ERROR) << "bt:" << __PRETTY_FUNCTION__; |
+ if (!ValidateDriver(driver)) |
+ return; |
+ |
+ if (!form_->SameFormAs(form)) { |
+ LOG(ERROR) << "bt:" << "form fields count is different"; |
+ return; |
+ } |
+ |
+ size_t index; |
+ if (!form_->GetFieldIndex(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( |
+ ContentAutofillDriver* driver, |
+ const FormData& form, |
+ const base::TimeTicks& timestamp) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ if (!ValidateDriver(driver)) |
+ return; |
+ |
+ if (!form_->SameFormAs(form)) { |
+ LOG(ERROR) << "bt:" << "Submitted form is different than current one"; |
+ return; |
+ } |
+ |
+ JNIEnv* env = AttachCurrentThread(); |
+ ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
+ if (obj.is_null()) |
+ return; |
+ Java_AutofillProvider_OnWillSubmitForm(env, obj); |
+} |
+ |
+void AutofillProviderAndroid::OnFocusNoLongerOnForm( |
+ ContentAutofillDriver* driver) { |
+ if (!ValidateDriver(driver)) |
+ return; |
+ OnFocusChanged(false, 0, RectF()); |
+} |
+ |
+void AutofillProviderAndroid::OnFocusChanged(bool focus_on_form, |
+ size_t index, |
+ const gfx::RectF& bounding_box) { |
+ 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( |
+ ContentAutofillDriver* driver, |
+ const FormData& form, |
+ base::TimeTicks timestamp) { |
+ if (driver != driver_ || !form_ || !form_->SameFormAs(form)) |
+ return; |
+ |
+ for (auto field : form.fields) { |
+ if (!field.is_autofilled) |
+ continue; |
+ OnTextFieldDidChange(driver, form, field, timestamp); |
+ } |
+} |
+ |
+void AutofillProviderAndroid::OnContentAutofillDriverDestroyed( |
+ ContentAutofillDriver* driver) { |
+ if (driver == driver_) |
+ driver_ = nullptr; |
+} |
+ |
+bool AutofillProviderAndroid::ValidateDriver(ContentAutofillDriver* driver) { |
+ bool ret = driver == driver_; |
+ if (!ret) |
+ driver_ = nullptr; |
+ return ret; |
+} |
+ |
+bool RegisterAutofillProvider(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+} // namespace autofil |