| 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/credential_cache_service_win.h" | 5 #include "chrome/browser/sync/credential_cache_service_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 128 |
| 129 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { | 129 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
| 130 // The user has signed in. Write the new value of the google username to | 130 // The user has signed in. Write the new value of the google username to |
| 131 // the local cache. | 131 // the local cache. |
| 132 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 132 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 133 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, | 133 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, |
| 134 signin->GetAuthenticatedUsername()); | 134 signin->GetAuthenticatedUsername()); |
| 135 break; | 135 break; |
| 136 } | 136 } |
| 137 | 137 |
| 138 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: { |
| 139 // Local sync configuration is done. The sync service is now ready to |
| 140 // be reconfigured. Immediately look for any unconsumed config changes in |
| 141 // the alternate profile. If all changes have been consumed, this is a |
| 142 // no-op. |
| 143 ScheduleNextReadFromAlternateCredentialCache(0); |
| 144 break; |
| 145 } |
| 146 |
| 138 case chrome::NOTIFICATION_SYNC_CONFIGURE_START: { | 147 case chrome::NOTIFICATION_SYNC_CONFIGURE_START: { |
| 139 // We have detected a sync sign in, auto-start or reconfigure. Write the | 148 // We have detected a sync sign in, auto-start or reconfigure. Write the |
| 140 // latest sync preferences to the local cache. | 149 // latest sync preferences to the local cache. |
| 141 WriteSyncPrefsToLocalCache(); | 150 WriteSyncPrefsToLocalCache(); |
| 142 break; | 151 break; |
| 143 } | 152 } |
| 144 | 153 |
| 145 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { | 154 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { |
| 146 // The token service has been fully initialized. Update the token service | 155 // The token service has been fully initialized. Update the token service |
| 147 // credentials in the local cache. This is a no-op if the cache already | 156 // credentials in the local cache. This is a no-op if the cache already |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 DCHECK(alternate_store_.get()); | 206 DCHECK(alternate_store_.get()); |
| 198 if (!HasPref(alternate_store_, prefs::kGoogleServicesUsername) || | 207 if (!HasPref(alternate_store_, prefs::kGoogleServicesUsername) || |
| 199 !HasPref(alternate_store_, GaiaConstants::kGaiaLsid) || | 208 !HasPref(alternate_store_, GaiaConstants::kGaiaLsid) || |
| 200 !HasPref(alternate_store_, GaiaConstants::kGaiaSid) || | 209 !HasPref(alternate_store_, GaiaConstants::kGaiaSid) || |
| 201 !(HasPref(alternate_store_, prefs::kSyncEncryptionBootstrapToken) || | 210 !(HasPref(alternate_store_, prefs::kSyncEncryptionBootstrapToken) || |
| 202 HasPref(alternate_store_, | 211 HasPref(alternate_store_, |
| 203 prefs::kSyncKeystoreEncryptionBootstrapToken)) || | 212 prefs::kSyncKeystoreEncryptionBootstrapToken)) || |
| 204 !HasPref(alternate_store_, prefs::kSyncKeepEverythingSynced)) { | 213 !HasPref(alternate_store_, prefs::kSyncKeepEverythingSynced)) { |
| 205 VLOG(1) << "Could not find cached credentials in \"" | 214 VLOG(1) << "Could not find cached credentials in \"" |
| 206 << GetCredentialPathInAlternateProfile().value() << "\"."; | 215 << GetCredentialPathInAlternateProfile().value() << "\"."; |
| 207 ScheduleNextReadFromAlternateCredentialCache(); | 216 ScheduleNextReadFromAlternateCredentialCache( |
| 217 kCredentialCachePollIntervalSecs); |
| 208 return; | 218 return; |
| 209 } | 219 } |
| 210 | 220 |
| 211 // Extract the google username, lsid and sid from the alternate credential | 221 // Extract the google username, lsid and sid from the alternate credential |
| 212 // cache. | 222 // cache. |
| 213 std::string alternate_google_services_username = | 223 std::string alternate_google_services_username = |
| 214 GetAndUnpackStringPref(alternate_store_, prefs::kGoogleServicesUsername); | 224 GetAndUnpackStringPref(alternate_store_, prefs::kGoogleServicesUsername); |
| 215 std::string alternate_lsid = | 225 std::string alternate_lsid = |
| 216 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaLsid); | 226 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaLsid); |
| 217 std::string alternate_sid = | 227 std::string alternate_sid = |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 alternate_google_services_username, | 313 alternate_google_services_username, |
| 304 alternate_encryption_bootstrap_token, | 314 alternate_encryption_bootstrap_token, |
| 305 alternate_keystore_encryption_bootstrap_token, | 315 alternate_keystore_encryption_bootstrap_token, |
| 306 alternate_keep_everything_synced, | 316 alternate_keep_everything_synced, |
| 307 alternate_preferred_types); | 317 alternate_preferred_types); |
| 308 UpdateTokenServiceCredentials(alternate_lsid, alternate_sid); | 318 UpdateTokenServiceCredentials(alternate_lsid, alternate_sid); |
| 309 } | 319 } |
| 310 | 320 |
| 311 // Schedule the next read from the alternate credential cache so that we can | 321 // Schedule the next read from the alternate credential cache so that we can |
| 312 // detect future reconfigures or sign outs. | 322 // detect future reconfigures or sign outs. |
| 313 ScheduleNextReadFromAlternateCredentialCache(); | 323 ScheduleNextReadFromAlternateCredentialCache( |
| 324 kCredentialCachePollIntervalSecs); |
| 314 } | 325 } |
| 315 | 326 |
| 316 void CredentialCacheService::WriteSyncPrefsToLocalCache() { | 327 void CredentialCacheService::WriteSyncPrefsToLocalCache() { |
| 317 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, | 328 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, |
| 318 sync_prefs_.HasKeepEverythingSynced()); | 329 sync_prefs_.HasKeepEverythingSynced()); |
| 319 ProfileSyncService* service = | 330 ProfileSyncService* service = |
| 320 ProfileSyncServiceFactory::GetForProfile(profile_); | 331 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 321 ModelTypeSet registered_types = service->GetRegisteredDataTypes(); | 332 ModelTypeSet registered_types = service->GetRegisteredDataTypes(); |
| 322 for (ModelTypeSet::Iterator it = registered_types.First(); | 333 for (ModelTypeSet::Iterator it = registered_types.First(); |
| 323 it.Good(); | 334 it.Good(); |
| 324 it.Inc()) { | 335 it.Inc()) { |
| 325 std::string datatype_pref_name = | 336 std::string datatype_pref_name = |
| 326 browser_sync::SyncPrefs::GetPrefNameForDataType(it.Get()); | 337 browser_sync::SyncPrefs::GetPrefNameForDataType(it.Get()); |
| 327 UpdateBooleanPref( | 338 UpdateBooleanPref( |
| 328 datatype_pref_name, | 339 datatype_pref_name, |
| 329 profile_->GetPrefs()->GetBoolean(datatype_pref_name.c_str())); | 340 profile_->GetPrefs()->GetBoolean(datatype_pref_name.c_str())); |
| 330 } | 341 } |
| 331 } | 342 } |
| 332 | 343 |
| 333 void CredentialCacheService::ScheduleNextReadFromAlternateCredentialCache() { | 344 void CredentialCacheService::ScheduleNextReadFromAlternateCredentialCache( |
| 345 int delay_secs) { |
| 346 DCHECK_LE(0, delay_secs); |
| 334 // We must reinitialize |alternate_store_| here because the underlying | 347 // We must reinitialize |alternate_store_| here because the underlying |
| 335 // credential file in the alternate profile might have changed, and we must | 348 // credential file in the alternate profile might have changed, and we must |
| 336 // re-read it afresh. | 349 // re-read it afresh. |
| 337 alternate_store_observer_.release(); | 350 alternate_store_observer_.release(); |
| 338 alternate_store_.release(); | 351 alternate_store_.release(); |
| 339 next_read_.Reset(base::Bind( | 352 next_read_.Reset(base::Bind( |
| 340 &CredentialCacheService::LookForCachedCredentialsInAlternateProfile, | 353 &CredentialCacheService::LookForCachedCredentialsInAlternateProfile, |
| 341 weak_factory_.GetWeakPtr())); | 354 weak_factory_.GetWeakPtr())); |
| 342 MessageLoop::current()->PostDelayedTask( | 355 MessageLoop::current()->PostDelayedTask( |
| 343 FROM_HERE, | 356 FROM_HERE, |
| 344 next_read_.callback(), | 357 next_read_.callback(), |
| 345 TimeDelta::FromSeconds(kCredentialCachePollIntervalSecs)); | 358 TimeDelta::FromSeconds(delay_secs)); |
| 346 } | 359 } |
| 347 | 360 |
| 348 bool CredentialCacheService::HasPref(scoped_refptr<JsonPrefStore> store, | 361 bool CredentialCacheService::HasPref(scoped_refptr<JsonPrefStore> store, |
| 349 const std::string& pref_name) { | 362 const std::string& pref_name) { |
| 350 return (store->GetValue(pref_name, NULL) == PrefStore::READ_OK); | 363 return (store->GetValue(pref_name, NULL) == PrefStore::READ_OK); |
| 351 } | 364 } |
| 352 | 365 |
| 353 // static | 366 // static |
| 354 base::StringValue* CredentialCacheService::PackCredential( | 367 base::StringValue* CredentialCacheService::PackCredential( |
| 355 const std::string& credential) { | 368 const std::string& credential) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 } | 590 } |
| 578 | 591 |
| 579 void CredentialCacheService::AlternateStoreObserver::OnInitializationCompleted( | 592 void CredentialCacheService::AlternateStoreObserver::OnInitializationCompleted( |
| 580 bool succeeded) { | 593 bool succeeded) { |
| 581 // If an alternate credential cache was found, begin consuming its contents. | 594 // If an alternate credential cache was found, begin consuming its contents. |
| 582 // If not, schedule a future read. | 595 // If not, schedule a future read. |
| 583 if (succeeded && | 596 if (succeeded && |
| 584 alternate_store_->GetReadError() == JsonPrefStore::PREF_READ_ERROR_NONE) { | 597 alternate_store_->GetReadError() == JsonPrefStore::PREF_READ_ERROR_NONE) { |
| 585 service_->ReadCachedCredentialsFromAlternateProfile(); | 598 service_->ReadCachedCredentialsFromAlternateProfile(); |
| 586 } else { | 599 } else { |
| 587 service_->ScheduleNextReadFromAlternateCredentialCache(); | 600 service_->ScheduleNextReadFromAlternateCredentialCache( |
| 601 kCredentialCachePollIntervalSecs); |
| 588 } | 602 } |
| 589 } | 603 } |
| 590 | 604 |
| 591 void CredentialCacheService::AlternateStoreObserver::OnPrefValueChanged( | 605 void CredentialCacheService::AlternateStoreObserver::OnPrefValueChanged( |
| 592 const std::string& key) { | 606 const std::string& key) { |
| 593 // Nothing to do here, since credentials are cached silently. | 607 // Nothing to do here, since credentials are cached silently. |
| 594 } | 608 } |
| 595 | 609 |
| 596 FilePath CredentialCacheService::GetCredentialPathInCurrentProfile() const { | 610 FilePath CredentialCacheService::GetCredentialPathInCurrentProfile() const { |
| 597 // The sync credential path in the default Desktop profile is | 611 // The sync credential path in the default Desktop profile is |
| (...skipping 29 matching lines...) Expand all Loading... |
| 627 content::Source<Profile>(profile_)); | 641 content::Source<Profile>(profile_)); |
| 628 registrar_.Add(this, | 642 registrar_.Add(this, |
| 629 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 643 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 630 content::Source<Profile>(profile_)); | 644 content::Source<Profile>(profile_)); |
| 631 | 645 |
| 632 // Register for notifications for sync configuration changes that could occur | 646 // Register for notifications for sync configuration changes that could occur |
| 633 // during sign in or reconfiguration. | 647 // during sign in or reconfiguration. |
| 634 ProfileSyncService* service = | 648 ProfileSyncService* service = |
| 635 ProfileSyncServiceFactory::GetForProfile(profile_); | 649 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 636 registrar_.Add(this, | 650 registrar_.Add(this, |
| 651 chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
| 652 content::Source<ProfileSyncService>(service)); |
| 653 registrar_.Add(this, |
| 637 chrome::NOTIFICATION_SYNC_CONFIGURE_START, | 654 chrome::NOTIFICATION_SYNC_CONFIGURE_START, |
| 638 content::Source<ProfileSyncService>(service)); | 655 content::Source<ProfileSyncService>(service)); |
| 639 | 656 |
| 640 // Register for notifications for updates to the sync encryption tokens, which | 657 // Register for notifications for updates to the sync encryption tokens, which |
| 641 // are stored in the PrefStore. | 658 // are stored in the PrefStore. |
| 642 pref_registrar_.Init(profile_->GetPrefs()); | 659 pref_registrar_.Init(profile_->GetPrefs()); |
| 643 pref_registrar_.Add(prefs::kSyncEncryptionBootstrapToken, this); | 660 pref_registrar_.Add(prefs::kSyncEncryptionBootstrapToken, this); |
| 644 pref_registrar_.Add(prefs::kSyncKeystoreEncryptionBootstrapToken, this); | 661 pref_registrar_.Add(prefs::kSyncKeystoreEncryptionBootstrapToken, this); |
| 645 | 662 |
| 646 // Register for notifications for updates to lsid and sid, which are stored in | 663 // Register for notifications for updates to lsid and sid, which are stored in |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 | 748 |
| 732 void CredentialCacheService::InitiateSignOut() { | 749 void CredentialCacheService::InitiateSignOut() { |
| 733 ProfileSyncService* service = | 750 ProfileSyncService* service = |
| 734 ProfileSyncServiceFactory::GetForProfile(profile_); | 751 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 735 service->DisableForUser(); | 752 service->DisableForUser(); |
| 736 } | 753 } |
| 737 | 754 |
| 738 bool CredentialCacheService::HaveSyncPrefsChanged( | 755 bool CredentialCacheService::HaveSyncPrefsChanged( |
| 739 bool alternate_keep_everything_synced, | 756 bool alternate_keep_everything_synced, |
| 740 ModelTypeSet alternate_preferred_types) const { | 757 ModelTypeSet alternate_preferred_types) const { |
| 758 if (alternate_keep_everything_synced && |
| 759 sync_prefs_.HasKeepEverythingSynced()) { |
| 760 return false; |
| 761 } |
| 741 ProfileSyncService* service = | 762 ProfileSyncService* service = |
| 742 ProfileSyncServiceFactory::GetForProfile(profile_); | 763 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 743 ModelTypeSet local_preferred_types = | 764 ModelTypeSet local_preferred_types = |
| 744 sync_prefs_.GetPreferredDataTypes(service->GetRegisteredDataTypes()); | 765 sync_prefs_.GetPreferredDataTypes(service->GetRegisteredDataTypes()); |
| 745 return | 766 return |
| 746 (alternate_keep_everything_synced != | 767 (alternate_keep_everything_synced != |
| 747 sync_prefs_.HasKeepEverythingSynced()) || | 768 sync_prefs_.HasKeepEverythingSynced()) || |
| 748 !alternate_preferred_types.Equals(local_preferred_types); | 769 !alternate_preferred_types.Equals(local_preferred_types); |
| 749 } | 770 } |
| 750 | 771 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 } | 818 } |
| 798 | 819 |
| 799 bool CredentialCacheService::MayReconfigureSync( | 820 bool CredentialCacheService::MayReconfigureSync( |
| 800 const std::string& alternate_google_services_username) { | 821 const std::string& alternate_google_services_username) { |
| 801 // We may attempt to reconfigure sync iff: | 822 // We may attempt to reconfigure sync iff: |
| 802 // 1) The user is signed in to the local profile. | 823 // 1) The user is signed in to the local profile. |
| 803 // 2) The user has never signed out of the local profile in the past. | 824 // 2) The user has never signed out of the local profile in the past. |
| 804 // 3) The user is signed in to the alternate profile with the same account. | 825 // 3) The user is signed in to the alternate profile with the same account. |
| 805 // 4) The user is not already in the process of configuring sync. | 826 // 4) The user is not already in the process of configuring sync. |
| 806 // 5) The alternate cache was updated more recently than the local cache. | 827 // 5) The alternate cache was updated more recently than the local cache. |
| 828 // 6) The sync backend is initialized and ready to consume config changes. |
| 807 ProfileSyncService* service = | 829 ProfileSyncService* service = |
| 808 ProfileSyncServiceFactory::GetForProfile(profile_); | 830 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 809 return !service->signin()->GetAuthenticatedUsername().empty() && | 831 return !service->signin()->GetAuthenticatedUsername().empty() && |
| 810 !HasUserSignedOut() && | 832 !HasUserSignedOut() && |
| 811 (alternate_google_services_username == | 833 (alternate_google_services_username == |
| 812 service->signin()->GetAuthenticatedUsername()) && | 834 service->signin()->GetAuthenticatedUsername()) && |
| 813 !service->setup_in_progress() && | 835 !service->setup_in_progress() && |
| 814 AlternateCacheIsMoreRecent(); | 836 AlternateCacheIsMoreRecent() && |
| 837 service->ShouldPushChanges(); |
| 815 } | 838 } |
| 816 | 839 |
| 817 bool CredentialCacheService::ShouldSignInToSync( | 840 bool CredentialCacheService::ShouldSignInToSync( |
| 818 const std::string& alternate_google_services_username, | 841 const std::string& alternate_google_services_username, |
| 819 const std::string& alternate_lsid, | 842 const std::string& alternate_lsid, |
| 820 const std::string& alternate_sid, | 843 const std::string& alternate_sid, |
| 821 const std::string& alternate_encryption_bootstrap_token, | 844 const std::string& alternate_encryption_bootstrap_token, |
| 822 const std::string& alternate_keystore_encryption_bootstrap_token) { | 845 const std::string& alternate_keystore_encryption_bootstrap_token) { |
| 823 // We should sign in with cached credentials from the alternate profile iff: | 846 // We should sign in with cached credentials from the alternate profile iff: |
| 824 // 1) The user is not currently signed in to the local profile. | 847 // 1) The user is not currently signed in to the local profile. |
| 825 // 2) The user has never signed out of the local profile in the past. | 848 // 2) The user has never signed out of the local profile in the past. |
| 826 // 3) Valid cached credentials are available in the alternate profile. | 849 // 3) Valid cached credentials are available in the alternate profile. |
| 827 // 4) The user is not already in the process of configuring sync. | 850 // 4) The user is not already in the process of configuring sync. |
| 828 ProfileSyncService* service = | 851 ProfileSyncService* service = |
| 829 ProfileSyncServiceFactory::GetForProfile(profile_); | 852 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 830 return service->signin()->GetAuthenticatedUsername().empty() && | 853 return service->signin()->GetAuthenticatedUsername().empty() && |
| 831 !HasUserSignedOut() && | 854 !HasUserSignedOut() && |
| 832 !alternate_google_services_username.empty() && | 855 !alternate_google_services_username.empty() && |
| 833 !alternate_lsid.empty() && | 856 !alternate_lsid.empty() && |
| 834 !alternate_sid.empty() && | 857 !alternate_sid.empty() && |
| 835 !(alternate_encryption_bootstrap_token.empty() && | 858 !(alternate_encryption_bootstrap_token.empty() && |
| 836 alternate_keystore_encryption_bootstrap_token.empty()) && | 859 alternate_keystore_encryption_bootstrap_token.empty()) && |
| 837 !service->setup_in_progress(); | 860 !service->setup_in_progress(); |
| 838 } | 861 } |
| 839 | 862 |
| 840 } // namespace syncer | 863 } // namespace syncer |
| OLD | NEW |