| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 if (!MatchesPattern(haystack[i + j], regex_needles[j])) | 43 if (!MatchesPattern(haystack[i + j], regex_needles[j])) |
| 44 break; | 44 break; |
| 45 | 45 |
| 46 if (j == regex_needles.size() - 1) | 46 if (j == regex_needles.size() - 1) |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 return false; | 50 return false; |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Returns true if a field that has |max_length| can fit the data for a field of |
| 54 // |type|. |
| 55 bool FieldCanFitDataForFieldType(int max_length, ServerFieldType type) { |
| 56 if (max_length == 0) |
| 57 return true; |
| 58 |
| 59 switch (type) { |
| 60 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: { |
| 61 static int kMinimum2YearCcExpLength = strlen("12/14"); |
| 62 return max_length >= kMinimum2YearCcExpLength; |
| 63 } |
| 64 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: { |
| 65 static int kMinimum4YearCcExpLength = strlen("12/2014"); |
| 66 return max_length >= kMinimum4YearCcExpLength; |
| 67 } |
| 68 default: |
| 69 NOTREACHED(); |
| 70 return false; |
| 71 } |
| 72 } |
| 73 |
| 74 |
| 53 } // namespace | 75 } // namespace |
| 54 | 76 |
| 55 // static | 77 // static |
| 56 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { | 78 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { |
| 57 if (scanner->IsEnd()) | 79 if (scanner->IsEnd()) |
| 58 return nullptr; | 80 return nullptr; |
| 59 | 81 |
| 60 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 82 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
| 61 size_t saved_cursor = scanner->SaveCursor(); | 83 size_t saved_cursor = scanner->SaveCursor(); |
| 62 | 84 |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 kMatchTelAndSelect, | 392 kMatchTelAndSelect, |
| 371 &expiration_month_) && | 393 &expiration_month_) && |
| 372 ParseFieldSpecifics(scanner, | 394 ParseFieldSpecifics(scanner, |
| 373 base::ASCIIToUTF16("^(yy|yyyy)$"), | 395 base::ASCIIToUTF16("^(yy|yyyy)$"), |
| 374 kMatchTelAndSelect, | 396 kMatchTelAndSelect, |
| 375 &expiration_year_)) { | 397 &expiration_year_)) { |
| 376 return true; | 398 return true; |
| 377 } | 399 } |
| 378 | 400 |
| 379 // If that fails, try to parse a combined expiration field. | 401 // If that fails, try to parse a combined expiration field. |
| 380 // Look for a 2-digit year first. | |
| 381 // We allow <select> fields, because they're used e.g. on qvc.com. | 402 // We allow <select> fields, because they're used e.g. on qvc.com. |
| 382 scanner->RewindTo(month_year_saved_cursor); | 403 scanner->RewindTo(month_year_saved_cursor); |
| 383 | 404 |
| 384 static int kMinimum2YearCcExpLength = strlen("12/14"); | 405 // Bail out if the field cannot fit a 2-digit year expiration date. |
| 385 int current_field_max_length = scanner->Cursor()->max_length; | 406 const int current_field_max_length = scanner->Cursor()->max_length; |
| 386 if (current_field_max_length > 0 && | 407 if (!FieldCanFitDataForFieldType(current_field_max_length, |
| 387 current_field_max_length < kMinimum2YearCcExpLength) { | 408 CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)) { |
| 388 return false; | 409 return false; |
| 389 } | 410 } |
| 390 | 411 |
| 412 // Try to look for a 2-digit year expiration date. |
| 391 if (ParseFieldSpecifics(scanner, | 413 if (ParseFieldSpecifics(scanner, |
| 392 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), | 414 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), |
| 393 kMatchTelAndSelect, | 415 kMatchTelAndSelect, |
| 394 &expiration_date_)) { | 416 &expiration_date_)) { |
| 395 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 417 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
| 396 expiration_month_ = nullptr; | 418 expiration_month_ = nullptr; |
| 397 return true; | 419 return true; |
| 398 } | 420 } |
| 399 | 421 |
| 422 // Try to look for a generic expiration date field. (2 or 4 digit year) |
| 400 if (ParseFieldSpecifics(scanner, | 423 if (ParseFieldSpecifics(scanner, |
| 401 base::UTF8ToUTF16(kExpirationDateRe), | 424 base::UTF8ToUTF16(kExpirationDateRe), |
| 402 kMatchTelAndSelect, | 425 kMatchTelAndSelect, |
| 403 &expiration_date_)) { | 426 &expiration_date_)) { |
| 404 static int kMinimum4YearCcExpLength = strlen("12/2014"); | 427 // If such a field exists, but it cannot fit a 4-digit year expiration |
| 405 if (current_field_max_length > 0 && | 428 // date, then the likely possibility is that it is a 2-digit year expiration |
| 406 current_field_max_length < kMinimum4YearCcExpLength) { | 429 // date. |
| 430 if (!FieldCanFitDataForFieldType(current_field_max_length, |
| 431 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)) { |
| 407 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 432 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
| 408 } | 433 } |
| 409 expiration_month_ = nullptr; | 434 expiration_month_ = nullptr; |
| 410 return true; | 435 return true; |
| 411 } | 436 } |
| 412 | 437 |
| 438 // Try to look for a 4-digit year expiration date. |
| 439 if (FieldCanFitDataForFieldType(current_field_max_length, |
| 440 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) && |
| 441 ParseFieldSpecifics(scanner, |
| 442 base::UTF8ToUTF16(kExpirationDate4DigitYearRe), |
| 443 kMatchTelAndSelect, |
| 444 &expiration_date_)) { |
| 445 expiration_month_ = nullptr; |
| 446 return true; |
| 447 } |
| 448 |
| 413 return false; | 449 return false; |
| 414 } | 450 } |
| 415 | 451 |
| 416 ServerFieldType CreditCardField::GetExpirationYearType() const { | 452 ServerFieldType CreditCardField::GetExpirationYearType() const { |
| 417 return (expiration_date_ | 453 return (expiration_date_ |
| 418 ? exp_year_type_ | 454 ? exp_year_type_ |
| 419 : ((expiration_year_ && expiration_year_->max_length == 2) | 455 : ((expiration_year_ && expiration_year_->max_length == 2) |
| 420 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 456 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
| 421 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 457 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 422 } | 458 } |
| 423 | 459 |
| 424 } // namespace autofill | 460 } // namespace autofill |
| OLD | NEW |