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

Side by Side Diff: chrome/browser/autofill/credit_card_field.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 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 "chrome/browser/autofill/credit_card_field.h"
6
7 #include <stddef.h>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/string16.h"
12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/autofill/autofill_field.h"
15 #include "chrome/browser/autofill/autofill_regex_constants.h"
16 #include "chrome/browser/autofill/autofill_scanner.h"
17 #include "chrome/browser/autofill/field_types.h"
18 #include "ui/base/l10n/l10n_util.h"
19
20 // static
21 FormField* CreditCardField::Parse(AutofillScanner* scanner) {
22 if (scanner->IsEnd())
23 return NULL;
24
25 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
26 size_t saved_cursor = scanner->SaveCursor();
27
28 // Credit card fields can appear in many different orders.
29 // We loop until no more credit card related fields are found, see |break| at
30 // bottom of the loop.
31 for (int fields = 0; !scanner->IsEnd(); ++fields) {
32 // Ignore gift card fields.
33 if (ParseField(scanner, UTF8ToUTF16(autofill::kGiftCardRe), NULL))
34 break;
35
36 // Sometimes the cardholder field is just labeled "name". Unfortunately this
37 // is a dangerously generic word to search for, since it will often match a
38 // name (not cardholder name) field before or after credit card fields. So
39 // we search for "name" only when we've already parsed at least one other
40 // credit card field and haven't yet parsed the expiration date (which
41 // usually appears at the end).
42 if (credit_card_field->cardholder_ == NULL) {
43 string16 name_pattern;
44 if (fields == 0 || credit_card_field->expiration_month_) {
45 // at beginning or end
46 name_pattern = UTF8ToUTF16(autofill::kNameOnCardRe);
47 } else {
48 name_pattern = UTF8ToUTF16(autofill::kNameOnCardContextualRe);
49 }
50
51 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_))
52 continue;
53
54 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html
55 // and ExpediaBilling.html in our test suite), recognize separate fields
56 // for the cardholder's first and last name if they have the labels "cfnm"
57 // and "clnm".
58 scanner->SaveCursor();
59 const AutofillField* first;
60 if (ParseField(scanner, ASCIIToUTF16("^cfnm"), &first) &&
61 ParseField(scanner, ASCIIToUTF16("^clnm"),
62 &credit_card_field->cardholder_last_)) {
63 credit_card_field->cardholder_ = first;
64 continue;
65 }
66 scanner->Rewind();
67 }
68
69 // Check for a credit card type (Visa, MasterCard, etc.) field.
70 string16 type_pattern = UTF8ToUTF16(autofill::kCardTypeRe);
71 if (!credit_card_field->type_ &&
72 ParseFieldSpecifics(scanner, type_pattern,
73 MATCH_DEFAULT | MATCH_SELECT,
74 &credit_card_field->type_)) {
75 continue;
76 }
77
78 // We look for a card security code before we look for a credit
79 // card number and match the general term "number". The security code
80 // has a plethora of names; we've seen "verification #",
81 // "verification number", "card identification number" and others listed
82 // in the |pattern| below.
83 string16 pattern = UTF8ToUTF16(autofill::kCardCvcRe);
84 if (!credit_card_field->verification_ &&
85 ParseField(scanner, pattern, &credit_card_field->verification_)) {
86 continue;
87 }
88
89 pattern = UTF8ToUTF16(autofill::kCardNumberRe);
90 if (!credit_card_field->number_ &&
91 ParseField(scanner, pattern, &credit_card_field->number_)) {
92 continue;
93 }
94
95 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) {
96 credit_card_field->expiration_month_ = scanner->Cursor();
97 scanner->Advance();
98 } else {
99 // First try to parse split month/year expiration fields.
100 scanner->SaveCursor();
101 pattern = UTF8ToUTF16(autofill::kExpirationMonthRe);
102 if (!credit_card_field->expiration_month_ &&
103 ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT,
104 &credit_card_field->expiration_month_)) {
105 pattern = UTF8ToUTF16(autofill::kExpirationYearRe);
106 if (ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT,
107 &credit_card_field->expiration_year_)) {
108 continue;
109 }
110 }
111
112 // If that fails, try to parse a combined expiration field.
113 if (!credit_card_field->expiration_date_) {
114 // Look for a 2-digit year first.
115 scanner->Rewind();
116 pattern = UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe);
117 // We allow <select> fields, because they're used e.g. on qvc.com.
118 if (ParseFieldSpecifics(scanner, pattern,
119 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT |
120 MATCH_SELECT,
121 &credit_card_field->expiration_date_)) {
122 credit_card_field->is_two_digit_year_ = true;
123 continue;
124 }
125
126 pattern = UTF8ToUTF16(autofill::kExpirationDateRe);
127 if (ParseFieldSpecifics(scanner, pattern,
128 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT |
129 MATCH_SELECT,
130 &credit_card_field->expiration_date_)) {
131 continue;
132 }
133 }
134
135 if (credit_card_field->expiration_month_ &&
136 !credit_card_field->expiration_year_ &&
137 !credit_card_field->expiration_date_) {
138 // Parsed a month but couldn't parse a year; give up.
139 scanner->RewindTo(saved_cursor);
140 return NULL;
141 }
142 }
143
144 // Some pages (e.g. ExpediaBilling.html) have a "card description"
145 // field; we parse this field but ignore it.
146 // We also ignore any other fields within a credit card block that
147 // start with "card", under the assumption that they are related to
148 // the credit card section being processed but are uninteresting to us.
149 if (ParseField(scanner, UTF8ToUTF16(autofill::kCardIgnoredRe), NULL))
150 continue;
151
152 break;
153 }
154
155 // Some pages have a billing address field after the cardholder name field.
156 // For that case, allow only just the cardholder name field. The remaining
157 // CC fields will be picked up in a following CreditCardField.
158 if (credit_card_field->cardholder_)
159 return credit_card_field.release();
160
161 // On some pages, the user selects a card type using radio buttons
162 // (e.g. test page Apple Store Billing.html). We can't handle that yet,
163 // so we treat the card type as optional for now.
164 // The existence of a number or cvc in combination with expiration date is
165 // a strong enough signal that this is a credit card. It is possible that
166 // the number and name were parsed in a separate part of the form. So if
167 // the cvc and date were found independently they are returned.
168 if ((credit_card_field->number_ || credit_card_field->verification_) &&
169 (credit_card_field->expiration_date_ ||
170 (credit_card_field->expiration_month_ &&
171 (credit_card_field->expiration_year_ ||
172 (LowerCaseEqualsASCII(
173 credit_card_field->expiration_month_->form_control_type,
174 "month")))))) {
175 return credit_card_field.release();
176 }
177
178 scanner->RewindTo(saved_cursor);
179 return NULL;
180 }
181
182 CreditCardField::CreditCardField()
183 : cardholder_(NULL),
184 cardholder_last_(NULL),
185 type_(NULL),
186 number_(NULL),
187 verification_(NULL),
188 expiration_month_(NULL),
189 expiration_year_(NULL),
190 expiration_date_(NULL),
191 is_two_digit_year_(false) {
192 }
193
194 bool CreditCardField::ClassifyField(FieldTypeMap* map) const {
195 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map);
196 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map);
197 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE,
198 map);
199
200 // If the heuristics detected first and last name in separate fields,
201 // then ignore both fields. Putting them into separate fields is probably
202 // wrong, because the credit card can also contain a middle name or middle
203 // initial.
204 if (cardholder_last_ == NULL)
205 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map);
206
207 if (expiration_date_) {
208 if (is_two_digit_year_) {
209 ok = ok && AddClassification(expiration_date_,
210 CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, map);
211 } else {
212 ok = ok && AddClassification(expiration_date_,
213 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, map);
214 }
215 } else {
216 ok = ok && AddClassification(expiration_month_, CREDIT_CARD_EXP_MONTH, map);
217 if (is_two_digit_year_) {
218 ok = ok && AddClassification(expiration_year_,
219 CREDIT_CARD_EXP_2_DIGIT_YEAR,
220 map);
221 } else {
222 ok = ok && AddClassification(expiration_year_,
223 CREDIT_CARD_EXP_4_DIGIT_YEAR,
224 map);
225 }
226 }
227
228 return ok;
229 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/credit_card_field.h ('k') | chrome/browser/autofill/credit_card_field_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698