| 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" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) | 53 if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_)) |
| 54 continue; | 54 continue; |
| 55 | 55 |
| 56 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html | 56 // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html |
| 57 // and ExpediaBilling.html in our test suite), recognize separate fields | 57 // 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" | 58 // for the cardholder's first and last name if they have the labels "cfnm" |
| 59 // and "clnm". | 59 // and "clnm". |
| 60 scanner->SaveCursor(); | 60 scanner->SaveCursor(); |
| 61 const AutofillField* first; | 61 const AutofillField* first; |
| 62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && | 62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && |
| 63 ParseField(scanner, base::ASCIIToUTF16("^clnm"), | 63 ParseField(scanner, |
| 64 base::ASCIIToUTF16("^clnm"), |
| 64 &credit_card_field->cardholder_last_)) { | 65 &credit_card_field->cardholder_last_)) { |
| 65 credit_card_field->cardholder_ = first; | 66 credit_card_field->cardholder_ = first; |
| 66 continue; | 67 continue; |
| 67 } | 68 } |
| 68 scanner->Rewind(); | 69 scanner->Rewind(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 // Check for a credit card type (Visa, MasterCard, etc.) field. | 72 // Check for a credit card type (Visa, MasterCard, etc.) field. |
| 72 base::string16 type_pattern = base::UTF8ToUTF16(autofill::kCardTypeRe); | 73 base::string16 type_pattern = base::UTF8ToUTF16(autofill::kCardTypeRe); |
| 73 if (!credit_card_field->type_ && | 74 if (!credit_card_field->type_ && |
| 74 ParseFieldSpecifics(scanner, type_pattern, | 75 ParseFieldSpecifics(scanner, |
| 76 type_pattern, |
| 75 MATCH_DEFAULT | MATCH_SELECT, | 77 MATCH_DEFAULT | MATCH_SELECT, |
| 76 &credit_card_field->type_)) { | 78 &credit_card_field->type_)) { |
| 77 continue; | 79 continue; |
| 78 } | 80 } |
| 79 | 81 |
| 80 // We look for a card security code before we look for a credit | 82 // We look for a card security code before we look for a credit |
| 81 // card number and match the general term "number". The security code | 83 // card number and match the general term "number". The security code |
| 82 // has a plethora of names; we've seen "verification #", | 84 // has a plethora of names; we've seen "verification #", |
| 83 // "verification number", "card identification number" and others listed | 85 // "verification number", "card identification number" and others listed |
| 84 // in the |pattern| below. | 86 // in the |pattern| below. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 95 } | 97 } |
| 96 | 98 |
| 97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { | 99 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { |
| 98 credit_card_field->expiration_date_ = scanner->Cursor(); | 100 credit_card_field->expiration_date_ = scanner->Cursor(); |
| 99 scanner->Advance(); | 101 scanner->Advance(); |
| 100 } else { | 102 } else { |
| 101 // First try to parse split month/year expiration fields. | 103 // First try to parse split month/year expiration fields. |
| 102 scanner->SaveCursor(); | 104 scanner->SaveCursor(); |
| 103 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); | 105 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); |
| 104 if (!credit_card_field->expiration_month_ && | 106 if (!credit_card_field->expiration_month_ && |
| 105 ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, | 107 ParseFieldSpecifics(scanner, |
| 108 pattern, |
| 109 MATCH_DEFAULT | MATCH_SELECT, |
| 106 &credit_card_field->expiration_month_)) { | 110 &credit_card_field->expiration_month_)) { |
| 107 pattern = base::UTF8ToUTF16(autofill::kExpirationYearRe); | 111 pattern = base::UTF8ToUTF16(autofill::kExpirationYearRe); |
| 108 if (ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, | 112 if (ParseFieldSpecifics(scanner, |
| 109 &credit_card_field->expiration_year_)) { | 113 pattern, |
| 114 MATCH_DEFAULT | MATCH_SELECT, |
| 115 &credit_card_field->expiration_year_)) { |
| 110 continue; | 116 continue; |
| 111 } | 117 } |
| 112 } | 118 } |
| 113 | 119 |
| 114 // If that fails, try to parse a combined expiration field. | 120 // If that fails, try to parse a combined expiration field. |
| 115 if (!credit_card_field->expiration_date_) { | 121 if (!credit_card_field->expiration_date_) { |
| 116 // Look for a 2-digit year first. | 122 // Look for a 2-digit year first. |
| 117 scanner->Rewind(); | 123 scanner->Rewind(); |
| 118 pattern = base::UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe); | 124 pattern = base::UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe); |
| 119 // We allow <select> fields, because they're used e.g. on qvc.com. | 125 // We allow <select> fields, because they're used e.g. on qvc.com. |
| 120 if (ParseFieldSpecifics(scanner, pattern, | 126 if (ParseFieldSpecifics( |
| 121 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | | 127 scanner, |
| 122 MATCH_SELECT, | 128 pattern, |
| 123 &credit_card_field->expiration_date_)) { | 129 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | MATCH_SELECT, |
| 124 credit_card_field->is_two_digit_year_ = true; | 130 &credit_card_field->expiration_date_)) { |
| 131 credit_card_field->exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
| 125 continue; | 132 continue; |
| 126 } | 133 } |
| 127 | 134 |
| 128 pattern = base::UTF8ToUTF16(autofill::kExpirationDateRe); | 135 pattern = base::UTF8ToUTF16(autofill::kExpirationDateRe); |
| 129 if (ParseFieldSpecifics(scanner, pattern, | 136 if (ParseFieldSpecifics( |
| 130 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | | 137 scanner, |
| 131 MATCH_SELECT, | 138 pattern, |
| 132 &credit_card_field->expiration_date_)) { | 139 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | MATCH_SELECT, |
| 140 &credit_card_field->expiration_date_)) { |
| 133 continue; | 141 continue; |
| 134 } | 142 } |
| 135 } | 143 } |
| 136 | 144 |
| 137 if (credit_card_field->expiration_month_ && | 145 if (credit_card_field->expiration_month_ && |
| 138 !credit_card_field->expiration_year_ && | 146 !credit_card_field->expiration_year_ && |
| 139 !credit_card_field->expiration_date_) { | 147 !credit_card_field->expiration_date_) { |
| 140 // Parsed a month but couldn't parse a year; give up. | 148 // Parsed a month but couldn't parse a year; give up. |
| 141 scanner->RewindTo(saved_cursor); | 149 scanner->RewindTo(saved_cursor); |
| 142 return NULL; | 150 return NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 188 |
| 181 CreditCardField::CreditCardField() | 189 CreditCardField::CreditCardField() |
| 182 : cardholder_(NULL), | 190 : cardholder_(NULL), |
| 183 cardholder_last_(NULL), | 191 cardholder_last_(NULL), |
| 184 type_(NULL), | 192 type_(NULL), |
| 185 number_(NULL), | 193 number_(NULL), |
| 186 verification_(NULL), | 194 verification_(NULL), |
| 187 expiration_month_(NULL), | 195 expiration_month_(NULL), |
| 188 expiration_year_(NULL), | 196 expiration_year_(NULL), |
| 189 expiration_date_(NULL), | 197 expiration_date_(NULL), |
| 190 is_two_digit_year_(false) { | 198 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { |
| 191 } | 199 } |
| 192 | 200 |
| 193 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { | 201 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { |
| 194 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); | 202 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); |
| 195 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); | 203 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); |
| 196 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, | 204 ok = ok && |
| 197 map); | 205 AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, map); |
| 198 | 206 |
| 199 // If the heuristics detected first and last name in separate fields, | 207 // If the heuristics detected first and last name in separate fields, |
| 200 // then ignore both fields. Putting them into separate fields is probably | 208 // 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 | 209 // wrong, because the credit card can also contain a middle name or middle |
| 202 // initial. | 210 // initial. |
| 203 if (cardholder_last_ == NULL) | 211 if (cardholder_last_ == NULL) |
| 204 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); | 212 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); |
| 205 | 213 |
| 206 if (expiration_date_) { | 214 if (expiration_date_) { |
| 207 if (is_two_digit_year_) { | 215 ok = |
| 208 ok = ok && AddClassification(expiration_date_, | 216 ok && AddClassification(expiration_date_, GetExpirationYearType(), map); |
| 209 CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, map); | |
| 210 } else { | |
| 211 ok = ok && AddClassification(expiration_date_, | |
| 212 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, map); | |
| 213 } | |
| 214 } else { | 217 } else { |
| 215 ok = ok && AddClassification(expiration_month_, CREDIT_CARD_EXP_MONTH, map); | 218 ok = ok && AddClassification(expiration_month_, CREDIT_CARD_EXP_MONTH, map); |
| 216 if (is_two_digit_year_) { | 219 ok = |
| 217 ok = ok && AddClassification(expiration_year_, | 220 ok && AddClassification(expiration_year_, GetExpirationYearType(), map); |
| 218 CREDIT_CARD_EXP_2_DIGIT_YEAR, | |
| 219 map); | |
| 220 } else { | |
| 221 ok = ok && AddClassification(expiration_year_, | |
| 222 CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
| 223 map); | |
| 224 } | |
| 225 } | 221 } |
| 226 | 222 |
| 227 return ok; | 223 return ok; |
| 228 } | 224 } |
| 229 | 225 |
| 226 ServerFieldType CreditCardField::GetExpirationYearType() const { |
| 227 return (expiration_date_ |
| 228 ? exp_year_type_ |
| 229 : ((expiration_year_ && expiration_year_->max_length == 2) |
| 230 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
| 231 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 232 } |
| 233 |
| 230 } // namespace autofill | 234 } // namespace autofill |
| OLD | NEW |