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_
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/keyed_service/core/keyed_service.h"
21 #include "components/signin/core/browser/signin_client.h"
22 #include "components/signin/core/browser/signin_manager.h"
23 #include "google_apis/gaia/gaia_auth_consumer.h"
24 #include "google_apis/gaia/google_service_auth_error.h"
25 #include "google_apis/gaia/merge_session_helper.h"
26 #include "google_apis/gaia/oauth2_token_service.h"
28 class GaiaAuthFetcher
;
29 class ProfileOAuth2TokenService
;
33 class CanonicalCookie
;
36 class AccountReconcilor
: public KeyedService
,
37 public GaiaAuthConsumer
,
38 public MergeSessionHelper::Observer
,
39 public OAuth2TokenService::Observer
,
40 public SigninManagerBase::Observer
{
42 AccountReconcilor(ProfileOAuth2TokenService
* token_service
,
43 SigninManagerBase
* signin_manager
,
44 SigninClient
* client
);
45 ~AccountReconcilor() override
;
47 void Initialize(bool start_reconcile_if_tokens_available
);
49 // Signal that the status of the new_profile_management flag has changed.
50 // Pass the new status as an explicit parameter since disabling the flag
51 // doesn't remove it from the CommandLine::ForCurrentProcess().
52 void OnNewProfileManagementFlagChanged(bool new_flag_status
);
54 // KeyedService implementation.
55 void Shutdown() override
;
57 // Add or remove observers for the merge session notification.
58 void AddMergeSessionObserver(MergeSessionHelper::Observer
* observer
);
59 void RemoveMergeSessionObserver(MergeSessionHelper::Observer
* observer
);
61 ProfileOAuth2TokenService
* token_service() { return token_service_
; }
62 SigninClient
* client() { return client_
; }
65 // Used during GetAccountsFromCookie.
66 // Stores a callback for the next action to perform.
67 typedef base::Callback
<
68 void(const GoogleServiceAuthError
& error
,
69 const std::vector
<std::pair
<std::string
, bool> >&)>
70 GetAccountsFromCookieCallback
;
72 virtual void GetAccountsFromCookie(GetAccountsFromCookieCallback callback
);
75 bool IsRegisteredWithTokenService() const {
76 return registered_with_token_service_
;
79 bool AreGaiaAccountsSet() const { return are_gaia_accounts_set_
; }
81 const std::vector
<std::pair
<std::string
, bool> >& GetGaiaAccountsForTesting()
83 return gaia_accounts_
;
86 // Virtual so that it can be overridden in tests.
87 virtual void StartFetchingExternalCcResult();
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
, GetAccountsFromCookieSuccess
);
94 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, GetAccountsFromCookieFailure
);
95 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoop
);
96 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoopWithDots
);
97 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoopMultiple
);
98 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileAddToCookie
);
99 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
100 StartReconcileRemoveFromCookie
);
101 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
102 StartReconcileAddToCookieTwice
);
103 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileBadPrimary
);
104 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileOnlyOnce
);
105 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
106 StartReconcileWithSessionInfoExpiredDefault
);
107 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
108 MergeSessionCompletedWithBogusAccount
);
110 // Register and unregister with dependent services.
111 void RegisterForCookieChanges();
112 void UnregisterForCookieChanges();
113 void RegisterWithSigninManager();
114 void UnregisterWithSigninManager();
115 void RegisterWithTokenService();
116 void UnregisterWithTokenService();
118 bool IsProfileConnected();
120 // All actions with side effects. Virtual so that they can be overridden
122 virtual void PerformMergeAction(const std::string
& account_id
);
123 virtual void PerformLogoutAllAccountsAction();
125 // Used during periodic reconciliation.
126 void StartReconcile();
127 void FinishReconcile();
128 void AbortReconcile();
129 void CalculateIfReconcileIsDone();
130 void ScheduleStartReconcileIfChromeAccountsChanged();
132 void ContinueReconcileActionAfterGetGaiaAccounts(
133 const GoogleServiceAuthError
& error
,
134 const std::vector
<std::pair
<std::string
, bool> >& accounts
);
135 void ValidateAccountsFromTokenService();
136 // Note internally that this |account_id| is added to the cookie jar.
137 bool MarkAccountAsAddedToCookie(const std::string
& account_id
);
139 void OnCookieChanged(const net::CanonicalCookie
& cookie
, bool removed
);
141 // Overriden from GaiaAuthConsumer.
142 void OnListAccountsSuccess(const std::string
& data
) override
;
143 void OnListAccountsFailure(const GoogleServiceAuthError
& error
) override
;
145 // Overriden from MergeSessionHelper::Observer.
146 void MergeSessionCompleted(const std::string
& account_id
,
147 const GoogleServiceAuthError
& error
) override
;
148 void GetCheckConnectionInfoCompleted(bool succeeded
) override
;
150 // Overriden from OAuth2TokenService::Observer.
151 void OnEndBatchChanges() override
;
153 // Overriden from SigninManagerBase::Observer.
154 void GoogleSigninSucceeded(const std::string
& account_id
,
155 const std::string
& username
,
156 const std::string
& password
) override
;
157 void GoogleSignedOut(const std::string
& account_id
,
158 const std::string
& username
) override
;
160 void MayBeDoNextListAccounts();
162 // The ProfileOAuth2TokenService associated with this reconcilor.
163 ProfileOAuth2TokenService
* token_service_
;
165 // The SigninManager associated with this reconcilor.
166 SigninManagerBase
* signin_manager_
;
168 // The SigninClient associated with this reconcilor.
169 SigninClient
* client_
;
171 MergeSessionHelper merge_session_helper_
;
172 scoped_ptr
<GaiaAuthFetcher
> gaia_fetcher_
;
173 bool registered_with_token_service_
;
175 // True while the reconcilor is busy checking or managing the accounts in
177 bool is_reconcile_started_
;
178 base::Time m_reconcile_start_time_
;
180 // True iff this is the first time the reconcilor is executing.
181 bool first_execution_
;
183 // Used during reconcile action.
184 // These members are used to validate the gaia cookie. |gaia_accounts_|
185 // holds the state of google accounts in the gaia cookie. Each element is
186 // a pair that holds the email address of the account and a boolean that
187 // indicates whether the account is valid or not. The accounts in the vector
188 // are ordered the in same way as the gaia cookie.
189 bool are_gaia_accounts_set_
;
190 std::vector
<std::pair
<std::string
, bool> > gaia_accounts_
;
192 // Used during reconcile action.
193 // These members are used to validate the tokens in OAuth2TokenService.
194 std::string primary_account_
;
195 std::vector
<std::string
> chrome_accounts_
;
196 std::vector
<std::string
> add_to_cookie_
;
198 std::deque
<GetAccountsFromCookieCallback
> get_gaia_accounts_callbacks_
;
200 scoped_ptr
<SigninClient::CookieChangedSubscription
>
201 cookie_changed_subscription_
;
203 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor
);
206 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_