Chromium Code Reviews| 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 "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(); |
| 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 19 matching lines...) Expand all Loading... | |
| 51 } | 55 } |
| 52 | 56 |
| 53 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) | 57 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) |
| 54 continue; | 58 continue; |
| 55 | 59 |
| 56 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html | 60 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html |
| 57 // and ExpediaBilling.html in our test suite), recognize separate fields | 61 // 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" | 62 // for the cardholder's first and last name if they have the labels "cfnm" |
| 59 // and "clnm". | 63 // and "clnm". |
| 60 scanner->SaveCursor(); | 64 scanner->SaveCursor(); |
| 61 const AutofillField* first; | 65 AutofillField* first; |
| 62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && | 66 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && |
| 63 ParseField(scanner, | 67 ParseField(scanner, |
| 64 base::ASCIIToUTF16("^clnm"), | 68 base::ASCIIToUTF16("^clnm"), |
| 65 &credit_card_field->cardholder_last_)) { | 69 &credit_card_field->cardholder_last_)) { |
| 66 credit_card_field->cardholder_ = first; | 70 credit_card_field->cardholder_ = first; |
| 67 continue; | 71 continue; |
| 68 } | 72 } |
| 69 scanner->Rewind(); | 73 scanner->Rewind(); |
| 70 } | 74 } |
| 71 | 75 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 84 // has a plethora of names; we've seen "verification #", | 88 // has a plethora of names; we've seen "verification #", |
| 85 // "verification number", "card identification number" and others listed | 89 // "verification number", "card identification number" and others listed |
| 86 // in the |pattern| below. | 90 // in the |pattern| below. |
| 87 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); | 91 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); |
| 88 if (!credit_card_field->verification_ && | 92 if (!credit_card_field->verification_ && |
| 89 ParseField(scanner, pattern, &credit_card_field->verification_)) { | 93 ParseField(scanner, pattern, &credit_card_field->verification_)) { |
| 90 continue; | 94 continue; |
| 91 } | 95 } |
| 92 | 96 |
| 93 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); | 97 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); |
| 94 if (!credit_card_field->number_ && | 98 AutofillField* current_number_field; |
| 95 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()->credit_card_number_offset() + | |
| 106 credit_card_field->numbers_.back()->max_length; | |
| 107 | |
| 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. | |
| 112 if (last_number_field_size == 0U || | |
| 113 last_number_field_size >= kMaxValidCardNumberSize) { | |
| 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; | |
|
Ilya Sherman
2014/09/16 02:35:27
I'm still not convinced that swallowing all of the
Pritam Nikam
2014/09/16 10:49:36
Done.
Going ahead with option (2) as you suggeste
| |
| 117 } | |
| 118 | |
| 119 start_index = last_number_field_size; | |
| 120 } | |
| 121 | |
| 122 current_number_field->set_credit_card_number_offset(start_index); | |
| 123 credit_card_field->numbers_.push_back(current_number_field); | |
| 96 continue; | 124 continue; |
| 97 } | 125 } |
| 98 | 126 |
| 99 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { | 127 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { |
| 100 credit_card_field->expiration_date_ = scanner->Cursor(); | 128 credit_card_field->expiration_date_ = scanner->Cursor(); |
| 101 scanner->Advance(); | 129 scanner->Advance(); |
| 102 } else { | 130 } else { |
| 103 // First try to parse split month/year expiration fields. | 131 // First try to parse split month/year expiration fields. |
| 104 scanner->SaveCursor(); | 132 scanner->SaveCursor(); |
| 105 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); | 133 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 if (credit_card_field->cardholder_) | 196 if (credit_card_field->cardholder_) |
| 169 return credit_card_field.release(); | 197 return credit_card_field.release(); |
| 170 | 198 |
| 171 // On some pages, the user selects a card type using radio buttons | 199 // 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, | 200 // (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. | 201 // so we treat the card type as optional for now. |
| 174 // The existence of a number or cvc in combination with expiration date is | 202 // 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 | 203 // 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 | 204 // 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. | 205 // the cvc and date were found independently they are returned. |
| 178 if ((credit_card_field->number_ || credit_card_field->verification_) && | 206 if ((!credit_card_field->numbers_.empty() || |
| 207 credit_card_field->verification_) && | |
| 179 (credit_card_field->expiration_date_ || | 208 (credit_card_field->expiration_date_ || |
| 180 (credit_card_field->expiration_month_ && | 209 (credit_card_field->expiration_month_ && |
| 181 credit_card_field->expiration_year_))) { | 210 credit_card_field->expiration_year_))) { |
| 182 return credit_card_field.release(); | 211 return credit_card_field.release(); |
| 183 } | 212 } |
| 184 | 213 |
| 185 scanner->RewindTo(saved_cursor); | 214 scanner->RewindTo(saved_cursor); |
| 186 return NULL; | 215 return NULL; |
| 187 } | 216 } |
| 188 | 217 |
| 189 CreditCardField::CreditCardField() | 218 CreditCardField::CreditCardField() |
| 190 : cardholder_(NULL), | 219 : cardholder_(NULL), |
| 191 cardholder_last_(NULL), | 220 cardholder_last_(NULL), |
| 192 type_(NULL), | 221 type_(NULL), |
| 193 number_(NULL), | |
| 194 verification_(NULL), | 222 verification_(NULL), |
| 195 expiration_month_(NULL), | 223 expiration_month_(NULL), |
| 196 expiration_year_(NULL), | 224 expiration_year_(NULL), |
| 197 expiration_date_(NULL), | 225 expiration_date_(NULL), |
| 198 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { | 226 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR), |
| 227 is_valid_card_number_split_(true) { | |
| 228 } | |
| 229 | |
| 230 CreditCardField::~CreditCardField() { | |
| 199 } | 231 } |
| 200 | 232 |
| 201 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { | 233 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { |
| 202 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); | 234 // Bail-out autofilling for invalid credit card number splits. |
| 235 if (!is_valid_card_number_split_) | |
| 236 return true; | |
| 237 | |
| 238 bool ok = true; | |
| 239 for (size_t index = 0; index < numbers_.size(); ++index) { | |
| 240 ok = ok && AddClassification(numbers_.at(index), CREDIT_CARD_NUMBER, map); | |
|
Ilya Sherman
2014/09/16 02:35:27
nit: Please use "numbers_[index]" rather than "num
Pritam Nikam
2014/09/16 10:49:36
Done.
| |
| 241 } | |
| 242 | |
| 203 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); | 243 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); |
| 204 ok = ok && | 244 ok = ok && |
| 205 AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, map); | 245 AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, map); |
| 206 | 246 |
| 207 // If the heuristics detected first and last name in separate fields, | 247 // If the heuristics detected first and last name in separate fields, |
| 208 // then ignore both fields. Putting them into separate fields is probably | 248 // 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 | 249 // wrong, because the credit card can also contain a middle name or middle |
| 210 // initial. | 250 // initial. |
| 211 if (cardholder_last_ == NULL) | 251 if (cardholder_last_ == NULL) |
| 212 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); | 252 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 225 | 265 |
| 226 ServerFieldType CreditCardField::GetExpirationYearType() const { | 266 ServerFieldType CreditCardField::GetExpirationYearType() const { |
| 227 return (expiration_date_ | 267 return (expiration_date_ |
| 228 ? exp_year_type_ | 268 ? exp_year_type_ |
| 229 : ((expiration_year_ && expiration_year_->max_length == 2) | 269 : ((expiration_year_ && expiration_year_->max_length == 2) |
| 230 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 270 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
| 231 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 271 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 232 } | 272 } |
| 233 | 273 |
| 234 } // namespace autofill | 274 } // namespace autofill |
| OLD | NEW |