Don't preload rarely seen large images
[chromium-blink-merge.git] / components / autofill / core / common / form_data.cc
blob3691b6a55ffb1fdcbbc0ff42f70591a800437698
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(base::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 base::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(base::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, base::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 base::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(base::PickleIterator* iter, FormData* form_data) {
138 int version;
139 FormData temp_form_data;
140 if (!iter->ReadInt(&version)) {
141 DVLOG(1) << "Bad pickle of FormData, no version present";
142 return false;
145 if (version < 1 || version > kPickleVersion) {
146 DVLOG(1) << "Unknown FormData pickle version " << version;
147 return false;
150 if (!iter->ReadString16(&temp_form_data.name)) {
151 LogDeserializationError(version);
152 return false;
155 if (version == 1) {
156 base::string16 method;
157 if (!iter->ReadString16(&method)) {
158 LogDeserializationError(version);
159 return false;
163 if (!ReadGURL(iter, &temp_form_data.origin) ||
164 !ReadGURL(iter, &temp_form_data.action) ||
165 !iter->ReadBool(&temp_form_data.user_submitted) ||
166 !DeserializeFormFieldDataVector(iter, &temp_form_data.fields)) {
167 LogDeserializationError(version);
168 return false;
171 if (version == 3) {
172 if (!iter->ReadBool(&temp_form_data.is_form_tag)) {
173 LogDeserializationError(version);
174 return false;
176 } else {
177 form_data->is_form_tag = true;
180 *form_data = temp_form_data;
181 return true;
184 bool DeserializeFormDataFromBase64String(const base::StringPiece& input,
185 FormData* form_data) {
186 if (input.empty())
187 return false;
188 std::string pickle_data;
189 Base64Decode(input, &pickle_data);
190 base::Pickle pickle(pickle_data.data(), static_cast<int>(pickle_data.size()));
191 base::PickleIterator iter(pickle);
192 return DeserializeFormData(&iter, form_data);
195 } // namespace autofill