Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / android_webview / native / aw_autofill_client.cc
blob246008847a9845d79a4ec815c4dd8bd7e9cbac16
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/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"
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::AwAutofillClient);
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 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)));
46 AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
47 aw_contents->SetAwAutofillClient(delegate.obj());
48 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
51 AwAutofillClient::~AwAutofillClient() {
52 HideAutofillPopup();
55 void AwAutofillClient::SetSaveFormData(bool enabled) {
56 save_form_data_ = enabled;
59 bool AwAutofillClient::GetSaveFormData() {
60 return save_form_data_;
63 PrefService* AwAutofillClient::GetPrefs() {
64 return user_prefs::UserPrefs::Get(
65 AwContentBrowserClient::GetAwBrowserContext());
68 autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() {
69 return NULL;
72 scoped_refptr<autofill::AutofillWebDataService>
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();
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;
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();
97 ShowAutofillPopupImpl(element_bounds_in_screen_space,
98 text_direction == base::i18n::RIGHT_TO_LEFT,
99 values,
100 labels,
101 identifiers);
104 void AwAutofillClient::ShowAutofillPopupImpl(
105 const gfx::RectF& element_bounds,
106 bool is_rtl,
107 const std::vector<base::string16>& values,
108 const std::vector<base::string16>& labels,
109 const std::vector<int>& identifiers) {
110 JNIEnv* env = AttachCurrentThread();
111 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
112 if (obj.is_null())
113 return;
115 // We need an array of AutofillSuggestion.
116 size_t count = values.size();
118 ScopedJavaLocalRef<jobjectArray> data_array =
119 Java_AwAutofillClient_createAutofillSuggestionArray(env, count);
121 for (size_t i = 0; i < count; ++i) {
122 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]);
123 ScopedJavaLocalRef<jstring> label =
124 ConvertUTF16ToJavaString(env, labels[i]);
125 Java_AwAutofillClient_addToAutofillSuggestionArray(
126 env, data_array.obj(), i, name.obj(), label.obj(), identifiers[i]);
129 Java_AwAutofillClient_showAutofillPopup(env,
130 obj.obj(),
131 element_bounds.x(),
132 element_bounds.y(),
133 element_bounds.width(),
134 element_bounds.height(),
135 is_rtl,
136 data_array.obj());
139 void AwAutofillClient::UpdateAutofillPopupDataListValues(
140 const std::vector<base::string16>& values,
141 const std::vector<base::string16>& labels) {
142 // Leaving as an empty method since updating autofill popup window
143 // dynamically does not seem to be a useful feature for android webview.
144 // See crrev.com/18102002 if need to implement.
147 void AwAutofillClient::HideAutofillPopup() {
148 JNIEnv* env = AttachCurrentThread();
149 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
150 if (obj.is_null())
151 return;
152 delegate_.reset();
153 Java_AwAutofillClient_hideAutofillPopup(env, obj.obj());
156 bool AwAutofillClient::IsAutocompleteEnabled() {
157 return GetSaveFormData();
160 void AwAutofillClient::DetectAccountCreationForms(
161 const std::vector<autofill::FormStructure*>& forms) {
164 void AwAutofillClient::DidFillOrPreviewField(
165 const base::string16& autofilled_value,
166 const base::string16& profile_full_name) {
169 void AwAutofillClient::SuggestionSelected(JNIEnv* env,
170 jobject object,
171 jint position) {
172 if (delegate_)
173 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
176 void AwAutofillClient::HideRequestAutocompleteDialog() {
177 NOTIMPLEMENTED();
180 void AwAutofillClient::ShowAutofillSettings() {
181 NOTIMPLEMENTED();
184 void AwAutofillClient::ConfirmSaveCreditCard(
185 const autofill::AutofillMetrics& metric_logger,
186 const base::Closure& save_card_callback) {
187 NOTIMPLEMENTED();
190 void AwAutofillClient::ShowRequestAutocompleteDialog(
191 const autofill::FormData& form,
192 const GURL& source_url,
193 const ResultCallback& callback) {
194 NOTIMPLEMENTED();
197 bool RegisterAwAutofillClient(JNIEnv* env) {
198 return RegisterNativesImpl(env);
201 } // namespace android_webview