summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/credit_card.h
blob: f437a901c23cf706769609016b1e93f351bfdd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_
#define CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_
#pragma once

#include <vector>

#include "base/string16.h"
#include "chrome/browser/autofill/form_group.h"

// A form group that stores credit card information.
class CreditCard : public FormGroup {
 public:
  // DEPRECATED
  // TODO(dhollowa): Remove unique ID and label.  http://crbug.com/58813
  CreditCard(const string16& label, int unique_id);
  explicit CreditCard(const std::string& guid);

  // For use in STL containers.
  CreditCard();
  CreditCard(const CreditCard& card);
  virtual ~CreditCard();

  // FormGroup implementation:
  FormGroup* Clone() const;
  virtual void GetPossibleFieldTypes(const string16& text,
                                     FieldTypeSet* possible_types) const;
  virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const;
  virtual void FindInfoMatches(const AutoFillType& type,
                               const string16& info,
                               std::vector<string16>* matched_text) const;
  virtual string16 GetFieldText(const AutoFillType& type) const;
  virtual string16 GetPreviewText(const AutoFillType& type) const;
  virtual void SetInfo(const AutoFillType& type, const string16& value);
  const string16& Label() const { return label_; }

  // The number altered for display, for example: ******1234
  string16 ObfuscatedNumber() const;
  // Credit card preview summary, for example: ******1234, Exp: 01/2020
  string16 PreviewSummary() const;
  // The last four digits of the credit card number.
  string16 LastFourDigits() const;

  const string16& type() const { return type_; }
  int billing_address_id() const { return billing_address_id_; }

  int unique_id() const { return unique_id_; }
  void set_unique_id(int id) { unique_id_ = id; }

  // The guid is the primary identifier for |CreditCard| objects.
  const std::string guid() const { return guid_; }
  void set_guid(const std::string& guid) { guid_ = guid; }

  // The caller should verify that the corresponding AutoFillProfile exists.
  void set_billing_address_id(int address_id) {
    billing_address_id_ = address_id;
  }

  // For use in STL containers.
  void operator=(const CreditCard&);

  // Used by tests.
  bool operator==(const CreditCard& creditcard) const;
  bool operator!=(const CreditCard& creditcard) const;
  void set_label(const string16& label) { label_ = label; }

  // Returns true if |value| is a credit card number.  Uses the Luhn formula to
  // validate the number.
  static bool IsCreditCardNumber(const string16& text);

  // Returns true if there are no values (field types) set.
  bool IsEmpty() const;

 private:
  // The month and year are zero if not present.
  int Expiration4DigitYear() const { return expiration_year_; }
  int Expiration2DigitYear() const { return expiration_year_ % 100; }
  string16 ExpirationMonthAsString() const;
  string16 Expiration4DigitYearAsString() const;
  string16 Expiration2DigitYearAsString() const;

  // Sets |expiration_month_| to the integer conversion of |text|.
  void SetExpirationMonthFromString(const string16& text);

  // Sets |expiration_year_| to the integer conversion of |text|.
  void SetExpirationYearFromString(const string16& text);

  const string16& number() const { return number_; }
  const string16& name_on_card() const { return name_on_card_; }
  const string16& last_four_digits() const { return last_four_digits_; }
  int expiration_month() const { return expiration_month_; }
  int expiration_year() const { return expiration_year_; }

  void set_number(const string16& number) { number_ = number; }
  void set_name_on_card(const string16& name_on_card) {
    name_on_card_ = name_on_card;
  }
  void set_type(const string16& type) { type_ = type; }
  void set_last_four_digits(const string16& last_four_digits) {
    last_four_digits_ = last_four_digits;
  }

  // These setters verify that the month and year are within appropriate
  // ranges.
  void set_expiration_month(int expiration_month);
  void set_expiration_year(int expiration_year);

  // A helper function for FindInfoMatches that only handles matching the info
  // with the requested field type.
  bool FindInfoMatchesHelper(const AutoFillFieldType& field_type,
                             const string16& info,
                             string16* match) const;

  // Returns true if |text| matches the name on the card.  The comparison is
  // case-insensitive.
  bool IsNameOnCard(const string16& text) const;

  // Returns true if |text| matches the expiration month of the card.
  bool IsExpirationMonth(const string16& text) const;

  // Returns true if the integer value of |text| matches the 2-digit expiration
  // year.
  bool Is2DigitExpirationYear(const string16& text) const;

  // Returns true if the integer value of |text| matches the 4-digit expiration
  // year.
  bool Is4DigitExpirationYear(const string16& text) const;

  // Converts |date| to an integer form.  Returns true if the conversion
  // succeeded.
  bool ConvertDate(const string16& date, int* num) const;

  string16 number_;  // The credit card number.
  string16 name_on_card_;  // The cardholder's name.
  string16 type_;  // The type of the card.

  // Stores the last four digits of the credit card number.
  string16 last_four_digits_;

  // These members are zero if not present.
  int expiration_month_;
  int expiration_year_;

  // This is the display name of the card set by the user, e.g., Amazon Visa.
  string16 label_;

  // The billing address. This is the unique ID of the AutoFillProfile that
  // contains the corresponding billing address.
  int billing_address_id_;

  // The unique ID of this credit card.
  int unique_id_;

  // The guid of this credit card.
  std::string guid_;
};

// So we can compare CreditCards with EXPECT_EQ().
std::ostream& operator<<(std::ostream& os, const CreditCard& creditcard);

#endif  // CHROME_BROWSER_AUTOFILL_CREDIT_CARD_H_