- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / wallet / wallet_client.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_CLIENT_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_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/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "components/autofill/content/browser/wallet/full_wallet.h"
18 #include "components/autofill/content/browser/wallet/wallet_items.h"
19 #include "components/autofill/core/browser/autofill_manager_delegate.h"
20 #include "components/autofill/core/browser/autofill_metrics.h"
21 #include "net/url_request/url_fetcher_delegate.h"
22 #include "testing/gtest/include/gtest/gtest_prod.h"
23 #include "url/gurl.h"
24
25 namespace net {
26 class URLFetcher;
27 class URLRequestContextGetter;
28 }
29
30 namespace autofill {
31 namespace wallet {
32
33 class Address;
34 class FullWallet;
35 class Instrument;
36 class WalletClientDelegate;
37
38 // WalletClient is responsible for making calls to the Online Wallet backend on
39 // the user's behalf. The normal flow for using this class is as follows:
40 // 1) GetWalletItems should be called to retrieve the user's Wallet.
41 //   a) If the user does not have a Wallet, they must AcceptLegalDocuments and
42 //      SaveToWallet to set up their account before continuing.
43 //   b) If the user has not accepted the most recent legal documents for
44 //      Wallet, they must AcceptLegalDocuments.
45 // 2) The user then chooses what instrument and shipping address to use for the
46 //    current transaction.
47 //   a) If they choose an instrument with a zip code only address, the billing
48 //      address will need to be updated using SaveToWallet.
49 //   b) The user may also choose to add a new instrument or address using
50 //      SaveToWallet.
51 // 3) Once the user has selected the backing instrument and shipping address
52 //    for this transaction, a FullWallet with the fronting card is generated
53 //    using GetFullWallet.
54 //   a) GetFullWallet may return a Risk challenge for the user. In that case,
55 //      the user will need to verify who they are by authenticating their
56 //      chosen backing instrument through AuthenticateInstrument
57 //
58 // WalletClient is designed so only one request to Online Wallet can be outgoing
59 // at any one time. If |HasRequestInProgress()| is true while calling e.g.
60 // GetWalletItems(), the request will be queued and started later. Queued
61 // requests start in the order they were received.
62
63 class WalletClient : public net::URLFetcherDelegate {
64  public:
65   // The Risk challenges supported by users of WalletClient.
66   enum RiskCapability {
67     RELOGIN,
68     VERIFY_CVC,
69   };
70
71   // The type of error returned by Online Wallet.
72   enum ErrorType {
73     // Errors to display to users.
74     BUYER_ACCOUNT_ERROR,                // Risk deny, unsupported country, or
75                                         // account closed.
76     BUYER_LEGAL_ADDRESS_NOT_SUPPORTED,  // User's Buyer Legal Address is
77                                         // unsupported by Online Wallet.
78     UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS,  // User's "know your customer" KYC
79                                            // state is not verified (either
80                                            // KYC_REFER or KYC_FAIL).
81     UNSUPPORTED_MERCHANT,               // Merchant is blacklisted due to
82                                         // compliance violation.
83
84     // API errors.
85     BAD_REQUEST,              // Request was very malformed or sent to the
86                               // wrong endpoint.
87     INVALID_PARAMS,           // API call had missing or invalid parameters.
88     UNSUPPORTED_API_VERSION,  // The server API version of the request is no
89                               // longer supported.
90
91     // Server errors.
92     INTERNAL_ERROR,           // Unknown server side error.
93     SERVICE_UNAVAILABLE,      // Online Wallet is down.
94
95     // Other errors.
96     MALFORMED_RESPONSE,       // The response from Wallet was malformed.
97     NETWORK_ERROR,            // The response code of the server was something
98                               // other than a 200 or 400.
99
100     UNKNOWN_ERROR,            // Catch all error type.
101   };
102
103   struct FullWalletRequest {
104    public:
105     FullWalletRequest(const std::string& instrument_id,
106                       const std::string& address_id,
107                       const std::string& google_transaction_id,
108                       const std::vector<RiskCapability> risk_capabilities,
109                       bool new_wallet_user);
110     ~FullWalletRequest();
111
112     // The ID of the backing instrument. Should have been selected by the user
113     // in some UI.
114     std::string instrument_id;
115
116     // The ID of the shipping address. Should have been selected by the user
117     // in some UI.
118     std::string address_id;
119
120     // The transaction ID from GetWalletItems.
121     std::string google_transaction_id;
122
123     // The Risk challenges supported by the user of WalletClient
124     std::vector<RiskCapability> risk_capabilities;
125
126     // True if the user does not have Wallet profile.
127     bool new_wallet_user;
128
129    private:
130     DISALLOW_ASSIGN(FullWalletRequest);
131   };
132
133   // |context_getter| is reference counted so it has no lifetime or ownership
134   // requirements. |delegate| must outlive |this|. |source_url| is the url
135   // of the merchant page.
136   WalletClient(net::URLRequestContextGetter* context_getter,
137                WalletClientDelegate* delegate,
138                const GURL& source_url);
139
140   virtual ~WalletClient();
141
142   // GetWalletItems retrieves the user's online wallet. The WalletItems
143   // returned may require additional action such as presenting legal documents
144   // to the user to be accepted.
145   virtual void GetWalletItems();
146
147   // The GetWalletItems call to the Online Wallet backend may require the user
148   // to accept various legal documents before a FullWallet can be generated.
149   // The |google_transaction_id| is provided in the response to the
150   // GetWalletItems call. If |documents| are empty, |delegate_| will not receive
151   // a corresponding |OnDidAcceptLegalDocuments()| call.
152   virtual void AcceptLegalDocuments(
153       const std::vector<WalletItems::LegalDocument*>& documents,
154       const std::string& google_transaction_id);
155
156   // Authenticates that |card_verification_number| is for the backing instrument
157   // with |instrument_id|. |obfuscated_gaia_id| is used as a key when escrowing
158   // |card_verification_number|. |delegate_| is notified when the request is
159   // complete. Used to respond to Risk challenges.
160   virtual void AuthenticateInstrument(
161       const std::string& instrument_id,
162       const std::string& card_verification_number);
163
164   // GetFullWallet retrieves the a FullWallet for the user.
165   virtual void GetFullWallet(const FullWalletRequest& full_wallet_request);
166
167   // Saves the data in |instrument| and/or |address| to Wallet. |instrument|
168   // does not have to be complete if its being used to update an existing
169   // instrument, like in the case of expiration date or address only updates.
170   virtual void SaveToWallet(scoped_ptr<Instrument> instrument,
171                             scoped_ptr<Address> address);
172
173   bool HasRequestInProgress() const;
174
175   // Cancels and clears the current |request_| and |pending_requests_| (if any).
176   void CancelRequests();
177
178   size_t user_index() const { return user_index_; }
179   void set_user_index(size_t user_index) {
180     user_index_ = user_index;
181   }
182
183  private:
184   FRIEND_TEST_ALL_PREFIXES(WalletClientTest, PendingRequest);
185   FRIEND_TEST_ALL_PREFIXES(WalletClientTest, CancelRequests);
186
187   enum RequestType {
188     NO_PENDING_REQUEST,
189     ACCEPT_LEGAL_DOCUMENTS,
190     AUTHENTICATE_INSTRUMENT,
191     GET_FULL_WALLET,
192     GET_WALLET_ITEMS,
193     SAVE_TO_WALLET,
194   };
195
196   // Like AcceptLegalDocuments, but takes a vector of document ids.
197   void DoAcceptLegalDocuments(
198       const std::vector<std::string>& document_ids,
199       const std::string& google_transaction_id);
200
201   // Posts |post_body| to |url| with content type |mime_type| and notifies
202   // |delegate_| when the request is complete.
203   void MakeWalletRequest(const GURL& url,
204                          const std::string& post_body,
205                          const std::string& mime_type);
206
207   // Performs bookkeeping tasks for any invalid requests.
208   void HandleMalformedResponse(RequestType request_type,
209                                net::URLFetcher* request);
210   void HandleNetworkError(int response_code);
211   void HandleWalletError(ErrorType error_type);
212
213   // Start the next pending request (if any).
214   void StartNextPendingRequest();
215
216   // net::URLFetcherDelegate:
217   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
218
219   // Logs an UMA metric for each of the |required_actions|.
220   void LogRequiredActions(
221       const std::vector<RequiredAction>& required_actions) const;
222
223   // Converts |request_type| to an UMA metric.
224   AutofillMetrics::WalletApiCallMetric RequestTypeToUmaMetric(
225       RequestType request_type) const;
226
227   // The context for the request. Ensures the gdToken cookie is set as a header
228   // in the requests to Online Wallet if it is present.
229   scoped_refptr<net::URLRequestContextGetter> context_getter_;
230
231   // Observer class that has its various On* methods called based on the results
232   // of a request to Online Wallet.
233   WalletClientDelegate* const delegate_;  // must outlive |this|.
234
235   // The index of the user account we're making requests for. The index is into
236   // GAIA's list of signed in users.
237   size_t user_index_;
238
239   // The URL of the page we're making requests on behalf of.
240   GURL source_url_;
241
242   // The current request object.
243   scoped_ptr<net::URLFetcher> request_;
244
245   // The type of the current request. Must be NO_PENDING_REQUEST for a request
246   // to be initiated as only one request may be running at a given time.
247   RequestType request_type_;
248
249   // The one time pad used for GetFullWallet encryption.
250   std::vector<uint8> one_time_pad_;
251
252   // Requests that are waiting to be run.
253   std::queue<base::Closure> pending_requests_;
254
255   // When the current request started. Used to track client side latency.
256   base::Time request_started_timestamp_;
257
258   base::WeakPtrFactory<WalletClient> weak_ptr_factory_;
259
260   DISALLOW_COPY_AND_ASSIGN(WalletClient);
261 };
262
263 }  // namespace wallet
264 }  // namespace autofill
265
266 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_