| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ | 5 #ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ |
| 6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ | 6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" // For base::Closure. | 12 #include "base/callback.h" // For base::Closure. |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "components/autofill/content/browser/autocheckout_statistic.h" | 16 #include "components/autofill/content/browser/autocheckout_statistic.h" |
| 17 #include "components/autofill/content/browser/wallet/encryption_escrow_client.h" | |
| 18 #include "components/autofill/content/browser/wallet/encryption_escrow_client_ob
server.h" | |
| 19 #include "components/autofill/content/browser/wallet/full_wallet.h" | 17 #include "components/autofill/content/browser/wallet/full_wallet.h" |
| 20 #include "components/autofill/content/browser/wallet/wallet_items.h" | 18 #include "components/autofill/content/browser/wallet/wallet_items.h" |
| 21 #include "components/autofill/core/browser/autofill_manager_delegate.h" | 19 #include "components/autofill/core/browser/autofill_manager_delegate.h" |
| 22 #include "components/autofill/core/browser/autofill_metrics.h" | 20 #include "components/autofill/core/browser/autofill_metrics.h" |
| 23 #include "components/autofill/core/common/autocheckout_status.h" | 21 #include "components/autofill/core/common/autocheckout_status.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "testing/gtest/include/gtest/gtest_prod.h" | 23 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 26 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 27 | 25 |
| 28 namespace net { | 26 namespace net { |
| 29 class URLFetcher; | 27 class URLFetcher; |
| 30 class URLRequestContextGetter; | 28 class URLRequestContextGetter; |
| 31 } | 29 } |
| 32 | 30 |
| 33 namespace autofill { | 31 namespace autofill { |
| 34 namespace wallet { | 32 namespace wallet { |
| 35 | 33 |
| 36 class Address; | 34 class Address; |
| 37 class FullWallet; | 35 class FullWallet; |
| 38 class Instrument; | 36 class Instrument; |
| 39 class WalletClientDelegate; | 37 class WalletClientDelegate; |
| 40 | 38 |
| 41 // WalletClient is responsible for making calls to the Online Wallet backend on | 39 // WalletClient is responsible for making calls to the Online Wallet backend on |
| 42 // the user's behalf. The normal flow for using this class is as follows: | 40 // the user's behalf. The normal flow for using this class is as follows: |
| 43 // 1) GetWalletItems should be called to retrieve the user's Wallet. | 41 // 1) GetWalletItems should be called to retrieve the user's Wallet. |
| 44 // a) If the user does not have a Wallet, they must AcceptLegalDocuments and | 42 // a) If the user does not have a Wallet, they must AcceptLegalDocuments and |
| 45 // SaveInstrumentAndAddress before continuing. | 43 // SaveToWallet to set up their account before continuing. |
| 46 // b) If the user has not accepted the most recent legal documents for | 44 // b) If the user has not accepted the most recent legal documents for |
| 47 // Wallet, they must AcceptLegalDocuments. | 45 // Wallet, they must AcceptLegalDocuments. |
| 48 // 2) The user then chooses what instrument and shipping address to use for the | 46 // 2) The user then chooses what instrument and shipping address to use for the |
| 49 // current transaction. | 47 // current transaction. |
| 50 // a) If they choose an instrument with a zip code only address, the billing | 48 // a) If they choose an instrument with a zip code only address, the billing |
| 51 // address will need to be updated using UpdateInstrument. | 49 // address will need to be updated using SaveToWallet. |
| 52 // b) The user may also choose to add a new instrument or address using | 50 // b) The user may also choose to add a new instrument or address using |
| 53 // SaveAddress, SaveInstrument, or SaveInstrumentAndAddress. | 51 // SaveToWallet. |
| 54 // 3) Once the user has selected the backing instrument and shipping address | 52 // 3) Once the user has selected the backing instrument and shipping address |
| 55 // for this transaction, a FullWallet with the fronting card is generated | 53 // for this transaction, a FullWallet with the fronting card is generated |
| 56 // using GetFullWallet. | 54 // using GetFullWallet. |
| 57 // a) GetFullWallet may return a Risk challenge for the user. In that case, | 55 // a) GetFullWallet may return a Risk challenge for the user. In that case, |
| 58 // the user will need to verify who they are by authenticating their | 56 // the user will need to verify who they are by authenticating their |
| 59 // chosen backing instrument through AuthenticateInstrument | 57 // chosen backing instrument through AuthenticateInstrument |
| 60 // 4) If the user initiated Autocheckout, SendAutocheckoutStatus to notify | 58 // 4) If the user initiated Autocheckout, SendAutocheckoutStatus to notify |
| 61 // Online Wallet of the status flow to record various metrics. | 59 // Online Wallet of the status flow to record various metrics. |
| 62 // | 60 // |
| 63 // WalletClient is designed so only one request to Online Wallet can be outgoing | 61 // WalletClient is designed so only one request to Online Wallet can be outgoing |
| 64 // at any one time. If |HasRequestInProgress()| is true while calling e.g. | 62 // at any one time. If |HasRequestInProgress()| is true while calling e.g. |
| 65 // GetWalletItems(), the request will be queued and started later. Queued | 63 // GetWalletItems(), the request will be queued and started later. Queued |
| 66 // requests start in the order they were received. | 64 // requests start in the order they were received. |
| 67 | 65 |
| 68 class WalletClient | 66 class WalletClient : public net::URLFetcherDelegate { |
| 69 : public net::URLFetcherDelegate, | |
| 70 public EncryptionEscrowClientObserver { | |
| 71 public: | 67 public: |
| 72 // The Risk challenges supported by users of WalletClient. | 68 // The Risk challenges supported by users of WalletClient. |
| 73 enum RiskCapability { | 69 enum RiskCapability { |
| 74 RELOGIN, | 70 RELOGIN, |
| 75 VERIFY_CVC, | 71 VERIFY_CVC, |
| 76 }; | 72 }; |
| 77 | 73 |
| 78 // The type of error returned by Online Wallet. | 74 // The type of error returned by Online Wallet. |
| 79 enum ErrorType { | 75 enum ErrorType { |
| 80 // Errors to display to users. | 76 // Errors to display to users. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // The transaction ID from GetWalletItems. | 119 // The transaction ID from GetWalletItems. |
| 124 std::string google_transaction_id; | 120 std::string google_transaction_id; |
| 125 | 121 |
| 126 // The Risk challenges supported by the user of WalletClient | 122 // The Risk challenges supported by the user of WalletClient |
| 127 std::vector<RiskCapability> risk_capabilities; | 123 std::vector<RiskCapability> risk_capabilities; |
| 128 | 124 |
| 129 private: | 125 private: |
| 130 DISALLOW_ASSIGN(FullWalletRequest); | 126 DISALLOW_ASSIGN(FullWalletRequest); |
| 131 }; | 127 }; |
| 132 | 128 |
| 133 struct UpdateInstrumentRequest { | |
| 134 public: | |
| 135 UpdateInstrumentRequest(const std::string& instrument_id, | |
| 136 const GURL& source_url); | |
| 137 ~UpdateInstrumentRequest(); | |
| 138 | |
| 139 // The id of the instrument being modified. | |
| 140 std::string instrument_id; | |
| 141 | |
| 142 // The new expiration date. If these are set, |card_verification_number| and | |
| 143 // |obfuscated_gaia_id| must be provided. | |
| 144 int expiration_month; | |
| 145 int expiration_year; | |
| 146 | |
| 147 // Used to authenticate the card the user is modifying. | |
| 148 std::string card_verification_number; | |
| 149 | |
| 150 // Used to key the escrow of |card_verification_number|. | |
| 151 std::string obfuscated_gaia_id; | |
| 152 | |
| 153 // The url this call is initiated from. | |
| 154 GURL source_url; | |
| 155 | |
| 156 private: | |
| 157 DISALLOW_ASSIGN(UpdateInstrumentRequest); | |
| 158 }; | |
| 159 | |
| 160 // |context_getter| is reference counted so it has no lifetime or ownership | 129 // |context_getter| is reference counted so it has no lifetime or ownership |
| 161 // requirements. |delegate| must outlive |this|. | 130 // requirements. |delegate| must outlive |this|. |
| 162 WalletClient(net::URLRequestContextGetter* context_getter, | 131 WalletClient(net::URLRequestContextGetter* context_getter, |
| 163 WalletClientDelegate* delegate); | 132 WalletClientDelegate* delegate); |
| 164 | 133 |
| 165 virtual ~WalletClient(); | 134 virtual ~WalletClient(); |
| 166 | 135 |
| 167 // GetWalletItems retrieves the user's online wallet. The WalletItems | 136 // GetWalletItems retrieves the user's online wallet. The WalletItems |
| 168 // returned may require additional action such as presenting legal documents | 137 // returned may require additional action such as presenting legal documents |
| 169 // to the user to be accepted. | 138 // to the user to be accepted. |
| 170 virtual void GetWalletItems(const GURL& source_url); | 139 virtual void GetWalletItems(const GURL& source_url); |
| 171 | 140 |
| 172 // The GetWalletItems call to the Online Wallet backend may require the user | 141 // The GetWalletItems call to the Online Wallet backend may require the user |
| 173 // to accept various legal documents before a FullWallet can be generated. | 142 // to accept various legal documents before a FullWallet can be generated. |
| 174 // The |google_transaction_id| is provided in the response to the | 143 // The |google_transaction_id| is provided in the response to the |
| 175 // GetWalletItems call. If |documents| are empty, |delegate_| will not receive | 144 // GetWalletItems call. If |documents| are empty, |delegate_| will not receive |
| 176 // a corresponding |OnDidAcceptLegalDocuments()| call. | 145 // a corresponding |OnDidAcceptLegalDocuments()| call. |
| 177 virtual void AcceptLegalDocuments( | 146 virtual void AcceptLegalDocuments( |
| 178 const std::vector<WalletItems::LegalDocument*>& documents, | 147 const std::vector<WalletItems::LegalDocument*>& documents, |
| 179 const std::string& google_transaction_id, | 148 const std::string& google_transaction_id, |
| 180 const GURL& source_url); | 149 const GURL& source_url); |
| 181 | 150 |
| 182 // Authenticates that |card_verification_number| is for the backing instrument | 151 // Authenticates that |card_verification_number| is for the backing instrument |
| 183 // with |instrument_id|. |obfuscated_gaia_id| is used as a key when escrowing | 152 // with |instrument_id|. |obfuscated_gaia_id| is used as a key when escrowing |
| 184 // |card_verification_number|. |delegate_| is notified when the request is | 153 // |card_verification_number|. |delegate_| is notified when the request is |
| 185 // complete. Used to respond to Risk challenges. | 154 // complete. Used to respond to Risk challenges. |
| 186 virtual void AuthenticateInstrument( | 155 virtual void AuthenticateInstrument( |
| 187 const std::string& instrument_id, | 156 const std::string& instrument_id, |
| 188 const std::string& card_verification_number, | 157 const std::string& card_verification_number); |
| 189 const std::string& obfuscated_gaia_id); | |
| 190 | 158 |
| 191 // GetFullWallet retrieves the a FullWallet for the user. | 159 // GetFullWallet retrieves the a FullWallet for the user. |
| 192 virtual void GetFullWallet(const FullWalletRequest& full_wallet_request); | 160 virtual void GetFullWallet(const FullWalletRequest& full_wallet_request); |
| 193 | 161 |
| 194 // SaveAddress saves a new shipping address. | 162 // Saves the data in |instrument| and/or |address| to Wallet. |instrument| |
| 195 virtual void SaveAddress(const Address& address, const GURL& source_url); | 163 // does not have to be complete if its being used to update an existing |
| 196 | 164 // instrument, like in the case of expiration date or address only updates. |
| 197 // SaveInstrument saves a new instrument. | 165 virtual void SaveToWallet(scoped_ptr<Instrument> instrument, |
| 198 virtual void SaveInstrument(const Instrument& instrument, | 166 scoped_ptr<Address> address, |
| 199 const std::string& obfuscated_gaia_id, | 167 const GURL& source_url); |
| 200 const GURL& source_url); | |
| 201 | |
| 202 // SaveInstrumentAndAddress saves a new instrument and address. | |
| 203 virtual void SaveInstrumentAndAddress(const Instrument& instrument, | |
| 204 const Address& shipping_address, | |
| 205 const std::string& obfuscated_gaia_id, | |
| 206 const GURL& source_url); | |
| 207 | 168 |
| 208 // SendAutocheckoutStatus is used for tracking the success of Autocheckout | 169 // SendAutocheckoutStatus is used for tracking the success of Autocheckout |
| 209 // flows. |status| is the result of the flow, |source_url| is the domain | 170 // flows. |status| is the result of the flow, |source_url| is the domain |
| 210 // where the purchase occured, and |google_transaction_id| is the same as the | 171 // where the purchase occured, and |google_transaction_id| is the same as the |
| 211 // one provided by GetWalletItems. |latency_statistics| contain statistics | 172 // one provided by GetWalletItems. |latency_statistics| contain statistics |
| 212 // required to measure Autocheckout process. | 173 // required to measure Autocheckout process. |
| 213 virtual void SendAutocheckoutStatus( | 174 virtual void SendAutocheckoutStatus( |
| 214 autofill::AutocheckoutStatus status, | 175 autofill::AutocheckoutStatus status, |
| 215 const GURL& source_url, | 176 const GURL& source_url, |
| 216 const std::vector<AutocheckoutStatistic>& latency_statistics, | 177 const std::vector<AutocheckoutStatistic>& latency_statistics, |
| 217 const std::string& google_transaction_id); | 178 const std::string& google_transaction_id); |
| 218 | 179 |
| 219 // UpdateAddress updates Online Wallet with the data in |address|. | |
| 220 virtual void UpdateAddress(const Address& address, const GURL& source_url); | |
| 221 | |
| 222 // Updates Online Wallet with the data in |update_instrument_request| and, if | |
| 223 // it's provided, |billing_address|. | |
| 224 virtual void UpdateInstrument( | |
| 225 const UpdateInstrumentRequest& update_instrument_request, | |
| 226 scoped_ptr<Address> billing_address); | |
| 227 | |
| 228 bool HasRequestInProgress() const; | 180 bool HasRequestInProgress() const; |
| 229 | 181 |
| 230 // Cancels and clears the current |request_| and |pending_requests_| (if any). | 182 // Cancels and clears the current |request_| and |pending_requests_| (if any). |
| 231 void CancelRequests(); | 183 void CancelRequests(); |
| 232 | 184 |
| 233 private: | 185 private: |
| 234 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, PendingRequest); | 186 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, PendingRequest); |
| 235 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, CancelRequests); | 187 FRIEND_TEST_ALL_PREFIXES(WalletClientTest, CancelRequests); |
| 236 | 188 |
| 237 enum RequestType { | 189 enum RequestType { |
| 238 NO_PENDING_REQUEST, | 190 NO_PENDING_REQUEST, |
| 239 ACCEPT_LEGAL_DOCUMENTS, | 191 ACCEPT_LEGAL_DOCUMENTS, |
| 240 AUTHENTICATE_INSTRUMENT, | 192 AUTHENTICATE_INSTRUMENT, |
| 241 GET_FULL_WALLET, | 193 GET_FULL_WALLET, |
| 242 GET_WALLET_ITEMS, | 194 GET_WALLET_ITEMS, |
| 243 SAVE_ADDRESS, | 195 SAVE_TO_WALLET, |
| 244 SAVE_INSTRUMENT, | |
| 245 SAVE_INSTRUMENT_AND_ADDRESS, | |
| 246 SEND_STATUS, | 196 SEND_STATUS, |
| 247 UPDATE_ADDRESS, | |
| 248 UPDATE_INSTRUMENT, | |
| 249 }; | 197 }; |
| 250 | 198 |
| 251 // Like AcceptLegalDocuments, but takes a vector of document ids. | 199 // Like AcceptLegalDocuments, but takes a vector of document ids. |
| 252 void DoAcceptLegalDocuments( | 200 void DoAcceptLegalDocuments( |
| 253 const std::vector<std::string>& document_ids, | 201 const std::vector<std::string>& document_ids, |
| 254 const std::string& google_transaction_id, | 202 const std::string& google_transaction_id, |
| 255 const GURL& source_url); | 203 const GURL& source_url); |
| 256 | 204 |
| 257 // Posts |post_body| to |url| and notifies |delegate_| when the request is | 205 // Posts |post_body| to |url| with content type |mime_type| and notifies |
| 258 // complete. | 206 // |delegate_| when the request is complete. |
| 259 void MakeWalletRequest(const GURL& url, const std::string& post_body); | 207 void MakeWalletRequest(const GURL& url, |
| 208 const std::string& post_body, |
| 209 const std::string& mime_type); |
| 260 | 210 |
| 261 // Performs bookkeeping tasks for any invalid requests. | 211 // Performs bookkeeping tasks for any invalid requests. |
| 262 void HandleMalformedResponse(); | 212 void HandleMalformedResponse(); |
| 263 void HandleNetworkError(int response_code); | 213 void HandleNetworkError(int response_code); |
| 264 void HandleWalletError(ErrorType error_type); | 214 void HandleWalletError(ErrorType error_type); |
| 265 | 215 |
| 266 // Start the next pending request (if any). | 216 // Start the next pending request (if any). |
| 267 void StartNextPendingRequest(); | 217 void StartNextPendingRequest(); |
| 268 | 218 |
| 269 // net::URLFetcherDelegate: | 219 // net::URLFetcherDelegate: |
| 270 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 220 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 271 | 221 |
| 272 // EncryptionEscrowClientObserver: | |
| 273 virtual void OnDidEncryptOneTimePad( | |
| 274 const std::string& encrypted_one_time_pad, | |
| 275 const std::string& session_material) OVERRIDE; | |
| 276 virtual void OnDidEscrowInstrumentInformation( | |
| 277 const std::string& escrow_handle) OVERRIDE; | |
| 278 virtual void OnDidEscrowCardVerificationNumber( | |
| 279 const std::string& escrow_handle) OVERRIDE; | |
| 280 virtual void OnDidMakeRequest() OVERRIDE; | |
| 281 virtual void OnNetworkError() OVERRIDE; | |
| 282 virtual void OnMalformedResponse() OVERRIDE; | |
| 283 | |
| 284 // Logs an UMA metric for each of the |required_actions|. | 222 // Logs an UMA metric for each of the |required_actions|. |
| 285 void LogRequiredActions( | 223 void LogRequiredActions( |
| 286 const std::vector<RequiredAction>& required_actions) const; | 224 const std::vector<RequiredAction>& required_actions) const; |
| 287 | 225 |
| 288 // Converts |request_type| to an UMA metric. | 226 // Converts |request_type| to an UMA metric. |
| 289 AutofillMetrics::WalletApiCallMetric RequestTypeToUmaMetric( | 227 AutofillMetrics::WalletApiCallMetric RequestTypeToUmaMetric( |
| 290 RequestType request_type) const; | 228 RequestType request_type) const; |
| 291 | 229 |
| 292 // The context for the request. Ensures the gdToken cookie is set as a header | 230 // The context for the request. Ensures the gdToken cookie is set as a header |
| 293 // in the requests to Online Wallet if it is present. | 231 // in the requests to Online Wallet if it is present. |
| 294 scoped_refptr<net::URLRequestContextGetter> context_getter_; | 232 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
| 295 | 233 |
| 296 // Observer class that has its various On* methods called based on the results | 234 // Observer class that has its various On* methods called based on the results |
| 297 // of a request to Online Wallet. | 235 // of a request to Online Wallet. |
| 298 WalletClientDelegate* const delegate_; // must outlive |this|. | 236 WalletClientDelegate* const delegate_; // must outlive |this|. |
| 299 | 237 |
| 300 // The current request object. | 238 // The current request object. |
| 301 scoped_ptr<net::URLFetcher> request_; | 239 scoped_ptr<net::URLFetcher> request_; |
| 302 | 240 |
| 303 // The type of the current request. Must be NO_PENDING_REQUEST for a request | 241 // The type of the current request. Must be NO_PENDING_REQUEST for a request |
| 304 // to be initiated as only one request may be running at a given time. | 242 // to be initiated as only one request may be running at a given time. |
| 305 RequestType request_type_; | 243 RequestType request_type_; |
| 306 | 244 |
| 307 // The one time pad used for GetFullWallet encryption. | 245 // The one time pad used for GetFullWallet encryption. |
| 308 std::vector<uint8> one_time_pad_; | 246 std::vector<uint8> one_time_pad_; |
| 309 | 247 |
| 310 // GetFullWallet requests and requests that alter instruments rely on requests | |
| 311 // made through the |encryption_escrow_client_| finishing first. The request | |
| 312 // body is saved here while that those requests are in flight. | |
| 313 base::DictionaryValue pending_request_body_; | |
| 314 | |
| 315 // Requests that are waiting to be run. | 248 // Requests that are waiting to be run. |
| 316 std::queue<base::Closure> pending_requests_; | 249 std::queue<base::Closure> pending_requests_; |
| 317 | 250 |
| 318 // This client is repsonsible for making encryption and escrow calls to Online | |
| 319 // Wallet. | |
| 320 EncryptionEscrowClient encryption_escrow_client_; | |
| 321 | |
| 322 // When the current request started. Used to track client side latency. | 251 // When the current request started. Used to track client side latency. |
| 323 base::Time request_started_timestamp_; | 252 base::Time request_started_timestamp_; |
| 324 | 253 |
| 325 DISALLOW_COPY_AND_ASSIGN(WalletClient); | 254 DISALLOW_COPY_AND_ASSIGN(WalletClient); |
| 326 }; | 255 }; |
| 327 | 256 |
| 328 } // namespace wallet | 257 } // namespace wallet |
| 329 } // namespace autofill | 258 } // namespace autofill |
| 330 | 259 |
| 331 #endif // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ | 260 #endif // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_ |
| OLD | NEW |