| 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 = 2; | 17 const int kPickleVersion = 2; |
| 18 | 18 |
| 19 void AddVectorToPickle(std::vector<base::string16> strings, | 19 void AddVectorToPickle(std::vector<base::string16> strings, |
| 20 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(PickleIterator* iter, | 27 bool ReadStringVector(base::PickleIterator* iter, |
| 28 std::vector<base::string16>* strings) { | 28 std::vector<base::string16>* strings) { |
| 29 int size; | 29 int size; |
| 30 if (!iter->ReadInt(&size)) | 30 if (!iter->ReadInt(&size)) |
| 31 return false; | 31 return false; |
| 32 | 32 |
| 33 base::string16 pickle_data; | 33 base::string16 pickle_data; |
| 34 for (int i = 0; i < size; i++) { | 34 for (int i = 0; i < size; i++) { |
| 35 if (!iter->ReadString16(&pickle_data)) | 35 if (!iter->ReadString16(&pickle_data)) |
| 36 return false; | 36 return false; |
| 37 | 37 |
| 38 strings->push_back(pickle_data); | 38 strings->push_back(pickle_data); |
| 39 } | 39 } |
| 40 return true; | 40 return true; |
| 41 } | 41 } |
| 42 | 42 |
| 43 template <typename T> | 43 template <typename T> |
| 44 bool ReadAsInt(PickleIterator* iter, T* target_value) { | 44 bool ReadAsInt(base::PickleIterator* iter, T* target_value) { |
| 45 int pickle_data; | 45 int pickle_data; |
| 46 if (!iter->ReadInt(&pickle_data)) | 46 if (!iter->ReadInt(&pickle_data)) |
| 47 return false; | 47 return false; |
| 48 | 48 |
| 49 *target_value = static_cast<T>(pickle_data); | 49 *target_value = static_cast<T>(pickle_data); |
| 50 return true; | 50 return true; |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool DeserializeCommonSection1(PickleIterator* iter, | 53 bool DeserializeCommonSection1(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->ReadSizeT(&field_data->max_length) && | 60 iter->ReadSizeT(&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 iter->ReadBool(&field_data->is_checked) && |
| 63 iter->ReadBool(&field_data->is_checkable) && | 63 iter->ReadBool(&field_data->is_checkable) && |
| 64 iter->ReadBool(&field_data->is_focusable) && | 64 iter->ReadBool(&field_data->is_focusable) && |
| 65 iter->ReadBool(&field_data->should_autocomplete); | 65 iter->ReadBool(&field_data->should_autocomplete); |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool DeserializeCommonSection2(PickleIterator* iter, | 68 bool DeserializeCommonSection2(base::PickleIterator* iter, |
| 69 FormFieldData* field_data) { | 69 FormFieldData* field_data) { |
| 70 return ReadAsInt(iter, &field_data->text_direction) && | 70 return ReadAsInt(iter, &field_data->text_direction) && |
| 71 ReadStringVector(iter, &field_data->option_values) && | 71 ReadStringVector(iter, &field_data->option_values) && |
| 72 ReadStringVector(iter, &field_data->option_contents); | 72 ReadStringVector(iter, &field_data->option_contents); |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool DeserializeVersion2Specific(PickleIterator* iter, | 75 bool DeserializeVersion2Specific(base::PickleIterator* iter, |
| 76 FormFieldData* field_data) { | 76 FormFieldData* field_data) { |
| 77 return ReadAsInt(iter, &field_data->role); | 77 return ReadAsInt(iter, &field_data->role); |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 FormFieldData::FormFieldData() | 82 FormFieldData::FormFieldData() |
| 83 : max_length(0), | 83 : max_length(0), |
| 84 is_autofilled(false), | 84 is_autofilled(false), |
| 85 is_checked(false), | 85 is_checked(false), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 if (should_autocomplete > field.should_autocomplete) return false; | 136 if (should_autocomplete > field.should_autocomplete) return false; |
| 137 if (role < field.role) return true; | 137 if (role < field.role) return true; |
| 138 if (role > field.role) return false; | 138 if (role > field.role) return false; |
| 139 if (text_direction < field.text_direction) return true; | 139 if (text_direction < field.text_direction) return true; |
| 140 if (text_direction > field.text_direction) return false; | 140 if (text_direction > field.text_direction) return false; |
| 141 // 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. |
| 142 return false; | 142 return false; |
| 143 } | 143 } |
| 144 | 144 |
| 145 void SerializeFormFieldData(const FormFieldData& field_data, | 145 void SerializeFormFieldData(const FormFieldData& field_data, |
| 146 Pickle* pickle) { | 146 base::Pickle* pickle) { |
| 147 pickle->WriteInt(kPickleVersion); | 147 pickle->WriteInt(kPickleVersion); |
| 148 pickle->WriteString16(field_data.label); | 148 pickle->WriteString16(field_data.label); |
| 149 pickle->WriteString16(field_data.name); | 149 pickle->WriteString16(field_data.name); |
| 150 pickle->WriteString16(field_data.value); | 150 pickle->WriteString16(field_data.value); |
| 151 pickle->WriteString(field_data.form_control_type); | 151 pickle->WriteString(field_data.form_control_type); |
| 152 pickle->WriteString(field_data.autocomplete_attribute); | 152 pickle->WriteString(field_data.autocomplete_attribute); |
| 153 pickle->WriteSizeT(field_data.max_length); | 153 pickle->WriteSizeT(field_data.max_length); |
| 154 pickle->WriteBool(field_data.is_autofilled); | 154 pickle->WriteBool(field_data.is_autofilled); |
| 155 pickle->WriteBool(field_data.is_checked); | 155 pickle->WriteBool(field_data.is_checked); |
| 156 pickle->WriteBool(field_data.is_checkable); | 156 pickle->WriteBool(field_data.is_checkable); |
| 157 pickle->WriteBool(field_data.is_focusable); | 157 pickle->WriteBool(field_data.is_focusable); |
| 158 pickle->WriteBool(field_data.should_autocomplete); | 158 pickle->WriteBool(field_data.should_autocomplete); |
| 159 pickle->WriteInt(field_data.role); | 159 pickle->WriteInt(field_data.role); |
| 160 pickle->WriteInt(field_data.text_direction); | 160 pickle->WriteInt(field_data.text_direction); |
| 161 AddVectorToPickle(field_data.option_values, pickle); | 161 AddVectorToPickle(field_data.option_values, pickle); |
| 162 AddVectorToPickle(field_data.option_contents, pickle); | 162 AddVectorToPickle(field_data.option_contents, pickle); |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool DeserializeFormFieldData(PickleIterator* iter, | 165 bool DeserializeFormFieldData(base::PickleIterator* iter, |
| 166 FormFieldData* field_data) { | 166 FormFieldData* field_data) { |
| 167 int version; | 167 int version; |
| 168 FormFieldData temp_form_field_data; | 168 FormFieldData temp_form_field_data; |
| 169 if (!iter->ReadInt(&version)) { | 169 if (!iter->ReadInt(&version)) { |
| 170 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; | 170 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; |
| 171 return false; | 171 return false; |
| 172 } | 172 } |
| 173 | 173 |
| 174 switch (version) { | 174 switch (version) { |
| 175 case 1: { | 175 case 1: { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 205 << " " << field.autocomplete_attribute << " " << field.max_length | 205 << " " << field.autocomplete_attribute << " " << field.max_length |
| 206 << " " << (field.is_autofilled ? "true" : "false") << " " | 206 << " " << (field.is_autofilled ? "true" : "false") << " " |
| 207 << (field.is_checked ? "true" : "false") << " " | 207 << (field.is_checked ? "true" : "false") << " " |
| 208 << (field.is_checkable ? "true" : "false") << " " | 208 << (field.is_checkable ? "true" : "false") << " " |
| 209 << (field.is_focusable ? "true" : "false") << " " | 209 << (field.is_focusable ? "true" : "false") << " " |
| 210 << (field.should_autocomplete ? "true" : "false") << " " | 210 << (field.should_autocomplete ? "true" : "false") << " " |
| 211 << field.role << " " << field.text_direction; | 211 << field.role << " " << field.text_direction; |
| 212 } | 212 } |
| 213 | 213 |
| 214 } // namespace autofill | 214 } // namespace autofill |
| OLD | NEW |