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
26using base::android::AttachCurrentThread;
27using base::android::ConvertUTF16ToJavaString;
28using base::android::ScopedJavaLocalRef;
29using content::WebContents;
30
31DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient);
32
33namespace 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.
39AwAutofillClient::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
51AwAutofillClient::~AwAutofillClient() {
52  HideAutofillPopup();
53}
54
55void AwAutofillClient::SetSaveFormData(bool enabled) {
56  save_form_data_ = enabled;
57}
58
59bool AwAutofillClient::GetSaveFormData() {
60  return save_form_data_;
61}
62
63PrefService* AwAutofillClient::GetPrefs() {
64  return user_prefs::UserPrefs::Get(
65      AwContentBrowserClient::GetAwBrowserContext());
66}
67
68autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() {
69  return NULL;
70}
71
72scoped_refptr<autofill::AutofillWebDataService>
73AwAutofillClient::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
80void 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(element_bounds_in_screen_space,
98                        text_direction == base::i18n::RIGHT_TO_LEFT,
99                        values,
100                        labels,
101                        identifiers);
102}
103
104void 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;
114
115  // We need an array of AutofillSuggestion.
116  size_t count = values.size();
117
118  ScopedJavaLocalRef<jobjectArray> data_array =
119      Java_AwAutofillClient_createAutofillSuggestionArray(env, count);
120
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]);
127  }
128
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());
137}
138
139void 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.
145}
146
147void 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());
154}
155
156bool AwAutofillClient::IsAutocompleteEnabled() {
157  return GetSaveFormData();
158}
159
160void AwAutofillClient::DetectAccountCreationForms(
161    const std::vector<autofill::FormStructure*>& forms) {
162}
163
164void AwAutofillClient::DidFillOrPreviewField(
165    const base::string16& autofilled_value,
166    const base::string16& profile_full_name) {
167}
168
169void AwAutofillClient::SuggestionSelected(JNIEnv* env,
170                                          jobject object,
171                                          jint position) {
172  if (delegate_)
173    delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
174}
175
176void AwAutofillClient::HideRequestAutocompleteDialog() {
177  NOTIMPLEMENTED();
178}
179
180void AwAutofillClient::ShowAutofillSettings() {
181  NOTIMPLEMENTED();
182}
183
184void AwAutofillClient::ConfirmSaveCreditCard(
185    const autofill::AutofillMetrics& metric_logger,
186    const base::Closure& save_card_callback) {
187  NOTIMPLEMENTED();
188}
189
190void AwAutofillClient::ShowRequestAutocompleteDialog(
191    const autofill::FormData& form,
192    const GURL& source_url,
193    const ResultCallback& callback) {
194  NOTIMPLEMENTED();
195}
196
197bool RegisterAwAutofillClient(JNIEnv* env) {
198  return RegisterNativesImpl(env);
199}
200
201}  // namespace android_webview
202