Home | History | Annotate | Download | only in wallet
      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_SIGNIN_HELPER_H_
      6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_SIGNIN_HELPER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "net/url_request/url_fetcher_delegate.h"
     13 
     14 namespace net {
     15 class URLFetcher;
     16 class URLRequestContextGetter;
     17 class URLRequestStatus;
     18 }
     19 
     20 class GoogleServiceAuthError;
     21 
     22 namespace autofill {
     23 namespace wallet {
     24 
     25 class WalletSigninHelperDelegate;
     26 
     27 // Authenticates the user against the Online Wallet service.
     28 // This class is not thread-safe.  An instance may be used on any thread, but
     29 // should not be accessed from multiple threads.
     30 class WalletSigninHelper : public net::URLFetcherDelegate {
     31  public:
     32   // Constructs a helper that works with a given |delegate| and uses a given
     33   // |getter| to obtain a context for URL. Both |delegate| and |getter| shall
     34   // remain valid over the entire lifetime of the created instance.
     35   WalletSigninHelper(WalletSigninHelperDelegate* delegate,
     36                      net::URLRequestContextGetter* getter);
     37 
     38   virtual ~WalletSigninHelper();
     39 
     40   // Initiates an attempt to passively sign the user into the Online Wallet.
     41   // A passive sign-in is a non-interactive refresh of content area cookies,
     42   // and it succeeds as long as the Online Wallet service could safely accept
     43   // or refresh the existing area cookies, and the user doesn't need to be
     44   // fully reauthenticated with the service.
     45   // Either OnPassiveSigninSuccess or OnPassiveSigninFailure will be called
     46   // on the original thread.
     47   void StartPassiveSignin();
     48 
     49   // Initiates a fetch of the user name of a signed-in user.
     50   // Either OnUserNameFetchSuccess or OnUserNameFetchFailure will
     51   // be called on the original thread.
     52   void StartUserNameFetch();
     53 
     54   // Initiates the fetch of the user's Google Wallet cookie.
     55   void StartWalletCookieValueFetch();
     56 
     57  protected:
     58   // Sign-in helper states (for tests).
     59   enum State {
     60     IDLE,
     61     PASSIVE_EXECUTING_SIGNIN,
     62     PASSIVE_FETCHING_USERINFO,
     63     USERNAME_FETCHING_USERINFO,
     64   };
     65 
     66   // (For tests) Current state of the sign-in helper.
     67   State state() const { return state_; }
     68 
     69   // (For tests) URL used to fetch the currently signed-in user info.
     70   std::string GetGetAccountInfoUrlForTesting() const;
     71 
     72  private:
     73   // Called if a service authentication error occurs.
     74   void OnServiceError(const GoogleServiceAuthError& error);
     75 
     76   // Called if any other error occurs.
     77   void OnOtherError();
     78 
     79   // URLFetcherDelegate implementation.
     80   virtual void OnURLFetchComplete(const net::URLFetcher* fetcher) OVERRIDE;
     81 
     82   // Initiates fetching of the currently signed-in user information.
     83   void StartFetchingUserNameFromSession();
     84 
     85   // Processes the user information received from the server by url_fetcher_
     86   // and calls the delegate callbacks on success/failure.
     87   void ProcessGetAccountInfoResponseAndFinish();
     88 
     89   // Attempts to parse a response from the Online Wallet sign-in.
     90   // Returns true if the response is correct and the sign-in has succeeded.
     91   // Otherwise, it calls OnServiceError() and returns false.
     92   bool ParseSignInResponse();
     93 
     94   // Attempts to parse the GetAccountInfo response from the server.
     95   // Returns true on success; the obtained email address is stored into |email|.
     96   bool ParseGetAccountInfoResponse(const net::URLFetcher* fetcher,
     97                                    std::string* email);
     98 
     99   // Callback for when the Google Wallet cookie has been retrieved.
    100   void ReturnWalletCookieValue(const std::string& cookie_value);
    101 
    102   // Should be valid throughout the lifetime of the instance.
    103   WalletSigninHelperDelegate* const delegate_;
    104 
    105   // URLRequestContextGetter to be used for URLFetchers.
    106   net::URLRequestContextGetter* const getter_;
    107 
    108   // While passive login/merge session URL fetches are going on:
    109   scoped_ptr<net::URLFetcher> url_fetcher_;
    110 
    111   // User account name (email) fetched from OnGetUserInfoSuccess().
    112   std::string username_;
    113 
    114   // Current internal state of the helper.
    115   State state_;
    116 
    117   base::WeakPtrFactory<WalletSigninHelper> weak_ptr_factory_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(WalletSigninHelper);
    120 };
    121 
    122 }  // namespace wallet
    123 }  // namespace autofill
    124 
    125 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_SIGNIN_HELPER_H_
    126