Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / credit_card.h
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
7
8 #include <iosfwd>
9 #include <string>
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/strings/string16.h"
14 #include "components/autofill/core/browser/autofill_data_model.h"
15
16 namespace autofill {
17
18 struct FormFieldData;
19
20 // A form group that stores credit card information.
21 class CreditCard : public AutofillDataModel {
22  public:
23   CreditCard(const std::string& guid, const std::string& origin);
24
25   // For use in STL containers.
26   CreditCard();
27   CreditCard(const CreditCard& credit_card);
28   virtual ~CreditCard();
29
30   // Returns a version of |number| that has any separator characters removed.
31   static const base::string16 StripSeparators(const base::string16& number);
32
33   // The user-visible type of the card, e.g. 'Mastercard'.
34   static base::string16 TypeForDisplay(const std::string& type);
35
36   // The ResourceBundle ID for the appropriate credit card image.
37   static int IconResourceId(const std::string& type);
38
39   // Returns the internal representation of credit card type corresponding to
40   // the given |number|.  The credit card type is determined purely according to
41   // the Issuer Identification Number (IIN), a.k.a. the "Bank Identification
42   // Number (BIN)", which is parsed from the relevant prefix of the |number|.
43   // This function performs no additional validation checks on the |number|.
44   // Hence, the returned type for both the valid card "4111-1111-1111-1111" and
45   // the invalid card "4garbage" will be Visa, which has an IIN of 4.
46   static const char* GetCreditCardType(const base::string16& number);
47
48   // FormGroup:
49   virtual void GetMatchingTypes(
50       const base::string16& text,
51       const std::string& app_locale,
52       ServerFieldTypeSet* matching_types) const OVERRIDE;
53   virtual base::string16 GetRawInfo(ServerFieldType type) const OVERRIDE;
54   virtual void SetRawInfo(ServerFieldType type,
55                           const base::string16& value) OVERRIDE;
56   virtual base::string16 GetInfo(const AutofillType& type,
57                                  const std::string& app_locale) const OVERRIDE;
58   virtual bool SetInfo(const AutofillType& type,
59                        const base::string16& value,
60                        const std::string& app_locale) OVERRIDE;
61
62   // Credit card preview summary, for example: ******1234, Exp: 01/2020
63   const base::string16 Label() const;
64
65   // Special method to set value for HTML5 month input type.
66   void SetInfoForMonthInputType(const base::string16& value);
67
68   // The number altered for display, for example: ******1234
69   base::string16 ObfuscatedNumber() const;
70   // The last four digits of the credit card number.
71   base::string16 LastFourDigits() const;
72   // The user-visible type of the card, e.g. 'Mastercard'.
73   base::string16 TypeForDisplay() const;
74   // A label for this credit card formatted as 'Cardname - 2345'.
75   base::string16 TypeAndLastFourDigits() const;
76
77   const std::string& type() const { return type_; }
78
79   int expiration_month() const { return expiration_month_; }
80   int expiration_year() const { return expiration_year_; }
81
82   // For use in STL containers.
83   void operator=(const CreditCard& credit_card);
84
85   // If the card numbers for |this| and |imported_card| match, and merging the
86   // two wouldn't result in unverified data overwriting verified data,
87   // overwrites |this| card's data with the data in |credit_card|.
88   // Returns true if the card numbers match, false otherwise.
89   bool UpdateFromImportedCard(const CreditCard& imported_card,
90                               const std::string& app_locale) WARN_UNUSED_RESULT;
91
92   // Comparison for Sync.  Returns 0 if the credit card is the same as |this|,
93   // or < 0, or > 0 if it is different.  The implied ordering can be used for
94   // culling duplicates.  The ordering is based on collation order of the
95   // textual contents of the fields.
96   // GUIDs, origins, labels, and unique IDs are not compared, only the values of
97   // the credit cards themselves.
98   int Compare(const CreditCard& credit_card) const;
99
100   // Used by tests.
101   bool operator==(const CreditCard& credit_card) const;
102   bool operator!=(const CreditCard& credit_card) const;
103
104   // Returns true if there are no values (field types) set.
105   bool IsEmpty(const std::string& app_locale) const;
106
107   // Returns true if all field types have valid values set.
108   bool IsComplete() const;
109
110   // Returns true if all field types have valid values set and the card is not
111   // expired.
112   bool IsValid() const;
113
114   // Returns the credit card number.
115   const base::string16& number() const { return number_; }
116
117  private:
118   // FormGroup:
119   virtual void GetSupportedTypes(
120       ServerFieldTypeSet* supported_types) const OVERRIDE;
121
122   // The month and year are zero if not present.
123   int Expiration4DigitYear() const { return expiration_year_; }
124   int Expiration2DigitYear() const { return expiration_year_ % 100; }
125   base::string16 ExpirationMonthAsString() const;
126   base::string16 Expiration4DigitYearAsString() const;
127   base::string16 Expiration2DigitYearAsString() const;
128
129   // Sets |expiration_month_| to the integer conversion of |text|.
130   void SetExpirationMonthFromString(const base::string16& text,
131                                     const std::string& app_locale);
132
133   // Sets |expiration_year_| to the integer conversion of |text|.
134   void SetExpirationYearFromString(const base::string16& text);
135
136   // Sets |number_| to |number| and computes the appropriate card |type_|.
137   void SetNumber(const base::string16& number);
138
139   // These setters verify that the month and year are within appropriate
140   // ranges.
141   void SetExpirationMonth(int expiration_month);
142   void SetExpirationYear(int expiration_year);
143
144   base::string16 number_;  // The credit card number.
145   base::string16 name_on_card_;  // The cardholder's name.
146   std::string type_;  // The type of the card.
147
148   // These members are zero if not present.
149   int expiration_month_;
150   int expiration_year_;
151 };
152
153 // So we can compare CreditCards with EXPECT_EQ().
154 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card);
155
156 // The string identifiers for credit card icon resources.
157 extern const char* const kAmericanExpressCard;
158 extern const char* const kDinersCard;
159 extern const char* const kDiscoverCard;
160 extern const char* const kGenericCard;
161 extern const char* const kJCBCard;
162 extern const char* const kMasterCard;
163 extern const char* const kUnionPay;
164 extern const char* const kVisaCard;
165
166 }  // namespace autofill
167
168 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_