File: form_filler.h

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 (278 lines) | stat: -rw-r--r-- 12,477 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FILLING_FORM_FILLER_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FILLING_FORM_FILLER_H_

#include <optional>
#include <string>
#include <variant>

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/autofill/core/browser/autofill_trigger_source.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_model/autofill_ai/entity_instance.h"
#include "components/autofill/core/browser/data_model/payments/credit_card.h"
#include "components/autofill/core/browser/filling/field_filling_skip_reason.h"
#include "components/autofill/core/browser/filling/filling_product.h"
#include "components/autofill/core/browser/filling/form_autofill_history.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/logging/log_manager.h"
#include "components/autofill/core/common/autofill_constants.h"

namespace autofill {

class BrowserAutofillManager;

// Denotes the reason for triggering a refill attempt.
// These values are persisted to UMA logs. Entries should not be renumbered and
// numeric values should never be reused. Keep this enum up to date with the one
// in tools/metrics/histograms/metadata/autofill/enums.xml.
enum class RefillTriggerReason {
  kFormChanged = 0,
  kSelectOptionsChanged = 1,
  kExpirationDateFormatted = 2,
  kMaxValue = kExpirationDateFormatted
};

using VerifiedProfile = std::map<FieldType, std::u16string>;

using FillingPayload = std::variant<const AutofillProfile*,
                                    const CreditCard*,
                                    const EntityInstance*,
                                    const VerifiedProfile*>;

// Helper class responsible for [re]filling forms and fields.
//
// It is privately owned by the BrowserAutofillManager, which is the only
// component that talks to it.
//
// It receives cached data and is responsible for either filling directly or
// triggering a refill (which eventually results in a filling operation), and
// then sending the filled form to the renderer (via AutofillDriver).
//
// Additionally, it provides an API to determine which fields will be
// filled/skipped based on the given context.
//
// The class is directly responsible for modifying the cached fields in the form
// cache (BrowserAutofillManager::form_structures_) since it receives references
// to cached fields and modifies some attributes during filling.
//
// The class is also indirectly responsible for modifying blink forms, since
// after filling FormData objects it sends them to the renderer, which is
// directly responsible for filling the web forms.
//
// It holds any state that is only relevant for [re]filling.
class FormFiller {
 public:
  explicit FormFiller(BrowserAutofillManager& manager);

  FormFiller(const FormFiller&) = delete;
  FormFiller& operator=(const FormFiller&) = delete;

  virtual ~FormFiller();

  // Given `field`, the corresponding `autofill_field` to fill, and the
  // `trigger_field`, return the set of all reasons for that field to be skipped
  // for filling. If the field should not be skipped, an empty set is returned
  // (and not {FieldFillingSkipReason::kNotSkipped}).
  // `type_count` tracks the number of times a type of field has been filled.
  // `type_groups_originally_filled` denotes, in case of a refill, what groups
  // where filled in the initial filling.
  // `blocked_fields` are fields which must not be filled because another
  // filling product of higher priority claims them.
  // `filling_product` is the type of filling calling this function.
  // TODO(crbug.com/40281552): Make `type_groups_originally_filled` also a
  // FieldTypeSet.
  // TODO(crbug.com/40227496): Keep only one of 'field' and 'autofill_field'.
  static DenseSet<FieldFillingSkipReason> GetFillingSkipReasonsForField(
      const FormFieldData& field,
      const AutofillField& autofill_field,
      const AutofillField& trigger_field,
      base::flat_map<FieldType, size_t>& type_count,
      std::optional<DenseSet<FieldTypeGroup>> type_groups_originally_filled,
      const base::flat_set<FieldGlobalId>& blocked_fields,
      FillingProduct filling_product,
      bool is_refill = false);

  // Resets states that FormFiller holds and maintains.
  void Reset();

  base::TimeDelta get_limit_before_refill() { return limit_before_refill_; }

  // Given a `form`, returns a map from each field's id to the skip reason for
  // that field. See additional comments in GetFieldFillingSkipReason.
  // TODO(crbug.com/40227496): Keep only one of 'form' and 'form_structure'.
  // TODO(crbug.com/40281552): Make `type_groups_originally_filled` also a
  // FieldTypeSet.
  base::flat_map<FieldGlobalId, DenseSet<FieldFillingSkipReason>>
  GetFieldFillingSkipReasons(
      base::span<const FormFieldData> fields,
      const FormStructure& form_structure,
      const AutofillField& trigger_field,
      std::optional<DenseSet<FieldTypeGroup>> type_groups_originally_filled,
      FillingProduct filling_product,
      bool is_refill) const;

  // Reverts the last autofill operation on `form` that affected
  // `trigger_field`. `renderer_action` denotes whether this is an actual
  // filling or a preview operation on the renderer side.
  // TODO(crbug.com/40227496): Keep only one of `form` and `form_structure`.
  void UndoAutofill(mojom::ActionPersistence action_persistence,
                    FormData form,
                    FormStructure& form_structure,
                    const FormFieldData& trigger_field,
                    FillingProduct filling_product);

  // Records filling information if possible and routes back to the renderer.
  void FillOrPreviewField(mojom::ActionPersistence action_persistence,
                          mojom::FieldActionType action_type,
                          const FormFieldData& field,
                          AutofillField* autofill_field,
                          const std::u16string& value,
                          FillingProduct filling_product,
                          std::optional<FieldType> field_type_used);

