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/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "components/autofill/core/browser/autofill_data_util.h" |
25 #include "components/autofill/core/browser/autofill_field.h" | 26 #include "components/autofill/core/browser/autofill_field.h" |
26 #include "components/autofill/core/browser/autofill_type.h" | 27 #include "components/autofill/core/browser/autofill_type.h" |
27 #include "components/autofill/core/browser/validation.h" | 28 #include "components/autofill/core/browser/validation.h" |
28 #include "components/autofill/core/common/autofill_l10n_util.h" | 29 #include "components/autofill/core/common/autofill_l10n_util.h" |
29 #include "components/autofill/core/common/autofill_regexes.h" | 30 #include "components/autofill/core/common/autofill_regexes.h" |
30 #include "components/autofill/core/common/form_field_data.h" | 31 #include "components/autofill/core/common/form_field_data.h" |
31 #include "grit/components_scaled_resources.h" | 32 #include "grit/components_scaled_resources.h" |
32 #include "grit/components_strings.h" | 33 #include "grit/components_strings.h" |
33 #include "third_party/icu/source/common/unicode/uloc.h" | 34 #include "third_party/icu/source/common/unicode/uloc.h" |
34 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" | 35 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 258 } |
258 | 259 |
259 CreditCard::ServerStatus CreditCard::GetServerStatus() const { | 260 CreditCard::ServerStatus CreditCard::GetServerStatus() const { |
260 DCHECK_NE(LOCAL_CARD, record_type()); | 261 DCHECK_NE(LOCAL_CARD, record_type()); |
261 return server_status_; | 262 return server_status_; |
262 } | 263 } |
263 | 264 |
264 base::string16 CreditCard::GetRawInfo(ServerFieldType type) const { | 265 base::string16 CreditCard::GetRawInfo(ServerFieldType type) const { |
265 DCHECK_EQ(CREDIT_CARD, AutofillType(type).group()); | 266 DCHECK_EQ(CREDIT_CARD, AutofillType(type).group()); |
266 switch (type) { | 267 switch (type) { |
267 case CREDIT_CARD_NAME: | 268 case CREDIT_CARD_NAME_FULL: |
268 return name_on_card_; | 269 return name_on_card_; |
269 | 270 |
| 271 case CREDIT_CARD_NAME_FIRST: |
| 272 return data_util::SplitName(name_on_card_).given; |
| 273 |
| 274 case CREDIT_CARD_NAME_LAST: |
| 275 return data_util::SplitName(name_on_card_).family; |
| 276 |
270 case CREDIT_CARD_EXP_MONTH: | 277 case CREDIT_CARD_EXP_MONTH: |
271 return ExpirationMonthAsString(); | 278 return ExpirationMonthAsString(); |
272 | 279 |
273 case CREDIT_CARD_EXP_2_DIGIT_YEAR: | 280 case CREDIT_CARD_EXP_2_DIGIT_YEAR: |
274 return Expiration2DigitYearAsString(); | 281 return Expiration2DigitYearAsString(); |
275 | 282 |
276 case CREDIT_CARD_EXP_4_DIGIT_YEAR: | 283 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
277 return Expiration4DigitYearAsString(); | 284 return Expiration4DigitYearAsString(); |
278 | 285 |
279 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: { | 286 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: { |
(...skipping 25 matching lines...) Expand all Loading... |
305 default: | 312 default: |
306 // ComputeDataPresentForArray will hit this repeatedly. | 313 // ComputeDataPresentForArray will hit this repeatedly. |
307 return base::string16(); | 314 return base::string16(); |
308 } | 315 } |
309 } | 316 } |
310 | 317 |
311 void CreditCard::SetRawInfo(ServerFieldType type, | 318 void CreditCard::SetRawInfo(ServerFieldType type, |
312 const base::string16& value) { | 319 const base::string16& value) { |
313 DCHECK_EQ(CREDIT_CARD, AutofillType(type).group()); | 320 DCHECK_EQ(CREDIT_CARD, AutofillType(type).group()); |
314 switch (type) { | 321 switch (type) { |
315 case CREDIT_CARD_NAME: | 322 case CREDIT_CARD_NAME_FULL: |
316 name_on_card_ = value; | 323 name_on_card_ = value; |
317 break; | 324 break; |
318 | 325 |
319 case CREDIT_CARD_EXP_MONTH: | 326 case CREDIT_CARD_EXP_MONTH: |
320 SetExpirationMonthFromString(value, std::string()); | 327 SetExpirationMonthFromString(value, std::string()); |
321 break; | 328 break; |
322 | 329 |
323 case CREDIT_CARD_EXP_2_DIGIT_YEAR: | 330 case CREDIT_CARD_EXP_2_DIGIT_YEAR: |
324 // This is a read-only attribute. | 331 // This is a read-only attribute. |
325 break; | 332 break; |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 expiration_month_ = imported_card.expiration_month_; | 552 expiration_month_ = imported_card.expiration_month_; |
546 expiration_year_ = imported_card.expiration_year_; | 553 expiration_year_ = imported_card.expiration_year_; |
547 | 554 |
548 return true; | 555 return true; |
549 } | 556 } |
550 | 557 |
551 int CreditCard::Compare(const CreditCard& credit_card) const { | 558 int CreditCard::Compare(const CreditCard& credit_card) const { |
552 // The following CreditCard field types are the only types we store in the | 559 // The following CreditCard field types are the only types we store in the |
553 // WebDB so far, so we're only concerned with matching these types in the | 560 // WebDB so far, so we're only concerned with matching these types in the |
554 // credit card. | 561 // credit card. |
555 const ServerFieldType types[] = { CREDIT_CARD_NAME, | 562 const ServerFieldType types[] = {CREDIT_CARD_NAME_FULL, CREDIT_CARD_NUMBER, |
556 CREDIT_CARD_NUMBER, | 563 CREDIT_CARD_EXP_MONTH, |
557 CREDIT_CARD_EXP_MONTH, | 564 CREDIT_CARD_EXP_4_DIGIT_YEAR}; |
558 CREDIT_CARD_EXP_4_DIGIT_YEAR }; | |
559 for (size_t i = 0; i < arraysize(types); ++i) { | 565 for (size_t i = 0; i < arraysize(types); ++i) { |
560 int comparison = | 566 int comparison = |
561 GetRawInfo(types[i]).compare(credit_card.GetRawInfo(types[i])); | 567 GetRawInfo(types[i]).compare(credit_card.GetRawInfo(types[i])); |
562 if (comparison != 0) | 568 if (comparison != 0) |
563 return comparison; | 569 return comparison; |
564 } | 570 } |
565 | 571 |
566 int comparison = server_id_.compare(credit_card.server_id_); | 572 int comparison = server_id_.compare(credit_card.server_id_); |
567 if (comparison != 0) | 573 if (comparison != 0) |
568 return comparison; | 574 return comparison; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 expiration_month_ != 0 && | 632 expiration_month_ != 0 && |
627 expiration_year_ != 0; | 633 expiration_year_ != 0; |
628 } | 634 } |
629 | 635 |
630 bool CreditCard::IsValid() const { | 636 bool CreditCard::IsValid() const { |
631 return IsValidCreditCardNumber(number_) && | 637 return IsValidCreditCardNumber(number_) && |
632 IsValidCreditCardExpirationDate( | 638 IsValidCreditCardExpirationDate( |
633 expiration_year_, expiration_month_, base::Time::Now()); | 639 expiration_year_, expiration_month_, base::Time::Now()); |
634 } | 640 } |
635 | 641 |
| 642 // TODO(crbug.com/589536): Upload new credit card types to the server. |
636 void CreditCard::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { | 643 void CreditCard::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { |
637 supported_types->insert(CREDIT_CARD_NAME); | 644 supported_types->insert(CREDIT_CARD_NAME_FULL); |
638 supported_types->insert(CREDIT_CARD_NUMBER); | 645 supported_types->insert(CREDIT_CARD_NUMBER); |
639 supported_types->insert(CREDIT_CARD_TYPE); | 646 supported_types->insert(CREDIT_CARD_TYPE); |
640 supported_types->insert(CREDIT_CARD_EXP_MONTH); | 647 supported_types->insert(CREDIT_CARD_EXP_MONTH); |
641 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); | 648 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); |
642 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); | 649 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); |
643 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); | 650 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); |
644 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | 651 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); |
645 } | 652 } |
646 | 653 |
647 base::string16 CreditCard::ExpirationMonthAsString() const { | 654 base::string16 CreditCard::ExpirationMonthAsString() const { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | 763 *num = i + 1; // Adjust from 0-indexed to 1-indexed. |
757 return true; | 764 return true; |
758 } | 765 } |
759 } | 766 } |
760 | 767 |
761 return false; | 768 return false; |
762 } | 769 } |
763 | 770 |
764 // So we can compare CreditCards with EXPECT_EQ(). | 771 // So we can compare CreditCards with EXPECT_EQ(). |
765 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { | 772 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card) { |
766 return os | 773 return os << base::UTF16ToUTF8(credit_card.Label()) << " " |
767 << base::UTF16ToUTF8(credit_card.Label()) | 774 << credit_card.guid() << " " << credit_card.origin() << " " |
768 << " " | 775 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME_FULL)) |
769 << credit_card.guid() | 776 << " " |
770 << " " | 777 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_TYPE)) |
771 << credit_card.origin() | 778 << " " |
772 << " " | 779 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NUMBER)) |
773 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NAME)) | 780 << " " |
774 << " " | 781 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_EXP_MONTH)) |
775 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_TYPE)) | 782 << " " << base::UTF16ToUTF8( |
776 << " " | 783 credit_card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
777 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_NUMBER)) | |
778 << " " | |
779 << base::UTF16ToUTF8(credit_card.GetRawInfo(CREDIT_CARD_EXP_MONTH)) | |
780 << " " | |
781 << base::UTF16ToUTF8( | |
782 credit_card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
783 } | 784 } |
784 | 785 |
785 // These values must match the values in WebKitPlatformSupportImpl in | 786 // These values must match the values in WebKitPlatformSupportImpl in |
786 // webkit/glue. We send these strings to WebKit, which then asks | 787 // webkit/glue. We send these strings to WebKit, which then asks |
787 // WebKitPlatformSupportImpl to load the image data. | 788 // WebKitPlatformSupportImpl to load the image data. |
788 const char kAmericanExpressCard[] = "americanExpressCC"; | 789 const char kAmericanExpressCard[] = "americanExpressCC"; |
789 const char kDinersCard[] = "dinersCC"; | 790 const char kDinersCard[] = "dinersCC"; |
790 const char kDiscoverCard[] = "discoverCC"; | 791 const char kDiscoverCard[] = "discoverCC"; |
791 const char kGenericCard[] = "genericCC"; | 792 const char kGenericCard[] = "genericCC"; |
792 const char kJCBCard[] = "jcbCC"; | 793 const char kJCBCard[] = "jcbCC"; |
793 const char kMasterCard[] = "masterCardCC"; | 794 const char kMasterCard[] = "masterCardCC"; |
794 const char kUnionPay[] = "unionPayCC"; | 795 const char kUnionPay[] = "unionPayCC"; |
795 const char kVisaCard[] = "visaCC"; | 796 const char kVisaCard[] = "visaCC"; |
796 | 797 |
797 } // namespace autofill | 798 } // namespace autofill |
OLD | NEW |