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.
5 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_
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"
16 class AccountInfoFetcher
;
17 class OAuth2TokenService
;
21 class DictionaryValue
;
24 // AccountTrackerService is a KeyedService that retrieves and caches GAIA
25 // information about Google Accounts.
26 class AccountTrackerService
: public KeyedService
,
27 public OAuth2TokenService::Observer
{
29 // Name of the preference property that persists the account information
30 // tracked by this service.
31 static const char kAccountInfoPref
[];
33 // Information about a specific account.
35 std::string account_id
; // The account ID used by OAuth2TokenService.
38 // TODO(rogerta): eventually this structure will include other information
39 // about the account, like full name, profile picture URL, etc.
42 // Clients of AccountTrackerService can implement this interface and register
43 // with AddObserver() to learn about account information changes.
46 virtual ~Observer() {}
47 virtual void OnAccountUpdated(const AccountInfo
& info
) = 0;
48 virtual void OnAccountRemoved(const AccountInfo
& info
) = 0;
51 // Possible values for the kAccountIdMigrationState preference.
52 enum AccountIdMigrationState
{
53 MIGRATION_NOT_STARTED
,
54 MIGRATION_IN_PROGRESS
,
58 AccountTrackerService();
59 ~AccountTrackerService() override
;
61 // KeyedService implementation.
62 void Shutdown() override
;
64 void AddObserver(Observer
* observer
);
65 void RemoveObserver(Observer
* observer
);
67 void Initialize(OAuth2TokenService
* token_service
,
68 PrefService
* pref_service
,
69 net::URLRequestContextGetter
* request_context_getter
);
71 // Returns the list of known accounts and for which gaia IDs
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
);
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;
82 // Picks the correct account_id for the specified account depending on the
84 std::string
PickAccountIdForAccount(const std::string
& gaia
,
85 const std::string
& email
);
86 static std::string
PickAccountIdForAccount(PrefService
* pref_service
,
87 const std::string
& gaia
,
88 const std::string
& email
);
90 // Seeds the account whose account_id is given by PickAccountIdForAccount()
91 // with its corresponding gaia id and email address.
92 void SeedAccountInfo(const std::string
& gaia
, const std::string
& email
);
94 AccountIdMigrationState
GetMigrationState();
95 static AccountIdMigrationState
GetMigrationState(PrefService
* pref_service
);
98 friend class AccountInfoFetcher
;
100 // These methods are called by fetchers.
101 void OnUserInfoFetchSuccess(AccountInfoFetcher
* fetcher
,
102 const base::DictionaryValue
* user_info
);
103 void OnUserInfoFetchFailure(AccountInfoFetcher
* fetcher
);
105 // OAuth2TokenService::Observer implementation.
106 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
107 void OnRefreshTokenRevoked(const std::string
& account_id
) override
;
109 struct AccountState
{
113 void NotifyAccountUpdated(const AccountState
& state
);
114 void NotifyAccountRemoved(const AccountState
& state
);
116 void StartTrackingAccount(const std::string
& account_id
);
117 void StopTrackingAccount(const std::string
& account_id
);
119 // Virtual so that tests can override the network fetching behaviour.
120 virtual void StartFetchingUserInfo(const std::string
& account_id
);
121 void DeleteFetcher(AccountInfoFetcher
* fetcher
);
123 // Load the current state of the account info from the preferences file.
124 void LoadFromPrefs();
125 void SaveToPrefs(const AccountState
& account
);
126 void RemoveFromPrefs(const AccountState
& account
);
128 void LoadFromTokenService();
130 OAuth2TokenService
* token_service_
; // Not owned.
131 PrefService
* pref_service_
; // Not owned.
132 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
133 std::map
<std::string
, AccountInfoFetcher
*> user_info_requests_
;
134 std::map
<std::string
, AccountState
> accounts_
;
135 ObserverList
<Observer
> observer_list_
;
136 bool shutdown_called_
;
138 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService
);
141 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_