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

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

Issue 381613005: [Autofill] Autofill fails to fill credit card number when split across fields. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporated review inputs. Created 6 years, 3 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/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "components/autofill/core/browser/autofill_field.h" 14 #include "components/autofill/core/browser/autofill_field.h"
15 #include "components/autofill/core/browser/autofill_regex_constants.h" 15 #include "components/autofill/core/browser/autofill_regex_constants.h"
16 #include "components/autofill/core/browser/autofill_scanner.h" 16 #include "components/autofill/core/browser/autofill_scanner.h"
17 #include "components/autofill/core/browser/field_types.h" 17 #include "components/autofill/core/browser/field_types.h"
18 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/base/l10n/l10n_util.h"
19 19
20 namespace autofill { 20 namespace autofill {
21 21
22 // Credit card numbers are at most 19 digits in length.
23 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number]
24 static const size_t kMaxValidCardNumberSize = 19;
25
22 // static 26 // static
23 FormField* CreditCardField::Parse(AutofillScanner* scanner) { 27 FormField* CreditCardField::Parse(AutofillScanner* scanner) {
24 if (scanner->IsEnd()) 28 if (scanner->IsEnd())
25 return NULL; 29 return NULL;
26 30
27 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); 31 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
28 size_t saved_cursor = scanner->SaveCursor(); 32 size_t saved_cursor = scanner->SaveCursor();
33 bool form_has_valid_card_number_fields = true;
29 34
30 // Credit card fields can appear in many different orders. 35 // Credit card fields can appear in many different orders.
31 // We loop until no more credit card related fields are found, see |break| at 36 // We loop until no more credit card related fields are found, see |break| at
32 // bottom of the loop. 37 // bottom of the loop.
33 for (int fields = 0; !scanner->IsEnd(); ++fields) { 38 for (int fields = 0; !scanner->IsEnd(); ++fields) {
34 // Ignore gift card fields. 39 // Ignore gift card fields.
35 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kGiftCardRe), NULL)) 40 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kGiftCardRe), NULL))
36 break; 41 break;
37 42
38 // Sometimes the cardholder field is just labeled "name". Unfortunately this 43 // Sometimes the cardholder field is just labeled "name". Unfortunately this
(...skipping 12 matching lines...) Expand all
51 } 56 }
52 57
53 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) 58 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_))
54 continue; 59 continue;
55 60
56 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html 61 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html
57 // and ExpediaBilling.html in our test suite), recognize separate fields 62 // and ExpediaBilling.html in our test suite), recognize separate fields
58 // for the cardholder's first and last name if they have the labels "cfnm" 63 // for the cardholder's first and last name if they have the labels "cfnm"
59 // and "clnm". 64 // and "clnm".
60 scanner->SaveCursor(); 65 scanner->SaveCursor();
61 const AutofillField* first; 66 AutofillField* first;
62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && 67 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) &&
63 ParseField(scanner, 68 ParseField(scanner,
64 base::ASCIIToUTF16("^clnm"), 69 base::ASCIIToUTF16("^clnm"),
65 &credit_card_field->cardholder_last_)) { 70 &credit_card_field->cardholder_last_)) {
66 credit_card_field->cardholder_ = first; 71 credit_card_field->cardholder_ = first;
67 continue; 72 continue;
68 } 73 }
69 scanner->Rewind(); 74 scanner->Rewind();
70 } 75 }
71 76
(...skipping 12 matching lines...) Expand all
84 // has a plethora of names; we've seen "verification #", 89 // has a plethora of names; we've seen "verification #",
85 // "verification number", "card identification number" and others listed 90 // "verification number", "card identification number" and others listed
86 // in the |pattern| below. 91 // in the |pattern| below.
87 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); 92 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe);
88 if (!credit_card_field->verification_ && 93 if (!credit_card_field->verification_ &&
89 ParseField(scanner, pattern, &credit_card_field->verification_)) { 94 ParseField(scanner, pattern, &credit_card_field->verification_)) {
90 continue; 95 continue;
91 } 96 }
92 97
93 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); 98 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe);
94 if (!credit_card_field->number_ && 99 AutofillField* current_number_field;
95 ParseField(scanner, pattern, &credit_card_field->number_)) { 100 if (ParseField(scanner, pattern, &current_number_field)) {
101 // Avoid autofilling any credit card number field having very low or high
102 // |start_index| on the HTML form.
103 size_t start_index = 0;
104 if (!credit_card_field->numbers_.empty()) {
105 size_t last_number_field_size =
106 credit_card_field->numbers_.back()->credit_card_number_offset() +
107 credit_card_field->numbers_.back()->max_length;
108
109 // In some cases, HTML form may have credit card number split across
110 // multiple input fields and either one or cumulatively having
111 // |max_length| more than |kMaxValidCardNumberSize|, mark these input
112 // form fields as invalid and skip autofilling them.
113 if (last_number_field_size == 0U ||
114 last_number_field_size >= kMaxValidCardNumberSize) {
115 // Mark that the credit card number splits are invalid. But keep
116 // scanning HTML form so that cursor moves beyond related fields.
117 form_has_valid_card_number_fields = false;
118 }
119
120 start_index = last_number_field_size;
121 }
122
123 current_number_field->set_credit_card_number_offset(start_index);
124 credit_card_field->numbers_.push_back(current_number_field);
96 continue; 125 continue;
97 } 126 }
98 127
99 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { 128 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) {
100 credit_card_field->expiration_date_ = scanner->Cursor(); 129 credit_card_field->expiration_date_ = scanner->Cursor();
101 scanner->Advance(); 130 scanner->Advance();
102 } else { 131 } else {
103 // First try to parse split month/year expiration fields. 132 // First try to parse split month/year expiration fields.
104 scanner->SaveCursor(); 133 scanner->SaveCursor();
105 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); 134 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // field; we parse this field but ignore it. 184 // field; we parse this field but ignore it.
156 // We also ignore any other fields within a credit card block that 185 // We also ignore any other fields within a credit card block that
157 // start with "card", under the assumption that they are related to 186 // start with "card", under the assumption that they are related to
158 // the credit card section being processed but are uninteresting to us. 187 // the credit card section being processed but are uninteresting to us.
159 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kCardIgnoredRe), NULL)) 188 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kCardIgnoredRe), NULL))
160 continue; 189 continue;
161 190
162 break; 191 break;
163 } 192 }
164 193
194 // Cases where heuristic misinterprets input field as credit card number
195 // field, refuse to autofill credit card number fields.
196 if (!form_has_valid_card_number_fields)
197 credit_card_field->numbers_.clear();
198
165 // Some pages have a billing address field after the cardholder name field. 199 // Some pages have a billing address field after the cardholder name field.
166 // For that case, allow only just the cardholder name field. The remaining 200 // For that case, allow only just the cardholder name field. The remaining
167 // CC fields will be picked up in a following CreditCardField. 201 // CC fields will be picked up in a following CreditCardField.
168 if (credit_card_field->cardholder_) 202 if (credit_card_field->cardholder_)
169 return credit_card_field.release(); 203 return credit_card_field.release();
170 204
171 // On some pages, the user selects a card type using radio buttons 205 // On some pages, the user selects a card type using radio buttons
172 // (e.g. test page Apple Store Billing.html). We can't handle that yet, 206 // (e.g. test page Apple Store Billing.html). We can't handle that yet,
173 // so we treat the card type as optional for now. 207 // so we treat the card type as optional for now.
174 // The existence of a number or cvc in combination with expiration date is 208 // The existence of a number or cvc in combination with expiration date is
175 // a strong enough signal that this is a credit card. It is possible that 209 // a strong enough signal that this is a credit card. It is possible that
176 // the number and name were parsed in a separate part of the form. So if 210 // the number and name were parsed in a separate part of the form. So if
177 // the cvc and date were found independently they are returned. 211 // the cvc and date were found independently they are returned.
178 if ((credit_card_field->number_ || credit_card_field->verification_) && 212 if ((!credit_card_field->numbers_.empty() ||
213 credit_card_field->verification_ ||
214 !form_has_valid_card_number_fields) &&
179 (credit_card_field->expiration_date_ || 215 (credit_card_field->expiration_date_ ||
180 (credit_card_field->expiration_month_ && 216 (credit_card_field->expiration_month_ &&
181 credit_card_field->expiration_year_))) { 217 credit_card_field->expiration_year_))) {
182 return credit_card_field.release(); 218 return credit_card_field.release();
183 } 219 }
184 220
185 scanner->RewindTo(saved_cursor); 221 scanner->RewindTo(saved_cursor);
186 return NULL; 222 return NULL;
187 } 223 }
188 224
189 CreditCardField::CreditCardField() 225 CreditCardField::CreditCardField()
190 : cardholder_(NULL), 226 : cardholder_(NULL),
191 cardholder_last_(NULL), 227 cardholder_last_(NULL),
192 type_(NULL), 228 type_(NULL),
193 number_(NULL),
194 verification_(NULL), 229 verification_(NULL),
195 expiration_month_(NULL), 230 expiration_month_(NULL),
196 expiration_year_(NULL), 231 expiration_year_(NULL),
197 expiration_date_(NULL), 232 expiration_date_(NULL),
198 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { 233 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
199 } 234 }
200 235
236 CreditCardField::~CreditCardField() {
237 }
238
201 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { 239 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const {
202 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); 240 bool ok = true;
241 for (size_t index = 0; index < numbers_.size(); ++index) {
242 ok = ok && AddClassification(numbers_[index], CREDIT_CARD_NUMBER, map);
243 }
244
203 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); 245 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map);
204 ok = ok && 246 ok = ok &&
205 AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, map); 247 AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, map);
206 248
207 // If the heuristics detected first and last name in separate fields, 249 // If the heuristics detected first and last name in separate fields,
208 // then ignore both fields. Putting them into separate fields is probably 250 // then ignore both fields. Putting them into separate fields is probably
209 // wrong, because the credit card can also contain a middle name or middle 251 // wrong, because the credit card can also contain a middle name or middle
210 // initial. 252 // initial.
211 if (cardholder_last_ == NULL) 253 if (cardholder_last_ == NULL)
212 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); 254 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map);
(...skipping 12 matching lines...) Expand all
225 267
226 ServerFieldType CreditCardField::GetExpirationYearType() const { 268 ServerFieldType CreditCardField::GetExpirationYearType() const {
227 return (expiration_date_ 269 return (expiration_date_
228 ? exp_year_type_ 270 ? exp_year_type_
229 : ((expiration_year_ && expiration_year_->max_length == 2) 271 : ((expiration_year_ && expiration_year_->max_length == 2)
230 ? CREDIT_CARD_EXP_2_DIGIT_YEAR 272 ? CREDIT_CARD_EXP_2_DIGIT_YEAR
231 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); 273 : CREDIT_CARD_EXP_4_DIGIT_YEAR));
232 } 274 }
233 275
234 } // namespace autofill 276 } // 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