Home | History | Annotate | Download | only in gaia
      1 // Copyright 2014 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 GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
      6 #define GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
      7 
      8 #include <deque>
      9 
     10 #include "base/observer_list.h"
     11 #include "google_apis/gaia/gaia_auth_consumer.h"
     12 #include "google_apis/gaia/ubertoken_fetcher.h"
     13 #include "net/url_request/url_fetcher_delegate.h"
     14 
     15 class GaiaAuthFetcher;
     16 class GoogleServiceAuthError;
     17 class OAuth2TokenService;
     18 
     19 namespace net {
     20 class URLRequestContextGetter;
     21 }
     22 
     23 // Merges a Google account known to Chrome into the cookie jar.  When merging
     24 // multiple accounts, one instance of the helper is better than multiple
     25 // instances if there is the possibility that they run concurrently, since
     26 // changes to the cookie must be serialized.
     27 //
     28 // By default instances of MergeSessionHelper delete themselves when done.
     29 class MergeSessionHelper : public GaiaAuthConsumer,
     30                            public UbertokenConsumer,
     31                            public net::URLFetcherDelegate {
     32  public:
     33   class Observer {
     34    public:
     35     // Called whenever a merge session is completed.  The account that was
     36     // merged is given by |account_id|.  If |error| is equal to
     37     // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded.
     38     virtual void MergeSessionCompleted(const std::string& account_id,
     39                                        const GoogleServiceAuthError& error) = 0;
     40    protected:
     41     virtual ~Observer() {}
     42   };
     43 
     44   MergeSessionHelper(OAuth2TokenService* token_service,
     45                      net::URLRequestContextGetter* request_context,
     46                      Observer* observer);
     47   virtual ~MergeSessionHelper();
     48 
     49   void LogIn(const std::string& account_id);
     50 
     51   // Add or remove observers of this helper.
     52   void AddObserver(Observer* observer);
     53   void RemoveObserver(Observer* observer);
     54 
     55   // Cancel all login requests.
     56   void CancelAll();
     57 
     58   // Signout of |account_id| given a list of accounts already signed in.
     59   // Since this involves signing out of all accounts and resigning back in,
     60   // the order which |accounts| are given is important as it will dictate
     61   // the sign in order. |account_id| does not have to be in |accounts|.
     62   void LogOut(const std::string& account_id,
     63               const std::vector<std::string>& accounts);
     64 
     65   // Signout all accounts.
     66   void LogOutAllAccounts();
     67 
     68   // Call observers when merge session completes.  This public so that callers
     69   // that know that a given account is already in the cookie jar can simply
     70   // inform the observers.
     71   void SignalComplete(const std::string& account_id,
     72                       const GoogleServiceAuthError& error);
     73 
     74  private:
     75   // Overridden from UbertokenConsumer.
     76   virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
     77   virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
     78 
     79   // Overridden from GaiaAuthConsumer.
     80   virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
     81   virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
     82       OVERRIDE;
     83 
     84   void LogOutInternal(const std::string& account_id,
     85                       const std::vector<std::string>& accounts);
     86 
     87   // Starts the proess of fetching the uber token and performing a merge session
     88   // for the next account.  Virtual so that it can be overriden in tests.
     89   virtual void StartFetching();
     90 
     91   // Virtual for testing purpose.
     92   virtual void StartLogOutUrlFetch();
     93 
     94   // Start the next merge session, if needed.
     95   void HandleNextAccount();
     96 
     97   // Overridden from URLFetcherDelgate.
     98   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     99 
    100   OAuth2TokenService* token_service_;
    101   net::URLRequestContextGetter* request_context_;
    102   scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
    103   scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
    104 
    105   // A worklist for this class. Accounts names are stored here if
    106   // we are pending a signin action for that account. Empty strings
    107   // represent a signout request.
    108   std::deque<std::string> accounts_;
    109 
    110   // List of observers to notify when merge session completes.
    111   // Makes sure list is empty on destruction.
    112   ObserverList<Observer, true> observer_list_;
    113 
    114   DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
    115 };
    116 
    117 #endif  // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
    118