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