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 18 matching lines...) Expand all Loading... |
29 #include "third_party/icu/source/common/unicode/uloc.h" | 29 #include "third_party/icu/source/common/unicode/uloc.h" |
30 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" | 30 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" |
31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
32 | 32 |
33 namespace autofill { | 33 namespace autofill { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const base::char16 kCreditCardObfuscationSymbol = '*'; | 37 const base::char16 kCreditCardObfuscationSymbol = '*'; |
38 | 38 |
39 // This is the maximum obfuscated symbols displayed. | |
40 // It is introduced to avoid rare cases where the credit card number is | |
41 // too large and fills the screen. | |
42 const size_t kMaxObfuscationSize = 20; | |
43 | |
44 bool ConvertYear(const base::string16& year, int* num) { | 39 bool ConvertYear(const base::string16& year, int* num) { |
45 // If the |year| is empty, clear the stored value. | 40 // If the |year| is empty, clear the stored value. |
46 if (year.empty()) { | 41 if (year.empty()) { |
47 *num = 0; | 42 *num = 0; |
48 return true; | 43 return true; |
49 } | 44 } |
50 | 45 |
51 // Try parsing the |year| as a number. | 46 // Try parsing the |year| as a number. |
52 if (base::StringToInt(year, num)) | 47 if (base::StringToInt(year, num)) |
53 return true; | 48 return true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 if (lowercased_month == base::StringToLowerASCII(icu_month)) { | 96 if (lowercased_month == base::StringToLowerASCII(icu_month)) { |
102 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | 97 *num = i + 1; // Adjust from 0-indexed to 1-indexed. |
103 return true; | 98 return true; |
104 } | 99 } |
105 } | 100 } |
106 | 101 |
107 *num = 0; | 102 *num = 0; |
108 return false; | 103 return false; |
109 } | 104 } |
110 | 105 |
| 106 base::string16 TypeForFill(const std::string& type) { |
| 107 if (type == kAmericanExpressCard) |
| 108 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); |
| 109 if (type == kDinersCard) |
| 110 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); |
| 111 if (type == kDiscoverCard) |
| 112 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); |
| 113 if (type == kJCBCard) |
| 114 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); |
| 115 if (type == kMasterCard) |
| 116 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD); |
| 117 if (type == kUnionPay) |
| 118 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_UNION_PAY); |
| 119 if (type == kVisaCard) |
| 120 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); |
| 121 |
| 122 // If you hit this DCHECK, the above list of cases needs to be updated to |
| 123 // include a new card. |
| 124 DCHECK_EQ(kGenericCard, type); |
| 125 return base::string16(); |
| 126 } |
| 127 |
111 } // namespace | 128 } // namespace |
112 | 129 |
113 CreditCard::CreditCard(const std::string& guid, const std::string& origin) | 130 CreditCard::CreditCard(const std::string& guid, const std::string& origin) |
114 : AutofillDataModel(guid, origin), | 131 : AutofillDataModel(guid, origin), |
115 record_type_(LOCAL_CARD), | 132 record_type_(LOCAL_CARD), |
116 type_(kGenericCard), | 133 type_(kGenericCard), |
117 expiration_month_(0), | 134 expiration_month_(0), |
118 expiration_year_(0) { | 135 expiration_year_(0) { |
119 } | 136 } |
120 | 137 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 170 |
154 // static | 171 // static |
155 const base::string16 CreditCard::StripSeparators(const base::string16& number) { | 172 const base::string16 CreditCard::StripSeparators(const base::string16& number) { |
156 base::string16 stripped; | 173 base::string16 stripped; |
157 base::RemoveChars(number, base::ASCIIToUTF16("- "), &stripped); | 174 base::RemoveChars(number, base::ASCIIToUTF16("- "), &stripped); |
158 return stripped; | 175 return stripped; |
159 } | 176 } |
160 | 177 |
161 // static | 178 // static |
162 base::string16 CreditCard::TypeForDisplay(const std::string& type) { | 179 base::string16 CreditCard::TypeForDisplay(const std::string& type) { |
163 if (type == kAmericanExpressCard) | 180 if (kGenericCard == type) |
164 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); | 181 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_GENERIC); |
165 if (type == kDinersCard) | 182 return ::autofill::TypeForFill(type); |
166 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); | |
167 if (type == kDiscoverCard) | |
168 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); | |
169 if (type == kJCBCard) | |
170 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); | |
171 if (type == kMasterCard) | |
172 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD); | |
173 if (type == kUnionPay) | |
174 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_UNION_PAY); | |
175 if (type == kVisaCard) | |
176 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); | |
177 | |
178 // If you hit this DCHECK, the above list of cases needs to be updated to | |
179 // include a new card. | |
180 DCHECK_EQ(kGenericCard, type); | |
181 return base::string16(); | |
182 } | 183 } |
183 | 184 |
184 // This method is not compiled on iOS because the resources are not used and | 185 // This method is not compiled on iOS because the resources are not used and |
185 // should not be shipped. | 186 // should not be shipped. |
186 #if !defined(OS_IOS) | 187 #if !defined(OS_IOS) |
187 // static | 188 // static |
188 int CreditCard::IconResourceId(const std::string& type) { | 189 int CreditCard::IconResourceId(const std::string& type) { |
189 if (type == kAmericanExpressCard) | 190 if (type == kAmericanExpressCard) |
190 return IDR_AUTOFILL_CC_AMEX; | 191 return IDR_AUTOFILL_CC_AMEX; |
191 if (type == kDinersCard) | 192 if (type == kDinersCard) |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 325 |
325 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: { | 326 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: { |
326 base::string16 month = ExpirationMonthAsString(); | 327 base::string16 month = ExpirationMonthAsString(); |
327 base::string16 year = Expiration4DigitYearAsString(); | 328 base::string16 year = Expiration4DigitYearAsString(); |
328 if (!month.empty() && !year.empty()) | 329 if (!month.empty() && !year.empty()) |
329 return month + base::ASCIIToUTF16("/") + year; | 330 return month + base::ASCIIToUTF16("/") + year; |
330 return base::string16(); | 331 return base::string16(); |
331 } | 332 } |
332 | 333 |
333 case CREDIT_CARD_TYPE: | 334 case CREDIT_CARD_TYPE: |
334 return TypeForDisplay(); | 335 return TypeForFill(); |
335 | 336 |
336 case CREDIT_CARD_NUMBER: | 337 case CREDIT_CARD_NUMBER: |
337 return number_; | 338 return number_; |
338 | 339 |
339 case CREDIT_CARD_VERIFICATION_CODE: | 340 case CREDIT_CARD_VERIFICATION_CODE: |
340 // Chrome doesn't store credit card verification codes. | 341 // Chrome doesn't store credit card verification codes. |
341 return base::string16(); | 342 return base::string16(); |
342 | 343 |
343 default: | 344 default: |
344 // ComputeDataPresentForArray will hit this repeatedly. | 345 // ComputeDataPresentForArray will hit this repeatedly. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 month == expiration_month_) { | 434 month == expiration_month_) { |
434 matching_types->insert(CREDIT_CARD_EXP_MONTH); | 435 matching_types->insert(CREDIT_CARD_EXP_MONTH); |
435 } | 436 } |
436 } | 437 } |
437 | 438 |
438 const base::string16 CreditCard::Label() const { | 439 const base::string16 CreditCard::Label() const { |
439 base::string16 label; | 440 base::string16 label; |
440 if (number().empty()) | 441 if (number().empty()) |
441 return name_on_card_; // No CC number, return name only. | 442 return name_on_card_; // No CC number, return name only. |
442 | 443 |
443 base::string16 obfuscated_cc_number = ObfuscatedNumber(); | 444 base::string16 obfuscated_cc_number = TypeAndLastFourDigits(); |
444 if (!expiration_month_ || !expiration_year_) | 445 if (!expiration_month_ || !expiration_year_) |
445 return obfuscated_cc_number; // No expiration date set. | 446 return obfuscated_cc_number; // No expiration date set. |
446 | 447 |
447 // TODO(georgey): Internationalize date. | 448 // TODO(georgey): Internationalize date. |
448 base::string16 formatted_date(ExpirationMonthAsString()); | 449 base::string16 formatted_date(ExpirationMonthAsString()); |
449 formatted_date.append(base::ASCIIToUTF16("/")); | 450 formatted_date.append(base::ASCIIToUTF16("/")); |
450 formatted_date.append(Expiration4DigitYearAsString()); | 451 formatted_date.append(Expiration4DigitYearAsString()); |
451 | 452 |
452 label = l10n_util::GetStringFUTF16(IDS_CREDIT_CARD_NUMBER_PREVIEW_FORMAT, | 453 label = l10n_util::GetStringFUTF16(IDS_CREDIT_CARD_NUMBER_PREVIEW_FORMAT, |
453 obfuscated_cc_number, | 454 obfuscated_cc_number, |
(...skipping 14 matching lines...) Expand all Loading... |
468 int num = 0; | 469 int num = 0; |
469 bool converted = false; | 470 bool converted = false; |
470 converted = base::StringToInt(year_month[0], &num); | 471 converted = base::StringToInt(year_month[0], &num); |
471 DCHECK(converted); | 472 DCHECK(converted); |
472 SetExpirationYear(num); | 473 SetExpirationYear(num); |
473 converted = base::StringToInt(year_month[1], &num); | 474 converted = base::StringToInt(year_month[1], &num); |
474 DCHECK(converted); | 475 DCHECK(converted); |
475 SetExpirationMonth(num); | 476 SetExpirationMonth(num); |
476 } | 477 } |
477 | 478 |
478 base::string16 CreditCard::ObfuscatedNumber() const { | |
479 // If the number is four or less digits, there's no need to obfuscate it. | |
480 if (number_.size() <= 4) | |
481 return number_; | |
482 | |
483 base::string16 number = StripSeparators(number_); | |
484 | |
485 // Avoid making very long obfuscated numbers. | |
486 size_t obfuscated_digits = std::min(kMaxObfuscationSize, number.size() - 4); | |
487 base::string16 result(obfuscated_digits, kCreditCardObfuscationSymbol); | |
488 return result.append(LastFourDigits()); | |
489 } | |
490 | |
491 base::string16 CreditCard::LastFourDigits() const { | 479 base::string16 CreditCard::LastFourDigits() const { |
492 static const size_t kNumLastDigits = 4; | 480 static const size_t kNumLastDigits = 4; |
493 | 481 |
494 base::string16 number = StripSeparators(number_); | 482 base::string16 number = StripSeparators(number_); |
495 if (number.size() <= kNumLastDigits) | 483 if (number.size() <= kNumLastDigits) |
496 return number; | 484 return number; |
497 | 485 |
498 return number.substr(number.size() - kNumLastDigits, kNumLastDigits); | 486 return number.substr(number.size() - kNumLastDigits, kNumLastDigits); |
499 } | 487 } |
500 | 488 |
501 base::string16 CreditCard::TypeForDisplay() const { | 489 base::string16 CreditCard::TypeForDisplay() const { |
502 return CreditCard::TypeForDisplay(type_); | 490 return CreditCard::TypeForDisplay(type_); |
503 } | 491 } |
504 | 492 |
505 base::string16 CreditCard::TypeAndLastFourDigits() const { | 493 base::string16 CreditCard::TypeAndLastFourDigits() const { |
506 base::string16 type = TypeForDisplay(); | 494 base::string16 type = TypeForDisplay(); |
507 // TODO(estade): type may be empty, we probably want to return | |
508 // "Card - 1234" or something in that case. | |
509 | 495 |
510 base::string16 digits = LastFourDigits(); | 496 base::string16 digits = LastFourDigits(); |
511 if (digits.empty()) | 497 if (digits.empty()) |
512 return type; | 498 return type; |
513 | 499 |
514 // TODO(estade): i18n. | 500 // TODO(estade): i18n. |
515 return type + base::ASCIIToUTF16(" - ") + digits; | 501 return type + base::ASCIIToUTF16(" - ") + digits; |
516 } | 502 } |
517 | 503 |
518 void CreditCard::operator=(const CreditCard& credit_card) { | 504 void CreditCard::operator=(const CreditCard& credit_card) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 | 610 |
625 base::string16 month = base::IntToString16(expiration_month_); | 611 base::string16 month = base::IntToString16(expiration_month_); |
626 if (expiration_month_ >= 10) | 612 if (expiration_month_ >= 10) |
627 return month; | 613 return month; |
628 | 614 |
629 base::string16 zero = base::ASCIIToUTF16("0"); | 615 base::string16 zero = base::ASCIIToUTF16("0"); |
630 zero.append(month); | 616 zero.append(month); |
631 return zero; | 617 return zero; |
632 } | 618 } |
633 | 619 |
| 620 base::string16 CreditCard::TypeForFill() const { |
| 621 return ::autofill::TypeForFill(type_); |
| 622 } |
| 623 |
634 base::string16 CreditCard::Expiration4DigitYearAsString() const { | 624 base::string16 CreditCard::Expiration4DigitYearAsString() const { |
635 if (expiration_year_ == 0) | 625 if (expiration_year_ == 0) |
636 return base::string16(); | 626 return base::string16(); |
637 | 627 |
638 return base::IntToString16(Expiration4DigitYear()); | 628 return base::IntToString16(Expiration4DigitYear()); |
639 } | 629 } |
640 | 630 |
641 base::string16 CreditCard::Expiration2DigitYearAsString() const { | 631 base::string16 CreditCard::Expiration2DigitYearAsString() const { |
642 if (expiration_year_ == 0) | 632 if (expiration_year_ == 0) |
643 return base::string16(); | 633 return base::string16(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 const char* const kAmericanExpressCard = "americanExpressCC"; | 700 const char* const kAmericanExpressCard = "americanExpressCC"; |
711 const char* const kDinersCard = "dinersCC"; | 701 const char* const kDinersCard = "dinersCC"; |
712 const char* const kDiscoverCard = "discoverCC"; | 702 const char* const kDiscoverCard = "discoverCC"; |
713 const char* const kGenericCard = "genericCC"; | 703 const char* const kGenericCard = "genericCC"; |
714 const char* const kJCBCard = "jcbCC"; | 704 const char* const kJCBCard = "jcbCC"; |
715 const char* const kMasterCard = "masterCardCC"; | 705 const char* const kMasterCard = "masterCardCC"; |
716 const char* const kUnionPay = "unionPayCC"; | 706 const char* const kUnionPay = "unionPayCC"; |
717 const char* const kVisaCard = "visaCC"; | 707 const char* const kVisaCard = "visaCC"; |
718 | 708 |
719 } // namespace autofill | 709 } // namespace autofill |
OLD | NEW |