GN + Android: extract android_standalone_library rule.
[chromium-blink-merge.git] / components / signin / core / browser / account_tracker_service.h
blob24477a769d39a5094b6cb536d697c36909591a85
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/containers/scoped_ptr_hash_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
17 class AccountInfoFetcher;
18 class OAuth2TokenService;
19 class PrefService;
20 class RefreshTokenAnnotationRequest;
21 class SigninClient;
23 namespace base {
24 class DictionaryValue;
27 // AccountTrackerService is a KeyedService that retrieves and caches GAIA
28 // information about Google Accounts.
29 class AccountTrackerService : public KeyedService,
30 public OAuth2TokenService::Observer {
31 public:
32 // Name of the preference property that persists the account information
33 // tracked by this service.
34 static const char kAccountInfoPref[];
36 // TODO(mlerman): Remove all references to Profile::kNoHostedDomainFound in
37 // favour of this.
38 // Value representing no hosted domain in the kProfileHostedDomain preference.
39 static const char kNoHostedDomainFound[];
41 // Information about a specific account.
42 struct AccountInfo {
43 AccountInfo();
44 ~AccountInfo();
46 std::string account_id; // The account ID used by OAuth2TokenService.
47 std::string gaia;
48 std::string email;
49 std::string hosted_domain;
50 // TODO(rogerta): eventually this structure will include other information
51 // about the account, like full name, profile picture URL, etc.
53 bool IsValid();
56 // Clients of AccountTrackerService can implement this interface and register
57 // with AddObserver() to learn about account information changes.
58 class Observer {
59 public:
60 virtual ~Observer() {}
61 virtual void OnAccountUpdated(const AccountInfo& info) {}
62 virtual void OnAccountUpdateFailed(const std::string& account_id) {}
63 virtual void OnAccountRemoved(const AccountInfo& info) {}
66 // Possible values for the kAccountIdMigrationState preference.
67 enum AccountIdMigrationState {
68 MIGRATION_NOT_STARTED,
69 MIGRATION_IN_PROGRESS,
70 MIGRATION_DONE
73 AccountTrackerService();
74 ~AccountTrackerService() override;
76 // KeyedService implementation.
77 void Shutdown() override;
79 void AddObserver(Observer* observer);
80 void RemoveObserver(Observer* observer);
82 // Take a SigninClient rather than a PrefService and a URLRequestContextGetter
83 // since RequestContext cannot be created at startup.
84 // (see http://crbug.com/171406)
85 void Initialize(OAuth2TokenService* token_service,
86 SigninClient* signin_client);
88 // Returns the list of known accounts and for which gaia IDs
89 // have been fetched.
90 std::vector<AccountInfo> GetAccounts() const;
91 AccountInfo GetAccountInfo(const std::string& account_id);
92 AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id);
93 AccountInfo FindAccountInfoByEmail(const std::string& email);
95 // Indicates if all user information has been fetched. If the result is false,
96 // there are still unfininshed fetchers.
97 virtual bool IsAllUserInfoFetched() const;
99 // Picks the correct account_id for the specified account depending on the
100 // migration state.
101 std::string PickAccountIdForAccount(const std::string& gaia,
102 const std::string& email);
103 static std::string PickAccountIdForAccount(PrefService* pref_service,
104 const std::string& gaia,
105 const std::string& email);
107 // Seeds the account whose account_id is given by PickAccountIdForAccount()
108 // with its corresponding gaia id and email address.
109 void SeedAccountInfo(const std::string& gaia, const std::string& email);
111 AccountIdMigrationState GetMigrationState();
112 static AccountIdMigrationState GetMigrationState(PrefService* pref_service);
114 protected:
115 // Available to be called in tests.
116 void SetAccountStateFromUserInfo(const std::string& account_id,
117 const base::DictionaryValue* user_info);
119 private:
120 friend class AccountInfoFetcher;
122 // These methods are called by fetchers.
123 void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher,
124 const base::DictionaryValue* user_info);
125 void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher);
127 // OAuth2TokenService::Observer implementation.
128 void OnRefreshTokenAvailable(const std::string& account_id) override;
129 void OnRefreshTokenRevoked(const std::string& account_id) override;
131 struct AccountState {
132 AccountInfo info;
135 void NotifyAccountUpdated(const AccountState& state);
136 void NotifyAccountUpdateFailed(const std::string& account_id);
137 void NotifyAccountRemoved(const AccountState& state);
139 void StartTrackingAccount(const std::string& account_id);
140 void StopTrackingAccount(const std::string& account_id);
142 // Virtual so that tests can override the network fetching behaviour.
143 virtual void StartFetchingUserInfo(const std::string& account_id);
144 void DeleteFetcher(AccountInfoFetcher* fetcher);
146 // Load the current state of the account info from the preferences file.
147 void LoadFromPrefs();
148 void SaveToPrefs(const AccountState& account);
149 void RemoveFromPrefs(const AccountState& account);
151 void LoadFromTokenService();
153 // Virtual so that tests can override the network fetching behaviour.
154 virtual void SendRefreshTokenAnnotationRequest(const std::string& account_id);
155 void RefreshTokenAnnotationRequestDone(const std::string& account_id);
157 OAuth2TokenService* token_service_; // Not owned.
158 SigninClient* signin_client_; // Not owned.
159 std::map<std::string, AccountInfoFetcher*> user_info_requests_;
160 std::map<std::string, AccountState> accounts_;
161 ObserverList<Observer> observer_list_;
162 bool shutdown_called_;
164 // Holds references to refresh token annotation requests keyed by account_id.
165 base::ScopedPtrHashMap<std::string, RefreshTokenAnnotationRequest>
166 refresh_token_annotation_requests_;
168 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService);
171 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_