1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_ 6 #define CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_ 7 #pragma once 8 9 #include <ostream> 10 #include <string> 11 #include <vector> 12 13 #include "base/string16.h" 14 #include "chrome/browser/autofill/field_types.h" 15 #include "chrome/browser/autofill/form_group.h" 16 17 // A form group that stores credit card information. 18 class CreditCard : public FormGroup { 19 public: 20 explicit CreditCard(const std::string& guid); 21 22 // For use in STL containers. 23 CreditCard(); 24 CreditCard(const CreditCard& credit_card); 25 virtual ~CreditCard(); 26 27 // FormGroup implementation: 28 virtual void GetPossibleFieldTypes(const string16& text, 29 FieldTypeSet* possible_types) const; 30 virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const; 31 virtual string16 GetInfo(AutofillFieldType type) const; 32 virtual void SetInfo(AutofillFieldType type, const string16& value); 33 // Credit card preview summary, for example: ******1234, Exp: 01/2020 34 virtual const string16 Label() const; 35 36 // Special method to set value for HTML5 month input type. 37 void SetInfoForMonthInputType(const string16& value); 38 39 // The number altered for display, for example: ******1234 40 string16 ObfuscatedNumber() const; 41 // The last four digits of the credit card number. 42 string16 LastFourDigits() const; 43 type()44 const std::string& type() const { return type_; } 45 46 // The guid is the primary identifier for |CreditCard| objects. guid()47 const std::string guid() const { return guid_; } set_guid(const std::string & guid)48 void set_guid(const std::string& guid) { guid_ = guid; } 49 50 // For use in STL containers. 51 void operator=(const CreditCard& credit_card); 52 53 // Comparison for Sync. Returns 0 if the credit card is the same as |this|, 54 // or < 0, or > 0 if it is different. The implied ordering can be used for 55 // culling duplicates. The ordering is based on collation order of the 56 // textual contents of the fields. 57 // GUIDs, labels, and unique IDs are not compared, only the values of the 58 // credit cards themselves. 59 int Compare(const CreditCard& credit_card) const; 60 61 // Used by tests. 62 bool operator==(const CreditCard& credit_card) const; 63 bool operator!=(const CreditCard& credit_card) const; 64 65 // Return a version of |number| that has any separator characters removed. 66 static const string16 StripSeparators(const string16& number); 67 68 // Returns true if |text| looks like a valid credit card number. 69 // Uses the Luhn formula to validate the number. 70 static bool IsValidCreditCardNumber(const string16& text); 71 72 // Returns true if there are no values (field types) set. 73 bool IsEmpty() const; 74 75 // Returns the credit card number. number()76 const string16& number() const { return number_; } 77 78 private: 79 // The month and year are zero if not present. Expiration4DigitYear()80 int Expiration4DigitYear() const { return expiration_year_; } Expiration2DigitYear()81 int Expiration2DigitYear() const { return expiration_year_ % 100; } 82 string16 ExpirationMonthAsString() const; 83 string16 Expiration4DigitYearAsString() const; 84 string16 Expiration2DigitYearAsString() const; 85 86 // Sets |expiration_month_| to the integer conversion of |text|. 87 void SetExpirationMonthFromString(const string16& text); 88 89 // Sets |expiration_year_| to the integer conversion of |text|. 90 void SetExpirationYearFromString(const string16& text); 91 92 // Sets |number_| to the stripped version of |number|, containing only digits. 93 void SetNumber(const string16& number); 94 95 // These setters verify that the month and year are within appropriate 96 // ranges. 97 void SetExpirationMonth(int expiration_month); 98 void SetExpirationYear(int expiration_year); 99 100 // Returns true if |text| matches the name on the card. The comparison is 101 // case-insensitive. 102 bool IsNameOnCard(const string16& text) const; 103 104 // Returns true if |text| matches the card number. 105 bool IsNumber(const string16& text) const; 106 107 // Returns true if |text| matches the expiration month of the card. 108 bool IsExpirationMonth(const string16& text) const; 109 110 // Returns true if the integer value of |text| matches the 2-digit expiration 111 // year. 112 bool Is2DigitExpirationYear(const string16& text) const; 113 114 // Returns true if the integer value of |text| matches the 4-digit expiration 115 // year. 116 bool Is4DigitExpirationYear(const string16& text) const; 117 118 string16 number_; // The credit card number. 119 string16 name_on_card_; // The cardholder's name. 120 std::string type_; // The type of the card. 121 122 // These members are zero if not present. 123 int expiration_month_; 124 int expiration_year_; 125 126 // The guid of this credit card. 127 std::string guid_; 128 }; 129 130 // So we can compare CreditCards with EXPECT_EQ(). 131 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card); 132 133 // The string identifiers for credit card icon resources. 134 extern const char* const kAmericanExpressCard; 135 extern const char* const kDinersCard; 136 extern const char* const kDiscoverCard; 137 extern const char* const kGenericCard; 138 extern const char* const kJCBCard; 139 extern const char* const kMasterCard; 140 extern const char* const kSoloCard; 141 extern const char* const kVisaCard; 142 143 #endif // CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_ 144