File: autofill_agent.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1389 lines) | stat: -rw-r--r-- 55,278 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/autofill/ios/browser/autofill_agent.h"

#import <UIKit/UIKit.h>

#import <algorithm>
#import <cstdint>
#import <memory>
#import <optional>
#import <string>
#import <tuple>
#import <utility>
#import <variant>

#import "base/apple/foundation_util.h"
#import "base/check_op.h"
#import "base/containers/map_util.h"
#import "base/debug/crash_logging.h"
#import "base/feature_list.h"
#import "base/format_macros.h"
#import "base/functional/bind.h"
#import "base/json/json_reader.h"
#import "base/json/json_writer.h"
#import "base/memory/raw_ptr.h"
#import "base/memory/weak_ptr.h"
#import "base/metrics/field_trial.h"
#import "base/metrics/histogram_functions.h"
#import "base/strings/string_number_conversions.h"
#import "base/strings/sys_string_conversions.h"
#import "base/strings/utf_string_conversions.h"
#import "base/time/time.h"
#import "base/types/cxx23_to_underlying.h"
#import "base/types/zip.h"
#import "base/uuid.h"
#import "base/values.h"
#import "build/branding_buildflags.h"
#import "components/autofill/core/browser/autofill_field.h"
#import "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#import "components/autofill/core/browser/data_model/payments/credit_card.h"
#import "components/autofill/core/browser/filling/filling_product.h"
#import "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#import "components/autofill/core/browser/metrics/autofill_metrics.h"
#import "components/autofill/core/browser/suggestions/suggestion.h"
#import "components/autofill/core/browser/suggestions/suggestion_type.h"
#import "components/autofill/core/common/autofill_constants.h"
#import "components/autofill/core/common/autofill_features.h"
#import "components/autofill/core/common/autofill_payments_features.h"
#import "components/autofill/core/common/autofill_prefs.h"
#import "components/autofill/core/common/autofill_util.h"
#import "components/autofill/core/common/field_data_manager.h"
#import "components/autofill/core/common/form_data.h"
#import "components/autofill/core/common/form_data_predictions.h"
#import "components/autofill/core/common/form_field_data.h"
#import "components/autofill/core/common/unique_ids.h"
#import "components/autofill/ios/browser/autofill_driver_ios.h"
#import "components/autofill/ios/browser/autofill_driver_ios_bridge.h"
#import "components/autofill/ios/browser/autofill_java_script_feature.h"
#import "components/autofill/ios/browser/autofill_util.h"
#import "components/autofill/ios/browser/form_suggestion.h"
#import "components/autofill/ios/browser/form_suggestion_provider.h"
#import "components/autofill/ios/browser/password_autofill_agent.h"
#import "components/autofill/ios/common/features.h"
#import "components/autofill/ios/common/field_data_manager_factory_ios.h"
#import "components/autofill/ios/form_util/autofill_form_features_injector.h"
#import "components/autofill/ios/form_util/autofill_form_features_java_script_feature.h"
#import "components/autofill/ios/form_util/form_activity_observer_bridge.h"
#import "components/autofill/ios/form_util/form_activity_params.h"
#import "components/autofill/ios/form_util/form_handlers_java_script_feature.h"
#import "components/autofill/ios/form_util/form_util_java_script_feature.h"
#import "components/feature_engagement/public/feature_constants.h"
#import "components/grit/components_resources.h"
#import "components/plus_addresses/features.h"
#import "components/plus_addresses/grit/plus_addresses_strings.h"
#import "components/prefs/ios/pref_observer_bridge.h"
#import "components/prefs/pref_change_registrar.h"
#import "components/prefs/pref_service.h"
#import "components/ukm/ios/ukm_url_recorder.h"
#import "ios/web/common/url_scheme_util.h"
#import "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/js_messaging/web_frames_manager.h"
#import "ios/web/public/js_messaging/web_frames_manager_observer_bridge.h"
#import "ios/web/public/navigation/navigation_context.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_observer_bridge.h"
#import "services/metrics/public/cpp/ukm_builders.h"
#import "ui/base/l10n/l10n_util_mac.h"
#import "ui/base/resource/resource_bundle.h"
#import "ui/gfx/geometry/rect.h"
#import "ui/gfx/image/image.h"
#import "url/gurl.h"

using autofill::AutofillFormFeaturesInjector;
using autofill::AutofillFormFeaturesJavaScriptFeature;
using autofill::AutofillJavaScriptFeature;
using autofill::FieldDataManager;
using autofill::FieldDataManagerFactoryIOS;
using autofill::FieldGlobalId;
using autofill::FieldRendererId;
using autofill::FormData;
using autofill::FormFieldData;
using autofill::FormGlobalId;
using autofill::FormHandlersJavaScriptFeature;
using autofill::FormRendererId;
using autofill::FieldPropertiesFlags::kAutofilledOnUserTrigger;
using base::NumberToString;
using base::SysNSStringToUTF16;
using base::SysNSStringToUTF8;
using base::SysUTF16ToNSString;
using base::SysUTF8ToNSString;

namespace {

using FormDataVector = std::vector<FormData>;
// Maps each field id to their respective host form id. This is needed as the
// information linking the fields to their host form is lost between the moment
// of filling and when receiving the filling response.
using FieldToFormLookupMap = std::map<FieldRendererId, FormRendererId>;

// Contains the data for doing filling.
struct AutofillData {
  std::string frameID;
  base::Value::Dict payload;
  FieldToFormLookupMap fieldToFormLookupMap;
};

// Delay for setting an utterance to be queued, it is required to ensure that
// standard announcements have already been started and thus would not interrupt
// the enqueued utterance.
constexpr base::TimeDelta kA11yAnnouncementQueueDelay = base::Seconds(1);

// The correct icon size to use in suggestions. Used to ensure images are scaled
// appropriately.
constexpr CGFloat kSuggestionIconWidth = 32;

bool ContainsFocusableField(const FormData& form, FieldRendererId field_id) {
  auto it =
      std::ranges::find(form.fields(), field_id, &FormFieldData::renderer_id);
  return it != form.fields().end() && it->is_focusable();
}

}  // namespace

