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