summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/content/renderer/autofill_agent.cc
blob: ddd2cf1543ee98e4287091a1344f8f500c41548a (plain)
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
// Copyright 2013 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 "components/autofill/content/renderer/autofill_agent.h"

#include <stddef.h>

#include <tuple>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/containers/cxx20_erase_set.h"
#include "base/debug/alias.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/i18n/case_conversion.h"
#include "base/location.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/autofill/content/renderer/autofill_assistant_agent.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/content/renderer/form_tracker.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "components/autofill/content/renderer/renderer_save_password_progress_logger.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_data_validation.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/autofill_tick_clock.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_predictions.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "components/autofill/core/common/save_password_progress_logger.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/origin_util.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "net/cert/cert_status_flags.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/web/web_ax_enums.h"
#include "third_party/blink/public/web/web_ax_object.h"
#include "third_party/blink/public/web/web_console_message.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element_collection.h"
#include "third_party/blink/public/web/web_form_control_element.h"
#include "third_party/blink/public/web/web_form_element.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_node.h"
#include "third_party/blink/public/web/web_option_element.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/keycodes/keyboard_codes.h"

using blink::WebAutofillClient;
using blink::WebAutofillState;
using blink::WebAXObject;
using blink::WebConsoleMessage;
using blink::WebDocument;
using blink::WebElement;
using blink::WebElementCollection;
using blink::WebFormControlElement;
using blink::WebFormElement;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebKeyboardEvent;
using blink::WebLocalFrame;
using blink::WebNode;
using blink::WebOptionElement;
using blink::WebString;
using blink::WebVector;

namespace autofill {

using form_util::ExtractMask;
using form_util::FindFormAndFieldForFormControlElement;
using form_util::IsOwnedByFrame;
using mojom::SubmissionSource;
using ShowAll = PasswordAutofillAgent::ShowAll;
using GenerationShowing = PasswordAutofillAgent::GenerationShowing;

namespace {

// Time to wait in ms to ensure that only a single select or datalist change
// will be acted upon, instead of multiple in close succession (debounce time).
size_t kWaitTimeForOptionsChangesMs = 50;

// Helper function to return EXTRACT_DATALIST if kAutofillExtractAllDatalist is
// enabled, otherwise EXTRACT_NONE is returned.
ExtractMask GetExtractDatalistMask() {
  return base::FeatureList::IsEnabled(features::kAutofillExtractAllDatalists)
             ? form_util::EXTRACT_DATALIST
             : form_util::EXTRACT_NONE;
}

}  // namespace

// During prerendering, we do not want the renderer to send messages to the
// corresponding driver. Since we use a channel associated interface, we still
// need to set up the mojo connection as before (i.e., we can't defer binding
// the interface). Instead, we enqueue our messages here as post-activation
// tasks. See post-prerendering activation steps here:
// https://wicg.github.io/nav-speculation/prerendering.html#prerendering-bcs-subsection
class AutofillAgent::DeferringAutofillDriver : public mojom::AutofillDriver {
 public:
  explicit DeferringAutofillDriver(AutofillAgent* agent) : agent_(agent) {}
  ~DeferringAutofillDriver() override = default;

 private:
  template <typename F, typename... Args>
  void SendMsg(F fn, Args&&... args) {
    DCHECK(!agent_->IsPrerendering());
    mojom::AutofillDriver& autofill_driver = agent_->GetAutofillDriver();
    DCHECK_NE(&autofill_driver, this);
    (autofill_driver.*fn)(std::forward<Args>(args)...);
  }
  template <typename F, typename... Args>
  void DeferMsg(F fn, Args... args) {
    DCHECK(agent_->IsPrerendering());
    agent_->render_frame()
        ->GetWebFrame()
        ->GetDocument()
        .AddPostPrerenderingActivationStep(base::BindOnce(
            &DeferringAutofillDriver::SendMsg<F, Args...>,
            weak_ptr_factory_.GetWeakPtr(), fn, std::forward<Args>(args)...));
  }
  void SetFormToBeProbablySubmitted(
      const absl::optional<FormData>& form) override {
    DeferMsg(&mojom::AutofillDriver::SetFormToBeProbablySubmitted, form);
  }
  void FormsSeen(const std::vector<FormData>& updated_forms,
                 const std::vector<FormRendererId>& removed_forms) override {
    DeferMsg(&mojom::AutofillDriver::FormsSeen, updated_forms, removed_forms);
  }
  void FormSubmitted(const FormData& form,
                     bool known_success,
                     mojom::SubmissionSource source) override {
    DeferMsg(&mojom::AutofillDriver::FormSubmitted, form, known_success,
             source);
  }
  void TextFieldDidChange(const FormData& form,
                          const FormFieldData& field,
                          const gfx::RectF& bounding_box,
                          base::TimeTicks timestamp) override {
    DeferMsg(&mojom::AutofillDriver::TextFieldDidChange, form, field,
             bounding_box, timestamp);
  }
  void TextFieldDidScroll(const FormData& form,
                          const FormFieldData& field,
                          const gfx::RectF& bounding_box) override {
    DeferMsg(&mojom::AutofillDriver::TextFieldDidScroll, form, field,
             bounding_box);
  }
  void SelectControlDidChange(const FormData& form,
                              const FormFieldData& field,
                              const gfx::RectF& bounding_box) override {
    DeferMsg(&mojom::AutofillDriver::SelectControlDidChange, form, field,
             bounding_box);
  }
  void SelectFieldOptionsDidChange(const FormData& form) override {
    DeferMsg(&mojom::AutofillDriver::SelectFieldOptionsDidChange, form);
  }
  void AskForValuesToFill(int32_t id,
                          const FormData& form,
                          const FormFieldData& field,
                          const gfx::RectF& bounding_box,
                          bool autoselect_first_suggestion) override {
    DeferMsg(&mojom::AutofillDriver::AskForValuesToFill, id, form, field,
             bounding_box, autoselect_first_suggestion);
  }
  void HidePopup() override { DeferMsg(&mojom::AutofillDriver::HidePopup); }
  void FocusNoLongerOnForm(bool had_interacted_form) override {
    DeferMsg(&mojom::AutofillDriver::FocusNoLongerOnForm, had_interacted_form);
  }
  void FocusOnFormField(const FormData& form,
                        const FormFieldData& field,
                        const gfx::RectF& bounding_box) override {
    DeferMsg(&mojom::AutofillDriver::FocusOnFormField, form, field,
             bounding_box);
  }
  void DidFillAutofillFormData(const FormData& form,
                               base::TimeTicks timestamp) override {
    DeferMsg(&mojom::AutofillDriver::DidFillAutofillFormData, form, timestamp);
  }
  void DidPreviewAutofillFormData() override {
    DeferMsg(&mojom::AutofillDriver::DidPreviewAutofillFormData);
  }
  void DidEndTextFieldEditing() override {
    DeferMsg(&mojom::AutofillDriver::DidEndTextFieldEditing);
  }