@interface AutofillAgent () <CRWWebStateObserver,
                             CRWWebFramesManagerObserver,
                             FormActivityObserver,
                             PrefObserverDelegate> {
  // The WebState this instance is observing. Will be null after
  // -webStateDestroyed: has been called.
  raw_ptr<web::WebState> _webState;

  // Bridge to observe the web state from Objective-C.
  std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge;

  // Bridge to observe the web frames manager from Objective-C.
  std::unique_ptr<web::WebFramesManagerObserverBridge>
      _webFramesManagerObserverBridge;

  // The pref service for which this agent was created.
  raw_ptr<PrefService> _prefService;

  // The unique renderer ID of the most recent autocomplete field;
  // tracks the currently-focused form element in order to force filling of
  // the currently selected form element, even if it's non-empty.
  FieldRendererId _pendingAutocompleteFieldID;

  // Suggestions state:
  // The most recent form suggestions.
  NSArray* _mostRecentSuggestions;

  // The completion to inform FormSuggestionController that a user selection
  // has been handled.
  SuggestionHandledCompletion _suggestionHandledCompletion;

  // The completion to inform FormSuggestionController that suggestions are
  // available for a given form and field.
  SuggestionsAvailableCompletion _suggestionsAvailableCompletion;

  // The text entered by the user into the active field.
  NSString* _typedValue;

  // Delegate for the most recent suggestions.
  // The reference is weak because a weak pointer is sent to our
  // BrowserAutofillManagerDelegate.
  base::WeakPtr<autofill::AutofillSuggestionDelegate> _suggestionDelegate;

  // The autofill data that needs to be sent when the |webState_| is shown.
  std::optional<AutofillData> _pendingFormData;

  // Bridge to listen to pref changes.
  std::unique_ptr<PrefObserverBridge> _prefObserverBridge;
  // Registrar for pref changes notifications.
  PrefChangeRegistrar _prefChangeRegistrar;

  // Bridge to observe form activity in |webState_|.
  std::unique_ptr<autofill::FormActivityObserverBridge>
      _formActivityObserverBridge;

  // ID of the last Autofill query made. Used to discard outdated suggestions.
  FieldGlobalId _lastQueriedFieldID;

  // Helper for setting feature flags in page content world WebFrames.
  std::unique_ptr<AutofillFormFeaturesInjector> _page_world_features_injector;
}

@end

@implementation AutofillAgent

- (instancetype)initWithPrefService:(PrefService*)prefService
                           webState:(web::WebState*)webState {
  DCHECK(prefService);
  DCHECK(webState);
  self = [super init];
  if (self) {
    _webState = webState;
    _webStateObserverBridge =
        std::make_unique<web::WebStateObserverBridge>(self);
    _webState->AddObserver(_webStateObserverBridge.get());
    _webFramesManagerObserverBridge =
        std::make_unique<web::WebFramesManagerObserverBridge>(self);
    web::WebFramesManager* framesManager =
        AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(
            _webState);
    framesManager->AddObserver(_webFramesManagerObserverBridge.get());
    _formActivityObserverBridge =
        std::make_unique<autofill::FormActivityObserverBridge>(_webState, self);
    _prefService = prefService;
    _prefObserverBridge = std::make_unique<PrefObserverBridge>(self);
    _prefChangeRegistrar.Init(prefService);
    _prefObserverBridge->ObserveChangesForPreference(
        autofill::prefs::kAutofillCreditCardEnabled, &_prefChangeRegistrar);
    _prefObserverBridge->ObserveChangesForPreference(
        autofill::prefs::kAutofillProfileEnabled, &_prefChangeRegistrar);

    // Inject feature flags in the page content world when running in the
    // isolated world. Feature flags are needed for the form submission hook
    // that is injected in that the page world.
    if (base::FeatureList::IsEnabled(kAutofillIsolatedWorldForJavascriptIos)) {
      _page_world_features_injector =
          std::make_unique<AutofillFormFeaturesInjector>(
              webState, web::ContentWorld::kPageContentWorld);
    }
  }
  return self;
}

- (void)dealloc {
  if (_webState) {
    [self webStateDestroyed:_webState];
  }
}

#pragma mark - FormSuggestionProvider

- (void)checkIfSuggestionsAvailableForForm:
            (FormSuggestionProviderQuery*)formQuery
                            hasUserGesture:(BOOL)hasUserGesture
                                  webState:(web::WebState*)webState
                         completionHandler:
                             (SuggestionsAvailableCompletion)completion {
  DCHECK_EQ(_webState, webState);

  if (![self isAutofillEnabled]) {
    completion(NO);
    return;
  }

  // Check for suggestions if the form activity is initiated by the user.
  if (!hasUserGesture) {
    completion(NO);
    return;
  }

  web::WebFramesManager* frames_manager =
      AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(_webState);
  web::WebFrame* frame =
      frames_manager->GetFrameWithId(SysNSStringToUTF8(formQuery.frameID));
  if (!frame) {
    completion(NO);
    return;
  }

  auto* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(_webState, frame);
  if (!driver) {
    completion(NO);
    return;
  }

  const auto callback = [](AutofillAgent* agent,
                           FormSuggestionProviderQuery* formQuery,
                           base::WeakPtr<web::WebFrame> frame,
                           base::WeakPtr<web::WebState> webState,
                           SuggestionsAvailableCompletion completion,
                           std::optional<FormDataVector> forms) {
    if (forms && forms->size() == 1) {
      // Once the active form and field are extracted, send a query to the
      // BrowserAutofillManager for suggestions.
      [agent queryAutofillForForm:forms.value()[0]
                  fieldIdentifier:formQuery.fieldRendererID
                             type:formQuery.type
                       typedValue:formQuery.typedValue
                            frame:frame
                         webState:webState
                completionHandler:completion];
    }
  };

  // Re-extract the active form and field only. All forms with at least one
  // input element are considered because key/value suggestions are offered
  // even on short forms.
  driver->FetchFormsFilteredByName(
      SysNSStringToUTF16(formQuery.formName),
      base::BindOnce(callback, self, formQuery, frame->AsWeakPtr(),
                     webState->GetWeakPtr(), completion));
}

- (void)retrieveSuggestionsForForm:(FormSuggestionProviderQuery*)formQuery
                          webState:(web::WebState*)webState
                 completionHandler:(SuggestionsReadyCompletion)completion {
  DCHECK(_mostRecentSuggestions) << "Requestor should have called "
                                 << "|checkIfSuggestionsAvailableForForm:"
                                    "webState:completionHandler:|.";
  completion(_mostRecentSuggestions, self);
}

