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> |
(...skipping 12 matching lines...) Expand all Loading... |
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/net/chrome_cookie_notification_details.h" | 28 #include "chrome/browser/net/chrome_cookie_notification_details.h" |
29 #include "chrome/browser/prefs/pref_service_syncable.h" | 29 #include "chrome/browser/prefs/pref_service_syncable.h" |
30 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/signin/about_signin_internals.h" | 31 #include "chrome/browser/signin/about_signin_internals.h" |
32 #include "chrome/browser/signin/about_signin_internals_factory.h" | 32 #include "chrome/browser/signin/about_signin_internals_factory.h" |
| 33 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
33 #include "chrome/browser/signin/signin_manager.h" | 34 #include "chrome/browser/signin/signin_manager.h" |
34 #include "chrome/browser/signin/signin_manager_factory.h" | 35 #include "chrome/browser/signin/signin_manager_factory.h" |
35 #include "chrome/browser/signin/token_service.h" | 36 #include "chrome/browser/signin/token_service.h" |
36 #include "chrome/browser/signin/token_service_factory.h" | 37 #include "chrome/browser/signin/token_service_factory.h" |
37 #include "chrome/browser/sync/backend_migrator.h" | 38 #include "chrome/browser/sync/backend_migrator.h" |
38 #include "chrome/browser/sync/glue/change_processor.h" | 39 #include "chrome/browser/sync/glue/change_processor.h" |
39 #include "chrome/browser/sync/glue/chrome_encryptor.h" | 40 #include "chrome/browser/sync/glue/chrome_encryptor.h" |
40 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h" | 41 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h" |
41 #include "chrome/browser/sync/glue/data_type_controller.h" | 42 #include "chrome/browser/sync/glue/data_type_controller.h" |
42 #include "chrome/browser/sync/glue/device_info.h" | 43 #include "chrome/browser/sync/glue/device_info.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 false, | 141 false, |
141 }; | 142 }; |
142 | 143 |
143 bool ShouldShowActionOnUI( | 144 bool ShouldShowActionOnUI( |
144 const syncer::SyncProtocolError& error) { | 145 const syncer::SyncProtocolError& error) { |
145 return (error.action != syncer::UNKNOWN_ACTION && | 146 return (error.action != syncer::UNKNOWN_ACTION && |
146 error.action != syncer::DISABLE_SYNC_ON_CLIENT && | 147 error.action != syncer::DISABLE_SYNC_ON_CLIENT && |
147 error.action != syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT); | 148 error.action != syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT); |
148 } | 149 } |
149 | 150 |
150 ProfileSyncService::ProfileSyncService(ProfileSyncComponentsFactory* factory, | 151 ProfileSyncService::ProfileSyncService( |
151 Profile* profile, | 152 ProfileSyncComponentsFactory* factory, |
152 SigninManagerBase* signin_manager, | 153 Profile* profile, |
153 OAuth2TokenService* oauth2_token_service, | 154 SigninManagerBase* signin_manager, |
154 StartBehavior start_behavior) | 155 ProfileOAuth2TokenService* oauth2_token_service, |
| 156 StartBehavior start_behavior) |
155 : last_auth_error_(AuthError::AuthErrorNone()), | 157 : last_auth_error_(AuthError::AuthErrorNone()), |
156 passphrase_required_reason_(syncer::REASON_PASSPHRASE_NOT_REQUIRED), | 158 passphrase_required_reason_(syncer::REASON_PASSPHRASE_NOT_REQUIRED), |
157 factory_(factory), | 159 factory_(factory), |
158 profile_(profile), | 160 profile_(profile), |
159 // |profile| may be NULL in unit tests. | 161 // |profile| may be NULL in unit tests. |
160 sync_prefs_(profile_ ? profile_->GetPrefs() : NULL), | 162 sync_prefs_(profile_ ? profile_->GetPrefs() : NULL), |
161 sync_service_url_(kDevServerUrl), | 163 sync_service_url_(kDevServerUrl), |
162 data_type_requested_sync_startup_(false), | 164 data_type_requested_sync_startup_(false), |
163 is_first_time_sync_configure_(false), | 165 is_first_time_sync_configure_(false), |
164 backend_initialized_(false), | 166 backend_initialized_(false), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 209 } |
208 | 210 |
209 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { | 211 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { |
210 // Function name doesn't reflect which token is checked. Function checks | 212 // Function name doesn't reflect which token is checked. Function checks |
211 // refresh token when use_oauth2_token_ is true (all platforms except android) | 213 // refresh token when use_oauth2_token_ is true (all platforms except android) |
212 // and sync token otherwise (for android). | 214 // and sync token otherwise (for android). |
213 // TODO(pavely): Remove "else" part once use_oauth2_token_ is gone. | 215 // TODO(pavely): Remove "else" part once use_oauth2_token_ is gone. |
214 if (use_oauth2_token_) { | 216 if (use_oauth2_token_) { |
215 if (!oauth2_token_service_) | 217 if (!oauth2_token_service_) |
216 return false; | 218 return false; |
217 return oauth2_token_service_->RefreshTokenIsAvailable(); | 219 return oauth2_token_service_->RefreshTokenIsAvailable( |
| 220 oauth2_token_service_->GetPrimaryAccountId()); |
218 } else { | 221 } else { |
219 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 222 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
220 if (!token_service) | 223 if (!token_service) |
221 return false; | 224 return false; |
222 | 225 |
223 return token_service->HasTokenForService(GaiaConstants::kSyncService); | 226 return token_service->HasTokenForService(GaiaConstants::kSyncService); |
224 } | 227 } |
225 } | 228 } |
226 | 229 |
227 void ProfileSyncService::Initialize() { | 230 void ProfileSyncService::Initialize() { |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 } | 707 } |
705 default: { | 708 default: { |
706 // Show error to user. | 709 // Show error to user. |
707 UpdateAuthErrorState(error); | 710 UpdateAuthErrorState(error); |
708 } | 711 } |
709 } | 712 } |
710 } | 713 } |
711 | 714 |
712 void ProfileSyncService::OnRefreshTokenAvailable( | 715 void ProfileSyncService::OnRefreshTokenAvailable( |
713 const std::string& account_id) { | 716 const std::string& account_id) { |
714 OnRefreshTokensLoaded(); | 717 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) |
| 718 OnRefreshTokensLoaded(); |
715 } | 719 } |
716 | 720 |
717 void ProfileSyncService::OnRefreshTokenRevoked( | 721 void ProfileSyncService::OnRefreshTokenRevoked( |
718 const std::string& account_id) { | 722 const std::string& account_id) { |
719 if (!IsOAuthRefreshTokenAvailable()) { | 723 if (!IsOAuthRefreshTokenAvailable()) { |
| 724 access_token_.clear(); |
720 // The additional check around IsOAuthRefreshTokenAvailable() above | 725 // The additional check around IsOAuthRefreshTokenAvailable() above |
721 // prevents us sounding the alarm if we actually have a valid token but | 726 // prevents us sounding the alarm if we actually have a valid token but |
722 // a refresh attempt by TokenService failed for any variety of reasons | 727 // a refresh attempt by TokenService failed for any variety of reasons |
723 // (e.g. flaky network). It's possible the token we do have is also | 728 // (e.g. flaky network). It's possible the token we do have is also |
724 // invalid, but in that case we should already have (or can expect) an | 729 // invalid, but in that case we should already have (or can expect) an |
725 // auth error sent from the sync backend. | 730 // auth error sent from the sync backend. |
726 UpdateAuthErrorState( | 731 UpdateAuthErrorState( |
727 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); | 732 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); |
728 } | 733 } |
729 } | 734 } |
730 | 735 |
731 void ProfileSyncService::OnRefreshTokensLoaded() { | 736 void ProfileSyncService::OnRefreshTokensLoaded() { |
732 // This notification gets fired when TokenService loads the tokens | 737 // This notification gets fired when TokenService loads the tokens |
733 // from storage. | 738 // from storage. |
734 // Initialize the backend if sync is enabled. If the sync token was | 739 // Initialize the backend if sync is enabled. If the sync token was |
735 // not loaded, GetCredentials() will generate invalid credentials to | 740 // not loaded, GetCredentials() will generate invalid credentials to |
736 // cause the backend to generate an auth error (crbug.com/121755). | 741 // cause the backend to generate an auth error (crbug.com/121755). |
737 if (backend_) { | 742 if (backend_) { |
738 RequestAccessToken(); | 743 RequestAccessToken(); |
739 } else { | 744 } else { |
740 TryStart(); | 745 TryStart(); |
741 } | 746 } |
742 } | 747 } |
743 | 748 |
744 void ProfileSyncService::OnRefreshTokensCleared() { | |
745 access_token_.clear(); | |
746 } | |
747 | |
748 void ProfileSyncService::Shutdown() { | 749 void ProfileSyncService::Shutdown() { |
749 UnregisterAuthNotifications(); | 750 UnregisterAuthNotifications(); |
750 | 751 |
751 if (profile_) | 752 if (profile_) |
752 SigninGlobalError::GetForProfile(profile_)->RemoveProvider(this); | 753 SigninGlobalError::GetForProfile(profile_)->RemoveProvider(this); |
753 | 754 |
754 ShutdownImpl(browser_sync::SyncBackendHost::STOP); | 755 ShutdownImpl(browser_sync::SyncBackendHost::STOP); |
755 | 756 |
756 if (sync_thread_) | 757 if (sync_thread_) |
757 sync_thread_->Stop(); | 758 sync_thread_->Stop(); |
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1895 request_access_token_retry_timer_.Stop(); | 1896 request_access_token_retry_timer_.Stop(); |
1896 OAuth2TokenService::ScopeSet oauth2_scopes; | 1897 OAuth2TokenService::ScopeSet oauth2_scopes; |
1897 if (profile_->IsManaged()) { | 1898 if (profile_->IsManaged()) { |
1898 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); | 1899 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); |
1899 } else { | 1900 } else { |
1900 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 1901 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
1901 } | 1902 } |
1902 | 1903 |
1903 // Invalidate previous token, otherwise token service will return the same | 1904 // Invalidate previous token, otherwise token service will return the same |
1904 // token again. | 1905 // token again. |
1905 if (!access_token_.empty()) | 1906 const std::string& account_id = oauth2_token_service_->GetPrimaryAccountId(); |
1906 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); | 1907 if (!access_token_.empty()) { |
| 1908 oauth2_token_service_->InvalidateToken( |
| 1909 account_id, oauth2_scopes, access_token_); |
| 1910 } |
1907 | 1911 |
1908 access_token_.clear(); | 1912 access_token_.clear(); |
1909 access_token_request_ = | 1913 access_token_request_ = |
1910 oauth2_token_service_->StartRequest(oauth2_scopes, this); | 1914 oauth2_token_service_->StartRequest(account_id, oauth2_scopes, this); |
1911 } | 1915 } |
1912 | 1916 |
1913 void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase, | 1917 void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase, |
1914 PassphraseType type) { | 1918 PassphraseType type) { |
1915 // This should only be called when the backend has been initialized. | 1919 // This should only be called when the backend has been initialized. |
1916 DCHECK(sync_initialized()); | 1920 DCHECK(sync_initialized()); |
1917 DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) << | 1921 DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) << |
1918 "Data is already encrypted using an explicit passphrase"; | 1922 "Data is already encrypted using an explicit passphrase"; |
1919 DCHECK(!(type == EXPLICIT && | 1923 DCHECK(!(type == EXPLICIT && |
1920 passphrase_required_reason_ == syncer::REASON_DECRYPTION)) << | 1924 passphrase_required_reason_ == syncer::REASON_DECRYPTION)) << |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2146 NOTREACHED(); | 2150 NOTREACHED(); |
2147 #endif | 2151 #endif |
2148 } | 2152 } |
2149 | 2153 |
2150 return signin_->GetAuthenticatedUsername(); | 2154 return signin_->GetAuthenticatedUsername(); |
2151 } | 2155 } |
2152 | 2156 |
2153 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { | 2157 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { |
2154 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); | 2158 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); |
2155 } | 2159 } |
OLD | NEW |