Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / signin / core / browser / account_tracker_service.h
blob327124e18a9ba7f0cfd17c94efc226ed70d16190
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 <list>
9 #include <map>
10 #include <string>
11 #include <vector>
13 #include "base/memory/ref_counted.h"
14 #include "base/observer_list.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/timer/timer.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "google_apis/gaia/gaia_auth_util.h"
21 class PrefService;
22 class SigninClient;
24 namespace base {
25 class DictionaryValue;
28 namespace user_prefs {
29 class PrefRegistrySyncable;
32 // AccountTrackerService is a KeyedService that retrieves and caches GAIA
33 // information about Google Accounts.
34 class AccountTrackerService : public KeyedService,
35 public base::NonThreadSafe {
36 public:
37 // Name of the preference property that persists the account information
38 // tracked by this service.
39 static const char kAccountInfoPref[];
41 // TODO(mlerman): Remove all references to Profile::kNoHostedDomainFound in
42 // favour of this.
43 // Value representing no hosted domain in the kProfileHostedDomain preference.
44 static const char kNoHostedDomainFound[];
46 // Value representing no picture URL associated with an account.
47 static const char kNoPictureURLFound[];
49 // TODO(knn): Move to ChildAccountInfoFetcher once deprecated service flags
50 // have been migrated from preferences.
51 // Child account service flag name.
52 static const char kChildAccountServiceFlag[];
54 // Information about a specific account.
55 struct AccountInfo {
56 AccountInfo();
57 ~AccountInfo();
59 std::string account_id; // The account ID used by OAuth2TokenService.
60 std::string gaia;
61 std::string email;
62 std::string full_name;
63 std::string given_name;
64 std::string hosted_domain;
65 std::string locale;
66 std::string picture_url;
67 bool is_child_account;
69 bool IsValid() const;
72 // Clients of AccountTrackerService can implement this interface and register
73 // with AddObserver() to learn about account information changes.
74 class Observer {
75 public:
76 virtual ~Observer() {}
77 virtual void OnAccountUpdated(const AccountInfo& info) {}
78 virtual void OnAccountUpdateFailed(const std::string& account_id) {}
79 virtual void OnAccountRemoved(const AccountInfo& info) {}
82 // Possible values for the kAccountIdMigrationState preference.
83 enum AccountIdMigrationState {
84 MIGRATION_NOT_STARTED,
85 MIGRATION_IN_PROGRESS,
86 MIGRATION_DONE
89 AccountTrackerService();
90 ~AccountTrackerService() override;
92 // Registers the preferences used by AccountTrackerService.
93 static void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry);
95 // KeyedService implementation.
96 void Shutdown() override;
98 void AddObserver(Observer* observer);
99 void RemoveObserver(Observer* observer);
101 // Take a SigninClient rather than a PrefService and a URLRequestContextGetter
102 // since RequestContext cannot be created at startup.
103 // (see http://crbug.com/171406)
104 void Initialize(SigninClient* signin_client);
106 // Returns the list of known accounts and for which gaia IDs
107 // have been fetched.
108 std::vector<AccountInfo> GetAccounts() const;
109 AccountInfo GetAccountInfo(const std::string& account_id);
110 AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id);
111 AccountInfo FindAccountInfoByEmail(const std::string& email);
113 // Picks the correct account_id for the specified account depending on the
114 // migration state.
115 std::string PickAccountIdForAccount(const std::string& gaia,
116 const std::string& email);
117 static std::string PickAccountIdForAccount(PrefService* pref_service,
118 const std::string& gaia,
119 const std::string& email);
121 // Seeds the account whose account_id is given by PickAccountIdForAccount()
122 // with its corresponding gaia id and email address. Returns the same
123 // value PickAccountIdForAccount() when given the same arguments.
124 std::string SeedAccountInfo(const std::string& gaia,
125 const std::string& email);
126 void SeedAccountInfo(AccountInfo info);
128 AccountIdMigrationState GetMigrationState();
129 void SetMigrationDone();
130 static AccountIdMigrationState GetMigrationState(PrefService* pref_service);
132 protected:
133 // Available to be called in tests.
134 void SetAccountStateFromUserInfo(const std::string& account_id,
135 const base::DictionaryValue* user_info);
136 void SetIsChildAccount(const std::string& account_id,
137 const bool& is_child_account);
139 private:
140 friend class AccountFetcherService;
141 friend class FakeAccountFetcherService;
142 struct AccountState {
143 AccountInfo info;
146 void NotifyAccountUpdated(const AccountState& state);
147 void NotifyAccountUpdateFailed(const std::string& account_id);
148 void NotifyAccountRemoved(const AccountState& state);
150 void StartTrackingAccount(const std::string& account_id);
151 void StopTrackingAccount(const std::string& account_id);
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 // Gaia id migration.
159 bool IsMigratable();
160 void MigrateToGaiaId();
161 void SetMigrationState(AccountIdMigrationState state);
163 SigninClient* signin_client_; // Not owned.
164 std::map<std::string, AccountState> accounts_;
165 base::ObserverList<Observer> observer_list_;
167 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService);
170 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_