ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / form_field.cc
blob43807079963877df758fcaf07e6b24e02a618f26
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
3 // found in the LICENSE file.
5 #include "components/autofill/core/browser/form_field.h"
7 #include <stddef.h>
8 #include <string>
9 #include <utility>
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/core/browser/address_field.h"
17 #include "components/autofill/core/browser/autofill_field.h"
18 #include "components/autofill/core/browser/autofill_regexes.h"
19 #include "components/autofill/core/browser/autofill_scanner.h"
20 #include "components/autofill/core/browser/credit_card_field.h"
21 #include "components/autofill/core/browser/email_field.h"
22 #include "components/autofill/core/browser/form_structure.h"
23 #include "components/autofill/core/browser/name_field.h"
24 #include "components/autofill/core/browser/phone_field.h"
26 namespace autofill {
27 namespace {
29 bool ShouldBeIgnored(const AutofillField* field) {
30 // Ignore checkable fields as they interfere with parsers assuming context.
31 // Eg., while parsing address, "Is PO box" checkbox after ADDRESS_LINE1
32 // interferes with correctly understanding ADDRESS_LINE2.
33 // Ignore fields marked as presentational. See
34 // http://www.w3.org/TR/wai-aria/roles#presentation
35 return field->is_checkable ||
36 field->role == FormFieldData::ROLE_ATTRIBUTE_PRESENTATION;
39 } // namespace
41 // static
42 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields,
43 bool is_form_tag,
44 ServerFieldTypeMap* map) {
45 DCHECK(map->empty());
47 // Set up a working copy of the fields to be processed.
48 std::vector<AutofillField*> remaining_fields(fields.size());
49 std::copy(fields.begin(), fields.end(), remaining_fields.begin());
51 remaining_fields.erase(
52 std::remove_if(remaining_fields.begin(), remaining_fields.end(),
53 ShouldBeIgnored),
54 remaining_fields.end());
56 ServerFieldTypeMap saved_map = *map;
58 // Email pass.
59 ParseFormFieldsPass(EmailField::Parse, &remaining_fields, map);
60 size_t email_count = map->size();
62 // Phone pass.
63 ParseFormFieldsPass(PhoneField::Parse, &remaining_fields, map);
65 // Address pass.
66 ParseFormFieldsPass(AddressField::Parse, &remaining_fields, map);
68 // Credit card pass.
69 ParseFormFieldsPass(CreditCardField::Parse, &remaining_fields, map);
71 // Name pass.
72 ParseFormFieldsPass(NameField::Parse, &remaining_fields, map);
74 // Do not autofill a form if there are less than 3 recognized fields.
75 // Otherwise it is very easy to have false positives. http://crbug.com/447332
76 // For <form> tags, make an exception for email fields, which are commonly the
77 // only recognized field on account registration sites.
78 size_t kThreshold = 3;
79 bool accept_parsing = (map->size() >= kThreshold ||
80 (is_form_tag && email_count > 0));
81 if (!accept_parsing)
82 *map = saved_map;
85 // static
86 bool FormField::ParseField(AutofillScanner* scanner,
87 const base::string16& pattern,
88 AutofillField** match) {
89 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match);
92 // static
93 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner,
94 const base::string16& pattern,
95 int match_type,
96 AutofillField** match) {
97 if (scanner->IsEnd())
98 return false;
100 const AutofillField* field = scanner->Cursor();
102 if (!MatchesFormControlType(field->form_control_type, match_type))
103 return false;
105 return MatchAndAdvance(scanner, pattern, match_type, match);
108 // static
109 bool FormField::ParseEmptyLabel(AutofillScanner* scanner,
110 AutofillField** match) {
111 return ParseFieldSpecifics(scanner,
112 base::ASCIIToUTF16("^$"),
113 MATCH_LABEL | MATCH_ALL_INPUTS,
114 match);
117 // static
118 bool FormField::AddClassification(const AutofillField* field,
119 ServerFieldType type,
120 ServerFieldTypeMap* map) {
121 // Several fields are optional.
122 if (!field)
123 return true;
125 return map->insert(make_pair(field->unique_name(), type)).second;
128 // static.
129 bool FormField::MatchAndAdvance(AutofillScanner* scanner,
130 const base::string16& pattern,
131 int match_type,
132 AutofillField** match) {
133 AutofillField* field = scanner->Cursor();
134 if (FormField::Match(field, pattern, match_type)) {
135 if (match)
136 *match = field;
137 scanner->Advance();
138 return true;
141 return false;
144 // static
145 bool FormField::Match(const AutofillField* field,
146 const base::string16& pattern,
147 int match_type) {
148 if ((match_type & FormField::MATCH_LABEL) &&
149 MatchesPattern(field->label, pattern)) {
150 return true;
153 if ((match_type & FormField::MATCH_NAME) &&
154 MatchesPattern(field->name, pattern)) {
155 return true;
158 if ((match_type & FormField::MATCH_VALUE) &&
159 MatchesPattern(field->value, pattern)) {
160 return true;
163 return false;
166 // static
167 void FormField::ParseFormFieldsPass(ParseFunction parse,
168 std::vector<AutofillField*>* fields,
169 ServerFieldTypeMap* map) {
170 // Store unmatched fields for further processing by the caller.
171 std::vector<AutofillField*> remaining_fields;
173 AutofillScanner scanner(*fields);
174 while (!scanner.IsEnd()) {
175 scoped_ptr<FormField> form_field(parse(&scanner));
176 if (!form_field) {
177 remaining_fields.push_back(scanner.Cursor());
178 scanner.Advance();
179 continue;
182 // Add entries into the map for each field type found in |form_field|.
183 bool ok = form_field->ClassifyField(map);
184 DCHECK(ok);
187 std::swap(*fields, remaining_fields);
190 bool FormField::MatchesFormControlType(const std::string& type,
191 int match_type) {
192 if ((match_type & MATCH_TEXT) && type == "text")
193 return true;
195 if ((match_type & MATCH_EMAIL) && type == "email")
196 return true;
198 if ((match_type & MATCH_TELEPHONE) && type == "tel")
199 return true;
201 if ((match_type & MATCH_SELECT) && type == "select-one")
202 return true;
204 if ((match_type & MATCH_TEXT_AREA) && type == "textarea")
205 return true;
207 if ((match_type & MATCH_PASSWORD) && type == "password")
208 return true;
210 return false;
213 } // namespace autofill