Port Android relocation packer to chromium build
[chromium-blink-merge.git] / components / signin / core / browser / account_reconcilor.h
blob1d7adcf4eb12b3afa2da91a435885f81a9ce0042
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.
4 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_
5 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_
7 #include <deque>
8 #include <functional>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/callback_forward.h"
16 #include "base/compiler_specific.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/time/time.h"
20 #include "components/content_settings/core/browser/content_settings_observer.h"
21 #include "components/content_settings/core/common/content_settings_pattern.h"
22 #include "components/keyed_service/core/keyed_service.h"
23 #include "components/signin/core/browser/signin_client.h"
24 #include "components/signin/core/browser/signin_manager.h"
25 #include "google_apis/gaia/gaia_auth_consumer.h"
26 #include "google_apis/gaia/google_service_auth_error.h"
27 #include "google_apis/gaia/merge_session_helper.h"
28 #include "google_apis/gaia/oauth2_token_service.h"
30 class GaiaAuthFetcher;
31 class ProfileOAuth2TokenService;
32 class SigninClient;
34 namespace net {
35 class CanonicalCookie;
38 class AccountReconcilor : public KeyedService,
39 public content_settings::Observer,
40 public GaiaAuthConsumer,
41 public MergeSessionHelper::Observer,
42 public OAuth2TokenService::Observer,
43 public SigninManagerBase::Observer {
44 public:
45 AccountReconcilor(ProfileOAuth2TokenService* token_service,
46 SigninManagerBase* signin_manager,
47 SigninClient* client);
48 ~AccountReconcilor() override;
50 void Initialize(bool start_reconcile_if_tokens_available);
52 // Signal that the status of the new_profile_management flag has changed.
53 // Pass the new status as an explicit parameter since disabling the flag
54 // doesn't remove it from the CommandLine::ForCurrentProcess().
55 void OnNewProfileManagementFlagChanged(bool new_flag_status);
57 // KeyedService implementation.
58 void Shutdown() override;
60 // Add or remove observers for the merge session notification.
61 void AddMergeSessionObserver(MergeSessionHelper::Observer* observer);
62 void RemoveMergeSessionObserver(MergeSessionHelper::Observer* observer);
64 ProfileOAuth2TokenService* token_service() { return token_service_; }
65 SigninClient* client() { return client_; }
67 protected:
68 // Used during GetAccountsFromCookie.
69 // Stores a callback for the next action to perform.
70 typedef base::Callback<
71 void(const GoogleServiceAuthError& error,
72 const std::vector<std::pair<std::string, bool> >&)>
73 GetAccountsFromCookieCallback;
75 virtual void GetAccountsFromCookie(GetAccountsFromCookieCallback callback);
77 private:
78 bool IsRegisteredWithTokenService() const {
79 return registered_with_token_service_;
82 bool AreGaiaAccountsSet() const { return are_gaia_accounts_set_; }
84 const std::vector<std::pair<std::string, bool> >& GetGaiaAccountsForTesting()
85 const {
86 return gaia_accounts_;
89 friend class AccountReconcilorTest;
90 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, SigninManagerRegistration);
91 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, Reauth);
92 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, ProfileAlreadyConnected);
93 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
94 StartReconcileCookiesDisabled);
95 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
96 StartReconcileContentSettings);
97 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
98 StartReconcileContentSettingsGaiaUrl);
99 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
100 StartReconcileContentSettingsNonGaiaUrl);
101 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
102 StartReconcileContentSettingsInvalidPattern);
103 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, GetAccountsFromCookieSuccess);
104 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, GetAccountsFromCookieFailure);
105 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoop);
106 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoopWithDots);
107 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoopMultiple);
108 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileAddToCookie);
109 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
110 StartReconcileRemoveFromCookie);
111 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
112 StartReconcileAddToCookieTwice);
113 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileBadPrimary);
114 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileOnlyOnce);
115 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
116 StartReconcileWithSessionInfoExpiredDefault);
117 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest,
118 MergeSessionCompletedWithBogusAccount);
120 // Register and unregister with dependent services.
121 void RegisterForCookieChanges();
122 void UnregisterForCookieChanges();
123 void RegisterWithSigninManager();
124 void UnregisterWithSigninManager();
125 void RegisterWithTokenService();
126 void UnregisterWithTokenService();
127 void RegisterWithMergeSessionHelper();
128 void UnregisterWithMergeSessionHelper();
129 void RegisterWithContentSettings();
130 void UnregisterWithContentSettings();
132 bool IsProfileConnected();
134 // All actions with side effects. Virtual so that they can be overridden
135 // in tests.
136 virtual void PerformMergeAction(const std::string& account_id);
137 virtual void PerformLogoutAllAccountsAction();
139 // Used during periodic reconciliation.
140 void StartReconcile();
141 void FinishReconcile();
142 void AbortReconcile();
143 void CalculateIfReconcileIsDone();
144 void ScheduleStartReconcileIfChromeAccountsChanged();
146 void ContinueReconcileActionAfterGetGaiaAccounts(
147 const GoogleServiceAuthError& error,
148 const std::vector<std::pair<std::string, bool> >& accounts);
149 void ValidateAccountsFromTokenService();
150 // Note internally that this |account_id| is added to the cookie jar.
151 bool MarkAccountAsAddedToCookie(const std::string& account_id);
153 void OnCookieChanged(const net::CanonicalCookie& cookie, bool removed);
155 // Overriden from content_settings::Observer.
156 void OnContentSettingChanged(
157 const ContentSettingsPattern& primary_pattern,
158 const ContentSettingsPattern& secondary_pattern,
159 ContentSettingsType content_type,
160 std::string resource_identifier) override;
162 // Overriden from GaiaAuthConsumer.
163 void OnListAccountsSuccess(const std::string& data) override;
164 void OnListAccountsFailure(const GoogleServiceAuthError& error) override;
166 // Overriden from MergeSessionHelper::Observer.
167 void MergeSessionCompleted(const std::string& account_id,
168 const GoogleServiceAuthError& error) override;
169 void GetCheckConnectionInfoCompleted(bool succeeded) override;
171 // Overriden from OAuth2TokenService::Observer.
172 void OnEndBatchChanges() override;
174 // Overriden from SigninManagerBase::Observer.
175 void GoogleSigninSucceeded(const std::string& account_id,
176 const std::string& username,
177 const std::string& password) override;
178 void GoogleSignedOut(const std::string& account_id,
179 const std::string& username) override;
181 void MayBeDoNextListAccounts();
183 // The ProfileOAuth2TokenService associated with this reconcilor.
184 ProfileOAuth2TokenService* token_service_;
186 // The SigninManager associated with this reconcilor.
187 SigninManagerBase* signin_manager_;
189 // The SigninClient associated with this reconcilor.
190 SigninClient* client_;
192 MergeSessionHelper merge_session_helper_;
193 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_;
194 bool registered_with_token_service_;
195 bool registered_with_merge_session_helper_;
196 bool registered_with_content_settings_;
198 // True while the reconcilor is busy checking or managing the accounts in
199 // this profile.
200 bool is_reconcile_started_;
201 base::Time m_reconcile_start_time_;
203 // True iff this is the first time the reconcilor is executing.
204 bool first_execution_;
206 // Used during reconcile action.
207 // These members are used to validate the gaia cookie. |gaia_accounts_|
208 // holds the state of google accounts in the gaia cookie. Each element is
209 // a pair that holds the email address of the account and a boolean that
210 // indicates whether the account is valid or not. The accounts in the vector
211 // are ordered the in same way as the gaia cookie.
212 bool are_gaia_accounts_set_;
213 std::vector<std::pair<std::string, bool> > gaia_accounts_;
215 // Used during reconcile action.
216 // These members are used to validate the tokens in OAuth2TokenService.
217 std::string primary_account_;
218 std::vector<std::string> chrome_accounts_;
219 std::vector<std::string> add_to_cookie_;
220 bool chrome_accounts_changed_;
222 std::deque<GetAccountsFromCookieCallback> get_gaia_accounts_callbacks_;
224 scoped_ptr<SigninClient::CookieChangedSubscription>
225 cookie_changed_subscription_;
227 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor);
230 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_