  AutofillAgent* agent_ = nullptr;
  base::WeakPtrFactory<DeferringAutofillDriver> weak_ptr_factory_{this};
};

AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
    : autofill_on_empty_values(false),
      requires_caret_at_end(false),
      show_full_suggestion_list(false),
      autoselect_first_suggestion(false) {}

AutofillAgent::AutofillAgent(content::RenderFrame* render_frame,
                             PasswordAutofillAgent* password_autofill_agent,
                             PasswordGenerationAgent* password_generation_agent,
                             AutofillAssistantAgent* autofill_assistant_agent,
                             blink::AssociatedInterfaceRegistry* registry)
    : content::RenderFrameObserver(render_frame),
      form_cache_(render_frame->GetWebFrame()),
      password_autofill_agent_(password_autofill_agent),
      password_generation_agent_(password_generation_agent),
      autofill_assistant_agent_(autofill_assistant_agent),
      autofill_query_id_(0),
      query_node_autofill_state_(WebAutofillState::kNotFilled),
      is_popup_possibly_visible_(false),
      is_generation_popup_possibly_visible_(false),
      is_user_gesture_required_(true),
      is_secure_context_required_(false),
      form_tracker_(render_frame),
      field_data_manager_(password_autofill_agent->GetFieldDataManager()) {
  render_frame->GetWebFrame()->SetAutofillClient(this);
  password_autofill_agent->SetAutofillAgent(this);
  AddFormObserver(this);
  registry->AddInterface(base::BindRepeating(
      &AutofillAgent::BindPendingReceiver, base::Unretained(this)));
}

// The destructor is not guaranteed to be called. Destruction happens (only)
// through the OnDestruct() event, which posts a task to delete this object.
// The process may be killed before this deletion can happen.
AutofillAgent::~AutofillAgent() {
  RemoveFormObserver(this);
}

void AutofillAgent::BindPendingReceiver(
    mojo::PendingAssociatedReceiver<mojom::AutofillAgent> pending_receiver) {
  receiver_.Bind(std::move(pending_receiver));
}

void AutofillAgent::DidCommitProvisionalLoad(ui::PageTransition transition) {
  blink::WebFrame* frame = render_frame()->GetWebFrame();
  // TODO(dvadym): check if we need to check if it is main frame navigation
  // http://crbug.com/443155
  if (frame->Parent())
    return;  // Not a top-level navigation.

  // Navigation to a new page or a page refresh.

  element_.Reset();

  form_cache_.Reset();
  ResetLastInteractedElements();
  OnFormNoLongerSubmittable();
  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::DidDispatchDOMContentLoadedEvent() {
  ProcessForms();
}

void AutofillAgent::DidChangeScrollOffset() {
  if (element_.IsNull())
    return;

  if (!focus_requires_scroll_) {
    // Post a task here since scroll offset may change during layout.
    // (https://crbug.com/804886)
    weak_ptr_factory_.InvalidateWeakPtrs();
    render_frame()
        ->GetTaskRunner(blink::TaskType::kInternalUserInteraction)
        ->PostTask(FROM_HERE,
                   base::BindOnce(&AutofillAgent::DidChangeScrollOffsetImpl,
                                  weak_ptr_factory_.GetWeakPtr(), element_));
  } else {
    HidePopup();
  }
}

void AutofillAgent::DidChangeScrollOffsetImpl(
    const WebFormControlElement& element) {
  if (element != element_ || element_.IsNull() || focus_requires_scroll_ ||
      !is_popup_possibly_visible_ || !element_.Focused())
    return;

  FormData form;
  FormFieldData field;
  if (FindFormAndFieldForFormControlElement(
          element_, field_data_manager_.get(),
          static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
                                   GetExtractDatalistMask()),
          &form, &field)) {
    GetAutofillDriver().TextFieldDidScroll(form, field, field.bounds);
  }

  // Ignore subsequent scroll offset changes.
  HidePopup();
}

void AutofillAgent::FocusedElementChanged(const WebElement& element) {
  HidePopup();

  if (element.IsNull()) {
    // Focus moved away from the last interacted form (if any) to somewhere else
    // on the page.
    GetAutofillDriver().FocusNoLongerOnForm(!last_interacted_form_.IsNull());
    return;
  }

  const WebInputElement input = element.DynamicTo<WebInputElement>();

  bool focus_moved_to_new_form = false;
  if (!last_interacted_form_.IsNull() &&
      (input.IsNull() || last_interacted_form_ != input.Form())) {
    // The focused element is not part of the last interacted form (could be
    // in a different form).
    GetAutofillDriver().FocusNoLongerOnForm(/*had_interacted_form=*/true);
    focus_moved_to_new_form = true;
  }

  // Calls HandleFocusChangeComplete() after notifying the focus is no longer on
  // the previous form, then early return. No need to notify the newly focused
  // element because that will be done by HandleFocusChangeComplete() which
  // triggers FormControlElementClicked().
  // Refer to http://crbug.com/1105254
  if ((IsKeyboardAccessoryEnabled() || !focus_requires_scroll_) &&
      !element.IsNull() &&
      element.GetDocument().GetFrame()->HasTransientUserActivation()) {
    focused_node_was_last_clicked_ = true;
    HandleFocusChangeComplete();
  }

  if (focus_moved_to_new_form)
    return;

  if (input.IsNull() || !input.IsEnabled() || input.IsReadOnly() ||
      !input.IsTextField())
    return;

  element_ = input;

  FormData form;
  FormFieldData field;
  if (FindFormAndFieldForFormControlElement(
          element_, field_data_manager_.get(),
          static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
                                   GetExtractDatalistMask()),
          &form, &field)) {
    GetAutofillDriver().FocusOnFormField(form, field, field.bounds);
  }
}

void AutofillAgent::OnDestruct() {
  Shutdown();
  base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
}

void AutofillAgent::AccessibilityModeChanged(const ui::AXMode& mode) {
  is_screen_reader_enabled_ = mode.has_mode(ui::AXMode::kScreenReader);
}

void AutofillAgent::FireHostSubmitEvents(const WebFormElement& form,
                                         bool known_success,
                                         SubmissionSource source) {
  DCHECK(IsOwnedByFrame(form, render_frame()));

  FormData form_data;
  if (!form_util::ExtractFormData(form, *field_data_manager_.get(), &form_data))
    return;

  FireHostSubmitEvents(form_data, known_success, source);
}

