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/pickle.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/common/form_field_data.h"
16 const int kPickleVersion
= 1;
18 bool ReadGURL(PickleIterator
* iter
, GURL
* url
) {
20 if (!iter
->ReadString(&spec
))
27 void SerializeFormFieldDataVector(const std::vector
<FormFieldData
>& fields
,
29 pickle
->WriteInt(static_cast<int>(fields
.size()));
30 for (size_t i
= 0; i
< fields
.size(); ++i
) {
31 SerializeFormFieldData(fields
[i
], pickle
);
35 bool DeserializeFormFieldDataVector(PickleIterator
* iter
,
36 std::vector
<FormFieldData
>* fields
) {
38 if (!iter
->ReadInt(&size
))
42 for (int i
= 0; i
< size
; ++i
) {
43 if (!DeserializeFormFieldData(iter
, &temp
))
46 fields
->push_back(temp
);
54 : user_submitted(false) {
57 FormData::FormData(const FormData
& data
)
62 user_submitted(data
.user_submitted
),
66 FormData::~FormData() {
69 bool FormData::operator==(const FormData
& form
) const {
70 return name
== form
.name
&&
71 StringToLowerASCII(method
) == StringToLowerASCII(form
.method
) &&
72 origin
== form
.origin
&&
73 action
== form
.action
&&
74 user_submitted
== form
.user_submitted
&&
75 fields
== form
.fields
;
78 bool FormData::operator!=(const FormData
& form
) const {
79 return !operator==(form
);
82 bool FormData::operator<(const FormData
& form
) const {
83 if (name
!= form
.name
)
84 return name
< form
.name
;
85 if (StringToLowerASCII(method
) != StringToLowerASCII(form
.method
))
86 return StringToLowerASCII(method
) < StringToLowerASCII(form
.method
);
87 if (origin
!= form
.origin
)
88 return origin
< form
.origin
;
89 if (action
!= form
.action
)
90 return action
< form
.action
;
91 if (user_submitted
!= form
.user_submitted
)
92 return user_submitted
< form
.user_submitted
;
93 return fields
< form
.fields
;
96 std::ostream
& operator<<(std::ostream
& os
, const FormData
& form
) {
97 os
<< base::UTF16ToUTF8(form
.name
) << " "
98 << base::UTF16ToUTF8(form
.method
) << " "
100 << form
.action
<< " "
101 << form
.user_submitted
<< " "
103 for (size_t i
= 0; i
< form
.fields
.size(); ++i
) {
104 os
<< form
.fields
[i
] << ",";
109 void SerializeFormData(const FormData
& form_data
, Pickle
* pickle
) {
110 pickle
->WriteInt(kPickleVersion
);
111 pickle
->WriteString16(form_data
.name
);
112 pickle
->WriteString16(form_data
.method
);
113 pickle
->WriteString(form_data
.origin
.spec());
114 pickle
->WriteString(form_data
.action
.spec());
115 pickle
->WriteBool(form_data
.user_submitted
);
116 SerializeFormFieldDataVector(form_data
.fields
, pickle
);
119 bool DeserializeFormData(PickleIterator
* iter
, FormData
* form_data
) {
121 if (!iter
->ReadInt(&version
)) {
122 LOG(ERROR
) << "Bad pickle of FormData, no version present";
128 if (!iter
->ReadString16(&form_data
->name
) ||
129 !iter
->ReadString16(&form_data
->method
) ||
130 !ReadGURL(iter
, &form_data
->origin
) ||
131 !ReadGURL(iter
, &form_data
->action
) ||
132 !iter
->ReadBool(&form_data
->user_submitted
) ||
133 !DeserializeFormFieldDataVector(iter
, &form_data
->fields
)) {
134 LOG(ERROR
) << "Could not deserialize FormData from pickle";
140 LOG(ERROR
) << "Unknown FormData pickle version " << version
;
147 } // namespace autofill