OLD | NEW |
---|---|
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 // Credit card numbers are at most 19 digits in length. | |
25 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number] | |
26 static const size_t kMaxValidCardNumberSize = 19; | |
27 | |
22 // static | 28 // static |
23 FormField* CreditCardField::Parse(AutofillScanner* scanner) { | 29 FormField* CreditCardField::Parse(AutofillScanner* scanner) { |
24 if (scanner->IsEnd()) | 30 if (scanner->IsEnd()) |
25 return NULL; | 31 return NULL; |
26 | 32 |
27 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 33 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
28 size_t saved_cursor = scanner->SaveCursor(); | 34 size_t saved_cursor = scanner->SaveCursor(); |
29 | 35 |
30 // Credit card fields can appear in many different orders. | 36 // Credit card fields can appear in many different orders. |
31 // We loop until no more credit card related fields are found, see |break| at | 37 // We loop until no more credit card related fields are found, see |break| at |
(...skipping 19 matching lines...) Expand all Loading... | |
51 } | 57 } |
52 | 58 |
53 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) | 59 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) |
54 continue; | 60 continue; |
55 | 61 |
56 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html | 62 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html |
57 // and ExpediaBilling.html in our test suite), recognize separate fields | 63 // 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" | 64 // for the cardholder's first and last name if they have the labels "cfnm" |
59 // and "clnm". | 65 // and "clnm". |
60 scanner->SaveCursor(); | 66 scanner->SaveCursor(); |
61 const AutofillField* first; | 67 AutofillField* first; |
62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && | 68 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && |
63 ParseField(scanner, base::ASCIIToUTF16("^clnm"), | 69 ParseField(scanner, base::ASCIIToUTF16("^clnm"), |
64 &credit_card_field->cardholder_last_)) { | 70 &credit_card_field->cardholder_last_)) { |
65 credit_card_field->cardholder_ = first; | 71 credit_card_field->cardholder_ = first; |
66 continue; | 72 continue; |
67 } | 73 } |
68 scanner->Rewind(); | 74 scanner->Rewind(); |
69 } | 75 } |
70 | 76 |
71 // Check for a credit card type (Visa, MasterCard, etc.) field. | 77 // Check for a credit card type (Visa, MasterCard, etc.) field. |
(...skipping 10 matching lines...) Expand all Loading... | |
82 // has a plethora of names; we've seen "verification #", | 88 // has a plethora of names; we've seen "verification #", |
83 // "verification number", "card identification number" and others listed | 89 // "verification number", "card identification number" and others listed |
84 // in the |pattern| below. | 90 // in the |pattern| below. |
85 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); | 91 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); |
86 if (!credit_card_field->verification_ && | 92 if (!credit_card_field->verification_ && |
87 ParseField(scanner, pattern, &credit_card_field->verification_)) { | 93 ParseField(scanner, pattern, &credit_card_field->verification_)) { |
88 continue; | 94 continue; |
89 } | 95 } |
90 | 96 |
91 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); | 97 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); |
92 if (!credit_card_field->number_ && | 98 AutofillField* current_number_field; |
93 ParseField(scanner, pattern, &credit_card_field->number_)) { | 99 if (ParseField(scanner, pattern, ¤t_number_field)) { |
100 // Avoid autofilling any credit card number field having very low or high | |
101 // |start_index| on the HTML form. | |
102 size_t start_index = 0; | |
103 if (!credit_card_field->numbers_.empty()) { | |
104 size_t last_number_field_size = | |
105 credit_card_field->numbers_.back() | |
106 ->credit_card_number_start_index() + | |
107 credit_card_field->numbers_.back()->max_length; | |
Evan Stade
2014/08/28 18:18:42
nit: \n
Pritam Nikam
2014/09/01 09:03:35
Done.
| |
108 // In some cases, HTML form may have credit card number split across | |
109 // multiple input fields and either one or cumulatively having | |
110 // |max_length| more than |kMaxValidCardNumberSize|, mark these input | |
111 // form fields as invalid and skip autofilling them. | |
Evan Stade
2014/08/28 18:18:42
Not sure if this would happen in the wild, but it
Pritam Nikam
2014/09/01 09:03:35
This is already taken care with current logic.
Mod
| |
112 if (last_number_field_size == 0U || | |
113 last_number_field_size >= kMaxValidCardNumberSize) | |
Evan Stade
2014/08/28 18:18:42
nit: curlies
Pritam Nikam
2014/09/01 09:03:35
Done.
| |
114 // Mark that the credit card number splits are invalid. But keep | |
115 // scanning HTML form so that cursor moves beyond related fields. | |
116 credit_card_field->is_valid_card_number_split_ = false; | |
117 | |
118 start_index = last_number_field_size; | |
119 } | |
120 | |
121 current_number_field->set_credit_card_number_start_index(start_index); | |
122 credit_card_field->numbers_.push_back(current_number_field); | |
94 continue; | 123 continue; |
95 } | 124 } |
96 | 125 |
97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { | 126 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { |
98 credit_card_field->expiration_date_ = scanner->Cursor(); | 127 credit_card_field->expiration_date_ = scanner->Cursor(); |
99 scanner->Advance(); | 128 scanner->Advance(); |
100 } else { | 129 } else { |
101 // First try to parse split month/year expiration fields. | 130 // First try to parse split month/year expiration fields. |
102 scanner->SaveCursor(); | 131 scanner->SaveCursor(); |
103 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); | 132 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 if (credit_card_field->cardholder_) | 189 if (credit_card_field->cardholder_) |
161 return credit_card_field.release(); | 190 return credit_card_field.release(); |
162 | 191 |
163 // On some pages, the user selects a card type using radio buttons | 192 // 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, | 193 // (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. | 194 // so we treat the card type as optional for now. |
166 // The existence of a number or cvc in combination with expiration date is | 195 // 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 | 196 // 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 | 197 // 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. | 198 // the cvc and date were found independently they are returned. |
170 if ((credit_card_field->number_ || credit_card_field->verification_) && | 199 if ((!credit_card_field->numbers_.empty() || |
200 credit_card_field->verification_) && | |
171 (credit_card_field->expiration_date_ || | 201 (credit_card_field->expiration_date_ || |
172 (credit_card_field->expiration_month_ && | 202 (credit_card_field->expiration_month_ && |
173 credit_card_field->expiration_year_))) { | 203 credit_card_field->expiration_year_))) { |
174 return credit_card_field.release(); | 204 return credit_card_field.release(); |
175 } | 205 } |
176 | 206 |
177 scanner->RewindTo(saved_cursor); | 207 scanner->RewindTo(saved_cursor); |
178 return NULL; | 208 return NULL; |
179 } | 209 } |
180 | 210 |
181 CreditCardField::CreditCardField() | 211 CreditCardField::CreditCardField() |
182 : cardholder_(NULL), | 212 : cardholder_(NULL), |
183 cardholder_last_(NULL), | 213 cardholder_last_(NULL), |
184 type_(NULL), | 214 type_(NULL), |
185 number_(NULL), | |
186 verification_(NULL), | 215 verification_(NULL), |
187 expiration_month_(NULL), | 216 expiration_month_(NULL), |
188 expiration_year_(NULL), | 217 expiration_year_(NULL), |
189 expiration_date_(NULL), | 218 expiration_date_(NULL), |
190 is_two_digit_year_(false) { | 219 is_two_digit_year_(false), |
220 is_valid_card_number_split_(true) { | |
221 } | |
222 | |
223 CreditCardField::~CreditCardField() { | |
191 } | 224 } |
192 | 225 |
193 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { | 226 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { |
194 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); | 227 // Bail-out autofilling for invalid credit card number splits. |
228 if (!is_valid_card_number_split_) | |
229 return false; | |
230 | |
231 bool ok = true; | |
232 for (size_t index = 0; index < numbers_.size(); ++index) { | |
233 ok = ok && AddClassification(numbers_.at(index), CREDIT_CARD_NUMBER, map); | |
234 } | |
235 | |
195 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); | 236 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); |
196 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, | 237 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, |
197 map); | 238 map); |
198 | 239 |
199 // If the heuristics detected first and last name in separate fields, | 240 // If the heuristics detected first and last name in separate fields, |
200 // then ignore both fields. Putting them into separate fields is probably | 241 // 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 | 242 // wrong, because the credit card can also contain a middle name or middle |
202 // initial. | 243 // initial. |
203 if (cardholder_last_ == NULL) | 244 if (cardholder_last_ == NULL) |
204 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); | 245 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); |
(...skipping 16 matching lines...) Expand all Loading... | |
221 ok = ok && AddClassification(expiration_year_, | 262 ok = ok && AddClassification(expiration_year_, |
222 CREDIT_CARD_EXP_4_DIGIT_YEAR, | 263 CREDIT_CARD_EXP_4_DIGIT_YEAR, |
223 map); | 264 map); |
224 } | 265 } |
225 } | 266 } |
226 | 267 |
227 return ok; | 268 return ok; |
228 } | 269 } |
229 | 270 |
230 } // namespace autofill | 271 } // namespace autofill |
OLD | NEW |