OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/core/common/form_field_data.h" | 5 #include "components/autofill/core/common/form_field_data.h" |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 | 10 |
| 11 namespace autofill { |
| 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 // Increment this anytime pickle format is modified as well as provide | 15 // Increment this anytime pickle format is modified as well as provide |
14 // deserialization routine from previous kPickleVersion format. | 16 // deserialization routine from previous kPickleVersion format. |
15 const int kPickleVersion = 2; | 17 const int kPickleVersion = 2; |
16 | 18 |
17 void AddVectorToPickle(std::vector<base::string16> strings, | 19 void AddVectorToPickle(std::vector<base::string16> strings, |
18 Pickle* pickle) { | 20 Pickle* pickle) { |
19 pickle->WriteInt(static_cast<int>(strings.size())); | 21 pickle->WriteInt(static_cast<int>(strings.size())); |
20 for (size_t i = 0; i < strings.size(); ++i) { | 22 for (size_t i = 0; i < strings.size(); ++i) { |
(...skipping 20 matching lines...) Expand all Loading... |
41 template <typename T> | 43 template <typename T> |
42 bool ReadAsInt(PickleIterator* iter, T* target_value) { | 44 bool ReadAsInt(PickleIterator* iter, T* target_value) { |
43 int pickle_data; | 45 int pickle_data; |
44 if (!iter->ReadInt(&pickle_data)) | 46 if (!iter->ReadInt(&pickle_data)) |
45 return false; | 47 return false; |
46 | 48 |
47 *target_value = static_cast<T>(pickle_data); | 49 *target_value = static_cast<T>(pickle_data); |
48 return true; | 50 return true; |
49 } | 51 } |
50 | 52 |
| 53 bool DeserializeCommonSection1(PickleIterator* iter, |
| 54 FormFieldData* field_data) { |
| 55 return iter->ReadString16(&field_data->label) && |
| 56 iter->ReadString16(&field_data->name) && |
| 57 iter->ReadString16(&field_data->value) && |
| 58 iter->ReadString(&field_data->form_control_type) && |
| 59 iter->ReadString(&field_data->autocomplete_attribute) && |
| 60 iter->ReadSizeT(&field_data->max_length) && |
| 61 iter->ReadBool(&field_data->is_autofilled) && |
| 62 iter->ReadBool(&field_data->is_checked) && |
| 63 iter->ReadBool(&field_data->is_checkable) && |
| 64 iter->ReadBool(&field_data->is_focusable) && |
| 65 iter->ReadBool(&field_data->should_autocomplete); |
| 66 } |
| 67 |
| 68 bool DeserializeCommonSection2(PickleIterator* iter, |
| 69 FormFieldData* field_data) { |
| 70 return ReadAsInt(iter, &field_data->text_direction) && |
| 71 ReadStringVector(iter, &field_data->option_values) && |
| 72 ReadStringVector(iter, &field_data->option_contents); |
| 73 } |
| 74 |
| 75 bool DeserializeVersion2Specific(PickleIterator* iter, |
| 76 FormFieldData* field_data) { |
| 77 return ReadAsInt(iter, &field_data->role); |
| 78 } |
| 79 |
51 } // namespace | 80 } // namespace |
52 | 81 |
53 namespace autofill { | |
54 | |
55 FormFieldData::FormFieldData() | 82 FormFieldData::FormFieldData() |
56 : max_length(0), | 83 : max_length(0), |
57 is_autofilled(false), | 84 is_autofilled(false), |
58 is_checked(false), | 85 is_checked(false), |
59 is_checkable(false), | 86 is_checkable(false), |
60 is_focusable(false), | 87 is_focusable(false), |
61 should_autocomplete(true), | 88 should_autocomplete(true), |
62 role(ROLE_ATTRIBUTE_OTHER), | 89 role(ROLE_ATTRIBUTE_OTHER), |
63 text_direction(base::i18n::UNKNOWN_DIRECTION) { | 90 text_direction(base::i18n::UNKNOWN_DIRECTION) { |
64 } | 91 } |
65 | 92 |
66 FormFieldData::~FormFieldData() { | 93 FormFieldData::~FormFieldData() { |
67 } | 94 } |
68 | 95 |
69 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { | 96 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { |
70 // A FormFieldData stores a value, but the value is not part of the identity | 97 // A FormFieldData stores a value, but the value is not part of the identity |
71 // of the field, so we don't want to compare the values. | 98 // of the field, so we don't want to compare the values. |
72 return (label == field.label && name == field.name && | 99 return label == field.label && name == field.name && |
73 form_control_type == field.form_control_type && | 100 form_control_type == field.form_control_type && |
74 autocomplete_attribute == field.autocomplete_attribute && | 101 autocomplete_attribute == field.autocomplete_attribute && |
75 max_length == field.max_length && | 102 max_length == field.max_length && |
76 // is_checked and is_autofilled counts as "value" since these change | 103 // is_checked and is_autofilled counts as "value" since these change |
77 // when we fill things in. | 104 // when we fill things in. |
78 is_checkable == field.is_checkable && | 105 is_checkable == field.is_checkable && |
79 is_focusable == field.is_focusable && | 106 is_focusable == field.is_focusable && |
80 should_autocomplete == field.should_autocomplete && | 107 should_autocomplete == field.should_autocomplete && |
81 role == field.role && text_direction == field.text_direction); | 108 role == field.role && text_direction == field.text_direction; |
82 // The option values/contents whith are the list of items in the list | 109 // The option values/contents which are the list of items in the list |
83 // of a drop-down are currently not considered part of the identity of | 110 // of a drop-down are currently not considered part of the identity of |
84 // a form element. This is debatable, since one might base heuristics | 111 // a form element. This is debatable, since one might base heuristics |
85 // on the types of elements that are available. Alternatively, one | 112 // on the types of elements that are available. Alternatively, one |
86 // could imagine some forms that dynamically change the element | 113 // could imagine some forms that dynamically change the element |
87 // contents (say, insert years starting from the current year) that | 114 // contents (say, insert years starting from the current year) that |
88 // should not be considered changes in the structure of the form. | 115 // should not be considered changes in the structure of the form. |
89 } | 116 } |
90 | 117 |
91 bool FormFieldData::operator<(const FormFieldData& field) const { | 118 bool FormFieldData::operator<(const FormFieldData& field) const { |
92 // Like operator==, this ignores the value. | 119 // Like operator==, this ignores the value. |
93 if (label < field.label) return true; | 120 if (label < field.label) return true; |
94 if (label > field.label) return false; | 121 if (label > field.label) return false; |
95 if (name < field.name) return true; | 122 if (name < field.name) return true; |
96 if (name > field.name) return false; | 123 if (name > field.name) return false; |
97 if (form_control_type < field.form_control_type) return true; | 124 if (form_control_type < field.form_control_type) return true; |
98 if (form_control_type > field.form_control_type) return false; | 125 if (form_control_type > field.form_control_type) return false; |
99 if (autocomplete_attribute < field.autocomplete_attribute) return true; | 126 if (autocomplete_attribute < field.autocomplete_attribute) return true; |
100 if (autocomplete_attribute > field.autocomplete_attribute) return false; | 127 if (autocomplete_attribute > field.autocomplete_attribute) return false; |
101 if (max_length < field.max_length) return true; | 128 if (max_length < field.max_length) return true; |
102 if (max_length > field.max_length) return false; | 129 if (max_length > field.max_length) return false; |
103 // Skip is_checked and is_autofilled as in SameFieldAs. | 130 // Skip |is_checked| and |is_autofilled| as in SameFieldAs. |
104 if (is_checkable < field.is_checkable) return true; | 131 if (is_checkable < field.is_checkable) return true; |
105 if (is_checkable > field.is_checkable) return false; | 132 if (is_checkable > field.is_checkable) return false; |
106 if (is_focusable < field.is_focusable) return true; | 133 if (is_focusable < field.is_focusable) return true; |
107 if (is_focusable > field.is_focusable) return false; | 134 if (is_focusable > field.is_focusable) return false; |
108 if (should_autocomplete < field.should_autocomplete) return true; | 135 if (should_autocomplete < field.should_autocomplete) return true; |
109 if (should_autocomplete > field.should_autocomplete) return false; | 136 if (should_autocomplete > field.should_autocomplete) return false; |
110 if (role < field.role) | 137 if (role < field.role) return true; |
111 return true; | 138 if (role > field.role) return false; |
112 if (role > field.role) | |
113 return false; | |
114 if (text_direction < field.text_direction) return true; | 139 if (text_direction < field.text_direction) return true; |
115 if (text_direction > field.text_direction) return false; | 140 if (text_direction > field.text_direction) return false; |
116 // See operator== above for why we don't check option_values/contents. | 141 // See operator== above for why we don't check option_values/contents. |
117 return false; | 142 return false; |
118 } | 143 } |
119 | 144 |
120 void SerializeFormFieldData(const FormFieldData& field_data, | 145 void SerializeFormFieldData(const FormFieldData& field_data, |
121 Pickle* pickle) { | 146 Pickle* pickle) { |
122 pickle->WriteInt(kPickleVersion); | 147 pickle->WriteInt(kPickleVersion); |
123 pickle->WriteString16(field_data.label); | 148 pickle->WriteString16(field_data.label); |
(...skipping 16 matching lines...) Expand all Loading... |
140 bool DeserializeFormFieldData(PickleIterator* iter, | 165 bool DeserializeFormFieldData(PickleIterator* iter, |
141 FormFieldData* field_data) { | 166 FormFieldData* field_data) { |
142 int version; | 167 int version; |
143 if (!iter->ReadInt(&version)) { | 168 if (!iter->ReadInt(&version)) { |
144 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; | 169 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; |
145 return false; | 170 return false; |
146 } | 171 } |
147 | 172 |
148 switch (version) { | 173 switch (version) { |
149 case 1: { | 174 case 1: { |
150 if (!iter->ReadString16(&field_data->label) || | 175 if (!DeserializeCommonSection1(iter, field_data) || |
151 !iter->ReadString16(&field_data->name) || | 176 !DeserializeCommonSection2(iter, field_data)) { |
152 !iter->ReadString16(&field_data->value) || | |
153 !iter->ReadString(&field_data->form_control_type) || | |
154 !iter->ReadString(&field_data->autocomplete_attribute) || | |
155 !iter->ReadSizeT(&field_data->max_length) || | |
156 !iter->ReadBool(&field_data->is_autofilled) || | |
157 !iter->ReadBool(&field_data->is_checked) || | |
158 !iter->ReadBool(&field_data->is_checkable) || | |
159 !iter->ReadBool(&field_data->is_focusable) || | |
160 !iter->ReadBool(&field_data->should_autocomplete) || | |
161 !ReadAsInt(iter, &field_data->text_direction) || | |
162 !ReadStringVector(iter, &field_data->option_values) || | |
163 !ReadStringVector(iter, &field_data->option_contents)) { | |
164 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 177 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
165 return false; | 178 return false; |
166 } | 179 } |
167 break; | 180 break; |
168 } | 181 } |
169 case 2: { | 182 case 2: { |
170 if (!iter->ReadString16(&field_data->label) || | 183 if (!DeserializeCommonSection1(iter, field_data) || |
171 !iter->ReadString16(&field_data->name) || | 184 !DeserializeVersion2Specific(iter, field_data) || |
172 !iter->ReadString16(&field_data->value) || | 185 !DeserializeCommonSection2(iter, field_data)) { |
173 !iter->ReadString(&field_data->form_control_type) || | |
174 !iter->ReadString(&field_data->autocomplete_attribute) || | |
175 !iter->ReadSizeT(&field_data->max_length) || | |
176 !iter->ReadBool(&field_data->is_autofilled) || | |
177 !iter->ReadBool(&field_data->is_checked) || | |
178 !iter->ReadBool(&field_data->is_checkable) || | |
179 !iter->ReadBool(&field_data->is_focusable) || | |
180 !iter->ReadBool(&field_data->should_autocomplete) || | |
181 !ReadAsInt(iter, &field_data->role) || | |
182 !ReadAsInt(iter, &field_data->text_direction) || | |
183 !ReadStringVector(iter, &field_data->option_values) || | |
184 !ReadStringVector(iter, &field_data->option_contents)) { | |
185 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 186 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
186 return false; | 187 return false; |
187 } | 188 } |
188 break; | 189 break; |
189 } | 190 } |
190 default: { | 191 default: { |
191 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; | 192 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; |
192 return false; | 193 return false; |
193 } | 194 } |
194 } | 195 } |
195 return true; | 196 return true; |
196 } | 197 } |
197 | 198 |
198 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { | 199 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { |
199 return os << base::UTF16ToUTF8(field.label) << " " | 200 return os << base::UTF16ToUTF8(field.label) << " " |
200 << base::UTF16ToUTF8(field.name) << " " | 201 << base::UTF16ToUTF8(field.name) << " " |
201 << base::UTF16ToUTF8(field.value) << " " << field.form_control_type | 202 << base::UTF16ToUTF8(field.value) << " " << field.form_control_type |
202 << " " << field.autocomplete_attribute << " " << field.max_length | 203 << " " << field.autocomplete_attribute << " " << field.max_length |
203 << " " << (field.is_autofilled ? "true" : "false") << " " | 204 << " " << (field.is_autofilled ? "true" : "false") << " " |
204 << (field.is_checked ? "true" : "false") << " " | 205 << (field.is_checked ? "true" : "false") << " " |
205 << (field.is_checkable ? "true" : "false") << " " | 206 << (field.is_checkable ? "true" : "false") << " " |
206 << (field.is_focusable ? "true" : "false") << " " | 207 << (field.is_focusable ? "true" : "false") << " " |
207 << (field.should_autocomplete ? "true" : "false") << " " | 208 << (field.should_autocomplete ? "true" : "false") << " " |
208 << field.role << " " << field.text_direction; | 209 << field.role << " " << field.text_direction; |
209 } | 210 } |
210 | 211 |
211 } // namespace autofill | 212 } // namespace autofill |
OLD | NEW |