File: autofill_crowdsourcing_encoding.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 (173 lines) | stat: -rw-r--r-- 7,490 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 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_CROWDSOURCING_AUTOFILL_CROWDSOURCING_ENCODING_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_CROWDSOURCING_AUTOFILL_CROWDSOURCING_ENCODING_H_

#include <optional>
#include <string>

#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/crowdsourcing/randomized_encoder.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/logging/log_manager.h"
#include "components/autofill/core/browser/proto/api_v1.pb.h"
#include "components/autofill/core/common/signatures.h"

namespace autofill {

// Specifies if the Username First Flow vote has intermediate values.
enum class IsMostRecentSingleUsernameCandidate {
  // Field is not part of Username First Flow.
  kNotPartOfUsernameFirstFlow = 0,
  // Field is candidate for username in Username First Flow and has no
  // intermediate fields
  kMostRecentCandidate = 1,
  // Field is candidate for username in Username First Flow and has intermediate
  // fields between candidate and password form.
  kHasIntermediateValuesInBetween = 2,
};

struct EncodeUploadRequestOptions {
  struct Field {
    Field();
    Field(const Field&&) = delete;
    Field(Field&&);
    Field& operator=(const Field&&) = delete;
    Field& operator=(Field&&);
    ~Field();

    // All date format strings that match the field value.
    base::flat_set<std::u16string> format_strings;

    // Strength of the single username vote signal, if applicable.
    std::optional<AutofillUploadContents::Field::SingleUsernameVoteType>
        single_username_vote_type;

    // If set to `kMostRecentCandidate`, the field is candidate for username
    // in Username First Flow and the field has no intermediate
    // fields (like OTP/Captcha) between the candidate and the password form.
    // If set to `kHasIntermediateValuesInBetween`, the field is candidate for
    // username in Username First Flow, but has intermediate fields between the
    // candidate and the password form.
    // If set to `kNotPartOfUsernameFirstFlow`, the field is not part of
    // Username First Flow.
    IsMostRecentSingleUsernameCandidate
        is_most_recent_single_username_candidate =
            IsMostRecentSingleUsernameCandidate::kNotPartOfUsernameFirstFlow;

    // The type of password generation event, if it happened.
    AutofillUploadContents::Field::PasswordGenerationType generation_type =
        AutofillUploadContents::Field::NO_GENERATION;

    // Whether the generated password was changed by user.
    bool generated_password_changed = false;

    // For username fields, a low-entropy hash of the field's initial value
    // before user-interactions or automatic fillings. This field is used to
    // detect static placeholders. On non-username fields, it is not set.
    std::optional<uint32_t> initial_value_hash;

    // The vote type, if the autofill type is USERNAME or any password vote.
    // Otherwise, the field is ignored. `vote_type` provides context as to what
    // triggered the vote.
    AutofillUploadContents::Field::VoteType vote_type =
        AutofillUploadContents::Field::NO_INFORMATION;
  };

  EncodeUploadRequestOptions();
  EncodeUploadRequestOptions(const EncodeUploadRequestOptions&) = delete;
  EncodeUploadRequestOptions(EncodeUploadRequestOptions&&);
  EncodeUploadRequestOptions& operator=(const EncodeUploadRequestOptions&) =
      delete;
  EncodeUploadRequestOptions& operator=(EncodeUploadRequestOptions&&);
  ~EncodeUploadRequestOptions();

  // The randomized encoder to use to encode form metadata during upload.
  // If this is nullptr, no randomized metadata is sent.
  std::optional<RandomizedEncoder> encoder;

  // The type of the event that was taken as an indication that the form has
  // been successfully submitted.
  mojom::SubmissionIndicatorEvent submission_event =
      mojom::SubmissionIndicatorEvent::NONE;

  // The signatures of forms recently submitted on the same origin within a
  // small period of time.
  FormStructure::FormAssociations form_associations;

  FieldTypeSet available_field_types;

  std::optional<FormSignature> login_form_signature;

  bool observed_submission = false;

  std::map<FieldGlobalId, Field> fields;
};

// Encodes the given FormStructure as a vector of protobufs.
//
// On success, the returned vector is non-empty. The first element encodes the
// entire FormStructure. In some cases, a `login_form_signature` is included
// as part of the upload. This field is empty when sending upload requests for
// non-login forms.
//
// If the FormStructure is a frame-transcending form, there may be additional
// AutofillUploadContents elements in the vector, which encode the renderer
// forms (see below for an explanation). These elements omit the renderer
// form's metadata because retrieving this would require significant plumbing
// from AutofillDriverRouter.
//
// The renderer forms are the forms that constitute a frame-transcending form.
// AutofillDriverRouter receives these forms from the renderer and flattens
// them into a single fresh form. Only the latter form is exposed to the rest
// of the browser process. For server predictions, however, we want to query
// and upload also votes also for the signatures of the renderer forms. For
// example, the frame-transcending form
//   <form id=1>
//     <input autocomplete="cc-name">
//     <iframe>
//       #document
//         <form id=2>
//           <input autocomplete="cc-number">
//         </form>
//     </iframe>
//   </form>
// is flattened into a single form that contains the cc-name and cc-number
// fields. We want to vote for this flattened form as well as for the original
// form signatures of forms 1 and 2.
std::vector<AutofillUploadContents> EncodeUploadRequest(
    const FormStructure& form,
    const EncodeUploadRequestOptions& options);

// Encodes the list of `forms` and their fields that are valid into an
// `AutofillPageQueryRequest` proto. The queried FormSignatures and
// FieldSignatures are also returned in the same order as in `query`. In case
// multiple FormStructures have the same FormSignature, only the first one is
// included in `AutofillPageQueryRequest` and the returned queried form
// signatures.
std::pair<AutofillPageQueryRequest, std::vector<FormSignature>>
EncodeAutofillPageQueryRequest(
    const std::vector<raw_ptr<const FormStructure, VectorExperimental>>& forms);

// Parses `payload` as AutofillQueryResponse proto and calls
// `ProcessServerPredictionsQueryResponse`.
void ParseServerPredictionsQueryResponse(
    std::string_view payload,
    const std::vector<raw_ptr<FormStructure, VectorExperimental>>& forms,
    const std::vector<FormSignature>& queried_form_signatures,
    LogManager* log_manager);

// Parses the field types from the server query response. `forms` must be the
// same as the one passed to `EncodeAutofillPageQueryRequest()` when
// constructing the query.
void ProcessServerPredictionsQueryResponse(
    const AutofillQueryResponse& response,
    const std::vector<raw_ptr<FormStructure, VectorExperimental>>& forms,
    const std::vector<FormSignature>& queried_form_signatures,
    LogManager* log_manager);

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_CROWDSOURCING_AUTOFILL_CROWDSOURCING_ENCODING_H_