Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / signin / core / browser / account_tracker_service.h
blob2a05ed2fff4f098b9b81d1baaf0f0515ffc2e5b2
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 "components/signin/core/browser/account_info.h"
19 #include "google_apis/gaia/gaia_auth_util.h"
22 class PrefService;
23 class SigninClient;
25 namespace base {
26 class DictionaryValue;
29 namespace user_prefs {
30 class PrefRegistrySyncable;
33 // AccountTrackerService is a KeyedService that retrieves and caches GAIA
34 // information about Google Accounts.
35 class AccountTrackerService : public KeyedService,
36 public base::NonThreadSafe {
37 public:
38 // Name of the preference property that persists the account information
39 // tracked by this service.
40 static const char kAccountInfoPref[];
42 // TODO(mlerman): Remove all references to Profile::kNoHostedDomainFound in
43 // favour of this.
44 // Value representing no hosted domain in the kProfileHostedDomain preference.
45 static const char kNoHostedDomainFound[];
47 // Value representing no picture URL associated with an account.
48 static const char kNoPictureURLFound[];
50 // TODO(knn): Move to ChildAccountInfoFetcher once deprecated service flags
51 // have been migrated from preferences.
52 // Child account service flag name.
53 static const char kChildAccountServiceFlag[];
55 // Clients of AccountTrackerService can implement this interface and register
56 // with AddObserver() to learn about account information changes.
57 class Observer {
58 public:
59 virtual ~Observer() {}
60 virtual void OnAccountUpdated(const AccountInfo& info) {}
61 virtual void OnAccountUpdateFailed(const std::string& account_id) {}
62 virtual void OnAccountRemoved(const AccountInfo& info) {}
65 // Possible values for the kAccountIdMigrationState preference.
66 enum AccountIdMigrationState {
67 MIGRATION_NOT_STARTED,
68 MIGRATION_IN_PROGRESS,
69 MIGRATION_DONE
72 AccountTrackerService();
73 ~AccountTrackerService() override;
75 // Registers the preferences used by AccountTrackerService.
76 static void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry);
78 // KeyedService implementation.
79 void Shutdown() override;
81 void AddObserver(Observer* observer);
82 void RemoveObserver(Observer* observer);
84 // Take a SigninClient rather than a PrefService and a URLRequestContextGetter
85 // since RequestContext cannot be created at startup.
86 // (see http://crbug.com/171406)
87 void Initialize(SigninClient* signin_client);
89 // Returns the list of known accounts and for which gaia IDs
90 // have been fetched.
91 std::vector<AccountInfo> GetAccounts() const;
92 AccountInfo GetAccountInfo(const std::string& account_id);
93 AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id);
94 AccountInfo FindAccountInfoByEmail(const std::string& email);
96 // Picks the correct account_id for the specified account depending on the
97 // migration state.
98 std::string PickAccountIdForAccount(const std::string& gaia,
99 const std::string& email);
100 static std::string PickAccountIdForAccount(PrefService* pref_service,
101 const std::string& gaia,
102 const std::string& email);
104 // Seeds the account whose account_id is given by PickAccountIdForAccount()
105 // with its corresponding gaia id and email address. Returns the same
106 // value PickAccountIdForAccount() when given the same arguments.
107 std::string SeedAccountInfo(const std::string& gaia,
108 const std::string& email);
109 void SeedAccountInfo(AccountInfo info);
111 void RemoveAccount(const std::string& account_id);
113 AccountIdMigrationState GetMigrationState();
114 void SetMigrationDone();
115 static AccountIdMigrationState GetMigrationState(PrefService* pref_service);
117 protected:
118 // Available to be called in tests.
119 void SetAccountStateFromUserInfo(const std::string& account_id,
120 const base::DictionaryValue* user_info);
121 void SetIsChildAccount(const std::string& account_id,
122 const bool& is_child_account);
124 private:
125 friend class AccountFetcherService;
126 friend class FakeAccountFetcherService;
127 struct AccountState {
128 AccountInfo info;
131 void NotifyAccountUpdated(const AccountState& state);
132 void NotifyAccountUpdateFailed(const std::string& account_id);
133 void NotifyAccountRemoved(const AccountState& state);
135 void StartTrackingAccount(const std::string& account_id);
136 void StopTrackingAccount(const std::string& account_id);
138 // Load the current state of the account info from the preferences file.
139 void LoadFromPrefs();
140 void SaveToPrefs(const AccountState& account);
141 void RemoveFromPrefs(const AccountState& account);
143 // Gaia id migration.
144 bool IsMigratable();
145 void MigrateToGaiaId();
146 void SetMigrationState(AccountIdMigrationState state);
148 SigninClient* signin_client_; // Not owned.
149 std::map<std::string, AccountState> accounts_;
150 base::ObserverList<Observer> observer_list_;
152 DISALLOW_COPY_AND_ASSIGN(AccountTrackerService);
155 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_