credit_card.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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 <vector>
10
11#include "base/string16.h"
12#include "chrome/browser/autofill/form_group.h"
13
14// A form group that stores credit card information.
15class CreditCard : public FormGroup {
16 public:
17  explicit CreditCard(const std::string& guid);
18
19  // For use in STL containers.
20  CreditCard();
21  CreditCard(const CreditCard& credit_card);
22  virtual ~CreditCard();
23
24  // FormGroup implementation:
25  FormGroup* Clone() const;
26  virtual void GetPossibleFieldTypes(const string16& text,
27                                     FieldTypeSet* possible_types) const;
28  virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const;
29  virtual void FindInfoMatches(const AutoFillType& type,
30                               const string16& info,
31                               std::vector<string16>* matched_text) const;
32  virtual string16 GetFieldText(const AutoFillType& type) const;
33  virtual string16 GetPreviewText(const AutoFillType& type) const;
34  virtual void SetInfo(const AutoFillType& type, const string16& value);
35  virtual const string16 Label() const { return label_; }
36
37  // The number altered for display, for example: ******1234
38  string16 ObfuscatedNumber() const;
39  // Credit card preview summary, for example: ******1234, Exp: 01/2020
40  string16 PreviewSummary() const;
41  // The last four digits of the credit card number.
42  string16 LastFourDigits() const;
43
44  const string16& type() const { return type_; }
45
46  // The guid is the primary identifier for |CreditCard| objects.
47  const std::string guid() const { return 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  void set_label(const string16& label) { label_ = label; }
65
66  // Returns true if |value| is a credit card number.  Uses the Luhn formula to
67  // validate the number.
68  static bool IsCreditCardNumber(const string16& text);
69
70  // Returns true if there are no values (field types) set.
71  bool IsEmpty() const;
72
73 private:
74  // The month and year are zero if not present.
75  int Expiration4DigitYear() const { return expiration_year_; }
76  int Expiration2DigitYear() const { return expiration_year_ % 100; }
77  string16 ExpirationMonthAsString() const;
78  string16 Expiration4DigitYearAsString() const;
79  string16 Expiration2DigitYearAsString() const;
80
81  // Sets |expiration_month_| to the integer conversion of |text|.
82  void SetExpirationMonthFromString(const string16& text);
83
84  // Sets |expiration_year_| to the integer conversion of |text|.
85  void SetExpirationYearFromString(const string16& text);
86
87  const string16& number() const { return number_; }
88  const string16& name_on_card() const { return name_on_card_; }
89  const string16& last_four_digits() const { return last_four_digits_; }
90  int expiration_month() const { return expiration_month_; }
91  int expiration_year() const { return expiration_year_; }
92
93  void set_number(const string16& number) { number_ = number; }
94  void set_name_on_card(const string16& name_on_card) {
95    name_on_card_ = name_on_card;
96  }
97  void set_type(const string16& type) { type_ = type; }
98  void set_last_four_digits(const string16& last_four_digits) {
99    last_four_digits_ = last_four_digits;
100  }
101
102  // These setters verify that the month and year are within appropriate
103  // ranges.
104  void set_expiration_month(int expiration_month);
105  void set_expiration_year(int expiration_year);
106
107  // A helper function for FindInfoMatches that only handles matching the info
108  // with the requested field type.
109  bool FindInfoMatchesHelper(const AutoFillFieldType& field_type,
110                             const string16& info,
111                             string16* match) const;
112
113  // Returns true if |text| matches the name on the card.  The comparison is
114  // case-insensitive.
115  bool IsNameOnCard(const string16& text) const;
116
117  // Returns true if |text| matches the expiration month of the card.
118  bool IsExpirationMonth(const string16& text) const;
119
120  // Returns true if the integer value of |text| matches the 2-digit expiration
121  // year.
122  bool Is2DigitExpirationYear(const string16& text) const;
123
124  // Returns true if the integer value of |text| matches the 4-digit expiration
125  // year.
126  bool Is4DigitExpirationYear(const string16& text) const;
127
128  // Converts |date| to an integer form.  Returns true if the conversion
129  // succeeded.
130  bool ConvertDate(const string16& date, int* num) const;
131
132  string16 number_;  // The credit card number.
133  string16 name_on_card_;  // The cardholder's name.
134  string16 type_;  // The type of the card.
135
136  // Stores the last four digits of the credit card number.
137  string16 last_four_digits_;
138
139  // These members are zero if not present.
140  int expiration_month_;
141  int expiration_year_;
142
143  // This is the display name of the card set by the user, e.g., Amazon Visa.
144  string16 label_;
145
146  // The guid of this credit card.
147  std::string guid_;
148};
149
150// So we can compare CreditCards with EXPECT_EQ().
151std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card);
152
153#endif  // CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_
154