| 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 "sync/internal_api/sync_manager_impl.h" | 5 #include "sync/internal_api/sync_manager_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 const base::FilePath& database_location, | 342 const base::FilePath& database_location, |
| 343 const WeakHandle<JsEventHandler>& event_handler, | 343 const WeakHandle<JsEventHandler>& event_handler, |
| 344 const std::string& sync_server_and_path, | 344 const std::string& sync_server_and_path, |
| 345 int port, | 345 int port, |
| 346 bool use_ssl, | 346 bool use_ssl, |
| 347 scoped_ptr<HttpPostProviderFactory> post_factory, | 347 scoped_ptr<HttpPostProviderFactory> post_factory, |
| 348 const std::vector<ModelSafeWorker*>& workers, | 348 const std::vector<ModelSafeWorker*>& workers, |
| 349 ExtensionsActivityMonitor* extensions_activity_monitor, | 349 ExtensionsActivityMonitor* extensions_activity_monitor, |
| 350 SyncManager::ChangeDelegate* change_delegate, | 350 SyncManager::ChangeDelegate* change_delegate, |
| 351 const SyncCredentials& credentials, | 351 const SyncCredentials& credentials, |
| 352 scoped_ptr<Invalidator> invalidator, | |
| 353 const std::string& invalidator_client_id, | 352 const std::string& invalidator_client_id, |
| 354 const std::string& restored_key_for_bootstrapping, | 353 const std::string& restored_key_for_bootstrapping, |
| 355 const std::string& restored_keystore_key_for_bootstrapping, | 354 const std::string& restored_keystore_key_for_bootstrapping, |
| 356 scoped_ptr<InternalComponentsFactory> internal_components_factory, | 355 scoped_ptr<InternalComponentsFactory> internal_components_factory, |
| 357 Encryptor* encryptor, | 356 Encryptor* encryptor, |
| 358 UnrecoverableErrorHandler* unrecoverable_error_handler, | 357 UnrecoverableErrorHandler* unrecoverable_error_handler, |
| 359 ReportUnrecoverableErrorFunction report_unrecoverable_error_function, | 358 ReportUnrecoverableErrorFunction report_unrecoverable_error_function, |
| 360 bool use_oauth2_token) { | 359 bool use_oauth2_token) { |
| 361 CHECK(!initialized_); | 360 CHECK(!initialized_); |
| 362 DCHECK(thread_checker_.CalledOnValidThread()); | 361 DCHECK(thread_checker_.CalledOnValidThread()); |
| 363 DCHECK(post_factory.get()); | 362 DCHECK(post_factory.get()); |
| 364 DCHECK(!credentials.email.empty()); | 363 DCHECK(!credentials.email.empty()); |
| 365 DCHECK(!credentials.sync_token.empty()); | 364 DCHECK(!credentials.sync_token.empty()); |
| 366 DVLOG(1) << "SyncManager starting Init..."; | 365 DVLOG(1) << "SyncManager starting Init..."; |
| 367 | 366 |
| 368 weak_handle_this_ = MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); | 367 weak_handle_this_ = MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); |
| 369 | 368 |
| 370 change_delegate_ = change_delegate; | 369 change_delegate_ = change_delegate; |
| 371 | 370 |
| 372 invalidator_ = invalidator.Pass(); | |
| 373 invalidator_->RegisterHandler(this); | |
| 374 | |
| 375 AddObserver(&js_sync_manager_observer_); | 371 AddObserver(&js_sync_manager_observer_); |
| 376 SetJsEventHandler(event_handler); | 372 SetJsEventHandler(event_handler); |
| 377 | 373 |
| 378 AddObserver(&debug_info_event_listener_); | 374 AddObserver(&debug_info_event_listener_); |
| 379 | 375 |
| 380 database_path_ = database_location.Append( | 376 database_path_ = database_location.Append( |
| 381 syncable::Directory::kSyncDatabaseFilename); | 377 syncable::Directory::kSyncDatabaseFilename); |
| 382 encryptor_ = encryptor; | 378 encryptor_ = encryptor; |
| 383 unrecoverable_error_handler_ = unrecoverable_error_handler; | 379 unrecoverable_error_handler_ = unrecoverable_error_handler; |
| 384 report_unrecoverable_error_function_ = report_unrecoverable_error_function; | 380 report_unrecoverable_error_function_ = report_unrecoverable_error_function; |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { | 598 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { |
| 603 DCHECK(thread_checker_.CalledOnValidThread()); | 599 DCHECK(thread_checker_.CalledOnValidThread()); |
| 604 DCHECK(initialized_); | 600 DCHECK(initialized_); |
| 605 DCHECK(!credentials.email.empty()); | 601 DCHECK(!credentials.email.empty()); |
| 606 DCHECK(!credentials.sync_token.empty()); | 602 DCHECK(!credentials.sync_token.empty()); |
| 607 | 603 |
| 608 observing_network_connectivity_changes_ = true; | 604 observing_network_connectivity_changes_ = true; |
| 609 if (!connection_manager_->SetAuthToken(credentials.sync_token)) | 605 if (!connection_manager_->SetAuthToken(credentials.sync_token)) |
| 610 return; // Auth token is known to be invalid, so exit early. | 606 return; // Auth token is known to be invalid, so exit early. |
| 611 | 607 |
| 612 invalidator_->UpdateCredentials(credentials.email, credentials.sync_token); | |
| 613 scheduler_->OnCredentialsUpdated(); | 608 scheduler_->OnCredentialsUpdated(); |
| 614 | 609 |
| 615 // TODO(zea): pass the credential age to the debug info event listener. | 610 // TODO(zea): pass the credential age to the debug info event listener. |
| 616 } | 611 } |
| 617 | 612 |
| 618 void SyncManagerImpl::UpdateEnabledTypes(ModelTypeSet enabled_types) { | |
| 619 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 620 DCHECK(initialized_); | |
| 621 invalidator_->UpdateRegisteredIds( | |
| 622 this, | |
| 623 ModelTypeSetToObjectIdSet(enabled_types)); | |
| 624 } | |
| 625 | |
| 626 void SyncManagerImpl::RegisterInvalidationHandler( | |
| 627 InvalidationHandler* handler) { | |
| 628 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 629 DCHECK(initialized_); | |
| 630 invalidator_->RegisterHandler(handler); | |
| 631 } | |
| 632 | |
| 633 void SyncManagerImpl::UpdateRegisteredInvalidationIds( | |
| 634 InvalidationHandler* handler, | |
| 635 const ObjectIdSet& ids) { | |
| 636 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 637 DCHECK(initialized_); | |
| 638 invalidator_->UpdateRegisteredIds(handler, ids); | |
| 639 } | |
| 640 | |
| 641 void SyncManagerImpl::UnregisterInvalidationHandler( | |
| 642 InvalidationHandler* handler) { | |
| 643 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 644 DCHECK(initialized_); | |
| 645 invalidator_->UnregisterHandler(handler); | |
| 646 } | |
| 647 | |
| 648 void SyncManagerImpl::AcknowledgeInvalidation( | |
| 649 const invalidation::ObjectId& id, const syncer::AckHandle& ack_handle) { | |
| 650 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 651 DCHECK(initialized_); | |
| 652 invalidator_->Acknowledge(id, ack_handle); | |
| 653 } | |
| 654 | |
| 655 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { | 613 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { |
| 656 DCHECK(thread_checker_.CalledOnValidThread()); | 614 DCHECK(thread_checker_.CalledOnValidThread()); |
| 657 observers_.AddObserver(observer); | 615 observers_.AddObserver(observer); |
| 658 } | 616 } |
| 659 | 617 |
| 660 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { | 618 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { |
| 661 DCHECK(thread_checker_.CalledOnValidThread()); | 619 DCHECK(thread_checker_.CalledOnValidThread()); |
| 662 observers_.RemoveObserver(observer); | 620 observers_.RemoveObserver(observer); |
| 663 } | 621 } |
| 664 | 622 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 683 if (sync_encryption_handler_) { | 641 if (sync_encryption_handler_) { |
| 684 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); | 642 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); |
| 685 sync_encryption_handler_->RemoveObserver(this); | 643 sync_encryption_handler_->RemoveObserver(this); |
| 686 } | 644 } |
| 687 | 645 |
| 688 SetJsEventHandler(WeakHandle<JsEventHandler>()); | 646 SetJsEventHandler(WeakHandle<JsEventHandler>()); |
| 689 RemoveObserver(&js_sync_manager_observer_); | 647 RemoveObserver(&js_sync_manager_observer_); |
| 690 | 648 |
| 691 RemoveObserver(&debug_info_event_listener_); | 649 RemoveObserver(&debug_info_event_listener_); |
| 692 | 650 |
| 693 // |invalidator_| and |connection_manager_| may end up being NULL here in | 651 // |connection_manager_| may end up being NULL here in tests (in synchronous |
| 694 // tests (in synchronous initialization mode). | 652 // initialization mode). |
| 695 // | 653 // |
| 696 // TODO(akalin): Fix this behavior. | 654 // TODO(akalin): Fix this behavior. |
| 697 | |
| 698 if (invalidator_) | |
| 699 invalidator_->UnregisterHandler(this); | |
| 700 invalidator_.reset(); | |
| 701 | |
| 702 if (connection_manager_) | 655 if (connection_manager_) |
| 703 connection_manager_->RemoveListener(this); | 656 connection_manager_->RemoveListener(this); |
| 704 connection_manager_.reset(); | 657 connection_manager_.reset(); |
| 705 | 658 |
| 706 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 659 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 707 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 660 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 708 observing_network_connectivity_changes_ = false; | 661 observing_network_connectivity_changes_ = false; |
| 709 | 662 |
| 710 if (initialized_ && directory()) { | 663 if (initialized_ && directory()) { |
| 711 directory()->SaveChanges(); | 664 directory()->SaveChanges(); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { | 934 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { |
| 982 if (!initialized_) { | 935 if (!initialized_) { |
| 983 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " | 936 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " |
| 984 << "initialized"; | 937 << "initialized"; |
| 985 return; | 938 return; |
| 986 } | 939 } |
| 987 | 940 |
| 988 DVLOG(1) << "Sending OnSyncCycleCompleted"; | 941 DVLOG(1) << "Sending OnSyncCycleCompleted"; |
| 989 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 942 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 990 OnSyncCycleCompleted(event.snapshot)); | 943 OnSyncCycleCompleted(event.snapshot)); |
| 991 | |
| 992 // This is here for tests, which are still using p2p notifications. | |
| 993 bool is_notifiable_commit = | |
| 994 (event.snapshot.model_neutral_state().num_successful_commits > 0); | |
| 995 if (is_notifiable_commit) { | |
| 996 if (invalidator_) { | |
| 997 const ObjectIdInvalidationMap& invalidation_map = | |
| 998 ModelTypeInvalidationMapToObjectIdInvalidationMap( | |
| 999 event.snapshot.source().types); | |
| 1000 invalidator_->SendInvalidation(invalidation_map); | |
| 1001 } else { | |
| 1002 DVLOG(1) << "Not sending invalidation: invalidator_ is NULL"; | |
| 1003 } | |
| 1004 } | |
| 1005 } | 944 } |
| 1006 | 945 |
| 1007 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { | 946 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { |
| 1008 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 947 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1009 OnStopSyncingPermanently()); | 948 OnStopSyncingPermanently()); |
| 1010 return; | 949 return; |
| 1011 } | 950 } |
| 1012 | 951 |
| 1013 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { | 952 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { |
| 1014 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 953 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1217 const ModelTypeInvalidationMap& invalidation_map) { | 1156 const ModelTypeInvalidationMap& invalidation_map) { |
| 1218 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); | 1157 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); |
| 1219 it != invalidation_map.end(); ++it) { | 1158 it != invalidation_map.end(); ++it) { |
| 1220 NotificationInfo* info = ¬ification_info_map_[it->first]; | 1159 NotificationInfo* info = ¬ification_info_map_[it->first]; |
| 1221 info->total_count++; | 1160 info->total_count++; |
| 1222 info->payload = it->second.payload; | 1161 info->payload = it->second.payload; |
| 1223 } | 1162 } |
| 1224 } | 1163 } |
| 1225 | 1164 |
| 1226 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { | 1165 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { |
| 1166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1167 |
| 1227 const std::string& state_str = InvalidatorStateToString(state); | 1168 const std::string& state_str = InvalidatorStateToString(state); |
| 1228 invalidator_state_ = state; | 1169 invalidator_state_ = state; |
| 1229 DVLOG(1) << "Invalidator state changed to: " << state_str; | 1170 DVLOG(1) << "Invalidator state changed to: " << state_str; |
| 1230 const bool notifications_enabled = | 1171 const bool notifications_enabled = |
| 1231 (invalidator_state_ == INVALIDATIONS_ENABLED); | 1172 (invalidator_state_ == INVALIDATIONS_ENABLED); |
| 1232 allstatus_.SetNotificationsEnabled(notifications_enabled); | 1173 allstatus_.SetNotificationsEnabled(notifications_enabled); |
| 1233 scheduler_->SetNotificationsEnabled(notifications_enabled); | 1174 scheduler_->SetNotificationsEnabled(notifications_enabled); |
| 1234 | 1175 |
| 1235 if (invalidator_state_ == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | |
| 1236 // If the invalidator's credentials were rejected, that means that | |
| 1237 // our sync credentials are also bad, so invalidate those. | |
| 1238 connection_manager_->OnInvalidationCredentialsRejected(); | |
| 1239 } | |
| 1240 | |
| 1241 if (js_event_handler_.IsInitialized()) { | 1176 if (js_event_handler_.IsInitialized()) { |
| 1242 base::DictionaryValue details; | 1177 base::DictionaryValue details; |
| 1243 details.SetString("state", state_str); | 1178 details.SetString("state", state_str); |
| 1244 js_event_handler_.Call(FROM_HERE, | 1179 js_event_handler_.Call(FROM_HERE, |
| 1245 &JsEventHandler::HandleJsEvent, | 1180 &JsEventHandler::HandleJsEvent, |
| 1246 "onNotificationStateChange", | 1181 "onNotificationStateChange", |
| 1247 JsEventDetails(&details)); | 1182 JsEventDetails(&details)); |
| 1248 } | 1183 } |
| 1249 } | 1184 } |
| 1250 | 1185 |
| 1251 void SyncManagerImpl::OnIncomingInvalidation( | 1186 void SyncManagerImpl::OnIncomingInvalidation( |
| 1252 const ObjectIdInvalidationMap& invalidation_map) { | 1187 const ObjectIdInvalidationMap& invalidation_map) { |
| 1253 DCHECK(thread_checker_.CalledOnValidThread()); | 1188 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1254 | 1189 |
| 1255 // TODO(dcheng): Acknowledge immediately for now. Fix this once the | |
| 1256 // invalidator doesn't repeatedly ping for unacknowledged invaliations, since | |
| 1257 // it conflicts with the sync scheduler's internal backoff algorithm. | |
| 1258 // See http://crbug.com/124149 for more information. | |
| 1259 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin(); | |
| 1260 it != invalidation_map.end(); ++it) { | |
| 1261 invalidator_->Acknowledge(it->first, it->second.ack_handle); | |
| 1262 } | |
| 1263 | |
| 1264 const ModelTypeInvalidationMap& type_invalidation_map = | 1190 const ModelTypeInvalidationMap& type_invalidation_map = |
| 1265 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); | 1191 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); |
| 1266 if (type_invalidation_map.empty()) { | 1192 if (type_invalidation_map.empty()) { |
| 1267 LOG(WARNING) << "Sync received invalidation without any type information."; | 1193 LOG(WARNING) << "Sync received invalidation without any type information."; |
| 1268 } else { | 1194 } else { |
| 1269 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); | 1195 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); |
| 1270 scheduler_->ScheduleInvalidationNudge( | 1196 scheduler_->ScheduleInvalidationNudge( |
| 1271 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), | 1197 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), |
| 1272 type_invalidation_map, FROM_HERE); | 1198 type_invalidation_map, FROM_HERE); |
| 1273 allstatus_.IncrementNotificationsReceived(); | 1199 allstatus_.IncrementNotificationsReceived(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1396 int SyncManagerImpl::GetDefaultNudgeDelay() { | 1322 int SyncManagerImpl::GetDefaultNudgeDelay() { |
| 1397 return kDefaultNudgeDelayMilliseconds; | 1323 return kDefaultNudgeDelayMilliseconds; |
| 1398 } | 1324 } |
| 1399 | 1325 |
| 1400 // static. | 1326 // static. |
| 1401 int SyncManagerImpl::GetPreferencesNudgeDelay() { | 1327 int SyncManagerImpl::GetPreferencesNudgeDelay() { |
| 1402 return kPreferencesNudgeDelayMilliseconds; | 1328 return kPreferencesNudgeDelayMilliseconds; |
| 1403 } | 1329 } |
| 1404 | 1330 |
| 1405 } // namespace syncer | 1331 } // namespace syncer |
| OLD | NEW |