- add sources.
[platform/framework/web/crosswalk.git] / src / android_webview / native / aw_autofill_manager_delegate.cc
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.
4
5 #include "android_webview/native/aw_autofill_manager_delegate.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_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"
24
25 using base::android::AttachCurrentThread;
26 using base::android::ConvertUTF16ToJavaString;
27 using base::android::ScopedJavaLocalRef;
28 using content::WebContents;
29
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillManagerDelegate);
31
32 namespace android_webview {
33
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;
43   delegate.Reset(
44       Java_AwAutofillManagerDelegate_create(env, reinterpret_cast<jint>(this)));
45
46   AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
47   aw_contents->SetAwAutofillManagerDelegate(delegate.obj());
48   java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
49 }
50
51 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() {
52   HideAutofillPopup();
53 }
54
55 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled) {
56   save_form_data_ = enabled;
57 }
58
59 bool AwAutofillManagerDelegate::GetSaveFormData() {
60   return save_form_data_;
61 }
62
63 PrefService* AwAutofillManagerDelegate::GetPrefs() {
64   return user_prefs::UserPrefs::Get(
65       AwContentBrowserClient::GetAwBrowserContext());
66 }
67
68 autofill::PersonalDataManager*
69 AwAutofillManagerDelegate::GetPersonalDataManager() {
70   return NULL;
71 }
72
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) {
81
82   values_ = values;
83   identifiers_ = identifiers;
84   delegate_ = delegate;
85
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();
91
92   ShowAutofillPopupImpl(element_bounds_in_screen_space,
93                         values,
94                         labels,
95                         identifiers);
96 }
97
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);
105   if (obj.is_null())
106     return;
107
108   // We need an array of AutofillSuggestion.
109   size_t count = values.size();
110
111   ScopedJavaLocalRef<jobjectArray> data_array =
112       Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env, count);
113
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(
119         env,
120         data_array.obj(),
121         i,
122         name.obj(),
123         label.obj(),
124         identifiers[i]);
125   }
126
127   Java_AwAutofillManagerDelegate_showAutofillPopup(
128       env,
129       obj.obj(),
130       element_bounds.x(),
131       element_bounds.y(), element_bounds.width(),
132       element_bounds.height(), data_array.obj());
133 }
134
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.
141 }
142
143 void AwAutofillManagerDelegate::HideAutofillPopup() {
144   JNIEnv* env = AttachCurrentThread();
145   ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
146   if (obj.is_null())
147     return;
148   delegate_.reset();
149   Java_AwAutofillManagerDelegate_hideAutofillPopup(env, obj.obj());
150 }
151
152 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() {
153   return GetSaveFormData();
154 }
155
156 void AwAutofillManagerDelegate::DetectAccountCreationForms(
157     const std::vector<autofill::FormStructure*>& forms) {}
158
159 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv* env,
160                                                    jobject object,
161                                                    jint position) {
162   if (delegate_)
163     delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
164 }
165
166 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() {
167   NOTIMPLEMENTED();
168 }
169
170 void AwAutofillManagerDelegate::ShowAutofillSettings() {
171   NOTIMPLEMENTED();
172 }
173
174 void AwAutofillManagerDelegate::ConfirmSaveCreditCard(
175     const autofill::AutofillMetrics& metric_logger,
176     const autofill::CreditCard& credit_card,
177     const base::Closure& save_card_callback) {
178   NOTIMPLEMENTED();
179 }
180
181 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog(
182     const autofill::FormData& form,
183     const GURL& source_url,
184     const base::Callback<void(const autofill::FormStructure*)>& callback) {
185   NOTIMPLEMENTED();
186 }
187
188 bool RegisterAwAutofillManagerDelegate(JNIEnv* env) {
189   return RegisterNativesImpl(env) >= 0;
190 }
191
192 } // namespace android_webview