Chromium Code Reviews| 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/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 | 131 |
| 132 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { | 132 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
| 133 // The user has signed in. Write the new value of the google username to | 133 // The user has signed in. Write the new value of the google username to |
| 134 // the local cache. | 134 // the local cache. |
| 135 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 135 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 136 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, | 136 PackAndUpdateStringPref(prefs::kGoogleServicesUsername, |
| 137 signin->GetAuthenticatedUsername()); | 137 signin->GetAuthenticatedUsername()); |
| 138 break; | 138 break; |
| 139 } | 139 } |
| 140 | 140 |
| 141 case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: { | |
| 142 // Local sync configuration is done. The sync service is now ready to | |
| 143 // be reconfigured. Immediately look for any unconsumed config changes in | |
| 144 // the alternate profile. If all changes have been consumed, this is a | |
| 145 // no-op. | |
| 146 ScheduleNextReadFromAlternateCredentialCache(0); | |
| 147 break; | |
| 148 } | |
| 149 | |
| 141 case chrome::NOTIFICATION_SYNC_CONFIGURE_START: { | 150 case chrome::NOTIFICATION_SYNC_CONFIGURE_START: { |
| 142 // We have detected a sync sign in, auto-start or reconfigure. Write the | 151 // We have detected a sync sign in, auto-start or reconfigure. Write the |
| 143 // latest sync preferences to the local cache. | 152 // latest sync preferences to the local cache. |
| 144 WriteSyncPrefsToLocalCache(); | 153 WriteSyncPrefsToLocalCache(); |
| 145 break; | 154 break; |
| 146 } | 155 } |
| 147 | 156 |
| 148 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { | 157 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { |
| 149 // The token service has been fully initialized. Update the token service | 158 // The token service has been fully initialized. Update the token service |
| 150 // credentials in the local cache. This is a no-op if the cache already | 159 // 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... | |
| 200 DCHECK(alternate_store_.get()); | 209 DCHECK(alternate_store_.get()); |
| 201 if (!HasPref(alternate_store_, prefs::kGoogleServicesUsername) || | 210 if (!HasPref(alternate_store_, prefs::kGoogleServicesUsername) || |
| 202 !HasPref(alternate_store_, GaiaConstants::kGaiaLsid) || | 211 !HasPref(alternate_store_, GaiaConstants::kGaiaLsid) || |
| 203 !HasPref(alternate_store_, GaiaConstants::kGaiaSid) || | 212 !HasPref(alternate_store_, GaiaConstants::kGaiaSid) || |
| 204 !(HasPref(alternate_store_, prefs::kSyncEncryptionBootstrapToken) || | 213 !(HasPref(alternate_store_, prefs::kSyncEncryptionBootstrapToken) || |
| 205 HasPref(alternate_store_, | 214 HasPref(alternate_store_, |
| 206 prefs::kSyncKeystoreEncryptionBootstrapToken)) || | 215 prefs::kSyncKeystoreEncryptionBootstrapToken)) || |
| 207 !HasPref(alternate_store_, prefs::kSyncKeepEverythingSynced)) { | 216 !HasPref(alternate_store_, prefs::kSyncKeepEverythingSynced)) { |
| 208 VLOG(1) << "Could not find cached credentials in \"" | 217 VLOG(1) << "Could not find cached credentials in \"" |
| 209 << GetCredentialPathInAlternateProfile().value() << "\"."; | 218 << GetCredentialPathInAlternateProfile().value() << "\"."; |
| 210 ScheduleNextReadFromAlternateCredentialCache(); | 219 ScheduleNextReadFromAlternateCredentialCache( |
| 220 kCredentialCachePollIntervalSecs); | |
| 211 return; | 221 return; |
| 212 } | 222 } |
| 213 | 223 |
| 214 // Extract the google username, lsid and sid from the alternate credential | 224 // Extract the google username, lsid and sid from the alternate credential |
| 215 // cache. | 225 // cache. |
| 216 std::string alternate_google_services_username = | 226 std::string alternate_google_services_username = |
| 217 GetAndUnpackStringPref(alternate_store_, prefs::kGoogleServicesUsername); | 227 GetAndUnpackStringPref(alternate_store_, prefs::kGoogleServicesUsername); |
| 218 std::string alternate_lsid = | 228 std::string alternate_lsid = |
| 219 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaLsid); | 229 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaLsid); |
| 220 std::string alternate_sid = | 230 std::string alternate_sid = |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 alternate_google_services_username, | 316 alternate_google_services_username, |
| 307 alternate_encryption_bootstrap_token, | 317 alternate_encryption_bootstrap_token, |
| 308 alternate_keystore_encryption_bootstrap_token, | 318 alternate_keystore_encryption_bootstrap_token, |
| 309 alternate_keep_everything_synced, | 319 alternate_keep_everything_synced, |
| 310 alternate_preferred_types); | 320 alternate_preferred_types); |
| 311 UpdateTokenServiceCredentials(alternate_lsid, alternate_sid); | 321 UpdateTokenServiceCredentials(alternate_lsid, alternate_sid); |
| 312 } | 322 } |
| 313 | 323 |
| 314 // Schedule the next read from the alternate credential cache so that we can | 324 // Schedule the next read from the alternate credential cache so that we can |
| 315 // detect future reconfigures or sign outs. | 325 // detect future reconfigures or sign outs. |
| 316 ScheduleNextReadFromAlternateCredentialCache(); | 326 ScheduleNextReadFromAlternateCredentialCache( |
| 327 kCredentialCachePollIntervalSecs); | |
| 317 } | 328 } |
| 318 | 329 |
| 319 void CredentialCacheService::WriteSyncPrefsToLocalCache() { | 330 void CredentialCacheService::WriteSyncPrefsToLocalCache() { |
| 320 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, | 331 UpdateBooleanPref(prefs::kSyncKeepEverythingSynced, |
| 321 sync_prefs_.HasKeepEverythingSynced()); | 332 sync_prefs_.HasKeepEverythingSynced()); |
| 322 ProfileSyncService* service = | 333 ProfileSyncService* service = |
| 323 ProfileSyncServiceFactory::GetForProfile(profile_); | 334 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 324 ModelTypeSet registered_types = service->GetRegisteredDataTypes(); | 335 ModelTypeSet registered_types = service->GetRegisteredDataTypes(); |
| 325 for (ModelTypeSet::Iterator it = registered_types.First(); | 336 for (ModelTypeSet::Iterator it = registered_types.First(); |
| 326 it.Good(); | 337 it.Good(); |
| 327 it.Inc()) { | 338 it.Inc()) { |
| 328 std::string datatype_pref_name = | 339 std::string datatype_pref_name = |
| 329 browser_sync::SyncPrefs::GetPrefNameForDataType(it.Get()); | 340 browser_sync::SyncPrefs::GetPrefNameForDataType(it.Get()); |
| 330 UpdateBooleanPref( | 341 UpdateBooleanPref( |
| 331 datatype_pref_name, | 342 datatype_pref_name, |
| 332 profile_->GetPrefs()->GetBoolean(datatype_pref_name.c_str())); | 343 profile_->GetPrefs()->GetBoolean(datatype_pref_name.c_str())); |
| 333 } | 344 } |
| 334 } | 345 } |
| 335 | 346 |
| 336 void CredentialCacheService::ScheduleNextReadFromAlternateCredentialCache() { | 347 void CredentialCacheService::ScheduleNextReadFromAlternateCredentialCache( |
| 348 int delay_secs) { | |
| 349 DCHECK_LE(0, delay_secs); | |
| 337 // We must reinitialize |alternate_store_| here because the underlying | 350 // We must reinitialize |alternate_store_| here because the underlying |
| 338 // credential file in the alternate profile might have changed, and we must | 351 // credential file in the alternate profile might have changed, and we must |
| 339 // re-read it afresh. | 352 // re-read it afresh. |
| 340 alternate_store_observer_.release(); | 353 alternate_store_observer_.release(); |
| 341 alternate_store_.release(); | 354 alternate_store_.release(); |
| 342 next_read_.Reset(base::Bind( | 355 next_read_.Reset(base::Bind( |
| 343 &CredentialCacheService::LookForCachedCredentialsInAlternateProfile, | 356 &CredentialCacheService::LookForCachedCredentialsInAlternateProfile, |
| 344 weak_factory_.GetWeakPtr())); | 357 weak_factory_.GetWeakPtr())); |
| 345 MessageLoop::current()->PostDelayedTask( | 358 MessageLoop::current()->PostDelayedTask( |
| 346 FROM_HERE, | 359 FROM_HERE, |
| 347 next_read_.callback(), | 360 next_read_.callback(), |
| 348 TimeDelta::FromSeconds(kCredentialCachePollIntervalSecs)); | 361 TimeDelta::FromSeconds(delay_secs)); |
| 349 } | 362 } |
| 350 | 363 |
| 351 bool CredentialCacheService::HasPref(scoped_refptr<JsonPrefStore> store, | 364 bool CredentialCacheService::HasPref(scoped_refptr<JsonPrefStore> store, |
| 352 const std::string& pref_name) { | 365 const std::string& pref_name) { |
| 353 return (store->GetValue(pref_name, NULL) == PrefStore::READ_OK); | 366 return (store->GetValue(pref_name, NULL) == PrefStore::READ_OK); |
| 354 } | 367 } |
| 355 | 368 |
| 356 // static | 369 // static |
| 357 base::StringValue* CredentialCacheService::PackCredential( | 370 base::StringValue* CredentialCacheService::PackCredential( |
| 358 const std::string& credential) { | 371 const std::string& credential) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 580 } | 593 } |
| 581 | 594 |
| 582 void CredentialCacheService::AlternateStoreObserver::OnInitializationCompleted( | 595 void CredentialCacheService::AlternateStoreObserver::OnInitializationCompleted( |
| 583 bool succeeded) { | 596 bool succeeded) { |
| 584 // If an alternate credential cache was found, begin consuming its contents. | 597 // If an alternate credential cache was found, begin consuming its contents. |
| 585 // If not, schedule a future read. | 598 // If not, schedule a future read. |
| 586 if (succeeded && | 599 if (succeeded && |
| 587 alternate_store_->GetReadError() == JsonPrefStore::PREF_READ_ERROR_NONE) { | 600 alternate_store_->GetReadError() == JsonPrefStore::PREF_READ_ERROR_NONE) { |
| 588 service_->ReadCachedCredentialsFromAlternateProfile(); | 601 service_->ReadCachedCredentialsFromAlternateProfile(); |
| 589 } else { | 602 } else { |
| 590 service_->ScheduleNextReadFromAlternateCredentialCache(); | 603 service_->ScheduleNextReadFromAlternateCredentialCache( |
| 604 kCredentialCachePollIntervalSecs); | |
| 591 } | 605 } |
| 592 } | 606 } |
| 593 | 607 |
| 594 void CredentialCacheService::AlternateStoreObserver::OnPrefValueChanged( | 608 void CredentialCacheService::AlternateStoreObserver::OnPrefValueChanged( |
| 595 const std::string& key) { | 609 const std::string& key) { |
| 596 // Nothing to do here, since credentials are cached silently. | 610 // Nothing to do here, since credentials are cached silently. |
| 597 } | 611 } |
| 598 | 612 |
| 599 FilePath CredentialCacheService::GetCredentialPathInCurrentProfile() const { | 613 FilePath CredentialCacheService::GetCredentialPathInCurrentProfile() const { |
| 600 // The sync credential path in the default Desktop profile is | 614 // The sync credential path in the default Desktop profile is |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 content::Source<Profile>(profile_)); | 656 content::Source<Profile>(profile_)); |
| 643 registrar_.Add(this, | 657 registrar_.Add(this, |
| 644 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 658 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 645 content::Source<Profile>(profile_)); | 659 content::Source<Profile>(profile_)); |
| 646 | 660 |
| 647 // Register for notifications for sync configuration changes that could occur | 661 // Register for notifications for sync configuration changes that could occur |
| 648 // during sign in or reconfiguration. | 662 // during sign in or reconfiguration. |
| 649 ProfileSyncService* service = | 663 ProfileSyncService* service = |
| 650 ProfileSyncServiceFactory::GetForProfile(profile_); | 664 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 651 registrar_.Add(this, | 665 registrar_.Add(this, |
| 666 chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | |
| 667 content::Source<ProfileSyncService>(service)); | |
| 668 registrar_.Add(this, | |
| 652 chrome::NOTIFICATION_SYNC_CONFIGURE_START, | 669 chrome::NOTIFICATION_SYNC_CONFIGURE_START, |
| 653 content::Source<ProfileSyncService>(service)); | 670 content::Source<ProfileSyncService>(service)); |
| 654 | 671 |
| 655 // Register for notifications for updates to the sync encryption tokens, which | 672 // Register for notifications for updates to the sync encryption tokens, which |
| 656 // are stored in the PrefStore. | 673 // are stored in the PrefStore. |
| 657 pref_registrar_.Init(profile_->GetPrefs()); | 674 pref_registrar_.Init(profile_->GetPrefs()); |
| 658 pref_registrar_.Add(prefs::kSyncEncryptionBootstrapToken, this); | 675 pref_registrar_.Add(prefs::kSyncEncryptionBootstrapToken, this); |
| 659 pref_registrar_.Add(prefs::kSyncKeystoreEncryptionBootstrapToken, this); | 676 pref_registrar_.Add(prefs::kSyncKeystoreEncryptionBootstrapToken, this); |
| 660 | 677 |
| 661 // Register for notifications for updates to lsid and sid, which are stored in | 678 // 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... | |
| 746 | 763 |
| 747 void CredentialCacheService::InitiateSignOut() { | 764 void CredentialCacheService::InitiateSignOut() { |
| 748 ProfileSyncService* service = | 765 ProfileSyncService* service = |
| 749 ProfileSyncServiceFactory::GetForProfile(profile_); | 766 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 750 service->DisableForUser(); | 767 service->DisableForUser(); |
| 751 } | 768 } |
| 752 | 769 |
| 753 bool CredentialCacheService::HaveSyncPrefsChanged( | 770 bool CredentialCacheService::HaveSyncPrefsChanged( |
| 754 bool alternate_keep_everything_synced, | 771 bool alternate_keep_everything_synced, |
| 755 ModelTypeSet alternate_preferred_types) const { | 772 ModelTypeSet alternate_preferred_types) const { |
| 773 if (alternate_keep_everything_synced && | |
| 774 sync_prefs_.HasKeepEverythingSynced()) { | |
| 775 return false; | |
| 776 } | |
| 756 ProfileSyncService* service = | 777 ProfileSyncService* service = |
| 757 ProfileSyncServiceFactory::GetForProfile(profile_); | 778 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 758 ModelTypeSet local_preferred_types = | 779 ModelTypeSet local_preferred_types = |
| 759 sync_prefs_.GetPreferredDataTypes(service->GetRegisteredDataTypes()); | 780 sync_prefs_.GetPreferredDataTypes(service->GetRegisteredDataTypes()); |
| 760 return | 781 return |
| 761 (alternate_keep_everything_synced != | 782 (alternate_keep_everything_synced != |
| 762 sync_prefs_.HasKeepEverythingSynced()) || | 783 sync_prefs_.HasKeepEverythingSynced()) || |
| 763 !alternate_preferred_types.Equals(local_preferred_types); | 784 !alternate_preferred_types.Equals(local_preferred_types); |
| 764 } | 785 } |
| 765 | 786 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 812 } | 833 } |
| 813 | 834 |
| 814 bool CredentialCacheService::MayReconfigureSync( | 835 bool CredentialCacheService::MayReconfigureSync( |
| 815 const std::string& alternate_google_services_username) { | 836 const std::string& alternate_google_services_username) { |
| 816 // We may attempt to reconfigure sync iff: | 837 // We may attempt to reconfigure sync iff: |
| 817 // 1) The user is signed in to the local profile. | 838 // 1) The user is signed in to the local profile. |
| 818 // 2) The user has never signed out of the local profile in the past. | 839 // 2) The user has never signed out of the local profile in the past. |
| 819 // 3) The user is signed in to the alternate profile with the same account. | 840 // 3) The user is signed in to the alternate profile with the same account. |
| 820 // 4) The user is not already in the process of configuring sync. | 841 // 4) The user is not already in the process of configuring sync. |
| 821 // 5) The alternate cache was updated more recently than the local cache. | 842 // 5) The alternate cache was updated more recently than the local cache. |
| 843 // 6) The sync backend is initialized and ready to consume config changes. | |
| 822 ProfileSyncService* service = | 844 ProfileSyncService* service = |
| 823 ProfileSyncServiceFactory::GetForProfile(profile_); | 845 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 824 return !service->signin()->GetAuthenticatedUsername().empty() && | 846 return !service->signin()->GetAuthenticatedUsername().empty() && |
| 825 !HasUserSignedOut() && | 847 !HasUserSignedOut() && |
| 826 (alternate_google_services_username == | 848 (alternate_google_services_username == |
| 827 service->signin()->GetAuthenticatedUsername()) && | 849 service->signin()->GetAuthenticatedUsername()) && |
| 828 !service->setup_in_progress() && | 850 !service->setup_in_progress() && |
| 829 AlternateCacheIsMoreRecent(); | 851 AlternateCacheIsMoreRecent() && |
| 852 service->sync_initialized(); | |
|
Nicolas Zea
2012/08/23 23:00:48
would should push changes be more appropriate here
Raghu Simha
2012/08/23 23:15:20
Good idea. Makes sense to wait for local config to
| |
| 830 } | 853 } |
| 831 | 854 |
| 832 bool CredentialCacheService::ShouldSignInToSync( | 855 bool CredentialCacheService::ShouldSignInToSync( |
| 833 const std::string& alternate_google_services_username, | 856 const std::string& alternate_google_services_username, |
| 834 const std::string& alternate_lsid, | 857 const std::string& alternate_lsid, |
| 835 const std::string& alternate_sid, | 858 const std::string& alternate_sid, |
| 836 const std::string& alternate_encryption_bootstrap_token, | 859 const std::string& alternate_encryption_bootstrap_token, |
| 837 const std::string& alternate_keystore_encryption_bootstrap_token) { | 860 const std::string& alternate_keystore_encryption_bootstrap_token) { |
| 838 // We should sign in with cached credentials from the alternate profile iff: | 861 // We should sign in with cached credentials from the alternate profile iff: |
| 839 // 1) The user is not currently signed in to the local profile. | 862 // 1) The user is not currently signed in to the local profile. |
| 840 // 2) The user has never signed out of the local profile in the past. | 863 // 2) The user has never signed out of the local profile in the past. |
| 841 // 3) Valid cached credentials are available in the alternate profile. | 864 // 3) Valid cached credentials are available in the alternate profile. |
| 842 // 4) The user is not already in the process of configuring sync. | 865 // 4) The user is not already in the process of configuring sync. |
| 843 ProfileSyncService* service = | 866 ProfileSyncService* service = |
| 844 ProfileSyncServiceFactory::GetForProfile(profile_); | 867 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 845 return service->signin()->GetAuthenticatedUsername().empty() && | 868 return service->signin()->GetAuthenticatedUsername().empty() && |
| 846 !HasUserSignedOut() && | 869 !HasUserSignedOut() && |
| 847 !alternate_google_services_username.empty() && | 870 !alternate_google_services_username.empty() && |
| 848 !alternate_lsid.empty() && | 871 !alternate_lsid.empty() && |
| 849 !alternate_sid.empty() && | 872 !alternate_sid.empty() && |
| 850 !(alternate_encryption_bootstrap_token.empty() && | 873 !(alternate_encryption_bootstrap_token.empty() && |
| 851 alternate_keystore_encryption_bootstrap_token.empty()) && | 874 alternate_keystore_encryption_bootstrap_token.empty()) && |
| 852 !service->setup_in_progress(); | 875 !service->setup_in_progress(); |
| 853 } | 876 } |
| 854 | 877 |
| 855 } // namespace syncer | 878 } // namespace syncer |
| OLD | NEW |