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