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 11 #include "base/observer_list.h" 12 #include "chrome/browser/signin/signin_global_error.h" 13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_source.h" 16 #include "google_apis/gaia/gaia_oauth_client.h" 17 #include "google_apis/gaia/oauth2_token_service.h" 18 19 class GoogleServiceAuthError; 20 class Profile; 21 22 namespace extensions { 23 24 struct AccountIds { 25 std::string account_key; // The account ID used by OAuth2TokenService. 26 std::string gaia; 27 std::string email; 28 }; 29 30 class AccountIdFetcher; 31 32 // The AccountTracker keeps track of what accounts exist on the 33 // profile and the state of their credentials. The tracker fetches the 34 // gaia ID of each account it knows about. 35 // 36 // The AccountTracker maintains these invariants: 37 // 1. Events are only fired after the gaia ID has been fetched. 38 // 2. Add/Remove and SignIn/SignOut pairs are always generated in order. 39 // 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove. 40 class AccountTracker : public OAuth2TokenService::Observer, 41 public content::NotificationObserver, 42 public SigninGlobalError::AuthStatusProvider { 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 // OAuth2TokenService::Observer implementation. 64 virtual void OnRefreshTokenAvailable(const std::string& account_key) OVERRIDE; 65 virtual void OnRefreshTokenRevoked(const std::string& account_key) OVERRIDE; 66 67 // content::NotificationObserver implementation. 68 virtual void Observe(int type, 69 const content::NotificationSource& source, 70 const content::NotificationDetails& details) OVERRIDE; 71 72 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, 73 const std::string& gaia_id); 74 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); 75 76 // AuthStatusProvider implementation. 77 virtual std::string GetAccountId() const OVERRIDE; 78 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; 79 80 private: 81 struct AccountState { 82 AccountIds ids; 83 bool is_signed_in; 84 }; 85 86 void NotifyAccountAdded(const AccountState& account); 87 void NotifyAccountRemoved(const AccountState& account); 88 void NotifySignInChanged(const AccountState& account); 89 90 void ClearAuthError(const std::string& account_key); 91 void UpdateSignInState(const std::string& account_key, bool is_signed_in); 92 93 void StartTrackingAccount(const std::string& account_key); 94 void StopTrackingAccount(const std::string& account_key); 95 void StartFetchingUserInfo(const std::string& account_key); 96 void DeleteFetcher(AccountIdFetcher* fetcher); 97 98 Profile* profile_; 99 std::map<std::string, AccountIdFetcher*> user_info_requests_; 100 std::map<std::string, AccountState> accounts_; 101 std::map<std::string, GoogleServiceAuthError> account_errors_; 102 ObserverList<Observer> observer_list_; 103 content::NotificationRegistrar registrar_; 104 }; 105 106 class AccountIdFetcher : public OAuth2TokenService::Consumer, 107 public gaia::GaiaOAuthClient::Delegate { 108 public: 109 AccountIdFetcher(Profile* profile, 110 AccountTracker* tracker, 111 const std::string& account_key); 112 virtual ~AccountIdFetcher(); 113 account_key()114 const std::string& account_key() { return account_key_; } 115 116 void Start(); 117 118 // OAuth2TokenService::Consumer implementation. 119 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 120 const std::string& access_token, 121 const base::Time& expiration_time) OVERRIDE; 122 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 123 const GoogleServiceAuthError& error) OVERRIDE; 124 125 // gaia::GaiaOAuthClient::Delegate implementation. 126 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; 127 virtual void OnOAuthError() OVERRIDE; 128 virtual void OnNetworkError(int response_code) OVERRIDE; 129 130 private: 131 Profile* profile_; 132 AccountTracker* tracker_; 133 const std::string account_key_; 134 135 scoped_ptr<OAuth2TokenService::Request> login_token_request_; 136 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; 137 }; 138 139 } // namespace extensions 140 141 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_ 142