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

Side by Side Diff: components/autofill/core/browser/credit_card_field.cc

Issue 1030073003: Autofill: Better recognize credit card type fields. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 8 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
OLDNEW
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/credit_card_field.h" 5 #include "components/autofill/core/browser/credit_card_field.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "components/autofill/core/browser/autofill_field.h" 16 #include "components/autofill/core/browser/autofill_field.h"
17 #include "components/autofill/core/browser/autofill_regex_constants.h" 17 #include "components/autofill/core/browser/autofill_regex_constants.h"
18 #include "components/autofill/core/browser/autofill_regexes.h" 18 #include "components/autofill/core/browser/autofill_regexes.h"
19 #include "components/autofill/core/browser/autofill_scanner.h" 19 #include "components/autofill/core/browser/autofill_scanner.h"
20 #include "components/autofill/core/browser/field_types.h" 20 #include "components/autofill/core/browser/field_types.h"
21 #include "grit/components_strings.h"
22 #include "ui/base/l10n/l10n_util.h"
21 23
22 namespace autofill { 24 namespace autofill {
23 25
24 namespace { 26 namespace {
25 27
26 // Credit card numbers are at most 19 digits in length. 28 // Credit card numbers are at most 19 digits in length.
27 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number] 29 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number]
28 const size_t kMaxValidCardNumberSize = 19; 30 const size_t kMaxValidCardNumberSize = 19;
29 31
30 // Look for the vector |regex_needles| in |haystack|. Returns true if a 32 // Look for the vector |regex_needles| in |haystack|. Returns true if a
(...skipping 22 matching lines...) Expand all
53 // static 55 // static
54 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { 56 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) {
55 if (scanner->IsEnd()) 57 if (scanner->IsEnd())
56 return nullptr; 58 return nullptr;
57 59
58 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); 60 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
59 size_t saved_cursor = scanner->SaveCursor(); 61 size_t saved_cursor = scanner->SaveCursor();
60 62
61 // Credit card fields can appear in many different orders. 63 // Credit card fields can appear in many different orders.
62 // We loop until no more credit card related fields are found, see |break| at 64 // We loop until no more credit card related fields are found, see |break| at
63 // bottom of the loop. 65 // the bottom of the loop.
64 for (int fields = 0; !scanner->IsEnd(); ++fields) { 66 for (int fields = 0; !scanner->IsEnd(); ++fields) {
65 // Ignore gift card fields. 67 // Ignore gift card fields.
66 if (ParseField(scanner, base::UTF8ToUTF16(kGiftCardRe), nullptr)) 68 if (ParseField(scanner, base::UTF8ToUTF16(kGiftCardRe), nullptr))
67 break; 69 break;
68 70
69 if (!credit_card_field->cardholder_) { 71 if (!credit_card_field->cardholder_) {
70 if (ParseField(scanner, 72 if (ParseField(scanner,
71 base::UTF8ToUTF16(kNameOnCardRe), 73 base::UTF8ToUTF16(kNameOnCardRe),
72 &credit_card_field->cardholder_)) { 74 &credit_card_field->cardholder_)) {
73 continue; 75 continue;
74 } 76 }
75 77
76 // Sometimes the cardholder field is just labeled "name". Unfortunately 78 // Sometimes the cardholder field is just labeled "name". Unfortunately
77 // this is a dangerously generic word to search for, since it will often 79 // this is a dangerously generic word to search for, since it will often
78 // match a name (not cardholder name) field before or after credit card 80 // match a name (not cardholder name) field before or after credit card
79 // fields. So we search for "name" only when we've already parsed at 81 // fields. So we search for "name" only when we've already parsed at
80 // least one other credit card field and haven't yet parsed the 82 // least one other credit card field and haven't yet parsed the
81 // expiration date (which usually appears at the end). 83 // expiration date (which usually appears at the end).
82 if (fields > 0 && 84 if (fields > 0 &&
83 !credit_card_field->expiration_month_ && 85 !credit_card_field->expiration_month_ &&
84 ParseField(scanner, 86 ParseField(scanner,
85 base::UTF8ToUTF16(kNameOnCardContextualRe), 87 base::UTF8ToUTF16(kNameOnCardContextualRe),
86 &credit_card_field->cardholder_)) { 88 &credit_card_field->cardholder_)) {
87 continue; 89 continue;
88 } 90 }
89 } 91 }
90 92
91 // Check for a credit card type (Visa, MasterCard, etc.) field. 93 // Check for a credit card type (Visa, MasterCard, etc.) field.
92 if (!credit_card_field->type_ && 94 // All CC type fields encountered so far have been of type select.
93 ParseFieldSpecifics(scanner, 95 if (!credit_card_field->type_ && LikelyCardTypeSelectField(scanner)) {
94 base::UTF8ToUTF16(kCardTypeRe), 96 credit_card_field->type_ = scanner->Cursor();
95 MATCH_DEFAULT | MATCH_SELECT, 97 scanner->Advance();
96 &credit_card_field->type_)) {
97 continue; 98 continue;
98 } 99 }
99 100
100 // We look for a card security code before we look for a credit card number 101 // We look for a card security code before we look for a credit card number
101 // and match the general term "number". The security code has a plethora of 102 // and match the general term "number". The security code has a plethora of
102 // names; we've seen "verification #", "verification number", "card 103 // names; we've seen "verification #", "verification number", "card
103 // identification number", and others listed in the regex pattern used 104 // identification number", and others listed in the regex pattern used
104 // below. 105 // below.
105 // Note: Some sites use type="tel" or type="number" for numerical inputs. 106 // Note: Some sites use type="tel" or type="number" for numerical inputs.
106 const int kMatchNumAndTel = MATCH_DEFAULT | MATCH_NUMBER | MATCH_TELEPHONE; 107 const int kMatchNumAndTel = MATCH_DEFAULT | MATCH_NUMBER | MATCH_TELEPHONE;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 continue; 144 continue;
144 145
145 if (credit_card_field->expiration_month_ && 146 if (credit_card_field->expiration_month_ &&
146 !credit_card_field->expiration_year_ && 147 !credit_card_field->expiration_year_ &&
147 !credit_card_field->expiration_date_) { 148 !credit_card_field->expiration_date_) {
148 // Parsed a month but couldn't parse a year; give up. 149 // Parsed a month but couldn't parse a year; give up.
149 scanner->RewindTo(saved_cursor); 150 scanner->RewindTo(saved_cursor);
150 return nullptr; 151 return nullptr;
151 } 152 }
152 153
153 // Some pages (e.g. ExpediaBilling.html) have a "card description"
154 // field; we parse this field but ignore it.
155 // We also ignore any other fields within a credit card block that
156 // start with "card", under the assumption that they are related to
157 // the credit card section being processed but are uninteresting to us.
158 if (ParseField(scanner, base::UTF8ToUTF16(kCardIgnoredRe), nullptr))
159 continue;
160
161 break; 154 break;
162 } 155 }
163 156
164 // Some pages have a billing address field after the cardholder name field. 157 // Some pages have a billing address field after the cardholder name field.
165 // For that case, allow only just the cardholder name field. The remaining 158 // For that case, allow only just the cardholder name field. The remaining
166 // CC fields will be picked up in a following CreditCardField. 159 // CC fields will be picked up in a following CreditCardField.
167 if (credit_card_field->cardholder_) 160 if (credit_card_field->cardholder_)
168 return credit_card_field.Pass(); 161 return credit_card_field.Pass();
169 162
170 // On some pages, the user selects a card type using radio buttons 163 // On some pages, the user selects a card type using radio buttons
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 std::vector<base::string16> years_to_check; 233 std::vector<base::string16> years_to_check;
241 for (int year = time_exploded.year; 234 for (int year = time_exploded.year;
242 year < time_exploded.year + kYearsToMatch; 235 year < time_exploded.year + kYearsToMatch;
243 ++year) { 236 ++year) {
244 years_to_check.push_back(base::IntToString16(year)); 237 years_to_check.push_back(base::IntToString16(year));
245 } 238 }
246 return (FindConsecutiveStrings(years_to_check, field->option_values) || 239 return (FindConsecutiveStrings(years_to_check, field->option_values) ||
247 FindConsecutiveStrings(years_to_check, field->option_contents)); 240 FindConsecutiveStrings(years_to_check, field->option_contents));
248 } 241 }
249 242
243 // static
244 bool CreditCardField::LikelyCardTypeSelectField(AutofillScanner* scanner) {
245 if (scanner->IsEnd())
246 return false;
247
248 AutofillField* field = scanner->Cursor();
249 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT))
250 return false;
251
252 return AutofillField::FindValueInSelectControl(
253 *field, l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA),
254 nullptr) ||
255 AutofillField::FindValueInSelectControl(
256 *field, l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD),
257 nullptr);
258 }
259
250 CreditCardField::CreditCardField() 260 CreditCardField::CreditCardField()
251 : cardholder_(nullptr), 261 : cardholder_(nullptr),
252 cardholder_last_(nullptr), 262 cardholder_last_(nullptr),
253 type_(nullptr), 263 type_(nullptr),
254 verification_(nullptr), 264 verification_(nullptr),
255 expiration_month_(nullptr), 265 expiration_month_(nullptr),
256 expiration_year_(nullptr), 266 expiration_year_(nullptr),
257 expiration_date_(nullptr), 267 expiration_date_(nullptr),
258 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { 268 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
259 } 269 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 384
375 ServerFieldType CreditCardField::GetExpirationYearType() const { 385 ServerFieldType CreditCardField::GetExpirationYearType() const {
376 return (expiration_date_ 386 return (expiration_date_
377 ? exp_year_type_ 387 ? exp_year_type_
378 : ((expiration_year_ && expiration_year_->max_length == 2) 388 : ((expiration_year_ && expiration_year_->max_length == 2)
379 ? CREDIT_CARD_EXP_2_DIGIT_YEAR 389 ? CREDIT_CARD_EXP_2_DIGIT_YEAR
380 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); 390 : CREDIT_CARD_EXP_4_DIGIT_YEAR));
381 } 391 }
382 392
383 } // namespace autofill 393 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/credit_card_field.h ('k') | components/autofill/core/browser/credit_card_field_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698