| OLD | NEW |
| (Empty) |
| 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 | |
| 16 class AccountInfoFetcher; | |
| 17 class OAuth2TokenService; | |
| 18 class PrefService; | |
| 19 | |
| 20 namespace base { | |
| 21 class DictionaryValue; | |
| 22 } | |
| 23 | |
| 24 // AccountTrackerService is a KeyedService that retrieves and caches GAIA | |
| 25 // information about Google Accounts. | |
| 26 class 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 AccountTrackerService(); | |
| 52 virtual ~AccountTrackerService(); | |
| 53 | |
| 54 // KeyedService implementation. | |
| 55 virtual void Shutdown() OVERRIDE; | |
| 56 | |
| 57 void AddObserver(Observer* observer); | |
| 58 void RemoveObserver(Observer* observer); | |
| 59 | |
| 60 void Initialize(OAuth2TokenService* token_service, | |
| 61 PrefService* pref_service, | |
| 62 net::URLRequestContextGetter* request_context_getter); | |
| 63 | |
| 64 // Returns the list of known accounts and for which gaia IDs | |
| 65 // have been fetched. | |
| 66 std::vector<AccountInfo> GetAccounts() const; | |
| 67 AccountInfo GetAccountInfo(const std::string& account_id); | |
| 68 AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id); | |
| 69 AccountInfo FindAccountInfoByEmail(const std::string& email); | |
| 70 | |
| 71 // Indicates if all user information has been fetched. If the result is false, | |
| 72 // there are still unfininshed fetchers. | |
| 73 virtual bool IsAllUserInfoFetched() const; | |
| 74 | |
| 75 private: | |
| 76 friend class AccountInfoFetcher; | |
| 77 | |
| 78 // These methods are called by fetchers. | |
| 79 void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher, | |
| 80 const base::DictionaryValue* user_info); | |
| 81 void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher); | |
| 82 | |
| 83 // OAuth2TokenService::Observer implementation. | |
| 84 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | |
| 85 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; | |
| 86 | |
| 87 struct AccountState { | |
| 88 AccountInfo info; | |
| 89 }; | |
| 90 | |
| 91 void NotifyAccountUpdated(const AccountState& state); | |
| 92 void NotifyAccountRemoved(const AccountState& state); | |
| 93 | |
| 94 void StartTrackingAccount(const std::string& account_id); | |
| 95 void StopTrackingAccount(const std::string& account_id); | |
| 96 void StartFetchingUserInfo(const std::string& account_id); | |
| 97 void DeleteFetcher(AccountInfoFetcher* fetcher); | |
| 98 | |
| 99 // Load the current state of the account info from the preferences file. | |
| 100 void LoadFromPrefs(); | |
| 101 void SaveToPrefs(const AccountState& account); | |
| 102 void RemoveFromPrefs(const AccountState& account); | |
| 103 | |
| 104 void LoadFromTokenService(); | |
| 105 | |
| 106 OAuth2TokenService* token_service_; // Not owned. | |
| 107 PrefService* pref_service_; // Not owned. | |
| 108 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 109 std::map<std::string, AccountInfoFetcher*> user_info_requests_; | |
| 110 std::map<std::string, AccountState> accounts_; | |
| 111 ObserverList<Observer> observer_list_; | |
| 112 bool shutdown_called_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService); | |
| 115 }; | |
| 116 | |
| 117 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ | |
| OLD | NEW |