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 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <ostream> | 10 #include <ostream> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/guid.h" | 14 #include "base/guid.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "components/autofill/core/browser/autofill_field.h" | 22 #include "components/autofill/core/browser/autofill_field.h" |
| 23 #include "components/autofill/core/browser/autofill_regexes.h" | 23 #include "components/autofill/core/browser/autofill_regexes.h" |
| 24 #include "components/autofill/core/browser/autofill_type.h" | 24 #include "components/autofill/core/browser/autofill_type.h" |
| 25 #include "components/autofill/core/browser/validation.h" | 25 #include "components/autofill/core/browser/validation.h" |
| 26 #include "components/autofill/core/common/autofill_l10n_util.h" | |
| 26 #include "components/autofill/core/common/form_field_data.h" | 27 #include "components/autofill/core/common/form_field_data.h" |
| 27 #include "grit/components_scaled_resources.h" | 28 #include "grit/components_scaled_resources.h" |
| 28 #include "grit/components_strings.h" | 29 #include "grit/components_strings.h" |
| 29 #include "third_party/icu/source/common/unicode/uloc.h" | 30 #include "third_party/icu/source/common/unicode/uloc.h" |
| 30 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" | 31 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| 32 | 33 |
| 33 namespace autofill { | 34 namespace autofill { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 const base::char16 kCreditCardObfuscationSymbol = '*'; | 38 const base::char16 kCreditCardObfuscationSymbol = '*'; |
| 38 | 39 |
| 39 bool ConvertYear(const base::string16& year, int* num) { | 40 bool ConvertYear(const base::string16& year, int* num) { |
| 40 // If the |year| is empty, clear the stored value. | 41 // If the |year| is empty, clear the stored value. |
| 41 if (year.empty()) { | 42 if (year.empty()) { |
| 42 *num = 0; | 43 *num = 0; |
| 43 return true; | 44 return true; |
| 44 } | 45 } |
| 45 | 46 |
| 46 // Try parsing the |year| as a number. | 47 // Try parsing the |year| as a number. |
| 47 if (base::StringToInt(year, num)) | 48 if (base::StringToInt(year, num)) |
| 48 return true; | 49 return true; |
| 49 | 50 |
| 50 *num = 0; | 51 *num = 0; |
| 51 return false; | 52 return false; |
| 52 } | 53 } |
| 53 | 54 |
| 54 bool ConvertMonth(const base::string16& month, | |
| 55 const std::string& app_locale, | |
| 56 int* num) { | |
| 57 // If the |month| is empty, clear the stored value. | |
| 58 if (month.empty()) { | |
| 59 *num = 0; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 // Try parsing the |month| as a number. | |
| 64 if (base::StringToInt(month, num)) | |
| 65 return true; | |
| 66 | |
| 67 // If the locale is unknown, give up. | |
| 68 if (app_locale.empty()) | |
| 69 return false; | |
| 70 | |
| 71 // Otherwise, try parsing the |month| as a named month, e.g. "January" or | |
| 72 // "Jan". | |
| 73 base::string16 lowercased_month = base::StringToLowerASCII(month); | |
| 74 | |
| 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 = base::string16(months[i].getBuffer(), | |
| 85 months[i].length()); | |
| 86 if (lowercased_month == base::StringToLowerASCII(icu_month)) { | |
| 87 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 88 return true; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 months = date_format_symbols.getShortMonths(num_months); | |
| 93 for (int32_t i = 0; i < num_months; ++i) { | |
| 94 const base::string16 icu_month = base::string16(months[i].getBuffer(), | |
| 95 months[i].length()); | |
| 96 if (lowercased_month == base::StringToLowerASCII(icu_month)) { | |
| 97 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 98 return true; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 *num = 0; | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 base::string16 TypeForFill(const std::string& type) { | 55 base::string16 TypeForFill(const std::string& type) { |
| 107 if (type == kAmericanExpressCard) | 56 if (type == kAmericanExpressCard) |
| 108 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); | 57 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); |
| 109 if (type == kDinersCard) | 58 if (type == kDinersCard) |
| 110 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); | 59 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); |
| 111 if (type == kDiscoverCard) | 60 if (type == kDiscoverCard) |
| 112 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); | 61 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); |
| 113 if (type == kJCBCard) | 62 if (type == kJCBCard) |
| 114 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); | 63 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); |
| 115 if (type == kMasterCard) | 64 if (type == kMasterCard) |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 750 | 699 |
| 751 void CreditCard::SetNumber(const base::string16& number) { | 700 void CreditCard::SetNumber(const base::string16& number) { |
| 752 number_ = number; | 701 number_ = number; |
| 753 | 702 |
| 754 // Set the type based on the card number, but only for full numbers, not | 703 // Set the type based on the card number, but only for full numbers, not |
| 755 // when we have masked cards from the server (last 4 digits). | 704 // when we have masked cards from the server (last 4 digits). |
| 756 if (record_type_ != MASKED_SERVER_CARD) | 705 if (record_type_ != MASKED_SERVER_CARD) |
| 757 type_ = GetCreditCardType(StripSeparators(number_)); | 706 type_ = GetCreditCardType(StripSeparators(number_)); |
| 758 } | 707 } |
| 759 | 708 |
| 709 // static | |
| 710 bool CreditCard::ConvertMonth(const base::string16& month, | |
| 711 const std::string& app_locale, | |
| 712 int* num) { | |
| 713 // If the |month| is empty, clear the stored value. | |
| 714 if (month.empty()) { | |
| 715 *num = 0; | |
| 716 return true; | |
|
Lei Zhang
2015/04/13 19:59:13
I wonder if this should return false. It's not a s
Evan Stade
2015/04/13 21:34:13
good point. It makes more sense if this check is m
| |
| 717 } | |
| 718 | |
| 719 // Try parsing the |month| as a number. | |
| 720 if (base::StringToInt(month, num)) | |
| 721 return true; | |
| 722 | |
| 723 // If the locale is unknown, give up. | |
| 724 if (app_locale.empty()) | |
| 725 return false; | |
| 726 | |
| 727 // Otherwise, try parsing the |month| as a named month, e.g. "January" or | |
| 728 // "Jan". | |
| 729 l10n::CaseInsensitiveCompare compare; | |
| 730 UErrorCode status = U_ZERO_ERROR; | |
| 731 icu::Locale locale(app_locale.c_str()); | |
| 732 icu::DateFormatSymbols date_format_symbols(locale, status); | |
| 733 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || | |
| 734 status == U_USING_DEFAULT_WARNING); | |
| 735 | |
| 736 int32_t num_months; | |
| 737 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months); | |
| 738 for (int32_t i = 0; i < num_months; ++i) { | |
| 739 const base::string16 icu_month(months[i].getBuffer(), months[i].length()); | |
| 740 if (compare.StringsEqual(icu_month, month)) { | |
| 741 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 742 return true; | |
| 743 } | |
| 744 } | |
| 745 | |
| 746 months = date_format_symbols.getShortMonths(num_months); | |
| 747 // Some abbreviations have . at the end (e.g., "janv." in French). We don't | |
| 748 // care about matching that. | |
| 749 base::string16 trimmed_month; | |
| 750 base::TrimString(month, base::ASCIIToUTF16("."), &trimmed_month); | |
| 751 for (int32_t i = 0; i < num_months; ++i) { | |
| 752 base::string16 icu_month(months[i].getBuffer(), months[i].length()); | |
| 753 base::TrimString(icu_month, base::ASCIIToUTF16("."), &icu_month); | |
| 754 if (compare.StringsEqual(icu_month, trimmed_month)) { | |
| 755 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 756 return true; | |
| 757 } | |
| 758 } | |
| 759 | |
| 760 *num = 0; | |
| 761 return false; | |
| 762 } | |
| 763 | |
| 760 // So we can compare CreditCards with EXPECT_EQ(). | 764 // So we can compare CreditCards with EXPECT_EQ(). |
| 761 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { | 765 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { |
| 762 return os | 766 return os |
| 763 << base::UTF16ToUTF8(credit_card.Label()) | 767 << base::UTF16ToUTF8(credit_card.Label()) |
| 764 << " " | 768 << " " |
| 765 << credit_card.guid() | 769 << credit_card.guid() |
| 766 << " " | 770 << " " |
| 767 << credit_card.origin() | 771 << credit_card.origin() |
| 768 << " " | 772 << " " |
| 769 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME)) | 773 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME)) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 784 const char* const kAmericanExpressCard = "americanExpressCC"; | 788 const char* const kAmericanExpressCard = "americanExpressCC"; |
| 785 const char* const kDinersCard = "dinersCC"; | 789 const char* const kDinersCard = "dinersCC"; |
| 786 const char* const kDiscoverCard = "discoverCC"; | 790 const char* const kDiscoverCard = "discoverCC"; |
| 787 const char* const kGenericCard = "genericCC"; | 791 const char* const kGenericCard = "genericCC"; |
| 788 const char* const kJCBCard = "jcbCC"; | 792 const char* const kJCBCard = "jcbCC"; |
| 789 const char* const kMasterCard = "masterCardCC"; | 793 const char* const kMasterCard = "masterCardCC"; |
| 790 const char* const kUnionPay = "unionPayCC"; | 794 const char* const kUnionPay = "unionPayCC"; |
| 791 const char* const kVisaCard = "visaCC"; | 795 const char* const kVisaCard = "visaCC"; |
| 792 | 796 |
| 793 } // namespace autofill | 797 } // namespace autofill |
| OLD | NEW |