1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/native/aw_autofill_client.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/aw_content_browser_client.h"
9 #include "android_webview/browser/aw_form_database_service.h"
10 #include "android_webview/browser/aw_pref_store.h"
11 #include "android_webview/native/aw_contents.h"
12 #include "base/android/jni_android.h"
13 #include "base/android/jni_string.h"
14 #include "base/android/scoped_java_ref.h"
15 #include "base/logging.h"
16 #include "base/prefs/pref_registry_simple.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/prefs/pref_service_factory.h"
19 #include "components/autofill/core/browser/autofill_popup_delegate.h"
20 #include "components/autofill/core/browser/suggestion.h"
21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
22 #include "components/autofill/core/common/autofill_pref_names.h"
23 #include "components/user_prefs/user_prefs.h"
24 #include "content/public/browser/web_contents.h"
25 #include "jni/AwAutofillClient_jni.h"
27 using base::android::AttachCurrentThread
;
28 using base::android::ConvertUTF16ToJavaString
;
29 using base::android::ScopedJavaLocalRef
;
30 using content::WebContents
;
32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient
);
34 namespace android_webview
{
36 // Ownership: The native object is created (if autofill enabled) and owned by
37 // AwContents. The native object creates the java peer which handles most
38 // autofill functionality at the java side. The java peer is owned by Java
39 // AwContents. The native object only maintains a weak ref to it.
40 AwAutofillClient::AwAutofillClient(WebContents
* contents
)
41 : web_contents_(contents
), save_form_data_(false) {
42 JNIEnv
* env
= AttachCurrentThread();
43 ScopedJavaLocalRef
<jobject
> delegate
;
45 Java_AwAutofillClient_create(env
, reinterpret_cast<intptr_t>(this)));
47 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents_
);
48 aw_contents
->SetAwAutofillClient(delegate
.obj());
49 java_ref_
= JavaObjectWeakGlobalRef(env
, delegate
.obj());
52 AwAutofillClient::~AwAutofillClient() {
56 void AwAutofillClient::SetSaveFormData(bool enabled
) {
57 save_form_data_
= enabled
;
60 bool AwAutofillClient::GetSaveFormData() {
61 return save_form_data_
;
64 PrefService
* AwAutofillClient::GetPrefs() {
65 return user_prefs::UserPrefs::Get(
66 AwContentBrowserClient::GetAwBrowserContext());
69 autofill::PersonalDataManager
* AwAutofillClient::GetPersonalDataManager() {
73 scoped_refptr
<autofill::AutofillWebDataService
>
74 AwAutofillClient::GetDatabase() {
75 android_webview::AwFormDatabaseService
* service
=
76 static_cast<android_webview::AwBrowserContext
*>(
77 web_contents_
->GetBrowserContext())->GetFormDatabaseService();
78 return service
->get_autofill_webdata_service();
81 void AwAutofillClient::ShowAutofillPopup(
82 const gfx::RectF
& element_bounds
,
83 base::i18n::TextDirection text_direction
,
84 const std::vector
<autofill::Suggestion
>& suggestions
,
85 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
86 suggestions_
= suggestions
;
89 // Convert element_bounds to be in screen space.
90 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
91 gfx::RectF element_bounds_in_screen_space
=
92 element_bounds
+ client_area
.OffsetFromOrigin();
94 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
95 text_direction
== base::i18n::RIGHT_TO_LEFT
,
99 void AwAutofillClient::ShowAutofillPopupImpl(
100 const gfx::RectF
& element_bounds
,
102 const std::vector
<autofill::Suggestion
>& suggestions
) {
103 JNIEnv
* env
= AttachCurrentThread();
104 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
108 // We need an array of AutofillSuggestion.
109 size_t count
= suggestions
.size();
111 ScopedJavaLocalRef
<jobjectArray
> data_array
=
112 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
114 for (size_t i
= 0; i
< count
; ++i
) {
115 ScopedJavaLocalRef
<jstring
> name
=
116 ConvertUTF16ToJavaString(env
, suggestions
[i
].value
);
117 ScopedJavaLocalRef
<jstring
> label
=
118 ConvertUTF16ToJavaString(env
, suggestions
[i
].label
);
119 Java_AwAutofillClient_addToAutofillSuggestionArray(
120 env
, data_array
.obj(), i
, name
.obj(), label
.obj(),
121 suggestions
[i
].frontend_id
);
124 Java_AwAutofillClient_showAutofillPopup(env
,
128 element_bounds
.width(),
129 element_bounds
.height(),
134 void AwAutofillClient::UpdateAutofillPopupDataListValues(
135 const std::vector
<base::string16
>& values
,
136 const std::vector
<base::string16
>& labels
) {
137 // Leaving as an empty method since updating autofill popup window
138 // dynamically does not seem to be a useful feature for android webview.
139 // See crrev.com/18102002 if need to implement.
142 void AwAutofillClient::HideAutofillPopup() {
143 JNIEnv
* env
= AttachCurrentThread();
144 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
148 Java_AwAutofillClient_hideAutofillPopup(env
, obj
.obj());
151 bool AwAutofillClient::IsAutocompleteEnabled() {
152 return GetSaveFormData();
155 void AwAutofillClient::DetectAccountCreationForms(
156 content::RenderFrameHost
* rfh
,
157 const std::vector
<autofill::FormStructure
*>& forms
) {
161 void AwAutofillClient::DidFillOrPreviewField(
162 const base::string16
& autofilled_value
,
163 const base::string16
& profile_full_name
) {
166 void AwAutofillClient::OnFirstUserGestureObserved() {
170 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
174 delegate_
->DidAcceptSuggestion(suggestions_
[position
].value
,
175 suggestions_
[position
].frontend_id
);
179 void AwAutofillClient::HideRequestAutocompleteDialog() {
183 void AwAutofillClient::ShowAutofillSettings() {
187 void AwAutofillClient::ShowUnmaskPrompt(
188 const autofill::CreditCard
& card
,
189 base::WeakPtr
<autofill::CardUnmaskDelegate
> delegate
) {
193 void AwAutofillClient::OnUnmaskVerificationResult(bool success
) {
197 void AwAutofillClient::ConfirmSaveCreditCard(
198 const base::Closure
& save_card_callback
) {
202 bool AwAutofillClient::HasCreditCardScanFeature() {
206 void AwAutofillClient::ScanCreditCard(const CreditCardScanCallback
& callback
) {
210 void AwAutofillClient::ShowRequestAutocompleteDialog(
211 const autofill::FormData
& form
,
212 content::RenderFrameHost
* rfh
,
213 const ResultCallback
& callback
) {
217 bool RegisterAwAutofillClient(JNIEnv
* env
) {
218 return RegisterNativesImpl(env
);
221 } // namespace android_webview