- (void)didSelectSuggestion:(FormSuggestion*)suggestion
                    atIndex:(NSInteger)index
                       form:(NSString*)formName
             formRendererID:(FormRendererId)formRendererID
            fieldIdentifier:(NSString*)fieldIdentifier
            fieldRendererID:(FieldRendererId)fieldRendererID
                    frameID:(NSString*)frameID
          completionHandler:(SuggestionHandledCompletion)completion {
  [[UIDevice currentDevice] playInputClick];
  DCHECK(completion);
  // TODO(crbug.com/366247033): This double-checks the assumption that this
  // crash is caused by an unexpected suggestion type, and not a nil suggestion.
  // It can be removed once a root cause for the issue is known.
  CHECK(suggestion);

  _suggestionHandledCompletion = [completion copy];

  if (suggestion.acceptanceA11yAnnouncement != nil) {
    // The announcement is done asyncronously with certain delay to make sure
    // it is not interrupted by (almost) immediate standard announcements.
    dispatch_after(
        dispatch_time(DISPATCH_TIME_NOW,
                      kA11yAnnouncementQueueDelay.InNanoseconds()),
        dispatch_get_main_queue(), ^{
          // Queueing flag allows to preserve standard announcements,
          // they are conveyed first and then announce this message.
          // This is a tradeoff as there is no control over the
          // standard utterances (they are interrupting) and it is
          // not desirable to interrupt them. Hence acceptance
          // announcement is done after standard ones (which takes
          // seconds).
          NSAttributedString* message = [[NSAttributedString alloc]
              initWithString:suggestion.acceptanceA11yAnnouncement
                  attributes:@{
                    UIAccessibilitySpeechAttributeQueueAnnouncement : @YES
                  }];
          UIAccessibilityPostNotification(
              UIAccessibilityAnnouncementNotification, message);
        });
  }

  if (suggestion.type == autofill::SuggestionType::kAddressEntry ||
      suggestion.type == autofill::SuggestionType::kCreditCardEntry ||
      suggestion.type == autofill::SuggestionType::kCreateNewPlusAddress ||
      suggestion.type == autofill::SuggestionType::kVirtualCreditCardEntry ||
      suggestion.type ==
          autofill::SuggestionType::kAddressFieldByFieldFilling) {
    _pendingAutocompleteFieldID = fieldRendererID;
    if (_suggestionDelegate) {
      autofill::Suggestion autofill_suggestion;
      autofill_suggestion.main_text.value =
          SysNSStringToUTF16(suggestion.value);
      autofill_suggestion.type = suggestion.type;
      autofill_suggestion.field_by_field_filling_type_used =
          suggestion.fieldByFieldFillingTypeUsed;
      const std::string guid =
          std::holds_alternative<autofill::Suggestion::AutofillProfilePayload>(
              suggestion.payload)
              ? std::get<autofill::Suggestion::AutofillProfilePayload>(
                    suggestion.payload)
                    .guid.value()
              : std::get<autofill::Suggestion::Guid>(suggestion.payload)
                    .value();
      if (guid.empty()) {
        autofill_suggestion.payload = autofill::Suggestion::Payload();
      } else {
        autofill_suggestion.payload = suggestion.payload;
      }

      _suggestionDelegate->DidAcceptSuggestion(autofill_suggestion,
                                               {static_cast<int>(index), 0});
    }
    return;
  }

  web::WebFramesManager* frames_manager =
      AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(_webState);
  web::WebFrame* frame =
      frames_manager->GetFrameWithId(SysNSStringToUTF8(frameID));
  if (!frame) {
    // The frame no longer exists, so the field can not be filled.
    if (SuggestionHandledCompletion c =
            std::exchange(_suggestionHandledCompletion, nil)) {
      c();
    }
    return;
  }

  if (suggestion.type == autofill::SuggestionType::kAutocompleteEntry ||
      suggestion.type == autofill::SuggestionType::kFillExistingPlusAddress) {
    // FormSuggestion is a simple, single value that can be filled out now.
    [self fillField:SysNSStringToUTF8(fieldIdentifier)
        fieldRendererID:fieldRendererID
         formRendererID:formRendererID
               formName:SysNSStringToUTF8(formName)
                  value:SysNSStringToUTF16(suggestion.value)
                inFrame:frame];
  } else if (suggestion.type == autofill::SuggestionType::kUndoOrClear) {
    const auto callback = [](__weak AutofillAgent* agent,
                             base::WeakPtr<web::WebFrame> frame,
                             FormRendererId formId,
                             SuggestionHandledCompletion completion,
                             NSString* jsonString) {
      if (frame) {
        [agent onDidClearFields:jsonString inFrame:frame.get() inForm:formId];
      }
      // Only run the completion if set as it isn't impossible that the provided
      // completion is nil.
      if (completion) {
        completion();
      }
    };

    __weak __typeof(self) weakSelf = self;
    AutofillJavaScriptFeature::GetInstance()->ClearAutofilledFieldsForForm(
        frame, formRendererID, fieldRendererID,
        base::BindOnce(callback, weakSelf, frame->AsWeakPtr(), formRendererID,
                       std::exchange(_suggestionHandledCompletion, nil)));

  } else {
    // TODO(crbug.com/366247033): Remove this crash key once the underlying
    // crash has been fixed.
    SCOPED_CRASH_KEY_NUMBER("Bug366247033", "suggestion_type",
                            static_cast<int>(suggestion.type));
    NOTREACHED();
  }
}

- (SuggestionProviderType)type {
  return SuggestionProviderTypeAutofill;
}

- (autofill::FillingProduct)mainFillingProduct {
  return _suggestionDelegate ? _suggestionDelegate->GetMainFillingProduct()
                             : autofill::FillingProduct::kNone;
}

#pragma mark - AutofillDriverIOSBridge

- (void)fillData:(const std::vector<autofill::FormFieldData::FillData>&)data
         inFrame:(web::WebFrame*)frame {
  base::Value::Dict fieldsData;
  FieldToFormLookupMap fieldToFormLookupMap;

  for (const auto& field : data) {
    // Skip empty fields and those that are not autofilled.
    if (field.value.empty() || !field.is_autofilled) {
      continue;
    }

    base::Value::Dict fieldData;
    fieldData.Set("value", field.value);
    fieldData.Set("section", field.section.ToString());
    fieldData.Set("hostFormId", static_cast<int>(*field.host_form_id));
    fieldsData.Set(NumberToString(*field.renderer_id), std::move(fieldData));

    fieldToFormLookupMap[field.renderer_id] = field.host_form_id;
  }
  auto payload = base::Value::Dict().Set("fields", std::move(fieldsData));
  AutofillData autofillData = {
      .frameID = frame ? frame->GetFrameId() : "",
      .payload = std::move(payload),
      .fieldToFormLookupMap = std::move(fieldToFormLookupMap)};

  // Store the form data when WebState is not visible, to send it as soon as it
  // becomes visible again, e.g., when the CVC unmask prompt is showing.
  if (!_webState->IsVisible()) {
    _pendingFormData = std::move(autofillData);
  } else {
    [self sendData:std::move(autofillData) toFrame:frame];
  }
}

