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

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 comments. Created 6 years, 4 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 <algorithm>
10
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
14 #include "components/autofill/core/browser/autofill_field.h" 16 #include "components/autofill/core/browser/autofill_field.h"
15 #include "components/autofill/core/browser/autofill_regex_constants.h" 17 #include "components/autofill/core/browser/autofill_regex_constants.h"
16 #include "components/autofill/core/browser/autofill_scanner.h" 18 #include "components/autofill/core/browser/autofill_scanner.h"
17 #include "components/autofill/core/browser/field_types.h" 19 #include "components/autofill/core/browser/field_types.h"
18 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
19 21
20 namespace autofill { 22 namespace autofill {
21 23
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();
29 33
30 // Credit card fields can appear in many different orders. 34 // Credit card fields can appear in many different orders.
31 // We loop until no more credit card related fields are found, see |break| at 35 // We loop until no more credit card related fields are found, see |break| at
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 // has a plethora of names; we've seen "verification #", 86 // has a plethora of names; we've seen "verification #",
83 // "verification number", "card identification number" and others listed 87 // "verification number", "card identification number" and others listed
84 // in the |pattern| below. 88 // in the |pattern| below.
85 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); 89 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe);
86 if (!credit_card_field->verification_ && 90 if (!credit_card_field->verification_ &&
87 ParseField(scanner, pattern, &credit_card_field->verification_)) { 91 ParseField(scanner, pattern, &credit_card_field->verification_)) {
88 continue; 92 continue;
89 } 93 }
90 94
91 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); 95 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe);
92 if (!credit_card_field->number_ && 96 const AutofillField* cc_number_field;
93 ParseField(scanner, pattern, &credit_card_field->number_)) { 97 if (ParseField(scanner, pattern, &cc_number_field)) {
98 // Avoid autofilling any credit card number field having very low or high
99 // |start_index| on the HTML form.
100 size_t start_index = 0;
101 if (!credit_card_field->numbers_.empty()) {
102 size_t last_number_field_size =
103 credit_card_field->numbers_.back()
104 ->credit_card_number_start_index() +
Evan Stade 2014/08/12 16:05:07 style-wise, a lot of this seems off to me. Could y
Pritam Nikam 2014/08/12 18:39:11 It's after git cl format only :)
105 credit_card_field->numbers_.back()->max_length;
106 if (!last_number_field_size ||
Evan Stade 2014/08/12 16:05:07 please don't implicitly convert size_t to boolean.
Pritam Nikam 2014/08/12 18:39:11 Done.
107 last_number_field_size >= kMaxValidCardNumberSize)
108 return NULL;
Evan Stade 2014/08/12 16:05:07 \n
Pritam Nikam 2014/08/12 18:39:11 Done.
109 start_index = last_number_field_size;
110 }
111
112 AutofillField* current_number_field =
113 const_cast<AutofillField*>(cc_number_field);
Evan Stade 2014/08/12 16:05:07 you still have this const_cast... I agree with Ily
Pritam Nikam 2014/08/12 18:39:11 [Same as my previous comments] I didn't get any al
Evan Stade 2014/08/12 19:19:55 What is the cursor module?
114 current_number_field->set_credit_card_number_start_index(start_index);
115 credit_card_field->numbers_.push_back(current_number_field);
94 continue; 116 continue;
95 } 117 }
96 118
97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { 119 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) {
98 credit_card_field->expiration_date_ = scanner->Cursor(); 120 credit_card_field->expiration_date_ = scanner->Cursor();
99 scanner->Advance(); 121 scanner->Advance();
100 } else { 122 } else {
101 // First try to parse split month/year expiration fields. 123 // First try to parse split month/year expiration fields.
102 scanner->SaveCursor(); 124 scanner->SaveCursor();
103 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); 125 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 if (credit_card_field->cardholder_) 182 if (credit_card_field->cardholder_)
161 return credit_card_field.release(); 183 return credit_card_field.release();
162 184
163 // On some pages, the user selects a card type using radio buttons 185 // On some pages, the user selects a card type using radio buttons
164 // (e.g. test page Apple Store Billing.html). We can't handle that yet, 186 // (e.g. test page Apple Store Billing.html). We can't handle that yet,
165 // so we treat the card type as optional for now. 187 // so we treat the card type as optional for now.
166 // The existence of a number or cvc in combination with expiration date is 188 // The existence of a number or cvc in combination with expiration date is
167 // a strong enough signal that this is a credit card. It is possible that 189 // a strong enough signal that this is a credit card. It is possible that
168 // the number and name were parsed in a separate part of the form. So if 190 // the number and name were parsed in a separate part of the form. So if
169 // the cvc and date were found independently they are returned. 191 // the cvc and date were found independently they are returned.
170 if ((credit_card_field->number_ || credit_card_field->verification_) && 192 if ((!credit_card_field->numbers_.empty() ||
193 credit_card_field->verification_) &&
171 (credit_card_field->expiration_date_ || 194 (credit_card_field->expiration_date_ ||
172 (credit_card_field->expiration_month_ && 195 (credit_card_field->expiration_month_ &&
173 credit_card_field->expiration_year_))) { 196 credit_card_field->expiration_year_))) {
174 return credit_card_field.release(); 197 return credit_card_field.release();
175 } 198 }
176 199
177 scanner->RewindTo(saved_cursor); 200 scanner->RewindTo(saved_cursor);
178 return NULL; 201 return NULL;
179 } 202 }
180 203
181 CreditCardField::CreditCardField() 204 CreditCardField::CreditCardField()
182 : cardholder_(NULL), 205 : cardholder_(NULL),
183 cardholder_last_(NULL), 206 cardholder_last_(NULL),
184 type_(NULL), 207 type_(NULL),
185 number_(NULL),
186 verification_(NULL), 208 verification_(NULL),
187 expiration_month_(NULL), 209 expiration_month_(NULL),
188 expiration_year_(NULL), 210 expiration_year_(NULL),
189 expiration_date_(NULL), 211 expiration_date_(NULL),
190 is_two_digit_year_(false) { 212 is_two_digit_year_(false) {
191 } 213 }
192 214
215 CreditCardField::~CreditCardField() {
216 }
217
193 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { 218 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const {
194 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); 219 bool ok = true;
220 for (std::vector<const AutofillField*>::const_iterator it = numbers_.begin();
221 it != numbers_.end();
222 ++it) {
223 ok = ok && AddClassification(*it, CREDIT_CARD_NUMBER, map);
224 }
225
195 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); 226 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map);
196 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, 227 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE,
197 map); 228 map);
198 229
199 // If the heuristics detected first and last name in separate fields, 230 // If the heuristics detected first and last name in separate fields,
200 // then ignore both fields. Putting them into separate fields is probably 231 // then ignore both fields. Putting them into separate fields is probably
201 // wrong, because the credit card can also contain a middle name or middle 232 // wrong, because the credit card can also contain a middle name or middle
202 // initial. 233 // initial.
203 if (cardholder_last_ == NULL) 234 if (cardholder_last_ == NULL)
204 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); 235 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map);
(...skipping 16 matching lines...) Expand all
221 ok = ok && AddClassification(expiration_year_, 252 ok = ok && AddClassification(expiration_year_,
222 CREDIT_CARD_EXP_4_DIGIT_YEAR, 253 CREDIT_CARD_EXP_4_DIGIT_YEAR,
223 map); 254 map);
224 } 255 }
225 } 256 }
226 257
227 return ok; 258 return ok;
228 } 259 }
229 260
230 } // namespace autofill 261 } // 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