Home | History | Annotate | Download | only in identity
      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 CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/observer_list.h"
     13 #include "components/signin/core/browser/signin_error_controller.h"
     14 #include "components/signin/core/browser/signin_manager_base.h"
     15 #include "google_apis/gaia/gaia_oauth_client.h"
     16 #include "google_apis/gaia/oauth2_token_service.h"
     17 
     18 class GoogleServiceAuthError;
     19 class Profile;
     20 
     21 namespace extensions {
     22 
     23 struct AccountIds {
     24   std::string account_key;  // The account ID used by OAuth2TokenService.
     25   std::string gaia;
     26   std::string email;
     27 };
     28 
     29 class AccountIdFetcher;
     30 
     31 // The AccountTracker keeps track of what accounts exist on the
     32 // profile and the state of their credentials. The tracker fetches the
     33 // gaia ID of each account it knows about.
     34 //
     35 // The AccountTracker maintains these invariants:
     36 // 1. Events are only fired after the gaia ID has been fetched.
     37 // 2. Add/Remove and SignIn/SignOut pairs are always generated in order.
     38 // 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove.
     39 // 4. If there is no primary account, there are no other accounts.
     40 class AccountTracker : public OAuth2TokenService::Observer,
     41                        public SigninErrorController::AuthStatusProvider,
     42                        public SigninManagerBase::Observer {
     43  public:
     44   explicit AccountTracker(Profile* profile);
     45   virtual ~AccountTracker();
     46 
     47   class Observer {
     48    public:
     49     virtual void OnAccountAdded(const AccountIds& ids) = 0;
     50     virtual void OnAccountRemoved(const AccountIds& ids) = 0;
     51     virtual void OnAccountSignInChanged(const AccountIds& ids,
     52                                         bool is_signed_in) = 0;
     53   };
     54 
     55   void Shutdown();
     56 
     57   void ReportAuthError(const std::string& account_key,
     58                        const GoogleServiceAuthError& error);
     59 
     60   void AddObserver(Observer* observer);
     61   void RemoveObserver(Observer* observer);
     62 
     63   // Returns the list of accounts that are signed in, and for which gaia IDs
     64   // have been fetched. The primary account for the profile will be first
     65   // in the vector. Additional accounts will be in order of their gaia IDs.
     66   std::vector<AccountIds> GetAccounts() const;
     67   std::string FindAccountKeyByGaiaId(const std::string& gaia_id);
     68 
     69   // OAuth2TokenService::Observer implementation.
     70   virtual void OnRefreshTokenAvailable(const std::string& account_key) OVERRIDE;
     71   virtual void OnRefreshTokenRevoked(const std::string& account_key) OVERRIDE;
     72 
     73   void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
     74                               const std::string& gaia_id);
     75   void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
     76 
     77   // AuthStatusProvider implementation.
     78   virtual std::string GetAccountId() const OVERRIDE;
     79   virtual std::string GetUsername() const OVERRIDE;
     80   virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
     81 
     82   // SigninManagerBase::Observer implementation.
     83   virtual void GoogleSigninSucceeded(const std::string& username,
     84                                      const std::string& password) OVERRIDE;
     85   virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
     86 
     87   // Sets the state of an account. Does not fire notifications.
     88   void SetAccountStateForTest(AccountIds ids, bool is_signed_in);
     89 
     90  private:
     91   struct AccountState {
     92     AccountIds ids;
     93     bool is_signed_in;
     94   };
     95 
     96   const std::string signin_manager_account_id() const;
     97 
     98   void NotifyAccountAdded(const AccountState& account);
     99   void NotifyAccountRemoved(const AccountState& account);
    100   void NotifySignInChanged(const AccountState& account);
    101 
    102   void ClearAuthError(const std::string& account_key);
    103   void UpdateSignInState(const std::string& account_key, bool is_signed_in);
    104 
    105   void StartTrackingAccount(const std::string& account_key);
    106   void StopTrackingAccount(const std::string& account_key);
    107   void StopTrackingAllAccounts();
    108   void StartFetchingUserInfo(const std::string& account_key);
    109   void DeleteFetcher(AccountIdFetcher* fetcher);
    110 
    111   Profile* profile_;
    112   std::map<std::string, AccountIdFetcher*> user_info_requests_;
    113   std::map<std::string, AccountState> accounts_;
    114   std::map<std::string, GoogleServiceAuthError> account_errors_;
    115   ObserverList<Observer> observer_list_;
    116 };
    117 
    118 class AccountIdFetcher : public OAuth2TokenService::Consumer,
    119                          public gaia::GaiaOAuthClient::Delegate {
    120  public:
    121   AccountIdFetcher(Profile* profile,
    122                    AccountTracker* tracker,
    123                    const std::string& account_key);
    124   virtual ~AccountIdFetcher();
    125 
    126   const std::string& account_key() { return account_key_; }
    127 
    128   void Start();
    129 
    130   // OAuth2TokenService::Consumer implementation.
    131   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
    132                                  const std::string& access_token,
    133                                  const base::Time& expiration_time) OVERRIDE;
    134   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
    135                                  const GoogleServiceAuthError& error) OVERRIDE;
    136 
    137   // gaia::GaiaOAuthClient::Delegate implementation.
    138   virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE;
    139   virtual void OnOAuthError() OVERRIDE;
    140   virtual void OnNetworkError(int response_code) OVERRIDE;
    141 
    142  private:
    143   Profile* profile_;
    144   AccountTracker* tracker_;
    145   const std::string account_key_;
    146 
    147   scoped_ptr<OAuth2TokenService::Request> login_token_request_;
    148   scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
    149 };
    150 
    151 }  // namespace extensions
    152 
    153 #endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_
    154