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