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