aw_autofill_client.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #include "android_webview/browser/aw_autofill_client.h"
  5. #include <utility>
  6. #include "android_webview/browser/aw_browser_context.h"
  7. #include "android_webview/browser/aw_content_browser_client.h"
  8. #include "android_webview/browser/aw_contents.h"
  9. #include "android_webview/browser/aw_form_database_service.h"
  10. #include "android_webview/browser_jni_headers/AwAutofillClient_jni.h"
  11. #include "base/android/build_info.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/notreached.h"
  16. #include "components/autofill/core/browser/payments/legal_message_line.h"
  17. #include "components/autofill/core/browser/ui/autofill_popup_delegate.h"
  18. #include "components/autofill/core/browser/ui/suggestion.h"
  19. #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
  20. #include "components/prefs/pref_registry_simple.h"
  21. #include "components/prefs/pref_service.h"
  22. #include "components/prefs/pref_service_factory.h"
  23. #include "components/user_prefs/user_prefs.h"
  24. #include "content/public/browser/navigation_entry.h"
  25. #include "content/public/browser/ssl_status.h"
  26. #include "content/public/browser/web_contents.h"
  27. #include "ui/android/view_android.h"
  28. #include "ui/gfx/geometry/rect_f.h"
  29. using base::android::AttachCurrentThread;
  30. using base::android::ConvertUTF16ToJavaString;
  31. using base::android::JavaParamRef;
  32. using base::android::ScopedJavaLocalRef;
  33. using content::WebContents;
  34. namespace android_webview {
  35. AwAutofillClient::~AwAutofillClient() {
  36. HideAutofillPopup(autofill::PopupHidingReason::kTabGone);
  37. }
  38. void AwAutofillClient::SetSaveFormData(bool enabled) {
  39. save_form_data_ = enabled;
  40. }
  41. bool AwAutofillClient::GetSaveFormData() {
  42. return save_form_data_;
  43. }
  44. autofill::PersonalDataManager* AwAutofillClient::GetPersonalDataManager() {
  45. return nullptr;
  46. }
  47. autofill::AutocompleteHistoryManager*
  48. AwAutofillClient::GetAutocompleteHistoryManager() {
  49. return AwBrowserContext::FromWebContents(&GetWebContents())
  50. ->GetAutocompleteHistoryManager();
  51. }
  52. PrefService* AwAutofillClient::GetPrefs() {
  53. return const_cast<PrefService*>(base::as_const(*this).GetPrefs());
  54. }
  55. const PrefService* AwAutofillClient::GetPrefs() const {
  56. return user_prefs::UserPrefs::Get(AwBrowserContext::FromWebContents(
  57. const_cast<WebContents*>(&GetWebContents())));
  58. }
  59. syncer::SyncService* AwAutofillClient::GetSyncService() {
  60. return nullptr;
  61. }
  62. signin::IdentityManager* AwAutofillClient::GetIdentityManager() {
  63. return nullptr;
  64. }
  65. autofill::FormDataImporter* AwAutofillClient::GetFormDataImporter() {
  66. return nullptr;
  67. }
  68. autofill::payments::PaymentsClient* AwAutofillClient::GetPaymentsClient() {
  69. return nullptr;
  70. }
  71. autofill::StrikeDatabase* AwAutofillClient::GetStrikeDatabase() {
  72. return nullptr;
  73. }
  74. ukm::UkmRecorder* AwAutofillClient::GetUkmRecorder() {
  75. return nullptr;
  76. }
  77. ukm::SourceId AwAutofillClient::GetUkmSourceId() {
  78. // UKM recording is not supported for WebViews.
  79. return ukm::kInvalidSourceId;
  80. }
  81. autofill::AddressNormalizer* AwAutofillClient::GetAddressNormalizer() {
  82. return nullptr;
  83. }
  84. const GURL& AwAutofillClient::GetLastCommittedURL() const {
  85. return GetWebContents().GetLastCommittedURL();
  86. }
  87. security_state::SecurityLevel
  88. AwAutofillClient::GetSecurityLevelForUmaHistograms() {
  89. // The metrics are not recorded for Android webview, so return the count value
  90. // which will not be recorded.
  91. return security_state::SecurityLevel::SECURITY_LEVEL_COUNT;
  92. }
  93. const translate::LanguageState* AwAutofillClient::GetLanguageState() {
  94. return nullptr;
  95. }
  96. translate::TranslateDriver* AwAutofillClient::GetTranslateDriver() {
  97. return nullptr;
  98. }
  99. void AwAutofillClient::ShowAutofillSettings(bool show_credit_card_settings) {
  100. NOTIMPLEMENTED();
  101. }
  102. void AwAutofillClient::ShowUnmaskPrompt(
  103. const autofill::CreditCard& card,
  104. UnmaskCardReason reason,
  105. base::WeakPtr<autofill::CardUnmaskDelegate> delegate) {
  106. NOTIMPLEMENTED();
  107. }
  108. void AwAutofillClient::OnUnmaskVerificationResult(PaymentsRpcResult result) {
  109. NOTIMPLEMENTED();
  110. }
  111. void AwAutofillClient::ConfirmAccountNameFixFlow(
  112. base::OnceCallback<void(const std::u16string&)> callback) {
  113. NOTIMPLEMENTED();
  114. }
  115. void AwAutofillClient::ConfirmExpirationDateFixFlow(
  116. const autofill::CreditCard& card,
  117. base::OnceCallback<void(const std::u16string&, const std::u16string&)>
  118. callback) {
  119. NOTIMPLEMENTED();
  120. }
  121. void AwAutofillClient::ConfirmSaveCreditCardLocally(
  122. const autofill::CreditCard& card,
  123. SaveCreditCardOptions options,
  124. LocalSaveCardPromptCallback callback) {
  125. NOTIMPLEMENTED();
  126. }
  127. void AwAutofillClient::ConfirmSaveCreditCardToCloud(
  128. const autofill::CreditCard& card,
  129. const autofill::LegalMessageLines& legal_message_lines,
  130. SaveCreditCardOptions options,
  131. UploadSaveCardPromptCallback callback) {
  132. NOTIMPLEMENTED();
  133. }
  134. void AwAutofillClient::CreditCardUploadCompleted(bool card_saved) {
  135. NOTIMPLEMENTED();
  136. }
  137. void AwAutofillClient::ConfirmCreditCardFillAssist(
  138. const autofill::CreditCard& card,
  139. base::OnceClosure callback) {
  140. NOTIMPLEMENTED();
  141. }
  142. void AwAutofillClient::ConfirmSaveAddressProfile(
  143. const autofill::AutofillProfile& profile,
  144. const autofill::AutofillProfile* original_profile,
  145. SaveAddressProfilePromptOptions options,
  146. AddressProfileSavePromptCallback callback) {
  147. NOTIMPLEMENTED();
  148. }
  149. bool AwAutofillClient::HasCreditCardScanFeature() {
  150. return false;
  151. }
  152. void AwAutofillClient::ScanCreditCard(CreditCardScanCallback callback) {
  153. NOTIMPLEMENTED();
  154. }
  155. bool AwAutofillClient::IsTouchToFillCreditCardSupported() {
  156. return false;
  157. }
  158. bool AwAutofillClient::ShowTouchToFillCreditCard(
  159. base::WeakPtr<autofill::TouchToFillDelegate> delegate) {
  160. NOTREACHED();
  161. return false;
  162. }
  163. void AwAutofillClient::HideTouchToFillCreditCard() {
  164. NOTREACHED();
  165. }
  166. void AwAutofillClient::ShowAutofillPopup(
  167. const autofill::AutofillClient::PopupOpenArgs& open_args,
  168. base::WeakPtr<autofill::AutofillPopupDelegate> delegate) {
  169. suggestions_ = open_args.suggestions;
  170. delegate_ = delegate;
  171. // Convert element_bounds to be in screen space.
  172. gfx::Rect client_area = GetWebContents().GetContainerBounds();
  173. gfx::RectF element_bounds_in_screen_space =
  174. open_args.element_bounds + client_area.OffsetFromOrigin();
  175. ShowAutofillPopupImpl(element_bounds_in_screen_space,
  176. open_args.text_direction == base::i18n::RIGHT_TO_LEFT,
  177. open_args.suggestions);
  178. }
  179. void AwAutofillClient::UpdateAutofillPopupDataListValues(
  180. const std::vector<std::u16string>& values,
  181. const std::vector<std::u16string>& labels) {
  182. // Leaving as an empty method since updating autofill popup window
  183. // dynamically does not seem to be a useful feature for android webview.
  184. // See crrev.com/18102002 if need to implement.
  185. }
  186. base::span<const autofill::Suggestion> AwAutofillClient::GetPopupSuggestions()
  187. const {
  188. NOTIMPLEMENTED();
  189. return base::span<const autofill::Suggestion>();
  190. }
  191. void AwAutofillClient::PinPopupView() {
  192. NOTIMPLEMENTED();
  193. }
  194. autofill::AutofillClient::PopupOpenArgs AwAutofillClient::GetReopenPopupArgs()
  195. const {
  196. NOTIMPLEMENTED();
  197. return {};
  198. }
  199. void AwAutofillClient::UpdatePopup(
  200. const std::vector<autofill::Suggestion>& suggestions,
  201. autofill::PopupType popup_type) {
  202. NOTIMPLEMENTED();
  203. }
  204. void AwAutofillClient::HideAutofillPopup(autofill::PopupHidingReason reason) {
  205. JNIEnv* env = AttachCurrentThread();
  206. ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
  207. if (!obj)
  208. return;
  209. delegate_.reset();
  210. Java_AwAutofillClient_hideAutofillPopup(env, obj);
  211. }
  212. bool AwAutofillClient::IsAutocompleteEnabled() {
  213. return GetSaveFormData();
  214. }
  215. bool AwAutofillClient::IsPasswordManagerEnabled() {
  216. // Android O+ relies on the AndroidAutofillManager, which does not call this
  217. // function. If it ever does, the function needs to be implemented in a
  218. // meaningful way.
  219. if (base::android::BuildInfo::GetInstance()->sdk_int() >=
  220. base::android::SDK_VERSION_OREO) {
  221. NOTREACHED();
  222. }
  223. // This is behavior preserving: For pre-O versions, AwAutofill did rely on a
  224. // BrowserAutofillManager, which now calls the function. But pre-O only
  225. // offered an autocomplete feature that restored values of specific input
  226. // elements. It did not support password management.
  227. return false;
  228. }
  229. void AwAutofillClient::PropagateAutofillPredictions(
  230. autofill::AutofillDriver* driver,
  231. const std::vector<autofill::FormStructure*>& forms) {}
  232. void AwAutofillClient::DidFillOrPreviewField(
  233. const std::u16string& autofilled_value,
  234. const std::u16string& profile_full_name) {}
  235. bool AwAutofillClient::IsContextSecure() const {
  236. content::SSLStatus ssl_status;
  237. content::NavigationEntry* navigation_entry =
  238. GetWebContents().GetController().GetLastCommittedEntry();
  239. if (!navigation_entry)
  240. return false;
  241. ssl_status = navigation_entry->GetSSL();
  242. // Note: As of crbug.com/701018, Chrome relies on SecurityStateTabHelper to
  243. // determine whether the page is secure, but WebView has no equivalent class.
  244. return navigation_entry->GetURL().SchemeIsCryptographic() &&
  245. ssl_status.certificate &&
  246. !net::IsCertStatusError(ssl_status.cert_status) &&
  247. !(ssl_status.content_status &
  248. content::SSLStatus::RAN_INSECURE_CONTENT);
  249. }
  250. bool AwAutofillClient::ShouldShowSigninPromo() {
  251. return false;
  252. }
  253. bool AwAutofillClient::AreServerCardsSupported() const {
  254. return true;
  255. }
  256. void AwAutofillClient::ExecuteCommand(int id) {
  257. NOTIMPLEMENTED();
  258. }
  259. void AwAutofillClient::OpenPromoCodeOfferDetailsURL(const GURL& url) {
  260. NOTIMPLEMENTED();
  261. }
  262. void AwAutofillClient::LoadRiskData(
  263. base::OnceCallback<void(const std::string&)> callback) {
  264. NOTIMPLEMENTED();
  265. }
  266. void AwAutofillClient::Dismissed(JNIEnv* env,
  267. const JavaParamRef<jobject>& obj) {
  268. anchor_view_.Reset();
  269. }
  270. void AwAutofillClient::SuggestionSelected(JNIEnv* env,
  271. const JavaParamRef<jobject>& object,
  272. jint position) {
  273. if (delegate_) {
  274. delegate_->DidAcceptSuggestion(suggestions_[position].main_text.value,
  275. suggestions_[position].frontend_id,
  276. suggestions_[position].payload, position);
  277. }
  278. }
  279. // Ownership: The native object is created (if autofill enabled) and owned by
  280. // AwContents. The native object creates the java peer which handles most
  281. // autofill functionality at the java side. The java peer is owned by Java
  282. // AwContents. The native object only maintains a weak ref to it.
  283. AwAutofillClient::AwAutofillClient(WebContents* contents)
  284. : content::WebContentsUserData<AwAutofillClient>(*contents) {
  285. JNIEnv* env = AttachCurrentThread();
  286. ScopedJavaLocalRef<jobject> delegate;
  287. delegate.Reset(
  288. Java_AwAutofillClient_create(env, reinterpret_cast<intptr_t>(this)));
  289. AwContents* aw_contents = AwContents::FromWebContents(contents);
  290. aw_contents->SetAwAutofillClient(delegate);
  291. java_ref_ = JavaObjectWeakGlobalRef(env, delegate);
  292. }
  293. void AwAutofillClient::ShowAutofillPopupImpl(
  294. const gfx::RectF& element_bounds,
  295. bool is_rtl,
  296. const std::vector<autofill::Suggestion>& suggestions) {
  297. JNIEnv* env = AttachCurrentThread();
  298. ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
  299. if (!obj)
  300. return;
  301. // We need an array of AutofillSuggestion.
  302. size_t count = suggestions.size();
  303. ScopedJavaLocalRef<jobjectArray> data_array =
  304. Java_AwAutofillClient_createAutofillSuggestionArray(env, count);
  305. for (size_t i = 0; i < count; ++i) {
  306. ScopedJavaLocalRef<jstring> name =
  307. ConvertUTF16ToJavaString(env, suggestions[i].main_text.value);
  308. ScopedJavaLocalRef<jstring> label =
  309. ConvertUTF16ToJavaString(env, suggestions[i].label);
  310. Java_AwAutofillClient_addToAutofillSuggestionArray(
  311. env, data_array, i, name, label, suggestions[i].frontend_id);
  312. }
  313. ui::ViewAndroid* view_android = GetWebContents().GetNativeView();
  314. if (!view_android)
  315. return;
  316. const ScopedJavaLocalRef<jobject> current_view = anchor_view_.view();
  317. if (!current_view)
  318. anchor_view_ = view_android->AcquireAnchorView();
  319. const ScopedJavaLocalRef<jobject> view = anchor_view_.view();
  320. if (!view)
  321. return;
  322. view_android->SetAnchorRect(view, element_bounds);
  323. Java_AwAutofillClient_showAutofillPopup(env, obj, view, is_rtl, data_array);
  324. }
  325. content::WebContents& AwAutofillClient::GetWebContents() const {
  326. // While a const_cast is not ideal. The Autofill API uses const in various
  327. // spots and the content public API doesn't have const accessors. So the const
  328. // cast is the lesser of two evils.
  329. return const_cast<content::WebContents&>(
  330. content::WebContentsUserData<AwAutofillClient>::GetWebContents());
  331. }
  332. WEB_CONTENTS_USER_DATA_KEY_IMPL(AwAutofillClient);
  333. } // namespace android_webview