| 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 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 const base::FilePath& database_location, | 355 const base::FilePath& database_location, |
| 356 const WeakHandle<JsEventHandler>& event_handler, | 356 const WeakHandle<JsEventHandler>& event_handler, |
| 357 const std::string& sync_server_and_path, | 357 const std::string& sync_server_and_path, |
| 358 int port, | 358 int port, |
| 359 bool use_ssl, | 359 bool use_ssl, |
| 360 scoped_ptr<HttpPostProviderFactory> post_factory, | 360 scoped_ptr<HttpPostProviderFactory> post_factory, |
| 361 const std::vector<ModelSafeWorker*>& workers, | 361 const std::vector<ModelSafeWorker*>& workers, |
| 362 ExtensionsActivityMonitor* extensions_activity_monitor, | 362 ExtensionsActivityMonitor* extensions_activity_monitor, |
| 363 SyncManager::ChangeDelegate* change_delegate, | 363 SyncManager::ChangeDelegate* change_delegate, |
| 364 const SyncCredentials& credentials, | 364 const SyncCredentials& credentials, |
| 365 scoped_ptr<Invalidator> invalidator, | |
| 366 const std::string& invalidator_client_id, | 365 const std::string& invalidator_client_id, |
| 367 const std::string& restored_key_for_bootstrapping, | 366 const std::string& restored_key_for_bootstrapping, |
| 368 const std::string& restored_keystore_key_for_bootstrapping, | 367 const std::string& restored_keystore_key_for_bootstrapping, |
| 369 scoped_ptr<InternalComponentsFactory> internal_components_factory, | 368 scoped_ptr<InternalComponentsFactory> internal_components_factory, |
| 370 Encryptor* encryptor, | 369 Encryptor* encryptor, |
| 371 UnrecoverableErrorHandler* unrecoverable_error_handler, | 370 UnrecoverableErrorHandler* unrecoverable_error_handler, |
| 372 ReportUnrecoverableErrorFunction report_unrecoverable_error_function, | 371 ReportUnrecoverableErrorFunction report_unrecoverable_error_function, |
| 373 bool use_oauth2_token) { | 372 bool use_oauth2_token) { |
| 374 CHECK(!initialized_); | 373 CHECK(!initialized_); |
| 375 DCHECK(thread_checker_.CalledOnValidThread()); | 374 DCHECK(thread_checker_.CalledOnValidThread()); |
| 376 DCHECK(post_factory.get()); | 375 DCHECK(post_factory.get()); |
| 377 DCHECK(!credentials.email.empty()); | 376 DCHECK(!credentials.email.empty()); |
| 378 DCHECK(!credentials.sync_token.empty()); | 377 DCHECK(!credentials.sync_token.empty()); |
| 379 DVLOG(1) << "SyncManager starting Init..."; | 378 DVLOG(1) << "SyncManager starting Init..."; |
| 380 | 379 |
| 381 weak_handle_this_ = MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); | 380 weak_handle_this_ = MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()); |
| 382 | 381 |
| 383 change_delegate_ = change_delegate; | 382 change_delegate_ = change_delegate; |
| 384 | 383 |
| 385 invalidator_ = invalidator.Pass(); | |
| 386 invalidator_->RegisterHandler(this); | |
| 387 | |
| 388 AddObserver(&js_sync_manager_observer_); | 384 AddObserver(&js_sync_manager_observer_); |
| 389 SetJsEventHandler(event_handler); | 385 SetJsEventHandler(event_handler); |
| 390 | 386 |
| 391 AddObserver(&debug_info_event_listener_); | 387 AddObserver(&debug_info_event_listener_); |
| 392 | 388 |
| 393 database_path_ = database_location.Append( | 389 database_path_ = database_location.Append( |
| 394 syncable::Directory::kSyncDatabaseFilename); | 390 syncable::Directory::kSyncDatabaseFilename); |
| 395 encryptor_ = encryptor; | 391 encryptor_ = encryptor; |
| 396 unrecoverable_error_handler_ = unrecoverable_error_handler; | 392 unrecoverable_error_handler_ = unrecoverable_error_handler; |
| 397 report_unrecoverable_error_function_ = report_unrecoverable_error_function; | 393 report_unrecoverable_error_function_ = report_unrecoverable_error_function; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { | 618 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { |
| 623 DCHECK(thread_checker_.CalledOnValidThread()); | 619 DCHECK(thread_checker_.CalledOnValidThread()); |
| 624 DCHECK(initialized_); | 620 DCHECK(initialized_); |
| 625 DCHECK(!credentials.email.empty()); | 621 DCHECK(!credentials.email.empty()); |
| 626 DCHECK(!credentials.sync_token.empty()); | 622 DCHECK(!credentials.sync_token.empty()); |
| 627 | 623 |
| 628 observing_network_connectivity_changes_ = true; | 624 observing_network_connectivity_changes_ = true; |
| 629 if (!connection_manager_->SetAuthToken(credentials.sync_token)) | 625 if (!connection_manager_->SetAuthToken(credentials.sync_token)) |
| 630 return; // Auth token is known to be invalid, so exit early. | 626 return; // Auth token is known to be invalid, so exit early. |
| 631 | 627 |
| 632 invalidator_->UpdateCredentials(credentials.email, credentials.sync_token); | |
| 633 scheduler_->OnCredentialsUpdated(); | 628 scheduler_->OnCredentialsUpdated(); |
| 634 | 629 |
| 635 // TODO(zea): pass the credential age to the debug info event listener. | 630 // TODO(zea): pass the credential age to the debug info event listener. |
| 636 } | 631 } |
| 637 | 632 |
| 638 void SyncManagerImpl::UpdateEnabledTypes(ModelTypeSet enabled_types) { | |
| 639 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 640 DCHECK(initialized_); | |
| 641 invalidator_->UpdateRegisteredIds( | |
| 642 this, | |
| 643 ModelTypeSetToObjectIdSet(enabled_types)); | |
| 644 } | |
| 645 | |
| 646 void SyncManagerImpl::RegisterInvalidationHandler( | |
| 647 InvalidationHandler* handler) { | |
| 648 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 649 DCHECK(initialized_); | |
| 650 invalidator_->RegisterHandler(handler); | |
| 651 } | |
| 652 | |
| 653 void SyncManagerImpl::UpdateRegisteredInvalidationIds( | |
| 654 InvalidationHandler* handler, | |
| 655 const ObjectIdSet& ids) { | |
| 656 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 657 DCHECK(initialized_); | |
| 658 invalidator_->UpdateRegisteredIds(handler, ids); | |
| 659 } | |
| 660 | |
| 661 void SyncManagerImpl::UnregisterInvalidationHandler( | |
| 662 InvalidationHandler* handler) { | |
| 663 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 664 DCHECK(initialized_); | |
| 665 invalidator_->UnregisterHandler(handler); | |
| 666 } | |
| 667 | |
| 668 void SyncManagerImpl::AcknowledgeInvalidation( | |
| 669 const invalidation::ObjectId& id, const syncer::AckHandle& ack_handle) { | |
| 670 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 671 DCHECK(initialized_); | |
| 672 invalidator_->Acknowledge(id, ack_handle); | |
| 673 } | |
| 674 | |
| 675 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { | 633 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { |
| 676 DCHECK(thread_checker_.CalledOnValidThread()); | 634 DCHECK(thread_checker_.CalledOnValidThread()); |
| 677 observers_.AddObserver(observer); | 635 observers_.AddObserver(observer); |
| 678 } | 636 } |
| 679 | 637 |
| 680 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { | 638 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { |
| 681 DCHECK(thread_checker_.CalledOnValidThread()); | 639 DCHECK(thread_checker_.CalledOnValidThread()); |
| 682 observers_.RemoveObserver(observer); | 640 observers_.RemoveObserver(observer); |
| 683 } | 641 } |
| 684 | 642 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 703 if (sync_encryption_handler_) { | 661 if (sync_encryption_handler_) { |
| 704 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); | 662 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); |
| 705 sync_encryption_handler_->RemoveObserver(this); | 663 sync_encryption_handler_->RemoveObserver(this); |
| 706 } | 664 } |
| 707 | 665 |
| 708 SetJsEventHandler(WeakHandle<JsEventHandler>()); | 666 SetJsEventHandler(WeakHandle<JsEventHandler>()); |
| 709 RemoveObserver(&js_sync_manager_observer_); | 667 RemoveObserver(&js_sync_manager_observer_); |
| 710 | 668 |
| 711 RemoveObserver(&debug_info_event_listener_); | 669 RemoveObserver(&debug_info_event_listener_); |
| 712 | 670 |
| 713 // |invalidator_| and |connection_manager_| may end up being NULL here in | 671 // |connection_manager_| may end up being NULL here in tests (in synchronous |
| 714 // tests (in synchronous initialization mode). | 672 // initialization mode). |
| 715 // | 673 // |
| 716 // TODO(akalin): Fix this behavior. | 674 // TODO(akalin): Fix this behavior. |
| 717 | |
| 718 if (invalidator_) | |
| 719 invalidator_->UnregisterHandler(this); | |
| 720 invalidator_.reset(); | |
| 721 | |
| 722 if (connection_manager_) | 675 if (connection_manager_) |
| 723 connection_manager_->RemoveListener(this); | 676 connection_manager_->RemoveListener(this); |
| 724 connection_manager_.reset(); | 677 connection_manager_.reset(); |
| 725 | 678 |
| 726 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 679 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 727 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 680 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 728 observing_network_connectivity_changes_ = false; | 681 observing_network_connectivity_changes_ = false; |
| 729 | 682 |
| 730 if (initialized_ && directory()) { | 683 if (initialized_ && directory()) { |
| 731 directory()->SaveChanges(); | 684 directory()->SaveChanges(); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { | 955 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { |
| 1003 if (!initialized_) { | 956 if (!initialized_) { |
| 1004 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " | 957 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " |
| 1005 << "initialized"; | 958 << "initialized"; |
| 1006 return; | 959 return; |
| 1007 } | 960 } |
| 1008 | 961 |
| 1009 DVLOG(1) << "Sending OnSyncCycleCompleted"; | 962 DVLOG(1) << "Sending OnSyncCycleCompleted"; |
| 1010 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 963 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1011 OnSyncCycleCompleted(event.snapshot)); | 964 OnSyncCycleCompleted(event.snapshot)); |
| 1012 | |
| 1013 // This is here for tests, which are still using p2p notifications. | |
| 1014 bool is_notifiable_commit = | |
| 1015 (event.snapshot.model_neutral_state().num_successful_commits > 0); | |
| 1016 if (is_notifiable_commit) { | |
| 1017 if (invalidator_) { | |
| 1018 const ObjectIdInvalidationMap& invalidation_map = | |
| 1019 ModelTypeInvalidationMapToObjectIdInvalidationMap( | |
| 1020 event.snapshot.source().types); | |
| 1021 invalidator_->SendInvalidation(invalidation_map); | |
| 1022 } else { | |
| 1023 DVLOG(1) << "Not sending invalidation: invalidator_ is NULL"; | |
| 1024 } | |
| 1025 } | |
| 1026 } | 965 } |
| 1027 | 966 |
| 1028 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { | 967 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { |
| 1029 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 968 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1030 OnStopSyncingPermanently()); | 969 OnStopSyncingPermanently()); |
| 1031 return; | 970 return; |
| 1032 } | 971 } |
| 1033 | 972 |
| 1034 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { | 973 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { |
| 1035 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 974 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1238 const ModelTypeInvalidationMap& invalidation_map) { | 1177 const ModelTypeInvalidationMap& invalidation_map) { |
| 1239 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); | 1178 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); |
| 1240 it != invalidation_map.end(); ++it) { | 1179 it != invalidation_map.end(); ++it) { |
| 1241 NotificationInfo* info = ¬ification_info_map_[it->first]; | 1180 NotificationInfo* info = ¬ification_info_map_[it->first]; |
| 1242 info->total_count++; | 1181 info->total_count++; |
| 1243 info->payload = it->second.payload; | 1182 info->payload = it->second.payload; |
| 1244 } | 1183 } |
| 1245 } | 1184 } |
| 1246 | 1185 |
| 1247 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { | 1186 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { |
| 1187 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1188 |
| 1248 const std::string& state_str = InvalidatorStateToString(state); | 1189 const std::string& state_str = InvalidatorStateToString(state); |
| 1249 invalidator_state_ = state; | 1190 invalidator_state_ = state; |
| 1250 DVLOG(1) << "Invalidator state changed to: " << state_str; | 1191 DVLOG(1) << "Invalidator state changed to: " << state_str; |
| 1251 const bool notifications_enabled = | 1192 const bool notifications_enabled = |
| 1252 (invalidator_state_ == INVALIDATIONS_ENABLED); | 1193 (invalidator_state_ == INVALIDATIONS_ENABLED); |
| 1253 allstatus_.SetNotificationsEnabled(notifications_enabled); | 1194 allstatus_.SetNotificationsEnabled(notifications_enabled); |
| 1254 scheduler_->SetNotificationsEnabled(notifications_enabled); | 1195 scheduler_->SetNotificationsEnabled(notifications_enabled); |
| 1255 | 1196 |
| 1256 if (invalidator_state_ == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 1197 if (invalidator_state_ == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
| 1257 // If the invalidator's credentials were rejected, that means that | 1198 // If the invalidator's credentials were rejected, that means that |
| 1258 // our sync credentials are also bad, so invalidate those. | 1199 // our sync credentials are also bad, so invalidate those. |
| 1259 connection_manager_->OnInvalidationCredentialsRejected(); | 1200 connection_manager_->OnInvalidationCredentialsRejected(); |
| 1260 } | 1201 } |
| 1261 | 1202 |
| 1262 if (js_event_handler_.IsInitialized()) { | 1203 if (js_event_handler_.IsInitialized()) { |
| 1263 base::DictionaryValue details; | 1204 base::DictionaryValue details; |
| 1264 details.SetString("state", state_str); | 1205 details.SetString("state", state_str); |
| 1265 js_event_handler_.Call(FROM_HERE, | 1206 js_event_handler_.Call(FROM_HERE, |
| 1266 &JsEventHandler::HandleJsEvent, | 1207 &JsEventHandler::HandleJsEvent, |
| 1267 "onNotificationStateChange", | 1208 "onNotificationStateChange", |
| 1268 JsEventDetails(&details)); | 1209 JsEventDetails(&details)); |
| 1269 } | 1210 } |
| 1270 } | 1211 } |
| 1271 | 1212 |
| 1272 void SyncManagerImpl::OnIncomingInvalidation( | 1213 void SyncManagerImpl::OnIncomingInvalidation( |
| 1273 const ObjectIdInvalidationMap& invalidation_map) { | 1214 const ObjectIdInvalidationMap& invalidation_map) { |
| 1274 DCHECK(thread_checker_.CalledOnValidThread()); | 1215 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1275 | 1216 |
| 1276 // TODO(dcheng): Acknowledge immediately for now. Fix this once the | |
| 1277 // invalidator doesn't repeatedly ping for unacknowledged invaliations, since | |
| 1278 // it conflicts with the sync scheduler's internal backoff algorithm. | |
| 1279 // See http://crbug.com/124149 for more information. | |
| 1280 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin(); | |
| 1281 it != invalidation_map.end(); ++it) { | |
| 1282 invalidator_->Acknowledge(it->first, it->second.ack_handle); | |
| 1283 } | |
| 1284 | |
| 1285 const ModelTypeInvalidationMap& type_invalidation_map = | 1217 const ModelTypeInvalidationMap& type_invalidation_map = |
| 1286 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); | 1218 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); |
| 1287 if (type_invalidation_map.empty()) { | 1219 if (type_invalidation_map.empty()) { |
| 1288 LOG(WARNING) << "Sync received invalidation without any type information."; | 1220 LOG(WARNING) << "Sync received invalidation without any type information."; |
| 1289 } else { | 1221 } else { |
| 1290 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); | 1222 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); |
| 1291 scheduler_->ScheduleInvalidationNudge( | 1223 scheduler_->ScheduleInvalidationNudge( |
| 1292 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), | 1224 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), |
| 1293 type_invalidation_map, FROM_HERE); | 1225 type_invalidation_map, FROM_HERE); |
| 1294 allstatus_.IncrementNotificationsReceived(); | 1226 allstatus_.IncrementNotificationsReceived(); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1417 int SyncManagerImpl::GetDefaultNudgeDelay() { | 1349 int SyncManagerImpl::GetDefaultNudgeDelay() { |
| 1418 return kDefaultNudgeDelayMilliseconds; | 1350 return kDefaultNudgeDelayMilliseconds; |
| 1419 } | 1351 } |
| 1420 | 1352 |
| 1421 // static. | 1353 // static. |
| 1422 int SyncManagerImpl::GetPreferencesNudgeDelay() { | 1354 int SyncManagerImpl::GetPreferencesNudgeDelay() { |
| 1423 return kPreferencesNudgeDelayMilliseconds; | 1355 return kPreferencesNudgeDelayMilliseconds; |
| 1424 } | 1356 } |
| 1425 | 1357 |
| 1426 } // namespace syncer | 1358 } // namespace syncer |
| OLD | NEW |