1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <queue>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"  // For base::Closure.
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/memory/weak_ptr.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_items.h"
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/autofill/core/browser/autofill_client.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_metrics.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest_prod.h"
237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace net {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class URLFetcher;
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class URLRequestContextGetter;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace wallet {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Address;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FullWallet;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Instrument;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class WalletClientDelegate;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// WalletClient is responsible for making calls to the Online Wallet backend on
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the user's behalf. The normal flow for using this class is as follows:
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 1) GetWalletItems should be called to retrieve the user's Wallet.
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   a) If the user does not have a Wallet, they must AcceptLegalDocuments and
42ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//      SaveToWallet to set up their account before continuing.
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   b) If the user has not accepted the most recent legal documents for
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//      Wallet, they must AcceptLegalDocuments.
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 2) The user then chooses what instrument and shipping address to use for the
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    current transaction.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   a) If they choose an instrument with a zip code only address, the billing
48ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//      address will need to be updated using SaveToWallet.
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   b) The user may also choose to add a new instrument or address using
50ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//      SaveToWallet.
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 3) Once the user has selected the backing instrument and shipping address
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    for this transaction, a FullWallet with the fronting card is generated
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//    using GetFullWallet.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   a) GetFullWallet may return a Risk challenge for the user. In that case,
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//      the user will need to verify who they are by authenticating their
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//      chosen backing instrument through AuthenticateInstrument
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// WalletClient is designed so only one request to Online Wallet can be outgoing
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// at any one time. If |HasRequestInProgress()| is true while calling e.g.
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// GetWalletItems(), the request will be queued and started later. Queued
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// requests start in the order they were received.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
63ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochclass WalletClient : public net::URLFetcherDelegate {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The Risk challenges supported by users of WalletClient.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum RiskCapability {
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RELOGIN,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    VERIFY_CVC,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The type of error returned by Online Wallet.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum ErrorType {
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Errors to display to users ----------------------------------------------
74ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    BUYER_ACCOUNT_ERROR,                // Risk deny, unsupported country, or
75ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                        // account closed.
76ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    BUYER_LEGAL_ADDRESS_NOT_SUPPORTED,  // User's Buyer Legal Address is
77ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                        // unsupported by Online Wallet.
78ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS,  // User's "know your customer" KYC
79ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                           // state is not verified (either
80ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                           // KYC_REFER or KYC_FAIL).
81ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    UNSUPPORTED_MERCHANT,               // Merchant is blacklisted due to
82ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                        // compliance violation.
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    SPENDING_LIMIT_EXCEEDED,            // The desired transaction amount was
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                        // over Wallet's limit.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // API errors --------------------------------------------------------------
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Request was very malformed or sent to the wrong endpoint.
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BAD_REQUEST,
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // API call had missing or invalid parameters.
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    INVALID_PARAMS,
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // The server API version of the request is no longer supported.
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    UNSUPPORTED_API_VERSION,
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // The user agent is not supported or a bad Google API key was provided.
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    UNSUPPORTED_USER_AGENT_OR_API_KEY,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Server errors -----------------------------------------------------------
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    INTERNAL_ERROR,           // Unknown server side error.
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SERVICE_UNAVAILABLE,      // Online Wallet is down.
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Other errors ------------------------------------------------------------
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MALFORMED_RESPONSE,       // The response from Wallet was malformed.
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    NETWORK_ERROR,            // The response code of the server was something
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              // other than a 200 or 400.
1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    UNKNOWN_ERROR,            // Catch all error type.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct FullWalletRequest {
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FullWalletRequest(const std::string& instrument_id,
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const std::string& address_id,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const std::string& google_transaction_id,
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      const std::vector<RiskCapability> risk_capabilities,
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      bool new_wallet_user);
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ~FullWalletRequest();
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The ID of the backing instrument. Should have been selected by the user
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // in some UI.
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string instrument_id;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The ID of the shipping address. Should have been selected by the user
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // in some UI.
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string address_id;
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The transaction ID from GetWalletItems.
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string google_transaction_id;
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The Risk challenges supported by the user of WalletClient
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<RiskCapability> risk_capabilities;
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // True if the user does not have Wallet profile.
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    bool new_wallet_user;
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   private:
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DISALLOW_ASSIGN(FullWalletRequest);
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |context_getter| is reference counted so it has no lifetime or ownership
1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // requirements. |delegate| must outlive |this|. |source_url| is the url
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // of the merchant page.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WalletClient(net::URLRequestContextGetter* context_getter,
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               WalletClientDelegate* delegate,
1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               const GURL& source_url);
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~WalletClient();
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GetWalletItems retrieves the user's online wallet. The WalletItems
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // returned may require additional action such as presenting legal documents
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // to the user to be accepted.
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void GetWalletItems(const base::string16& amount,
151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                              const base::string16& currency);
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The GetWalletItems call to the Online Wallet backend may require the user
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to accept various legal documents before a FullWallet can be generated.
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The |google_transaction_id| is provided in the response to the
156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // GetWalletItems call. If |documents| are empty, |delegate_| will not receive
157eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // a corresponding |OnDidAcceptLegalDocuments()| call.
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AcceptLegalDocuments(
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::vector<WalletItems::LegalDocument*>& documents,
1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const std::string& google_transaction_id);
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Authenticates that |card_verification_number| is for the backing instrument
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // with |instrument_id|. |obfuscated_gaia_id| is used as a key when escrowing
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |card_verification_number|. |delegate_| is notified when the request is
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // complete. Used to respond to Risk challenges.
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void AuthenticateInstrument(
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::string& instrument_id,
168ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      const std::string& card_verification_number);
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GetFullWallet retrieves the a FullWallet for the user.
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void GetFullWallet(const FullWalletRequest& full_wallet_request);
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
173ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Saves the data in |instrument| and/or |address| to Wallet. |instrument|
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // does not have to be complete if it's being used to update an existing
175ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // instrument, like in the case of expiration date or address only updates.
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |reference_instrument| and |reference_address| are the original instrument
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and address to be updated on the server (and should be NULL if |instrument|
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // or |address| are new data).
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SaveToWallet(
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Instrument> instrument,
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Address> address,
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const WalletItems::MaskedInstrument* reference_instrument,
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const Address* reference_address);
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HasRequestInProgress() const;
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Cancels and clears the current |request_|.
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CancelRequest();
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets the user index and cancels any pending requests.
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetUserIndex(size_t user_index);
1924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  size_t user_index() const { return user_index_; }
1934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(WalletClientTest, PendingRequest);
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(WalletClientTest, CancelRequests);
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum RequestType {
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    NO_REQUEST,
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ACCEPT_LEGAL_DOCUMENTS,
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AUTHENTICATE_INSTRUMENT,
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GET_FULL_WALLET,
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GET_WALLET_ITEMS,
204ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SAVE_TO_WALLET,
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Like AcceptLegalDocuments, but takes a vector of document ids.
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void DoAcceptLegalDocuments(
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::vector<std::string>& document_ids,
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const std::string& google_transaction_id);
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
212ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Posts |post_body| to |url| with content type |mime_type| and notifies
213ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // |delegate_| when the request is complete.
214ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void MakeWalletRequest(const GURL& url,
215ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                         const std::string& post_body,
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::string& mime_type,
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         RequestType request_type);
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Performs bookkeeping tasks for any invalid requests.
22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void HandleMalformedResponse(RequestType request_type,
22158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                               net::URLFetcher* request);
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void HandleNetworkError(int response_code);
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void HandleWalletError(ErrorType error_type);
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // net::URLFetcherDelegate:
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Logs an UMA metric for each of the |required_actions|.
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LogRequiredActions(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::vector<RequiredAction>& required_actions) const;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Converts |request_type| to an UMA metric.
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillMetrics::WalletApiCallMetric RequestTypeToUmaMetric(
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      RequestType request_type) const;
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The context for the request. Ensures the gdToken cookie is set as a header
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the requests to Online Wallet if it is present.
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<net::URLRequestContextGetter> context_getter_;
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Observer class that has its various On* methods called based on the results
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // of a request to Online Wallet.
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WalletClientDelegate* const delegate_;  // must outlive |this|.
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The index of the user account we're making requests for. The index is into
2454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // GAIA's list of signed in users.
2464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  size_t user_index_;
2474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The URL of the page we're making requests on behalf of.
2494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  GURL source_url_;
2504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The current request object.
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<net::URLFetcher> request_;
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The type of the current request. Must be NO_REQUEST for a request
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to be initiated as only one request may be running at a given time.
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  RequestType request_type_;
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The one time pad used for GetFullWallet encryption.
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<uint8> one_time_pad_;
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the current request started. Used to track client side latency.
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::Time request_started_timestamp_;
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
264a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::WeakPtrFactory<WalletClient> weak_ptr_factory_;
265a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WalletClient);
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace wallet
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_CLIENT_H_
273