sync_status_tracker.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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
  3. // found in the LICENSE file.
  4. #include "components/sync/engine/sync_status_tracker.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "components/sync/engine/net/server_connection_manager.h"
  9. #include "components/sync/engine/sync_cycle_event.h"
  10. namespace syncer {
  11. SyncStatusTracker::SyncStatusTracker(
  12. const base::RepeatingCallback<void(const SyncStatus&)>&
  13. status_changed_callback)
  14. : status_changed_callback_(status_changed_callback) {
  15. DCHECK(status_changed_callback_);
  16. }
  17. SyncStatusTracker::~SyncStatusTracker() {
  18. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  19. }
  20. SyncStatus SyncStatusTracker::CreateBlankStatus() const {
  21. // Status is initialized with the previous status value. Variables
  22. // whose values accumulate (e.g. lifetime counters like updates_received)
  23. // are not to be cleared here.
  24. SyncStatus status = status_;
  25. status.server_conflicts = 0;
  26. status.committed_count = 0;
  27. return status;
  28. }
  29. SyncStatus SyncStatusTracker::CalcSyncing(const SyncCycleEvent& event) const {
  30. SyncStatus status = CreateBlankStatus();
  31. const SyncCycleSnapshot& snapshot = event.snapshot;
  32. status.server_conflicts = snapshot.num_server_conflicts();
  33. status.committed_count =
  34. snapshot.model_neutral_state().num_successful_commits;
  35. switch (event.what_happened) {
  36. case SyncCycleEvent::SYNC_CYCLE_BEGIN:
  37. status.syncing = true;
  38. break;
  39. case SyncCycleEvent::SYNC_CYCLE_ENDED:
  40. status.syncing = false;
  41. // Accumulate update count only once per cycle to avoid double-counting.
  42. status.updates_received +=
  43. snapshot.model_neutral_state().num_updates_downloaded_total;
  44. status.tombstone_updates_received +=
  45. snapshot.model_neutral_state().num_tombstone_updates_downloaded_total;
  46. status.num_commits_total +=
  47. snapshot.model_neutral_state().num_successful_commits;
  48. break;
  49. case SyncCycleEvent::STATUS_CHANGED:
  50. break;
  51. }
  52. return status;
  53. }
  54. void SyncStatusTracker::OnSyncCycleEvent(const SyncCycleEvent& event) {
  55. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  56. status_ = CalcSyncing(event);
  57. status_changed_callback_.Run(status_);
  58. }
  59. void SyncStatusTracker::OnActionableError(
  60. const SyncProtocolError& sync_protocol_error) {
  61. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  62. status_ = CreateBlankStatus();
  63. status_.sync_protocol_error = sync_protocol_error;
  64. status_changed_callback_.Run(status_);
  65. }
  66. void SyncStatusTracker::OnRetryTimeChanged(base::Time retry_time) {
  67. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  68. status_.retry_time = retry_time;
  69. status_changed_callback_.Run(status_);
  70. }
  71. void SyncStatusTracker::OnThrottledTypesChanged(ModelTypeSet throttled_types) {
  72. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  73. status_.throttled_types = throttled_types;
  74. status_changed_callback_.Run(status_);
  75. }
  76. void SyncStatusTracker::OnBackedOffTypesChanged(ModelTypeSet backed_off_types) {
  77. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  78. status_.backed_off_types = backed_off_types;
  79. status_changed_callback_.Run(status_);
  80. }
  81. void SyncStatusTracker::OnMigrationRequested(ModelTypeSet) {
  82. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  83. }
  84. void SyncStatusTracker::OnProtocolEvent(const ProtocolEvent&) {
  85. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  86. }
  87. void SyncStatusTracker::SetNotificationsEnabled(bool notifications_enabled) {
  88. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  89. status_.notifications_enabled = notifications_enabled;
  90. status_changed_callback_.Run(status_);
  91. }
  92. void SyncStatusTracker::IncrementNotificationsReceived() {
  93. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  94. ++status_.notifications_received;
  95. status_changed_callback_.Run(status_);
  96. }
  97. void SyncStatusTracker::SetEncryptedTypes(ModelTypeSet types) {
  98. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  99. status_.encrypted_types = types;
  100. status_changed_callback_.Run(status_);
  101. }
  102. void SyncStatusTracker::SetCryptographerCanEncrypt(bool can_encrypt) {
  103. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  104. status_.cryptographer_can_encrypt = can_encrypt;
  105. status_changed_callback_.Run(status_);
  106. }
  107. void SyncStatusTracker::SetCryptoHasPendingKeys(bool has_pending_keys) {
  108. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  109. status_.crypto_has_pending_keys = has_pending_keys;
  110. status_changed_callback_.Run(status_);
  111. }
  112. void SyncStatusTracker::SetPassphraseType(PassphraseType type) {
  113. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  114. status_.passphrase_type = type;
  115. status_changed_callback_.Run(status_);
  116. }
  117. void SyncStatusTracker::SetHasKeystoreKey(bool has_keystore_key) {
  118. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  119. status_.has_keystore_key = has_keystore_key;
  120. status_changed_callback_.Run(status_);
  121. }
  122. void SyncStatusTracker::SetKeystoreMigrationTime(
  123. const base::Time& migration_time) {
  124. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  125. status_.keystore_migration_time = migration_time;
  126. status_changed_callback_.Run(status_);
  127. }
  128. void SyncStatusTracker::SetTrustedVaultDebugInfo(
  129. const sync_pb::NigoriSpecifics::TrustedVaultDebugInfo&
  130. trusted_vault_debug_info) {
  131. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  132. status_.trusted_vault_debug_info = trusted_vault_debug_info;
  133. status_changed_callback_.Run(status_);
  134. }
  135. void SyncStatusTracker::SetCacheGuid(const std::string& cache_guid) {
  136. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  137. status_.cache_guid = cache_guid;
  138. status_changed_callback_.Run(status_);
  139. }
  140. void SyncStatusTracker::SetInvalidatorClientId(
  141. const std::string& invalidator_client_id) {
  142. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  143. status_.invalidator_client_id = invalidator_client_id;
  144. status_changed_callback_.Run(status_);
  145. }
  146. void SyncStatusTracker::SetLocalBackendFolder(const std::string& folder) {
  147. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  148. status_.local_sync_folder = folder;
  149. status_changed_callback_.Run(status_);
  150. }
  151. } // namespace syncer