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/core/browser/autofill_popup_delegate.h"
19 #include "components/autofill/core/common/autofill_pref_names.h"
20 #include "components/user_prefs/user_prefs.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_view.h"
23 #include "jni/AwAutofillManagerDelegate_jni.h"
25 using base::android::AttachCurrentThread;
26 using base::android::ConvertUTF16ToJavaString;
27 using base::android::ScopedJavaLocalRef;
28 using content::WebContents;
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillManagerDelegate);
32 namespace android_webview {
34 // Ownership: The native object is created (if autofill enabled) and owned by
35 // AwContents. The native object creates the java peer which handles most
36 // autofill functionality at the java side. The java peer is owned by Java
37 // AwContents. The native object only maintains a weak ref to it.
38 AwAutofillManagerDelegate::AwAutofillManagerDelegate(WebContents* contents)
39 : web_contents_(contents),
40 save_form_data_(false) {
41 JNIEnv* env = AttachCurrentThread();
42 ScopedJavaLocalRef<jobject> delegate;
44 Java_AwAutofillManagerDelegate_create(env, reinterpret_cast<jint>(this)));
46 AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
47 aw_contents->SetAwAutofillManagerDelegate(delegate.obj());
48 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
51 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() {
55 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled) {
56 save_form_data_ = enabled;
59 bool AwAutofillManagerDelegate::GetSaveFormData() {
60 return save_form_data_;
63 PrefService* AwAutofillManagerDelegate::GetPrefs() {
64 return user_prefs::UserPrefs::Get(
65 AwContentBrowserClient::GetAwBrowserContext());
68 autofill::PersonalDataManager*
69 AwAutofillManagerDelegate::GetPersonalDataManager() {
73 void AwAutofillManagerDelegate::ShowAutofillPopup(
74 const gfx::RectF& element_bounds,
75 base::i18n::TextDirection text_direction,
76 const std::vector<string16>& values,
77 const std::vector<string16>& labels,
78 const std::vector<string16>& icons,
79 const std::vector<int>& identifiers,
80 base::WeakPtr<autofill::AutofillPopupDelegate> delegate) {
83 identifiers_ = identifiers;
86 // Convert element_bounds to be in screen space.
87 gfx::Rect client_area;
88 web_contents_->GetView()->GetContainerBounds(&client_area);
89 gfx::RectF element_bounds_in_screen_space =
90 element_bounds + client_area.OffsetFromOrigin();
92 ShowAutofillPopupImpl(element_bounds_in_screen_space,
98 void AwAutofillManagerDelegate::ShowAutofillPopupImpl(
99 const gfx::RectF& element_bounds,
100 const std::vector<string16>& values,
101 const std::vector<string16>& labels,
102 const std::vector<int>& identifiers) {
103 JNIEnv* env = AttachCurrentThread();
104 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
108 // We need an array of AutofillSuggestion.
109 size_t count = values.size();
111 ScopedJavaLocalRef<jobjectArray> data_array =
112 Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env, count);
114 for (size_t i = 0; i < count; ++i) {
115 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]);
116 ScopedJavaLocalRef<jstring> label =
117 ConvertUTF16ToJavaString(env, labels[i]);
118 Java_AwAutofillManagerDelegate_addToAutofillSuggestionArray(
127 Java_AwAutofillManagerDelegate_showAutofillPopup(
131 element_bounds.y(), element_bounds.width(),
132 element_bounds.height(), data_array.obj());
135 void AwAutofillManagerDelegate::UpdateAutofillPopupDataListValues(
136 const std::vector<base::string16>& values,
137 const std::vector<base::string16>& labels) {
138 // Leaving as an empty method since updating autofill popup window
139 // dynamically does not seem to be a useful feature for android webview.
140 // See crrev.com/18102002 if need to implement.
143 void AwAutofillManagerDelegate::HideAutofillPopup() {
144 JNIEnv* env = AttachCurrentThread();
145 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
149 Java_AwAutofillManagerDelegate_hideAutofillPopup(env, obj.obj());
152 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() {
153 return GetSaveFormData();
156 void AwAutofillManagerDelegate::DetectAccountCreationForms(
157 const std::vector<autofill::FormStructure*>& forms) {}
159 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv* env,
163 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
166 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() {
170 void AwAutofillManagerDelegate::ShowAutofillSettings() {
174 void AwAutofillManagerDelegate::ConfirmSaveCreditCard(
175 const autofill::AutofillMetrics& metric_logger,
176 const autofill::CreditCard& credit_card,
177 const base::Closure& save_card_callback) {
181 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog(
182 const autofill::FormData& form,
183 const GURL& source_url,
184 const base::Callback<void(const autofill::FormStructure*)>& callback) {
188 bool RegisterAwAutofillManagerDelegate(JNIEnv* env) {
189 return RegisterNativesImpl(env) >= 0;
192 } // namespace android_webview