| 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 14 matching lines...) Expand all Loading... |
| 25 return NULL; | 25 return NULL; |
| 26 | 26 |
| 27 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 27 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
| 28 size_t saved_cursor = scanner->SaveCursor(); | 28 size_t saved_cursor = scanner->SaveCursor(); |
| 29 | 29 |
| 30 // Credit card fields can appear in many different orders. | 30 // Credit card fields can appear in many different orders. |
| 31 // We loop until no more credit card related fields are found, see |break| at | 31 // We loop until no more credit card related fields are found, see |break| at |
| 32 // bottom of the loop. | 32 // bottom of the loop. |
| 33 for (int fields = 0; !scanner->IsEnd(); ++fields) { | 33 for (int fields = 0; !scanner->IsEnd(); ++fields) { |
| 34 // Ignore gift card fields. | 34 // Ignore gift card fields. |
| 35 if (ParseField(scanner, UTF8ToUTF16(autofill::kGiftCardRe), NULL)) | 35 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kGiftCardRe), NULL)) |
| 36 break; | 36 break; |
| 37 | 37 |
| 38 // Sometimes the cardholder field is just labeled "name". Unfortunately this | 38 // Sometimes the cardholder field is just labeled "name". Unfortunately this |
| 39 // is a dangerously generic word to search for, since it will often match a | 39 // is a dangerously generic word to search for, since it will often match a |
| 40 // name (not cardholder name) field before or after credit card fields. So | 40 // name (not cardholder name) field before or after credit card fields. So |
| 41 // we search for "name" only when we've already parsed at least one other | 41 // we search for "name" only when we've already parsed at least one other |
| 42 // credit card field and haven't yet parsed the expiration date (which | 42 // credit card field and haven't yet parsed the expiration date (which |
| 43 // usually appears at the end). | 43 // usually appears at the end). |
| 44 if (credit_card_field->cardholder_ == NULL) { | 44 if (credit_card_field->cardholder_ == NULL) { |
| 45 base::string16 name_pattern; | 45 base::string16 name_pattern; |
| 46 if (fields == 0 || credit_card_field->expiration_month_) { | 46 if (fields == 0 || credit_card_field->expiration_month_) { |
| 47 // at beginning or end | 47 // at beginning or end |
| 48 name_pattern = UTF8ToUTF16(autofill::kNameOnCardRe); | 48 name_pattern = base::UTF8ToUTF16(autofill::kNameOnCardRe); |
| 49 } else { | 49 } else { |
| 50 name_pattern = UTF8ToUTF16(autofill::kNameOnCardContextualRe); | 50 name_pattern = base::UTF8ToUTF16(autofill::kNameOnCardContextualRe); |
| 51 } | 51 } |
| 52 | 52 |
| 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, ASCIIToUTF16("^cfnm"), &first) && | 62 if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) && |
| 63 ParseField(scanner, ASCIIToUTF16("^clnm"), | 63 ParseField(scanner, base::ASCIIToUTF16("^clnm"), |
| 64 &credit_card_field->cardholder_last_)) { | 64 &credit_card_field->cardholder_last_)) { |
| 65 credit_card_field->cardholder_ = first; | 65 credit_card_field->cardholder_ = first; |
| 66 continue; | 66 continue; |
| 67 } | 67 } |
| 68 scanner->Rewind(); | 68 scanner->Rewind(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Check for a credit card type (Visa, MasterCard, etc.) field. | 71 // Check for a credit card type (Visa, MasterCard, etc.) field. |
| 72 base::string16 type_pattern = UTF8ToUTF16(autofill::kCardTypeRe); | 72 base::string16 type_pattern = base::UTF8ToUTF16(autofill::kCardTypeRe); |
| 73 if (!credit_card_field->type_ && | 73 if (!credit_card_field->type_ && |
| 74 ParseFieldSpecifics(scanner, type_pattern, | 74 ParseFieldSpecifics(scanner, type_pattern, |
| 75 MATCH_DEFAULT | MATCH_SELECT, | 75 MATCH_DEFAULT | MATCH_SELECT, |
| 76 &credit_card_field->type_)) { | 76 &credit_card_field->type_)) { |
| 77 continue; | 77 continue; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // We look for a card security code before we look for a credit | 80 // 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 | 81 // card number and match the general term "number". The security code |
| 82 // has a plethora of names; we've seen "verification #", | 82 // has a plethora of names; we've seen "verification #", |
| 83 // "verification number", "card identification number" and others listed | 83 // "verification number", "card identification number" and others listed |
| 84 // in the |pattern| below. | 84 // in the |pattern| below. |
| 85 base::string16 pattern = UTF8ToUTF16(autofill::kCardCvcRe); | 85 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); |
| 86 if (!credit_card_field->verification_ && | 86 if (!credit_card_field->verification_ && |
| 87 ParseField(scanner, pattern, &credit_card_field->verification_)) { | 87 ParseField(scanner, pattern, &credit_card_field->verification_)) { |
| 88 continue; | 88 continue; |
| 89 } | 89 } |
| 90 | 90 |
| 91 pattern = UTF8ToUTF16(autofill::kCardNumberRe); | 91 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); |
| 92 if (!credit_card_field->number_ && | 92 if (!credit_card_field->number_ && |
| 93 ParseField(scanner, pattern, &credit_card_field->number_)) { | 93 ParseField(scanner, pattern, &credit_card_field->number_)) { |
| 94 continue; | 94 continue; |
| 95 } | 95 } |
| 96 | 96 |
| 97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { | 97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { |
| 98 credit_card_field->expiration_date_ = scanner->Cursor(); | 98 credit_card_field->expiration_date_ = scanner->Cursor(); |
| 99 scanner->Advance(); | 99 scanner->Advance(); |
| 100 } else { | 100 } else { |
| 101 // First try to parse split month/year expiration fields. | 101 // First try to parse split month/year expiration fields. |
| 102 scanner->SaveCursor(); | 102 scanner->SaveCursor(); |
| 103 pattern = UTF8ToUTF16(autofill::kExpirationMonthRe); | 103 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); |
| 104 if (!credit_card_field->expiration_month_ && | 104 if (!credit_card_field->expiration_month_ && |
| 105 ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, | 105 ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, |
| 106 &credit_card_field->expiration_month_)) { | 106 &credit_card_field->expiration_month_)) { |
| 107 pattern = UTF8ToUTF16(autofill::kExpirationYearRe); | 107 pattern = base::UTF8ToUTF16(autofill::kExpirationYearRe); |
| 108 if (ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, | 108 if (ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT, |
| 109 &credit_card_field->expiration_year_)) { | 109 &credit_card_field->expiration_year_)) { |
| 110 continue; | 110 continue; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 // If that fails, try to parse a combined expiration field. | 114 // If that fails, try to parse a combined expiration field. |
| 115 if (!credit_card_field->expiration_date_) { | 115 if (!credit_card_field->expiration_date_) { |
| 116 // Look for a 2-digit year first. | 116 // Look for a 2-digit year first. |
| 117 scanner->Rewind(); | 117 scanner->Rewind(); |
| 118 pattern = UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe); | 118 pattern = base::UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe); |
| 119 // We allow <select> fields, because they're used e.g. on qvc.com. | 119 // We allow <select> fields, because they're used e.g. on qvc.com. |
| 120 if (ParseFieldSpecifics(scanner, pattern, | 120 if (ParseFieldSpecifics(scanner, pattern, |
| 121 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | | 121 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | |
| 122 MATCH_SELECT, | 122 MATCH_SELECT, |
| 123 &credit_card_field->expiration_date_)) { | 123 &credit_card_field->expiration_date_)) { |
| 124 credit_card_field->is_two_digit_year_ = true; | 124 credit_card_field->is_two_digit_year_ = true; |
| 125 continue; | 125 continue; |
| 126 } | 126 } |
| 127 | 127 |
| 128 pattern = UTF8ToUTF16(autofill::kExpirationDateRe); | 128 pattern = base::UTF8ToUTF16(autofill::kExpirationDateRe); |
| 129 if (ParseFieldSpecifics(scanner, pattern, | 129 if (ParseFieldSpecifics(scanner, pattern, |
| 130 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | | 130 MATCH_LABEL | MATCH_VALUE | MATCH_TEXT | |
| 131 MATCH_SELECT, | 131 MATCH_SELECT, |
| 132 &credit_card_field->expiration_date_)) { | 132 &credit_card_field->expiration_date_)) { |
| 133 continue; | 133 continue; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 if (credit_card_field->expiration_month_ && | 137 if (credit_card_field->expiration_month_ && |
| 138 !credit_card_field->expiration_year_ && | 138 !credit_card_field->expiration_year_ && |
| 139 !credit_card_field->expiration_date_) { | 139 !credit_card_field->expiration_date_) { |
| 140 // Parsed a month but couldn't parse a year; give up. | 140 // Parsed a month but couldn't parse a year; give up. |
| 141 scanner->RewindTo(saved_cursor); | 141 scanner->RewindTo(saved_cursor); |
| 142 return NULL; | 142 return NULL; |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Some pages (e.g. ExpediaBilling.html) have a "card description" | 146 // Some pages (e.g. ExpediaBilling.html) have a "card description" |
| 147 // field; we parse this field but ignore it. | 147 // field; we parse this field but ignore it. |
| 148 // We also ignore any other fields within a credit card block that | 148 // We also ignore any other fields within a credit card block that |
| 149 // start with "card", under the assumption that they are related to | 149 // start with "card", under the assumption that they are related to |
| 150 // the credit card section being processed but are uninteresting to us. | 150 // the credit card section being processed but are uninteresting to us. |
| 151 if (ParseField(scanner, UTF8ToUTF16(autofill::kCardIgnoredRe), NULL)) | 151 if (ParseField(scanner, base::UTF8ToUTF16(autofill::kCardIgnoredRe), NULL)) |
| 152 continue; | 152 continue; |
| 153 | 153 |
| 154 break; | 154 break; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Some pages have a billing address field after the cardholder name field. | 157 // Some pages have a billing address field after the cardholder name field. |
| 158 // For that case, allow only just the cardholder name field. The remaining | 158 // For that case, allow only just the cardholder name field. The remaining |
| 159 // CC fields will be picked up in a following CreditCardField. | 159 // CC fields will be picked up in a following CreditCardField. |
| 160 if (credit_card_field->cardholder_) | 160 if (credit_card_field->cardholder_) |
| 161 return credit_card_field.release(); | 161 return credit_card_field.release(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 ok = ok && AddClassification(expiration_year_, | 221 ok = ok && AddClassification(expiration_year_, |
| 222 CREDIT_CARD_EXP_4_DIGIT_YEAR, | 222 CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 223 map); | 223 map); |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 | 226 |
| 227 return ok; | 227 return ok; |
| 228 } | 228 } |
| 229 | 229 |
| 230 } // namespace autofill | 230 } // namespace autofill |
| OLD | NEW |