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/browser/form_field.h" | 5 #include "components/autofill/core/browser/form_field.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" |
15 #include "components/autofill/core/browser/address_field.h" | 16 #include "components/autofill/core/browser/address_field.h" |
16 #include "components/autofill/core/browser/autofill_field.h" | 17 #include "components/autofill/core/browser/autofill_field.h" |
17 #include "components/autofill/core/browser/autofill_scanner.h" | 18 #include "components/autofill/core/browser/autofill_scanner.h" |
18 #include "components/autofill/core/browser/credit_card_field.h" | 19 #include "components/autofill/core/browser/credit_card_field.h" |
19 #include "components/autofill/core/browser/email_field.h" | 20 #include "components/autofill/core/browser/email_field.h" |
20 #include "components/autofill/core/browser/form_structure.h" | 21 #include "components/autofill/core/browser/form_structure.h" |
21 #include "components/autofill/core/browser/name_field.h" | 22 #include "components/autofill/core/browser/name_field.h" |
22 #include "components/autofill/core/browser/phone_field.h" | 23 #include "components/autofill/core/browser/phone_field.h" |
23 #include "components/autofill/core/common/autofill_regexes.h" | 24 #include "components/autofill/core/common/autofill_regexes.h" |
24 | 25 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // only recognized field on account registration sites. | 77 // only recognized field on account registration sites. |
77 size_t kThreshold = 3; | 78 size_t kThreshold = 3; |
78 bool accept_parsing = (map->size() >= kThreshold || | 79 bool accept_parsing = (map->size() >= kThreshold || |
79 (is_form_tag && email_count > 0)); | 80 (is_form_tag && email_count > 0)); |
80 if (!accept_parsing) | 81 if (!accept_parsing) |
81 *map = saved_map; | 82 *map = saved_map; |
82 } | 83 } |
83 | 84 |
84 // static | 85 // static |
85 bool FormField::ParseField(AutofillScanner* scanner, | 86 bool FormField::ParseField(AutofillScanner* scanner, |
86 const std::string& pattern, | 87 const base::string16& pattern, |
87 AutofillField** match) { | 88 AutofillField** match) { |
88 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match); | 89 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match); |
89 } | 90 } |
90 | 91 |
91 // static | 92 // static |
92 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, | 93 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, |
93 const std::string& pattern, | 94 const base::string16& pattern, |
94 int match_type, | 95 int match_type, |
95 AutofillField** match) { | 96 AutofillField** match) { |
96 if (scanner->IsEnd()) | 97 if (scanner->IsEnd()) |
97 return false; | 98 return false; |
98 | 99 |
99 const AutofillField* field = scanner->Cursor(); | 100 const AutofillField* field = scanner->Cursor(); |
100 | 101 |
101 if (!MatchesFormControlType(field->form_control_type, match_type)) | 102 if (!MatchesFormControlType(field->form_control_type, match_type)) |
102 return false; | 103 return false; |
103 | 104 |
104 return MatchAndAdvance(scanner, pattern, match_type, match); | 105 return MatchAndAdvance(scanner, pattern, match_type, match); |
105 } | 106 } |
106 | 107 |
107 // static | 108 // static |
108 FormField::ParseNameLabelResult FormField::ParseNameAndLabelSeparately( | 109 FormField::ParseNameLabelResult FormField::ParseNameAndLabelSeparately( |
109 AutofillScanner* scanner, | 110 AutofillScanner* scanner, |
110 const std::string& pattern, | 111 const base::string16& pattern, |
111 int match_type, | 112 int match_type, |
112 AutofillField** match) { | 113 AutofillField** match) { |
113 if (scanner->IsEnd()) | 114 if (scanner->IsEnd()) |
114 return RESULT_MATCH_NONE; | 115 return RESULT_MATCH_NONE; |
115 | 116 |
116 AutofillField* cur_match = nullptr; | 117 AutofillField* cur_match = nullptr; |
117 size_t saved_cursor = scanner->SaveCursor(); | 118 size_t saved_cursor = scanner->SaveCursor(); |
118 bool parsed_name = ParseFieldSpecifics(scanner, | 119 bool parsed_name = ParseFieldSpecifics(scanner, |
119 pattern, | 120 pattern, |
120 match_type & ~MATCH_LABEL, | 121 match_type & ~MATCH_LABEL, |
(...skipping 14 matching lines...) Expand all Loading... |
135 return RESULT_MATCH_NAME; | 136 return RESULT_MATCH_NAME; |
136 if (parsed_label) | 137 if (parsed_label) |
137 return RESULT_MATCH_LABEL; | 138 return RESULT_MATCH_LABEL; |
138 return RESULT_MATCH_NONE; | 139 return RESULT_MATCH_NONE; |
139 } | 140 } |
140 | 141 |
141 // static | 142 // static |
142 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, | 143 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, |
143 AutofillField** match) { | 144 AutofillField** match) { |
144 return ParseFieldSpecifics(scanner, | 145 return ParseFieldSpecifics(scanner, |
145 "^$", | 146 base::ASCIIToUTF16("^$"), |
146 MATCH_LABEL | MATCH_ALL_INPUTS, | 147 MATCH_LABEL | MATCH_ALL_INPUTS, |
147 match); | 148 match); |
148 } | 149 } |
149 | 150 |
150 // static | 151 // static |
151 bool FormField::AddClassification(const AutofillField* field, | 152 bool FormField::AddClassification(const AutofillField* field, |
152 ServerFieldType type, | 153 ServerFieldType type, |
153 ServerFieldTypeMap* map) { | 154 ServerFieldTypeMap* map) { |
154 // Several fields are optional. | 155 // Several fields are optional. |
155 if (!field) | 156 if (!field) |
156 return true; | 157 return true; |
157 | 158 |
158 return map->insert(make_pair(field->unique_name(), type)).second; | 159 return map->insert(make_pair(field->unique_name(), type)).second; |
159 } | 160 } |
160 | 161 |
161 // static. | 162 // static. |
162 bool FormField::MatchAndAdvance(AutofillScanner* scanner, | 163 bool FormField::MatchAndAdvance(AutofillScanner* scanner, |
163 const std::string& pattern, | 164 const base::string16& pattern, |
164 int match_type, | 165 int match_type, |
165 AutofillField** match) { | 166 AutofillField** match) { |
166 AutofillField* field = scanner->Cursor(); | 167 AutofillField* field = scanner->Cursor(); |
167 if (FormField::Match(field, pattern, match_type)) { | 168 if (FormField::Match(field, pattern, match_type)) { |
168 if (match) | 169 if (match) |
169 *match = field; | 170 *match = field; |
170 scanner->Advance(); | 171 scanner->Advance(); |
171 return true; | 172 return true; |
172 } | 173 } |
173 | 174 |
174 return false; | 175 return false; |
175 } | 176 } |
176 | 177 |
177 // static | 178 // static |
178 bool FormField::Match(const AutofillField* field, | 179 bool FormField::Match(const AutofillField* field, |
179 const std::string& pattern, | 180 const base::string16& pattern, |
180 int match_type) { | 181 int match_type) { |
181 if ((match_type & FormField::MATCH_LABEL) && | 182 if ((match_type & FormField::MATCH_LABEL) && |
182 MatchesPattern(field->label, pattern)) { | 183 MatchesPattern(field->label, pattern)) { |
183 return true; | 184 return true; |
184 } | 185 } |
185 | 186 |
186 if ((match_type & FormField::MATCH_NAME) && | 187 if ((match_type & FormField::MATCH_NAME) && |
187 MatchesPattern(field->name, pattern)) { | 188 MatchesPattern(field->name, pattern)) { |
188 return true; | 189 return true; |
189 } | 190 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if ((match_type & MATCH_PASSWORD) && type == "password") | 236 if ((match_type & MATCH_PASSWORD) && type == "password") |
236 return true; | 237 return true; |
237 | 238 |
238 if ((match_type & MATCH_NUMBER) && type == "number") | 239 if ((match_type & MATCH_NUMBER) && type == "number") |
239 return true; | 240 return true; |
240 | 241 |
241 return false; | 242 return false; |
242 } | 243 } |
243 | 244 |
244 } // namespace autofill | 245 } // namespace autofill |
OLD | NEW |