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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { | 208 bool ProfileSyncService::IsOAuthRefreshTokenAvailable() { |
209 // Function name doesn't reflect which token is checked. Function checks | 209 // Function name doesn't reflect which token is checked. Function checks |
210 // refresh token when use_oauth2_token_ is true (all platforms except android) | 210 // refresh token when use_oauth2_token_ is true (all platforms except android) |
211 // and sync token otherwise (for android). | 211 // and sync token otherwise (for android). |
212 // TODO(pavely): Remove "else" part once use_oauth2_token_ is gone. | 212 // TODO(pavely): Remove "else" part once use_oauth2_token_ is gone. |
213 if (use_oauth2_token_) { | 213 if (use_oauth2_token_) { |
214 ProfileOAuth2TokenService* token_service = | 214 ProfileOAuth2TokenService* token_service = |
215 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 215 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
216 if (!token_service) | 216 if (!token_service) |
217 return false; | 217 return false; |
218 return token_service->RefreshTokenIsAvailable(); | 218 return token_service->RefreshTokenIsAvailable( |
| 219 token_service->GetPrimaryAccountId()); |
219 } else { | 220 } else { |
220 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 221 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
221 if (!token_service) | 222 if (!token_service) |
222 return false; | 223 return false; |
223 return token_service->HasTokenForService(GaiaConstants::kSyncService); | 224 return token_service->HasTokenForService(GaiaConstants::kSyncService); |
224 } | 225 } |
225 } | 226 } |
226 | 227 |
227 void ProfileSyncService::Initialize() { | 228 void ProfileSyncService::Initialize() { |
228 if (profile_) | 229 if (profile_) |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 } | 709 } |
709 default: { | 710 default: { |
710 // Show error to user. | 711 // Show error to user. |
711 UpdateAuthErrorState(error); | 712 UpdateAuthErrorState(error); |
712 } | 713 } |
713 } | 714 } |
714 } | 715 } |
715 | 716 |
716 void ProfileSyncService::OnRefreshTokenAvailable( | 717 void ProfileSyncService::OnRefreshTokenAvailable( |
717 const std::string& account_id) { | 718 const std::string& account_id) { |
718 OnRefreshTokensLoaded(); | 719 if (ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> |
| 720 GetPrimaryAccountId() == account_id) { |
| 721 OnRefreshTokensLoaded(); |
| 722 } |
719 } | 723 } |
720 | 724 |
721 void ProfileSyncService::OnRefreshTokenRevoked( | 725 void ProfileSyncService::OnRefreshTokenRevoked( |
722 const std::string& account_id) { | 726 const std::string& account_id) { |
723 if (!IsOAuthRefreshTokenAvailable()) { | 727 if (!IsOAuthRefreshTokenAvailable()) { |
| 728 access_token_.clear(); |
724 // The additional check around IsOAuthRefreshTokenAvailable() above | 729 // The additional check around IsOAuthRefreshTokenAvailable() above |
725 // prevents us sounding the alarm if we actually have a valid token but | 730 // prevents us sounding the alarm if we actually have a valid token but |
726 // a refresh attempt by TokenService failed for any variety of reasons | 731 // a refresh attempt by TokenService failed for any variety of reasons |
727 // (e.g. flaky network). It's possible the token we do have is also | 732 // (e.g. flaky network). It's possible the token we do have is also |
728 // invalid, but in that case we should already have (or can expect) an | 733 // invalid, but in that case we should already have (or can expect) an |
729 // auth error sent from the sync backend. | 734 // auth error sent from the sync backend. |
730 UpdateAuthErrorState( | 735 UpdateAuthErrorState( |
731 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); | 736 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); |
732 } | 737 } |
733 } | 738 } |
734 | 739 |
735 void ProfileSyncService::OnRefreshTokensLoaded() { | 740 void ProfileSyncService::OnRefreshTokensLoaded() { |
736 // This notification gets fired when TokenService loads the tokens | 741 // This notification gets fired when TokenService loads the tokens |
737 // from storage. | 742 // from storage. |
738 // Initialize the backend if sync is enabled. If the sync token was | 743 // Initialize the backend if sync is enabled. If the sync token was |
739 // not loaded, GetCredentials() will generate invalid credentials to | 744 // not loaded, GetCredentials() will generate invalid credentials to |
740 // cause the backend to generate an auth error (crbug.com/121755). | 745 // cause the backend to generate an auth error (crbug.com/121755). |
741 if (backend_) { | 746 if (backend_) { |
742 RequestAccessToken(); | 747 RequestAccessToken(); |
743 } else { | 748 } else { |
744 TryStart(); | 749 TryStart(); |
745 } | 750 } |
746 } | 751 } |
747 | 752 |
748 void ProfileSyncService::OnRefreshTokensCleared() { | |
749 access_token_.clear(); | |
750 } | |
751 | |
752 void ProfileSyncService::Shutdown() { | 753 void ProfileSyncService::Shutdown() { |
753 UnregisterAuthNotifications(); | 754 UnregisterAuthNotifications(); |
754 | 755 |
755 if (profile_) | 756 if (profile_) |
756 SigninGlobalError::GetForProfile(profile_)->RemoveProvider(this); | 757 SigninGlobalError::GetForProfile(profile_)->RemoveProvider(this); |
757 | 758 |
758 ShutdownImpl(browser_sync::SyncBackendHost::STOP); | 759 ShutdownImpl(browser_sync::SyncBackendHost::STOP); |
759 | 760 |
760 if (sync_thread_) | 761 if (sync_thread_) |
761 sync_thread_->Stop(); | 762 sync_thread_->Stop(); |
(...skipping 1117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1879 if (access_token_request_ != NULL) | 1880 if (access_token_request_ != NULL) |
1880 return; | 1881 return; |
1881 request_access_token_retry_timer_.Stop(); | 1882 request_access_token_retry_timer_.Stop(); |
1882 OAuth2TokenService::ScopeSet oauth2_scopes; | 1883 OAuth2TokenService::ScopeSet oauth2_scopes; |
1883 if (profile_->IsManaged()) { | 1884 if (profile_->IsManaged()) { |
1884 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); | 1885 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); |
1885 } else { | 1886 } else { |
1886 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 1887 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
1887 } | 1888 } |
1888 | 1889 |
1889 OAuth2TokenService* token_service = | 1890 ProfileOAuth2TokenService* token_service = |
1890 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 1891 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
1891 // Invalidate previous token, otherwise token service will return the same | 1892 // Invalidate previous token, otherwise token service will return the same |
1892 // token again. | 1893 // token again. |
1893 if (!access_token_.empty()) | 1894 if (!access_token_.empty()) |
1894 token_service->InvalidateToken(oauth2_scopes, access_token_); | 1895 token_service->InvalidateToken(oauth2_scopes, access_token_); |
1895 access_token_.clear(); | 1896 access_token_.clear(); |
1896 access_token_request_ = token_service->StartRequest(oauth2_scopes, this); | 1897 access_token_request_ = token_service->StartRequest( |
| 1898 token_service->GetPrimaryAccountId(), oauth2_scopes, this); |
1897 } | 1899 } |
1898 | 1900 |
1899 void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase, | 1901 void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase, |
1900 PassphraseType type) { | 1902 PassphraseType type) { |
1901 // This should only be called when the backend has been initialized. | 1903 // This should only be called when the backend has been initialized. |
1902 DCHECK(sync_initialized()); | 1904 DCHECK(sync_initialized()); |
1903 DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) << | 1905 DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) << |
1904 "Data is already encrypted using an explicit passphrase"; | 1906 "Data is already encrypted using an explicit passphrase"; |
1905 DCHECK(!(type == EXPLICIT && | 1907 DCHECK(!(type == EXPLICIT && |
1906 passphrase_required_reason_ == syncer::REASON_DECRYPTION)) << | 1908 passphrase_required_reason_ == syncer::REASON_DECRYPTION)) << |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2132 NOTREACHED(); | 2134 NOTREACHED(); |
2133 #endif | 2135 #endif |
2134 } | 2136 } |
2135 | 2137 |
2136 return signin_->GetAuthenticatedUsername(); | 2138 return signin_->GetAuthenticatedUsername(); |
2137 } | 2139 } |
2138 | 2140 |
2139 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { | 2141 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { |
2140 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); | 2142 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); |
2141 } | 2143 } |
OLD | NEW |