| 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 { | 11 namespace autofill { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Increment this anytime pickle format is modified as well as provide | 15 // Increment this anytime pickle format is modified as well as provide |
| 16 // deserialization routine from previous kPickleVersion format. | 16 // deserialization routine from previous kPickleVersion format. |
| 17 const int kPickleVersion = 3; | 17 const int kPickleVersion = 4; |
| 18 | 18 |
| 19 void AddVectorToPickle(std::vector<base::string16> strings, | 19 void AddVectorToPickle(std::vector<base::string16> strings, |
| 20 base::Pickle* pickle) { | 20 base::Pickle* pickle) { |
| 21 pickle->WriteInt(static_cast<int>(strings.size())); | 21 pickle->WriteInt(static_cast<int>(strings.size())); |
| 22 for (size_t i = 0; i < strings.size(); ++i) { | 22 for (size_t i = 0; i < strings.size(); ++i) { |
| 23 pickle->WriteString16(strings[i]); | 23 pickle->WriteString16(strings[i]); |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 bool ReadStringVector(base::PickleIterator* iter, | 27 bool ReadStringVector(base::PickleIterator* iter, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool DeserializeSection1(base::PickleIterator* iter, | 53 bool DeserializeSection1(base::PickleIterator* iter, |
| 54 FormFieldData* field_data) { | 54 FormFieldData* field_data) { |
| 55 return iter->ReadString16(&field_data->label) && | 55 return iter->ReadString16(&field_data->label) && |
| 56 iter->ReadString16(&field_data->name) && | 56 iter->ReadString16(&field_data->name) && |
| 57 iter->ReadString16(&field_data->value) && | 57 iter->ReadString16(&field_data->value) && |
| 58 iter->ReadString(&field_data->form_control_type) && | 58 iter->ReadString(&field_data->form_control_type) && |
| 59 iter->ReadString(&field_data->autocomplete_attribute) && | 59 iter->ReadString(&field_data->autocomplete_attribute) && |
| 60 iter->ReadUInt64(&field_data->max_length) && | 60 iter->ReadUInt64(&field_data->max_length) && |
| 61 iter->ReadBool(&field_data->is_autofilled) && | 61 iter->ReadBool(&field_data->is_autofilled); |
| 62 iter->ReadBool(&field_data->is_checked) && | 62 } |
| 63 iter->ReadBool(&field_data->is_checkable) && | 63 |
| 64 iter->ReadBool(&field_data->is_focusable) && | 64 bool DeserializeSection5(base::PickleIterator* iter, |
| 65 FormFieldData* field_data) { |
| 66 bool is_checked = false; |
| 67 bool is_checkable = false; |
| 68 const bool success = |
| 69 iter->ReadBool(&is_checked) && iter->ReadBool(&is_checkable); |
| 70 |
| 71 if (success) |
| 72 SetCheckStatus(field_data, is_checkable, is_checked); |
| 73 |
| 74 return success; |
| 75 } |
| 76 |
| 77 bool DeserializeSection6(base::PickleIterator* iter, |
| 78 FormFieldData* field_data) { |
| 79 return ReadAsInt(iter, &field_data->check_status); |
| 80 ; |
| 81 } |
| 82 |
| 83 bool DeserializeSection7(base::PickleIterator* iter, |
| 84 FormFieldData* field_data) { |
| 85 return iter->ReadBool(&field_data->is_focusable) && |
| 65 iter->ReadBool(&field_data->should_autocomplete); | 86 iter->ReadBool(&field_data->should_autocomplete); |
| 66 } | 87 } |
| 67 | 88 |
| 68 bool DeserializeSection3(base::PickleIterator* iter, | 89 bool DeserializeSection3(base::PickleIterator* iter, |
| 69 FormFieldData* field_data) { | 90 FormFieldData* field_data) { |
| 70 return ReadAsInt(iter, &field_data->text_direction) && | 91 return ReadAsInt(iter, &field_data->text_direction) && |
| 71 ReadStringVector(iter, &field_data->option_values) && | 92 ReadStringVector(iter, &field_data->option_values) && |
| 72 ReadStringVector(iter, &field_data->option_contents); | 93 ReadStringVector(iter, &field_data->option_contents); |
| 73 } | 94 } |
| 74 | 95 |
| 75 bool DeserializeSection2(base::PickleIterator* iter, | 96 bool DeserializeSection2(base::PickleIterator* iter, |
| 76 FormFieldData* field_data) { | 97 FormFieldData* field_data) { |
| 77 return ReadAsInt(iter, &field_data->role); | 98 return ReadAsInt(iter, &field_data->role); |
| 78 } | 99 } |
| 79 | 100 |
| 80 bool DeserializeSection4(base::PickleIterator* iter, | 101 bool DeserializeSection4(base::PickleIterator* iter, |
| 81 FormFieldData* field_data) { | 102 FormFieldData* field_data) { |
| 82 return iter->ReadString16(&field_data->placeholder); | 103 return iter->ReadString16(&field_data->placeholder); |
| 83 } | 104 } |
| 84 | 105 |
| 85 } // namespace | 106 } // namespace |
| 86 | 107 |
| 87 FormFieldData::FormFieldData() | 108 FormFieldData::FormFieldData() |
| 88 : max_length(0), | 109 : max_length(0), |
| 89 is_autofilled(false), | 110 is_autofilled(false), |
| 90 is_checked(false), | 111 check_status(NOT_CHECKABLE), |
| 91 is_checkable(false), | |
| 92 is_focusable(false), | 112 is_focusable(false), |
| 93 should_autocomplete(true), | 113 should_autocomplete(true), |
| 94 role(ROLE_ATTRIBUTE_OTHER), | 114 role(ROLE_ATTRIBUTE_OTHER), |
| 95 text_direction(base::i18n::UNKNOWN_DIRECTION) { | 115 text_direction(base::i18n::UNKNOWN_DIRECTION) {} |
| 96 } | |
| 97 | 116 |
| 98 FormFieldData::FormFieldData(const FormFieldData& other) = default; | 117 FormFieldData::FormFieldData(const FormFieldData& other) = default; |
| 99 | 118 |
| 100 FormFieldData::~FormFieldData() { | 119 FormFieldData::~FormFieldData() { |
| 101 } | 120 } |
| 102 | 121 |
| 103 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { | 122 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { |
| 104 // A FormFieldData stores a value, but the value is not part of the identity | 123 // A FormFieldData stores a value, but the value is not part of the identity |
| 105 // of the field, so we don't want to compare the values. | 124 // of the field, so we don't want to compare the values. |
| 106 return label == field.label && name == field.name && | 125 return label == field.label && name == field.name && |
| 107 form_control_type == field.form_control_type && | 126 form_control_type == field.form_control_type && |
| 108 autocomplete_attribute == field.autocomplete_attribute && | 127 autocomplete_attribute == field.autocomplete_attribute && |
| 109 placeholder == field.placeholder && max_length == field.max_length && | 128 placeholder == field.placeholder && max_length == field.max_length && |
| 110 // is_checked and is_autofilled counts as "value" since these change | 129 // is_checked and is_autofilled counts as "value" since these change |
| 111 // when we fill things in. | 130 // when we fill things in. |
| 112 is_checkable == field.is_checkable && | 131 IsCheckable(check_status) == IsCheckable(field.check_status) && |
| 113 is_focusable == field.is_focusable && | 132 is_focusable == field.is_focusable && |
| 114 should_autocomplete == field.should_autocomplete && | 133 should_autocomplete == field.should_autocomplete && |
| 115 role == field.role && text_direction == field.text_direction; | 134 role == field.role && text_direction == field.text_direction; |
| 116 // The option values/contents which are the list of items in the list | 135 // The option values/contents which are the list of items in the list |
| 117 // of a drop-down are currently not considered part of the identity of | 136 // of a drop-down are currently not considered part of the identity of |
| 118 // a form element. This is debatable, since one might base heuristics | 137 // a form element. This is debatable, since one might base heuristics |
| 119 // on the types of elements that are available. Alternatively, one | 138 // on the types of elements that are available. Alternatively, one |
| 120 // could imagine some forms that dynamically change the element | 139 // could imagine some forms that dynamically change the element |
| 121 // contents (say, insert years starting from the current year) that | 140 // contents (say, insert years starting from the current year) that |
| 122 // should not be considered changes in the structure of the form. | 141 // should not be considered changes in the structure of the form. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 135 if (name > field.name) return false; | 154 if (name > field.name) return false; |
| 136 if (form_control_type < field.form_control_type) return true; | 155 if (form_control_type < field.form_control_type) return true; |
| 137 if (form_control_type > field.form_control_type) return false; | 156 if (form_control_type > field.form_control_type) return false; |
| 138 if (autocomplete_attribute < field.autocomplete_attribute) return true; | 157 if (autocomplete_attribute < field.autocomplete_attribute) return true; |
| 139 if (autocomplete_attribute > field.autocomplete_attribute) return false; | 158 if (autocomplete_attribute > field.autocomplete_attribute) return false; |
| 140 if (placeholder < field.placeholder) return true; | 159 if (placeholder < field.placeholder) return true; |
| 141 if (placeholder > field.placeholder) return false; | 160 if (placeholder > field.placeholder) return false; |
| 142 if (max_length < field.max_length) return true; | 161 if (max_length < field.max_length) return true; |
| 143 if (max_length > field.max_length) return false; | 162 if (max_length > field.max_length) return false; |
| 144 // Skip |is_checked| and |is_autofilled| as in SameFieldAs. | 163 // Skip |is_checked| and |is_autofilled| as in SameFieldAs. |
| 145 if (is_checkable < field.is_checkable) return true; | 164 if (IsCheckable(check_status) < IsCheckable(field.check_status)) return true; |
| 146 if (is_checkable > field.is_checkable) return false; | 165 if (IsCheckable(check_status) > IsCheckable(field.check_status)) return false; |
| 147 if (is_focusable < field.is_focusable) return true; | 166 if (is_focusable < field.is_focusable) return true; |
| 148 if (is_focusable > field.is_focusable) return false; | 167 if (is_focusable > field.is_focusable) return false; |
| 149 if (should_autocomplete < field.should_autocomplete) return true; | 168 if (should_autocomplete < field.should_autocomplete) return true; |
| 150 if (should_autocomplete > field.should_autocomplete) return false; | 169 if (should_autocomplete > field.should_autocomplete) return false; |
| 151 if (role < field.role) return true; | 170 if (role < field.role) return true; |
| 152 if (role > field.role) return false; | 171 if (role > field.role) return false; |
| 153 if (text_direction < field.text_direction) return true; | 172 if (text_direction < field.text_direction) return true; |
| 154 if (text_direction > field.text_direction) return false; | 173 if (text_direction > field.text_direction) return false; |
| 155 // See operator== above for why we don't check option_values/contents. | 174 // See operator== above for why we don't check option_values/contents. |
| 156 return false; | 175 return false; |
| 157 } | 176 } |
| 158 | 177 |
| 159 void SerializeFormFieldData(const FormFieldData& field_data, | 178 void SerializeFormFieldData(const FormFieldData& field_data, |
| 160 base::Pickle* pickle) { | 179 base::Pickle* pickle) { |
| 161 pickle->WriteInt(kPickleVersion); | 180 pickle->WriteInt(kPickleVersion); |
| 162 pickle->WriteString16(field_data.label); | 181 pickle->WriteString16(field_data.label); |
| 163 pickle->WriteString16(field_data.name); | 182 pickle->WriteString16(field_data.name); |
| 164 pickle->WriteString16(field_data.value); | 183 pickle->WriteString16(field_data.value); |
| 165 pickle->WriteString(field_data.form_control_type); | 184 pickle->WriteString(field_data.form_control_type); |
| 166 pickle->WriteString(field_data.autocomplete_attribute); | 185 pickle->WriteString(field_data.autocomplete_attribute); |
| 167 pickle->WriteUInt64(field_data.max_length); | 186 pickle->WriteUInt64(field_data.max_length); |
| 168 pickle->WriteBool(field_data.is_autofilled); | 187 pickle->WriteBool(field_data.is_autofilled); |
| 169 pickle->WriteBool(field_data.is_checked); | 188 pickle->WriteInt(field_data.check_status); |
| 170 pickle->WriteBool(field_data.is_checkable); | |
| 171 pickle->WriteBool(field_data.is_focusable); | 189 pickle->WriteBool(field_data.is_focusable); |
| 172 pickle->WriteBool(field_data.should_autocomplete); | 190 pickle->WriteBool(field_data.should_autocomplete); |
| 173 pickle->WriteInt(field_data.role); | 191 pickle->WriteInt(field_data.role); |
| 174 pickle->WriteInt(field_data.text_direction); | 192 pickle->WriteInt(field_data.text_direction); |
| 175 AddVectorToPickle(field_data.option_values, pickle); | 193 AddVectorToPickle(field_data.option_values, pickle); |
| 176 AddVectorToPickle(field_data.option_contents, pickle); | 194 AddVectorToPickle(field_data.option_contents, pickle); |
| 177 pickle->WriteString16(field_data.placeholder); | 195 pickle->WriteString16(field_data.placeholder); |
| 178 } | 196 } |
| 179 | 197 |
| 180 bool DeserializeFormFieldData(base::PickleIterator* iter, | 198 bool DeserializeFormFieldData(base::PickleIterator* iter, |
| 181 FormFieldData* field_data) { | 199 FormFieldData* field_data) { |
| 182 int version; | 200 int version; |
| 183 FormFieldData temp_form_field_data; | 201 FormFieldData temp_form_field_data; |
| 184 if (!iter->ReadInt(&version)) { | 202 if (!iter->ReadInt(&version)) { |
| 185 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; | 203 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; |
| 186 return false; | 204 return false; |
| 187 } | 205 } |
| 188 | 206 |
| 189 switch (version) { | 207 switch (version) { |
| 190 case 1: { | 208 case 1: { |
| 191 if (!DeserializeSection1(iter, &temp_form_field_data) || | 209 if (!DeserializeSection1(iter, &temp_form_field_data) || |
| 210 !DeserializeSection5(iter, &temp_form_field_data) || |
| 211 !DeserializeSection7(iter, &temp_form_field_data) || |
| 192 !DeserializeSection3(iter, &temp_form_field_data)) { | 212 !DeserializeSection3(iter, &temp_form_field_data)) { |
| 193 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 213 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 194 return false; | 214 return false; |
| 195 } | 215 } |
| 196 break; | 216 break; |
| 197 } | 217 } |
| 198 case 2: { | 218 case 2: { |
| 199 if (!DeserializeSection1(iter, &temp_form_field_data) || | 219 if (!DeserializeSection1(iter, &temp_form_field_data) || |
| 220 !DeserializeSection5(iter, &temp_form_field_data) || |
| 221 !DeserializeSection7(iter, &temp_form_field_data) || |
| 200 !DeserializeSection2(iter, &temp_form_field_data) || | 222 !DeserializeSection2(iter, &temp_form_field_data) || |
| 201 !DeserializeSection3(iter, &temp_form_field_data)) { | 223 !DeserializeSection3(iter, &temp_form_field_data)) { |
| 202 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 224 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 203 return false; | 225 return false; |
| 204 } | 226 } |
| 205 break; | 227 break; |
| 206 } | 228 } |
| 207 case 3: { | 229 case 3: { |
| 208 if (!DeserializeSection1(iter, &temp_form_field_data) || | 230 if (!DeserializeSection1(iter, &temp_form_field_data) || |
| 231 !DeserializeSection5(iter, &temp_form_field_data) || |
| 232 !DeserializeSection7(iter, &temp_form_field_data) || |
| 209 !DeserializeSection2(iter, &temp_form_field_data) || | 233 !DeserializeSection2(iter, &temp_form_field_data) || |
| 210 !DeserializeSection3(iter, &temp_form_field_data) || | 234 !DeserializeSection3(iter, &temp_form_field_data) || |
| 211 !DeserializeSection4(iter, &temp_form_field_data)) { | 235 !DeserializeSection4(iter, &temp_form_field_data)) { |
| 236 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 237 return false; |
| 238 } |
| 239 break; |
| 240 } |
| 241 case 4: { |
| 242 if (!DeserializeSection1(iter, &temp_form_field_data) || |
| 243 !DeserializeSection6(iter, &temp_form_field_data) || |
| 244 !DeserializeSection7(iter, &temp_form_field_data) || |
| 245 !DeserializeSection2(iter, &temp_form_field_data) || |
| 246 !DeserializeSection3(iter, &temp_form_field_data) || |
| 247 !DeserializeSection4(iter, &temp_form_field_data)) { |
| 212 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 248 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 213 return false; | 249 return false; |
| 214 } | 250 } |
| 215 break; | 251 break; |
| 216 } | 252 } |
| 217 default: { | 253 default: { |
| 218 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; | 254 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; |
| 219 return false; | 255 return false; |
| 220 } | 256 } |
| 221 } | 257 } |
| 222 *field_data = temp_form_field_data; | 258 *field_data = temp_form_field_data; |
| 223 return true; | 259 return true; |
| 224 } | 260 } |
| 225 | 261 |
| 226 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { | 262 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { |
| 263 std::string check_status_str; |
| 264 switch (field.check_status) { |
| 265 case FormFieldData::CheckStatus::NOT_CHECKABLE: |
| 266 check_status_str = "NOT_CHECKABLE"; |
| 267 break; |
| 268 case FormFieldData::CheckStatus::CHECKABLE_BUT_UNCHECKED: |
| 269 check_status_str = "CHECKABLE_BUT_UNCHECKED"; |
| 270 break; |
| 271 case FormFieldData::CheckStatus::CHECKED: |
| 272 check_status_str = "CHECKED"; |
| 273 break; |
| 274 } |
| 275 |
| 276 std::string role_str; |
| 277 switch (field.role) { |
| 278 case FormFieldData::RoleAttribute::ROLE_ATTRIBUTE_PRESENTATION: |
| 279 role_str = "ROLE_ATTRIBUTE_PRESENTATION"; |
| 280 break; |
| 281 case FormFieldData::RoleAttribute::ROLE_ATTRIBUTE_OTHER: |
| 282 role_str = "ROLE_ATTRIBUTE_OTHER"; |
| 283 break; |
| 284 } |
| 285 |
| 227 return os << base::UTF16ToUTF8(field.label) << " " | 286 return os << base::UTF16ToUTF8(field.label) << " " |
| 228 << base::UTF16ToUTF8(field.name) << " " | 287 << base::UTF16ToUTF8(field.name) << " " |
| 229 << base::UTF16ToUTF8(field.value) << " " << field.form_control_type | 288 << base::UTF16ToUTF8(field.value) << " " << field.form_control_type |
| 230 << " " << field.autocomplete_attribute << " " << field.placeholder | 289 << " " << field.autocomplete_attribute << " " << field.placeholder |
| 231 << " " << field.max_length << " " | 290 << " " << field.max_length << " " |
| 232 << (field.is_autofilled ? "true" : "false") << " " | 291 << (field.is_autofilled ? "true" : "false") << " " |
| 233 << (field.is_checked ? "true" : "false") << " " | 292 << check_status_str << (field.is_focusable ? "true" : "false") |
| 234 << (field.is_checkable ? "true" : "false") << " " | 293 << " " << (field.should_autocomplete ? "true" : "false") << " " |
| 235 << (field.is_focusable ? "true" : "false") << " " | 294 << role_str << " " << field.text_direction; |
| 236 << (field.should_autocomplete ? "true" : "false") << " " | 295 } |
| 237 << field.role << " " << field.text_direction; | 296 |
| 297 bool IsCheckable(const FormFieldData::CheckStatus& check_status) { |
| 298 return check_status != FormFieldData::CheckStatus::NOT_CHECKABLE; |
| 299 } |
| 300 |
| 301 bool IsChecked(const FormFieldData::CheckStatus& check_status) { |
| 302 return check_status == FormFieldData::CheckStatus::CHECKED; |
| 303 } |
| 304 |
| 305 void SetCheckStatus(FormFieldData* form_field_data, |
| 306 bool isCheckable, |
| 307 bool isChecked) { |
| 308 if (isChecked) { |
| 309 form_field_data->check_status = FormFieldData::CheckStatus::CHECKED; |
| 310 } else { |
| 311 if (isCheckable) { |
| 312 form_field_data->check_status = |
| 313 FormFieldData::CheckStatus::CHECKABLE_BUT_UNCHECKED; |
| 314 } else { |
| 315 form_field_data->check_status = FormFieldData::CheckStatus::NOT_CHECKABLE; |
| 316 } |
| 317 } |
| 238 } | 318 } |
| 239 | 319 |
| 240 } // namespace autofill | 320 } // namespace autofill |
| OLD | NEW |