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.h" | 5 #include "components/autofill/core/browser/credit_card.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <ostream> | 10 #include <ostream> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 } | 45 } |
46 | 46 |
47 // Try parsing the |year| as a number. | 47 // Try parsing the |year| as a number. |
48 if (base::StringToInt(year, num)) | 48 if (base::StringToInt(year, num)) |
49 return true; | 49 return true; |
50 | 50 |
51 *num = 0; | 51 *num = 0; |
52 return false; | 52 return false; |
53 } | 53 } |
54 | 54 |
55 bool ConvertMonth(const base::string16& month, | |
56 const std::string& app_locale, | |
57 int* num) { | |
58 // If the |month| is empty, clear the stored value. | |
59 if (month.empty()) { | |
60 *num = 0; | |
61 return true; | |
62 } | |
63 | |
64 // Try parsing the |month| as a number. | |
65 if (base::StringToInt(month, num)) | |
66 return true; | |
67 | |
68 // If the locale is unknown, give up. | |
69 if (app_locale.empty()) | |
70 return false; | |
71 | |
72 // Otherwise, try parsing the |month| as a named month, e.g. "January" or | |
73 // "Jan". | |
74 l10n::CaseInsensitiveCompare compare; | |
75 UErrorCode status = U_ZERO_ERROR; | |
76 icu::Locale locale(app_locale.c_str()); | |
77 icu::DateFormatSymbols date_format_symbols(locale, status); | |
78 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || | |
79 status == U_USING_DEFAULT_WARNING); | |
80 | |
81 int32_t num_months; | |
82 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months); | |
83 for (int32_t i = 0; i < num_months; ++i) { | |
84 const base::string16 icu_month(months[i].getBuffer(), months[i].length()); | |
85 if (compare.StringsEqual(icu_month, month)) { | |
86 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
87 return true; | |
88 } | |
89 } | |
90 | |
91 months = date_format_symbols.getShortMonths(num_months); | |
92 for (int32_t i = 0; i < num_months; ++i) { | |
93 const base::string16 icu_month(months[i].getBuffer(), months[i].length()); | |
94 if (compare.StringsEqual(icu_month, month)) { | |
95 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
96 return true; | |
97 } | |
98 } | |
99 | |
100 *num = 0; | |
101 return false; | |
102 } | |
103 | |
104 base::string16 TypeForFill(const std::string& type) { | 55 base::string16 TypeForFill(const std::string& type) { |
105 if (type == kAmericanExpressCard) | 56 if (type == kAmericanExpressCard) |
106 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); | 57 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); |
107 if (type == kDinersCard) | 58 if (type == kDinersCard) |
108 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); | 59 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); |
109 if (type == kDiscoverCard) | 60 if (type == kDiscoverCard) |
110 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); | 61 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); |
111 if (type == kJCBCard) | 62 if (type == kJCBCard) |
112 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); | 63 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); |
113 if (type == kMasterCard) | 64 if (type == kMasterCard) |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 void CreditCard::GetMatchingTypes(const base::string16& text, | 401 void CreditCard::GetMatchingTypes(const base::string16& text, |
451 const std::string& app_locale, | 402 const std::string& app_locale, |
452 ServerFieldTypeSet* matching_types) const { | 403 ServerFieldTypeSet* matching_types) const { |
453 FormGroup::GetMatchingTypes(text, app_locale, matching_types); | 404 FormGroup::GetMatchingTypes(text, app_locale, matching_types); |
454 | 405 |
455 base::string16 card_number = | 406 base::string16 card_number = |
456 GetInfo(AutofillType(CREDIT_CARD_NUMBER), app_locale); | 407 GetInfo(AutofillType(CREDIT_CARD_NUMBER), app_locale); |
457 if (!card_number.empty() && StripSeparators(text) == card_number) | 408 if (!card_number.empty() && StripSeparators(text) == card_number) |
458 matching_types->insert(CREDIT_CARD_NUMBER); | 409 matching_types->insert(CREDIT_CARD_NUMBER); |
459 | 410 |
| 411 NOTIMPLEMENTED() << " GETMATCINGTYPES " << text; |
460 int month; | 412 int month; |
461 if (ConvertMonth(text, app_locale, &month) && month != 0 && | 413 if (ConvertMonth(text, app_locale, &month) && |
462 month == expiration_month_) { | 414 month == expiration_month_) { |
463 matching_types->insert(CREDIT_CARD_EXP_MONTH); | 415 matching_types->insert(CREDIT_CARD_EXP_MONTH); |
464 } | 416 } |
465 } | 417 } |
466 | 418 |
467 const base::string16 CreditCard::Label() const { | 419 const base::string16 CreditCard::Label() const { |
468 std::pair<base::string16, base::string16> pieces = LabelPieces(); | 420 std::pair<base::string16, base::string16> pieces = LabelPieces(); |
469 return pieces.first + pieces.second; | 421 return pieces.first + pieces.second; |
470 } | 422 } |
471 | 423 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 | 676 |
725 base::string16 CreditCard::Expiration2DigitYearAsString() const { | 677 base::string16 CreditCard::Expiration2DigitYearAsString() const { |
726 if (expiration_year_ == 0) | 678 if (expiration_year_ == 0) |
727 return base::string16(); | 679 return base::string16(); |
728 | 680 |
729 return base::IntToString16(Expiration2DigitYear()); | 681 return base::IntToString16(Expiration2DigitYear()); |
730 } | 682 } |
731 | 683 |
732 void CreditCard::SetExpirationMonthFromString(const base::string16& text, | 684 void CreditCard::SetExpirationMonthFromString(const base::string16& text, |
733 const std::string& app_locale) { | 685 const std::string& app_locale) { |
734 int month; | 686 int month = 0; |
735 if (!ConvertMonth(text, app_locale, &month)) | 687 if (!text.empty() && !ConvertMonth(text, app_locale, &month)) |
736 return; | 688 return; |
737 | 689 |
738 SetExpirationMonth(month); | 690 SetExpirationMonth(month); |
739 } | 691 } |
740 | 692 |
741 void CreditCard::SetExpirationYearFromString(const base::string16& text) { | 693 void CreditCard::SetExpirationYearFromString(const base::string16& text) { |
742 int year; | 694 int year; |
743 if (!ConvertYear(text, &year)) | 695 if (!ConvertYear(text, &year)) |
744 return; | 696 return; |
745 | 697 |
746 SetExpirationYear(year); | 698 SetExpirationYear(year); |
747 } | 699 } |
748 | 700 |
749 void CreditCard::SetNumber(const base::string16& number) { | 701 void CreditCard::SetNumber(const base::string16& number) { |
750 number_ = number; | 702 number_ = number; |
751 | 703 |
752 // Set the type based on the card number, but only for full numbers, not | 704 // Set the type based on the card number, but only for full numbers, not |
753 // when we have masked cards from the server (last 4 digits). | 705 // when we have masked cards from the server (last 4 digits). |
754 if (record_type_ != MASKED_SERVER_CARD) | 706 if (record_type_ != MASKED_SERVER_CARD) |
755 type_ = GetCreditCardType(StripSeparators(number_)); | 707 type_ = GetCreditCardType(StripSeparators(number_)); |
756 } | 708 } |
757 | 709 |
| 710 // static |
| 711 bool CreditCard::ConvertMonth(const base::string16& month, |
| 712 const std::string& app_locale, |
| 713 int* num) { |
| 714 if (month.empty()) |
| 715 return false; |
| 716 |
| 717 // Try parsing the |month| as a number (this doesn't require |app_locale|). |
| 718 if (base::StringToInt(month, num)) |
| 719 return true; |
| 720 |
| 721 if (app_locale.empty()) |
| 722 return false; |
| 723 |
| 724 // Otherwise, try parsing the |month| as a named month, e.g. "January" or |
| 725 // "Jan". |
| 726 l10n::CaseInsensitiveCompare compare; |
| 727 UErrorCode status = U_ZERO_ERROR; |
| 728 icu::Locale locale(app_locale.c_str()); |
| 729 icu::DateFormatSymbols date_format_symbols(locale, status); |
| 730 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || |
| 731 status == U_USING_DEFAULT_WARNING); |
| 732 |
| 733 int32_t num_months; |
| 734 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months); |
| 735 for (int32_t i = 0; i < num_months; ++i) { |
| 736 const base::string16 icu_month(months[i].getBuffer(), months[i].length()); |
| 737 if (compare.StringsEqual(icu_month, month)) { |
| 738 *num = i + 1; // Adjust from 0-indexed to 1-indexed. |
| 739 return true; |
| 740 } |
| 741 } |
| 742 |
| 743 months = date_format_symbols.getShortMonths(num_months); |
| 744 // Some abbreviations have . at the end (e.g., "janv." in French). We don't |
| 745 // care about matching that. |
| 746 base::string16 trimmed_month; |
| 747 base::TrimString(month, base::ASCIIToUTF16("."), &trimmed_month); |
| 748 for (int32_t i = 0; i < num_months; ++i) { |
| 749 base::string16 icu_month(months[i].getBuffer(), months[i].length()); |
| 750 base::TrimString(icu_month, base::ASCIIToUTF16("."), &icu_month); |
| 751 if (compare.StringsEqual(icu_month, trimmed_month)) { |
| 752 *num = i + 1; // Adjust from 0-indexed to 1-indexed. |
| 753 return true; |
| 754 } |
| 755 } |
| 756 |
| 757 return false; |
| 758 } |
| 759 |
758 // So we can compare CreditCards with EXPECT_EQ(). | 760 // So we can compare CreditCards with EXPECT_EQ(). |
759 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { | 761 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { |
760 return os | 762 return os |
761 << base::UTF16ToUTF8(credit_card.Label()) | 763 << base::UTF16ToUTF8(credit_card.Label()) |
762 << " " | 764 << " " |
763 << credit_card.guid() | 765 << credit_card.guid() |
764 << " " | 766 << " " |
765 << credit_card.origin() | 767 << credit_card.origin() |
766 << " " | 768 << " " |
767 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME)) | 769 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME)) |
(...skipping 14 matching lines...) Expand all Loading... |
782 const char* const kAmericanExpressCard = "americanExpressCC"; | 784 const char* const kAmericanExpressCard = "americanExpressCC"; |
783 const char* const kDinersCard = "dinersCC"; | 785 const char* const kDinersCard = "dinersCC"; |
784 const char* const kDiscoverCard = "discoverCC"; | 786 const char* const kDiscoverCard = "discoverCC"; |
785 const char* const kGenericCard = "genericCC"; | 787 const char* const kGenericCard = "genericCC"; |
786 const char* const kJCBCard = "jcbCC"; | 788 const char* const kJCBCard = "jcbCC"; |
787 const char* const kMasterCard = "masterCardCC"; | 789 const char* const kMasterCard = "masterCardCC"; |
788 const char* const kUnionPay = "unionPayCC"; | 790 const char* const kUnionPay = "unionPayCC"; |
789 const char* const kVisaCard = "visaCC"; | 791 const char* const kVisaCard = "visaCC"; |
790 | 792 |
791 } // namespace autofill | 793 } // namespace autofill |
OLD | NEW |