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(
98 element_bounds_in_screen_space
, values
, labels
, identifiers
);
101 void AwAutofillClient::ShowAutofillPopupImpl(
102 const gfx::RectF
& element_bounds
,
103 const std::vector
<base::string16
>& values
,
104 const std::vector
<base::string16
>& labels
,
105 const std::vector
<int>& identifiers
) {
106 JNIEnv
* env
= AttachCurrentThread();
107 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
111 // We need an array of AutofillSuggestion.
112 size_t count
= values
.size();
114 ScopedJavaLocalRef
<jobjectArray
> data_array
=
115 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
117 for (size_t i
= 0; i
< count
; ++i
) {
118 ScopedJavaLocalRef
<jstring
> name
= ConvertUTF16ToJavaString(env
, values
[i
]);
119 ScopedJavaLocalRef
<jstring
> label
=
120 ConvertUTF16ToJavaString(env
, labels
[i
]);
121 Java_AwAutofillClient_addToAutofillSuggestionArray(
122 env
, data_array
.obj(), i
, name
.obj(), label
.obj(), identifiers
[i
]);
125 Java_AwAutofillClient_showAutofillPopup(env
,
129 element_bounds
.width(),
130 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 const std::vector
<autofill::FormStructure
*>& forms
) {
159 void AwAutofillClient::DidFillOrPreviewField(
160 const base::string16
& autofilled_value
,
161 const base::string16
& profile_full_name
) {
164 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
168 delegate_
->DidAcceptSuggestion(values_
[position
], identifiers_
[position
]);
171 void AwAutofillClient::HideRequestAutocompleteDialog() {
175 void AwAutofillClient::ShowAutofillSettings() {
179 void AwAutofillClient::ConfirmSaveCreditCard(
180 const autofill::AutofillMetrics
& metric_logger
,
181 const base::Closure
& save_card_callback
) {
185 void AwAutofillClient::ShowRequestAutocompleteDialog(
186 const autofill::FormData
& form
,
187 const GURL
& source_url
,
188 const ResultCallback
& callback
) {
192 bool RegisterAwAutofillClient(JNIEnv
* env
) {
193 return RegisterNativesImpl(env
);
196 } // namespace android_webview