Telemetry media_measurement plus play action and tests.
[chromium-blink-merge.git] / android_webview / native / aw_autofill_manager_delegate.cc
blob7e1cb73c7f3d2c64a1aa4d31809a951f75d4ea64
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.
5 #include "android_webview/native/aw_autofill_manager_delegate.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_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/content/browser/autocheckout/whitelist_manager.h"
19 #include "components/autofill/core/browser/autofill_popup_delegate.h"
20 #include "components/autofill/core/common/autofill_pref_names.h"
21 #include "components/user_prefs/user_prefs.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h"
24 #include "jni/AwAutofillManagerDelegate_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::AwAutofillManagerDelegate);
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 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(env, reinterpret_cast<jint>(this)));
47 AwContents* aw_contents = AwContents::FromWebContents(web_contents_);
48 aw_contents->SetAwAutofillManagerDelegate(delegate.obj());
49 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj());
52 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() {
53 HideAutofillPopup();
56 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled) {
57 save_form_data_ = enabled;
60 bool AwAutofillManagerDelegate::GetSaveFormData() {
61 return save_form_data_;
64 PrefService* AwAutofillManagerDelegate::GetPrefs() {
65 return user_prefs::UserPrefs::Get(
66 AwContentBrowserClient::GetAwBrowserContext());
69 autofill::PersonalDataManager*
70 AwAutofillManagerDelegate::GetPersonalDataManager() {
71 return NULL;
74 autofill::autocheckout::WhitelistManager*
75 AwAutofillManagerDelegate::GetAutocheckoutWhitelistManager() const {
76 return NULL;
79 void AwAutofillManagerDelegate::ShowAutofillPopup(
80 const gfx::RectF& element_bounds,
81 base::i18n::TextDirection text_direction,
82 const std::vector<string16>& values,
83 const std::vector<string16>& labels,
84 const std::vector<string16>& icons,
85 const std::vector<int>& identifiers,
86 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;
94 web_contents_->GetView()->GetContainerBounds(&client_area);
95 gfx::RectF element_bounds_in_screen_space =
96 element_bounds + client_area.OffsetFromOrigin();
98 ShowAutofillPopupImpl(element_bounds_in_screen_space,
99 values,
100 labels,
101 identifiers);
104 void AwAutofillManagerDelegate::ShowAutofillPopupImpl(
105 const gfx::RectF& element_bounds,
106 const std::vector<string16>& values,
107 const std::vector<string16>& labels,
108 const std::vector<int>& identifiers) {
109 JNIEnv* env = AttachCurrentThread();
110 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
111 if (obj.is_null())
112 return;
114 // We need an array of AutofillSuggestion.
115 size_t count = values.size();
117 ScopedJavaLocalRef<jobjectArray> data_array =
118 Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env, count);
120 for (size_t i = 0; i < count; ++i) {
121 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]);
122 ScopedJavaLocalRef<jstring> label =
123 ConvertUTF16ToJavaString(env, labels[i]);
124 Java_AwAutofillManagerDelegate_addToAutofillSuggestionArray(
125 env,
126 data_array.obj(),
128 name.obj(),
129 label.obj(),
130 identifiers[i]);
133 Java_AwAutofillManagerDelegate_showAutofillPopup(
134 env,
135 obj.obj(),
136 element_bounds.x(),
137 element_bounds.y(), element_bounds.width(),
138 element_bounds.height(), data_array.obj());
141 void AwAutofillManagerDelegate::HideAutofillPopup() {
142 JNIEnv* env = AttachCurrentThread();
143 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
144 if (obj.is_null())
145 return;
146 delegate_.reset();
147 Java_AwAutofillManagerDelegate_hideAutofillPopup(env, obj.obj());
150 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() {
151 return GetSaveFormData();
154 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv* env,
155 jobject object,
156 jint position) {
157 if (delegate_)
158 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]);
161 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() {
162 NOTIMPLEMENTED();
165 void AwAutofillManagerDelegate::OnAutocheckoutError() {
166 NOTIMPLEMENTED();
169 void AwAutofillManagerDelegate::OnAutocheckoutSuccess() {
170 NOTIMPLEMENTED();
173 void AwAutofillManagerDelegate::ShowAutofillSettings() {
174 NOTIMPLEMENTED();
177 void AwAutofillManagerDelegate::ConfirmSaveCreditCard(
178 const autofill::AutofillMetrics& metric_logger,
179 const autofill::CreditCard& credit_card,
180 const base::Closure& save_card_callback) {
181 NOTIMPLEMENTED();
184 void AwAutofillManagerDelegate::ShowAutocheckoutBubble(
185 const gfx::RectF& bounding_box,
186 bool is_google_user,
187 const base::Callback<void(autofill::AutocheckoutBubbleState)>& callback) {
188 NOTIMPLEMENTED();
191 void AwAutofillManagerDelegate::HideAutocheckoutBubble() {
192 NOTIMPLEMENTED();
195 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog(
196 const autofill::FormData& form,
197 const GURL& source_url,
198 autofill::DialogType dialog_type,
199 const base::Callback<void(const autofill::FormStructure*,
200 const std::string&)>& callback) {
201 NOTIMPLEMENTED();
204 void AwAutofillManagerDelegate::AddAutocheckoutStep(
205 autofill::AutocheckoutStepType step_type) {
206 NOTIMPLEMENTED();
209 void AwAutofillManagerDelegate::UpdateAutocheckoutStep(
210 autofill::AutocheckoutStepType step_type,
211 autofill::AutocheckoutStepStatus step_status) {
212 NOTIMPLEMENTED();
215 bool RegisterAwAutofillManagerDelegate(JNIEnv* env) {
216 return RegisterNativesImpl(env) >= 0;
219 } // namespace android_webview