File: sync_status_tracker.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (185 lines) | stat: -rw-r--r-- 6,284 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/engine/sync_status_tracker.h"

#include <algorithm>
#include <utility>

#include "base/logging.h"
#include "components/sync/engine/net/server_connection_manager.h"
#include "components/sync/engine/sync_cycle_event.h"

namespace syncer {

SyncStatusTracker::SyncStatusTracker(
    const base::RepeatingCallback<void(const SyncStatus&)>&
        status_changed_callback)
    : status_changed_callback_(status_changed_callback) {
  DCHECK(status_changed_callback_);
}

SyncStatusTracker::~SyncStatusTracker() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

SyncStatus SyncStatusTracker::CreateBlankStatus() const {
  // Status is initialized with the previous status value.  Variables
  // whose values accumulate (e.g. lifetime counters like updates_received)
  // are not to be cleared here.
  SyncStatus status = status_;
  status.server_conflicts = 0;
  status.committed_count = 0;
  return status;
}

SyncStatus SyncStatusTracker::CalcSyncing(const SyncCycleEvent& event) const {
  SyncStatus status = CreateBlankStatus();
  const SyncCycleSnapshot& snapshot = event.snapshot;
  status.server_conflicts = snapshot.num_server_conflicts();
  status.committed_count =
      snapshot.model_neutral_state().num_successful_commits;

  switch (event.what_happened) {
    case SyncCycleEvent::SYNC_CYCLE_BEGIN:
      status.syncing = true;
      break;
    case SyncCycleEvent::SYNC_CYCLE_ENDED:
      status.syncing = false;
      // Accumulate update count only once per cycle to avoid double-counting.
      status.updates_received +=
          snapshot.model_neutral_state().num_updates_downloaded_total;
      status.tombstone_updates_received +=
          snapshot.model_neutral_state().num_tombstone_updates_downloaded_total;
      status.num_commits_total +=
          snapshot.model_neutral_state().num_successful_commits;
      break;
    case SyncCycleEvent::STATUS_CHANGED:
      break;
  }
  return status;
}

void SyncStatusTracker::OnSyncCycleEvent(const SyncCycleEvent& event) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_ = CalcSyncing(event);
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::OnActionableProtocolError(
    const SyncProtocolError& sync_protocol_error) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_ = CreateBlankStatus();
  status_.sync_protocol_error = sync_protocol_error;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::OnRetryTimeChanged(base::Time retry_time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.retry_time = retry_time;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::OnThrottledTypesChanged(DataTypeSet throttled_types) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.throttled_types = throttled_types;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::OnBackedOffTypesChanged(DataTypeSet backed_off_types) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.backed_off_types = backed_off_types;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::OnMigrationRequested(DataTypeSet) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void SyncStatusTracker::OnProtocolEvent(const ProtocolEvent&) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void SyncStatusTracker::SetNotificationsEnabled(bool notifications_enabled) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.notifications_enabled = notifications_enabled;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::IncrementNotificationsReceived() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  ++status_.notifications_received;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetEncryptedTypes(DataTypeSet types) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.encrypted_types = types;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetCryptographerCanEncrypt(bool can_encrypt) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.cryptographer_can_encrypt = can_encrypt;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetCryptoHasPendingKeys(bool has_pending_keys) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.crypto_has_pending_keys = has_pending_keys;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetPassphraseType(PassphraseType type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.passphrase_type = type;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetHasKeystoreKey(bool has_keystore_key) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.has_keystore_key = has_keystore_key;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetKeystoreMigrationTime(
    const base::Time& migration_time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.keystore_migration_time = migration_time;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetTrustedVaultDebugInfo(
    const sync_pb::NigoriSpecifics::TrustedVaultDebugInfo&
        trusted_vault_debug_info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.trusted_vault_debug_info = trusted_vault_debug_info;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetCacheGuid(const std::string& cache_guid) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.cache_guid = cache_guid;
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetHasPendingInvalidations(
    DataType type,
    bool has_pending_invalidations) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (has_pending_invalidations) {
    status_.invalidated_data_types.Put(type);
  } else {
    status_.invalidated_data_types.Remove(type);
  }
  status_changed_callback_.Run(status_);
}

void SyncStatusTracker::SetLocalBackendFolder(const std::string& folder) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  status_.local_sync_folder = folder;
  status_changed_callback_.Run(status_);
}

}  // namespace syncer