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.h" | 5 #include "components/autofill/core/browser/credit_card.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <ostream> | 11 #include <ostream> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/guid.h" | 14 #include "base/guid.h" |
| 15 #include "base/i18n/time_formatting.h" | |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_UNION_PAY); | 82 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_UNION_PAY); |
| 82 if (type == kVisaCard) | 83 if (type == kVisaCard) |
| 83 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); | 84 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); |
| 84 | 85 |
| 85 // If you hit this DCHECK, the above list of cases needs to be updated to | 86 // If you hit this DCHECK, the above list of cases needs to be updated to |
| 86 // include a new card. | 87 // include a new card. |
| 87 DCHECK_EQ(kGenericCard, type); | 88 DCHECK_EQ(kGenericCard, type); |
| 88 return base::string16(); | 89 return base::string16(); |
| 89 } | 90 } |
| 90 | 91 |
| 92 // Padding month and day info to exactly 2 digits. | |
| 93 base::string16 PadTo2Digit(int32_t date_info) { | |
| 94 DCHECK(date_info > 0); | |
| 95 base::string16 date_in_2_digits = base::IntToString16(date_info); | |
| 96 if (date_info >= 10) | |
| 97 return date_in_2_digits; | |
| 98 | |
| 99 base::string16 padded_date = ASCIIToUTF16("0"); | |
| 100 padded_date.append(date_in_2_digits); | |
| 101 return padded_date; | |
|
jungshik at Google
2017/01/23 21:43:24
Some locales do NOT use ASCII digit. So that prepe
jiahuiguo
2017/01/26 05:46:12
Thanks for pointing this out, for the expiration d
| |
| 102 } | |
| 103 | |
| 91 } // namespace | 104 } // namespace |
| 92 | 105 |
| 93 CreditCard::CreditCard(const std::string& guid, const std::string& origin) | 106 CreditCard::CreditCard(const std::string& guid, const std::string& origin) |
| 94 : AutofillDataModel(guid, origin), | 107 : AutofillDataModel(guid, origin), |
| 95 record_type_(LOCAL_CARD), | 108 record_type_(LOCAL_CARD), |
| 96 type_(kGenericCard), | 109 type_(kGenericCard), |
| 97 expiration_month_(0), | 110 expiration_month_(0), |
| 98 expiration_year_(0), | 111 expiration_year_(0), |
| 99 server_status_(OK) {} | 112 server_status_(OK) {} |
| 100 | 113 |
| (...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 supported_types->insert(CREDIT_CARD_NUMBER); | 683 supported_types->insert(CREDIT_CARD_NUMBER); |
| 671 supported_types->insert(CREDIT_CARD_TYPE); | 684 supported_types->insert(CREDIT_CARD_TYPE); |
| 672 supported_types->insert(CREDIT_CARD_EXP_MONTH); | 685 supported_types->insert(CREDIT_CARD_EXP_MONTH); |
| 673 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); | 686 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); |
| 674 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); | 687 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); |
| 675 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); | 688 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); |
| 676 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | 689 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); |
| 677 } | 690 } |
| 678 | 691 |
| 679 base::string16 CreditCard::ExpirationMonthAsString() const { | 692 base::string16 CreditCard::ExpirationMonthAsString() const { |
| 680 if (expiration_month_ == 0) | 693 return expiration_month_ == 0 |
| 681 return base::string16(); | 694 ? base::string16() |
| 682 | 695 : PadTo2Digit(expiration_month_); |
| 683 base::string16 month = base::IntToString16(expiration_month_); | |
| 684 if (expiration_month_ >= 10) | |
| 685 return month; | |
| 686 | |
| 687 base::string16 zero = ASCIIToUTF16("0"); | |
| 688 zero.append(month); | |
| 689 return zero; | |
| 690 } | 696 } |
| 691 | 697 |
| 692 base::string16 CreditCard::TypeForFill() const { | 698 base::string16 CreditCard::TypeForFill() const { |
| 693 return ::autofill::TypeForFill(type_); | 699 return ::autofill::TypeForFill(type_); |
| 694 } | 700 } |
| 695 | 701 |
| 696 base::string16 CreditCard::Expiration4DigitYearAsString() const { | 702 base::string16 CreditCard::Expiration4DigitYearAsString() const { |
| 697 if (expiration_year_ == 0) | 703 if (expiration_year_ == 0) |
| 698 return base::string16(); | 704 return base::string16(); |
| 699 | 705 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 776 | 782 |
| 777 void CreditCard::SetNumber(const base::string16& number) { | 783 void CreditCard::SetNumber(const base::string16& number) { |
| 778 number_ = number; | 784 number_ = number; |
| 779 | 785 |
| 780 // Set the type based on the card number, but only for full numbers, not | 786 // Set the type based on the card number, but only for full numbers, not |
| 781 // when we have masked cards from the server (last 4 digits). | 787 // when we have masked cards from the server (last 4 digits). |
| 782 if (record_type_ != MASKED_SERVER_CARD) | 788 if (record_type_ != MASKED_SERVER_CARD) |
| 783 type_ = GetCreditCardType(StripSeparators(number_)); | 789 type_ = GetCreditCardType(StripSeparators(number_)); |
| 784 } | 790 } |
| 785 | 791 |
| 792 base::string16 CreditCard::GetLastUsedDateForDisplay( | |
| 793 bool show_expiration_date, | |
| 794 bool show_time_detail) const { | |
| 795 DCHECK(use_count() > 0); | |
| 796 // use_count() is initialized as 1 when the card is just added. | |
| 797 if (use_count() == 1) { | |
| 798 return l10n_util::GetStringFUTF16( | |
| 799 show_expiration_date | |
| 800 ? IDS_AUTOFILL_CREDIT_CARD_ADDED_DATE_LABEL_AND_EXP | |
| 801 : IDS_AUTOFILL_CREDIT_CARD_ADDED_DATE_LABEL, | |
| 802 base::TimeFormatWithPattern(use_date(), "MMMdd")); | |
| 803 } | |
| 804 // use_count() > 1 when the card has been used in autofill. | |
| 805 if (show_time_detail) { | |
| 806 return l10n_util::GetStringFUTF16( | |
| 807 show_expiration_date | |
| 808 ? IDS_AUTOFILL_CREDIT_CARD_LAST_USED_DATE_LABEL_AND_EXP_WITH_DETAIL | |
| 809 : IDS_AUTOFILL_CREDIT_CARD_LAST_USED_DATE_LABEL_WITH_DETAIL, | |
| 810 base::TimeFormatWithPattern(use_date(), "MMMddjmm")); | |
| 811 } else { | |
| 812 return l10n_util::GetStringFUTF16( | |
| 813 show_expiration_date | |
| 814 ? IDS_AUTOFILL_CREDIT_CARD_LAST_USED_DATE_LABEL_AND_EXP_NO_DETAIL | |
| 815 : IDS_AUTOFILL_CREDIT_CARD_LAST_USED_DATE_LABEL_NO_DETAIL, | |
| 816 base::TimeFormatWithPattern(use_date(), "MMMdd")); | |
|
jungshik at Google
2017/01/23 21:43:24
There are 4 combinations here and they're combined
jiahuiguo
2017/01/26 05:46:12
Done.
| |
| 817 } | |
| 818 } | |
| 819 | |
| 786 void CreditCard::RecordAndLogUse() { | 820 void CreditCard::RecordAndLogUse() { |
| 787 UMA_HISTOGRAM_COUNTS_1000("Autofill.DaysSinceLastUse.CreditCard", | 821 UMA_HISTOGRAM_COUNTS_1000("Autofill.DaysSinceLastUse.CreditCard", |
| 788 (base::Time::Now() - use_date()).InDays()); | 822 (base::Time::Now() - use_date()).InDays()); |
| 789 RecordUse(); | 823 RecordUse(); |
| 790 } | 824 } |
| 791 | 825 |
| 792 // static | 826 // static |
| 793 bool CreditCard::ConvertMonth(const base::string16& month, | 827 bool CreditCard::ConvertMonth(const base::string16& month, |
| 794 const std::string& app_locale, | 828 const std::string& app_locale, |
| 795 int* num) { | 829 int* num) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 872 const char kDinersCard[] = "dinersCC"; | 906 const char kDinersCard[] = "dinersCC"; |
| 873 const char kDiscoverCard[] = "discoverCC"; | 907 const char kDiscoverCard[] = "discoverCC"; |
| 874 const char kGenericCard[] = "genericCC"; | 908 const char kGenericCard[] = "genericCC"; |
| 875 const char kJCBCard[] = "jcbCC"; | 909 const char kJCBCard[] = "jcbCC"; |
| 876 const char kMasterCard[] = "masterCardCC"; | 910 const char kMasterCard[] = "masterCardCC"; |
| 877 const char kMirCard[] = "mirCC"; | 911 const char kMirCard[] = "mirCC"; |
| 878 const char kUnionPay[] = "unionPayCC"; | 912 const char kUnionPay[] = "unionPayCC"; |
| 879 const char kVisaCard[] = "visaCC"; | 913 const char kVisaCard[] = "visaCC"; |
| 880 | 914 |
| 881 } // namespace autofill | 915 } // namespace autofill |
| OLD | NEW |