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"
17 const int kPickleVersion
= 3;
19 bool ReadGURL(base::PickleIterator
* iter
, GURL
* url
) {
21 if (!iter
->ReadString(&spec
))
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
) {
39 if (!iter
->ReadInt(&size
))
43 for (int i
= 0; i
< size
; ++i
) {
44 if (!DeserializeFormFieldData(iter
, &temp
))
47 fields
->push_back(temp
);
52 void LogDeserializationError(int version
) {
53 DVLOG(1) << "Could not deserialize version " << version
54 << " FormData from pickle.";
60 : user_submitted(false),
64 FormData::FormData(const FormData
& data
)
68 user_submitted(data
.user_submitted
),
69 is_form_tag(data
.is_form_tag
),
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())
84 for (size_t i
= 0; i
< fields
.size(); ++i
) {
85 if (!fields
[i
].SameFieldAs(form
.fields
[i
]))
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
<< " "
112 for (size_t i
= 0; i
< form
.fields
.size(); ++i
) {
113 os
<< form
.fields
[i
] << ",";
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
) {
131 SerializeFormData(form_data
, &pickle
);
133 base::StringPiece(static_cast<const char*>(pickle
.data()), pickle
.size()),
137 bool DeserializeFormData(base::PickleIterator
* iter
, FormData
* form_data
) {
139 FormData temp_form_data
;
140 if (!iter
->ReadInt(&version
)) {
141 DVLOG(1) << "Bad pickle of FormData, no version present";
145 if (version
< 1 || version
> kPickleVersion
) {
146 DVLOG(1) << "Unknown FormData pickle version " << version
;
150 if (!iter
->ReadString16(&temp_form_data
.name
)) {
151 LogDeserializationError(version
);
156 base::string16 method
;
157 if (!iter
->ReadString16(&method
)) {
158 LogDeserializationError(version
);
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
);
172 if (!iter
->ReadBool(&temp_form_data
.is_form_tag
)) {
173 LogDeserializationError(version
);
177 form_data
->is_form_tag
= true;
180 *form_data
= temp_form_data
;
184 bool DeserializeFormDataFromBase64String(const base::StringPiece
& input
,
185 FormData
* form_data
) {
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