File: form_data.cc

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 (260 lines) | stat: -rw-r--r-- 7,849 bytes parent folder | download | duplicates (3)
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
// 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.

#include "components/autofill/core/common/form_data.h"

#include <stddef.h>
#include <string_view>
#include <tuple>

#include "base/base64.h"
#include "base/metrics/histogram_macros.h"
#include "base/pickle.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/logging/log_buffer.h"

namespace autofill {

namespace {

const int kFormDataPickleVersion = 8;

bool ReadGURL(base::PickleIterator* iter, GURL* url) {
  std::string spec;
  if (!iter->ReadString(&spec))
    return false;

  *url = GURL(spec);
  return true;
}

bool ReadOrigin(base::PickleIterator* iter, url::Origin* origin) {
  std::string spec;
  if (!iter->ReadString(&spec))
    return false;

  *origin = url::Origin::Create(GURL(spec));
  return true;
}

void SerializeFormFieldDataVector(const std::vector<FormFieldData>& fields,
                                  base::Pickle* pickle) {
  pickle->WriteInt(static_cast<int>(fields.size()));
  for (const FormFieldData& field : fields) {
    SerializeFormFieldData(field, pickle);
  }
}

bool DeserializeFormFieldDataVector(base::PickleIterator* iter,
                                    std::vector<FormFieldData>* fields) {
  int size;
  if (!iter->ReadInt(&size))
    return false;

  FormFieldData temp;
  for (int i = 0; i < size; ++i) {
    if (!DeserializeFormFieldData(iter, &temp))
      return false;

    fields->push_back(temp);
  }
  return true;
}

void LogDeserializationError(int version) {
  DVLOG(1) << "Could not deserialize version " << version
           << " FormData from pickle.";
}

}  // namespace

FrameTokenWithPredecessor::FrameTokenWithPredecessor() = default;
FrameTokenWithPredecessor::FrameTokenWithPredecessor(
    const FrameTokenWithPredecessor&) = default;
FrameTokenWithPredecessor::FrameTokenWithPredecessor(
    FrameTokenWithPredecessor&&) = default;
FrameTokenWithPredecessor& FrameTokenWithPredecessor::operator=(
    const FrameTokenWithPredecessor&) = default;
FrameTokenWithPredecessor& FrameTokenWithPredecessor::operator=(
    FrameTokenWithPredecessor&&) = default;
FrameTokenWithPredecessor::~FrameTokenWithPredecessor() = default;

FormData::FormData() = default;

FormData::FormData(const FormData&) = default;

FormData& FormData::operator=(const FormData&) = default;

FormData::FormData(FormData&&) = default;

FormData& FormData::operator=(FormData&&) = default;

FormData::~FormData() = default;

// static
bool FormData::DeepEqual(const FormData& a, const FormData& b) {
  // We compare all unique identifiers first, including the field renderer IDs,
  // because we expect most inequalities to be due to them.
  if (a.renderer_id() != b.renderer_id() ||
      a.child_frames() != b.child_frames() ||
      !std::ranges::equal(a.fields(), b.fields(), {},
                          &FormFieldData::renderer_id,
                          &FormFieldData::renderer_id)) {
    return false;
  }

  if (a.name() != b.name() || a.id_attribute() != b.id_attribute() ||
      a.name_attribute() != b.name_attribute() || a.url() != b.url() ||
      a.action() != b.action() ||
      a.likely_contains_captcha() != b.likely_contains_captcha() ||
      !std::ranges::equal(a.fields(), b.fields(), &FormFieldData::DeepEqual)) {
    return false;
  }
  return true;
}

bool FormHasNonEmptyPasswordField(const FormData& form) {
  for (const auto& field : form.fields()) {
    if (field.IsPasswordInputElement()) {
      if (!field.value().empty() || !field.user_input().empty()) {
        return true;
      }
    }
  }
  return false;
}

std::ostream& operator<<(std::ostream& os, const FormData& form) {
  os << base::UTF16ToUTF8(form.name()) << " " << form.url() << " "
     << form.action() << " " << form.main_frame_origin() << " " << "Fields:";
  for (const FormFieldData& field : form.fields()) {
    os << field << ",";
  }
  return os;
}

const FormFieldData* FormData::FindFieldByGlobalId(
    const FieldGlobalId& global_id) const {
  auto fields_it =
      std::ranges::find(fields(), global_id, &FormFieldData::global_id);

  // If the field is found, return a pointer to the field, otherwise return
  // nullptr.
  return fields_it != fields().end() ? &*fields_it : nullptr;
}

void SerializeFormData(const FormData& form_data, base::Pickle* pickle) {
  pickle->WriteInt(kFormDataPickleVersion);
  pickle->WriteString16(form_data.name());
  pickle->WriteString(form_data.url().spec());
  pickle->WriteString(form_data.action().spec());
  SerializeFormFieldDataVector(form_data.fields(), pickle);
  pickle->WriteString(form_data.main_frame_origin().Serialize());
}

bool DeserializeFormData(base::PickleIterator* iter, FormData* form_data) {
  int version;
  FormData temp_form_data;
  if (!iter->ReadInt(&version)) {
    DVLOG(1) << "Bad pickle of FormData, no version present";
    return false;
  }

  if (version < 1 || version > kFormDataPickleVersion) {
    DVLOG(1) << "Unknown FormData pickle version " << version;
    return false;
  }

  {
    std::u16string name;
    if (!iter->ReadString16(&name)) {
      LogDeserializationError(version);
      return false;
    }
    temp_form_data.set_name(std::move(name));
  }

  if (version == 1) {
    std::u16string method;
    if (!iter->ReadString16(&method)) {
      LogDeserializationError(version);
      return false;
    }
  }

  bool unused_user_submitted;
  {
    GURL url;
    GURL action;
    std::vector<FormFieldData> fields;
    if (!ReadGURL(iter, &url) || !ReadGURL(iter, &action) ||
        // user_submitted was removed/no longer serialized in version 4.
        (version < 4 && !iter->ReadBool(&unused_user_submitted)) ||
        !DeserializeFormFieldDataVector(iter, &fields)) {
      LogDeserializationError(version);
      return false;
    }
    temp_form_data.set_url(std::move(url));
    temp_form_data.set_action(std::move(action));
    temp_form_data.set_fields(std::move(fields));
  }

  if (version >= 3 && version <= 7) {
    bool temp_bool = false;
    if (!iter->ReadBool(&temp_bool)) {
      LogDeserializationError(version);
      return false;
    }
  }

  if (version >= 5 && version <= 6) {
    bool is_formless_checkout;
    if (!iter->ReadBool(&is_formless_checkout)) {
      LogDeserializationError(version);
      return false;
    }
  }

  if (version >= 6) {
    url::Origin main_frame_origin;
    if (!ReadOrigin(iter, &main_frame_origin)) {
      LogDeserializationError(version);
      return false;
    }
    temp_form_data.set_main_frame_origin(std::move(main_frame_origin));
  }

  *form_data = temp_form_data;
  return true;
}

LogBuffer& operator<<(LogBuffer& buffer, const FormData& form) {
  buffer << Tag{"div"} << Attrib{"class", "form"};
  buffer << Tag{"table"};
  buffer << Tr{} << "Form name:" << form.name();
  buffer << Tr{} << "Renderer id:"
         << base::NumberToString(form.global_id().renderer_id.value());
  buffer << Tr{} << "Host frame: "
         << base::StrCat({form.global_id().frame_token.ToString(), " (",
                          url::Origin::Create(form.url()).Serialize(), ")"});
  buffer << Tr{} << "URL:" << form.url();
  buffer << Tr{} << "Action:" << form.action();
  buffer << Tr{} << "Is action empty:" << form.is_action_empty();
  for (size_t i = 0; i < form.fields().size(); ++i) {
    buffer << Tag{"tr"};
    buffer << Tag{"td"} << "Field " << i << ": " << CTag{};
    buffer << Tag{"td"};
    buffer << Tag{"table"} << form.fields().at(i) << CTag{"table"};
    buffer << CTag{"td"};
    buffer << CTag{"tr"};
  }
  buffer << CTag{"table"};
  buffer << CTag{"div"};
  return buffer;
}

}  // namespace autofill