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.
4
5 #include "android_webview/native/aw_autofill_client.h"
6
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/webdata/autofill_webdata_service.h"
21 #include "components/autofill/core/common/autofill_pref_names.h"
22 #include "components/user_prefs/user_prefs.h"
23 #include "content/public/browser/web_contents.h"
24 #include "jni/AwAutofillClient_jni.h"
25
26 using base::android::AttachCurrentThread;
27 using base::android::ConvertUTF16ToJavaString;
28 using base::android::ScopedJavaLocalRef;
29 using content::WebContents;
30
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient);
32
33 namespace android_webview {
34
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.
AwAutofillClient(WebContents * contents)39 AwAutofillClient::AwAutofillClient(WebContents* contents)
40 : web_contents_(contents), save_form_data_(false) {
41 JNIEnv* env = AttachCurrentThread();
42 ScopedJavaLocalRef<jobject> delegate;
43 delegate.Reset(
44 Java_AwAutofillClient_create(env, reinterpret_cast<intptr_t>(this)));
45
46 AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
47 aw_contents->SetAwAutofillClient(delegate.obj());
48 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
49 }
50
~AwAutofillClient()51 AwAutofillClient::~AwAutofillClient() {
52 HideAutofillPopup();
53 }
54
SetSaveFormData(bool enabled)55 void AwAutofillClient::SetSaveFormData(bool enabled) {
56 save_form_data_ = enabled;
57 }
58
GetSaveFormData()59 bool AwAutofillClient::GetSaveFormData() {
60 return save_form_data_;
61 }
62
GetPrefs()63 PrefService* AwAutofillClient::GetPrefs() {
64 return user_prefs::UserPrefs::Get(
65 AwContentBrowserClient::GetAwBrowserContext());
66 }
67
GetPersonalDataManager()68 autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() {
69 return NULL;
70 }
71
72 scoped_refptr<autofill::AutofillWebDataService>
GetDatabase()73 AwAutofillClient::GetDatabase() {
74 android_webview::AwFormDatabaseService* service =
75 static_cast<android_webview::AwBrowserContext*>(
76 web_contents_->GetBrowserContext())->GetFormDatabaseService();
77 return service->get_autofill_webdata_service();
78 }
79
ShowAutofillPopup(const gfx::RectF & element_bounds,base::i18n::TextDirection text_direction,const std::vector<base::string16> & values,const std::vector<base::string16> & labels,const std::vector<base::string16> & icons,const std::vector<int> & identifiers,base::WeakPtr<autofill::AutofillPopupDelegate> delegate)80 void AwAutofillClient::ShowAutofillPopup(
81 const gfx::RectF& element_bounds,
82 base::i18n::TextDirection text_direction,
83 const std::vector<base::string16>& values,
84 const std::vector<base::string16>& labels,
85 const std::vector<base::string16>& icons,
86 const std::vector<int>& identifiers,
87 base::WeakPtr<autofill::AutofillPopupDelegate> delegate) {
88 values_ = values;
89 identifiers_ = identifiers;
90 delegate_ = delegate;
91
92 // Convert element_bounds to be in screen space.
93 gfx::Rect client_area = web_contents_->GetContainerBounds();
94 gfx::RectF element_bounds_in_screen_space =
95 element_bounds + client_area.OffsetFromOrigin();
96
97 ShowAutofillPopupImpl(
98 element_bounds_in_screen_space, values, labels, identifiers);
99 }
100
ShowAutofillPopupImpl(const gfx::RectF & element_bounds,const std::vector<base::string16> & values,const std::vector<base::string16> & labels,const std::vector<int> & identifiers)101 void AwAutofillClient::ShowAutofillPopupImpl(
102 const gfx::RectF& element_bounds,
103 const std::vector<base::string16>& values,
104 const std::vector<base::string16>& labels,
105 const std::vector<int>& identifiers) {
106 JNIEnv* env = AttachCurrentThread();
107 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
108 if (obj.is_null())
109 return;
110
111 // We need an array of AutofillSuggestion.
112 size_t count = values.size();
113
114 ScopedJavaLocalRef<jobjectArray> data_array =
115 Java_AwAutofillClient_createAutofillSuggestionArray(env, count);
116
117 for (size_t i = 0; i < count; ++i) {
118 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]);
119 ScopedJavaLocalRef<jstring> label =
120 ConvertUTF16ToJavaString(env, labels[i]);
121 Java_AwAutofillClient_addToAutofillSuggestionArray(
122 env, data_array.obj(), i, name.obj(), label.obj(), identifiers[i]);
123 }
124
125 Java_AwAutofillClient_showAutofillPopup(env,
126 obj.obj(),
127 element_bounds.x(),
128 element_bounds.y(),
129 element_bounds.width(),
130 element_bounds.height(),
131 data_array.obj());
132 }
133
UpdateAutofillPopupDataListValues(const std::vector<base::string16> & values,const std::vector<base::string16> & labels)134 void AwAutofillClient::UpdateAutofillPopupDataListValues(
135 const std::vector<base::string16>& values,
136 const std::vector<base::string16>& labels) {
137 // Leaving as an empty method since updating autofill popup window
138 // dynamically does not seem to be a useful feature for android webview.
139 // See crrev.com/18102002 if need to implement.
140 }
141
HideAutofillPopup()142 void AwAutofillClient::HideAutofillPopup() {
143 JNIEnv* env = AttachCurrentThread();
144 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
145 if (obj.is_null())
146 return;
147 delegate_.reset();
148 Java_AwAutofillClient_hideAutofillPopup(env, obj.obj());
149 }
150
IsAutocompleteEnabled()151 bool AwAutofillClient::IsAutocompleteEnabled() {
152 return GetSaveFormData();
153 }
154
DetectAccountCreationForms(const std::vector<autofill::FormStructure * > & forms)155 void AwAutofillClient::DetectAccountCreationForms(
156 const std::vector<autofill::FormStructure*>& forms) {
157 }
158
DidFillOrPreviewField(const base::string16 & autofilled_value,const base::string16 & profile_full_name)159 void AwAutofillClient::DidFillOrPreviewField(
160 const base::string16& autofilled_value,
161 const base::string16& profile_full_name) {
162 }
163
SuggestionSelected(JNIEnv * env,jobject object,jint position)164 void AwAutofillClient::SuggestionSelected(JNIEnv* env,
165 jobject object,
166 jint position) {
167 if (delegate_)
168 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
169 }
170
HideRequestAutocompleteDialog()171 void AwAutofillClient::HideRequestAutocompleteDialog() {
172 NOTIMPLEMENTED();
173 }
174
ShowAutofillSettings()175 void AwAutofillClient::ShowAutofillSettings() {
176 NOTIMPLEMENTED();
177 }
178
ConfirmSaveCreditCard(const autofill::AutofillMetrics & metric_logger,const base::Closure & save_card_callback)179 void AwAutofillClient::ConfirmSaveCreditCard(
180 const autofill::AutofillMetrics& metric_logger,
181 const base::Closure& save_card_callback) {
182 NOTIMPLEMENTED();
183 }
184
ShowRequestAutocompleteDialog(const autofill::FormData & form,const GURL & source_url,const ResultCallback & callback)185 void AwAutofillClient::ShowRequestAutocompleteDialog(
186 const autofill::FormData& form,
187 const GURL& source_url,
188 const ResultCallback& callback) {
189 NOTIMPLEMENTED();
190 }
191
RegisterAwAutofillClient(JNIEnv * env)192 bool RegisterAwAutofillClient(JNIEnv* env) {
193 return RegisterNativesImpl(env);
194 }
195
196 } // namespace android_webview
197