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_
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
19 class AccountInfoFetcher
;
20 class OAuth2TokenService
;
22 class RefreshTokenAnnotationRequest
;
26 class DictionaryValue
;
29 // AccountTrackerService is a KeyedService that retrieves and caches GAIA
30 // information about Google Accounts.
31 class AccountTrackerService
: public KeyedService
,
32 public OAuth2TokenService::Observer
,
33 public base::NonThreadSafe
{
35 // Name of the preference property that persists the account information
36 // tracked by this service.
37 static const char kAccountInfoPref
[];
39 // TODO(mlerman): Remove all references to Profile::kNoHostedDomainFound in
41 // Value representing no hosted domain in the kProfileHostedDomain preference.
42 static const char kNoHostedDomainFound
[];
44 // Information about a specific account.
49 std::string account_id
; // The account ID used by OAuth2TokenService.
52 std::string hosted_domain
;
53 // TODO(rogerta): eventually this structure will include other information
54 // about the account, like full name, profile picture URL, etc.
59 // Clients of AccountTrackerService can implement this interface and register
60 // with AddObserver() to learn about account information changes.
63 virtual ~Observer() {}
64 virtual void OnAccountUpdated(const AccountInfo
& info
) {}
65 virtual void OnAccountUpdateFailed(const std::string
& account_id
) {}
66 virtual void OnAccountRemoved(const AccountInfo
& info
) {}
69 // Possible values for the kAccountIdMigrationState preference.
70 enum AccountIdMigrationState
{
71 MIGRATION_NOT_STARTED
,
72 MIGRATION_IN_PROGRESS
,
76 AccountTrackerService();
77 ~AccountTrackerService() override
;
79 // KeyedService implementation.
80 void Shutdown() override
;
82 void AddObserver(Observer
* observer
);
83 void RemoveObserver(Observer
* observer
);
85 // Take a SigninClient rather than a PrefService and a URLRequestContextGetter
86 // since RequestContext cannot be created at startup.
87 // (see http://crbug.com/171406)
88 void Initialize(OAuth2TokenService
* token_service
,
89 SigninClient
* signin_client
);
91 // To be called after the Profile is fully initialized; permits network
92 // calls to be executed.
93 void EnableNetworkFetches();
95 // Returns the list of known accounts and for which gaia IDs
97 std::vector
<AccountInfo
> GetAccounts() const;
98 AccountInfo
GetAccountInfo(const std::string
& account_id
);
99 AccountInfo
FindAccountInfoByGaiaId(const std::string
& gaia_id
);
100 AccountInfo
FindAccountInfoByEmail(const std::string
& email
);
102 // Indicates if all user information has been fetched. If the result is false,
103 // there are still unfininshed fetchers.
104 virtual bool IsAllUserInfoFetched() const;
106 // Picks the correct account_id for the specified account depending on the
108 std::string
PickAccountIdForAccount(const std::string
& gaia
,
109 const std::string
& email
);
110 static std::string
PickAccountIdForAccount(PrefService
* pref_service
,
111 const std::string
& gaia
,
112 const std::string
& email
);
114 // Seeds the account whose account_id is given by PickAccountIdForAccount()
115 // with its corresponding gaia id and email address.
116 void SeedAccountInfo(const std::string
& gaia
, const std::string
& email
);
118 AccountIdMigrationState
GetMigrationState();
119 static AccountIdMigrationState
GetMigrationState(PrefService
* pref_service
);
122 // Available to be called in tests.
123 void SetAccountStateFromUserInfo(const std::string
& account_id
,
124 const base::DictionaryValue
* user_info
);
127 friend class AccountInfoFetcher
;
129 // These methods are called by fetchers.
130 void OnUserInfoFetchSuccess(AccountInfoFetcher
* fetcher
,
131 const base::DictionaryValue
* user_info
);
132 void OnUserInfoFetchFailure(AccountInfoFetcher
* fetcher
);
134 // OAuth2TokenService::Observer implementation.
135 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
136 void OnRefreshTokenRevoked(const std::string
& account_id
) override
;
138 struct AccountState
{
142 void NotifyAccountUpdated(const AccountState
& state
);
143 void NotifyAccountUpdateFailed(const std::string
& account_id
);
144 void NotifyAccountRemoved(const AccountState
& state
);
146 void StartTrackingAccount(const std::string
& account_id
);
147 void StopTrackingAccount(const std::string
& account_id
);
149 // Virtual so that tests can override the network fetching behaviour.
150 virtual void StartFetchingUserInfo(const std::string
& account_id
);
151 void DeleteFetcher(AccountInfoFetcher
* fetcher
);
153 // Load the current state of the account info from the preferences file.
154 void LoadFromPrefs();
155 void SaveToPrefs(const AccountState
& account
);
156 void RemoveFromPrefs(const AccountState
& account
);
158 void LoadFromTokenService();
160 // Virtual so that tests can override the network fetching behaviour.
161 virtual void SendRefreshTokenAnnotationRequest(const std::string
& account_id
);
162 void RefreshTokenAnnotationRequestDone(const std::string
& account_id
);
164 OAuth2TokenService
* token_service_
; // Not owned.
165 SigninClient
* signin_client_
; // Not owned.
166 std::map
<std::string
, AccountInfoFetcher
*> user_info_requests_
;
167 std::map
<std::string
, AccountState
> accounts_
;
168 ObserverList
<Observer
> observer_list_
;
169 bool shutdown_called_
;
170 bool network_fetches_enabled_
;
171 std::list
<std::string
> pending_user_info_fetches_
;
173 // Holds references to refresh token annotation requests keyed by account_id.
174 base::ScopedPtrHashMap
<std::string
, RefreshTokenAnnotationRequest
>
175 refresh_token_annotation_requests_
;
177 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService
);
180 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_