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/navigation_entry.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/ssl_status.h"
27 #include "jni/AwAutofillClient_jni.h"
29 using base::android::AttachCurrentThread
;
30 using base::android::ConvertUTF16ToJavaString
;
31 using base::android::ScopedJavaLocalRef
;
32 using content::WebContents
;
34 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient
);
36 namespace android_webview
{
38 // Ownership: The native object is created (if autofill enabled) and owned by
39 // AwContents. The native object creates the java peer which handles most
40 // autofill functionality at the java side. The java peer is owned by Java
41 // AwContents. The native object only maintains a weak ref to it.
42 AwAutofillClient::AwAutofillClient(WebContents
* contents
)
43 : web_contents_(contents
), save_form_data_(false) {
44 JNIEnv
* env
= AttachCurrentThread();
45 ScopedJavaLocalRef
<jobject
> delegate
;
47 Java_AwAutofillClient_create(env
, reinterpret_cast<intptr_t>(this)));
49 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents_
);
50 aw_contents
->SetAwAutofillClient(delegate
.obj());
51 java_ref_
= JavaObjectWeakGlobalRef(env
, delegate
.obj());
54 AwAutofillClient::~AwAutofillClient() {
58 void AwAutofillClient::SetSaveFormData(bool enabled
) {
59 save_form_data_
= enabled
;
62 bool AwAutofillClient::GetSaveFormData() {
63 return save_form_data_
;
66 PrefService
* AwAutofillClient::GetPrefs() {
67 return user_prefs::UserPrefs::Get(
68 AwContentBrowserClient::GetAwBrowserContext());
71 IdentityProvider
* AwAutofillClient::GetIdentityProvider() {
75 rappor::RapporService
* AwAutofillClient::GetRapporService() {
79 autofill::PersonalDataManager
* AwAutofillClient::GetPersonalDataManager() {
83 scoped_refptr
<autofill::AutofillWebDataService
>
84 AwAutofillClient::GetDatabase() {
85 android_webview::AwFormDatabaseService
* service
=
86 static_cast<android_webview::AwBrowserContext
*>(
87 web_contents_
->GetBrowserContext())->GetFormDatabaseService();
88 return service
->get_autofill_webdata_service();
91 void AwAutofillClient::ShowAutofillPopup(
92 const gfx::RectF
& element_bounds
,
93 base::i18n::TextDirection text_direction
,
94 const std::vector
<autofill::Suggestion
>& suggestions
,
95 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
96 suggestions_
= suggestions
;
99 // Convert element_bounds to be in screen space.
100 gfx::Rect client_area
= web_contents_
->GetContainerBounds();
101 gfx::RectF element_bounds_in_screen_space
=
102 element_bounds
+ client_area
.OffsetFromOrigin();
104 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
105 text_direction
== base::i18n::RIGHT_TO_LEFT
,
109 void AwAutofillClient::ShowAutofillPopupImpl(
110 const gfx::RectF
& element_bounds
,
112 const std::vector
<autofill::Suggestion
>& suggestions
) {
113 JNIEnv
* env
= AttachCurrentThread();
114 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
118 // We need an array of AutofillSuggestion.
119 size_t count
= suggestions
.size();
121 ScopedJavaLocalRef
<jobjectArray
> data_array
=
122 Java_AwAutofillClient_createAutofillSuggestionArray(env
, count
);
124 for (size_t i
= 0; i
< count
; ++i
) {
125 ScopedJavaLocalRef
<jstring
> name
=
126 ConvertUTF16ToJavaString(env
, suggestions
[i
].value
);
127 ScopedJavaLocalRef
<jstring
> label
=
128 ConvertUTF16ToJavaString(env
, suggestions
[i
].label
);
129 Java_AwAutofillClient_addToAutofillSuggestionArray(
130 env
, data_array
.obj(), i
, name
.obj(), label
.obj(),
131 suggestions
[i
].frontend_id
);
134 Java_AwAutofillClient_showAutofillPopup(env
,
138 element_bounds
.width(),
139 element_bounds
.height(),
144 void AwAutofillClient::UpdateAutofillPopupDataListValues(
145 const std::vector
<base::string16
>& values
,
146 const std::vector
<base::string16
>& labels
) {
147 // Leaving as an empty method since updating autofill popup window
148 // dynamically does not seem to be a useful feature for android webview.
149 // See crrev.com/18102002 if need to implement.
152 void AwAutofillClient::HideAutofillPopup() {
153 JNIEnv
* env
= AttachCurrentThread();
154 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
158 Java_AwAutofillClient_hideAutofillPopup(env
, obj
.obj());
161 bool AwAutofillClient::IsAutocompleteEnabled() {
162 return GetSaveFormData();
165 void AwAutofillClient::PropagateAutofillPredictions(
166 content::RenderFrameHost
* rfh
,
167 const std::vector
<autofill::FormStructure
*>& forms
) {
171 void AwAutofillClient::DidFillOrPreviewField(
172 const base::string16
& autofilled_value
,
173 const base::string16
& profile_full_name
) {
176 void AwAutofillClient::OnFirstUserGestureObserved() {
180 void AwAutofillClient::LinkClicked(const GURL
& url
,
181 WindowOpenDisposition disposition
) {
185 bool AwAutofillClient::IsContextSecure(const GURL
& form_origin
) {
186 content::SSLStatus ssl_status
;
187 content::NavigationEntry
* navigation_entry
=
188 web_contents_
->GetController().GetLastCommittedEntry();
189 if (!navigation_entry
)
192 ssl_status
= navigation_entry
->GetSSL();
193 // Note: The implementation below is a copy of the one in
194 // ChromeAutofillClient::IsContextSecure, and should be kept in sync
195 // until crbug.com/505388 gets implemented.
196 return ssl_status
.security_style
==
197 content::SECURITY_STYLE_AUTHENTICATED
&&
198 ssl_status
.content_status
== content::SSLStatus::NORMAL_CONTENT
;
201 void AwAutofillClient::SuggestionSelected(JNIEnv
* env
,
205 delegate_
->DidAcceptSuggestion(suggestions_
[position
].value
,
206 suggestions_
[position
].frontend_id
,
211 void AwAutofillClient::HideRequestAutocompleteDialog() {
215 void AwAutofillClient::ShowAutofillSettings() {
219 void AwAutofillClient::ShowUnmaskPrompt(
220 const autofill::CreditCard
& card
,
221 base::WeakPtr
<autofill::CardUnmaskDelegate
> delegate
) {
225 void AwAutofillClient::OnUnmaskVerificationResult(GetRealPanResult result
) {
229 void AwAutofillClient::ConfirmSaveCreditCard(
230 const base::Closure
& save_card_callback
) {
234 bool AwAutofillClient::HasCreditCardScanFeature() {
238 void AwAutofillClient::ScanCreditCard(const CreditCardScanCallback
& callback
) {
242 void AwAutofillClient::ShowRequestAutocompleteDialog(
243 const autofill::FormData
& form
,
244 content::RenderFrameHost
* rfh
,
245 const ResultCallback
& callback
) {
249 bool RegisterAwAutofillClient(JNIEnv
* env
) {
250 return RegisterNativesImpl(env
);
253 } // namespace android_webview