debug_info_event_listener.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/debug_info_event_listener.h"
  5. #include <stddef.h>
  6. #include "base/logging.h"
  7. #include "components/sync/engine/nigori/cryptographer.h"
  8. #include "components/sync/protocol/encryption.pb.h"
  9. namespace syncer {
  10. DebugInfoEventListener::DebugInfoEventListener()
  11. : events_dropped_(false),
  12. cryptographer_has_pending_keys_(false),
  13. cryptographer_can_encrypt_(false) {}
  14. DebugInfoEventListener::~DebugInfoEventListener() = default;
  15. void DebugInfoEventListener::InitializationComplete() {
  16. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  17. CreateAndAddEvent(sync_pb::SyncEnums::INITIALIZATION_COMPLETE);
  18. }
  19. void DebugInfoEventListener::OnSyncCycleCompleted(
  20. const SyncCycleSnapshot& snapshot) {
  21. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  22. sync_pb::DebugEventInfo event_info;
  23. sync_pb::SyncCycleCompletedEventInfo* sync_completed_event_info =
  24. event_info.mutable_sync_cycle_completed_event_info();
  25. sync_completed_event_info->set_num_server_conflicts(
  26. snapshot.num_server_conflicts());
  27. sync_completed_event_info->set_num_updates_downloaded(
  28. snapshot.model_neutral_state().num_updates_downloaded_total);
  29. sync_completed_event_info->set_get_updates_origin(
  30. snapshot.get_updates_origin());
  31. sync_completed_event_info->mutable_caller_info()->set_notifications_enabled(
  32. snapshot.notifications_enabled());
  33. // Fill the legacy GetUpdatesSource field. This is not used anymore, but it's
  34. // a required field so we still have to fill it with something.
  35. sync_completed_event_info->mutable_caller_info()->set_source(
  36. sync_pb::GetUpdatesCallerInfo::UNKNOWN);
  37. AddEventToQueue(event_info);
  38. }
  39. void DebugInfoEventListener::OnConnectionStatusChange(ConnectionStatus status) {
  40. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  41. CreateAndAddEvent(sync_pb::SyncEnums::CONNECTION_STATUS_CHANGE);
  42. }
  43. void DebugInfoEventListener::OnPassphraseRequired(
  44. const KeyDerivationParams& key_derivation_params,
  45. const sync_pb::EncryptedData& pending_keys) {
  46. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  47. CreateAndAddEvent(sync_pb::SyncEnums::PASSPHRASE_REQUIRED);
  48. }
  49. void DebugInfoEventListener::OnPassphraseAccepted() {
  50. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  51. CreateAndAddEvent(sync_pb::SyncEnums::PASSPHRASE_ACCEPTED);
  52. }
  53. void DebugInfoEventListener::OnTrustedVaultKeyRequired() {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. CreateAndAddEvent(sync_pb::SyncEnums::TRUSTED_VAULT_KEY_REQUIRED);
  56. }
  57. void DebugInfoEventListener::OnTrustedVaultKeyAccepted() {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. CreateAndAddEvent(sync_pb::SyncEnums::TRUSTED_VAULT_KEY_ACCEPTED);
  60. }
  61. void DebugInfoEventListener::OnEncryptedTypesChanged(
  62. ModelTypeSet encrypted_types,
  63. bool encrypt_everything) {
  64. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  65. CreateAndAddEvent(sync_pb::SyncEnums::ENCRYPTED_TYPES_CHANGED);
  66. }
  67. void DebugInfoEventListener::OnCryptographerStateChanged(
  68. Cryptographer* cryptographer,
  69. bool has_pending_keys) {
  70. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  71. cryptographer_has_pending_keys_ = has_pending_keys;
  72. cryptographer_can_encrypt_ = cryptographer->CanEncrypt();
  73. }
  74. void DebugInfoEventListener::OnPassphraseTypeChanged(
  75. PassphraseType type,
  76. base::Time explicit_passphrase_time) {
  77. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  78. CreateAndAddEvent(sync_pb::SyncEnums::PASSPHRASE_TYPE_CHANGED);
  79. }
  80. void DebugInfoEventListener::OnActionableError(
  81. const SyncProtocolError& sync_error) {
  82. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  83. CreateAndAddEvent(sync_pb::SyncEnums::ACTIONABLE_ERROR);
  84. }
  85. void DebugInfoEventListener::OnMigrationRequested(ModelTypeSet types) {}
  86. void DebugInfoEventListener::OnProtocolEvent(const ProtocolEvent& event) {}
  87. void DebugInfoEventListener::OnSyncStatusChanged(const SyncStatus& status) {}
  88. void DebugInfoEventListener::OnNudgeFromDatatype(ModelType datatype) {
  89. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  90. sync_pb::DebugEventInfo event_info;
  91. event_info.set_nudging_datatype(
  92. GetSpecificsFieldNumberFromModelType(datatype));
  93. AddEventToQueue(event_info);
  94. }
  95. sync_pb::DebugInfo DebugInfoEventListener::GetDebugInfo() const {
  96. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  97. DCHECK_LE(events_.size(), kMaxEntries);
  98. sync_pb::DebugInfo debug_info;
  99. for (const sync_pb::DebugEventInfo& event : events_) {
  100. *debug_info.add_events() = event;
  101. }
  102. debug_info.set_events_dropped(events_dropped_);
  103. debug_info.set_cryptographer_ready(cryptographer_can_encrypt_);
  104. debug_info.set_cryptographer_has_pending_keys(
  105. cryptographer_has_pending_keys_);
  106. return debug_info;
  107. }
  108. void DebugInfoEventListener::ClearDebugInfo() {
  109. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  110. DCHECK_LE(events_.size(), kMaxEntries);
  111. events_.clear();
  112. events_dropped_ = false;
  113. }
  114. void DebugInfoEventListener::CreateAndAddEvent(
  115. sync_pb::SyncEnums::SingletonDebugEventType type) {
  116. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  117. sync_pb::DebugEventInfo event_info;
  118. event_info.set_singleton_event(type);
  119. AddEventToQueue(event_info);
  120. }
  121. void DebugInfoEventListener::AddEventToQueue(
  122. const sync_pb::DebugEventInfo& event_info) {
  123. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  124. if (events_.size() >= kMaxEntries) {
  125. DVLOG(1) << "DebugInfoEventListener::AddEventToQueue Dropping an old event "
  126. << "because of full queue";
  127. events_.pop_front();
  128. events_dropped_ = true;
  129. }
  130. events_.push_back(event_info);
  131. }
  132. } // namespace syncer