Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: components/autofill/browser/form_field.cc

Issue 17392006: In components/autofill, move browser/ to core/browser/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase to fix conflicts Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/autofill/browser/form_field.h"
6
7 #include <stddef.h>
8 #include <string>
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "components/autofill/browser/address_field.h"
17 #include "components/autofill/browser/autofill_field.h"
18 #include "components/autofill/browser/autofill_regexes.h"
19 #include "components/autofill/browser/autofill_scanner.h"
20 #include "components/autofill/browser/credit_card_field.h"
21 #include "components/autofill/browser/email_field.h"
22 #include "components/autofill/browser/field_types.h"
23 #include "components/autofill/browser/form_structure.h"
24 #include "components/autofill/browser/name_field.h"
25 #include "components/autofill/browser/phone_field.h"
26 #include "ui/base/l10n/l10n_util.h"
27
28 namespace autofill {
29 namespace {
30
31 bool IsTextField(const std::string& type) {
32 return type == "text";
33 }
34
35 bool IsEmailField(const std::string& type) {
36 return type == "email";
37 }
38
39 bool IsTelephoneField(const std::string& type) {
40 return type == "tel";
41 }
42
43 bool IsSelectField(const std::string& type) {
44 return type == "select-one";
45 }
46
47 bool IsCheckable(const AutofillField* field) {
48 return field->is_checkable;
49 }
50
51 } // namespace
52
53 // static
54 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields,
55 FieldTypeMap* map) {
56 // Set up a working copy of the fields to be processed.
57 std::vector<const AutofillField*> remaining_fields(fields.size());
58 std::copy(fields.begin(), fields.end(), remaining_fields.begin());
59
60 // Ignore checkable fields as they interfere with parsers assuming context.
61 // Eg., while parsing address, "Is PO box" checkbox after ADDRESS_LINE1
62 // interferes with correctly understanding ADDRESS_LINE2.
63 remaining_fields.erase(
64 std::remove_if(remaining_fields.begin(), remaining_fields.end(),
65 IsCheckable),
66 remaining_fields.end());
67
68 // Email pass.
69 ParseFormFieldsPass(EmailField::Parse, &remaining_fields, map);
70
71 // Phone pass.
72 ParseFormFieldsPass(PhoneField::Parse, &remaining_fields, map);
73
74 // Address pass.
75 ParseFormFieldsPass(AddressField::Parse, &remaining_fields, map);
76
77 // Credit card pass.
78 ParseFormFieldsPass(CreditCardField::Parse, &remaining_fields, map);
79
80 // Name pass.
81 ParseFormFieldsPass(NameField::Parse, &remaining_fields, map);
82 }
83
84 // static
85 bool FormField::ParseField(AutofillScanner* scanner,
86 const base::string16& pattern,
87 const AutofillField** match) {
88 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match);
89 }
90
91 // static
92 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner,
93 const base::string16& pattern,
94 int match_type,
95 const AutofillField** match) {
96 if (scanner->IsEnd())
97 return false;
98
99 const AutofillField* field = scanner->Cursor();
100
101 if ((match_type & MATCH_TEXT) && IsTextField(field->form_control_type))
102 return MatchAndAdvance(scanner, pattern, match_type, match);
103
104 if ((match_type & MATCH_EMAIL) && IsEmailField(field->form_control_type))
105 return MatchAndAdvance(scanner, pattern, match_type, match);
106
107 if ((match_type & MATCH_TELEPHONE) &&
108 IsTelephoneField(field->form_control_type)) {
109 return MatchAndAdvance(scanner, pattern, match_type, match);
110 }
111
112 if ((match_type & MATCH_SELECT) && IsSelectField(field->form_control_type))
113 return MatchAndAdvance(scanner, pattern, match_type, match);
114
115 return false;
116 }
117
118 // static
119 bool FormField::ParseEmptyLabel(AutofillScanner* scanner,
120 const AutofillField** match) {
121 return ParseFieldSpecifics(scanner,
122 ASCIIToUTF16("^$"),
123 MATCH_LABEL | MATCH_ALL_INPUTS,
124 match);
125 }
126
127 // static
128 bool FormField::AddClassification(const AutofillField* field,
129 AutofillFieldType type,
130 FieldTypeMap* map) {
131 // Several fields are optional.
132 if (!field)
133 return true;
134
135 return map->insert(make_pair(field->unique_name(), type)).second;
136 }
137
138 // static.
139 bool FormField::MatchAndAdvance(AutofillScanner* scanner,
140 const base::string16& pattern,
141 int match_type,
142 const AutofillField** match) {
143 const AutofillField* field = scanner->Cursor();
144 if (FormField::Match(field, pattern, match_type)) {
145 if (match)
146 *match = field;
147 scanner->Advance();
148 return true;
149 }
150
151 return false;
152 }
153
154 // static
155 bool FormField::Match(const AutofillField* field,
156 const base::string16& pattern,
157 int match_type) {
158 if ((match_type & FormField::MATCH_LABEL) &&
159 autofill::MatchesPattern(field->label, pattern)) {
160 return true;
161 }
162
163 if ((match_type & FormField::MATCH_NAME) &&
164 autofill::MatchesPattern(field->name, pattern)) {
165 return true;
166 }
167
168 if ((match_type & FormField::MATCH_VALUE) &&
169 autofill::MatchesPattern(field->value, pattern)) {
170 return true;
171 }
172
173 return false;
174 }
175
176 // static
177 void FormField::ParseFormFieldsPass(ParseFunction parse,
178 std::vector<const AutofillField*>* fields,
179 FieldTypeMap* map) {
180 // Store unmatched fields for further processing by the caller.
181 std::vector<const AutofillField*> remaining_fields;
182
183 AutofillScanner scanner(*fields);
184 while (!scanner.IsEnd()) {
185 scoped_ptr<FormField> form_field(parse(&scanner));
186 if (!form_field.get()) {
187 remaining_fields.push_back(scanner.Cursor());
188 scanner.Advance();
189 continue;
190 }
191
192 // Add entries into the map for each field type found in |form_field|.
193 bool ok = form_field->ClassifyField(map);
194 DCHECK(ok);
195 }
196
197 std::swap(*fields, remaining_fields);
198 }
199
200 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/browser/form_field.h ('k') | components/autofill/browser/form_field_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698