Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / credit_card.h
blob2a6990a387d4f20cf991527901750f89cb204d33
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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
8 #include <iosfwd>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/compiler_specific.h"
14 #include "base/strings/string16.h"
15 #include "components/autofill/core/browser/autofill_data_model.h"
17 namespace autofill {
19 // A form group that stores credit card information.
20 class CreditCard : public AutofillDataModel {
21 public:
22 enum RecordType {
23 // A card with a complete number managed by Chrome (and not representing
24 // something on the server).
25 LOCAL_CARD,
27 // A card from Wallet with masked information. Such cards will only have
28 // the last 4 digits of the card number, and require an extra download to
29 // convert to a FULL_SERVER_CARD.
30 MASKED_SERVER_CARD,
32 // A card from the Wallet server with full information. This card is not
33 // locally editable.
34 FULL_SERVER_CARD,
37 // The status of this credit card. Only used for server cards.
38 enum ServerStatus {
39 EXPIRED,
40 OK,
43 CreditCard(const std::string& guid, const std::string& origin);
44 CreditCard(const base::string16& card_number,
45 int expiration_month,
46 int expiration_year);
48 // Creates a server card. The type must be MASKED_SERVER_CARD or
49 // FULL_SERVER_CARD.
50 CreditCard(RecordType type, const std::string& server_id);
52 // For use in STL containers.
53 CreditCard();
54 CreditCard(const CreditCard& credit_card);
55 ~CreditCard() override;
57 // Returns a version of |number| that has any separator characters removed.
58 static const base::string16 StripSeparators(const base::string16& number);
60 // The user-visible type of the card, e.g. 'Mastercard'.
61 static base::string16 TypeForDisplay(const std::string& type);
63 // This method is not compiled on iOS because the resources are not used and
64 // should not be shipped.
65 #if !defined(OS_IOS)
66 // The ResourceBundle ID for the appropriate credit card image.
67 static int IconResourceId(const std::string& type);
68 #endif // #if !defined(OS_IOS)
70 // Returns the internal representation of credit card type corresponding to
71 // the given |number|. The credit card type is determined purely according to
72 // the Issuer Identification Number (IIN), a.k.a. the "Bank Identification
73 // Number (BIN)", which is parsed from the relevant prefix of the |number|.
74 // This function performs no additional validation checks on the |number|.
75 // Hence, the returned type for both the valid card "4111-1111-1111-1111" and
76 // the invalid card "4garbage" will be Visa, which has an IIN of 4.
77 static const char* GetCreditCardType(const base::string16& number);
79 // Type strings are defined at the bottom of this file, e.g. kVisaCard.
80 void SetTypeForMaskedCard(const char* type);
82 // Sets/gets the status of a server card.
83 void SetServerStatus(ServerStatus status);
84 ServerStatus GetServerStatus() const;
86 // FormGroup:
87 void GetMatchingTypes(const base::string16& text,
88 const std::string& app_locale,
89 ServerFieldTypeSet* matching_types) const override;
90 base::string16 GetRawInfo(ServerFieldType type) const override;
91 void SetRawInfo(ServerFieldType type, const base::string16& value) override;
92 base::string16 GetInfo(const AutofillType& type,
93 const std::string& app_locale) const override;
94 bool SetInfo(const AutofillType& type,
95 const base::string16& value,
96 const std::string& app_locale) override;
98 // Credit card preview summary, for example: "Visa - 1234", ", 01/2020".
99 const std::pair<base::string16, base::string16> LabelPieces() const;
101 // Like LabelPieces, but appends the two pieces together.
102 const base::string16 Label() const;
104 // Special method to set value for HTML5 month input type.
105 void SetInfoForMonthInputType(const base::string16& value);
107 // The last four digits of the credit card number (or possibly less if there
108 // aren't enough characters).
109 base::string16 LastFourDigits() const;
110 // The user-visible type of the card, e.g. 'Mastercard'.
111 base::string16 TypeForDisplay() const;
112 // A label for this credit card formatted as 'Cardname - 2345'.
113 base::string16 TypeAndLastFourDigits() const;
115 const std::string& type() const { return type_; }
117 int expiration_month() const { return expiration_month_; }
118 int expiration_year() const { return expiration_year_; }
120 // These setters verify that the month and year are within appropriate
121 // ranges, or 0. They take integers as an alternative to setting the inputs
122 // from strings via SetInfo().
123 void SetExpirationMonth(int expiration_month);
124 void SetExpirationYear(int expiration_year);
126 const std::string& server_id() const { return server_id_; }
128 // For use in STL containers.
129 void operator=(const CreditCard& credit_card);
131 // If the card numbers for |this| and |imported_card| match, and merging the
132 // two wouldn't result in unverified data overwriting verified data,
133 // overwrites |this| card's data with the data in |credit_card|.
134 // Returns true if the card numbers match, false otherwise.
135 bool UpdateFromImportedCard(const CreditCard& imported_card,
136 const std::string& app_locale) WARN_UNUSED_RESULT;
138 // Comparison for Sync. Returns 0 if the credit card is the same as |this|,
139 // or < 0, or > 0 if it is different. The implied ordering can be used for
140 // culling duplicates. The ordering is based on collation order of the
141 // textual contents of the fields.
142 // GUIDs, origins, labels, and unique IDs are not compared, only the values of
143 // the credit cards themselves.
144 int Compare(const CreditCard& credit_card) const;
146 // Determines if |this| is a local version of the server card |other|.
147 bool IsLocalDuplicateOfServerCard(const CreditCard& other) const;
149 // Equality operators compare GUIDs, origins, and the contents.
150 // Usage metadata (use count, use date, modification date) are NOT compared.
151 bool operator==(const CreditCard& credit_card) const;
152 bool operator!=(const CreditCard& credit_card) const;
154 // How this card is stored.
155 RecordType record_type() const { return record_type_; }
156 void set_record_type(RecordType rt) { record_type_ = rt; }
158 // Returns true if there are no values (field types) set.
159 bool IsEmpty(const std::string& app_locale) const;
161 // Returns true if all field types have valid values set. Server masked cards
162 // will not be complete. MASKED_SERVER_CARDs will never be complete.
163 bool IsComplete() const;
165 // Returns true if all field types have valid values set and the card is not
166 // expired. MASKED_SERVER_CARDs will never be valid because the number is
167 // not complete.
168 bool IsValid() const;
170 // Returns the credit card number.
171 const base::string16& number() const { return number_; }
172 // Sets |number_| to |number| and computes the appropriate card |type_|.
173 void SetNumber(const base::string16& number);
175 // Converts a string representation of a month (such as "February" or "feb."
176 // or "2") into a numeric value in [1, 12]. Returns true on successful
177 // conversion or false if a month was not recognized.
178 static bool ConvertMonth(const base::string16& month,
179 const std::string& app_locale,
180 int* num);
182 private:
183 // FormGroup:
184 void GetSupportedTypes(ServerFieldTypeSet* supported_types) const override;
186 // The type of the card to fill in to the page, e.g. 'Mastercard'.
187 base::string16 TypeForFill() const;
189 // The month and year are zero if not present.
190 int Expiration4DigitYear() const { return expiration_year_; }
191 int Expiration2DigitYear() const { return expiration_year_ % 100; }
192 base::string16 ExpirationMonthAsString() const;
193 base::string16 Expiration4DigitYearAsString() const;
194 base::string16 Expiration2DigitYearAsString() const;
196 // Sets |expiration_month_| to the integer conversion of |text|.
197 void SetExpirationMonthFromString(const base::string16& text,
198 const std::string& app_locale);
200 // Sets |expiration_year_| to the integer conversion of |text|.
201 void SetExpirationYearFromString(const base::string16& text);
203 // See enum definition above.
204 RecordType record_type_;
206 // The credit card number. For MASKED_SERVER_CARDs, this number will
207 // just contain the last four digits of the card number.
208 base::string16 number_;
210 // The cardholder's name. May be empty.
211 base::string16 name_on_card_;
213 // The type of the card. This is one of the k...Card constants below.
214 std::string type_;
216 // These members are zero if not present.
217 int expiration_month_;
218 int expiration_year_;
220 // For server cards (both MASKED and UNMASKED) this is the ID assigned by the
221 // server to uniquely identify this card.
222 std::string server_id_;
224 // The status of the card, as reported by the server. Not valid for local
225 // cards.
226 ServerStatus server_status_;
229 // So we can compare CreditCards with EXPECT_EQ().
230 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card);
232 // The string identifiers for credit card icon resources.
233 extern const char* const kAmericanExpressCard;
234 extern const char* const kDinersCard;
235 extern const char* const kDiscoverCard;
236 extern const char* const kGenericCard;
237 extern const char* const kJCBCard;
238 extern const char* const kMasterCard;
239 extern const char* const kUnionPay;
240 extern const char* const kVisaCard;
242 } // namespace autofill
244 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_