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