void AutofillAgent::FireHostSubmitEvents(const FormData& form_data,
                                         bool known_success,
                                         SubmissionSource source) {
  // We don't want to fire duplicate submission event.
  if (!base::FeatureList::IsEnabled(
          features::kAutofillAllowDuplicateFormSubmissions) &&
      !submitted_forms_.insert(form_data.unique_renderer_id).second) {
    return;
  }

  GetAutofillDriver().FormSubmitted(form_data, known_success, source);
}

void AutofillAgent::Shutdown() {
  receiver_.reset();
  weak_ptr_factory_.InvalidateWeakPtrs();
}

void AutofillAgent::TextFieldDidEndEditing(const WebInputElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  // Sometimes "blur" events are side effects of the password generation
  // handling the page. They should not affect any UI in the browser.
  if (password_generation_agent_ &&
      password_generation_agent_->ShouldIgnoreBlur()) {
    return;
  }
  GetAutofillDriver().DidEndTextFieldEditing();
  password_autofill_agent_->DidEndTextFieldEditing();
  if (password_generation_agent_)
    password_generation_agent_->DidEndTextFieldEditing(element);

  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::SetUserGestureRequired(bool required) {
  form_tracker_.set_user_gesture_required(required);
}

void AutofillAgent::TextFieldDidChange(const WebFormControlElement& element) {
  form_tracker_.TextFieldDidChange(element);
}

void AutofillAgent::OnTextFieldDidChange(const WebInputElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  if (password_generation_agent_ &&
      password_generation_agent_->TextDidChangeInTextField(element)) {
    is_popup_possibly_visible_ = true;
    return;
  }

  if (password_autofill_agent_->TextDidChangeInTextField(element)) {
    is_popup_possibly_visible_ = true;
    element_ = element;
    return;
  }

  ShowSuggestionsOptions options;
  options.requires_caret_at_end = true;
  ShowSuggestions(element, options);

  FormData form;
  FormFieldData field;
  if (FindFormAndFieldForFormControlElement(
          element, field_data_manager_.get(),
          static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
                                   GetExtractDatalistMask()),
          &form, &field)) {
    GetAutofillDriver().TextFieldDidChange(form, field, field.bounds,
                                           AutofillTickClock::NowTicks());
  }
}

void AutofillAgent::TextFieldDidReceiveKeyDown(const WebInputElement& element,
                                               const WebKeyboardEvent& event) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  if (event.windows_key_code == ui::VKEY_DOWN ||
      event.windows_key_code == ui::VKEY_UP) {
    ShowSuggestionsOptions options;
    options.autofill_on_empty_values = true;
    options.requires_caret_at_end = true;
    options.autoselect_first_suggestion =
        ShouldAutoselectFirstSuggestionOnArrowDown();
    ShowSuggestions(element, options);
  }
}

void AutofillAgent::OpenTextDataListChooser(const WebInputElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  ShowSuggestionsOptions options;
  options.autofill_on_empty_values = true;
  ShowSuggestions(element, options);
}

// Notifies the AutofillDriver about changes in the <datalist> options in
// batches.
//
// A batch ends if no event occurred for `kWaitTimeForOptionsChangesMs`
// milliseconds. For a given batch, the AutofillDriver is informed only about
// the last field. That is, if within one batch the options of different
// fields changed, all but one of these events will be lost.
void AutofillAgent::DataListOptionsChanged(const WebInputElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  if (element.GetDocument().IsNull() || !is_popup_possibly_visible_ ||
      !element.Focused()) {
    return;
  }

  if (datalist_option_change_batch_timer_.IsRunning())
    datalist_option_change_batch_timer_.AbandonAndStop();

  datalist_option_change_batch_timer_.Start(
      FROM_HERE, base::Milliseconds(kWaitTimeForOptionsChangesMs),
      base::BindRepeating(&AutofillAgent::BatchDataListOptionChange,
                          weak_ptr_factory_.GetWeakPtr(), element));
}

void AutofillAgent::BatchDataListOptionChange(
    const blink::WebFormControlElement& element) {
  if (element.GetDocument().IsNull())
    return;

  OnProvisionallySaveForm(element.Form(), element,
                          ElementChangeSource::TEXTFIELD_CHANGED);
}

void AutofillAgent::UserGestureObserved() {
  password_autofill_agent_->UserGestureObserved();
}

void AutofillAgent::TriggerRefillIfNeeded(const FormData& form) {
  ReplaceElementIfNowInvalid(form);

  FormFieldData field;
  FormData updated_form;
  if (FindFormAndFieldForFormControlElement(element_, field_data_manager_.get(),
                                            &updated_form, &field) &&
      (!element_.IsAutofilled() || !form.DynamicallySameFormAs(updated_form))) {
    WebLocalFrame* frame = render_frame()->GetWebFrame();
    std::vector<FormData> forms;
    forms.push_back(updated_form);
    // Always communicate to browser process for topmost frame.
    if (!forms.empty() || !frame->Parent()) {
      GetAutofillDriver().FormsSeen(forms, {});
    }
  }
}

// mojom::AutofillAgent:
void AutofillAgent::FillOrPreviewForm(int32_t id,
                                      const FormData& form,
                                      mojom::RendererFormDataAction action) {
  // If |element_| is null or not focused, a Autofill was triggered from another
  // frame. In this case, set |element_| to some form field as if Autofill had
  // been triggered from that field. This is necessary because currently
  // AutofillAgent's relies on |elemet_| in many places.
  if (id == kCrossFrameFill && !form.fields.empty() &&
      (element_.IsNull() || !element_.Focused())) {
    WebDocument document = render_frame()->GetWebFrame()->GetDocument();
    element_ = form_util::FindFormControlElementByUniqueRendererId(
        document, form.fields.front().unique_renderer_id);
  }

  if (element_.IsNull())
    return;

  if (id != autofill_query_id_ && id != kCrossFrameFill &&
      (action == mojom::RendererFormDataAction::kPreview || id != kNoQueryId)) {
    return;
  }

  if (action == mojom::RendererFormDataAction::kPreview) {
    ClearPreviewedForm();

    query_node_autofill_state_ = element_.GetAutofillState();
    previewed_elements_ = form_util::FillOrPreviewForm(form, element_, action);

    GetAutofillDriver().DidPreviewAutofillFormData();
  } else {
    was_last_action_fill_ = true;

    // If this is a re-fill, replace the triggering element if it's invalid.
    if (id == kNoQueryId)
      ReplaceElementIfNowInvalid(form);

    query_node_autofill_state_ = element_.GetAutofillState();
    bool filled_some_fields =
        !form_util::FillOrPreviewForm(form, element_, action).empty();

    if (!element_.Form().IsNull()) {
      UpdateLastInteractedForm(element_.Form());
    } else {
      formless_elements_were_autofilled_ |= filled_some_fields;
    }

    // TODO(crbug.com/1198811): Inform the BrowserAutofillManager about the
    // fields that were actually filled. It's possible that the form has changed
    // since the time filling was triggered.
    GetAutofillDriver().DidFillAutofillFormData(form,
                                                AutofillTickClock::NowTicks());

    TriggerRefillIfNeeded(form);
    SendPotentiallySubmittedFormToBrowser();
  }
}

