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 "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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 593 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { | 589 void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) { |
| 594 DCHECK(thread_checker_.CalledOnValidThread()); | 590 DCHECK(thread_checker_.CalledOnValidThread()); |
| 595 DCHECK(initialized_); | 591 DCHECK(initialized_); |
| 596 DCHECK(!credentials.email.empty()); | 592 DCHECK(!credentials.email.empty()); |
| 597 DCHECK(!credentials.sync_token.empty()); | 593 DCHECK(!credentials.sync_token.empty()); |
| 598 | 594 |
| 599 observing_network_connectivity_changes_ = true; | 595 observing_network_connectivity_changes_ = true; |
| 600 if (!connection_manager_->set_auth_token(credentials.sync_token)) | 596 if (!connection_manager_->set_auth_token(credentials.sync_token)) |
| 601 return; // Auth token is known to be invalid, so exit early. | 597 return; // Auth token is known to be invalid, so exit early. |
| 602 | 598 |
| 603 invalidator_->UpdateCredentials(credentials.email, credentials.sync_token); | |
| 604 scheduler_->OnCredentialsUpdated(); | 599 scheduler_->OnCredentialsUpdated(); |
| 605 } | 600 } |
| 606 | 601 |
| 607 void SyncManagerImpl::UpdateEnabledTypes(ModelTypeSet enabled_types) { | |
| 608 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 609 DCHECK(initialized_); | |
| 610 invalidator_->UpdateRegisteredIds( | |
| 611 this, | |
| 612 ModelTypeSetToObjectIdSet(enabled_types)); | |
| 613 } | |
| 614 | |
| 615 void SyncManagerImpl::RegisterInvalidationHandler( | |
| 616 InvalidationHandler* handler) { | |
| 617 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 618 DCHECK(initialized_); | |
| 619 invalidator_->RegisterHandler(handler); | |
| 620 } | |
| 621 | |
| 622 void SyncManagerImpl::UpdateRegisteredInvalidationIds( | |
| 623 InvalidationHandler* handler, | |
| 624 const ObjectIdSet& ids) { | |
| 625 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 626 DCHECK(initialized_); | |
| 627 invalidator_->UpdateRegisteredIds(handler, ids); | |
| 628 } | |
| 629 | |
| 630 void SyncManagerImpl::UnregisterInvalidationHandler( | |
| 631 InvalidationHandler* handler) { | |
| 632 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 633 DCHECK(initialized_); | |
| 634 invalidator_->UnregisterHandler(handler); | |
| 635 } | |
| 636 | |
| 637 void SyncManagerImpl::AcknowledgeInvalidation( | |
| 638 const invalidation::ObjectId& id, const syncer::AckHandle& ack_handle) { | |
| 639 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 640 DCHECK(initialized_); | |
| 641 invalidator_->Acknowledge(id, ack_handle); | |
| 642 } | |
| 643 | |
| 644 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { | 602 void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) { |
| 645 DCHECK(thread_checker_.CalledOnValidThread()); | 603 DCHECK(thread_checker_.CalledOnValidThread()); |
| 646 observers_.AddObserver(observer); | 604 observers_.AddObserver(observer); |
| 647 } | 605 } |
| 648 | 606 |
| 649 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { | 607 void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) { |
| 650 DCHECK(thread_checker_.CalledOnValidThread()); | 608 DCHECK(thread_checker_.CalledOnValidThread()); |
| 651 observers_.RemoveObserver(observer); | 609 observers_.RemoveObserver(observer); |
| 652 } | 610 } |
| 653 | 611 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 672 if (sync_encryption_handler_.get()) { | 630 if (sync_encryption_handler_.get()) { |
| 673 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); | 631 sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_); |
| 674 sync_encryption_handler_->RemoveObserver(this); | 632 sync_encryption_handler_->RemoveObserver(this); |
| 675 } | 633 } |
| 676 | 634 |
| 677 SetJsEventHandler(WeakHandle<JsEventHandler>()); | 635 SetJsEventHandler(WeakHandle<JsEventHandler>()); |
| 678 RemoveObserver(&js_sync_manager_observer_); | 636 RemoveObserver(&js_sync_manager_observer_); |
| 679 | 637 |
| 680 RemoveObserver(&debug_info_event_listener_); | 638 RemoveObserver(&debug_info_event_listener_); |
| 681 | 639 |
| 682 // |invalidator_| and |connection_manager_| may end up being NULL here in | 640 // |connection_manager_| may end up being NULL here in tests (in synchronous |
| 683 // tests (in synchronous initialization mode). | 641 // initialization mode). |
| 684 // | 642 // |
| 685 // TODO(akalin): Fix this behavior. | 643 // TODO(akalin): Fix this behavior. |
|
tim (not reviewing)
2013/04/15 16:48:24
This TODO probably doesn't belong here anymore.
rlarocque
2013/04/22 21:47:15
I think it does. I left it there because I figure
| |
| 686 | |
| 687 if (invalidator_.get()) | |
| 688 invalidator_->UnregisterHandler(this); | |
| 689 invalidator_.reset(); | |
| 690 | |
| 691 if (connection_manager_.get()) | 644 if (connection_manager_.get()) |
| 692 connection_manager_->RemoveListener(this); | 645 connection_manager_->RemoveListener(this); |
| 693 connection_manager_.reset(); | 646 connection_manager_.reset(); |
| 694 | 647 |
| 695 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 648 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 696 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 649 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 697 observing_network_connectivity_changes_ = false; | 650 observing_network_connectivity_changes_ = false; |
| 698 | 651 |
| 699 if (initialized_ && directory()) { | 652 if (initialized_ && directory()) { |
| 700 directory()->SaveChanges(); | 653 directory()->SaveChanges(); |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 972 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { | 925 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { |
| 973 if (!initialized_) { | 926 if (!initialized_) { |
| 974 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " | 927 LOG(INFO) << "OnSyncCycleCompleted not sent because sync api is not " |
| 975 << "initialized"; | 928 << "initialized"; |
| 976 return; | 929 return; |
| 977 } | 930 } |
| 978 | 931 |
| 979 DVLOG(1) << "Sending OnSyncCycleCompleted"; | 932 DVLOG(1) << "Sending OnSyncCycleCompleted"; |
| 980 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 933 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 981 OnSyncCycleCompleted(event.snapshot)); | 934 OnSyncCycleCompleted(event.snapshot)); |
| 982 | |
| 983 // This is here for tests, which are still using p2p notifications. | |
| 984 bool is_notifiable_commit = | |
| 985 (event.snapshot.model_neutral_state().num_successful_commits > 0); | |
| 986 if (is_notifiable_commit) { | |
| 987 if (invalidator_.get()) { | |
| 988 const ObjectIdInvalidationMap& invalidation_map = | |
| 989 ModelTypeInvalidationMapToObjectIdInvalidationMap( | |
| 990 event.snapshot.source().types); | |
| 991 invalidator_->SendInvalidation(invalidation_map); | |
| 992 } else { | |
| 993 DVLOG(1) << "Not sending invalidation: invalidator_ is NULL"; | |
| 994 } | |
| 995 } | |
| 996 } | 935 } |
| 997 | 936 |
| 998 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { | 937 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { |
| 999 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 938 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1000 OnStopSyncingPermanently()); | 939 OnStopSyncingPermanently()); |
| 1001 return; | 940 return; |
| 1002 } | 941 } |
| 1003 | 942 |
| 1004 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { | 943 if (event.what_happened == SyncEngineEvent::UPDATED_TOKEN) { |
| 1005 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 944 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1207 void SyncManagerImpl::UpdateNotificationInfo( | 1146 void SyncManagerImpl::UpdateNotificationInfo( |
| 1208 const ModelTypeInvalidationMap& invalidation_map) { | 1147 const ModelTypeInvalidationMap& invalidation_map) { |
| 1209 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); | 1148 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin(); |
| 1210 it != invalidation_map.end(); ++it) { | 1149 it != invalidation_map.end(); ++it) { |
| 1211 NotificationInfo* info = ¬ification_info_map_[it->first]; | 1150 NotificationInfo* info = ¬ification_info_map_[it->first]; |
| 1212 info->total_count++; | 1151 info->total_count++; |
| 1213 info->payload = it->second.payload; | 1152 info->payload = it->second.payload; |
| 1214 } | 1153 } |
| 1215 } | 1154 } |
| 1216 | 1155 |
| 1217 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { | 1156 void SyncManagerImpl::UpdateInvalidatorState(InvalidatorState state) { |
| 1157 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 1158 | |
| 1218 const std::string& state_str = InvalidatorStateToString(state); | 1159 const std::string& state_str = InvalidatorStateToString(state); |
| 1219 invalidator_state_ = state; | 1160 invalidator_state_ = state; |
| 1220 DVLOG(1) << "Invalidator state changed to: " << state_str; | 1161 DVLOG(1) << "Invalidator state changed to: " << state_str; |
| 1221 const bool notifications_enabled = | 1162 const bool notifications_enabled = |
| 1222 (invalidator_state_ == INVALIDATIONS_ENABLED); | 1163 (invalidator_state_ == INVALIDATIONS_ENABLED); |
| 1223 allstatus_.SetNotificationsEnabled(notifications_enabled); | 1164 allstatus_.SetNotificationsEnabled(notifications_enabled); |
| 1224 scheduler_->SetNotificationsEnabled(notifications_enabled); | 1165 scheduler_->SetNotificationsEnabled(notifications_enabled); |
| 1225 | 1166 |
| 1226 if (invalidator_state_ == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 1167 if (invalidator_state_ == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
| 1227 // If the invalidator's credentials were rejected, that means that | 1168 // If the invalidator's credentials were rejected, that means that |
| 1228 // our sync credentials are also bad, so invalidate those. | 1169 // our sync credentials are also bad, so invalidate those. |
| 1229 connection_manager_->OnInvalidationCredentialsRejected(); | 1170 connection_manager_->OnInvalidationCredentialsRejected(); |
| 1230 } | 1171 } |
| 1231 | 1172 |
| 1232 if (js_event_handler_.IsInitialized()) { | 1173 if (js_event_handler_.IsInitialized()) { |
| 1233 base::DictionaryValue details; | 1174 base::DictionaryValue details; |
| 1234 details.SetString("state", state_str); | 1175 details.SetString("state", state_str); |
| 1235 js_event_handler_.Call(FROM_HERE, | 1176 js_event_handler_.Call(FROM_HERE, |
| 1236 &JsEventHandler::HandleJsEvent, | 1177 &JsEventHandler::HandleJsEvent, |
| 1237 "onNotificationStateChange", | 1178 "onNotificationStateChange", |
| 1238 JsEventDetails(&details)); | 1179 JsEventDetails(&details)); |
| 1239 } | 1180 } |
| 1240 } | 1181 } |
| 1241 | 1182 |
| 1242 void SyncManagerImpl::OnIncomingInvalidation( | 1183 void SyncManagerImpl::Invalidate( |
| 1243 const ObjectIdInvalidationMap& invalidation_map) { | 1184 const ObjectIdInvalidationMap& invalidation_map) { |
| 1244 DCHECK(thread_checker_.CalledOnValidThread()); | 1185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1245 | 1186 |
| 1246 // TODO(dcheng): Acknowledge immediately for now. Fix this once the | |
| 1247 // invalidator doesn't repeatedly ping for unacknowledged invaliations, since | |
| 1248 // it conflicts with the sync scheduler's internal backoff algorithm. | |
| 1249 // See http://crbug.com/124149 for more information. | |
| 1250 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin(); | |
| 1251 it != invalidation_map.end(); ++it) { | |
| 1252 invalidator_->Acknowledge(it->first, it->second.ack_handle); | |
| 1253 } | |
| 1254 | |
| 1255 const ModelTypeInvalidationMap& type_invalidation_map = | 1187 const ModelTypeInvalidationMap& type_invalidation_map = |
| 1256 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); | 1188 ObjectIdInvalidationMapToModelTypeInvalidationMap(invalidation_map); |
| 1257 if (type_invalidation_map.empty()) { | 1189 if (type_invalidation_map.empty()) { |
| 1258 LOG(WARNING) << "Sync received invalidation without any type information."; | 1190 LOG(WARNING) << "Sync received invalidation without any type information."; |
| 1259 } else { | 1191 } else { |
| 1260 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); | 1192 allstatus_.IncrementNudgeCounter(NUDGE_SOURCE_NOTIFICATION); |
| 1261 scheduler_->ScheduleNudgeWithStatesAsync( | 1193 scheduler_->ScheduleNudgeWithStatesAsync( |
| 1262 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), | 1194 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), |
| 1263 NUDGE_SOURCE_NOTIFICATION, | 1195 NUDGE_SOURCE_NOTIFICATION, |
| 1264 type_invalidation_map, FROM_HERE); | 1196 type_invalidation_map, FROM_HERE); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1400 int SyncManagerImpl::GetDefaultNudgeDelay() { | 1332 int SyncManagerImpl::GetDefaultNudgeDelay() { |
| 1401 return kDefaultNudgeDelayMilliseconds; | 1333 return kDefaultNudgeDelayMilliseconds; |
| 1402 } | 1334 } |
| 1403 | 1335 |
| 1404 // static. | 1336 // static. |
| 1405 int SyncManagerImpl::GetPreferencesNudgeDelay() { | 1337 int SyncManagerImpl::GetPreferencesNudgeDelay() { |
| 1406 return kPreferencesNudgeDelayMilliseconds; | 1338 return kPreferencesNudgeDelayMilliseconds; |
| 1407 } | 1339 } |
| 1408 | 1340 |
| 1409 } // namespace syncer | 1341 } // namespace syncer |
| OLD | NEW |