// Similar to `fillField`, but does not rely on `FillActiveFormField`, opting
// instead to find and fill a specific field in `frame` with `value`. In other
// words, `field` need not be `document.activeElement`.
- (void)fillSpecificFormField:(const FieldRendererId&)field
                    withValue:(const std::u16string)value
                      inFrame:(web::WebFrame*)frame {
  base::Value::Dict data;
  data.Set("renderer_id", static_cast<int>(field.value()));
  data.Set("value", value);

  const auto callback =
      [](__weak AutofillAgent* agent, SuggestionHandledCompletion completion,
         FieldRendererId fieldId, base::WeakPtr<web::WebFrame> frame,
         const std::u16string& value, BOOL success) {
        if (success && frame) {
          [agent onDidFillField:fieldId
                           form:std::nullopt
                          frame:frame.get()
                          value:value];
        }
        // Only run the completion if set as it isn't impossible that the
        // provided completion is nil.
        if (completion) {
          completion();
        }
      };

  __weak __typeof(self) weakSelf = self;
  AutofillJavaScriptFeature::GetInstance()->FillSpecificFormField(
      frame, std::move(data),
      base::BindOnce(callback, weakSelf,
                     std::exchange(_suggestionHandledCompletion, nil), field,
                     frame->AsWeakPtr(), value));
}

- (void)handleParsedForms:
            (const std::vector<
                raw_ptr<autofill::FormStructure, VectorExperimental>>&)forms
                  inFrame:(web::WebFrame*)frame {
}

- (void)fillFormDataPredictions:
            (const std::vector<autofill::FormDataPredictions>&)forms
                        inFrame:(web::WebFrame*)frame {
  CHECK(base::FeatureList::IsEnabled(
      autofill::features::test::kAutofillShowTypePredictions));

  base::Value::Dict predictionData;
  for (const auto& form : forms) {
    base::Value::Dict fieldData;
    for (const auto [field, field_prediction] :
         base::zip(form.data.fields(), form.fields)) {
      fieldData.Set(NumberToString(field.renderer_id().value()),
                    base::Value(field_prediction.overall_type));
    }
    predictionData.Set(base::UTF16ToUTF8(form.data.name()),
                       std::move(fieldData));
  }
  AutofillJavaScriptFeature::GetInstance()->FillPredictionData(
      frame, std::move(predictionData));
}

#pragma mark - AutofillClientIOSBridge

- (void)showAutofillPopup:
            (const std::vector<autofill::Suggestion>&)popup_suggestions
       suggestionDelegate:
           (const base::WeakPtr<autofill::AutofillSuggestionDelegate>&)
               delegate {
  // Convert the suggestions into an NSArray for the keyboard.
  NSMutableArray<FormSuggestion*>* suggestions = [[NSMutableArray alloc] init];
  for (auto popup_suggestion : popup_suggestions) {
    // In the Chromium implementation the identifiers represent rows on the
    // drop down of options. These include elements that aren't relevant to us
    // such as separators ... see blink::WebAutofillClient::MenuItemIDSeparator
    // for example. We can't include that enum because it's from WebKit, but
    // fortunately almost all the entries we are interested in (profile or
    // autofill entries) are zero or positive. Negative entries we are
    // interested in is autofill::SuggestionType::kUndoOrClear, used to show the
    // "clear form" button.
    // TODO(crbug.com/40266549): Replace Clear Form with Undo
    NSString* value = nil;
    NSString* minorValue = nil;
    NSString* displayDescription = nil;
    UIImage* icon = nil;

    if (popup_suggestion.type == autofill::SuggestionType::kAutocompleteEntry ||
        popup_suggestion.type == autofill::SuggestionType::kAddressEntry ||
        popup_suggestion.type == autofill::SuggestionType::kCreditCardEntry ||
        popup_suggestion.type ==
            autofill::SuggestionType::kVirtualCreditCardEntry ||
        popup_suggestion.type ==
            autofill::SuggestionType::kAddressFieldByFieldFilling) {
      // Filter out any key/value suggestions if the user hasn't typed yet.
      if (popup_suggestion.type ==
              autofill::SuggestionType::kAutocompleteEntry &&
          _typedValue.length == 0) {
        continue;
      }
      // Value will contain the text to be filled in the selected element while
      // displayDescription will contain a summary of the data to be filled in
      // the other elements.
      value = SysUTF16ToNSString(popup_suggestion.main_text.value);

      if (!popup_suggestion.minor_texts.empty()) {
        // For Virtual Cards, the main_text is just "Virtual card" so we need to
        // include the minor_text (which is the card name + last 4 digits ||
        // card holder's name) as the minorValue.
        minorValue = SysUTF16ToNSString(popup_suggestion.minor_texts[0].value);
      }

      if (!popup_suggestion.labels.empty() &&
          !popup_suggestion.labels.front().empty()) {
        DCHECK_EQ(popup_suggestion.labels.size(), 1U);
        DCHECK_EQ(popup_suggestion.labels[0].size(), 1U);
        displayDescription =
            SysUTF16ToNSString(popup_suggestion.labels[0][0].value);
      }

      // Only show icon for credit card suggestions.
      if (delegate && delegate->GetMainFillingProduct() ==
                          autofill::FillingProduct::kCreditCard) {
        icon = [self createIcon:popup_suggestion];
      }
    } else if (popup_suggestion.type ==
               autofill::SuggestionType::kUndoOrClear) {
      // Show the "clear form" button.
      // TODO(crbug.com/40266549): Replace Clear Form with Undo once this
      // changes
      value = SysUTF16ToNSString(popup_suggestion.main_text.value);
    } else if (popup_suggestion.type ==
                   autofill::SuggestionType::kFillExistingPlusAddress ||
               popup_suggestion.type ==
                   autofill::SuggestionType::kCreateNewPlusAddress) {
      // Show any plus_address suggestions.
      value = SysUTF16ToNSString(popup_suggestion.main_text.value);
      if (!popup_suggestion.labels.empty() &&
          !popup_suggestion.labels.front().empty() &&
          _delegate.isKeyboardAccessoryUpgradeEnabled) {
        displayDescription =
            SysUTF16ToNSString(popup_suggestion.labels[0][0].value);
      }
    }

    if (!value) {
      continue;
    }

    NSString* acceptanceA11yAnnouncement =
        popup_suggestion.acceptance_a11y_announcement.has_value()
            ? SysUTF16ToNSString(*popup_suggestion.acceptance_a11y_announcement)
            : nil;

    autofill::FieldType fieldByFieldFillingTypeUsed =
        (popup_suggestion.field_by_field_filling_type_used
             ? *popup_suggestion.field_by_field_filling_type_used
             : autofill::FieldType::EMPTY_TYPE);

    SuggestionIconType suggestionIconType = SuggestionIconType::kNone;
    if (base::FeatureList::IsEnabled(
            autofill::features::kAutofillEnableSupportForHomeAndWork)) {
      suggestionIconType =
          (popup_suggestion.icon == autofill::Suggestion::Icon::kHome)
              ? SuggestionIconType::kAccountHome
          : (popup_suggestion.icon == autofill::Suggestion::Icon::kWork)
              ? SuggestionIconType::kAccountWork
              : SuggestionIconType::kNone;
    }
    FormSuggestion* suggestion =
        [FormSuggestion suggestionWithValue:value
                                 minorValue:minorValue
                         displayDescription:displayDescription
                                       icon:icon
                                       type:popup_suggestion.type
                                    payload:popup_suggestion.payload
                fieldByFieldFillingTypeUsed:fieldByFieldFillingTypeUsed
                             requiresReauth:NO
                 acceptanceA11yAnnouncement:acceptanceA11yAnnouncement];

    suggestion.featureForIPH = SuggestionFeatureForIPH::kUnknown;
    suggestion.suggestionIconType = suggestionIconType;
    if (popup_suggestion.iph_metadata.feature ==
        &feature_engagement::
            kIPHAutofillExternalAccountProfileSuggestionFeature) {
      suggestion.featureForIPH =
          SuggestionFeatureForIPH::kAutofillExternalAccountProfile;
    } else if (popup_suggestion.iph_metadata.feature ==
               &feature_engagement::kIPHPlusAddressCreateSuggestionFeature) {
      suggestion.featureForIPH = SuggestionFeatureForIPH::kPlusAddressCreation;
    } else if (popup_suggestion.iph_metadata.feature ==
               &feature_engagement::
                   kIPHAutofillHomeWorkProfileSuggestionFeature) {
      suggestion.featureForIPH =
          SuggestionFeatureForIPH::kHomeWorkAddressSuggestion;
    }

    // Put "clear form" entry at the front of the suggestions.
    if (popup_suggestion.type == autofill::SuggestionType::kUndoOrClear) {
      [suggestions insertObject:suggestion atIndex:0];
    } else {
      [suggestions addObject:suggestion];
    }
  }

  [self onSuggestionsReady:suggestions suggestionDelegate:delegate];

  // TODO(crbug.com/363958046): Pass the actually shown suggestions instead of
  // `popup_suggestions`.
  if (delegate) {
    delegate->OnSuggestionsShown(popup_suggestions);
  }
}

