OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "android_webview/native/aw_autofill_manager_delegate.h" | 5 #include "android_webview/native/aw_autofill_client.h" |
6 | 6 |
7 #include "android_webview/browser/aw_browser_context.h" | 7 #include "android_webview/browser/aw_browser_context.h" |
8 #include "android_webview/browser/aw_content_browser_client.h" | 8 #include "android_webview/browser/aw_content_browser_client.h" |
9 #include "android_webview/browser/aw_form_database_service.h" | 9 #include "android_webview/browser/aw_form_database_service.h" |
10 #include "android_webview/browser/aw_pref_store.h" | 10 #include "android_webview/browser/aw_pref_store.h" |
11 #include "android_webview/native/aw_contents.h" | 11 #include "android_webview/native/aw_contents.h" |
12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
14 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/prefs/pref_registry_simple.h" | 16 #include "base/prefs/pref_registry_simple.h" |
17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
18 #include "base/prefs/pref_service_factory.h" | 18 #include "base/prefs/pref_service_factory.h" |
19 #include "components/autofill/core/browser/autofill_popup_delegate.h" | 19 #include "components/autofill/core/browser/autofill_popup_delegate.h" |
20 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 20 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
21 #include "components/autofill/core/common/autofill_pref_names.h" | 21 #include "components/autofill/core/common/autofill_pref_names.h" |
22 #include "components/user_prefs/user_prefs.h" | 22 #include "components/user_prefs/user_prefs.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "jni/AwAutofillManagerDelegate_jni.h" | 24 #include "jni/AwAutofillClient_jni.h" |
25 | 25 |
26 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
27 using base::android::ConvertUTF16ToJavaString; | 27 using base::android::ConvertUTF16ToJavaString; |
28 using base::android::ScopedJavaLocalRef; | 28 using base::android::ScopedJavaLocalRef; |
29 using content::WebContents; | 29 using content::WebContents; |
30 | 30 |
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillManagerDelegate); | 31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(android_webview::AwAutofillClient); |
32 | 32 |
33 namespace android_webview { | 33 namespace android_webview { |
34 | 34 |
35 // Ownership: The native object is created (if autofill enabled) and owned by | 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 | 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 | 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. | 38 // AwContents. The native object only maintains a weak ref to it. |
39 AwAutofillManagerDelegate::AwAutofillManagerDelegate(WebContents* contents) | 39 AwAutofillClient::AwAutofillClient(WebContents* contents) |
40 : web_contents_(contents), | 40 : web_contents_(contents), save_form_data_(false) { |
41 save_form_data_(false) { | |
42 JNIEnv* env = AttachCurrentThread(); | 41 JNIEnv* env = AttachCurrentThread(); |
43 ScopedJavaLocalRef<jobject> delegate; | 42 ScopedJavaLocalRef<jobject> delegate; |
44 delegate.Reset( | 43 delegate.Reset( |
45 Java_AwAutofillManagerDelegate_create( | 44 Java_AwAutofillClient_create(env, reinterpret_cast<intptr_t>(this))); |
46 env, reinterpret_cast<intptr_t>(this))); | |
47 | 45 |
48 AwContents* aw_contents = AwContents::FromWebContents(web_contents_); | 46 AwContents* aw_contents = AwContents::FromWebContents(web_contents_); |
49 aw_contents->SetAwAutofillManagerDelegate(delegate.obj()); | 47 aw_contents->SetAwAutofillClient(delegate.obj()); |
50 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj()); | 48 java_ref_ = JavaObjectWeakGlobalRef(env, delegate.obj()); |
51 } | 49 } |
52 | 50 |
53 AwAutofillManagerDelegate::~AwAutofillManagerDelegate() { | 51 AwAutofillClient::~AwAutofillClient() { |
54 HideAutofillPopup(); | 52 HideAutofillPopup(); |
55 } | 53 } |
56 | 54 |
57 void AwAutofillManagerDelegate::SetSaveFormData(bool enabled) { | 55 void AwAutofillClient::SetSaveFormData(bool enabled) { |
58 save_form_data_ = enabled; | 56 save_form_data_ = enabled; |
59 } | 57 } |
60 | 58 |
61 bool AwAutofillManagerDelegate::GetSaveFormData() { | 59 bool AwAutofillClient::GetSaveFormData() { |
62 return save_form_data_; | 60 return save_form_data_; |
63 } | 61 } |
64 | 62 |
65 PrefService* AwAutofillManagerDelegate::GetPrefs() { | 63 PrefService* AwAutofillClient::GetPrefs() { |
66 return user_prefs::UserPrefs::Get( | 64 return user_prefs::UserPrefs::Get( |
67 AwContentBrowserClient::GetAwBrowserContext()); | 65 AwContentBrowserClient::GetAwBrowserContext()); |
68 } | 66 } |
69 | 67 |
70 autofill::PersonalDataManager* | 68 autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() { |
71 AwAutofillManagerDelegate::GetPersonalDataManager() { | |
72 return NULL; | 69 return NULL; |
73 } | 70 } |
74 | 71 |
75 scoped_refptr<autofill::AutofillWebDataService> | 72 scoped_refptr<autofill::AutofillWebDataService> |
76 AwAutofillManagerDelegate::GetDatabase() { | 73 AwAutofillClient::GetDatabase() { |
77 android_webview::AwFormDatabaseService* service = | 74 android_webview::AwFormDatabaseService* service = |
78 static_cast<android_webview::AwBrowserContext*>( | 75 static_cast<android_webview::AwBrowserContext*>( |
79 web_contents_->GetBrowserContext())->GetFormDatabaseService(); | 76 web_contents_->GetBrowserContext())->GetFormDatabaseService(); |
80 return service->get_autofill_webdata_service(); | 77 return service->get_autofill_webdata_service(); |
81 } | 78 } |
82 | 79 |
83 void AwAutofillManagerDelegate::ShowAutofillPopup( | 80 void AwAutofillClient::ShowAutofillPopup( |
84 const gfx::RectF& element_bounds, | 81 const gfx::RectF& element_bounds, |
85 base::i18n::TextDirection text_direction, | 82 base::i18n::TextDirection text_direction, |
86 const std::vector<base::string16>& values, | 83 const std::vector<base::string16>& values, |
87 const std::vector<base::string16>& labels, | 84 const std::vector<base::string16>& labels, |
88 const std::vector<base::string16>& icons, | 85 const std::vector<base::string16>& icons, |
89 const std::vector<int>& identifiers, | 86 const std::vector<int>& identifiers, |
90 base::WeakPtr<autofill::AutofillPopupDelegate> delegate) { | 87 base::WeakPtr<autofill::AutofillPopupDelegate> delegate) { |
91 | |
92 values_ = values; | 88 values_ = values; |
93 identifiers_ = identifiers; | 89 identifiers_ = identifiers; |
94 delegate_ = delegate; | 90 delegate_ = delegate; |
95 | 91 |
96 // Convert element_bounds to be in screen space. | 92 // Convert element_bounds to be in screen space. |
97 gfx::Rect client_area = web_contents_->GetContainerBounds(); | 93 gfx::Rect client_area = web_contents_->GetContainerBounds(); |
98 gfx::RectF element_bounds_in_screen_space = | 94 gfx::RectF element_bounds_in_screen_space = |
99 element_bounds + client_area.OffsetFromOrigin(); | 95 element_bounds + client_area.OffsetFromOrigin(); |
100 | 96 |
101 ShowAutofillPopupImpl(element_bounds_in_screen_space, | 97 ShowAutofillPopupImpl( |
102 values, | 98 element_bounds_in_screen_space, values, labels, identifiers); |
103 labels, | |
104 identifiers); | |
105 } | 99 } |
106 | 100 |
107 void AwAutofillManagerDelegate::ShowAutofillPopupImpl( | 101 void AwAutofillClient::ShowAutofillPopupImpl( |
108 const gfx::RectF& element_bounds, | 102 const gfx::RectF& element_bounds, |
109 const std::vector<base::string16>& values, | 103 const std::vector<base::string16>& values, |
110 const std::vector<base::string16>& labels, | 104 const std::vector<base::string16>& labels, |
111 const std::vector<int>& identifiers) { | 105 const std::vector<int>& identifiers) { |
112 JNIEnv* env = AttachCurrentThread(); | 106 JNIEnv* env = AttachCurrentThread(); |
113 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 107 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
114 if (obj.is_null()) | 108 if (obj.is_null()) |
115 return; | 109 return; |
116 | 110 |
117 // We need an array of AutofillSuggestion. | 111 // We need an array of AutofillSuggestion. |
118 size_t count = values.size(); | 112 size_t count = values.size(); |
119 | 113 |
120 ScopedJavaLocalRef<jobjectArray> data_array = | 114 ScopedJavaLocalRef<jobjectArray> data_array = |
121 Java_AwAutofillManagerDelegate_createAutofillSuggestionArray(env, count); | 115 Java_AwAutofillClient_createAutofillSuggestionArray(env, count); |
122 | 116 |
123 for (size_t i = 0; i < count; ++i) { | 117 for (size_t i = 0; i < count; ++i) { |
124 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]); | 118 ScopedJavaLocalRef<jstring> name = ConvertUTF16ToJavaString(env, values[i]); |
125 ScopedJavaLocalRef<jstring> label = | 119 ScopedJavaLocalRef<jstring> label = |
126 ConvertUTF16ToJavaString(env, labels[i]); | 120 ConvertUTF16ToJavaString(env, labels[i]); |
127 Java_AwAutofillManagerDelegate_addToAutofillSuggestionArray( | 121 Java_AwAutofillClient_addToAutofillSuggestionArray( |
128 env, | 122 env, data_array.obj(), i, name.obj(), label.obj(), identifiers[i]); |
129 data_array.obj(), | |
130 i, | |
131 name.obj(), | |
132 label.obj(), | |
133 identifiers[i]); | |
134 } | 123 } |
135 | 124 |
136 Java_AwAutofillManagerDelegate_showAutofillPopup( | 125 Java_AwAutofillClient_showAutofillPopup(env, |
137 env, | 126 obj.obj(), |
138 obj.obj(), | 127 element_bounds.x(), |
139 element_bounds.x(), | 128 element_bounds.y(), |
140 element_bounds.y(), element_bounds.width(), | 129 element_bounds.width(), |
141 element_bounds.height(), data_array.obj()); | 130 element_bounds.height(), |
| 131 data_array.obj()); |
142 } | 132 } |
143 | 133 |
144 void AwAutofillManagerDelegate::UpdateAutofillPopupDataListValues( | 134 void AwAutofillClient::UpdateAutofillPopupDataListValues( |
145 const std::vector<base::string16>& values, | 135 const std::vector<base::string16>& values, |
146 const std::vector<base::string16>& labels) { | 136 const std::vector<base::string16>& labels) { |
147 // Leaving as an empty method since updating autofill popup window | 137 // Leaving as an empty method since updating autofill popup window |
148 // dynamically does not seem to be a useful feature for android webview. | 138 // dynamically does not seem to be a useful feature for android webview. |
149 // See crrev.com/18102002 if need to implement. | 139 // See crrev.com/18102002 if need to implement. |
150 } | 140 } |
151 | 141 |
152 void AwAutofillManagerDelegate::HideAutofillPopup() { | 142 void AwAutofillClient::HideAutofillPopup() { |
153 JNIEnv* env = AttachCurrentThread(); | 143 JNIEnv* env = AttachCurrentThread(); |
154 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 144 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
155 if (obj.is_null()) | 145 if (obj.is_null()) |
156 return; | 146 return; |
157 delegate_.reset(); | 147 delegate_.reset(); |
158 Java_AwAutofillManagerDelegate_hideAutofillPopup(env, obj.obj()); | 148 Java_AwAutofillClient_hideAutofillPopup(env, obj.obj()); |
159 } | 149 } |
160 | 150 |
161 bool AwAutofillManagerDelegate::IsAutocompleteEnabled() { | 151 bool AwAutofillClient::IsAutocompleteEnabled() { |
162 return GetSaveFormData(); | 152 return GetSaveFormData(); |
163 } | 153 } |
164 | 154 |
165 void AwAutofillManagerDelegate::DetectAccountCreationForms( | 155 void AwAutofillClient::DetectAccountCreationForms( |
166 const std::vector<autofill::FormStructure*>& forms) {} | 156 const std::vector<autofill::FormStructure*>& forms) { |
| 157 } |
167 | 158 |
168 void AwAutofillManagerDelegate::DidFillOrPreviewField( | 159 void AwAutofillClient::DidFillOrPreviewField( |
169 const base::string16& autofilled_value, | 160 const base::string16& autofilled_value, |
170 const base::string16& profile_full_name) {} | 161 const base::string16& profile_full_name) { |
| 162 } |
171 | 163 |
172 void AwAutofillManagerDelegate::SuggestionSelected(JNIEnv* env, | 164 void AwAutofillClient::SuggestionSelected(JNIEnv* env, |
173 jobject object, | 165 jobject object, |
174 jint position) { | 166 jint position) { |
175 if (delegate_) | 167 if (delegate_) |
176 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]); | 168 delegate_->DidAcceptSuggestion(values_[position], identifiers_[position]); |
177 } | 169 } |
178 | 170 |
179 void AwAutofillManagerDelegate::HideRequestAutocompleteDialog() { | 171 void AwAutofillClient::HideRequestAutocompleteDialog() { |
180 NOTIMPLEMENTED(); | 172 NOTIMPLEMENTED(); |
181 } | 173 } |
182 | 174 |
183 void AwAutofillManagerDelegate::ShowAutofillSettings() { | 175 void AwAutofillClient::ShowAutofillSettings() { |
184 NOTIMPLEMENTED(); | 176 NOTIMPLEMENTED(); |
185 } | 177 } |
186 | 178 |
187 void AwAutofillManagerDelegate::ConfirmSaveCreditCard( | 179 void AwAutofillClient::ConfirmSaveCreditCard( |
188 const autofill::AutofillMetrics& metric_logger, | 180 const autofill::AutofillMetrics& metric_logger, |
189 const base::Closure& save_card_callback) { | 181 const base::Closure& save_card_callback) { |
190 NOTIMPLEMENTED(); | 182 NOTIMPLEMENTED(); |
191 } | 183 } |
192 | 184 |
193 void AwAutofillManagerDelegate::ShowRequestAutocompleteDialog( | 185 void AwAutofillClient::ShowRequestAutocompleteDialog( |
194 const autofill::FormData& form, | 186 const autofill::FormData& form, |
195 const GURL& source_url, | 187 const GURL& source_url, |
196 const ResultCallback& callback) { | 188 const ResultCallback& callback) { |
197 NOTIMPLEMENTED(); | 189 NOTIMPLEMENTED(); |
198 } | 190 } |
199 | 191 |
200 bool RegisterAwAutofillManagerDelegate(JNIEnv* env) { | 192 bool RegisterAwAutofillClient(JNIEnv* env) { |
201 return RegisterNativesImpl(env); | 193 return RegisterNativesImpl(env); |
202 } | 194 } |
203 | 195 |
204 } // namespace android_webview | 196 } // namespace android_webview |
OLD | NEW |