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 rappor::RapporService
* AwAutofillClient::GetRapporService() {
77 autofill::PersonalDataManager
* AwAutofillClient::GetPersonalDataManager() {
81 scoped_refptr
<autofill::AutofillWebDataService
>
82 AwAutofillClient::GetDatabase() {
83 android_webview::AwFormDatabaseService
* service
=
84 static_cast<android_webview::AwBrowserContext
*>(
85 web_contents_
->GetBrowserContext())->GetFormDatabaseService();
86 return service
->get_autofill_webdata_service();
89 void AwAutofillClient::ShowAutofillPopup(
90 const gfx::RectF
& element_bounds
,
91 base::i18n::TextDirection text_direction
,
92 const std::vector
<autofill::Suggestion
>& suggestions
,
93 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
94 suggestions_
= suggestions
;
97 // Convert element_bounds to be in screen space.
98 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
99 gfx::RectF element_bounds_in_screen_space
=
100 element_bounds
+ client_area
.OffsetFromOrigin();
102 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
103 text_direction
== base::i18n::RIGHT_TO_LEFT
,
107 void AwAutofillClient::ShowAutofillPopupImpl(
108 const gfx::RectF
& element_bounds
,
110 const std::vector
<autofill::Suggestion
>& suggestions
) {
111 JNIEnv
* env
= AttachCurrentThread();
112 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
116 // We need an array of AutofillSuggestion.
117 size_t count
= suggestions
.size();
119 ScopedJavaLocalRef
<jobjectArray
> data_array
=
120 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
122 for (size_t i
= 0; i
< count
; ++i
) {
123 ScopedJavaLocalRef
<jstring
> name
=
124 ConvertUTF16ToJavaString(env
, suggestions
[i
].value
);
125 ScopedJavaLocalRef
<jstring
> label
=
126 ConvertUTF16ToJavaString(env
, suggestions
[i
].label
);
127 Java_AwAutofillClient_addToAutofillSuggestionArray(
128 env
, data_array
.obj(), i
, name
.obj(), label
.obj(),
129 suggestions
[i
].frontend_id
);
132 Java_AwAutofillClient_showAutofillPopup(env
,
136 element_bounds
.width(),
137 element_bounds
.height(),
142 void AwAutofillClient::UpdateAutofillPopupDataListValues(
143 const std::vector
<base::string16
>& values
,
144 const std::vector
<base::string16
>& labels
) {
145 // Leaving as an empty method since updating autofill popup window
146 // dynamically does not seem to be a useful feature for android webview.
147 // See crrev.com/18102002 if need to implement.
150 void AwAutofillClient::HideAutofillPopup() {
151 JNIEnv
* env
= AttachCurrentThread();
152 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
156 Java_AwAutofillClient_hideAutofillPopup(env
, obj
.obj());
159 bool AwAutofillClient::IsAutocompleteEnabled() {
160 return GetSaveFormData();
163 void AwAutofillClient::PropagateAutofillPredictions(
164 content::RenderFrameHost
* rfh
,
165 const std::vector
<autofill::FormStructure
*>& forms
) {
169 void AwAutofillClient::DidFillOrPreviewField(
170 const base::string16
& autofilled_value
,
171 const base::string16
& profile_full_name
) {
174 void AwAutofillClient::OnFirstUserGestureObserved() {
178 void AwAutofillClient::LinkClicked(const GURL
& url
,
179 WindowOpenDisposition disposition
) {
183 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
187 delegate_
->DidAcceptSuggestion(suggestions_
[position
].value
,
188 suggestions_
[position
].frontend_id
);
192 void AwAutofillClient::HideRequestAutocompleteDialog() {
196 void AwAutofillClient::ShowAutofillSettings() {
200 void AwAutofillClient::ShowUnmaskPrompt(
201 const autofill::CreditCard
& card
,
202 base::WeakPtr
<autofill::CardUnmaskDelegate
> delegate
) {
206 void AwAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result
) {
210 void AwAutofillClient::ConfirmSaveCreditCard(
211 const base::Closure
& save_card_callback
) {
215 bool AwAutofillClient::HasCreditCardScanFeature() {
219 void AwAutofillClient::ScanCreditCard(const CreditCardScanCallback
& callback
) {
223 void AwAutofillClient::ShowRequestAutocompleteDialog(
224 const autofill::FormData
& form
,
225 content::RenderFrameHost
* rfh
,
226 const ResultCallback
& callback
) {
230 bool RegisterAwAutofillClient(JNIEnv
* env
) {
231 return RegisterNativesImpl(env
);
234 } // namespace android_webview