1 // Copyright 2013 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_manager_delegate.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_pref_store.h"
10 #include "android_webview/native/aw_contents.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/logging.h"
15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/prefs/pref_service_builder.h"
18 #include "components/autofill/content/browser/autocheckout/whitelist_manager.h"
19 #include "components/autofill/core/browser/autofill_popup_delegate.h"
20 #include "components/autofill/core/common/autofill_pref_names.h"
21 #include "components/user_prefs/user_prefs.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h"
24 #include "jni/AwAutofillManagerDelegate_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::AwAutofillManagerDelegate
);
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 AwAutofillManagerDelegate::AwAutofillManagerDelegate(WebContents
* contents
)
40 : web_contents_(contents
),
41 save_form_data_(false) {
42 JNIEnv
* env
= AttachCurrentThread();
43 ScopedJavaLocalRef
<jobject
> delegate
;
45 Java_AwAutofillManagerDelegate_create(env
, reinterpret_cast<jint
>(this)));
47 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents_
);
48 aw_contents
->SetAwAutofillManagerDelegate(delegate
.obj());
49 java_ref_
= JavaObjectWeakGlobalRef(env
, delegate
.obj());
52 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() {
56 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled
) {
57 save_form_data_
= enabled
;
60 bool AwAutofillManagerDelegate::GetSaveFormData() {
61 return save_form_data_
;
64 PrefService
* AwAutofillManagerDelegate::GetPrefs() {
65 return user_prefs::UserPrefs::Get(
66 AwContentBrowserClient::GetAwBrowserContext());
69 autofill::PersonalDataManager
*
70 AwAutofillManagerDelegate::GetPersonalDataManager() {
74 autofill::autocheckout::WhitelistManager
*
75 AwAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const {
79 void AwAutofillManagerDelegate::ShowAutofillPopup(
80 const gfx::RectF
& element_bounds
,
81 base::i18n::TextDirection text_direction
,
82 const std::vector
<string16
>& values
,
83 const std::vector
<string16
>& labels
,
84 const std::vector
<string16
>& icons
,
85 const std::vector
<int>& identifiers
,
86 base::WeakPtr
<autofill::AutofillPopupDelegate
> delegate
) {
89 identifiers_
= identifiers
;
92 // Convert element_bounds to be in screen space.
93 gfx::Rect client_area
;
94 web_contents_
->GetView()->GetContainerBounds(&client_area
);
95 gfx::RectF element_bounds_in_screen_space
=
96 element_bounds
+ client_area
.OffsetFromOrigin();
98 ShowAutofillPopupImpl(element_bounds_in_screen_space
,
104 void AwAutofillManagerDelegate::ShowAutofillPopupImpl(
105 const gfx::RectF
& element_bounds
,
106 const std::vector
<string16
>& values
,
107 const std::vector
<string16
>& labels
,
108 const std::vector
<int>& identifiers
) {
109 JNIEnv
* env
= AttachCurrentThread();
110 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
114 // We need an array of AutofillSuggestion.
115 size_t count
= values
.size();
117 ScopedJavaLocalRef
<jobjectArray
> data_array
=
118 Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env
, count
);
120 for (size_t i
= 0; i
< count
; ++i
) {
121 ScopedJavaLocalRef
<jstring
> name
= ConvertUTF16ToJavaString(env
, values
[i
]);
122 ScopedJavaLocalRef
<jstring
> label
=
123 ConvertUTF16ToJavaString(env
, labels
[i
]);
124 Java_AwAutofillManagerDelegate_addToAutofillSuggestionArray(
133 Java_AwAutofillManagerDelegate_showAutofillPopup(
137 element_bounds
.y(), element_bounds
.width(),
138 element_bounds
.height(), data_array
.obj());
141 void AwAutofillManagerDelegate::HideAutofillPopup() {
142 JNIEnv
* env
= AttachCurrentThread();
143 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
147 Java_AwAutofillManagerDelegate_hideAutofillPopup(env
, obj
.obj());
150 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() {
151 return GetSaveFormData();
154 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv
* env
,
158 delegate_
->DidAcceptSuggestion(values_
[position
], identifiers_
[position
]);
161 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() {
165 void AwAutofillManagerDelegate::OnAutocheckoutError() {
169 void AwAutofillManagerDelegate::OnAutocheckoutSuccess() {
173 void AwAutofillManagerDelegate::ShowAutofillSettings() {
177 void AwAutofillManagerDelegate::ConfirmSaveCreditCard(
178 const autofill::AutofillMetrics
& metric_logger
,
179 const autofill::CreditCard
& credit_card
,
180 const base::Closure
& save_card_callback
) {
184 void AwAutofillManagerDelegate::ShowAutocheckoutBubble(
185 const gfx::RectF
& bounding_box
,
187 const base::Callback
<void(autofill::AutocheckoutBubbleState
)>& callback
) {
191 void AwAutofillManagerDelegate::HideAutocheckoutBubble() {
195 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog(
196 const autofill::FormData
& form
,
197 const GURL
& source_url
,
198 autofill::DialogType dialog_type
,
199 const base::Callback
<void(const autofill::FormStructure
*,
200 const std::string
&)>& callback
) {
204 void AwAutofillManagerDelegate::AddAutocheckoutStep(
205 autofill::AutocheckoutStepType step_type
) {
209 void AwAutofillManagerDelegate::UpdateAutocheckoutStep(
210 autofill::AutocheckoutStepType step_type
,
211 autofill::AutocheckoutStepStatus step_status
) {
215 bool RegisterAwAutofillManagerDelegate(JNIEnv
* env
) {
216 return RegisterNativesImpl(env
) >= 0;
219 } // namespace android_webview