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/webdata/autofill_webdata_service.h"
21 #include "components/autofill/core/common/autofill_pref_names.h"
22 #include "components/user_prefs/user_prefs.h"
23 #include "content/public/browser/web_contents.h"
24 #include "jni/AwAutofillClient_jni.h"
26 using base::android::AttachCurrentThread
;
27 using base::android::ConvertUTF16ToJavaString
;
28 using base::android::ScopedJavaLocalRef
;
29 using content::WebContents
;
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient
);
33 namespace android_webview
{
35 // Ownership: The native object is created (if autofill enabled) and owned by
36 // AwContents. The native object creates the java peer which handles most
37 // autofill functionality at the java side. The java peer is owned by Java
38 // AwContents. The native object only maintains a weak ref to it.
39 AwAutofillClient::AwAutofillClient(WebContents
* contents
)
40 : web_contents_(contents
), save_form_data_(false) {
41 JNIEnv
* env
= AttachCurrentThread();
42 ScopedJavaLocalRef
<jobject
> delegate
;
44 Java_AwAutofillClient_create(env
, reinterpret_cast<intptr_t>(this)));
46 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents_
);
47 aw_contents
->SetAwAutofillClient(delegate
.obj());
48 java_ref_
= JavaObjectWeakGlobalRef(env
, delegate
.obj());
51 AwAutofillClient::~AwAutofillClient() {
55 void AwAutofillClient::SetSaveFormData(bool enabled
) {
56 save_form_data_
= enabled
;
59 bool AwAutofillClient::GetSaveFormData() {
60 return save_form_data_
;
63 PrefService
* AwAutofillClient::GetPrefs() {
64 return user_prefs::UserPrefs::Get(
65 AwContentBrowserClient::GetAwBrowserContext());
68 autofill::PersonalDataManager
* AwAutofillClient::GetPersonalDataManager() {
72 scoped_refptr
<autofill::AutofillWebDataService
>
73 AwAutofillClient::GetDatabase() {
74 android_webview::AwFormDatabaseService
* service
=
75 static_cast<android_webview::AwBrowserContext
*>(
76 web_contents_
->GetBrowserContext())->GetFormDatabaseService();
77 return service
->get_autofill_webdata_service();
80 void AwAutofillClient::ShowAutofillPopup(
81 const gfx::RectF
& element_bounds
,
82 base::i18n::TextDirection text_direction
,
83 const std::vector
<base::string16
>& values
,
84 const std::vector
<base::string16
>& labels
,
85 const std::vector
<base::string16
>& icons
,
86 const std::vector
<int>& identifiers
,
87 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
89 identifiers_
= identifiers
;
92 // Convert element_bounds to be in screen space.
93 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
94 gfx::RectF element_bounds_in_screen_space
=
95 element_bounds
+ client_area
.OffsetFromOrigin();
97 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
98 text_direction
== base::i18n::RIGHT_TO_LEFT
,
104 void AwAutofillClient::ShowAutofillPopupImpl(
105 const gfx::RectF
& element_bounds
,
107 const std::vector
<base::string16
>& values
,
108 const std::vector
<base::string16
>& labels
,
109 const std::vector
<int>& identifiers
) {
110 JNIEnv
* env
= AttachCurrentThread();
111 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
115 // We need an array of AutofillSuggestion.
116 size_t count
= values
.size();
118 ScopedJavaLocalRef
<jobjectArray
> data_array
=
119 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
121 for (size_t i
= 0; i
< count
; ++i
) {
122 ScopedJavaLocalRef
<jstring
> name
= ConvertUTF16ToJavaString(env
, values
[i
]);
123 ScopedJavaLocalRef
<jstring
> label
=
124 ConvertUTF16ToJavaString(env
, labels
[i
]);
125 Java_AwAutofillClient_addToAutofillSuggestionArray(
126 env
, data_array
.obj(), i
, name
.obj(), label
.obj(), identifiers
[i
]);
129 Java_AwAutofillClient_showAutofillPopup(env
,
133 element_bounds
.width(),
134 element_bounds
.height(),
139 void AwAutofillClient::UpdateAutofillPopupDataListValues(
140 const std::vector
<base::string16
>& values
,
141 const std::vector
<base::string16
>& labels
) {
142 // Leaving as an empty method since updating autofill popup window
143 // dynamically does not seem to be a useful feature for android webview.
144 // See crrev.com/18102002 if need to implement.
147 void AwAutofillClient::HideAutofillPopup() {
148 JNIEnv
* env
= AttachCurrentThread();
149 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
153 Java_AwAutofillClient_hideAutofillPopup(env
, obj
.obj());
156 bool AwAutofillClient::IsAutocompleteEnabled() {
157 return GetSaveFormData();
160 void AwAutofillClient::DetectAccountCreationForms(
161 const std::vector
<autofill::FormStructure
*>& forms
) {
164 void AwAutofillClient::DidFillOrPreviewField(
165 const base::string16
& autofilled_value
,
166 const base::string16
& profile_full_name
) {
169 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
173 delegate_
->DidAcceptSuggestion(values_
[position
], identifiers_
[position
]);
176 void AwAutofillClient::HideRequestAutocompleteDialog() {
180 void AwAutofillClient::ShowAutofillSettings() {
184 void AwAutofillClient::ConfirmSaveCreditCard(
185 const autofill::AutofillMetrics
& metric_logger
,
186 const base::Closure
& save_card_callback
) {
190 void AwAutofillClient::ShowRequestAutocompleteDialog(
191 const autofill::FormData
& form
,
192 const GURL
& source_url
,
193 const ResultCallback
& callback
) {
197 bool RegisterAwAutofillClient(JNIEnv
* env
) {
198 return RegisterNativesImpl(env
);
201 } // namespace android_webview