Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/browser/sync/profile_sync_service.cc

Issue 137753012: [sync]: add ManagedUserSigninManagerWrapper and pass to ProfileSyncService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 #include "sync/internal_api/public/sync_encryption_handler.h" 79 #include "sync/internal_api/public/sync_encryption_handler.h"
79 #include "sync/internal_api/public/util/experiments.h" 80 #include "sync/internal_api/public/util/experiments.h"
80 #include "sync/internal_api/public/util/sync_string_conversions.h" 81 #include "sync/internal_api/public/util/sync_string_conversions.h"
81 #include "sync/js/js_arg_list.h" 82 #include "sync/js/js_arg_list.h"
82 #include "sync/js/js_event_details.h" 83 #include "sync/js/js_event_details.h"
83 #include "sync/util/cryptographer.h" 84 #include "sync/util/cryptographer.h"
84 #include "ui/base/l10n/l10n_util.h" 85 #include "ui/base/l10n/l10n_util.h"
85 #include "ui/base/l10n/time_format.h" 86 #include "ui/base/l10n/time_format.h"
86 87
87 #if defined(ENABLE_MANAGED_USERS) 88 #if defined(ENABLE_MANAGED_USERS)
88 #include "chrome/browser/managed_mode/managed_user_constants.h" 89 #include "chrome/browser/managed_mode/managed_user_constants.h"
pavely 2014/02/11 22:24:17 Do you need to remove this include as well?
89 #endif 90 #endif
90 91
91 #if defined(OS_ANDROID) 92 #if defined(OS_ANDROID)
92 #include "sync/internal_api/public/read_transaction.h" 93 #include "sync/internal_api/public/read_transaction.h"
93 #endif 94 #endif
94 95
95 using browser_sync::ChangeProcessor; 96 using browser_sync::ChangeProcessor;
96 using browser_sync::DataTypeController; 97 using browser_sync::DataTypeController;
97 using browser_sync::DataTypeManager; 98 using browser_sync::DataTypeManager;
98 using browser_sync::FailedDataTypesHandler; 99 using browser_sync::FailedDataTypesHandler;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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
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 1199 matching lines...) Expand 10 before | Expand all | Expand 10 after
1959 request_access_token_retry_timer_.Stop(); 1961 request_access_token_retry_timer_.Stop();
1960 OAuth2TokenService::ScopeSet oauth2_scopes; 1962 OAuth2TokenService::ScopeSet oauth2_scopes;
1961 if (profile_->IsManaged()) { 1963 if (profile_->IsManaged()) {
1962 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope); 1964 oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope);
1963 } else { 1965 } else {
1964 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); 1966 oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
1965 } 1967 }
1966 1968
1967 // Invalidate previous token, otherwise token service will return the same 1969 // Invalidate previous token, otherwise token service will return the same
1968 // token again. 1970 // token again.
1969 const std::string& account_id = GetAccountIdToUse(); 1971 const std::string& account_id = signin_->GetAccountIdToUse();
1970 if (!access_token_.empty()) { 1972 if (!access_token_.empty()) {
1971 oauth2_token_service_->InvalidateToken( 1973 oauth2_token_service_->InvalidateToken(
1972 account_id, oauth2_scopes, access_token_); 1974 account_id, oauth2_scopes, access_token_);
1973 } 1975 }
1974 1976
1975 access_token_.clear(); 1977 access_token_.clear();
1976 1978
1977 token_request_time_ = base::Time::Now(); 1979 token_request_time_ = base::Time::Now();
1978 token_receive_time_ = base::Time(); 1980 token_receive_time_ = base::Time();
1979 next_token_request_time_ = base::Time(); 1981 next_token_request_time_ = base::Time();
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
2145 if (backend_) { 2147 if (backend_) {
2146 backend_->UnregisterInvalidationIds(); 2148 backend_->UnregisterInvalidationIds();
2147 } 2149 }
2148 ShutdownImpl(browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD); 2150 ShutdownImpl(browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD);
2149 } 2151 }
2150 2152
2151 bool ProfileSyncService::IsStartSuppressed() const { 2153 bool ProfileSyncService::IsStartSuppressed() const {
2152 return sync_prefs_.IsStartSuppressed(); 2154 return sync_prefs_.IsStartSuppressed();
2153 } 2155 }
2154 2156
2157 SigninManagerBase* ProfileSyncService::signin() const {
2158 return signin_->GetOriginal();
2159 }
2160
2155 void ProfileSyncService::UnsuppressAndStart() { 2161 void ProfileSyncService::UnsuppressAndStart() {
2156 DCHECK(profile_); 2162 DCHECK(profile_);
2157 sync_prefs_.SetStartSuppressed(false); 2163 sync_prefs_.SetStartSuppressed(false);
2158 // Set username in SigninManager, as SigninManager::OnGetUserInfoSuccess 2164 // Set username in SigninManager, as SigninManager::OnGetUserInfoSuccess
2159 // is never called for some clients. 2165 // is never called for some clients.
2160 if (signin_ && signin_->GetAuthenticatedUsername().empty()) { 2166 if (signin_.get() &&
2161 signin_->SetAuthenticatedUsername(sync_prefs_.GetGoogleServicesUsername()); 2167 signin_->GetOriginal()->GetAuthenticatedUsername().empty()) {
2168 signin_->GetOriginal()->SetAuthenticatedUsername(
2169 sync_prefs_.GetGoogleServicesUsername());
2162 } 2170 }
2163 TryStart(); 2171 TryStart();
2164 } 2172 }
2165 2173
2166 void ProfileSyncService::AcknowledgeSyncedTypes() { 2174 void ProfileSyncService::AcknowledgeSyncedTypes() {
2167 sync_prefs_.AcknowledgeSyncedTypes(GetRegisteredDataTypes()); 2175 sync_prefs_.AcknowledgeSyncedTypes(GetRegisteredDataTypes());
2168 } 2176 }
2169 2177
2170 void ProfileSyncService::ReconfigureDatatypeManager() { 2178 void ProfileSyncService::ReconfigureDatatypeManager() {
2171 // If we haven't initialized yet, don't configure the DTM as it could cause 2179 // If we haven't initialized yet, don't configure the DTM as it could cause
(...skipping 29 matching lines...) Expand all
2201 } 2209 }
2202 2210
2203 bool ProfileSyncService::IsRetryingAccessTokenFetchForTest() const { 2211 bool ProfileSyncService::IsRetryingAccessTokenFetchForTest() const {
2204 return request_access_token_retry_timer_.IsRunning(); 2212 return request_access_token_retry_timer_.IsRunning();
2205 } 2213 }
2206 2214
2207 std::string ProfileSyncService::GetAccessTokenForTest() const { 2215 std::string ProfileSyncService::GetAccessTokenForTest() const {
2208 return access_token_; 2216 return access_token_;
2209 } 2217 }
2210 2218
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
2237 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { 2219 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() {
2238 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); 2220 return MakeWeakHandle(sync_js_controller_.AsWeakPtr());
2239 } 2221 }
2240 2222
2241 syncer::SyncableService* ProfileSyncService::GetSessionsSyncableService() { 2223 syncer::SyncableService* ProfileSyncService::GetSessionsSyncableService() {
2242 return sessions_sync_manager_.get(); 2224 return sessions_sync_manager_.get();
2243 } 2225 }
2244 2226
2245 ProfileSyncService::SyncTokenStatus::SyncTokenStatus() 2227 ProfileSyncService::SyncTokenStatus::SyncTokenStatus()
2246 : connection_status(syncer::CONNECTION_NOT_ATTEMPTED), 2228 : connection_status(syncer::CONNECTION_NOT_ATTEMPTED),
(...skipping 10 matching lines...) Expand all
2257 status.last_get_token_error = last_get_token_error_; 2239 status.last_get_token_error = last_get_token_error_;
2258 if (request_access_token_retry_timer_.IsRunning()) 2240 if (request_access_token_retry_timer_.IsRunning())
2259 status.next_token_request_time = next_token_request_time_; 2241 status.next_token_request_time = next_token_request_time_;
2260 return status; 2242 return status;
2261 } 2243 }
2262 2244
2263 void ProfileSyncService::OverrideNetworkResourcesForTest( 2245 void ProfileSyncService::OverrideNetworkResourcesForTest(
2264 scoped_ptr<syncer::NetworkResources> network_resources) { 2246 scoped_ptr<syncer::NetworkResources> network_resources) {
2265 network_resources_ = network_resources.Pass(); 2247 network_resources_ = network_resources.Pass();
2266 } 2248 }
OLDNEW
« no previous file with comments | « chrome/browser/sync/profile_sync_service.h ('k') | chrome/browser/sync/profile_sync_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698