OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ | 4 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ |
5 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ | 5 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <functional> | 8 #include <functional> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "components/content_settings/core/browser/content_settings_observer.h" | 20 #include "components/content_settings/core/browser/content_settings_observer.h" |
21 #include "components/content_settings/core/common/content_settings_pattern.h" | 21 #include "components/content_settings/core/common/content_settings_pattern.h" |
22 #include "components/keyed_service/core/keyed_service.h" | 22 #include "components/keyed_service/core/keyed_service.h" |
23 #include "components/signin/core/browser/gaia_cookie_manager_service.h" | 23 #include "components/signin/core/browser/gaia_cookie_manager_service.h" |
24 #include "components/signin/core/browser/signin_client.h" | 24 #include "components/signin/core/browser/signin_client.h" |
25 #include "components/signin/core/browser/signin_manager.h" | 25 #include "components/signin/core/browser/signin_manager.h" |
26 #include "google_apis/gaia/gaia_auth_consumer.h" | |
27 #include "google_apis/gaia/google_service_auth_error.h" | 26 #include "google_apis/gaia/google_service_auth_error.h" |
28 #include "google_apis/gaia/oauth2_token_service.h" | 27 #include "google_apis/gaia/oauth2_token_service.h" |
29 | 28 |
30 class GaiaAuthFetcher; | 29 class GaiaAuthFetcher; |
31 class ProfileOAuth2TokenService; | 30 class ProfileOAuth2TokenService; |
32 class SigninClient; | 31 class SigninClient; |
33 | 32 |
34 namespace net { | 33 namespace net { |
35 class CanonicalCookie; | 34 class CanonicalCookie; |
36 } | 35 } |
37 | 36 |
38 class AccountReconcilor : public KeyedService, | 37 class AccountReconcilor : public KeyedService, |
39 public content_settings::Observer, | 38 public content_settings::Observer, |
40 public GaiaAuthConsumer, | |
41 public GaiaCookieManagerService::Observer, | 39 public GaiaCookieManagerService::Observer, |
42 public OAuth2TokenService::Observer, | 40 public OAuth2TokenService::Observer, |
43 public SigninManagerBase::Observer { | 41 public SigninManagerBase::Observer { |
44 public: | 42 public: |
45 AccountReconcilor(ProfileOAuth2TokenService* token_service, | 43 AccountReconcilor(ProfileOAuth2TokenService* token_service, |
46 SigninManagerBase* signin_manager, | 44 SigninManagerBase* signin_manager, |
47 SigninClient* client, | 45 SigninClient* client, |
48 GaiaCookieManagerService* cookie_manager_service); | 46 GaiaCookieManagerService* cookie_manager_service); |
49 ~AccountReconcilor() override; | 47 ~AccountReconcilor() override; |
50 | 48 |
51 void Initialize(bool start_reconcile_if_tokens_available); | 49 void Initialize(bool start_reconcile_if_tokens_available); |
52 | 50 |
53 // Signal that the status of the new_profile_management flag has changed. | 51 // Signal that the status of the new_profile_management flag has changed. |
54 // Pass the new status as an explicit parameter since disabling the flag | 52 // Pass the new status as an explicit parameter since disabling the flag |
55 // doesn't remove it from the CommandLine::ForCurrentProcess(). | 53 // doesn't remove it from the CommandLine::ForCurrentProcess(). |
56 void OnNewProfileManagementFlagChanged(bool new_flag_status); | 54 void OnNewProfileManagementFlagChanged(bool new_flag_status); |
57 | 55 |
58 // KeyedService implementation. | 56 // KeyedService implementation. |
59 void Shutdown() override; | 57 void Shutdown() override; |
60 | 58 |
61 protected: | |
62 // Used during GetAccountsFromCookie. | |
63 // Stores a callback for the next action to perform. | |
64 typedef base::Callback< | |
65 void(const GoogleServiceAuthError& error, | |
66 const std::vector<std::pair<std::string, bool> >&)> | |
67 GetAccountsFromCookieCallback; | |
68 | |
69 virtual void GetAccountsFromCookie(GetAccountsFromCookieCallback callback); | |
70 | |
71 private: | 59 private: |
72 bool IsRegisteredWithTokenService() const { | 60 bool IsRegisteredWithTokenService() const { |
73 return registered_with_token_service_; | 61 return registered_with_token_service_; |
74 } | 62 } |
75 | 63 |
76 bool AreGaiaAccountsSet() const { return are_gaia_accounts_set_; } | |
77 | |
78 const std::vector<std::pair<std::string, bool> >& GetGaiaAccountsForTesting() | 64 const std::vector<std::pair<std::string, bool> >& GetGaiaAccountsForTesting() |
79 const { | 65 const { |
80 return gaia_accounts_; | 66 return gaia_accounts_; |
81 } | 67 } |
82 | 68 |
83 friend class AccountReconcilorTest; | 69 friend class AccountReconcilorTest; |
84 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, SigninManagerRegistration); | 70 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, SigninManagerRegistration); |
85 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, Reauth); | 71 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, Reauth); |
86 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, ProfileAlreadyConnected); | 72 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, ProfileAlreadyConnected); |
87 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | 73 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, |
(...skipping 17 matching lines...) Expand all Loading... |
105 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | 91 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, |
106 StartReconcileAddToCookieTwice); | 92 StartReconcileAddToCookieTwice); |
107 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileBadPrimary); | 93 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileBadPrimary); |
108 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileOnlyOnce); | 94 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileOnlyOnce); |
109 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | 95 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, |
110 StartReconcileWithSessionInfoExpiredDefault); | 96 StartReconcileWithSessionInfoExpiredDefault); |
111 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | 97 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, |
112 AddAccountToCookieCompletedWithBogusAccount); | 98 AddAccountToCookieCompletedWithBogusAccount); |
113 | 99 |
114 // Register and unregister with dependent services. | 100 // Register and unregister with dependent services. |
115 void RegisterForCookieChanges(); | |
116 void UnregisterForCookieChanges(); | |
117 void RegisterWithSigninManager(); | 101 void RegisterWithSigninManager(); |
118 void UnregisterWithSigninManager(); | 102 void UnregisterWithSigninManager(); |
119 void RegisterWithTokenService(); | 103 void RegisterWithTokenService(); |
120 void UnregisterWithTokenService(); | 104 void UnregisterWithTokenService(); |
121 void RegisterWithCookieManagerService(); | 105 void RegisterWithCookieManagerService(); |
122 void UnregisterWithCookieManagerService(); | 106 void UnregisterWithCookieManagerService(); |
123 void RegisterWithContentSettings(); | 107 void RegisterWithContentSettings(); |
124 void UnregisterWithContentSettings(); | 108 void UnregisterWithContentSettings(); |
125 | 109 |
126 bool IsProfileConnected(); | 110 bool IsProfileConnected(); |
127 | 111 |
128 // All actions with side effects. Virtual so that they can be overridden | 112 // All actions with side effects. Virtual so that they can be overridden |
129 // in tests. | 113 // in tests. |
130 virtual void PerformMergeAction(const std::string& account_id); | 114 virtual void PerformMergeAction(const std::string& account_id); |
131 virtual void PerformLogoutAllAccountsAction(); | 115 virtual void PerformLogoutAllAccountsAction(); |
132 | 116 |
133 // Used during periodic reconciliation. | 117 // Used during periodic reconciliation. |
134 void StartReconcile(); | 118 void StartReconcile(); |
135 void FinishReconcile(); | 119 void FinishReconcile(); |
136 void AbortReconcile(); | 120 void AbortReconcile(); |
137 void CalculateIfReconcileIsDone(); | 121 void CalculateIfReconcileIsDone(); |
138 void ScheduleStartReconcileIfChromeAccountsChanged(); | 122 void ScheduleStartReconcileIfChromeAccountsChanged(); |
139 | 123 |
140 void ContinueReconcileActionAfterGetGaiaAccounts( | |
141 const GoogleServiceAuthError& error, | |
142 const std::vector<std::pair<std::string, bool> >& accounts); | |
143 void ValidateAccountsFromTokenService(); | 124 void ValidateAccountsFromTokenService(); |
144 // Note internally that this |account_id| is added to the cookie jar. | 125 // Note internally that this |account_id| is added to the cookie jar. |
145 bool MarkAccountAsAddedToCookie(const std::string& account_id); | 126 bool MarkAccountAsAddedToCookie(const std::string& account_id); |
146 | 127 |
147 void OnCookieChanged(const net::CanonicalCookie& cookie, bool removed); | |
148 | |
149 // Overriden from content_settings::Observer. | 128 // Overriden from content_settings::Observer. |
150 void OnContentSettingChanged( | 129 void OnContentSettingChanged( |
151 const ContentSettingsPattern& primary_pattern, | 130 const ContentSettingsPattern& primary_pattern, |
152 const ContentSettingsPattern& secondary_pattern, | 131 const ContentSettingsPattern& secondary_pattern, |
153 ContentSettingsType content_type, | 132 ContentSettingsType content_type, |
154 std::string resource_identifier) override; | 133 std::string resource_identifier) override; |
155 | 134 |
156 // Overriden from GaiaAuthConsumer. | 135 // Overriden from GaiaGookieManagerService::Observer. |
157 void OnListAccountsSuccess(const std::string& data) override; | |
158 void OnListAccountsFailure(const GoogleServiceAuthError& error) override; | |
159 | |
160 // Overriden from MergeSessionHelper::Observer. | |
161 void OnAddAccountToCookieCompleted( | 136 void OnAddAccountToCookieCompleted( |
162 const std::string& account_id, | 137 const std::string& account_id, |
163 const GoogleServiceAuthError& error) override; | 138 const GoogleServiceAuthError& error) override; |
| 139 void OnGaiaAccountsInCookieUpdated( |
| 140 const std::vector<std::pair<std::string, bool> >& accounts, |
| 141 const GoogleServiceAuthError& error) override; |
164 | 142 |
165 // Overriden from OAuth2TokenService::Observer. | 143 // Overriden from OAuth2TokenService::Observer. |
166 void OnEndBatchChanges() override; | 144 void OnEndBatchChanges() override; |
167 | 145 |
168 // Overriden from SigninManagerBase::Observer. | 146 // Overriden from SigninManagerBase::Observer. |
169 void GoogleSigninSucceeded(const std::string& account_id, | 147 void GoogleSigninSucceeded(const std::string& account_id, |
170 const std::string& username, | 148 const std::string& username, |
171 const std::string& password) override; | 149 const std::string& password) override; |
172 void GoogleSignedOut(const std::string& account_id, | 150 void GoogleSignedOut(const std::string& account_id, |
173 const std::string& username) override; | 151 const std::string& username) override; |
174 | 152 |
175 void MayBeDoNextListAccounts(); | |
176 | |
177 // The ProfileOAuth2TokenService associated with this reconcilor. | 153 // The ProfileOAuth2TokenService associated with this reconcilor. |
178 ProfileOAuth2TokenService* token_service_; | 154 ProfileOAuth2TokenService* token_service_; |
179 | 155 |
180 // The SigninManager associated with this reconcilor. | 156 // The SigninManager associated with this reconcilor. |
181 SigninManagerBase* signin_manager_; | 157 SigninManagerBase* signin_manager_; |
182 | 158 |
183 // The SigninClient associated with this reconcilor. | 159 // The SigninClient associated with this reconcilor. |
184 SigninClient* client_; | 160 SigninClient* client_; |
185 | 161 |
186 // The GaiaCookieManagerService associated with this reconcilor. | 162 // The GaiaCookieManagerService associated with this reconcilor. |
187 GaiaCookieManagerService* cookie_manager_service_; | 163 GaiaCookieManagerService* cookie_manager_service_; |
188 | 164 |
189 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_; | |
190 bool registered_with_token_service_; | 165 bool registered_with_token_service_; |
191 bool registered_with_cookie_manager_service_; | 166 bool registered_with_cookie_manager_service_; |
192 bool registered_with_content_settings_; | 167 bool registered_with_content_settings_; |
193 | 168 |
194 // True while the reconcilor is busy checking or managing the accounts in | 169 // True while the reconcilor is busy checking or managing the accounts in |
195 // this profile. | 170 // this profile. |
196 bool is_reconcile_started_; | 171 bool is_reconcile_started_; |
197 base::Time m_reconcile_start_time_; | 172 base::Time m_reconcile_start_time_; |
198 | 173 |
199 // True iff this is the first time the reconcilor is executing. | 174 // True iff this is the first time the reconcilor is executing. |
200 bool first_execution_; | 175 bool first_execution_; |
201 | 176 |
202 // Used during reconcile action. | 177 // Used during reconcile action. |
203 // These members are used to validate the gaia cookie. |gaia_accounts_| | 178 // These members are used to validate the gaia cookie. |gaia_accounts_| |
204 // holds the state of google accounts in the gaia cookie. Each element is | 179 // holds the state of google accounts in the gaia cookie. Each element is |
205 // a pair that holds the email address of the account and a boolean that | 180 // a pair that holds the email address of the account and a boolean that |
206 // indicates whether the account is valid or not. The accounts in the vector | 181 // indicates whether the account is valid or not. The accounts in the vector |
207 // are ordered the in same way as the gaia cookie. | 182 // are ordered the in same way as the gaia cookie. |
208 bool are_gaia_accounts_set_; | |
209 std::vector<std::pair<std::string, bool> > gaia_accounts_; | 183 std::vector<std::pair<std::string, bool> > gaia_accounts_; |
210 | 184 |
211 // Used during reconcile action. | 185 // Used during reconcile action. |
212 // These members are used to validate the tokens in OAuth2TokenService. | 186 // These members are used to validate the tokens in OAuth2TokenService. |
213 std::string primary_account_; | 187 std::string primary_account_; |
214 std::vector<std::string> chrome_accounts_; | 188 std::vector<std::string> chrome_accounts_; |
215 std::vector<std::string> add_to_cookie_; | 189 std::vector<std::string> add_to_cookie_; |
216 bool chrome_accounts_changed_; | 190 bool chrome_accounts_changed_; |
217 | 191 |
218 std::deque<GetAccountsFromCookieCallback> get_gaia_accounts_callbacks_; | |
219 | |
220 scoped_ptr<SigninClient::CookieChangedSubscription> | |
221 cookie_changed_subscription_; | |
222 | |
223 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor); | 192 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor); |
224 }; | 193 }; |
225 | 194 |
226 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ | 195 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_RECONCILOR_H_ |
OLD | NEW |