| 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 base::string16 pickle_data; | 29 base::string16 pickle_data; |
| 30 for (int i = 0; i < size; i++) { | 30 for (int i = 0; i < size; i++) { |
| 31 if (!iter->ReadString16(&pickle_data)) | 31 if (!iter->ReadString16(&pickle_data)) |
| 32 return false; | 32 return false; |
| 33 | 33 |
| 34 strings->push_back(pickle_data); | 34 strings->push_back(pickle_data); |
| 35 } | 35 } |
| 36 return true; | 36 return true; |
| 37 } | 37 } |
| 38 | 38 |
| 39 bool ReadTextDirection(PickleIterator* iter, | 39 template <typename T> |
| 40 base::i18n::TextDirection* direction) { | 40 bool ReadAsInt(PickleIterator* iter, T* direction) { |
| 41 int pickle_data; | 41 int pickle_data; |
| 42 if (!iter->ReadInt(&pickle_data)) | 42 if (!iter->ReadInt(&pickle_data)) |
| 43 return false; | 43 return false; |
| 44 | 44 |
| 45 *direction = static_cast<base::i18n::TextDirection>(pickle_data); | 45 *direction = static_cast<T>(pickle_data); |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 namespace autofill { | 51 namespace autofill { |
| 52 | 52 |
| 53 FormFieldData::FormFieldData() | 53 FormFieldData::FormFieldData() |
| 54 : max_length(0), | 54 : max_length(0), |
| 55 is_autofilled(false), | 55 is_autofilled(false), |
| 56 is_checked(false), | 56 is_checked(false), |
| 57 is_checkable(false), | 57 is_checkable(false), |
| 58 is_focusable(false), | 58 is_focusable(false), |
| 59 should_autocomplete(true), | 59 should_autocomplete(true), |
| 60 role(ROLE_ATTRIBUTE_OTHER), |
| 60 text_direction(base::i18n::UNKNOWN_DIRECTION) { | 61 text_direction(base::i18n::UNKNOWN_DIRECTION) { |
| 61 } | 62 } |
| 62 | 63 |
| 63 FormFieldData::~FormFieldData() { | 64 FormFieldData::~FormFieldData() { |
| 64 } | 65 } |
| 65 | 66 |
| 66 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { | 67 bool FormFieldData::SameFieldAs(const FormFieldData& field) const { |
| 67 // A FormFieldData stores a value, but the value is not part of the identity | 68 // A FormFieldData stores a value, but the value is not part of the identity |
| 68 // of the field, so we don't want to compare the values. | 69 // of the field, so we don't want to compare the values. |
| 69 return (label == field.label && | 70 return (label == field.label && name == field.name && |
| 70 name == field.name && | |
| 71 form_control_type == field.form_control_type && | 71 form_control_type == field.form_control_type && |
| 72 autocomplete_attribute == field.autocomplete_attribute && | 72 autocomplete_attribute == field.autocomplete_attribute && |
| 73 max_length == field.max_length && | 73 max_length == field.max_length && |
| 74 // is_checked and is_autofilled counts as "value" since these change | 74 // is_checked and is_autofilled counts as "value" since these change |
| 75 // when we fill things in. | 75 // when we fill things in. |
| 76 is_checkable == field.is_checkable && | 76 is_checkable == field.is_checkable && |
| 77 is_focusable == field.is_focusable && | 77 is_focusable == field.is_focusable && |
| 78 should_autocomplete == field.should_autocomplete && | 78 should_autocomplete == field.should_autocomplete && |
| 79 text_direction == field.text_direction); | 79 role == field.role && text_direction == field.text_direction); |
| 80 // The option values/contents whith are the list of items in the list | 80 // The option values/contents whith are the list of items in the list |
| 81 // of a drop-down are currently not considered part of the identity of | 81 // of a drop-down are currently not considered part of the identity of |
| 82 // a form element. This is debatable, since one might base heuristics | 82 // a form element. This is debatable, since one might base heuristics |
| 83 // on the types of elements that are available. Alternatively, one | 83 // on the types of elements that are available. Alternatively, one |
| 84 // could imagine some forms that dynamically change the element | 84 // could imagine some forms that dynamically change the element |
| 85 // contents (say, insert years starting from the current year) that | 85 // contents (say, insert years starting from the current year) that |
| 86 // should not be considered changes in the structure of the form. | 86 // should not be considered changes in the structure of the form. |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool FormFieldData::operator<(const FormFieldData& field) const { | 89 bool FormFieldData::operator<(const FormFieldData& field) const { |
| 90 // Like operator==, this ignores the value. | 90 // Like operator==, this ignores the value. |
| 91 if (label < field.label) return true; | 91 if (label < field.label) return true; |
| 92 if (label > field.label) return false; | 92 if (label > field.label) return false; |
| 93 if (name < field.name) return true; | 93 if (name < field.name) return true; |
| 94 if (name > field.name) return false; | 94 if (name > field.name) return false; |
| 95 if (form_control_type < field.form_control_type) return true; | 95 if (form_control_type < field.form_control_type) return true; |
| 96 if (form_control_type > field.form_control_type) return false; | 96 if (form_control_type > field.form_control_type) return false; |
| 97 if (autocomplete_attribute < field.autocomplete_attribute) return true; | 97 if (autocomplete_attribute < field.autocomplete_attribute) return true; |
| 98 if (autocomplete_attribute > field.autocomplete_attribute) return false; | 98 if (autocomplete_attribute > field.autocomplete_attribute) return false; |
| 99 if (max_length < field.max_length) return true; | 99 if (max_length < field.max_length) return true; |
| 100 if (max_length > field.max_length) return false; | 100 if (max_length > field.max_length) return false; |
| 101 // Skip is_checked and is_autofilled as in SameFieldAs. | 101 // Skip is_checked and is_autofilled as in SameFieldAs. |
| 102 if (is_checkable < field.is_checkable) return true; | 102 if (is_checkable < field.is_checkable) return true; |
| 103 if (is_checkable > field.is_checkable) return false; | 103 if (is_checkable > field.is_checkable) return false; |
| 104 if (is_focusable < field.is_focusable) return true; | 104 if (is_focusable < field.is_focusable) return true; |
| 105 if (is_focusable > field.is_focusable) return false; | 105 if (is_focusable > field.is_focusable) return false; |
| 106 if (should_autocomplete < field.should_autocomplete) return true; | 106 if (should_autocomplete < field.should_autocomplete) return true; |
| 107 if (should_autocomplete > field.should_autocomplete) return false; | 107 if (should_autocomplete > field.should_autocomplete) return false; |
| 108 if (role < field.role) |
| 109 return true; |
| 110 if (role > field.role) |
| 111 return false; |
| 108 if (text_direction < field.text_direction) return true; | 112 if (text_direction < field.text_direction) return true; |
| 109 if (text_direction > field.text_direction) return false; | 113 if (text_direction > field.text_direction) return false; |
| 110 // See operator== above for why we don't check option_values/contents. | 114 // See operator== above for why we don't check option_values/contents. |
| 111 return false; | 115 return false; |
| 112 } | 116 } |
| 113 | 117 |
| 114 void SerializeFormFieldData(const FormFieldData& field_data, | 118 void SerializeFormFieldData(const FormFieldData& field_data, |
| 115 Pickle* pickle) { | 119 Pickle* pickle) { |
| 116 pickle->WriteInt(kPickleVersion); | 120 pickle->WriteInt(kPickleVersion); |
| 117 pickle->WriteString16(field_data.label); | 121 pickle->WriteString16(field_data.label); |
| 118 pickle->WriteString16(field_data.name); | 122 pickle->WriteString16(field_data.name); |
| 119 pickle->WriteString16(field_data.value); | 123 pickle->WriteString16(field_data.value); |
| 120 pickle->WriteString(field_data.form_control_type); | 124 pickle->WriteString(field_data.form_control_type); |
| 121 pickle->WriteString(field_data.autocomplete_attribute); | 125 pickle->WriteString(field_data.autocomplete_attribute); |
| 122 pickle->WriteSizeT(field_data.max_length); | 126 pickle->WriteSizeT(field_data.max_length); |
| 123 pickle->WriteBool(field_data.is_autofilled); | 127 pickle->WriteBool(field_data.is_autofilled); |
| 124 pickle->WriteBool(field_data.is_checked); | 128 pickle->WriteBool(field_data.is_checked); |
| 125 pickle->WriteBool(field_data.is_checkable); | 129 pickle->WriteBool(field_data.is_checkable); |
| 126 pickle->WriteBool(field_data.is_focusable); | 130 pickle->WriteBool(field_data.is_focusable); |
| 127 pickle->WriteBool(field_data.should_autocomplete); | 131 pickle->WriteBool(field_data.should_autocomplete); |
| 132 pickle->WriteInt(field_data.role); |
| 128 pickle->WriteInt(field_data.text_direction); | 133 pickle->WriteInt(field_data.text_direction); |
| 129 AddVectorToPickle(field_data.option_values, pickle); | 134 AddVectorToPickle(field_data.option_values, pickle); |
| 130 AddVectorToPickle(field_data.option_contents, pickle); | 135 AddVectorToPickle(field_data.option_contents, pickle); |
| 131 } | 136 } |
| 132 | 137 |
| 133 bool DeserializeFormFieldData(PickleIterator* iter, | 138 bool DeserializeFormFieldData(PickleIterator* iter, |
| 134 FormFieldData* field_data) { | 139 FormFieldData* field_data) { |
| 135 int version; | 140 int version; |
| 136 if (!iter->ReadInt(&version)) { | 141 if (!iter->ReadInt(&version)) { |
| 137 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; | 142 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; |
| 138 return false; | 143 return false; |
| 139 } | 144 } |
| 140 | 145 |
| 141 switch (version) { | 146 switch (version) { |
| 142 case 1: { | 147 case 1: { |
| 143 if (!iter->ReadString16(&field_data->label) || | 148 if (!iter->ReadString16(&field_data->label) || |
| 144 !iter->ReadString16(&field_data->name) || | 149 !iter->ReadString16(&field_data->name) || |
| 145 !iter->ReadString16(&field_data->value) || | 150 !iter->ReadString16(&field_data->value) || |
| 146 !iter->ReadString(&field_data->form_control_type) || | 151 !iter->ReadString(&field_data->form_control_type) || |
| 147 !iter->ReadString(&field_data->autocomplete_attribute) || | 152 !iter->ReadString(&field_data->autocomplete_attribute) || |
| 148 !iter->ReadSizeT(&field_data->max_length) || | 153 !iter->ReadSizeT(&field_data->max_length) || |
| 149 !iter->ReadBool(&field_data->is_autofilled) || | 154 !iter->ReadBool(&field_data->is_autofilled) || |
| 150 !iter->ReadBool(&field_data->is_checked) || | 155 !iter->ReadBool(&field_data->is_checked) || |
| 151 !iter->ReadBool(&field_data->is_checkable) || | 156 !iter->ReadBool(&field_data->is_checkable) || |
| 152 !iter->ReadBool(&field_data->is_focusable) || | 157 !iter->ReadBool(&field_data->is_focusable) || |
| 153 !iter->ReadBool(&field_data->should_autocomplete) || | 158 !iter->ReadBool(&field_data->should_autocomplete) || |
| 154 !ReadTextDirection(iter, &field_data->text_direction) || | 159 !ReadAsInt(iter, &field_data->role) || |
| 160 !ReadAsInt(iter, &field_data->text_direction) || |
| 155 !ReadStringVector(iter, &field_data->option_values) || | 161 !ReadStringVector(iter, &field_data->option_values) || |
| 156 !ReadStringVector(iter, &field_data->option_contents)) { | 162 !ReadStringVector(iter, &field_data->option_contents)) { |
| 157 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; | 163 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 158 return false; | 164 return false; |
| 159 } | 165 } |
| 160 break; | 166 break; |
| 161 } | 167 } |
| 162 default: { | 168 default: { |
| 163 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; | 169 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; |
| 164 return false; | 170 return false; |
| 165 } | 171 } |
| 166 } | 172 } |
| 167 return true; | 173 return true; |
| 168 } | 174 } |
| 169 | 175 |
| 170 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { | 176 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { |
| 171 return os | 177 return os << base::UTF16ToUTF8(field.label) << " " |
| 172 << base::UTF16ToUTF8(field.label) | 178 << base::UTF16ToUTF8(field.name) << " " |
| 173 << " " | 179 << base::UTF16ToUTF8(field.value) << " " << field.form_control_type |
| 174 << base::UTF16ToUTF8(field.name) | 180 << " " << field.autocomplete_attribute << " " << field.max_length |
| 175 << " " | 181 << " " << (field.is_autofilled ? "true" : "false") << " " |
| 176 << base::UTF16ToUTF8(field.value) | 182 << (field.is_checked ? "true" : "false") << " " |
| 177 << " " | 183 << (field.is_checkable ? "true" : "false") << " " |
| 178 << field.form_control_type | 184 << (field.is_focusable ? "true" : "false") << " " |
| 179 << " " | 185 << (field.should_autocomplete ? "true" : "false") << " " |
| 180 << field.autocomplete_attribute | 186 << field.role << " " << field.text_direction; |
| 181 << " " | |
| 182 << field.max_length | |
| 183 << " " | |
| 184 << (field.is_autofilled ? "true" : "false") | |
| 185 << " " | |
| 186 << (field.is_checked ? "true" : "false") | |
| 187 << " " | |
| 188 << (field.is_checkable ? "true" : "false") | |
| 189 << " " | |
| 190 << (field.is_focusable ? "true" : "false") | |
| 191 << " " | |
| 192 << (field.should_autocomplete ? "true" : "false") | |
| 193 << " " | |
| 194 << field.text_direction; | |
| 195 } | 187 } |
| 196 | 188 |
| 197 } // namespace autofill | 189 } // namespace autofill |
| OLD | NEW |