void AutofillAgent::FieldTypePredictionsAvailable(
    const std::vector<FormDataPredictions>& forms) {
  bool attach_predictions_to_dom =
      base::FeatureList::IsEnabled(features::kAutofillShowTypePredictions);
  for (const auto& form : forms) {
    form_cache_.ShowPredictions(form, attach_predictions_to_dom);
  }
}

void AutofillAgent::ClearSection() {
  if (element_.IsNull())
    return;

  form_cache_.ClearSectionWithElement(element_);
}

void AutofillAgent::ClearPreviewedForm() {
  // TODO(crbug.com/816533): It is very rare, but it looks like the |element_|
  // can be null if a provisional load was committed immediately prior to
  // clearing the previewed form.
  if (element_.IsNull())
    return;

  if (password_autofill_agent_->DidClearAutofillSelection(element_))
    return;

  form_util::ClearPreviewedElements(previewed_elements_, element_,
                                    query_node_autofill_state_);
  previewed_elements_ = {};
}

void AutofillAgent::FillFieldWithValue(FieldRendererId field_id,
                                       const std::u16string& value) {
  if (element_.IsNull() ||
      field_id != FieldRendererId(element_.UniqueRendererFormControlId())) {
    return;
  }

  WebInputElement input_element = element_.DynamicTo<WebInputElement>();
  if (!input_element.IsNull()) {
    DoFillFieldWithValue(value, input_element);
    input_element.SetAutofillState(WebAutofillState::kAutofilled);
  }
}

void AutofillAgent::PreviewFieldWithValue(FieldRendererId field_id,
                                          const std::u16string& value) {
  if (element_.IsNull() ||
      field_id != FieldRendererId(element_.UniqueRendererFormControlId())) {
    return;
  }

  WebInputElement input_element = element_.DynamicTo<WebInputElement>();
  if (!input_element.IsNull())
    DoPreviewFieldWithValue(value, input_element);
}

void AutofillAgent::SetSuggestionAvailability(
    FieldRendererId field_id,
    const mojom::AutofillState state) {
  if (element_.IsNull() ||
      field_id != FieldRendererId(element_.UniqueRendererFormControlId())) {
    return;
  }

  WebInputElement input_element = element_.DynamicTo<WebInputElement>();
  if (!input_element.IsNull()) {
    switch (state) {
      case autofill::mojom::AutofillState::kAutofillAvailable:
        WebAXObject::FromWebNode(input_element)
            .HandleAutofillStateChanged(
                blink::WebAXAutofillState::kAutofillAvailable);
        return;
      case autofill::mojom::AutofillState::kAutocompleteAvailable:
        WebAXObject::FromWebNode(input_element)
            .HandleAutofillStateChanged(
                blink::WebAXAutofillState::kAutocompleteAvailable);
        return;
      case autofill::mojom::AutofillState::kNoSuggestions:
        WebAXObject::FromWebNode(input_element)
            .HandleAutofillStateChanged(
                blink::WebAXAutofillState::kNoSuggestions);
        return;
    }
    NOTREACHED();
  }
}