- (void)hideAutofillPopup {
  [self
      onSuggestionsReady:@[]
      suggestionDelegate:base::WeakPtr<autofill::AutofillSuggestionDelegate>()];
}

- (bool)isLastQueriedField:(FieldGlobalId)fieldID {
  return fieldID == _lastQueriedFieldID;
}

- (void)showPlusAddressEmailOverrideNotification:
    (base::OnceClosure)emailOverrideUndoCallback {
  CHECK(_delegate);
  [_delegate
      showSnackbarWithMessage:
          l10n_util::GetNSString(
              IDS_PLUS_ADDRESS_SNACKBAR_UNDO_EMAIL_SWAP_DESCRIPTION_TEXT_IOS)
                   buttonText:
                       l10n_util::GetNSString(
                           IDS_PLUS_ADDRESS_SNACKBAR_UNDO_EMAIL_SWAP_ACTION_TEXT_IOS)
                messageAction:base::CallbackToBlock(
                                  std::move(emailOverrideUndoCallback))
             completionAction:nil];
}

#pragma mark - CRWWebStateObserver

- (void)webStateWasShown:(web::WebState*)webState {
  DCHECK_EQ(_webState, webState);
  if (!_pendingFormData) {
    return;
  }

  // The frameID cannot be empty.
  const std::string& frameID = _pendingFormData->frameID;
  CHECK(!frameID.empty());
  web::WebFramesManager* frames_manager =
      AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(_webState);
  web::WebFrame* frame = frames_manager->GetFrameWithId(frameID);
  [self sendData:std::move(*_pendingFormData) toFrame:frame];
  _pendingFormData.reset();
}

- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
  DCHECK_EQ(_webState, webState);
  if (![self isAutofillEnabled]) {
    return;
  }

  [self processPage:webState];
}

- (void)webStateDestroyed:(web::WebState*)webState {
  DCHECK_EQ(_webState, webState);
  if (_webState) {
    _formActivityObserverBridge.reset();
    _webState->RemoveObserver(_webStateObserverBridge.get());
    _webStateObserverBridge.reset();
    web::WebFramesManager* framesManager =
        AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(
            _webState);
    framesManager->RemoveObserver(_webFramesManagerObserverBridge.get());
    _webFramesManagerObserverBridge.reset();
    _webState = nullptr;
  }
  // Do not wait for deallocation. Remove all observers here.
  _prefChangeRegistrar.RemoveAll();
}

#pragma mark - CRWWebFramesManagerObserver

- (void)webFramesManager:(web::WebFramesManager*)webFramesManager
    frameBecameAvailable:(web::WebFrame*)webFrame {
  DCHECK(_webState);
  DCHECK(webFrame);
  if (![self isAutofillEnabled] || _webState->IsLoading()) {
    return;
  }
  if (webFrame->IsMainFrame()) {
    [self processPage:_webState];
    return;
  }
  // Check that the main frame has already been processed.
  if (!webFramesManager->GetMainWebFrame()) {
    return;
  }
  auto* main_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
      _webState, webFramesManager->GetMainWebFrame());
  DLOG_IF(WARNING, !main_driver) << "No AutofillDriverIOS found for WebFrame";
  if (!main_driver || !main_driver->is_processed()) {
    return;
  }
  [self processFrame:webFrame inWebState:_webState];
}

#pragma mark - FormActivityObserver

- (void)webState:(web::WebState*)webState
    didRegisterFormActivity:(const autofill::FormActivityParams&)params
                    inFrame:(web::WebFrame*)frame {
  DCHECK_EQ(_webState, webState);
  if (![self isAutofillEnabled]) {
    return;
  }

  if (!frame) {
    return;
  }

  // Return early if the page is not processed yet.
  auto* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(webState, frame);
  DLOG_IF(WARNING, !driver) << "No AutofillDriverIOS found for WebFrame";
  if (!driver || !driver->is_processed()) {
    return;
  }

  // Return early if |params| is not complete.
  if (params.input_missing) {
    return;
  }

  // If the event is a form_changed, then the event concerns the whole page and
  // not a particular form. The whole document's forms need to be extracted to
  // find the new forms.
  if (params.type == "form_changed") {
    driver->ScanForms();
    return;
  }

  // We are only interested in 'input' events in order to notify the autofill
  // manager for metrics purposes.
  if (params.type != "input" ||
      (params.field_type != "text" && params.field_type != "password")) {
    return;
  }

  // The completion block is executed asynchronously, thus it cannot refer
  // directly to `params.field_identifier` (as params is passed by reference
  // and may have been destroyed by the point the block is executed).
  __weak __typeof(self) weakSelf = self;
  const auto callback =
      [](__weak AutofillAgent* agent, base::WeakPtr<web::WebFrame> frame,
         FieldRendererId fieldId, std::optional<FormDataVector> forms) {
        if (!forms) {
          return;
        }
        [agent onFormsFetched:*forms webFrame:frame fieldIdentifier:fieldId];
      };

  // Extract the active form and field only.
  driver->FetchFormsFilteredByName(
      base::UTF8ToUTF16(params.form_name),
      base::BindOnce(callback, weakSelf, frame->AsWeakPtr(),
                     params.field_renderer_id));
}

