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/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 | 10 |
| 11 namespace { |
| 12 |
| 13 const int kPickleVersion = 1; |
| 14 |
| 15 void AddVectorToPickle(std::vector<base::string16> strings, |
| 16 Pickle* pickle) { |
| 17 pickle->WriteInt(static_cast<int>(strings.size())); |
| 18 for (size_t i = 0; i < strings.size(); ++i) { |
| 19 pickle->WriteString16(strings[i]); |
| 20 } |
| 21 } |
| 22 |
| 23 bool ReadStringVector(PickleIterator* iter, |
| 24 std::vector<base::string16>* strings) { |
| 25 int size; |
| 26 if (!iter->ReadInt(&size)) |
| 27 return false; |
| 28 |
| 29 string16 pickle_data; |
| 30 for (int i = 0; i < size; i++) { |
| 31 if (!iter->ReadString16(&pickle_data)) |
| 32 return false; |
| 33 |
| 34 strings->push_back(pickle_data); |
| 35 } |
| 36 return true; |
| 37 } |
| 38 |
| 39 bool ReadTextDirection(PickleIterator* iter, |
| 40 base::i18n::TextDirection* direction) { |
| 41 int pickle_data; |
| 42 if (!iter->ReadInt(&pickle_data)) |
| 43 return false; |
| 44 |
| 45 *direction = static_cast<base::i18n::TextDirection>(pickle_data); |
| 46 return true; |
| 47 } |
| 48 |
| 49 bool ReadSize(PickleIterator* iter, size_t* size) { |
| 50 uint64 pickle_data; |
| 51 if (!iter->ReadUInt64(&pickle_data)) |
| 52 return false; |
| 53 |
| 54 *size = static_cast<size_t>(pickle_data); |
| 55 return true; |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
10 namespace autofill { | 60 namespace autofill { |
11 | 61 |
12 FormFieldData::FormFieldData() | 62 FormFieldData::FormFieldData() |
13 : max_length(0), | 63 : max_length(0), |
14 is_autofilled(false), | 64 is_autofilled(false), |
15 is_checked(false), | 65 is_checked(false), |
16 is_checkable(false), | 66 is_checkable(false), |
17 is_focusable(false), | 67 is_focusable(false), |
18 should_autocomplete(true), | 68 should_autocomplete(true), |
19 text_direction(base::i18n::UNKNOWN_DIRECTION) { | 69 text_direction(base::i18n::UNKNOWN_DIRECTION) { |
(...skipping 16 matching lines...) Expand all Loading... |
36 return !operator==(field); | 86 return !operator==(field); |
37 } | 87 } |
38 | 88 |
39 bool FormFieldData::operator<(const FormFieldData& field) const { | 89 bool FormFieldData::operator<(const FormFieldData& field) const { |
40 if (label == field.label) | 90 if (label == field.label) |
41 return name < field.name; | 91 return name < field.name; |
42 | 92 |
43 return label < field.label; | 93 return label < field.label; |
44 } | 94 } |
45 | 95 |
| 96 void SerializeFormFieldData(const FormFieldData& field_data, |
| 97 Pickle* pickle) { |
| 98 pickle->WriteInt(kPickleVersion); |
| 99 pickle->WriteString16(field_data.label); |
| 100 pickle->WriteString16(field_data.name); |
| 101 pickle->WriteString16(field_data.value); |
| 102 pickle->WriteString(field_data.form_control_type); |
| 103 pickle->WriteString(field_data.autocomplete_attribute); |
| 104 pickle->WriteUInt64(static_cast<uint64>(field_data.max_length)); |
| 105 pickle->WriteBool(field_data.is_autofilled); |
| 106 pickle->WriteBool(field_data.is_checked); |
| 107 pickle->WriteBool(field_data.is_checkable); |
| 108 pickle->WriteBool(field_data.is_focusable); |
| 109 pickle->WriteBool(field_data.should_autocomplete); |
| 110 pickle->WriteInt(field_data.text_direction); |
| 111 AddVectorToPickle(field_data.option_values, pickle); |
| 112 AddVectorToPickle(field_data.option_contents, pickle); |
| 113 } |
| 114 |
| 115 bool DeserializeFormFieldData(PickleIterator* iter, |
| 116 FormFieldData* field_data) { |
| 117 int version; |
| 118 if (!iter->ReadInt(&version)) { |
| 119 LOG(ERROR) << "Bad pickle of FormFieldData, no version present"; |
| 120 return false; |
| 121 } |
| 122 |
| 123 switch (version) { |
| 124 case 1: { |
| 125 if (!iter->ReadString16(&field_data->label) || |
| 126 !iter->ReadString16(&field_data->name) || |
| 127 !iter->ReadString16(&field_data->value) || |
| 128 !iter->ReadString(&field_data->form_control_type) || |
| 129 !iter->ReadString(&field_data->autocomplete_attribute) || |
| 130 !ReadSize(iter, &field_data->max_length) || |
| 131 !iter->ReadBool(&field_data->is_autofilled) || |
| 132 !iter->ReadBool(&field_data->is_checked) || |
| 133 !iter->ReadBool(&field_data->is_checkable) || |
| 134 !iter->ReadBool(&field_data->is_focusable) || |
| 135 !iter->ReadBool(&field_data->should_autocomplete) || |
| 136 !ReadTextDirection(iter, &field_data->text_direction) || |
| 137 !ReadStringVector(iter, &field_data->option_values) || |
| 138 !ReadStringVector(iter, &field_data->option_contents)) { |
| 139 LOG(ERROR) << "Could not deserialize FormFieldData from pickle"; |
| 140 return false; |
| 141 } |
| 142 break; |
| 143 } |
| 144 default: { |
| 145 LOG(ERROR) << "Unknown FormFieldData pickle version " << version; |
| 146 return false; |
| 147 } |
| 148 } |
| 149 return true; |
| 150 } |
| 151 |
46 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { | 152 std::ostream& operator<<(std::ostream& os, const FormFieldData& field) { |
47 return os | 153 return os |
48 << UTF16ToUTF8(field.label) | 154 << UTF16ToUTF8(field.label) |
49 << " " | 155 << " " |
50 << UTF16ToUTF8(field.name) | 156 << UTF16ToUTF8(field.name) |
51 << " " | 157 << " " |
52 << UTF16ToUTF8(field.value) | 158 << UTF16ToUTF8(field.value) |
53 << " " | 159 << " " |
54 << field.form_control_type | 160 << field.form_control_type |
55 << " " | 161 << " " |
56 << field.autocomplete_attribute | 162 << field.autocomplete_attribute |
57 << " " | 163 << " " |
58 << field.max_length | 164 << field.max_length |
59 << " " | 165 << " " |
60 << (field.is_autofilled ? "true" : "false") | 166 << (field.is_autofilled ? "true" : "false") |
61 << " " | 167 << " " |
62 << (field.is_checked ? "true" : "false") | 168 << (field.is_checked ? "true" : "false") |
63 << " " | 169 << " " |
64 << (field.is_checkable ? "true" : "false") | 170 << (field.is_checkable ? "true" : "false") |
65 << " " | 171 << " " |
66 << (field.is_focusable ? "true" : "false") | 172 << (field.is_focusable ? "true" : "false") |
67 << " " | 173 << " " |
68 << (field.should_autocomplete ? "true" : "false") | 174 << (field.should_autocomplete ? "true" : "false") |
69 << " " | 175 << " " |
70 << field.text_direction; | 176 << field.text_direction; |
71 } | 177 } |
72 | 178 |
73 } // namespace autofill | 179 } // namespace autofill |
OLD | NEW |