OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_WALLET_WALLET_CLIENT_H_ | |
6 #define CHROME_BROWSER_AUTOFILL_WALLET_WALLET_CLIENT_H_ | |
7 | |
8 #include <queue> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" // For base::Closure. | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/autofill/autofill_manager_delegate.h" | |
16 #include "chrome/browser/autofill/wallet/encryption_escrow_client.h" | |
17 #include "chrome/browser/autofill/wallet/encryption_escrow_client_observer.h" | |
18 #include "chrome/browser/autofill/wallet/full_wallet.h" | |
19 #include "components/autofill/common/autocheckout_status.h" | |
20 #include "net/url_request/url_fetcher_delegate.h" | |
21 #include "testing/gtest/include/gtest/gtest_prod.h" | |
22 | |
23 class GURL; | |
24 | |
25 namespace net { | |
26 class URLFetcher; | |
27 class URLRequestContextGetter; | |
28 } | |
29 | |
30 namespace autofill { | |
31 namespace wallet { | |
32 | |
33 class Address; | |
34 class Cart; | |
35 class FullWallet; | |
36 class Instrument; | |
37 class WalletClientObserver; | |
38 class WalletItems; | |
39 | |
40 // WalletClient is responsible for making calls to the Online Wallet backend on | |
41 // the user's behalf. The normal flow for using this class is as follows: | |
42 // 1) GetWalletItems should be called to retrieve the user's Wallet. | |
43 // a) If the user does not have a Wallet, they must AcceptLegalDocuments and | |
44 // SaveInstrumentAndAddress before continuing. | |
45 // b) If the user has not acccepte the most recent legal documents for | |
46 // Wallet, they must AcceptLegalDocuments. | |
47 // 2) The user then chooses what instrument and shipping address to use for the | |
48 // current transaction. | |
49 // a) If they choose an instrument with a zip code only address, the billing | |
50 // address will need to be updated using UpdateInstrument. | |
51 // b) The user may also choose to add a new instrument or address using | |
52 // SaveAddress, SaveInstrument, or SaveInstrumentAndAddress. | |
53 // 3) Once the user has selected the backing instrument and shipping address | |
54 // for this transaction, a FullWallet with the fronting card is generated | |
55 // using GetFullWallet. | |
56 // a) GetFullWallet may return a Risk challenge for the user. In that case, | |
57 // the user will need to verify who they are by authenticating their | |
58 // chosen backing instrument through AuthenticateInstrument | |
59 // 4) If the user initiated Autocheckout, SendAutocheckoutStatus to notify | |
60 // Online Wallet of the status flow to record various metrics. | |
61 // | |
62 // WalletClient is designed so only one request to Online Wallet can be outgoing | |
63 // at any one time. If |HasRequestInProgress()| is true while calling e.g. | |
64 // GetWalletItems(), the request will be queued and started later. Queued | |
65 // requests start in the order they were received. | |
66 | |
67 class WalletClient | |
68 : public net::URLFetcherDelegate, | |
69 public EncryptionEscrowClientObserver { | |
70 public: | |
71 // |context_getter| is reference counted so it has no lifetime or ownership | |
72 // requirements. |observer| must outlive |this|. | |
73 WalletClient(net::URLRequestContextGetter* context_getter, | |
74 WalletClientObserver* observer); | |
75 | |
76 virtual ~WalletClient(); | |
77 | |
78 // GetWalletItems retrieves the user's online wallet. The WalletItems | |
79 // returned may require additional action such as presenting legal documents | |
80 // to the user to be accepted. | |
81 void GetWalletItems(const GURL& source_url); | |
82 | |
83 // The GetWalletItems call to the Online Wallet backend may require the user | |
84 // to accept various legal documents before a FullWallet can be generated. | |
85 // The |document_ids| and |google_transaction_id| are provided in the response | |
86 // to the GetWalletItems call. | |
87 void AcceptLegalDocuments(const std::vector<std::string>& document_ids, | |
88 const std::string& google_transaction_id, | |
89 const GURL& source_url); | |
90 | |
91 // Authenticates that |card_verification_number| is for the backing instrument | |
92 // with |instrument_id|. |obfuscated_gaia_id| is used as a key when escrowing | |
93 // |card_verification_number|. |observer| is notified when the request is | |
94 // complete. Used to respond to Risk challenges. | |
95 void AuthenticateInstrument(const std::string& instrument_id, | |
96 const std::string& card_verification_number, | |
97 const std::string& obfuscated_gaia_id); | |
98 | |
99 // GetFullWallet retrieves the a FullWallet for the user. |instrument_id| and | |
100 // |adddress_id| should have been selected by the user in some UI, | |
101 // |merchant_domain| should come from the BrowserContext, the |cart| | |
102 // information will have been provided by the browser, |dialog_type| indicates | |
103 // which dialog requests the full wallet, RequestAutocomplete or Autocheckout, | |
104 // and |google_transaction_id| is the same one that GetWalletItems returns. | |
105 void GetFullWallet(const std::string& instrument_id, | |
106 const std::string& address_id, | |
107 const GURL& source_url, | |
108 const Cart& cart, | |
109 const std::string& google_transaction_id, | |
110 autofill::DialogType dialog_type); | |
111 | |
112 // SaveAddress saves a new shipping address. | |
113 void SaveAddress(const Address& address, const GURL& source_url); | |
114 | |
115 // SaveInstrument saves a new instrument. | |
116 void SaveInstrument(const Instrument& instrument, | |
117 const std::string& obfuscated_gaia_id, | |
118 const GURL& source_url); | |
119 | |
120 // SaveInstrumentAndAddress saves a new instrument and address. | |
121 void SaveInstrumentAndAddress(const Instrument& instrument, | |
122 const Address& shipping_address, | |
123 const std::string& obfuscated_gaia_id, | |
124 const GURL& source_url); | |
125 | |
126 // SendAutocheckoutStatus is used for tracking the success of Autocheckout | |
127 // flows. |status| is the result of the flow, |merchant_domain| is the domain | |
128 // where the purchase occured, and |google_transaction_id| is the same as the | |
129 // one provided by GetWalletItems. | |
130 void SendAutocheckoutStatus(autofill::AutocheckoutStatus status, | |
131 const GURL& source_url, | |
132 const std::string& google_transaction_id); | |
133 | |
134 // UpdateInstrument changes the instrument with id |instrument_id| with the | |
135 // information in |billing_address|. Its primary use is for upgrading ZIP code | |
136 // only addresses or those missing phone numbers. DO NOT change the name on | |
137 // |billing_address| from the one returned by Online Wallet or this call will | |
138 // fail. | |
139 void UpdateInstrument(const std::string& instrument_id, | |
140 const Address& billing_address, | |
141 const GURL& source_url); | |
142 | |
143 // Whether there is a currently running request (i.e. |request_| != NULL). | |
144 bool HasRequestInProgress() const; | |
145 | |
146 // Cancels and clears all |pending_requests_|. | |
147 void CancelPendingRequests(); | |
148 | |
149 private: | |
150 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, PendingRequest); | |
151 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, CancelPendingRequests); | |
152 | |
153 // TODO(ahutter): Implement this. | |
154 std::string GetRiskParams() { return std::string(); } | |
155 | |
156 enum RequestType { | |
157 NO_PENDING_REQUEST, | |
158 ACCEPT_LEGAL_DOCUMENTS, | |
159 AUTHENTICATE_INSTRUMENT, | |
160 GET_FULL_WALLET, | |
161 GET_WALLET_ITEMS, | |
162 SAVE_ADDRESS, | |
163 SAVE_INSTRUMENT, | |
164 SAVE_INSTRUMENT_AND_ADDRESS, | |
165 SEND_STATUS, | |
166 UPDATE_INSTRUMENT, | |
167 }; | |
168 | |
169 // Posts |post_body| to |url| and notifies |observer| when the request is | |
170 // complete. | |
171 void MakeWalletRequest(const GURL& url, const std::string& post_body); | |
172 | |
173 // Performs bookkeeping tasks for any invalid requests. | |
174 void HandleMalformedResponse(); | |
175 | |
176 // Start the next pending request (if any). | |
177 void StartNextPendingRequest(); | |
178 | |
179 // net::URLFetcherDelegate: | |
180 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
181 | |
182 // EncryptionEscrowClientObserver: | |
183 virtual void OnDidEncryptOneTimePad( | |
184 const std::string& encrypted_one_time_pad, | |
185 const std::string& session_material) OVERRIDE; | |
186 virtual void OnDidEscrowInstrumentInformation( | |
187 const std::string& escrow_handle) OVERRIDE; | |
188 virtual void OnDidEscrowCardVerificationNumber( | |
189 const std::string& escrow_handle) OVERRIDE; | |
190 virtual void OnNetworkError(int response_code) OVERRIDE; | |
191 virtual void OnMalformedResponse() OVERRIDE; | |
192 | |
193 // The context for the request. Ensures the gdToken cookie is set as a header | |
194 // in the requests to Online Wallet if it is present. | |
195 scoped_refptr<net::URLRequestContextGetter> context_getter_; | |
196 | |
197 // Observer class that has its various On* methods called based on the results | |
198 // of a request to Online Wallet. | |
199 WalletClientObserver* const observer_; // must outlive |this|. | |
200 | |
201 // The current request object. | |
202 scoped_ptr<net::URLFetcher> request_; | |
203 | |
204 // The type of the current request. Must be NO_PENDING_REQUEST for a request | |
205 // to be initiated as only one request may be running at a given time. | |
206 RequestType request_type_; | |
207 | |
208 // The one time pad used for GetFullWallet encryption. | |
209 std::vector<uint8> one_time_pad_; | |
210 | |
211 // GetFullWallet requests and requests that alter instruments rely on requests | |
212 // made through the |encryption_escrow_client_| finishing first. The request | |
213 // body is saved here while that those requests are in flight. | |
214 base::DictionaryValue pending_request_body_; | |
215 | |
216 // Requests that are waiting to be run. | |
217 std::queue<base::Closure> pending_requests_; | |
218 | |
219 // This client is repsonsible for making encryption and escrow calls to Online | |
220 // Wallet. | |
221 EncryptionEscrowClient encryption_escrow_client_; | |
222 | |
223 DISALLOW_COPY_AND_ASSIGN(WalletClient); | |
224 }; | |
225 | |
226 } // namespace wallet | |
227 } // namespace autofill | |
228 | |
229 #endif // CHROME_BROWSER_AUTOFILL_WALLET_WALLET_CLIENT_H_ | |
OLD | NEW |