account_tracker_service.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ 6#define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/memory/ref_counted.h" 13#include "components/keyed_service/core/keyed_service.h" 14#include "google_apis/gaia/oauth2_token_service.h" 15 16class AccountInfoFetcher; 17class OAuth2TokenService; 18class PrefService; 19 20namespace base { 21class DictionaryValue; 22} 23 24// AccountTrackerService is a KeyedService that retrieves and caches GAIA 25// information about Google Accounts. 26class AccountTrackerService : public KeyedService, 27 public OAuth2TokenService::Observer { 28 public: 29 // Name of the preference property that persists the account information 30 // tracked by this service. 31 static const char kAccountInfoPref[]; 32 33 // Information about a specific account. 34 struct AccountInfo { 35 std::string account_id; // The account ID used by OAuth2TokenService. 36 std::string gaia; 37 std::string email; 38 // TODO(rogerta): eventually this structure will include other information 39 // about the account, like full name, profile picture URL, etc. 40 }; 41 42 // Clients of AccountTrackerService can implement this interface and register 43 // with AddObserver() to learn about account information changes. 44 class Observer { 45 public: 46 virtual ~Observer() {} 47 virtual void OnAccountUpdated(const AccountInfo& info) = 0; 48 virtual void OnAccountRemoved(const AccountInfo& info) = 0; 49 }; 50 51 // Possible values for the kAccountIdMigrationState preference. 52 enum AccountIdMigrationState { 53 MIGRATION_NOT_STARTED, 54 MIGRATION_IN_PROGRESS, 55 MIGRATION_DONE 56 }; 57 58 AccountTrackerService(); 59 virtual ~AccountTrackerService(); 60 61 // KeyedService implementation. 62 virtual void Shutdown() OVERRIDE; 63 64 void AddObserver(Observer* observer); 65 void RemoveObserver(Observer* observer); 66 67 void Initialize(OAuth2TokenService* token_service, 68 PrefService* pref_service, 69 net::URLRequestContextGetter* request_context_getter); 70 71 // Returns the list of known accounts and for which gaia IDs 72 // have been fetched. 73 std::vector<AccountInfo> GetAccounts() const; 74 AccountInfo GetAccountInfo(const std::string& account_id); 75 AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id); 76 AccountInfo FindAccountInfoByEmail(const std::string& email); 77 78 // Indicates if all user information has been fetched. If the result is false, 79 // there are still unfininshed fetchers. 80 virtual bool IsAllUserInfoFetched() const; 81 82 AccountIdMigrationState GetMigrationState(); 83 static AccountIdMigrationState GetMigrationState(PrefService* pref_service); 84 85 private: 86 friend class AccountInfoFetcher; 87 88 // These methods are called by fetchers. 89 void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher, 90 const base::DictionaryValue* user_info); 91 void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher); 92 93 // OAuth2TokenService::Observer implementation. 94 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; 95 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; 96 97 struct AccountState { 98 AccountInfo info; 99 }; 100 101 void NotifyAccountUpdated(const AccountState& state); 102 void NotifyAccountRemoved(const AccountState& state); 103 104 void StartTrackingAccount(const std::string& account_id); 105 void StopTrackingAccount(const std::string& account_id); 106 107 // Virtual so that tests can override the network fetching behaviour. 108 virtual void StartFetchingUserInfo(const std::string& account_id); 109 void DeleteFetcher(AccountInfoFetcher* fetcher); 110 111 // Load the current state of the account info from the preferences file. 112 void LoadFromPrefs(); 113 void SaveToPrefs(const AccountState& account); 114 void RemoveFromPrefs(const AccountState& account); 115 116 void LoadFromTokenService(); 117 118 OAuth2TokenService* token_service_; // Not owned. 119 PrefService* pref_service_; // Not owned. 120 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 121 std::map<std::string, AccountInfoFetcher*> user_info_requests_; 122 std::map<std::string, AccountState> accounts_; 123 ObserverList<Observer> observer_list_; 124 bool shutdown_called_; 125 126 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService); 127}; 128 129#endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ 130