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 IdentityProvider
* AwAutofillClient::GetIdentityProvider() {
73 autofill::PersonalDataManager
* AwAutofillClient::GetPersonalDataManager() {
77 scoped_refptr
<autofill::AutofillWebDataService
>
78 AwAutofillClient::GetDatabase() {
79 android_webview::AwFormDatabaseService
* service
=
80 static_cast<android_webview::AwBrowserContext
*>(
81 web_contents_
->GetBrowserContext())->GetFormDatabaseService();
82 return service
->get_autofill_webdata_service();
85 void AwAutofillClient::ShowAutofillPopup(
86 const gfx::RectF
& element_bounds
,
87 base::i18n::TextDirection text_direction
,
88 const std::vector
<autofill::Suggestion
>& suggestions
,
89 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
90 suggestions_
= suggestions
;
93 // Convert element_bounds to be in screen space.
94 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
95 gfx::RectF element_bounds_in_screen_space
=
96 element_bounds
+ client_area
.OffsetFromOrigin();
98 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
99 text_direction
== base::i18n::RIGHT_TO_LEFT
,
103 void AwAutofillClient::ShowAutofillPopupImpl(
104 const gfx::RectF
& element_bounds
,
106 const std::vector
<autofill::Suggestion
>& suggestions
) {
107 JNIEnv
* env
= AttachCurrentThread();
108 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
112 // We need an array of AutofillSuggestion.
113 size_t count
= suggestions
.size();
115 ScopedJavaLocalRef
<jobjectArray
> data_array
=
116 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
118 for (size_t i
= 0; i
< count
; ++i
) {
119 ScopedJavaLocalRef
<jstring
> name
=
120 ConvertUTF16ToJavaString(env
, suggestions
[i
].value
);
121 ScopedJavaLocalRef
<jstring
> label
=
122 ConvertUTF16ToJavaString(env
, suggestions
[i
].label
);
123 Java_AwAutofillClient_addToAutofillSuggestionArray(
124 env
, data_array
.obj(), i
, name
.obj(), label
.obj(),
125 suggestions
[i
].frontend_id
);
128 Java_AwAutofillClient_showAutofillPopup(env
,
132 element_bounds
.width(),
133 element_bounds
.height(),
138 void AwAutofillClient::UpdateAutofillPopupDataListValues(
139 const std::vector
<base::string16
>& values
,
140 const std::vector
<base::string16
>& labels
) {
141 // Leaving as an empty method since updating autofill popup window
142 // dynamically does not seem to be a useful feature for android webview.
143 // See crrev.com/18102002 if need to implement.
146 void AwAutofillClient::HideAutofillPopup() {
147 JNIEnv
* env
= AttachCurrentThread();
148 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
152 Java_AwAutofillClient_hideAutofillPopup(env
, obj
.obj());
155 bool AwAutofillClient::IsAutocompleteEnabled() {
156 return GetSaveFormData();
159 void AwAutofillClient::DetectAccountCreationForms(
160 content::RenderFrameHost
* rfh
,
161 const std::vector
<autofill::FormStructure
*>& forms
) {
165 void AwAutofillClient::DidFillOrPreviewField(
166 const base::string16
& autofilled_value
,
167 const base::string16
& profile_full_name
) {
170 void AwAutofillClient::OnFirstUserGestureObserved() {
174 void AwAutofillClient::LinkClicked(const GURL
& url
,
175 WindowOpenDisposition disposition
) {
179 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
183 delegate_
->DidAcceptSuggestion(suggestions_
[position
].value
,
184 suggestions_
[position
].frontend_id
);
188 void AwAutofillClient::HideRequestAutocompleteDialog() {
192 void AwAutofillClient::ShowAutofillSettings() {
196 void AwAutofillClient::ShowUnmaskPrompt(
197 const autofill::CreditCard
& card
,
198 base::WeakPtr
<autofill::CardUnmaskDelegate
> delegate
) {
202 void AwAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result
) {
206 void AwAutofillClient::ConfirmSaveCreditCard(
207 const base::Closure
& save_card_callback
) {
211 bool AwAutofillClient::HasCreditCardScanFeature() {
215 void AwAutofillClient::ScanCreditCard(const CreditCardScanCallback
& callback
) {
219 void AwAutofillClient::ShowRequestAutocompleteDialog(
220 const autofill::FormData
& form
,
221 content::RenderFrameHost
* rfh
,
222 const ResultCallback
& callback
) {
226 bool RegisterAwAutofillClient(JNIEnv
* env
) {
227 return RegisterNativesImpl(env
);
230 } // namespace android_webview