  // Fills or previews the data from `filling_payload` into `form`.
  // TODO(crbug.com/40227071): Clean up the API.
  void FillOrPreviewForm(
      mojom::ActionPersistence action_persistence,
      const FormData& form,
      const FillingPayload& filling_payload,
      FormStructure& form_structure,
      AutofillField& autofill_field,
      AutofillTriggerSource trigger_source,
      std::optional<RefillTriggerReason> refill_trigger_reason = std::nullopt);

  // May or may not trigger a refill operation on `form`. `field` and
  // `old_value` are only needed when `refill_trigger_reason` is
  // `RefillTriggerReason::kExpirationDateFormatted`, and in that case `field`
  // is the one that was reformatted and `old_value` is the value `field` had
  // before the reformatting.
  void MaybeTriggerRefill(
      const FormData& form,
      const FormStructure& form_structure,
      RefillTriggerReason refill_trigger_reason,
      AutofillTriggerSource trigger_source,
      base::optional_ref<const FormFieldData> field = std::nullopt,
      base::optional_ref<const std::u16string> old_value = std::nullopt);

 private:
  friend class FormFillerTestApi;
  friend class TestFormFiller;

  // Keeps track of the filling context for a form, used to make refill
  // attempts.
  struct RefillContext {
    // |filling_payload| contains the data used to perform the initial filling
    // operation.
    RefillContext(const AutofillField& field,
                  const FillingPayload& filling_payload);
    ~RefillContext();

    // Whether a refill attempt was made.
    bool attempted_refill = false;
    // The profile or credit card that was used for the initial fill. This is
    // slightly different from `filling_payload` that is used by the filling
    // function: This contains actual objects because this needs to survive
    // potential storage mutation, and this only contains payloads that support
    // refills.
    std::variant<CreditCard, AutofillProfile> profile_or_credit_card;
    // Possible identifiers of the field that was focused when the form was
    // initially filled. A refill shall be triggered from the same field.
    const FieldGlobalId filled_field_id;
    const FieldSignature filled_field_signature;
    // The security origin from which the field was filled.
    url::Origin filled_origin;
    // The time at which the initial fill occurred.
    // TODO(crbug.com/41490871): Remove in favor of
    // FormStructure::last_filling_timestamp_.
    const base::TimeTicks original_fill_time;
    // The timer used to trigger a refill.
    base::OneShotTimer on_refill_timer;
    // The field type groups that were initially filled.
    DenseSet<FieldTypeGroup> type_groups_originally_filled;
    // If populated, this map determines which values will be filled into a
    // field (it does not matter whether the field already contains a value).
    std::map<FieldGlobalId, std::u16string> forced_fill_values;
    // The form filled in the first attempt for filling. Used to check whether
    // a refill should be attempted upon parsing an updated FormData.
    std::optional<FormData> filled_form;
  };

  void SetRefillContext(FormGlobalId form_id,
                        std::unique_ptr<RefillContext> context);

  RefillContext* GetRefillContext(FormGlobalId form_id);

  // Schedules a call of TriggerRefill. Virtual for testing.
  virtual void ScheduleRefill(const FormData& form,
                              RefillContext& refill_context,
                              AutofillTriggerSource trigger_source,
                              RefillTriggerReason refill_trigger_reason);

  // Attempts to refill `form`.
  void TriggerRefill(const FormData& form,
                     AutofillTriggerSource trigger_source,
                     RefillTriggerReason refill_trigger_reason);

  // Stores the value to be filled into a field, along with its field type and
  // if it's an override.
  struct FieldFillingData {
    std::u16string value_to_fill;
    std::optional<FieldType> field_type;
    bool value_is_an_override;
  };

  // Returns the value to fill along with the field type and if the value is an
  // override.
  FieldFillingData GetFieldFillingData(
      const AutofillField& autofill_field,
      const FillingPayload& filling_payload,
      const std::map<FieldGlobalId, std::u16string>& forced_fill_values,
      const FormFieldData& field_data,
      mojom::ActionPersistence action_persistence,
      std::string* failure_to_fill);

  // Fills `field_data` and modifies `autofill_field` given all other states.
  // Returns true if the field has been filled, false otherwise. This is
  // independent of whether the field was filled or autofilled before.
  // When `allow_suggestion_swapping` is true, the method still returns true if
  // the `autofill_field` is emptied.
  // TODO(crbug.com/40227071): Cleanup API and logic.
  bool FillField(
      AutofillField& autofill_field,
      const FillingPayload& filling_payload,
      const std::map<FieldGlobalId, std::u16string>& forced_fill_values,
      FormFieldData& field_data,
      mojom::ActionPersistence action_persistence,
      bool allow_suggestion_swapping,
      std::string* failure_to_fill);

  LogManager* log_manager();

  // Container holding the history of Autofill filling operations. Used to undo
  // some of the filling operations.
  FormAutofillHistory form_autofill_history_;

  // A map from FormGlobalId to RefillContext instances used to make refill
  // attempts for dynamic forms.
  std::map<FormGlobalId, std::unique_ptr<RefillContext>> refill_context_;

  // The maximum amount of time between a change in the form and the original
  // fill that triggers a refill. This value is only changed in browser tests,
  // where time cannot be mocked, to avoid flakiness.
  base::TimeDelta limit_before_refill_ = kLimitBeforeRefill;

  const raw_ref<BrowserAutofillManager> manager_;

  base::WeakPtrFactory<FormFiller> weak_ptr_factory_{this};
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FILLING_FORM_FILLER_H_