void AutofillAgent::AcceptDataListSuggestion(
    FieldRendererId field_id,
    const std::u16string& suggested_value) {
  if (element_.IsNull() ||
      field_id != FieldRendererId(element_.UniqueRendererFormControlId())) {
    return;
  }

  WebInputElement input_element = element_.DynamicTo<WebInputElement>();
  if (input_element.IsNull()) {
    // For reasons not understood yet, this is triggered on elements which are
    // not input elements.

    // TODO(crbug.com/1048270) Gather debug data.
    DEBUG_ALIAS_FOR_CSTR(element_name, element_.TagName().Latin1().c_str(), 64);
    base::debug::DumpWithoutCrashing();

    // Keep this return after removing the TODO(crbug.com/1048270) above.
    return;
  }
  std::u16string new_value = suggested_value;
  // If this element takes multiple values then replace the last part with
  // the suggestion.
  if (input_element.IsMultiple() && input_element.IsEmailField()) {
    std::u16string value = input_element.EditingValue().Utf16();
    std::vector<base::StringPiece16> parts = base::SplitStringPiece(
        value, u",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
    if (parts.size() == 0)
      parts.push_back(base::StringPiece16());

    std::u16string last_part(parts.back());
    // We want to keep just the leading whitespace.
    for (size_t i = 0; i < last_part.size(); ++i) {
      if (!base::IsUnicodeWhitespace(last_part[i])) {
        last_part = last_part.substr(0, i);
        break;
      }
    }
    last_part.append(suggested_value);
    parts.back() = last_part;

    new_value = base::JoinString(parts, u",");
  }
  DoFillFieldWithValue(new_value, input_element);
}

void AutofillAgent::FillPasswordSuggestion(const std::u16string& username,
                                           const std::u16string& password) {
  if (element_.IsNull())
    return;

  bool handled =
      password_autofill_agent_->FillSuggestion(element_, username, password);
  DCHECK(handled);
}

void AutofillAgent::PreviewPasswordSuggestion(const std::u16string& username,
                                              const std::u16string& password) {
  if (element_.IsNull())
    return;

  bool handled = password_autofill_agent_->PreviewSuggestion(
      element_, blink::WebString::FromUTF16(username),
      blink::WebString::FromUTF16(password));
  DCHECK(handled);
}

bool AutofillAgent::CollectFormlessElements(FormData* output) const {
  if (render_frame() == nullptr || render_frame()->GetWebFrame() == nullptr)
    return false;

  WebDocument document = render_frame()->GetWebFrame()->GetDocument();

  // Build up the FormData from the unowned elements. This logic mostly
  // mirrors the construction of the synthetic form in form_cache.cc, but
  // happens at submit-time so we can capture the modifications the user
  // has made, and doesn't depend on form_cache's internal state.
  std::vector<WebElement> fieldsets;
  std::vector<WebFormControlElement> control_elements =
      form_util::GetUnownedAutofillableFormFieldElements(document, &fieldsets);

  std::vector<WebElement> iframe_elements =
      form_util::GetUnownedIframeElements(document);

  const ExtractMask extract_mask = static_cast<ExtractMask>(
      form_util::EXTRACT_VALUE | form_util::EXTRACT_OPTIONS);

  return form_util::UnownedFormElementsAndFieldSetsToFormData(
      fieldsets, control_elements, iframe_elements, nullptr, document,
      field_data_manager_.get(), extract_mask, output, nullptr);
}

void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
                                    const ShowSuggestionsOptions& options) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  if (!element.IsEnabled() || element.IsReadOnly())
    return;
  if (!element.SuggestedValue().IsEmpty())
    return;

  const WebInputElement input_element = element.DynamicTo<WebInputElement>();
  if (!input_element.IsNull()) {
    if (!input_element.IsTextField())
      return;
    if (!input_element.SuggestedValue().IsEmpty())
      return;
  } else {
    DCHECK(form_util::IsTextAreaElement(element));
    if (!element.To<WebFormControlElement>().SuggestedValue().IsEmpty())
      return;
  }

  // Don't attempt to autofill with values that are too large or if filling
  // criteria are not met.
  WebString value = element.EditingValue();
  if (value.length() > kMaxDataLength ||
      (!options.autofill_on_empty_values && value.IsEmpty()) ||
      (options.requires_caret_at_end &&
       (element.SelectionStart() != element.SelectionEnd() ||
        element.SelectionEnd() != static_cast<int>(value.length())))) {
    // Any popup currently showing is obsolete.
    HidePopup();
    return;
  }

  element_ = element;
  if (form_util::IsAutofillableInputElement(input_element) &&
      password_autofill_agent_->ShowSuggestions(
          input_element, ShowAll(options.show_full_suggestion_list),
          GenerationShowing(is_generation_popup_possibly_visible_))) {
    is_popup_possibly_visible_ = true;
    return;
  }

  if (is_generation_popup_possibly_visible_)
    return;

  // Password field elements should only have suggestions shown by the password
  // autofill agent.
  // The /*disable presubmit*/ comment below is used to disable a presubmit
  // script that ensures that only IsPasswordFieldForAutofill() is used in this
  // code (it has to appear between the function name and the parentesis to not
  // match a regex). In this specific case we are actually interested in whether
  // the field is currently a password field, not whether it has ever been a
  // password field.
  if (!input_element.IsNull() &&
      input_element.IsPasswordField /*disable presubmit*/ () &&
      !query_password_suggestion_) {
    return;
  }

  QueryAutofillSuggestions(element, options.autoselect_first_suggestion);
}

void AutofillAgent::SetQueryPasswordSuggestion(bool query) {
  query_password_suggestion_ = query;
}

void AutofillAgent::SetSecureContextRequired(bool required) {
  is_secure_context_required_ = required;
}

void AutofillAgent::SetFocusRequiresScroll(bool require) {
  focus_requires_scroll_ = require;
}

void AutofillAgent::GetElementFormAndFieldDataForDevToolsNodeId(
    const int backend_node_id,
    GetElementFormAndFieldDataForDevToolsNodeIdCallback callback) {
  blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
  if (!frame)
    return;

  blink::WebElement target_element =
      frame->GetDocument().GetElementByDevToolsNodeId(backend_node_id);

  FormData form;
  FormFieldData field;

  if (target_element.IsNull() || !target_element.IsFormControlElement()) {
    return std::move(callback).Run(form, field);
  }

  blink::WebFormControlElement target_form_control_element =
      target_element.To<blink::WebFormControlElement>();
  bool success = FindFormAndFieldForFormControlElement(
      target_form_control_element, field_data_manager_.get(), &form, &field);
  if (success) {
    // Remember this element so as to autofill the form without focusing the
    // field for Autofill Assistant.
    element_ = target_form_control_element;
  }
  // Do not expect failure.
  DCHECK(success);

  return std::move(callback).Run(form, field);
}

void AutofillAgent::SetAssistantKeyboardSuppressState(bool suppress) {
  DCHECK(autofill_assistant_agent_);
  if (suppress) {
    autofill_assistant_agent_->DisableKeyboard();
  } else {
    autofill_assistant_agent_->EnableKeyboard();
  }
}

void AutofillAgent::EnableHeavyFormDataScraping() {
  is_heavy_form_data_scraping_enabled_ = true;
}

void AutofillAgent::SetFieldsEligibleForManualFilling(
    const std::vector<FieldRendererId>& fields) {
  form_cache_.SetFieldsEligibleForManualFilling(fields);
}

void AutofillAgent::QueryAutofillSuggestions(
    const WebFormControlElement& element,
    bool autoselect_first_suggestion) {
  blink::WebLocalFrame* frame = element.GetDocument().GetFrame();
  if (!frame)
    return;

  DCHECK(!element.DynamicTo<WebInputElement>().IsNull() ||
         form_util::IsTextAreaElement(element));

  static int query_counter = 0;
  autofill_query_id_ = query_counter++;

  FormData form;
  FormFieldData field;
  if (!FindFormAndFieldForFormControlElement(
          element, field_data_manager_.get(),
          static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
                                   GetExtractDatalistMask()),
          &form, &field)) {
    // If we didn't find the cached form, at least let autocomplete have a shot
    // at providing suggestions.
    WebFormControlElementToFormField(
        form.unique_renderer_id, element, nullptr,
        static_cast<ExtractMask>(form_util::EXTRACT_VALUE |
                                 form_util::EXTRACT_BOUNDS |
                                 GetExtractDatalistMask()),
        &field);
  }

  if (is_secure_context_required_ &&
      !(element.GetDocument().IsSecureContext())) {
    LOG(WARNING) << "Autofill suggestions are disabled because the document "
                    "isn't a secure context.";
    return;
  }

  if (!base::FeatureList::IsEnabled(features::kAutofillExtractAllDatalists)) {
    const WebInputElement input_element = element.DynamicTo<WebInputElement>();
    if (!input_element.IsNull()) {
      // Find the datalist values and send them to the browser process.
      form_util::GetDataListSuggestions(input_element, &field.datalist_values,
                                        &field.datalist_labels);
    }
  }

  is_popup_possibly_visible_ = true;
  GetAutofillDriver().AskForValuesToFill(autofill_query_id_, form, field,
                                         field.bounds,
                                         autoselect_first_suggestion);
}

