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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 // static | 53 // static |
| 54 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { | 54 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { |
| 55 if (scanner->IsEnd()) | 55 if (scanner->IsEnd()) |
| 56 return nullptr; | 56 return nullptr; |
| 57 | 57 |
| 58 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 58 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
| 59 size_t saved_cursor = scanner->SaveCursor(); | 59 size_t saved_cursor = scanner->SaveCursor(); |
| 60 | 60 |
| 61 // Credit card fields can appear in many different orders. | 61 // Credit card fields can appear in many different orders. |
| 62 // We loop until no more credit card related fields are found, see |break| at | 62 // We loop until no more credit card related fields are found, see |break| at |
| 63 // bottom of the loop. | 63 // the bottom of the loop. |
| 64 for (int fields = 0; !scanner->IsEnd(); ++fields) { | 64 for (int fields = 0; !scanner->IsEnd(); ++fields) { |
| 65 // Ignore gift card fields. | 65 // Ignore gift card fields. |
| 66 if (ParseField(scanner, base::UTF8ToUTF16(kGiftCardRe), nullptr)) | 66 if (ParseField(scanner, base::UTF8ToUTF16(kGiftCardRe), nullptr)) |
| 67 break; | 67 break; |
| 68 | 68 |
| 69 if (!credit_card_field->cardholder_) { | 69 if (!credit_card_field->cardholder_) { |
| 70 if (ParseField(scanner, | 70 if (ParseField(scanner, |
| 71 base::UTF8ToUTF16(kNameOnCardRe), | 71 base::UTF8ToUTF16(kNameOnCardRe), |
| 72 &credit_card_field->cardholder_)) { | 72 &credit_card_field->cardholder_)) { |
| 73 continue; | 73 continue; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Sometimes the cardholder field is just labeled "name". Unfortunately | 76 // Sometimes the cardholder field is just labeled "name". Unfortunately |
| 77 // this is a dangerously generic word to search for, since it will often | 77 // this is a dangerously generic word to search for, since it will often |
| 78 // match a name (not cardholder name) field before or after credit card | 78 // match a name (not cardholder name) field before or after credit card |
| 79 // fields. So we search for "name" only when we've already parsed at | 79 // fields. So we search for "name" only when we've already parsed at |
| 80 // least one other credit card field and haven't yet parsed the | 80 // least one other credit card field and haven't yet parsed the |
| 81 // expiration date (which usually appears at the end). | 81 // expiration date (which usually appears at the end). |
| 82 if (fields > 0 && | 82 if (fields > 0 && |
| 83 !credit_card_field->expiration_month_ && | 83 !credit_card_field->expiration_month_ && |
| 84 ParseField(scanner, | 84 ParseField(scanner, |
| 85 base::UTF8ToUTF16(kNameOnCardContextualRe), | 85 base::UTF8ToUTF16(kNameOnCardContextualRe), |
| 86 &credit_card_field->cardholder_)) { | 86 &credit_card_field->cardholder_)) { |
| 87 continue; | 87 continue; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Check for a credit card type (Visa, MasterCard, etc.) field. | 91 // Check for a credit card type (Visa, MasterCard, etc.) field. |
| 92 if (!credit_card_field->type_ && | 92 if (!credit_card_field->type_ && LikelyCardTypeSelectField(scanner)) { |
| 93 ParseFieldSpecifics(scanner, | 93 credit_card_field->type_ = scanner->Cursor(); |
| 94 base::UTF8ToUTF16(kCardTypeRe), | 94 scanner->Advance(); |
| 95 MATCH_DEFAULT | MATCH_SELECT, | |
|
Evan Stade
2015/03/25 21:25:51
so we'll no longer fill text inputs?
Lei Zhang
2015/03/25 22:18:48
Correct. None of the websites in our heuristics te
Evan Stade
2015/03/25 22:23:08
k, seems fine. Can you put a comment to that effec
Lei Zhang
2015/03/25 22:39:03
Done.
| |
| 96 &credit_card_field->type_)) { | |
| 97 continue; | 95 continue; |
| 98 } | 96 } |
| 99 | 97 |
| 100 // We look for a card security code before we look for a credit card number | 98 // We look for a card security code before we look for a credit card number |
| 101 // and match the general term "number". The security code has a plethora of | 99 // and match the general term "number". The security code has a plethora of |
| 102 // names; we've seen "verification #", "verification number", "card | 100 // names; we've seen "verification #", "verification number", "card |
| 103 // identification number", and others listed in the regex pattern used | 101 // identification number", and others listed in the regex pattern used |
| 104 // below. | 102 // below. |
| 105 // Note: Some sites use type="tel" or type="number" for numerical inputs. | 103 // Note: Some sites use type="tel" or type="number" for numerical inputs. |
| 106 const int kMatchNumAndTel = MATCH_DEFAULT | MATCH_NUMBER | MATCH_TELEPHONE; | 104 const int kMatchNumAndTel = MATCH_DEFAULT | MATCH_NUMBER | MATCH_TELEPHONE; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 continue; | 141 continue; |
| 144 | 142 |
| 145 if (credit_card_field->expiration_month_ && | 143 if (credit_card_field->expiration_month_ && |
| 146 !credit_card_field->expiration_year_ && | 144 !credit_card_field->expiration_year_ && |
| 147 !credit_card_field->expiration_date_) { | 145 !credit_card_field->expiration_date_) { |
| 148 // Parsed a month but couldn't parse a year; give up. | 146 // Parsed a month but couldn't parse a year; give up. |
| 149 scanner->RewindTo(saved_cursor); | 147 scanner->RewindTo(saved_cursor); |
| 150 return nullptr; | 148 return nullptr; |
| 151 } | 149 } |
| 152 | 150 |
| 153 // Some pages (e.g. ExpediaBilling.html) have a "card description" | |
| 154 // field; we parse this field but ignore it. | |
| 155 // We also ignore any other fields within a credit card block that | |
| 156 // start with "card", under the assumption that they are related to | |
| 157 // the credit card section being processed but are uninteresting to us. | |
| 158 if (ParseField(scanner, base::UTF8ToUTF16(kCardIgnoredRe), nullptr)) | |
| 159 continue; | |
| 160 | |
| 161 break; | 151 break; |
| 162 } | 152 } |
| 163 | 153 |
| 164 // Some pages have a billing address field after the cardholder name field. | 154 // Some pages have a billing address field after the cardholder name field. |
| 165 // For that case, allow only just the cardholder name field. The remaining | 155 // For that case, allow only just the cardholder name field. The remaining |
| 166 // CC fields will be picked up in a following CreditCardField. | 156 // CC fields will be picked up in a following CreditCardField. |
| 167 if (credit_card_field->cardholder_) | 157 if (credit_card_field->cardholder_) |
| 168 return credit_card_field.Pass(); | 158 return credit_card_field.Pass(); |
| 169 | 159 |
| 170 // On some pages, the user selects a card type using radio buttons | 160 // On some pages, the user selects a card type using radio buttons |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 std::vector<base::string16> years_to_check; | 230 std::vector<base::string16> years_to_check; |
| 241 for (int year = time_exploded.year; | 231 for (int year = time_exploded.year; |
| 242 year < time_exploded.year + kYearsToMatch; | 232 year < time_exploded.year + kYearsToMatch; |
| 243 ++year) { | 233 ++year) { |
| 244 years_to_check.push_back(base::IntToString16(year)); | 234 years_to_check.push_back(base::IntToString16(year)); |
| 245 } | 235 } |
| 246 return (FindConsecutiveStrings(years_to_check, field->option_values) || | 236 return (FindConsecutiveStrings(years_to_check, field->option_values) || |
| 247 FindConsecutiveStrings(years_to_check, field->option_contents)); | 237 FindConsecutiveStrings(years_to_check, field->option_contents)); |
| 248 } | 238 } |
| 249 | 239 |
| 240 // static | |
| 241 bool CreditCardField::LikelyCardTypeSelectField(AutofillScanner* scanner) { | |
| 242 if (scanner->IsEnd()) | |
| 243 return false; | |
| 244 | |
| 245 AutofillField* field = scanner->Cursor(); | |
| 246 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT)) | |
| 247 return false; | |
| 248 | |
| 249 // VISA and Mastercard are oftentimes branded as is in other countries, so | |
| 250 // translations are probably not needed. | |
| 251 const char* const kCardBrands[] = { | |
|
Evan Stade
2015/03/25 21:25:51
nit: this is a little over-engineered imo. You can
Lei Zhang
2015/03/25 22:18:48
I'm just going to use IDS_AUTOFILL_CC_FOO since Ty
Evan Stade
2015/03/25 22:23:08
sgtm
| |
| 252 "visa", | |
| 253 "mastercard", | |
| 254 }; | |
| 255 for (const char* brand : kCardBrands) { | |
| 256 if (AutofillField::FindValueInCreditTypeSelectControl( | |
| 257 *field, base::ASCIIToUTF16(brand), nullptr)) { | |
| 258 return true; | |
| 259 } | |
| 260 } | |
| 261 return false; | |
| 262 } | |
| 263 | |
| 250 CreditCardField::CreditCardField() | 264 CreditCardField::CreditCardField() |
| 251 : cardholder_(nullptr), | 265 : cardholder_(nullptr), |
| 252 cardholder_last_(nullptr), | 266 cardholder_last_(nullptr), |
| 253 type_(nullptr), | 267 type_(nullptr), |
| 254 verification_(nullptr), | 268 verification_(nullptr), |
| 255 expiration_month_(nullptr), | 269 expiration_month_(nullptr), |
| 256 expiration_year_(nullptr), | 270 expiration_year_(nullptr), |
| 257 expiration_date_(nullptr), | 271 expiration_date_(nullptr), |
| 258 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { | 272 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { |
| 259 } | 273 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 | 388 |
| 375 ServerFieldType CreditCardField::GetExpirationYearType() const { | 389 ServerFieldType CreditCardField::GetExpirationYearType() const { |
| 376 return (expiration_date_ | 390 return (expiration_date_ |
| 377 ? exp_year_type_ | 391 ? exp_year_type_ |
| 378 : ((expiration_year_ && expiration_year_->max_length == 2) | 392 : ((expiration_year_ && expiration_year_->max_length == 2) |
| 379 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 393 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
| 380 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 394 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 381 } | 395 } |
| 382 | 396 |
| 383 } // namespace autofill | 397 } // namespace autofill |
| OLD | NEW |