1 // Copyright 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 
5 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_DEBUG_INFO_EVENT_LISTENER_H_
6 #define COMPONENTS_SYNC_ENGINE_IMPL_DEBUG_INFO_EVENT_LISTENER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/containers/circular_deque.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h"
15 #include "base/sequence_checker.h"
16 #include "components/sync/base/model_type.h"
17 #include "components/sync/base/weak_handle.h"
18 #include "components/sync/engine/cycle/sync_cycle_snapshot.h"
19 #include "components/sync/engine/data_type_debug_info_listener.h"
20 #include "components/sync/engine/sync_encryption_handler.h"
21 #include "components/sync/engine/sync_manager.h"
22 #include "components/sync/engine_impl/cycle/debug_info_getter.h"
23 #include "components/sync/js/js_backend.h"
24 #include "components/sync/protocol/sync.pb.h"
25 
26 namespace syncer {
27 
28 // In order to track datatype association results, we need at least as many
29 // entries as datatypes. Reserve additional space for other kinds of events that
30 // are likely to happen during first sync or startup.
31 const unsigned int kMaxEntries = ModelType::NUM_ENTRIES + 10;
32 
33 // Listens to events and records them in a queue. And passes the events to
34 // syncer when requested.
35 // This class is not thread safe and should only be accessed on the sync thread.
36 class DebugInfoEventListener : public SyncManager::Observer,
37                                public SyncEncryptionHandler::Observer,
38                                public DebugInfoGetter,
39                                public DataTypeDebugInfoListener {
40  public:
41   DebugInfoEventListener();
42   ~DebugInfoEventListener() override;
43 
44   // SyncManager::Observer implementation.
45   void OnSyncCycleCompleted(const SyncCycleSnapshot& snapshot) override;
46   void OnInitializationComplete(
47       const WeakHandle<JsBackend>& js_backend,
48       const WeakHandle<DataTypeDebugInfoListener>& debug_listener,
49       bool success) override;
50   void OnConnectionStatusChange(ConnectionStatus connection_status) override;
51   void OnActionableError(const SyncProtocolError& sync_error) override;
52   void OnMigrationRequested(ModelTypeSet types) override;
53   void OnProtocolEvent(const ProtocolEvent& event) override;
54 
55   // SyncEncryptionHandler::Observer implementation.
56   void OnPassphraseRequired(
57       const KeyDerivationParams& key_derivation_params,
58       const sync_pb::EncryptedData& pending_keys) override;
59   void OnPassphraseAccepted() override;
60   void OnTrustedVaultKeyRequired() override;
61   void OnTrustedVaultKeyAccepted() override;
62   void OnBootstrapTokenUpdated(const std::string& bootstrap_token,
63                                BootstrapTokenType type) override;
64   void OnEncryptedTypesChanged(ModelTypeSet encrypted_types,
65                                bool encrypt_everything) override;
66   void OnCryptographerStateChanged(Cryptographer* cryptographer,
67                                    bool has_pending_keys) override;
68   void OnPassphraseTypeChanged(PassphraseType type,
69                                base::Time explicit_passphrase_time) override;
70 
71   // Sync manager events.
72   void OnNudgeFromDatatype(ModelType datatype);
73 
74   // DebugInfoGetter implementation.
75   sync_pb::DebugInfo GetDebugInfo() const override;
76 
77   // DebugInfoGetter implementation.
78   void ClearDebugInfo() override;
79 
80   // DataTypeDebugInfoListener implementation.
81   void OnDataTypeConfigureComplete(
82       const std::vector<DataTypeConfigurationStats>& configuration_stats)
83       override;
84 
85   // Returns a weak pointer to this object.
86   base::WeakPtr<DataTypeDebugInfoListener> GetWeakPtr();
87 
88  private:
89   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyEventsAdded);
90   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyQueueSize);
91   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyGetEvents);
92   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyClearEvents);
93 
94   void AddEventToQueue(const sync_pb::DebugEventInfo& event_info);
95   void CreateAndAddEvent(sync_pb::SyncEnums::SingletonDebugEventType type);
96 
97   using DebugEventInfoQueue = base::circular_deque<sync_pb::DebugEventInfo>;
98   DebugEventInfoQueue events_;
99 
100   // True indicates we had to drop one or more events to keep our limit of
101   // |kMaxEntries|.
102   bool events_dropped_;
103 
104   // Cryptographer has keys that are not yet decrypted.
105   bool cryptographer_has_pending_keys_;
106 
107   // Cryptographer is able to encrypt data, which usually means it's initialized
108   // and does not have pending keys.
109   bool cryptographer_can_encrypt_;
110 
111   SEQUENCE_CHECKER(sequence_checker_);
112 
113   base::WeakPtrFactory<DebugInfoEventListener> weak_ptr_factory_{this};
114 
115   DISALLOW_COPY_AND_ASSIGN(DebugInfoEventListener);
116 };
117 
118 }  // namespace syncer
119 
120 #endif  // COMPONENTS_SYNC_ENGINE_IMPL_DEBUG_INFO_EVENT_LISTENER_H_
121