Chromium Code Reviews| Index: components/autofill/core/browser/credit_card.cc |
| diff --git a/components/autofill/core/browser/credit_card.cc b/components/autofill/core/browser/credit_card.cc |
| index 1503a437d38f38996a4dd88a084f9ee46c89851b..bd63bbad8fe1b24d63e3358dc8d8f87cd5d4b9d9 100644 |
| --- a/components/autofill/core/browser/credit_card.cc |
| +++ b/components/autofill/core/browser/credit_card.cc |
| @@ -35,6 +35,8 @@ |
| #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" |
| #include "ui/base/l10n/l10n_util.h" |
| +using base::ASCIIToUTF16; |
| + |
| namespace autofill { |
| const base::char16 kMidlineEllipsis[] = { 0x22ef, 0 }; |
| @@ -118,7 +120,7 @@ CreditCard::~CreditCard() {} |
| // static |
| const base::string16 CreditCard::StripSeparators(const base::string16& number) { |
| base::string16 stripped; |
| - base::RemoveChars(number, base::ASCIIToUTF16("- "), &stripped); |
| + base::RemoveChars(number, ASCIIToUTF16("- "), &stripped); |
| return stripped; |
| } |
| @@ -290,7 +292,7 @@ base::string16 CreditCard::GetRawInfo(ServerFieldType type) const { |
| base::string16 month = ExpirationMonthAsString(); |
| base::string16 year = Expiration2DigitYearAsString(); |
| if (!month.empty() && !year.empty()) |
| - return month + base::ASCIIToUTF16("/") + year; |
| + return month + ASCIIToUTF16("/") + year; |
| return base::string16(); |
| } |
| @@ -298,7 +300,7 @@ base::string16 CreditCard::GetRawInfo(ServerFieldType type) const { |
| base::string16 month = ExpirationMonthAsString(); |
| base::string16 year = Expiration4DigitYearAsString(); |
| if (!month.empty() && !year.empty()) |
| - return month + base::ASCIIToUTF16("/") + year; |
| + return month + ASCIIToUTF16("/") + year; |
| return base::string16(); |
| } |
| @@ -331,7 +333,7 @@ void CreditCard::SetRawInfo(ServerFieldType type, |
| break; |
| case CREDIT_CARD_EXP_2_DIGIT_YEAR: |
| - // This is a read-only attribute. |
| + SetExpirationYearFromString(value); |
| break; |
| case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| @@ -339,11 +341,11 @@ void CreditCard::SetRawInfo(ServerFieldType type, |
| break; |
| case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: |
| - // This is a read-only attribute. |
| + SetExpirationDateFromString(value); |
| break; |
| case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: |
| - // This is a read-only attribute. |
| + SetExpirationDateFromString(value); |
| break; |
| case CREDIT_CARD_TYPE: |
| @@ -431,7 +433,7 @@ const std::pair<base::string16, base::string16> CreditCard::LabelPieces() |
| return std::make_pair(obfuscated_cc_number, base::string16()); |
| base::string16 formatted_date(ExpirationMonthAsString()); |
| - formatted_date.append(base::ASCIIToUTF16("/")); |
| + formatted_date.append(ASCIIToUTF16("/")); |
| formatted_date.append(Expiration4DigitYearAsString()); |
| base::string16 separator = |
| @@ -445,8 +447,7 @@ void CreditCard::SetInfoForMonthInputType(const base::string16& value) { |
| return; |
| std::vector<base::StringPiece16> year_month = base::SplitStringPiece( |
| - value, base::ASCIIToUTF16("-"), |
| - base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + value, ASCIIToUTF16("-"), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| DCHECK_EQ(2u, year_month.size()); |
| int num = 0; |
| bool converted = false; |
| @@ -465,9 +466,16 @@ void CreditCard::SetExpirationMonth(int expiration_month) { |
| } |
| void CreditCard::SetExpirationYear(int expiration_year) { |
| - if (expiration_year != 0 && |
| - (expiration_year < 2006 || expiration_year > 10000)) |
| + if (expiration_year != 0 && expiration_year > 10000) |
| return; |
| + |
| + // Will normalize 2-digit years to the 4-digit version. |
| + if (expiration_year > 0 && expiration_year < 100) { |
| + base::Time::Exploded now_exploded; |
| + base::Time::Now().LocalExplode(&now_exploded); |
| + expiration_year += (now_exploded.year / 100) * 100; |
| + } |
| + |
| expiration_year_ = expiration_year; |
| } |
| @@ -672,7 +680,7 @@ base::string16 CreditCard::ExpirationMonthAsString() const { |
| if (expiration_month_ >= 10) |
| return month; |
| - base::string16 zero = base::ASCIIToUTF16("0"); |
| + base::string16 zero = ASCIIToUTF16("0"); |
| zero.append(month); |
| return zero; |
| } |
| @@ -716,6 +724,48 @@ void CreditCard::SetExpirationYearFromString(const base::string16& text) { |
| SetExpirationYear(year); |
| } |
| +void CreditCard::SetExpirationDateFromString(const base::string16& text) { |
| + // Check that it fits the supported patterns: myy, myyyy, mmyy, mmyyyy, m-yy, |
|
sebsg
2016/07/08 10:01:45
nit: it -> the |text|
Mathieu
2016/07/08 14:27:04
Done.
|
| + // mm-yy, m-yyyy and mm-yyyy. Separators: -, / and |. |
| + if (!MatchesPattern(text, base::UTF8ToUTF16("^[0-9]{1,2}[-/|]?[0-9]{2,4}$"))) |
| + return; |
| + |
| + base::string16 month; |
| + base::string16 year; |
| + |
| + // Check for a separator. |
| + base::string16 found_separator; |
| + const std::vector<const base::string16> kSeparators{ |
| + ASCIIToUTF16("-"), ASCIIToUTF16("/"), ASCIIToUTF16("|")}; |
| + for (const base::string16& separator : kSeparators) { |
| + if (text.find(separator) != base::string16::npos) |
| + found_separator = separator; |
| + } |
| + |
| + if (!found_separator.empty()) { |
| + std::vector<base::string16> month_year = base::SplitString( |
| + text, found_separator, base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + DCHECK_EQ(2u, month_year.size()); |
| + month = month_year[0]; |
| + year = month_year[1]; |
| + } else { |
| + // There is no separator, the format is either myy, mmyy, myyyy or mmyyyy. |
| + // Month is either 1 or 2 digits. Year is always even number of digits. |
| + base::string16::size_type month_length = text.size() % 2 == 0 ? 2 : 1; |
| + month = text.substr(0, month_length); |
| + year = text.substr(month_length); |
| + } |
| + |
| + int num = 0; |
| + bool converted = false; |
| + converted = base::StringToInt(month, &num); |
| + DCHECK(converted); |
| + SetExpirationMonth(num); |
| + converted = base::StringToInt(year, &num); |
| + DCHECK(converted); |
| + SetExpirationYear(num); |
| +} |
| + |
| void CreditCard::SetNumber(const base::string16& number) { |
| number_ = number; |
| @@ -768,10 +818,10 @@ bool CreditCard::ConvertMonth(const base::string16& month, |
| // Some abbreviations have . at the end (e.g., "janv." in French). We don't |
| // care about matching that. |
| base::string16 trimmed_month; |
| - base::TrimString(month, base::ASCIIToUTF16("."), &trimmed_month); |
| + base::TrimString(month, ASCIIToUTF16("."), &trimmed_month); |
| for (int32_t i = 0; i < num_months; ++i) { |
| base::string16 icu_month(months[i].getBuffer(), months[i].length()); |
| - base::TrimString(icu_month, base::ASCIIToUTF16("."), &icu_month); |
| + base::TrimString(icu_month, ASCIIToUTF16("."), &icu_month); |
| if (compare.StringsEqual(icu_month, trimmed_month)) { |
| *num = i + 1; // Adjust from 0-indexed to 1-indexed. |
| return true; |