- (void)webState:(web::WebState*)webState
    didSubmitDocumentWithFormData:(const FormData&)formData
                   hasUserGesture:(BOOL)hasUserGesture
                          inFrame:(web::WebFrame*)frame {
  if (![self isAutofillEnabled] || !frame) {
    return;
  }

  auto* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(webState, frame);
  if (!driver) {
    return;
  }

  driver->FormSubmitted(formData,
                        autofill::mojom::SubmissionSource::FORM_SUBMISSION);
}

- (void)webState:(web::WebState*)webState
    didRegisterFormRemoval:(const autofill::FormRemovalParams&)params
                   inFrame:(web::WebFrame*)frame {
  CHECK_EQ(_webState, webState);
  CHECK(!params.removed_forms.empty() || !params.removed_unowned_fields.empty())
      << "Invalid params. Form removal events with missing input should have "
         "been filtered out by FormActivityTabHelper.";

  autofill::AutofillDriverIOS* autofillDriver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(webState, frame);
  if (!autofillDriver) {
    return;
  }

  autofillDriver->FormsRemoved(params.removed_forms,
                               params.removed_unowned_fields);
}

#pragma mark - PrefObserverDelegate

- (void)onPreferenceChanged:(const std::string&)preferenceName {
  // Processing the page can be needed here if Autofill is enabled in settings
  // when the page is already loaded.
  if ([self isAutofillEnabled]) {
    [self processPage:_webState];
  }
}

#pragma mark - Private methods

// Returns whether Autofill is enabled by checking if Autofill is turned on and
// if the current URL has a web scheme and the page content is HTML.
- (BOOL)isAutofillEnabled {
  if (!autofill::prefs::IsAutofillProfileEnabled(_prefService) &&
      !autofill::prefs::IsAutofillPaymentMethodsEnabled(_prefService)) {
    return NO;
  }

  // Only web URLs are supported by Autofill.
  return web::UrlHasWebScheme(_webState->GetLastCommittedURL()) &&
         _webState->ContentIsHTML();
}

// Fills a field identified with |fieldIdentifier| on the form named
// |formName| in |frame| using |value| then move the cursor.
// TODO(crbug.com/41284261): |dataString| ends up at fillFormField() in
// autofill_controller.js. fillFormField() expects an AutofillFormFieldData
// object, which |dataString| is not because 'form' is not a specified member of
// AutofillFormFieldData. fillFormField() also expects members 'max_length' and
// 'is_checked' to exist.
- (void)fillField:(const std::string&)fieldIdentifier
    fieldRendererID:(FieldRendererId)fieldRendererID
     formRendererID:(FormRendererId)formRendererID
           formName:(const std::string&)formName
              value:(const std::u16string)value
            inFrame:(web::WebFrame*)frame {
  base::Value::Dict data;
  data.Set("renderer_id", static_cast<int>(fieldRendererID.value()));
  data.Set("identifier", fieldIdentifier);
  data.Set("form", formName);
  data.Set("value", value);

  DCHECK(_suggestionHandledCompletion);

  const auto callback = [](__weak AutofillAgent* agent,
                           SuggestionHandledCompletion completion,
                           FieldRendererId fieldId,
                           std::optional<FormRendererId> formId,
                           base::WeakPtr<web::WebFrame> frame,
                           const std::u16string& value, BOOL success) {
    if (success && frame) {
      [agent onDidFillField:fieldId form:formId frame:frame.get() value:value];
    }
    // Only run the completion if set as it isn't impossible that the provided
    // completion is nil.
    if (completion) {
      completion();
    }
  };

  __weak __typeof(self) weakSelf = self;
  AutofillJavaScriptFeature::GetInstance()->FillActiveFormField(
      frame, std::move(data),
      base::BindOnce(
          callback, weakSelf, std::exchange(_suggestionHandledCompletion, nil),
          fieldRendererID, formRendererID, frame->AsWeakPtr(), value));
}

// Called when did fill a specific field.
- (void)onDidFillField:(FieldRendererId)fieldID
                  form:(std::optional<FormRendererId>)formID
                 frame:(web::WebFrame*)frame
                 value:(const std::u16string&)value {
  [self updateFieldManagerForSpecificField:fieldID
                                   inFrame:frame
                                 withValue:value];
  [self notifyAboutValueChangeOnField:fieldID
                               inForm:formID
                                frame:frame
                            withValue:value];
}

// Called when did fill multiple fields and received results serialized in a
// JSON string.
- (void)onDidFillWithResults:(NSString*)resultsAsJsonStr
                     inFrame:(web::WebFrame*)frame
        fieldToFormLookupMap:(const FieldToFormLookupMap&)fieldToFormLookupMap {
  std::map<uint32_t, std::u16string> fillingResults;
  if (autofill::ExtractFillingResults(resultsAsJsonStr, &fillingResults)) {
    [self updateFieldManagerWithFillingResults:fillingResults inFrame:frame];
    [self notifyAboutFormFillingResults:fillingResults
                                inFrame:frame
                   fieldToFormLookupMap:fieldToFormLookupMap];
  }

  if (base::FeatureList::IsEnabled(kAutofillRefillForFormsIos) &&
      base::FeatureList::IsEnabled(
          autofill::features::kAutofillAcrossIframesIos)) {
    auto* driver =
        autofill::AutofillDriverIOS::FromWebStateAndWebFrame(_webState, frame);
    if (driver && driver->is_processed()) {
      driver->ScanForms();
    }
  }

  [self recordFormFillingSuccessMetrics:!fillingResults.empty()];
}

// Called when did clear fields.
- (void)onDidClearFields:(NSString*)clearedFieldsAsJsonStr
                 inFrame:(web::WebFrame*)frame
                  inForm:(FormRendererId)formID {
  const auto clearedIDs =
      autofill::ExtractIDs<FieldRendererId>(clearedFieldsAsJsonStr);
  if (!clearedIDs) {
    return;
  }

  [self updateFieldManagerForClearedIDs:*clearedIDs inFrame:frame];
  [self notifyAboutClearedFields:*clearedIDs inFrame:frame inForm:formID];
}

// Updates field managers with filling results.
- (void)updateFieldManagerWithFillingResults:
            (const std::map<uint32_t, std::u16string>&)fillingResults
                                     inFrame:(web::WebFrame*)frame {
  for (auto& fillData : fillingResults) {
    [self updateFieldManagerForSpecificField:FieldRendererId(fillData.first)
                                     inFrame:frame
                                   withValue:fillData.second];
  }
}

