Roll src/third_party/skia de7665a:76033be
[chromium-blink-merge.git] / components / autofill / core / common / form_data.cc
blob8842f0f42bb22af3b9869a96e059fe6cf374db10
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/autofill/core/common/form_data.h"
7 #include "base/base64.h"
8 #include "base/pickle.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/common/form_field_data.h"
13 namespace autofill {
15 namespace {
17 const int kPickleVersion = 3;
19 bool ReadGURL(PickleIterator* iter, GURL* url) {
20 std::string spec;
21 if (!iter->ReadString(&spec))
22 return false;
24 *url = GURL(spec);
25 return true;
28 void SerializeFormFieldDataVector(const std::vector<FormFieldData>& fields,
29 Pickle* pickle) {
30 pickle->WriteInt(static_cast<int>(fields.size()));
31 for (size_t i = 0; i < fields.size(); ++i) {
32 SerializeFormFieldData(fields[i], pickle);
36 bool DeserializeFormFieldDataVector(PickleIterator* iter,
37 std::vector<FormFieldData>* fields) {
38 int size;
39 if (!iter->ReadInt(&size))
40 return false;
42 FormFieldData temp;
43 for (int i = 0; i < size; ++i) {
44 if (!DeserializeFormFieldData(iter, &temp))
45 return false;
47 fields->push_back(temp);
49 return true;
52 void LogDeserializationError(int version) {
53 DVLOG(1) << "Could not deserialize version " << version
54 << " FormData from pickle.";
57 } // namespace
59 FormData::FormData()
60 : user_submitted(false),
61 is_form_tag(true) {
64 FormData::FormData(const FormData& data)
65 : name(data.name),
66 origin(data.origin),
67 action(data.action),
68 user_submitted(data.user_submitted),
69 is_form_tag(data.is_form_tag),
70 fields(data.fields) {
73 FormData::~FormData() {
76 bool FormData::SameFormAs(const FormData& form) const {
77 if (name != form.name ||
78 origin != form.origin ||
79 action != form.action ||
80 user_submitted != form.user_submitted ||
81 is_form_tag != form.is_form_tag ||
82 fields.size() != form.fields.size())
83 return false;
84 for (size_t i = 0; i < fields.size(); ++i) {
85 if (!fields[i].SameFieldAs(form.fields[i]))
86 return false;
88 return true;
91 bool FormData::operator<(const FormData& form) const {
92 if (name != form.name)
93 return name < form.name;
94 if (origin != form.origin)
95 return origin < form.origin;
96 if (action != form.action)
97 return action < form.action;
98 if (user_submitted != form.user_submitted)
99 return user_submitted < form.user_submitted;
100 if (is_form_tag != form.is_form_tag)
101 return is_form_tag < form.is_form_tag;
102 return fields < form.fields;
105 std::ostream& operator<<(std::ostream& os, const FormData& form) {
106 os << base::UTF16ToUTF8(form.name) << " "
107 << form.origin << " "
108 << form.action << " "
109 << form.user_submitted << " "
110 << form.is_form_tag << " "
111 << "Fields:";
112 for (size_t i = 0; i < form.fields.size(); ++i) {
113 os << form.fields[i] << ",";
115 return os;
118 void SerializeFormData(const FormData& form_data, Pickle* pickle) {
119 pickle->WriteInt(kPickleVersion);
120 pickle->WriteString16(form_data.name);
121 pickle->WriteString(form_data.origin.spec());
122 pickle->WriteString(form_data.action.spec());
123 pickle->WriteBool(form_data.user_submitted);
124 SerializeFormFieldDataVector(form_data.fields, pickle);
125 pickle->WriteBool(form_data.is_form_tag);
128 void SerializeFormDataToBase64String(const FormData& form_data,
129 std::string* output) {
130 Pickle pickle;
131 SerializeFormData(form_data, &pickle);
132 Base64Encode(
133 base::StringPiece(static_cast<const char*>(pickle.data()), pickle.size()),
134 output);
137 bool DeserializeFormData(PickleIterator* iter, FormData* form_data) {
138 int version;
139 if (!iter->ReadInt(&version)) {
140 DVLOG(1) << "Bad pickle of FormData, no version present";
141 return false;
144 if (version < 1 || version > kPickleVersion) {
145 DVLOG(1) << "Unknown FormData pickle version " << version;
146 return false;
149 if (!iter->ReadString16(&form_data->name)) {
150 LogDeserializationError(version);
151 return false;
154 if (version == 1) {
155 base::string16 method;
156 if (!iter->ReadString16(&method)) {
157 LogDeserializationError(version);
158 return false;
162 if (!ReadGURL(iter, &form_data->origin) ||
163 !ReadGURL(iter, &form_data->action) ||
164 !iter->ReadBool(&form_data->user_submitted) ||
165 !DeserializeFormFieldDataVector(iter, &form_data->fields)) {
166 LogDeserializationError(version);
167 return false;
170 if (version == 3) {
171 if (!iter->ReadBool(&form_data->is_form_tag)) {
172 LogDeserializationError(version);
173 return false;
175 } else {
176 form_data->is_form_tag = true;
179 return true;
182 bool DeserializeFormDataFromBase64String(const base::StringPiece& input,
183 FormData* form_data) {
184 if (input.empty())
185 return false;
186 std::string pickle_data;
187 Base64Decode(input, &pickle_data);
188 Pickle pickle(pickle_data.data(), static_cast<int>(pickle_data.size()));
189 PickleIterator iter(pickle);
190 return DeserializeFormData(&iter, form_data);
193 } // namespace autofill