void AutofillAgent::DoFillFieldWithValue(const std::u16string& value,
                                         WebInputElement& node) {
  DCHECK(IsOwnedByFrame(node, render_frame()));

  form_tracker_.set_ignore_control_changes(true);
  node.SetAutofillValue(blink::WebString::FromUTF16(value));
  password_autofill_agent_->UpdateStateForTextChange(node);
  form_tracker_.set_ignore_control_changes(false);
}

void AutofillAgent::DoPreviewFieldWithValue(const std::u16string& value,
                                            WebInputElement& node) {
  DCHECK(IsOwnedByFrame(node, render_frame()));

  ClearPreviewedForm();
  query_node_autofill_state_ = element_.GetAutofillState();
  node.SetSuggestedValue(blink::WebString::FromUTF16(value));
  node.SetAutofillState(WebAutofillState::kPreviewed);
  form_util::PreviewSuggestion(node.SuggestedValue().Utf16(),
                               node.Value().Utf16(), &node);
  previewed_elements_.push_back(node);
}

void AutofillAgent::TriggerReparse() {
  if (!reparse_timer_.IsRunning()) {
    reparse_timer_.Start(FROM_HERE, base::Milliseconds(100),
                         base::BindRepeating(&AutofillAgent::ProcessForms,
                                             weak_ptr_factory_.GetWeakPtr()));
  }
}

void AutofillAgent::ProcessForms() {
  FormCache::UpdateFormCacheResult cache =
      form_cache_.ExtractNewForms(field_data_manager_.get());

  // Always communicate to browser process for topmost frame.
  if (!cache.updated_forms.empty() || !cache.removed_forms.empty() ||
      !render_frame()->GetWebFrame()->Parent()) {
    GetAutofillDriver().FormsSeen(cache.updated_forms,
                                  std::move(cache.removed_forms).extract());
  }
}

void AutofillAgent::HidePopup() {
  if (!is_popup_possibly_visible_)
    return;
  is_popup_possibly_visible_ = false;
  is_generation_popup_possibly_visible_ = false;

  // The keyboard accessory has a separate, more complex hiding logic.
  if (IsKeyboardAccessoryEnabled())
    return;

  GetAutofillDriver().HidePopup();
}

void AutofillAgent::DidAssociateFormControlsDynamically() {
  // If the control flow is here than the document was at least loaded. The
  // whole page doesn't have to be loaded.
  ProcessForms();
  password_autofill_agent_->OnDynamicFormsSeen();
}

