/
chrome_autofill_client.cc
1043 lines (927 loc) · 42 KB
/
chrome_autofill_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/address_normalizer_factory.h"
#include "chrome/browser/autofill/autocomplete_history_manager_factory.h"
#include "chrome/browser/autofill/autofill_offer_manager_factory.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/autofill/risk_util.h"
#include "chrome/browser/autofill/strike_database_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_promo_util.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/autofill_snackbar_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/card_unmask_authentication_selection_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/card_unmask_otp_input_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/create_card_unmask_prompt_view.h"
#include "chrome/browser/ui/autofill/payments/credit_card_scanner_controller.h"
#include "chrome/browser/ui/autofill/payments/virtual_card_enroll_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/save_update_address_profile_bubble_controller_impl.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/page_info/page_info_dialog.h"
#include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
#include "chrome/browser/vr/vr_tab_helper.h"
#include "chrome/browser/web_data_service_factory.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/url_constants.h"
#include "components/autofill/content/browser/autofill_log_router_factory.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/data_model/autofill_offer_data.h"
#include "components/autofill/core/browser/form_data_importer.h"
#include "components/autofill/core/browser/payments/card_unmask_challenge_option.h"
#include "components/autofill/core/browser/payments/payments_client.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_prompt_view.h"
#include "components/autofill/core/browser/ui/popup_item_ids.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill_assistant/browser/public/runtime_manager.h"
#include "components/password_manager/content/browser/content_password_manager_driver.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/password_requirements_service.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/profile_metrics/browser_profile_type.h"
#include "components/security_state/core/security_state.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/driver/sync_service.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/ukm/content/source_url_recorder.h"
#include "components/user_prefs/user_prefs.h"
#include "components/variations/service/variations_service.h"
#include "components/webauthn/content/browser/internal_authenticator_impl.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/ssl_status.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/geometry/rect.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/preferences/autofill/autofill_profile_bridge.h"
#include "chrome/browser/android/signin/signin_bridge.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
#include "chrome/browser/ui/android/autofill/card_expiration_date_fix_flow_view_android.h"
#include "chrome/browser/ui/android/autofill/card_name_fix_flow_view_android.h"
#include "chrome/browser/ui/android/infobars/autofill_credit_card_filling_infobar.h"
#include "chrome/browser/ui/android/infobars/autofill_offer_notification_infobar.h"
#include "chrome/browser/ui/autofill/payments/offer_notification_infobar_controller_impl.h"
#include "components/autofill/core/browser/payments/autofill_credit_card_filling_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/payments/autofill_offer_notification_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/payments/autofill_save_card_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/payments/autofill_save_card_infobar_mobile.h"
#include "components/autofill/core/browser/ui/payments/card_expiration_date_fix_flow_view.h"
#include "components/autofill/core/browser/ui/payments/card_name_fix_flow_view.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/messages/android/messages_feature.h"
#include "components/webauthn/android/internal_authenticator_android.h"
#include "ui/android/window_android.h"
#else // BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/autofill/payments/offer_notification_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/save_card_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/save_upi_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/virtual_card_manual_fallback_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/virtual_card_selection_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_state.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_view.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "components/zoom/zoom_controller.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace autofill {
using AutofillErrorDialogType =
AutofillErrorDialogController::AutofillErrorDialogType;
using AutoselectFirstSuggestion =
AutofillClient::PopupOpenArgs::AutoselectFirstSuggestion;
ChromeAutofillClient::~ChromeAutofillClient() {
// NOTE: It is too late to clean up the autofill popup; that cleanup process
// requires that the WebContents instance still be valid and it is not at
// this point (in particular, the WebContentsImpl destructor has already
// finished running and we are now in the base class destructor).
DCHECK(!popup_controller_);
}
version_info::Channel ChromeAutofillClient::GetChannel() const {
return chrome::GetChannel();
}
PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return PersonalDataManagerFactory::GetForProfile(
profile->GetOriginalProfile());
}
AutocompleteHistoryManager*
ChromeAutofillClient::GetAutocompleteHistoryManager() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return AutocompleteHistoryManagerFactory::GetForProfile(profile);
}
PrefService* ChromeAutofillClient::GetPrefs() {
return const_cast<PrefService*>(base::as_const(*this).GetPrefs());
}
const PrefService* ChromeAutofillClient::GetPrefs() const {
return Profile::FromBrowserContext(web_contents()->GetBrowserContext())
->GetPrefs();
}
syncer::SyncService* ChromeAutofillClient::GetSyncService() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return SyncServiceFactory::GetForProfile(profile);
}
signin::IdentityManager* ChromeAutofillClient::GetIdentityManager() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return IdentityManagerFactory::GetForProfile(profile->GetOriginalProfile());
}
FormDataImporter* ChromeAutofillClient::GetFormDataImporter() {
return form_data_importer_.get();
}
payments::PaymentsClient* ChromeAutofillClient::GetPaymentsClient() {
return payments_client_.get();
}
StrikeDatabase* ChromeAutofillClient::GetStrikeDatabase() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
// No need to return a StrikeDatabase in incognito mode. It is primarily
// used to determine whether or not to offer save of Autofill data. However,
// we don't allow saving of Autofill data while in incognito anyway, so an
// incognito code path should never get far enough to query StrikeDatabase.
return StrikeDatabaseFactory::GetForProfile(profile);
}
ukm::UkmRecorder* ChromeAutofillClient::GetUkmRecorder() {
return ukm::UkmRecorder::Get();
}
ukm::SourceId ChromeAutofillClient::GetUkmSourceId() {
return ukm::GetSourceIdForWebContentsDocument(web_contents());
}
AddressNormalizer* ChromeAutofillClient::GetAddressNormalizer() {
return AddressNormalizerFactory::GetInstance();
}
AutofillOfferManager* ChromeAutofillClient::GetAutofillOfferManager() {
return AutofillOfferManagerFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
}
const GURL& ChromeAutofillClient::GetLastCommittedURL() const {
return web_contents()->GetLastCommittedURL();
}
security_state::SecurityLevel
ChromeAutofillClient::GetSecurityLevelForUmaHistograms() {
SecurityStateTabHelper* helper =
::SecurityStateTabHelper::FromWebContents(web_contents());
// If there is no helper, it means we are not in a "web" state (for example
// the file picker on CrOS). Return SECURITY_LEVEL_COUNT which will not be
// logged.
if (!helper)
return security_state::SecurityLevel::SECURITY_LEVEL_COUNT;
return helper->GetSecurityLevel();
}
const translate::LanguageState* ChromeAutofillClient::GetLanguageState() {
// TODO(crbug.com/912597): iOS vs other platforms extracts the language from
// the top level frame vs whatever frame directly holds the form.
auto* translate_manager =
ChromeTranslateClient::GetManagerFromWebContents(web_contents());
if (translate_manager)
return translate_manager->GetLanguageState();
return nullptr;
}
translate::TranslateDriver* ChromeAutofillClient::GetTranslateDriver() {
// TODO(crbug.com/912597): iOS vs other platforms extracts the language from
// the top level frame vs whatever frame directly holds the form.
auto* translate_client =
ChromeTranslateClient::FromWebContents(web_contents());
if (translate_client)
return translate_client->translate_driver();
return nullptr;
}
std::string ChromeAutofillClient::GetVariationConfigCountryCode() const {
variations::VariationsService* variation_service =
g_browser_process->variations_service();
// Retrieves the country code from variation service and converts it to upper
// case.
return variation_service
? base::ToUpperASCII(variation_service->GetLatestCountry())
: std::string();
}
profile_metrics::BrowserProfileType ChromeAutofillClient::GetProfileType()
const {
Profile* profile = GetProfile();
// Profile can only be null in tests, therefore it is safe to always return
// |kRegular| when it does not exist.
return profile ? profile_metrics::GetBrowserProfileType(profile)
: profile_metrics::BrowserProfileType::kRegular;
}
std::unique_ptr<webauthn::InternalAuthenticator>
ChromeAutofillClient::CreateCreditCardInternalAuthenticator(
content::RenderFrameHost* rfh) {
#if BUILDFLAG(IS_ANDROID)
return std::make_unique<InternalAuthenticatorAndroid>(rfh);
#else
return std::make_unique<content::InternalAuthenticatorImpl>(rfh);
#endif
}
void ChromeAutofillClient::ShowAutofillSettings(
bool show_credit_card_settings) {
#if BUILDFLAG(IS_ANDROID)
if (show_credit_card_settings) {
ShowAutofillCreditCardSettings(web_contents());
} else {
ShowAutofillProfileSettings(web_contents());
}
#else
Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
if (browser) {
if (show_credit_card_settings) {
chrome::ShowSettingsSubPage(browser, chrome::kPaymentsSubPage);
} else {
chrome::ShowSettingsSubPage(browser, chrome::kAddressesSubPage);
}
}
#endif // BUILDFLAG(IS_ANDROID)
}
void ChromeAutofillClient::ShowCardUnmaskOtpInputDialog(
const size_t& otp_length,
base::WeakPtr<OtpUnmaskDelegate> delegate) {
CardUnmaskOtpInputDialogControllerImpl::CreateForWebContents(web_contents());
CardUnmaskOtpInputDialogControllerImpl* controller =
CardUnmaskOtpInputDialogControllerImpl::FromWebContents(web_contents());
DCHECK(controller);
controller->ShowDialog(otp_length, delegate);
}
void ChromeAutofillClient::OnUnmaskOtpVerificationResult(
OtpUnmaskResult unmask_result) {
CardUnmaskOtpInputDialogControllerImpl::CreateForWebContents(web_contents());
CardUnmaskOtpInputDialogControllerImpl* controller =
CardUnmaskOtpInputDialogControllerImpl::FromWebContents(web_contents());
DCHECK(controller);
controller->OnOtpVerificationResult(unmask_result);
}
void ChromeAutofillClient::ShowUnmaskPrompt(
const CreditCard& card,
UnmaskCardReason reason,
base::WeakPtr<CardUnmaskDelegate> delegate) {
unmask_controller_.ShowPrompt(
base::BindOnce(&CreateCardUnmaskPromptView,
base::Unretained(&unmask_controller_),
base::Unretained(web_contents())),
card, reason, delegate);
}
// TODO(crbug.com/1220990): Refactor this for both CVC and Biometrics flows.
void ChromeAutofillClient::OnUnmaskVerificationResult(
PaymentsRpcResult result) {
unmask_controller_.OnVerificationResult(result);
#if BUILDFLAG(IS_ANDROID)
// For VCN related errors, on Android we show a new error dialog instead of
// updating the CVC unmask prompt with the error message.
switch (result) {
case AutofillClient::PaymentsRpcResult::kVcnRetrievalPermanentFailure:
ShowVirtualCardErrorDialog(/*is_permanent_error=*/true);
break;
case AutofillClient::PaymentsRpcResult::kVcnRetrievalTryAgainFailure:
ShowVirtualCardErrorDialog(/*is_permanent_error=*/false);
break;
case AutofillClient::PaymentsRpcResult::kSuccess:
case AutofillClient::PaymentsRpcResult::kTryAgainFailure:
case AutofillClient::PaymentsRpcResult::kPermanentFailure:
case AutofillClient::PaymentsRpcResult::kNetworkError:
// Do nothing
break;
case AutofillClient::PaymentsRpcResult::kNone:
NOTREACHED();
return;
}
#endif // BUILDFLAG(IS_ANDROID)
}
void ChromeAutofillClient::ShowUnmaskAuthenticatorSelectionDialog(
const std::vector<CardUnmaskChallengeOption>& challenge_options,
base::OnceCallback<void(const std::string&)>
confirm_unmask_challenge_option_callback,
base::OnceClosure cancel_unmasking_closure) {
CardUnmaskAuthenticationSelectionDialogControllerImpl::GetOrCreate(
web_contents())
->ShowDialog(challenge_options,
std::move(confirm_unmask_challenge_option_callback),
std::move(cancel_unmasking_closure));
}
void ChromeAutofillClient::DismissUnmaskAuthenticatorSelectionDialog(
bool server_success) {
CardUnmaskAuthenticationSelectionDialogControllerImpl::GetOrCreate(
web_contents())
->DismissDialogUponServerProcessedAuthenticationMethodRequest(
server_success);
}
raw_ptr<VirtualCardEnrollmentManager>
ChromeAutofillClient::GetVirtualCardEnrollmentManager() {
return form_data_importer_->GetVirtualCardEnrollmentManager();
}
void ChromeAutofillClient::ShowVirtualCardEnrollDialog(
const VirtualCardEnrollmentFields& virtual_card_enrollment_fields,
base::OnceClosure accept_virtual_card_callback,
base::OnceClosure decline_virtual_card_callback) {
VirtualCardEnrollBubbleControllerImpl::CreateForWebContents(web_contents());
VirtualCardEnrollBubbleControllerImpl* controller =
VirtualCardEnrollBubbleControllerImpl::FromWebContents(web_contents());
DCHECK(controller);
controller->ShowBubble(virtual_card_enrollment_fields,
std::move(accept_virtual_card_callback),
std::move(decline_virtual_card_callback));
}
#if !BUILDFLAG(IS_ANDROID)
std::vector<std::string>
ChromeAutofillClient::GetAllowedMerchantsForVirtualCards() {
if (!prefs::IsAutofillCreditCardEnabled(GetPrefs()))
return std::vector<std::string>();
return AutofillGstaticReader::GetInstance()
->GetTokenizationMerchantAllowlist();
}
std::vector<std::string>
ChromeAutofillClient::GetAllowedBinRangesForVirtualCards() {
if (!prefs::IsAutofillCreditCardEnabled(GetPrefs()))
return std::vector<std::string>();
return AutofillGstaticReader::GetInstance()
->GetTokenizationBinRangesAllowlist();
}
void ChromeAutofillClient::ShowLocalCardMigrationDialog(
base::OnceClosure show_migration_dialog_closure) {
ManageMigrationUiController::CreateForWebContents(web_contents());
ManageMigrationUiController* controller =
ManageMigrationUiController::FromWebContents(web_contents());
controller->ShowBubble(std::move(show_migration_dialog_closure));
}
void ChromeAutofillClient::ConfirmMigrateLocalCardToCloud(
const LegalMessageLines& legal_message_lines,
const std::string& user_email,
const std::vector<MigratableCreditCard>& migratable_credit_cards,
LocalCardMigrationCallback start_migrating_cards_callback) {
ManageMigrationUiController::CreateForWebContents(web_contents());
ManageMigrationUiController* controller =
ManageMigrationUiController::FromWebContents(web_contents());
controller->ShowOfferDialog(legal_message_lines, user_email,
migratable_credit_cards,
std::move(start_migrating_cards_callback));
}
void ChromeAutofillClient::ShowLocalCardMigrationResults(
const bool has_server_error,
const std::u16string& tip_message,
const std::vector<MigratableCreditCard>& migratable_credit_cards,
MigrationDeleteCardCallback delete_local_card_callback) {
ManageMigrationUiController::CreateForWebContents(web_contents());
ManageMigrationUiController* controller =
ManageMigrationUiController::FromWebContents(web_contents());
controller->UpdateCreditCardIcon(has_server_error, tip_message,
migratable_credit_cards,
delete_local_card_callback);
}
void ChromeAutofillClient::ShowWebauthnOfferDialog(
WebauthnDialogCallback offer_dialog_callback) {
WebauthnDialogControllerImpl::CreateForWebContents(web_contents());
WebauthnDialogControllerImpl::FromWebContents(web_contents())
->ShowOfferDialog(std::move(offer_dialog_callback));
}
void ChromeAutofillClient::ShowWebauthnVerifyPendingDialog(
WebauthnDialogCallback verify_pending_dialog_callback) {
WebauthnDialogControllerImpl::CreateForWebContents(web_contents());
WebauthnDialogControllerImpl::FromWebContents(web_contents())
->ShowVerifyPendingDialog(std::move(verify_pending_dialog_callback));
}
void ChromeAutofillClient::UpdateWebauthnOfferDialogWithError() {
WebauthnDialogControllerImpl* controller =
WebauthnDialogControllerImpl::FromWebContents(web_contents());
if (controller)
controller->UpdateDialog(WebauthnDialogState::kOfferError);
}
bool ChromeAutofillClient::CloseWebauthnDialog() {
WebauthnDialogControllerImpl* controller =
WebauthnDialogControllerImpl::FromWebContents(web_contents());
if (controller)
return controller->CloseDialog();
return false;
}
void ChromeAutofillClient::ConfirmSaveUpiIdLocally(
const std::string& upi_id,
base::OnceCallback<void(bool accept)> callback) {
SaveUPIBubbleControllerImpl::CreateForWebContents(web_contents());
SaveUPIBubbleControllerImpl* controller =
SaveUPIBubbleControllerImpl::FromWebContents(web_contents());
controller->OfferUpiIdLocalSave(upi_id, std::move(callback));
}
void ChromeAutofillClient::OfferVirtualCardOptions(
const std::vector<CreditCard*>& candidates,
base::OnceCallback<void(const std::string&)> callback) {
VirtualCardSelectionDialogControllerImpl::CreateForWebContents(
web_contents());
VirtualCardSelectionDialogControllerImpl::FromWebContents(web_contents())
->ShowDialog(candidates, std::move(callback));
}
#else // BUILDFLAG(IS_ANDROID)
void ChromeAutofillClient::ConfirmAccountNameFixFlow(
base::OnceCallback<void(const std::u16string&)> callback) {
DCHECK(!messages::IsSaveCardMessagesUiEnabled());
CardNameFixFlowViewAndroid* card_name_fix_flow_view_android =
new CardNameFixFlowViewAndroid(&card_name_fix_flow_controller_,
web_contents());
card_name_fix_flow_controller_.Show(
card_name_fix_flow_view_android, GetAccountHolderName(),
/*upload_save_card_callback=*/std::move(callback));
}
void ChromeAutofillClient::ConfirmExpirationDateFixFlow(
const CreditCard& card,
base::OnceCallback<void(const std::u16string&, const std::u16string&)>
callback) {
DCHECK(!messages::IsSaveCardMessagesUiEnabled());
CardExpirationDateFixFlowViewAndroid*
card_expiration_date_fix_flow_view_android =
new CardExpirationDateFixFlowViewAndroid(
&card_expiration_date_fix_flow_controller_, web_contents());
card_expiration_date_fix_flow_controller_.Show(
card_expiration_date_fix_flow_view_android, card,
/*upload_save_card_callback=*/std::move(callback));
}
#endif
void ChromeAutofillClient::ConfirmSaveCreditCardLocally(
const CreditCard& card,
SaveCreditCardOptions options,
LocalSaveCardPromptCallback callback) {
#if BUILDFLAG(IS_ANDROID)
DCHECK(options.show_prompt);
if (messages::IsSaveCardMessagesUiEnabled()) {
save_card_message_controller_android_.Show(
web_contents(), options, card, /*legal_message_lines=*/{},
GetAccountHolderName(),
/*upload_save_card_callback=*/{},
/*local_save_card_callback=*/std::move(callback));
return;
}
infobars::ContentInfoBarManager::FromWebContents(web_contents())
->AddInfoBar(CreateSaveCardInfoBarMobile(
std::make_unique<AutofillSaveCardInfoBarDelegateMobile>(
/*upload=*/false, options, card, LegalMessageLines(),
AutofillClient::UploadSaveCardPromptCallback(),
/*local_save_card_callback=*/std::move(callback),
AccountInfo())));
#else
// Do lazy initialization of SaveCardBubbleControllerImpl.
SaveCardBubbleControllerImpl::CreateForWebContents(web_contents());
SaveCardBubbleControllerImpl* controller =
SaveCardBubbleControllerImpl::FromWebContents(web_contents());
controller->OfferLocalSave(card, options, std::move(callback));
#endif
}
void ChromeAutofillClient::ConfirmSaveCreditCardToCloud(
const CreditCard& card,
const LegalMessageLines& legal_message_lines,
SaveCreditCardOptions options,
UploadSaveCardPromptCallback callback) {
#if BUILDFLAG(IS_ANDROID)
DCHECK(options.show_prompt);
if (messages::IsSaveCardMessagesUiEnabled()) {
save_card_message_controller_android_.Show(web_contents(), options, card,
legal_message_lines,
GetAccountHolderName(),
/*upload_save_card_callback=*/
std::move(callback),
/*local_save_card_callback=*/{});
return;
}
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(GetProfile());
AccountInfo account_info = identity_manager->FindExtendedAccountInfo(
identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin));
infobars::ContentInfoBarManager::FromWebContents(web_contents())
->AddInfoBar(CreateSaveCardInfoBarMobile(
std::make_unique<AutofillSaveCardInfoBarDelegateMobile>(
/*upload=*/true, options, card, legal_message_lines,
/*upload_save_card_callback=*/std::move(callback),
AutofillClient::LocalSaveCardPromptCallback(), account_info)));
#else
// Do lazy initialization of SaveCardBubbleControllerImpl.
SaveCardBubbleControllerImpl::CreateForWebContents(web_contents());
SaveCardBubbleControllerImpl* controller =
SaveCardBubbleControllerImpl::FromWebContents(web_contents());
controller->OfferUploadSave(card, legal_message_lines, options,
std::move(callback));
#endif
}
void ChromeAutofillClient::CreditCardUploadCompleted(bool card_saved) {
#if BUILDFLAG(IS_ANDROID)
// TODO(siyua@): Placeholder for Clank Notification.
#else
if (!base::FeatureList::IsEnabled(
features::kAutofillCreditCardUploadFeedback)) {
return;
}
// Do lazy initialization of SaveCardBubbleControllerImpl.
SaveCardBubbleControllerImpl::CreateForWebContents(web_contents());
SaveCardBubbleControllerImpl* controller =
SaveCardBubbleControllerImpl::FromWebContents(web_contents());
card_saved ? controller->UpdateIconForSaveCardSuccess()
: controller->UpdateIconForSaveCardFailure();
#endif
}
void ChromeAutofillClient::ConfirmCreditCardFillAssist(
const CreditCard& card,
base::OnceClosure callback) {
#if BUILDFLAG(IS_ANDROID)
auto infobar_delegate =
std::make_unique<AutofillCreditCardFillingInfoBarDelegateMobile>(
card, std::move(callback));
auto* raw_delegate = infobar_delegate.get();
if (infobars::ContentInfoBarManager::FromWebContents(web_contents())
->AddInfoBar(std::make_unique<AutofillCreditCardFillingInfoBar>(
std::move(infobar_delegate)))) {
raw_delegate->set_was_shown();
}
#endif
}
void ChromeAutofillClient::ConfirmSaveAddressProfile(
const AutofillProfile& profile,
const AutofillProfile* original_profile,
SaveAddressProfilePromptOptions options,
AddressProfileSavePromptCallback callback) {
#if BUILDFLAG(IS_ANDROID)
// TODO(crbug.com/1167061): Respect SaveAddressProfilePromptOptions.
save_update_address_profile_flow_manager_.OfferSave(
web_contents(), profile, original_profile, std::move(callback));
#else
SaveUpdateAddressProfileBubbleControllerImpl::CreateForWebContents(
web_contents());
SaveUpdateAddressProfileBubbleControllerImpl* controller =
SaveUpdateAddressProfileBubbleControllerImpl::FromWebContents(
web_contents());
controller->OfferSave(profile, original_profile, options,
std::move(callback));
#endif
}
bool ChromeAutofillClient::HasCreditCardScanFeature() {
return CreditCardScannerController::HasCreditCardScanFeature();
}
void ChromeAutofillClient::ScanCreditCard(CreditCardScanCallback callback) {
CreditCardScannerController::ScanCreditCard(web_contents(),
std::move(callback));
}
void ChromeAutofillClient::ShowAutofillPopup(
const autofill::AutofillClient::PopupOpenArgs& open_args,
base::WeakPtr<AutofillPopupDelegate> delegate) {
// Autofill popups should only be shown in focused windows because on Windows
// the popup may overlap the focused window (see crbug.com/1239760).
if (!has_focus_)
return;
// Don't show any popups while Autofill Assistant's UI is shown.
if (IsAutofillAssistantShowing()) {
return;
}
// Convert element_bounds to be in screen space.
gfx::Rect client_area = web_contents()->GetContainerBounds();
gfx::RectF element_bounds_in_screen_space =
open_args.element_bounds + client_area.OffsetFromOrigin();
// Will delete or reuse the old |popup_controller_|.
popup_controller_ = AutofillPopupControllerImpl::GetOrCreate(
popup_controller_, delegate, web_contents(),
web_contents()->GetNativeView(), element_bounds_in_screen_space,
open_args.text_direction);
popup_controller_->Show(open_args.suggestions,
open_args.autoselect_first_suggestion.value(),
open_args.popup_type);
// When testing, try to keep popup open when the reason to hide is from an
// external browser frame resize that is extraneous to our testing goals.
if (keep_popup_open_for_testing_ && popup_controller_.get()) {
popup_controller_->KeepPopupOpenForTesting();
}
}
void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
const std::vector<std::u16string>& values,
const std::vector<std::u16string>& labels) {
if (popup_controller_.get())
popup_controller_->UpdateDataListValues(values, labels);
}
base::span<const Suggestion> ChromeAutofillClient::GetPopupSuggestions() const {
if (!popup_controller_.get())
return base::span<const Suggestion>();
return popup_controller_->GetUnelidedSuggestions();
}
void ChromeAutofillClient::PinPopupView() {
if (popup_controller_.get())
popup_controller_->PinView();
}
autofill::AutofillClient::PopupOpenArgs
ChromeAutofillClient::GetReopenPopupArgs() const {
const AutofillPopupController* controller = popup_controller_.get();
if (!controller)
return autofill::AutofillClient::PopupOpenArgs();
// By calculating the screen space-independent values, bounds can be passed to
// |ShowAutofillPopup| which always computes the bounds in the screen space.
gfx::Rect client_area = web_contents()->GetContainerBounds();
gfx::RectF screen_space_independent_bounds =
controller->element_bounds() - client_area.OffsetFromOrigin();
return autofill::AutofillClient::PopupOpenArgs(
screen_space_independent_bounds,
controller->IsRTL() ? base::i18n::RIGHT_TO_LEFT
: base::i18n::LEFT_TO_RIGHT,
controller->GetSuggestions(), AutoselectFirstSuggestion(false),
controller->GetPopupType());
}
void ChromeAutofillClient::UpdatePopup(
const std::vector<Suggestion>& suggestions,
PopupType popup_type) {
if (!popup_controller_.get())
return; // Update only if there is a popup.
// When a form changes dynamically, |popup_controller_| may hold a delegate of
// the wrong type, so updating the popup would call into the wrong delegate.
// Hence, just close the existing popup (crbug/1113241).
// The cast is needed to access AutofillPopupController::GetPopupType().
if (popup_type !=
static_cast<const AutofillPopupController*>(popup_controller_.get())
->GetPopupType()) {
popup_controller_->Hide(PopupHidingReason::kStaleData);
return;
}
// Calling show will reuse the existing view automatically
popup_controller_->Show(suggestions, /*autoselect_first_suggestion=*/false,
popup_type);
}
void ChromeAutofillClient::HideAutofillPopup(PopupHidingReason reason) {
if (popup_controller_.get())
popup_controller_->Hide(reason);
}
void ChromeAutofillClient::UpdateOfferNotification(
const AutofillOfferData* offer,
bool notification_has_been_shown) {
DCHECK(offer);
CreditCard* card =
offer->eligible_instrument_id.empty()
? nullptr
: GetPersonalDataManager()->GetCreditCardByInstrumentId(
offer->eligible_instrument_id[0]);
if (offer->IsCardLinkedOffer() && !card)
return;
#if BUILDFLAG(IS_ANDROID)
if (notification_has_been_shown) {
// For Android, if notification has been shown on this merchant, don't show
// it again.
return;
}
std::unique_ptr<OfferNotificationInfoBarControllerImpl> controller =
std::make_unique<OfferNotificationInfoBarControllerImpl>(web_contents());
controller->ShowIfNecessary(offer, card);
#else
OfferNotificationBubbleControllerImpl::CreateForWebContents(web_contents());
OfferNotificationBubbleControllerImpl* controller =
OfferNotificationBubbleControllerImpl::FromWebContents(web_contents());
controller->ShowOfferNotificationIfApplicable(
offer, card, /*should_show_icon_only_=*/notification_has_been_shown);
#endif
}
void ChromeAutofillClient::DismissOfferNotification() {
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<OfferNotificationInfoBarControllerImpl> controller =
std::make_unique<OfferNotificationInfoBarControllerImpl>(web_contents());
DCHECK(controller);
controller->Dismiss();
#else
OfferNotificationBubbleControllerImpl* controller =
OfferNotificationBubbleControllerImpl::FromWebContents(web_contents());
if (!controller)
return;
controller->DismissNotification();
#endif
}
void ChromeAutofillClient::OnVirtualCardDataAvailable(
const std::u16string& masked_card_identifier_string,
const CreditCard* credit_card,
const std::u16string& cvc,
const gfx::Image& card_image) {
DCHECK(credit_card);
DCHECK(!cvc.empty());
GetFormDataImporter()->CacheFetchedVirtualCard(credit_card->LastFourDigits());
#if BUILDFLAG(IS_ANDROID)
// Show the virtual card snackbar only if the keyboard accessory feature is
// enabled. This is because the ManualFillingComponent for credit cards is
// only enabled when keyboard accessory is enabled.
if (features::IsAutofillManualFallbackEnabled()) {
(new AutofillSnackbarControllerImpl(web_contents()))->Show();
}
#else
VirtualCardManualFallbackBubbleControllerImpl::CreateForWebContents(
web_contents());
VirtualCardManualFallbackBubbleControllerImpl* controller =
VirtualCardManualFallbackBubbleControllerImpl::FromWebContents(
web_contents());
controller->ShowBubble(masked_card_identifier_string, credit_card, cvc,
card_image);
#endif
}
void ChromeAutofillClient::ShowVirtualCardErrorDialog(bool is_permanent_error) {
AutofillErrorDialogType error_dialog_type =
is_permanent_error
? AutofillErrorDialogType::VIRTUAL_CARD_PERMANENT_ERROR
: AutofillErrorDialogType::VIRTUAL_CARD_TEMPORARY_ERROR;
autofill_error_dialog_controller_.Show(error_dialog_type);
}
void ChromeAutofillClient::ShowAutofillProgressDialog(
base::OnceClosure cancel_callback) {
DCHECK(autofill_progress_dialog_controller_);
autofill_progress_dialog_controller_->ShowDialog(std::move(cancel_callback));
}
void ChromeAutofillClient::CloseAutofillProgressDialog(
bool show_confirmation_before_closing) {
DCHECK(autofill_progress_dialog_controller_);
autofill_progress_dialog_controller_->DismissDialog(
show_confirmation_before_closing);
}
bool ChromeAutofillClient::IsAutofillAssistantShowing() {
auto* assistant_runtime_manager =
autofill_assistant::RuntimeManager::GetForWebContents(web_contents());
return assistant_runtime_manager && assistant_runtime_manager->GetState() ==
autofill_assistant::UIState::kShown;
}
bool ChromeAutofillClient::IsAutocompleteEnabled() {
return prefs::IsAutocompleteEnabled(GetPrefs());
}
bool ChromeAutofillClient::IsPasswordManagerEnabled() {
return GetPrefs()->GetBoolean(
password_manager::prefs::kCredentialsEnableService);
}
void ChromeAutofillClient::PropagateAutofillPredictions(
content::RenderFrameHost* rfh,
const std::vector<FormStructure*>& forms) {
password_manager::ContentPasswordManagerDriver* driver =
password_manager::ContentPasswordManagerDriver::GetForRenderFrameHost(
rfh);
if (driver) {
driver->GetPasswordManager()->ProcessAutofillPredictions(driver, forms);
}
}
void ChromeAutofillClient::DidFillOrPreviewField(
const std::u16string& autofilled_value,
const std::u16string& profile_full_name) {
#if BUILDFLAG(IS_ANDROID)
AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value,
profile_full_name);
#endif // BUILDFLAG(IS_ANDROID)
}
bool ChromeAutofillClient::IsContextSecure() const {
SecurityStateTabHelper* helper =
SecurityStateTabHelper::FromWebContents(web_contents());
if (!helper)
return false;
const auto security_level = helper->GetSecurityLevel();
content::NavigationEntry* entry =
web_contents()->GetController().GetVisibleEntry();
// Only dangerous security states should prevent autofill.
//
// TODO(crbug.com/701018): Once passive mixed content and legacy TLS are less
// common, just use IsSslCertificateValid().
return entry && entry->GetURL().SchemeIsCryptographic() &&
security_level != security_state::DANGEROUS;
}
bool ChromeAutofillClient::ShouldShowSigninPromo() {
#if !BUILDFLAG(IS_ANDROID)
return false;
#else
return signin::ShouldShowPromo(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
#endif
}
bool ChromeAutofillClient::AreServerCardsSupported() const {
// When in VR, server side cards are not supported.
return !vr::VrTabHelper::IsInVr(web_contents());
}
void ChromeAutofillClient::ExecuteCommand(int id) {
#if BUILDFLAG(IS_ANDROID)
if (id == POPUP_ITEM_ID_CREDIT_CARD_SIGNIN_PROMO) {
auto* window = web_contents()->GetNativeView()->GetWindowAndroid();
if (window) {
SigninBridge::LaunchSigninActivity(
window, signin_metrics::AccessPoint::ACCESS_POINT_AUTOFILL_DROPDOWN);
}
}
#endif
}
LogManager* ChromeAutofillClient::GetLogManager() const {
return log_manager_.get();
}
void ChromeAutofillClient::LoadRiskData(
base::OnceCallback<void(const std::string&)> callback) {
risk_util::LoadRiskData(0, web_contents(), std::move(callback));
}
void ChromeAutofillClient::PrimaryMainFrameWasResized(bool width_changed) {
#if BUILDFLAG(IS_ANDROID)
// Ignore virtual keyboard showing and hiding a strip of suggestions.
if (!width_changed)
return;
#endif
HideAutofillPopup(PopupHidingReason::kWidgetChanged);
}
void ChromeAutofillClient::WebContentsDestroyed() {
HideAutofillPopup(PopupHidingReason::kTabGone);
}
void ChromeAutofillClient::OnWebContentsLostFocus(
content::RenderWidgetHost* render_widget_host) {
has_focus_ = false;
HideAutofillPopup(PopupHidingReason::kFocusChanged);
}
void ChromeAutofillClient::OnWebContentsFocused(
content::RenderWidgetHost* render_widget_host) {
has_focus_ = true;
#if BUILDFLAG(IS_ANDROID)
save_card_message_controller_android_.OnWebContentsFocused();
#endif
}
#if !BUILDFLAG(IS_ANDROID)
void ChromeAutofillClient::OnZoomChanged(
const zoom::ZoomController::ZoomChangedEventData& data) {
HideAutofillPopup(PopupHidingReason::kContentAreaMoved);
}
#endif // !BUILDFLAG(IS_ANDROID)
ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
: content::WebContentsUserData<ChromeAutofillClient>(*web_contents),
content::WebContentsObserver(web_contents),
payments_client_(std::make_unique<payments::PaymentsClient>(
Profile::FromBrowserContext(web_contents->GetBrowserContext())
->GetURLLoaderFactory(),
GetIdentityManager(),
GetPersonalDataManager(),
Profile::FromBrowserContext(web_contents->GetBrowserContext())
->IsOffTheRecord())),
form_data_importer_(std::make_unique<FormDataImporter>(
this,
payments_client_.get(),
GetPersonalDataManager(),
GetPersonalDataManager()->app_locale())),
unmask_controller_(
user_prefs::UserPrefs::Get(web_contents->GetBrowserContext())),
autofill_error_dialog_controller_(web_contents),
autofill_progress_dialog_controller_(
std::make_unique<AutofillProgressDialogControllerImpl>(
web_contents)) {
// TODO(crbug.com/928595): Replace the closure with a callback to the
// renderer that indicates if log messages should be sent from the
// renderer.
log_manager_ =
LogManager::Create(AutofillLogRouterFactory::GetForBrowserContext(
web_contents->GetBrowserContext()),
base::NullCallback());
// Initialize StrikeDatabase so its cache will be loaded and ready to use
// when requested by other Autofill classes.
GetStrikeDatabase();