- (void)updateFieldManagerForSpecificField:(FieldRendererId)fieldRendererID
                                   inFrame:(web::WebFrame*)frame
                                 withValue:(const std::u16string&)value {
  FieldDataManagerFactoryIOS::FromWebFrame(frame)->UpdateFieldDataMap(
      fieldRendererID, value, kAutofilledOnUserTrigger);
}

// Updates field managers for cleared fields.
- (void)updateFieldManagerForClearedIDs:
            (const std::set<FieldRendererId>&)clearedFields
                                inFrame:(web::WebFrame*)frame {
  for (const auto fieldID : clearedFields) {
    [self updateFieldManagerForSpecificField:fieldID
                                     inFrame:frame
                                   withValue:u""];
  }
}

// Notifies the PasswordAutofillAgent that the value of a field has changed.
- (void)notifyAboutValueChangeOnField:(FieldRendererId)fieldID
                               inForm:(std::optional<FormRendererId>)formID
                                frame:(web::WebFrame*)frame
                            withValue:(const std::u16string&)value {
  CHECK(frame);

  autofill::PasswordAutofillAgent* agent =
      autofill::PasswordAutofillAgent::FromWebState(_webState);
  agent->DidFillField(frame, formID, fieldID, value);
}

// Notifies that form filling results were received.
- (void)notifyAboutFormFillingResults:
            (const std::map<uint32_t, std::u16string>&)fillingResults
                              inFrame:(web::WebFrame*)frame
                 fieldToFormLookupMap:
                     (const FieldToFormLookupMap&)fieldToFormLookupMap {
  CHECK(frame);

  for (auto& fillData : fillingResults) {
    FieldRendererId fieldID = FieldRendererId(fillData.first);
    if (const FormRendererId* formID =
            base::FindOrNull(fieldToFormLookupMap, fieldID)) {
      [self notifyAboutValueChangeOnField:fieldID
                                   inForm:*formID
                                    frame:frame
                                withValue:fillData.second];
    }
  }
}

// Notifies that fields were cleared.
- (void)notifyAboutClearedFields:(const std::set<FieldRendererId>&)clearedFields
                         inFrame:(web::WebFrame*)frame
                          inForm:(FormRendererId)formID {
  CHECK(frame);

  for (auto fieldID : clearedFields) {
    [self notifyAboutValueChangeOnField:fieldID
                                 inForm:formID
                                  frame:frame
                              withValue:u""];
  }
}

// Sends the the |data| to |frame| to actually fill the data.
- (void)sendData:(AutofillData)data toFrame:(web::WebFrame*)frame {
  DCHECK(_webState->IsVisible());

  // `frame` may come from a frame ID that was previously cached; the frame
  // could have been destroyed since then. See crbug.com/425991572.
  if (!frame) {
    return;
  }

  __weak __typeof(self) weakSelf = self;
  const auto callback =
      [](__weak AutofillAgent* agent, base::WeakPtr<web::WebFrame> frame,
         SuggestionHandledCompletion completion,
         const FieldToFormLookupMap& map, NSString* jsonString) {
        if (frame) {
          [agent onDidFillWithResults:jsonString
                              inFrame:frame.get()
                 fieldToFormLookupMap:map];
        }

        // Only run the completion if set as it isn't impossible that the
        // provided completion is nil.
        if (completion) {
          completion();
        }
      };
  AutofillJavaScriptFeature::GetInstance()->FillForm(
      frame, std::move(data.payload), _pendingAutocompleteFieldID,
      base::BindOnce(callback, weakSelf, frame->AsWeakPtr(),
                     std::exchange(_suggestionHandledCompletion, nil),
                     std::move(data.fieldToFormLookupMap)));
}

// Helper method used to implement the aynchronous completion block of
// -webState:didRegisterFormActivity:inFrame:. Due to the asynchronous
// invocation, WebState* and WebFrame* may both have been destroyed, so
// the method needs to check for those edge cases.
- (void)onFormsFetched:(const FormDataVector&)forms
              webFrame:(base::WeakPtr<web::WebFrame>)webFrame
       fieldIdentifier:(FieldRendererId)fieldIdentifier {
  if (forms.size() != 1 || !_webState || !webFrame) {
    return;
  }

  auto* driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
      _webState, webFrame.get());
  if (!driver) {
    return;
  }
  const FormData& form = forms[0];
  if (!ContainsFocusableField(form, fieldIdentifier)) {
    return;
  }
  driver->TextFieldValueChanged(form, {form.host_frame(), fieldIdentifier},
                                base::TimeTicks::Now());
}

// Helper method to create icons for payment cards.
- (UIImage*)createIcon:(autofill::Suggestion)popup_suggestion {
  // If available, the custom icon for the card is preferred over the
  // generic network icon. The network icon may also be missing, in
  // which case we do not set an icon at all.
  if (auto* custom_icon =
          std::get_if<gfx::Image>(&popup_suggestion.custom_icon);
      custom_icon && !custom_icon->IsEmpty()) {
    UIImage* icon = custom_icon->ToUIImage();

    // On iOS, the keyboard accessory wants smaller icons than the default
    // 40x24 size, so we resize them to 32x20, if the provided icon is
    // larger than that.
    if (icon && (icon.size.width > kSuggestionIconWidth)) {
      // For a simple image resize, we can keep the same underlying image
      // and only adjust the ratio.
      CGFloat ratio = icon.size.width / kSuggestionIconWidth;
      return [UIImage imageWithCGImage:[icon CGImage]
                                 scale:icon.scale * ratio
                           orientation:icon.imageOrientation];
    }
    return icon;
  } else if (popup_suggestion.icon != autofill::Suggestion::Icon::kNoIcon) {
    const int resourceID =
        autofill::CreditCard::IconResourceId(popup_suggestion.icon);
    return ui::ResourceBundle::GetSharedInstance()
        .GetNativeImageNamed(resourceID)
        .ToUIImage();
  }
  return nil;
}

// Returns the autofill manager associated with a web::WebState instance.
// Returns nullptr if there is no autofill manager associated anymore, this can
// happen when |close| has been called on the |webState|. Also returns nullptr
// if -webStateDestroyed: has been called.
- (autofill::BrowserAutofillManager*)
    autofillManagerFromWebState:(web::WebState*)webState
                       webFrame:(web::WebFrame*)webFrame {
  if (!webState || !_webStateObserverBridge) {
    return nullptr;
  }
  auto* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(webState, webFrame);
  DLOG_IF(WARNING, !driver) << "No AutofillDriverIOS found for WebFrame";
  if (!driver) {
    return nullptr;
  }
  return &driver->GetAutofillManager();
}

