OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 4 |
5 #include "chrome/browser/sync/profile_sync_service.h" | 5 #include "chrome/browser/sync/profile_sync_service.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
26 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
27 #include "chrome/browser/defaults.h" | 27 #include "chrome/browser/defaults.h" |
28 #include "chrome/browser/managed_mode/managed_user_signin_manager_wrapper.h" | |
29 #include "chrome/browser/net/chrome_cookie_notification_details.h" | 28 #include "chrome/browser/net/chrome_cookie_notification_details.h" |
30 #include "chrome/browser/prefs/pref_service_syncable.h" | 29 #include "chrome/browser/prefs/pref_service_syncable.h" |
31 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
32 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 31 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
33 #include "chrome/browser/signin/about_signin_internals.h" | 32 #include "chrome/browser/signin/about_signin_internals.h" |
34 #include "chrome/browser/signin/about_signin_internals_factory.h" | 33 #include "chrome/browser/signin/about_signin_internals_factory.h" |
35 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 34 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
36 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 35 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
37 #include "chrome/browser/signin/signin_manager.h" | 36 #include "chrome/browser/signin/signin_manager.h" |
38 #include "chrome/browser/signin/signin_manager_factory.h" | 37 #include "chrome/browser/signin/signin_manager_factory.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 bool ShouldShowActionOnUI( | 151 bool ShouldShowActionOnUI( |
153 const syncer::SyncProtocolError& error) { | 152 const syncer::SyncProtocolError& error) { |
154 return (error.action != syncer::UNKNOWN_ACTION && | 153 return (error.action != syncer::UNKNOWN_ACTION && |
155 error.action != syncer::DISABLE_SYNC_ON_CLIENT && | 154 error.action != syncer::DISABLE_SYNC_ON_CLIENT && |
156 error.action != syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT); | 155 error.action != syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT); |
157 } | 156 } |
158 | 157 |
159 ProfileSyncService::ProfileSyncService( | 158 ProfileSyncService::ProfileSyncService( |
160 ProfileSyncComponentsFactory* factory, | 159 ProfileSyncComponentsFactory* factory, |
161 Profile* profile, | 160 Profile* profile, |
162 ManagedUserSigninManagerWrapper* signin_wrapper, | 161 SigninManagerBase* signin_manager, |
163 ProfileOAuth2TokenService* oauth2_token_service, | 162 ProfileOAuth2TokenService* oauth2_token_service, |
164 StartBehavior start_behavior) | 163 StartBehavior start_behavior) |
165 : OAuth2TokenService::Consumer("sync"), | 164 : OAuth2TokenService::Consumer("sync"), |
166 last_auth_error_(AuthError::AuthErrorNone()), | 165 last_auth_error_(AuthError::AuthErrorNone()), |
167 passphrase_required_reason_(syncer::REASON_PASSPHRASE_NOT_REQUIRED), | 166 passphrase_required_reason_(syncer::REASON_PASSPHRASE_NOT_REQUIRED), |
168 factory_(factory), | 167 factory_(factory), |
169 profile_(profile), | 168 profile_(profile), |
170 sync_prefs_(profile_->GetPrefs()), | 169 sync_prefs_(profile_->GetPrefs()), |
171 sync_service_url_(kDevServerUrl), | 170 sync_service_url_(kDevServerUrl), |
172 data_type_requested_sync_startup_(false), | 171 data_type_requested_sync_startup_(false), |
173 is_first_time_sync_configure_(false), | 172 is_first_time_sync_configure_(false), |
174 backend_initialized_(false), | 173 backend_initialized_(false), |
175 sync_disabled_by_admin_(false), | 174 sync_disabled_by_admin_(false), |
176 is_auth_in_progress_(false), | 175 is_auth_in_progress_(false), |
177 signin_(signin_wrapper), | 176 signin_(signin_manager), |
178 unrecoverable_error_reason_(ERROR_REASON_UNSET), | 177 unrecoverable_error_reason_(ERROR_REASON_UNSET), |
179 expect_sync_configuration_aborted_(false), | 178 expect_sync_configuration_aborted_(false), |
180 encrypted_types_(syncer::SyncEncryptionHandler::SensitiveTypes()), | 179 encrypted_types_(syncer::SyncEncryptionHandler::SensitiveTypes()), |
181 encrypt_everything_(false), | 180 encrypt_everything_(false), |
182 encryption_pending_(false), | 181 encryption_pending_(false), |
183 auto_start_enabled_(start_behavior == AUTO_START), | 182 auto_start_enabled_(start_behavior == AUTO_START), |
184 configure_status_(DataTypeManager::UNKNOWN), | 183 configure_status_(DataTypeManager::UNKNOWN), |
185 setup_in_progress_(false), | 184 setup_in_progress_(false), |
186 oauth2_token_service_(oauth2_token_service), | 185 oauth2_token_service_(oauth2_token_service), |
187 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), | 186 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // Shutdown() should have been called before destruction. | 218 // Shutdown() should have been called before destruction. |
220 CHECK(!backend_initialized_); | 219 CHECK(!backend_initialized_); |
221 } | 220 } |
222 | 221 |
223 bool ProfileSyncService::IsSyncEnabledAndLoggedIn() { | 222 bool ProfileSyncService::IsSyncEnabledAndLoggedIn() { |
224 // Exit if sync is disabled. | 223 // Exit if sync is disabled. |
225 if (IsManaged() || sync_prefs_.IsStartSuppressed()) | 224 if (IsManaged() || sync_prefs_.IsStartSuppressed()) |
226 return false; | 225 return false; |
227 | 226 |
228 // Sync is logged in if there is a non-empty effective username. | 227 // Sync is logged in if there is a non-empty effective username. |
229 return !signin_->GetEffectiveUsername().empty(); | 228 return !GetEffectiveUsername().empty(); |
230 } | 229 } |
231 | 230 |
232 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { | 231 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { |
233 if (!oauth2_token_service_) | 232 if (!oauth2_token_service_) |
234 return false; | 233 return false; |
235 | 234 |
236 return oauth2_token_service_->RefreshTokenIsAvailable( | 235 return oauth2_token_service_->RefreshTokenIsAvailable(GetAccountIdToUse()); |
237 signin_->GetAccountIdToUse()); | |
238 } | 236 } |
239 | 237 |
240 void ProfileSyncService::Initialize() { | 238 void ProfileSyncService::Initialize() { |
241 InitSettings(); | 239 InitSettings(); |
242 | 240 |
243 // We clear this here (vs Shutdown) because we want to remember that an error | 241 // We clear this here (vs Shutdown) because we want to remember that an error |
244 // happened on shutdown so we can display details (message, location) about it | 242 // happened on shutdown so we can display details (message, location) about it |
245 // in about:sync. | 243 // in about:sync. |
246 ClearStaleErrors(); | 244 ClearStaleErrors(); |
247 | 245 |
248 sync_prefs_.AddSyncPrefObserver(this); | 246 sync_prefs_.AddSyncPrefObserver(this); |
249 | 247 |
250 // For now, the only thing we can do through policy is to turn sync off. | 248 // For now, the only thing we can do through policy is to turn sync off. |
251 if (IsManaged()) { | 249 if (IsManaged()) { |
252 DisableForUser(); | 250 DisableForUser(); |
253 return; | 251 return; |
254 } | 252 } |
255 | 253 |
256 RegisterAuthNotifications(); | 254 RegisterAuthNotifications(); |
257 | 255 |
258 if (!HasSyncSetupCompleted() || signin_->GetEffectiveUsername().empty()) { | 256 if (!HasSyncSetupCompleted() || GetEffectiveUsername().empty()) { |
259 // Clean up in case of previous crash / setup abort / signout. | 257 // Clean up in case of previous crash / setup abort / signout. |
260 DisableForUser(); | 258 DisableForUser(); |
261 } | 259 } |
262 | 260 |
263 TrySyncDatatypePrefRecovery(); | 261 TrySyncDatatypePrefRecovery(); |
264 | 262 |
265 last_synced_time_ = sync_prefs_.GetLastSyncedTime(); | 263 last_synced_time_ = sync_prefs_.GetLastSyncedTime(); |
266 | 264 |
267 #if defined(OS_CHROMEOS) | 265 #if defined(OS_CHROMEOS) |
268 std::string bootstrap_token = sync_prefs_.GetEncryptionBootstrapToken(); | 266 std::string bootstrap_token = sync_prefs_.GetEncryptionBootstrapToken(); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 } else { | 527 } else { |
530 LOG(WARNING) << "The following sync URL specified at the command-line " | 528 LOG(WARNING) << "The following sync URL specified at the command-line " |
531 << "is invalid: " << value; | 529 << "is invalid: " << value; |
532 } | 530 } |
533 } | 531 } |
534 } | 532 } |
535 } | 533 } |
536 | 534 |
537 SyncCredentials ProfileSyncService::GetCredentials() { | 535 SyncCredentials ProfileSyncService::GetCredentials() { |
538 SyncCredentials credentials; | 536 SyncCredentials credentials; |
539 credentials.email = signin_->GetEffectiveUsername(); | 537 credentials.email = GetEffectiveUsername(); |
540 DCHECK(!credentials.email.empty()); | 538 DCHECK(!credentials.email.empty()); |
541 credentials.sync_token = access_token_; | 539 credentials.sync_token = access_token_; |
542 | 540 |
543 if (credentials.sync_token.empty()) | 541 if (credentials.sync_token.empty()) |
544 credentials.sync_token = "credentials_lost"; | 542 credentials.sync_token = "credentials_lost"; |
545 return credentials; | 543 return credentials; |
546 } | 544 } |
547 | 545 |
548 void ProfileSyncService::InitializeBackend(bool delete_stale_data) { | 546 void ProfileSyncService::InitializeBackend(bool delete_stale_data) { |
549 if (!backend_) { | 547 if (!backend_) { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 } | 739 } |
742 default: { | 740 default: { |
743 // Show error to user. | 741 // Show error to user. |
744 UpdateAuthErrorState(error); | 742 UpdateAuthErrorState(error); |
745 } | 743 } |
746 } | 744 } |
747 } | 745 } |
748 | 746 |
749 void ProfileSyncService::OnRefreshTokenAvailable( | 747 void ProfileSyncService::OnRefreshTokenAvailable( |
750 const std::string& account_id) { | 748 const std::string& account_id) { |
751 if (account_id == signin_->GetAccountIdToUse()) | 749 if (account_id == GetAccountIdToUse()) |
752 OnRefreshTokensLoaded(); | 750 OnRefreshTokensLoaded(); |
753 } | 751 } |
754 | 752 |
755 void ProfileSyncService::OnRefreshTokenRevoked( | 753 void ProfileSyncService::OnRefreshTokenRevoked( |
756 const std::string& account_id) { | 754 const std::string& account_id) { |
757 if (!IsOAuthRefreshTokenAvailable()) { | 755 if (!IsOAuthRefreshTokenAvailable()) { |
758 access_token_.clear(); | 756 access_token_.clear(); |
759 // The additional check around IsOAuthRefreshTokenAvailable() above | 757 // The additional check around IsOAuthRefreshTokenAvailable() above |
760 // prevents us sounding the alarm if we actually have a valid token but | 758 // prevents us sounding the alarm if we actually have a valid token but |
761 // a refresh attempt failed for any variety of reasons | 759 // a refresh attempt failed for any variety of reasons |
(...skipping 1199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1961 request_access_token_retry_timer_.Stop(); | 1959 request_access_token_retry_timer_.Stop(); |
1962 OAuth2TokenService::ScopeSet oauth2_scopes; | 1960 OAuth2TokenService::ScopeSet oauth2_scopes; |
1963 if (profile_->IsManaged()) { | 1961 if (profile_->IsManaged()) { |
1964 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); | 1962 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); |
1965 } else { | 1963 } else { |
1966 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 1964 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
1967 } | 1965 } |
1968 | 1966 |
1969 // Invalidate previous token, otherwise token service will return the same | 1967 // Invalidate previous token, otherwise token service will return the same |
1970 // token again. | 1968 // token again. |
1971 const std::string& account_id = signin_->GetAccountIdToUse(); | 1969 const std::string& account_id = GetAccountIdToUse(); |
1972 if (!access_token_.empty()) { | 1970 if (!access_token_.empty()) { |
1973 oauth2_token_service_->InvalidateToken( | 1971 oauth2_token_service_->InvalidateToken( |
1974 account_id, oauth2_scopes, access_token_); | 1972 account_id, oauth2_scopes, access_token_); |
1975 } | 1973 } |
1976 | 1974 |
1977 access_token_.clear(); | 1975 access_token_.clear(); |
1978 | 1976 |
1979 token_request_time_ = base::Time::Now(); | 1977 token_request_time_ = base::Time::Now(); |
1980 token_receive_time_ = base::Time(); | 1978 token_receive_time_ = base::Time(); |
1981 next_token_request_time_ = base::Time(); | 1979 next_token_request_time_ = base::Time(); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2147 if (backend_) { | 2145 if (backend_) { |
2148 backend_->UnregisterInvalidationIds(); | 2146 backend_->UnregisterInvalidationIds(); |
2149 } | 2147 } |
2150 ShutdownImpl(browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD); | 2148 ShutdownImpl(browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD); |
2151 } | 2149 } |
2152 | 2150 |
2153 bool ProfileSyncService::IsStartSuppressed() const { | 2151 bool ProfileSyncService::IsStartSuppressed() const { |
2154 return sync_prefs_.IsStartSuppressed(); | 2152 return sync_prefs_.IsStartSuppressed(); |
2155 } | 2153 } |
2156 | 2154 |
2157 SigninManagerBase* ProfileSyncService::signin() const { | |
2158 return signin_->GetOriginal(); | |
2159 } | |
2160 | |
2161 void ProfileSyncService::UnsuppressAndStart() { | 2155 void ProfileSyncService::UnsuppressAndStart() { |
2162 DCHECK(profile_); | 2156 DCHECK(profile_); |
2163 sync_prefs_.SetStartSuppressed(false); | 2157 sync_prefs_.SetStartSuppressed(false); |
2164 // Set username in SigninManager, as SigninManager::OnGetUserInfoSuccess | 2158 // Set username in SigninManager, as SigninManager::OnGetUserInfoSuccess |
2165 // is never called for some clients. | 2159 // is never called for some clients. |
2166 if (signin_.get() && | 2160 if (signin_ && signin_->GetAuthenticatedUsername().empty()) { |
2167 signin_->GetOriginal()->GetAuthenticatedUsername().empty()) { | 2161 signin_->SetAuthenticatedUsername(sync_prefs_.GetGoogleServicesUsername()); |
2168 signin_->GetOriginal()->SetAuthenticatedUsername( | |
2169 sync_prefs_.GetGoogleServicesUsername()); | |
2170 } | 2162 } |
2171 TryStart(); | 2163 TryStart(); |
2172 } | 2164 } |
2173 | 2165 |
2174 void ProfileSyncService::AcknowledgeSyncedTypes() { | 2166 void ProfileSyncService::AcknowledgeSyncedTypes() { |
2175 sync_prefs_.AcknowledgeSyncedTypes(GetRegisteredDataTypes()); | 2167 sync_prefs_.AcknowledgeSyncedTypes(GetRegisteredDataTypes()); |
2176 } | 2168 } |
2177 | 2169 |
2178 void ProfileSyncService::ReconfigureDatatypeManager() { | 2170 void ProfileSyncService::ReconfigureDatatypeManager() { |
2179 // If we haven't initialized yet, don't configure the DTM as it could cause | 2171 // If we haven't initialized yet, don't configure the DTM as it could cause |
(...skipping 29 matching lines...) Expand all Loading... |
2209 } | 2201 } |
2210 | 2202 |
2211 bool ProfileSyncService::IsRetryingAccessTokenFetchForTest() const { | 2203 bool ProfileSyncService::IsRetryingAccessTokenFetchForTest() const { |
2212 return request_access_token_retry_timer_.IsRunning(); | 2204 return request_access_token_retry_timer_.IsRunning(); |
2213 } | 2205 } |
2214 | 2206 |
2215 std::string ProfileSyncService::GetAccessTokenForTest() const { | 2207 std::string ProfileSyncService::GetAccessTokenForTest() const { |
2216 return access_token_; | 2208 return access_token_; |
2217 } | 2209 } |
2218 | 2210 |
| 2211 std::string ProfileSyncService::GetEffectiveUsername() { |
| 2212 if (profile_->IsManaged()) { |
| 2213 #if defined(ENABLE_MANAGED_USERS) |
| 2214 DCHECK_EQ(std::string(), signin_->GetAuthenticatedUsername()); |
| 2215 return managed_users::kManagedUserPseudoEmail; |
| 2216 #else |
| 2217 NOTREACHED(); |
| 2218 #endif |
| 2219 } |
| 2220 |
| 2221 return signin_->GetAuthenticatedUsername(); |
| 2222 } |
| 2223 |
| 2224 std::string ProfileSyncService::GetAccountIdToUse() { |
| 2225 if (profile_->IsManaged()) { |
| 2226 #if defined(ENABLE_MANAGED_USERS) |
| 2227 return managed_users::kManagedUserPseudoEmail; |
| 2228 #else |
| 2229 NOTREACHED(); |
| 2230 #endif |
| 2231 } |
| 2232 |
| 2233 // TODO(fgorski): Use GetPrimaryAccountId() when it's available. |
| 2234 return signin_->GetAuthenticatedUsername(); |
| 2235 } |
| 2236 |
2219 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { | 2237 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { |
2220 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); | 2238 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); |
2221 } | 2239 } |
2222 | 2240 |
2223 syncer::SyncableService* ProfileSyncService::GetSessionsSyncableService() { | 2241 syncer::SyncableService* ProfileSyncService::GetSessionsSyncableService() { |
2224 return sessions_sync_manager_.get(); | 2242 return sessions_sync_manager_.get(); |
2225 } | 2243 } |
2226 | 2244 |
2227 ProfileSyncService::SyncTokenStatus::SyncTokenStatus() | 2245 ProfileSyncService::SyncTokenStatus::SyncTokenStatus() |
2228 : connection_status(syncer::CONNECTION_NOT_ATTEMPTED), | 2246 : connection_status(syncer::CONNECTION_NOT_ATTEMPTED), |
(...skipping 10 matching lines...) Expand all Loading... |
2239 status.last_get_token_error = last_get_token_error_; | 2257 status.last_get_token_error = last_get_token_error_; |
2240 if (request_access_token_retry_timer_.IsRunning()) | 2258 if (request_access_token_retry_timer_.IsRunning()) |
2241 status.next_token_request_time = next_token_request_time_; | 2259 status.next_token_request_time = next_token_request_time_; |
2242 return status; | 2260 return status; |
2243 } | 2261 } |
2244 | 2262 |
2245 void ProfileSyncService::OverrideNetworkResourcesForTest( | 2263 void ProfileSyncService::OverrideNetworkResourcesForTest( |
2246 scoped_ptr<syncer::NetworkResources> network_resources) { | 2264 scoped_ptr<syncer::NetworkResources> network_resources) { |
2247 network_resources_ = network_resources.Pass(); | 2265 network_resources_ = network_resources.Pass(); |
2248 } | 2266 } |
OLD | NEW |