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/time/time.h"
19 #include "components/content_settings/core/browser/content_settings_observer.h"
20 #include "components/content_settings/core/common/content_settings_pattern.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "components/signin/core/browser/gaia_cookie_manager_service.h"
23 #include "components/signin/core/browser/signin_client.h"
24 #include "components/signin/core/browser/signin_manager.h"
25 #include "components/signin/core/browser/signin_metrics.h"
26 #include "google_apis/gaia/google_service_auth_error.h"
27 #include "google_apis/gaia/oauth2_token_service.h"
29 class ProfileOAuth2TokenService
;
33 class CanonicalCookie
;
36 class AccountReconcilor
: public KeyedService
,
37 public content_settings::Observer
,
38 public GaiaCookieManagerService::Observer
,
39 public OAuth2TokenService::Observer
,
40 public SigninManagerBase::Observer
{
42 AccountReconcilor(ProfileOAuth2TokenService
* token_service
,
43 SigninManagerBase
* signin_manager
,
45 GaiaCookieManagerService
* cookie_manager_service
);
46 ~AccountReconcilor() override
;
48 void Initialize(bool start_reconcile_if_tokens_available
);
50 // Signal that the status of the new_profile_management flag has changed.
51 // Pass the new status as an explicit parameter since disabling the flag
52 // doesn't remove it from the CommandLine::ForCurrentProcess().
53 void OnNewProfileManagementFlagChanged(bool new_flag_status
);
55 // KeyedService implementation.
56 void Shutdown() override
;
58 // Determine what the reconcilor is currently doing.
59 signin_metrics::AccountReconcilorState
GetState();
62 bool IsRegisteredWithTokenService() const {
63 return registered_with_token_service_
;
66 friend class AccountReconcilorTest
;
67 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, SigninManagerRegistration
);
68 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, Reauth
);
69 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, ProfileAlreadyConnected
);
70 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
71 StartReconcileCookiesDisabled
);
72 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
73 StartReconcileContentSettings
);
74 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
75 StartReconcileContentSettingsGaiaUrl
);
76 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
77 StartReconcileContentSettingsNonGaiaUrl
);
78 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
79 StartReconcileContentSettingsInvalidPattern
);
80 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, GetAccountsFromCookieSuccess
);
81 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, GetAccountsFromCookieFailure
);
82 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoop
);
83 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoopWithDots
);
84 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileNoopMultiple
);
85 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileAddToCookie
);
86 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
87 StartReconcileRemoveFromCookie
);
88 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
89 StartReconcileAddToCookieTwice
);
90 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileBadPrimary
);
91 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileOnlyOnce
);
92 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
93 StartReconcileWithSessionInfoExpiredDefault
);
94 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
95 AddAccountToCookieCompletedWithBogusAccount
);
96 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, NoLoopWithBadPrimary
);
97 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, WontMergeAccountsWithError
);
99 // Register and unregister with dependent services.
100 void RegisterWithSigninManager();
101 void UnregisterWithSigninManager();
102 void RegisterWithTokenService();
103 void UnregisterWithTokenService();
104 void RegisterWithCookieManagerService();
105 void UnregisterWithCookieManagerService();
106 void RegisterWithContentSettings();
107 void UnregisterWithContentSettings();
109 bool IsProfileConnected();
111 // All actions with side effects. Virtual so that they can be overridden
113 virtual void PerformMergeAction(const std::string
& account_id
);
114 virtual void PerformLogoutAllAccountsAction();
116 // Used during periodic reconciliation.
117 void StartReconcile();
118 void FinishReconcile();
119 void AbortReconcile();
120 void CalculateIfReconcileIsDone();
121 void ScheduleStartReconcileIfChromeAccountsChanged();
123 void ValidateAccountsFromTokenService();
124 // Note internally that this |account_id| is added to the cookie jar.
125 bool MarkAccountAsAddedToCookie(const std::string
& account_id
);
127 // Overriden from content_settings::Observer.
128 void OnContentSettingChanged(
129 const ContentSettingsPattern
& primary_pattern
,
130 const ContentSettingsPattern
& secondary_pattern
,
131 ContentSettingsType content_type
,
132 std::string resource_identifier
) override
;
134 // Overriden from GaiaGookieManagerService::Observer.
135 void OnAddAccountToCookieCompleted(
136 const std::string
& account_id
,
137 const GoogleServiceAuthError
& error
) override
;
138 void OnGaiaAccountsInCookieUpdated(
139 const std::vector
<gaia::ListedAccount
>& accounts
,
140 const GoogleServiceAuthError
& error
) override
;
142 // Overriden from OAuth2TokenService::Observer.
143 void OnEndBatchChanges() override
;
145 // Overriden from SigninManagerBase::Observer.
146 void GoogleSigninSucceeded(const std::string
& account_id
,
147 const std::string
& username
,
148 const std::string
& password
) override
;
149 void GoogleSignedOut(const std::string
& account_id
,
150 const std::string
& username
) override
;
152 // The ProfileOAuth2TokenService associated with this reconcilor.
153 ProfileOAuth2TokenService
* token_service_
;
155 // The SigninManager associated with this reconcilor.
156 SigninManagerBase
* signin_manager_
;
158 // The SigninClient associated with this reconcilor.
159 SigninClient
* client_
;
161 // The GaiaCookieManagerService associated with this reconcilor.
162 GaiaCookieManagerService
* cookie_manager_service_
;
164 bool registered_with_token_service_
;
165 bool registered_with_cookie_manager_service_
;
166 bool registered_with_content_settings_
;
168 // True while the reconcilor is busy checking or managing the accounts in
170 bool is_reconcile_started_
;
171 base::Time m_reconcile_start_time_
;
173 // True iff this is the first time the reconcilor is executing.
174 bool first_execution_
;
176 // True iff an error occured during the last attempt to reconcile.
177 bool error_during_last_reconcile_
;
179 // Used during reconcile action.
180 // These members are used to validate the gaia cookie. |gaia_accounts_|
181 // holds the state of google accounts in the gaia cookie. Each element is
182 // holds the email address, gaia id and validity as returned from GAIA. The
183 // accounts in the vector are ordered the in same way as the gaia cookie.
184 std::vector
<gaia::ListedAccount
> gaia_accounts_
;
186 // Used during reconcile action.
187 // These members are used to validate the tokens in OAuth2TokenService.
188 std::string primary_account_
;
189 std::vector
<std::string
> chrome_accounts_
;
190 std::vector
<std::string
> add_to_cookie_
;
191 bool chrome_accounts_changed_
;
193 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor
);
196 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_