// Notifies the autofill manager when forms are detected on a page.
- (void)notifyFormsSeen:(const FormDataVector&)updatedForms
                inFrame:(web::WebFrame*)frame {
  auto* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(_webState, frame);
  if (!driver) {
    return;
  }

  DCHECK(!updatedForms.empty());

  driver->FormsSeen(/*updated_forms=*/updatedForms, /*removed_forms=*/{});
}

// Invokes the form extraction script in |frame| and loads the output into the
// format expected by the BrowserAutofillManager.
// If |filtered| is NO, all forms are extracted.
// If |filtered| is YES,
//   - if |formName| is non-empty, only a form of that name is extracted.
//   - if |formName| is empty, unowned fields are extracted.
// Only forms with at least |requiredFieldsCount| fields are extracted.
// Calls |completionHandler| with a success BOOL of YES and the form data that
// was extracted.
// Calls |completionHandler| with NO if the forms could not be extracted.
// |completionHandler| cannot be nil.
- (void)fetchFormsFiltered:(BOOL)filtered
                  withName:(const std::u16string&)formName
                   inFrame:(web::WebFrame*)frame
         completionHandler:(FormFetchCompletion)completionHandler {
  DCHECK(completionHandler);

  // Necessary so the values can be used inside a block.
  GURL pageURL = _webState->GetLastCommittedURL();
  url::Origin frameOrigin =
      frame ? frame->GetSecurityOrigin() : url::Origin::Create(pageURL);

  if (auto* driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
          _webState, frame)) {
    driver->OnDidTriggerFormFetch();
  }

  const scoped_refptr<FieldDataManager> fieldDataManager =
      FieldDataManagerFactoryIOS::GetRetainable(frame);
  const auto callback = [](FormFetchCompletion completion, BOOL filtered,
                           const std::u16string& formName, const GURL& pageURL,
                           const url::Origin& frameOrigin,
                           scoped_refptr<FieldDataManager> fieldDataManager,
                           const std::string& frame_id, NSString* formJSON) {
    std::optional<std::vector<FormData>> formData =
        autofill::ExtractFormsData(formJSON, filtered, formName, pageURL,
                                   frameOrigin, *fieldDataManager, frame_id);
    std::move(completion).Run(std::move(formData));
  };
  AutofillJavaScriptFeature::GetInstance()->FetchForms(
      frame, base::BindOnce(callback, std::move(completionHandler), filtered,
                            formName, pageURL, frameOrigin, fieldDataManager,
                            frame->GetFrameId()));
}

- (void)onSuggestionsReady:(NSArray<FormSuggestion*>*)suggestions
        suggestionDelegate:
            (const base::WeakPtr<autofill::AutofillSuggestionDelegate>&)
                delegate {
  _suggestionDelegate = delegate;
  _mostRecentSuggestions = suggestions;
  if (SuggestionsAvailableCompletion completion =
          std::exchange(_suggestionsAvailableCompletion, nil)) {
    completion([_mostRecentSuggestions count] > 0);
  }
}

// Sends a request to BrowserAutofillManager to retrieve suggestions for the
// specified form and field.
- (void)queryAutofillForForm:(const FormData&)form
             fieldIdentifier:(FieldRendererId)fieldIdentifier
                        type:(NSString*)type
                  typedValue:(NSString*)typedValue
                       frame:(base::WeakPtr<web::WebFrame>)frame
                    webState:(base::WeakPtr<web::WebState>)webState
           completionHandler:(SuggestionsAvailableCompletion)completion {
  if (!frame || !webState) {
    completion(NO);
    return;
  }

  // Save the completion and go look for suggestions.
  _suggestionsAvailableCompletion = [completion copy];
  _typedValue = typedValue;

  // Query the BrowserAutofillManager for suggestions. Results will arrive in
  // -showAutofillPopup:suggestionDelegate:.
  if (!ContainsFocusableField(form, fieldIdentifier)) {
    return;
  }
  _lastQueriedFieldID = {form.host_frame(), fieldIdentifier};
  auto* driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
      _webState, frame.get());
  DLOG_IF(WARNING, !driver) << "No AutofillDriverIOS found for WebFrame";
  if (!driver) {
    return;
  }
  driver->AskForValuesToFill(form, _lastQueriedFieldID);
}

- (void)processPage:(web::WebState*)webState {
  web::WebFramesManager* frames_manager =
      AutofillJavaScriptFeature::GetInstance()->GetWebFramesManager(webState);
  if (!frames_manager->GetMainWebFrame()) {
    return;
  }
  [self processFrame:frames_manager->GetMainWebFrame() inWebState:webState];
  for (auto* frame : frames_manager->GetAllWebFrames()) {
    if (frame->IsMainFrame()) {
      continue;
    }
    [self processFrame:frame inWebState:webState];
  }
}

- (void)processFrame:(web::WebFrame*)frame inWebState:(web::WebState*)webState {
  if (!frame) {
    return;
  }

  autofill::AutofillDriverIOS* driver =
      autofill::AutofillDriverIOS::FromWebStateAndWebFrame(webState, frame);
  DLOG_IF(WARNING, !driver) << "No AutofillDriverIOS found for WebFrame";
  // This process is only done once.
  if (!driver || driver->is_processed()) {
    return;
  }
  driver->set_processed(true);

  // Inject feature flags in frame directly to make sure the flags are set
  // before triggering form extraction. We could use
  // AutofillFormFeaturesInjector but there is no guarantee that it will inject
  // the flags before this code is run.
  autofill::SetAutofillFormFeatureFlags(frame);

  if (frame->IsMainFrame()) {
    _suggestionDelegate.reset();
    _suggestionsAvailableCompletion = nil;
    _suggestionHandledCompletion = nil;
    _mostRecentSuggestions = nil;
    _typedValue = nil;
  }

  FormHandlersJavaScriptFeature* formHandlerFeature =
      FormHandlersJavaScriptFeature::GetInstance();

  // Use a delay of 200ms when tracking form mutations to reduce the
  // communication overhead (as mutations are likely to come in batch).
  constexpr int kMutationTrackingEnabledDelayInMs = 200;
  formHandlerFeature->TrackFormMutations(frame,
                                         kMutationTrackingEnabledDelayInMs);

  driver->ScanForms(/*immediately=*/base::FeatureList::IsEnabled(
      kAutofillThrottleDocumentFormScanForceFirstScanIos));
}

// Records if the renderer was able to fill the Autofill-provided values in a
// form or formless fields.
- (void)recordFormFillingSuccessMetrics:(BOOL)success {
  base::UmaHistogramBoolean(/*name=*/"Autofill.FormFillSuccessIOS",
                            /*sample=*/success);

  ukm::SourceId source_id = ukm::GetSourceIdForWebStateDocument(_webState);
  ukm::builders::Autofill_FormFillSuccessIOS(source_id)
      .SetFormFillSuccess(success)
      .Record(ukm::UkmRecorder::Get());
}

@end