void AutofillAgent::DidCompleteFocusChangeInFrame() {
  WebDocument doc = render_frame()->GetWebFrame()->GetDocument();
  WebElement focused_element;
  if (!doc.IsNull())
    focused_element = doc.FocusedElement();

  if (!focused_element.IsNull() && password_autofill_agent_)
    password_autofill_agent_->FocusedNodeHasChanged(focused_element);

  // PasswordGenerationAgent needs to know about focus changes, even if there is
  // no focused element.
  if (password_generation_agent_ &&
      password_generation_agent_->FocusedNodeHasChanged(focused_element)) {
    is_generation_popup_possibly_visible_ = true;
    is_popup_possibly_visible_ = true;
  }

  if (!IsKeyboardAccessoryEnabled() && focus_requires_scroll_)
    HandleFocusChangeComplete();

  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode(
    const WebNode& node) {
  DCHECK(!node.IsNull());
  focused_node_was_last_clicked_ = node.Focused();

#if defined(ANDROID)
  HandleFocusChangeComplete();
#else
  if (!focus_requires_scroll_) {
    HandleFocusChangeComplete();
  }
#endif
}

void AutofillAgent::SelectControlDidChange(
    const WebFormControlElement& element) {
  form_tracker_.SelectControlDidChange(element);
}

// Notifies the AutofillDriver about changes in the <select> options in batches.
//
// A batch ends if no event occurred for `kWaitTimeForOptionsChangesMs`
// milliseconds. For a given batch, the AutofillDriver is informed only about
// the last FormData. That is, if within one batch the options of different
// forms changed, all but one of these events will be lost.
void AutofillAgent::SelectFieldOptionsChanged(
    const blink::WebFormControlElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  if (!was_last_action_fill_ || element_.IsNull())
    return;

  if (select_option_change_batch_timer_.IsRunning())
    select_option_change_batch_timer_.AbandonAndStop();

  select_option_change_batch_timer_.Start(
      FROM_HERE, base::Milliseconds(kWaitTimeForOptionsChangesMs),
      base::BindRepeating(&AutofillAgent::BatchSelectOptionChange,
                          weak_ptr_factory_.GetWeakPtr(), element));
}

void AutofillAgent::BatchSelectOptionChange(
    const blink::WebFormControlElement& element) {
  if (element.GetDocument().IsNull())
    return;

  // Look for the form and field associated with the select element. If they are
  // found, notify the driver that the form was modified dynamically.
  FormData form;
  FormFieldData field;
  if (FindFormAndFieldForFormControlElement(element, field_data_manager_.get(),
                                            &form, &field) &&
      !field.options.empty()) {
    GetAutofillDriver().SelectFieldOptionsDidChange(form);
  }
}

bool AutofillAgent::ShouldSuppressKeyboard(
    const WebFormControlElement& element) {
  // Note: Consider supporting other autofill types in the future as well.
  return password_autofill_agent_->ShouldSuppressKeyboard() ||
         (autofill_assistant_agent_ &&
          autofill_assistant_agent_->ShouldSuppressKeyboard());
}

void AutofillAgent::FormElementReset(const WebFormElement& form) {
  DCHECK(IsOwnedByFrame(form, render_frame()));

  password_autofill_agent_->InformAboutFormClearing(form);
}

void AutofillAgent::PasswordFieldReset(const WebInputElement& element) {
  DCHECK(IsOwnedByFrame(element, render_frame()));

  password_autofill_agent_->InformAboutFieldClearing(element);
}

bool AutofillAgent::IsPrerendering() const {
  return blink::features::IsPrerender2Enabled() &&
         render_frame()->GetWebFrame()->GetDocument().IsPrerendering();
}

void AutofillAgent::FormControlElementClicked(
    const WebFormControlElement& element) {
  last_clicked_form_control_element_for_testing_ =
      FieldRendererId(element.UniqueRendererFormControlId());
  was_last_action_fill_ = false;

  const WebInputElement input_element = element.DynamicTo<WebInputElement>();
  if (input_element.IsNull() && !form_util::IsTextAreaElement(element))
    return;

#if defined(ANDROID)
  password_autofill_agent_->TryToShowTouchToFill(element);
#endif

  ShowSuggestionsOptions options;
  options.autofill_on_empty_values = true;
  // Even if the user has not edited an input element, it may still contain a
  // value: A default value filled by the website. In that case, we don't want
  // to elide suggestions that don't have a common prefix with the default
  // value.
  options.show_full_suggestion_list =
      element.IsAutofilled() || !element.UserHasEditedTheField();

  ShowSuggestions(element, options);

  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::HandleFocusChangeComplete() {
  WebElement focused_element =
      render_frame()->GetWebFrame()->GetDocument().FocusedElement();
  // When using Talkback on Android, and possibly others, traversing to and
  // focusing a field will not register as a click. Thus, when screen readers
  // are used, treat the focused node as if it was the last clicked. Also check
  // to ensure focus is on a field where text can be entered.
  if ((focused_node_was_last_clicked_ || is_screen_reader_enabled_) &&
      !focused_element.IsNull() && focused_element.IsFormControlElement() &&
      (form_util::IsTextInput(focused_element.DynamicTo<WebInputElement>()) ||
       focused_element.HasHTMLTagName("textarea"))) {
    FormControlElementClicked(focused_element.To<WebFormControlElement>());
  }

  focused_node_was_last_clicked_ = false;

  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::AjaxSucceeded() {
  form_tracker_.AjaxSucceeded();

  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::OnProvisionallySaveForm(
    const WebFormElement& form,
    const WebFormControlElement& element,
    ElementChangeSource source) {
  if (source == ElementChangeSource::WILL_SEND_SUBMIT_EVENT) {
    // Fire the form submission event to avoid missing submission when web site
    // handles the onsubmit event, this also gets the form before Javascript
    // could change it.
    // We don't clear submitted_forms_ because OnFormSubmitted will normally be
    // invoked afterwards and we don't want to fire the same event twice.
    FireHostSubmitEvents(form, /*known_success=*/false,
                         SubmissionSource::FORM_SUBMISSION);
    ResetLastInteractedElements();
  } else if (source == ElementChangeSource::TEXTFIELD_CHANGED ||
             source == ElementChangeSource::SELECT_CHANGED) {
    // Remember the last form the user interacted with.
    if (!element.Form().IsNull()) {
      UpdateLastInteractedForm(element.Form());
    } else {
      // Remove visible elements.
      WebDocument doc = render_frame()->GetWebFrame()->GetDocument();
      if (!doc.IsNull()) {
        base::EraseIf(
            formless_elements_user_edited_,
            [&doc](const FieldRendererId field_id) {
              WebFormControlElement field =
                  form_util::FindFormControlElementByUniqueRendererId(
                      doc, field_id, /*form_to_be_searched =*/FormRendererId());
              return !field.IsNull() && form_util::IsWebElementVisible(field);
            });
      }
      formless_elements_user_edited_.insert(
          FieldRendererId(element.UniqueRendererFormControlId()));
      provisionally_saved_form_ = absl::make_optional<FormData>();
      if (!CollectFormlessElements(&provisionally_saved_form_.value())) {
        provisionally_saved_form_.reset();
      } else {
        last_interacted_form_.Reset();
      }
    }

    if (source == ElementChangeSource::TEXTFIELD_CHANGED) {
      OnTextFieldDidChange(element.To<WebInputElement>());
    } else {
      FormData form_data;
      FormFieldData field;
      if (FindFormAndFieldForFormControlElement(
              element, field_data_manager_.get(),
              static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
                                       GetExtractDatalistMask()),
              &form_data, &field)) {
        GetAutofillDriver().SelectControlDidChange(form_data, field,
                                                   field.bounds);
      }
    }
  }
  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::OnProbablyFormSubmitted() {
  absl::optional<FormData> form_data = GetSubmittedForm();
  if (form_data.has_value()) {
    FireHostSubmitEvents(form_data.value(), /*known_success=*/false,
                         SubmissionSource::PROBABLY_FORM_SUBMITTED);
  }
  ResetLastInteractedElements();
  OnFormNoLongerSubmittable();
  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::OnFormSubmitted(const WebFormElement& form) {
  DCHECK(IsOwnedByFrame(form, render_frame()));

  // Fire the submission event here because WILL_SEND_SUBMIT_EVENT is skipped
  // if javascript calls submit() directly.
  FireHostSubmitEvents(form, /*known_success=*/false,
                       SubmissionSource::FORM_SUBMISSION);
  ResetLastInteractedElements();
  OnFormNoLongerSubmittable();
  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::OnInferredFormSubmission(SubmissionSource source) {
  // Only handle iframe for FRAME_DETACHED or main frame for
  // SAME_DOCUMENT_NAVIGATION.
  if ((source == SubmissionSource::FRAME_DETACHED &&
       !render_frame()->GetWebFrame()->Parent()) ||
      (source == SubmissionSource::SAME_DOCUMENT_NAVIGATION &&
       render_frame()->GetWebFrame()->Parent())) {
    ResetLastInteractedElements();
    OnFormNoLongerSubmittable();
    SendPotentiallySubmittedFormToBrowser();
    return;
  }

  if (source == SubmissionSource::FRAME_DETACHED) {
    // Should not access the frame because it is now detached. Instead, use
    // |provisionally_saved_form_|.
    if (provisionally_saved_form_.has_value())
      FireHostSubmitEvents(provisionally_saved_form_.value(),
                           /*known_success=*/true, source);
  } else {
    absl::optional<FormData> form_data = GetSubmittedForm();
    if (form_data.has_value())
      FireHostSubmitEvents(form_data.value(), /*known_success=*/true, source);
  }
  ResetLastInteractedElements();
  OnFormNoLongerSubmittable();
  SendPotentiallySubmittedFormToBrowser();
}

void AutofillAgent::AddFormObserver(Observer* observer) {
  form_tracker_.AddObserver(observer);
}

void AutofillAgent::RemoveFormObserver(Observer* observer) {
  form_tracker_.RemoveObserver(observer);
}

void AutofillAgent::TrackAutofilledElement(
    const blink::WebFormControlElement& element) {
  form_tracker_.TrackAutofilledElement(element);
}

absl::optional<FormData> AutofillAgent::GetSubmittedForm() const {
  if (!last_interacted_form_.IsNull()) {
    FormData form;
    if (form_util::ExtractFormData(last_interacted_form_,
                                   *field_data_manager_.get(), &form)) {
      return absl::make_optional(form);
    } else if (provisionally_saved_form_.has_value()) {
      return absl::make_optional(provisionally_saved_form_.value());
    }
  } else if (formless_elements_were_autofilled_ ||
             (formless_elements_user_edited_.size() != 0 &&
              !form_util::IsSomeControlElementVisible(
                  render_frame()->GetWebFrame(),
                  formless_elements_user_edited_))) {
    // we check if all the elements the user has interacted with are gone,
    // to decide if submission has occurred, and use the
    // provisionally_saved_form_ saved in OnProvisionallySaveForm() if fail to
    // construct form.
    FormData form;
    if (CollectFormlessElements(&form)) {
      return absl::make_optional(form);
    } else if (provisionally_saved_form_.has_value()) {
      return absl::make_optional(provisionally_saved_form_.value());
    }
  }
  return absl::nullopt;
}

void AutofillAgent::SendPotentiallySubmittedFormToBrowser() {
  GetAutofillDriver().SetFormToBeProbablySubmitted(GetSubmittedForm());
}

void AutofillAgent::ResetLastInteractedElements() {
  last_interacted_form_.Reset();
  last_clicked_form_control_element_for_testing_ = {};
  formless_elements_user_edited_.clear();
  formless_elements_were_autofilled_ = false;
  provisionally_saved_form_.reset();
}

void AutofillAgent::UpdateLastInteractedForm(
    const blink::WebFormElement& form) {
  DCHECK(IsOwnedByFrame(form, render_frame()));

  last_interacted_form_ = form;
  provisionally_saved_form_ = absl::make_optional<FormData>();
  if (!form_util::ExtractFormData(last_interacted_form_,
                                  *field_data_manager_.get(),
                                  &provisionally_saved_form_.value())) {
    provisionally_saved_form_.reset();
  }
}

void AutofillAgent::OnFormNoLongerSubmittable() {
  submitted_forms_.clear();
}

bool AutofillAgent::FindTheUniqueNewVersionOfOldElement(
    const WebVector<WebFormControlElement>& elements,
    bool& potential_match_encountered,
    WebFormControlElement& matching_element,
    const WebFormControlElement& original_element) {
  if (original_element.IsNull())
    return false;

  const auto original_element_section = original_element.AutofillSection();
  for (const WebFormControlElement& current_element : elements) {
    if (current_element.IsFocusable() &&
        original_element.NameForAutofill() ==
            current_element.NameForAutofill()) {
      // If this is the first matching element, or is the first with the right
      // section, this is the best match so far.
      // In other words: bad, then good. => pick good.
      if (!potential_match_encountered ||
          (current_element.AutofillSection() == original_element_section &&
           (matching_element.IsNull() ||
            matching_element.AutofillSection() != original_element_section))) {
        matching_element = current_element;
        potential_match_encountered = true;
      } else if (current_element.AutofillSection() !=
                     original_element_section &&
                 !matching_element.IsNull() &&
                 matching_element.AutofillSection() !=
                     original_element_section) {
        // The so far matching fields are equally bad. Continue the search if
        // none of them have the correct section.
        // In other words: bad, then bad => pick none.
        matching_element.Reset();
      } else if (current_element.AutofillSection() ==
                     original_element_section &&
                 !matching_element.IsNull() &&
                 matching_element.AutofillSection() ==
                     original_element_section) {
        // If two or more fields have the matching name and section, we can't
        // decide. Two equally good fields => fail.
        matching_element.Reset();
        return false;
      }  // For the good, then bad case => keep good. Continue the search.
    }
  }
  return true;
}

// TODO(crbug.com/896689): Update this method to use the unique ids once they
// are implemented.
void AutofillAgent::ReplaceElementIfNowInvalid(const FormData& original_form) {
  // If the document is invalid, bail out.
  if (element_.GetDocument().IsNull())
    return;

  const auto original_element = element_;
  WebFormControlElement matching_element;
  bool potential_match_encountered = false;

  if (original_form.name.empty()) {
    // If the form has no name, check all the forms.
    for (const WebFormElement& form : element_.GetDocument().Forms()) {
      // If finding a unique element is impossible, don't look further.
      if (!FindTheUniqueNewVersionOfOldElement(
              form.GetFormControlElements(), potential_match_encountered,
              matching_element, original_element))
        return;
    }
    // If the element is not found, we should still check for unowned elements.
    if (!matching_element.IsNull()) {
      element_ = matching_element;
      return;
    }
  }

  // If |element_|'s parent form has no elements, |element_| is now invalid
  // and should be updated.
  if (!element_.Form().IsNull() &&
      element_.Form().GetFormControlElements().empty()) {
    return;
  }

  WebFormElement form_element;
  bool form_is_found = false;
  if (!original_form.name.empty()) {
    // Try to find the new version of the form.
    for (const WebFormElement& form : element_.GetDocument().Forms()) {
      if (original_form.name == form.GetName().Utf16() ||
          original_form.name == form.GetAttribute("id").Utf16()) {
        if (!form_is_found)
          form_element = form;
        else  // multiple forms with the matching name.
          return;
      }
    }
  }

  if (form_element.IsNull()) {
    // Could not find the new version of the form, get all the unowned elements.
    std::vector<WebElement> fieldsets;
    WebVector<WebFormControlElement> elements =
        form_util::GetUnownedAutofillableFormFieldElements(
            element_.GetDocument(), &fieldsets);
    // If a unique match was found.
    if (FindTheUniqueNewVersionOfOldElement(
            elements, potential_match_encountered, matching_element,
            original_element) &&
        !matching_element.IsNull()) {
      element_ = matching_element;
    }
    return;
  }
  // This is the case for owned fields that belong to the right named form.
  // Get all the elements of the new version of the form.
  // If a unique match was found.
  if (FindTheUniqueNewVersionOfOldElement(form_element.GetFormControlElements(),
                                          potential_match_encountered,
                                          matching_element, original_element) &&
      !matching_element.IsNull()) {
    element_ = matching_element;
  }
}

mojom::AutofillDriver& AutofillAgent::GetAutofillDriver() {
  if (IsPrerendering()) {
    if (!deferring_autofill_driver_) {
      deferring_autofill_driver_ =
          std::make_unique<DeferringAutofillDriver>(this);
    }
    return *deferring_autofill_driver_;
  }

  // Lazily bind this interface.
  if (!autofill_driver_) {
    render_frame()->GetRemoteAssociatedInterfaces()->GetInterface(
        &autofill_driver_);
  }

  return *autofill_driver_;
}

mojom::PasswordManagerDriver& AutofillAgent::GetPasswordManagerDriver() {
  DCHECK(password_autofill_agent_);
  return password_autofill_agent_->GetPasswordManagerDriver();
}

}  // namespace autofill