022000404c9df3f969c78193b3b6bb9467518943
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / wallet / wallet_items.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_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/strings/string16.h"
17 #include "components/autofill/content/browser/wallet/required_action.h"
18 #include "components/autofill/content/browser/wallet/wallet_address.h"
19 #include "url/gurl.h"
20
21 namespace base {
22 class DictionaryValue;
23 }
24
25 namespace gfx {
26 class Image;
27 }
28
29 namespace autofill {
30
31 class AutofillType;
32
33 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest, GetInfoCreditCardExpMonth);
34 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest,
35                      GetDisplayTextEmptyWhenExpired);
36
37 namespace wallet {
38
39 class GaiaAccount;
40 class WalletItemsTest;
41
42 enum AmexPermission {
43   AMEX_ALLOWED,
44   AMEX_DISALLOWED,
45 };
46
47 // WalletItems is a collection of cards and addresses that a user picks from to
48 // construct a full wallet. However, it also provides a transaction ID which
49 // must be used throughout all API calls being made using this data.
50 // Additionally, user actions may be required before a purchase can be completed
51 // using Online Wallet and those actions are present in the object as well.
52 class WalletItems {
53  public:
54   // Container for all information about a credit card except for it's card
55   // verfication number (CVN) and it's complete primary account number (PAN).
56   class MaskedInstrument {
57    public:
58     enum Type {
59       AMEX,
60       DISCOVER,
61       MAESTRO,
62       MASTER_CARD,
63       SOLO,
64       SWITCH,
65       UNKNOWN,  // Catch all type.
66       VISA,
67     };
68     enum Status {
69       AMEX_NOT_SUPPORTED,
70       BILLING_INCOMPLETE,
71       DECLINED,
72       DISABLED_FOR_THIS_MERCHANT,  // Deprecated.
73       EXPIRED,
74       INAPPLICABLE,  // Catch all status.
75       PENDING,
76       UNSUPPORTED_COUNTRY,
77       VALID,
78     };
79
80     ~MaskedInstrument();
81
82     // Returns an empty scoped_ptr if input is invalid or a valid masked
83     // instrument.
84     static scoped_ptr<MaskedInstrument>
85         CreateMaskedInstrument(const base::DictionaryValue& dictionary);
86
87     bool operator==(const MaskedInstrument& other) const;
88     bool operator!=(const MaskedInstrument& other) const;
89
90     // Gets an image to display for this instrument.
91     const gfx::Image& CardIcon() const;
92
93     // Returns a pair of strings that summarizes this CC,
94     // suitable for display to the user.
95     base::string16 DisplayName() const;
96     base::string16 DisplayNameDetail() const;
97
98     // Gets info that corresponds with |type|.
99     base::string16 GetInfo(const AutofillType& type,
100                            const std::string& app_locale) const;
101
102     // Returns the display type of the and last four digits (e.g. Visa - 4444).
103     base::string16 TypeAndLastFourDigits() const;
104
105     const base::string16& descriptive_name() const { return descriptive_name_; }
106     const Type& type() const { return type_; }
107     const base::string16& last_four_digits() const { return last_four_digits_; }
108     int expiration_month() const { return expiration_month_; }
109     int expiration_year() const { return expiration_year_; }
110     const Address& address() const { return *address_; }
111     const Status& status() const { return status_; }
112     const std::string& object_id() const { return object_id_; }
113
114    private:
115     friend class WalletItemsTest;
116     friend scoped_ptr<MaskedInstrument> GetTestMaskedInstrumentWithDetails(
117         const std::string& id,
118         scoped_ptr<Address> address,
119         Type type,
120         Status status);
121     FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
122                              GetInfoCreditCardExpMonth);
123     FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
124                              GetDisplayTextEmptyWhenExpired);
125     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateMaskedInstrument);
126     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
127
128     MaskedInstrument(const base::string16& descriptive_name,
129                      const Type& type,
130                      const base::string16& last_four_digits,
131                      int expiration_month,
132                      int expiration_year,
133                      scoped_ptr<Address> address,
134                      const Status& status,
135                      const std::string& object_id);
136
137     // A user-provided description of the instrument. For example, "Google Visa
138     // Card".
139     base::string16 descriptive_name_;
140
141     // The payment network of the instrument. For example, Visa.
142     Type type_;
143
144     // The last four digits of the primary account number of the instrument.
145     base::string16 last_four_digits_;
146
147     // |expiration month_| should be 1-12.
148     int expiration_month_;
149
150     // |expiration_year_| should be a 4-digit year.
151     int expiration_year_;
152
153     // The billing address for the instrument.
154     scoped_ptr<Address> address_;
155
156     // The current status of the instrument. For example, expired or declined.
157     Status status_;
158
159     // Externalized Online Wallet id for this instrument.
160     std::string object_id_;
161
162     DISALLOW_COPY_AND_ASSIGN(MaskedInstrument);
163   };
164
165   // Class representing a legal document that the user must accept before they
166   // can use Online Wallet.
167   class LegalDocument {
168    public:
169     ~LegalDocument();
170
171     // Returns null if input is invalid or a valid legal document.
172     static scoped_ptr<LegalDocument>
173         CreateLegalDocument(const base::DictionaryValue& dictionary);
174
175     // Returns a document for the privacy policy (acceptance of which is not
176     // tracked by the server).
177     static scoped_ptr<LegalDocument> CreatePrivacyPolicyDocument();
178
179     bool operator==(const LegalDocument& other) const;
180     bool operator!=(const LegalDocument& other) const;
181
182     const std::string& id() { return id_; }
183     const GURL& url() const { return url_; }
184     const base::string16& display_name() const { return display_name_; }
185
186    private:
187     friend class WalletItemsTest;
188     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateLegalDocument);
189     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
190     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentUrl);
191     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentEmptyId);
192     LegalDocument(const std::string& id,
193                   const base::string16& display_name);
194     LegalDocument(const GURL& url,
195                   const base::string16& display_name);
196
197     // Externalized Online Wallet id for the document, or an empty string for
198     // documents not tracked by the server (such as the privacy policy).
199     std::string id_;
200     // The human-visitable URL that displays the document.
201     GURL url_;
202     // User displayable name for the document.
203     base::string16 display_name_;
204     DISALLOW_COPY_AND_ASSIGN(LegalDocument);
205   };
206
207   ~WalletItems();
208
209   // Returns null on invalid input, an empty wallet items with required
210   // actions if any are present, and a populated wallet items otherwise. Caller
211   // owns returned pointer.
212   static scoped_ptr<WalletItems>
213       CreateWalletItems(const base::DictionaryValue& dictionary);
214
215   bool operator==(const WalletItems& other) const;
216   bool operator!=(const WalletItems& other) const;
217
218   void AddAccount(scoped_ptr<GaiaAccount> account);
219   void AddInstrument(scoped_ptr<MaskedInstrument> instrument) {
220     DCHECK(instrument);
221     instruments_.push_back(instrument.release());
222   }
223   void AddAddress(scoped_ptr<Address> address) {
224     DCHECK(address);
225     addresses_.push_back(address.release());
226   }
227   void AddLegalDocument(scoped_ptr<LegalDocument> legal_document) {
228     DCHECK(legal_document);
229     legal_documents_.push_back(legal_document.release());
230   }
231
232   // Return the corresponding instrument for |id| or NULL if it doesn't exist.
233   const WalletItems::MaskedInstrument* GetInstrumentById(
234       const std::string& object_id) const;
235
236   // Whether or not |action| is in |required_actions_|.
237   bool HasRequiredAction(RequiredAction action) const;
238
239   // Checks whether |card_number| is supported by Wallet for this merchant and
240   // if not, fills in |message| with a user-visible explanation.
241   bool SupportsCard(const base::string16& card_number,
242                     base::string16* message) const;
243
244   const std::vector<GaiaAccount*>& gaia_accounts() const {
245     return gaia_accounts_.get();
246   }
247   const std::vector<RequiredAction>& required_actions() const {
248     return required_actions_;
249   }
250   const std::string& google_transaction_id() const {
251     return google_transaction_id_;
252   }
253   const std::vector<MaskedInstrument*>& instruments() const {
254     return instruments_.get();
255   }
256   const std::string& default_instrument_id() const {
257     return default_instrument_id_;
258   }
259   const std::vector<Address*>& addresses() const { return addresses_.get(); }
260   const std::string& default_address_id() const { return default_address_id_; }
261   // Returns the GAIA id of the active account, or an empty string if no account
262   // is active.
263   std::string ObfuscatedGaiaId() const;
264   size_t active_account_index() const { return active_account_index_; }
265   const std::vector<LegalDocument*>& legal_documents() const {
266     return legal_documents_.get();
267   }
268
269  private:
270   friend class WalletItemsTest;
271   friend scoped_ptr<WalletItems> GetTestWalletItemsWithDetails(
272       const std::vector<RequiredAction>& required_actions,
273       const std::string& default_instrument_id,
274       const std::string& default_address_id,
275       AmexPermission amex_permission);
276   friend scoped_ptr<WalletItems> GetTestWalletItemsWithDefaultIds(
277       RequiredAction action);
278   FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
279   FRIEND_TEST_ALL_PREFIXES(WalletItemsTest,
280                            CreateWalletItemsWithRequiredActions);
281
282   WalletItems(const std::vector<RequiredAction>& required_actions,
283               const std::string& google_transaction_id,
284               const std::string& default_instrument_id,
285               const std::string& default_address_id,
286               AmexPermission amex_permission);
287
288   // Actions that must be completed by the user before a FullWallet can be
289   // issued to them by the Online Wallet service.
290   std::vector<RequiredAction> required_actions_;
291
292   // The id for this transaction issued by Google.
293   std::string google_transaction_id_;
294
295   // The id of the user's default instrument.
296   std::string default_instrument_id_;
297
298   // The id of the user's default address.
299   std::string default_address_id_;
300
301   // The index into |gaia_accounts_| of the account for which this object
302   // holds data.
303   size_t active_account_index_;
304
305   // The complete set of logged in GAIA accounts.
306   ScopedVector<GaiaAccount> gaia_accounts_;
307
308   // The user's backing instruments.
309   ScopedVector<MaskedInstrument> instruments_;
310
311   // The user's shipping addresses.
312   ScopedVector<Address> addresses_;
313
314   // Legal documents the user must accept before using Online Wallet.
315   ScopedVector<LegalDocument> legal_documents_;
316
317   // Whether Google Wallet allows American Express card for this merchant.
318   AmexPermission amex_permission_;
319
320   DISALLOW_COPY_AND_ASSIGN(WalletItems);
321 };
322
323 }  // namespace wallet
324 }  // namespace autofill
325
326 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_