debug_info_event_listener.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 SYNC_INTERNAL_API_DEBUG_INFO_EVENT_LISTENER_H_
6#define SYNC_INTERNAL_API_DEBUG_INFO_EVENT_LISTENER_H_
7
8#include <queue>
9#include <string>
10
11#include "base/compiler_specific.h"
12#include "sync/base/sync_export.h"
13#include "sync/internal_api/public/data_type_debug_info_listener.h"
14#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
15#include "sync/internal_api/public/sync_encryption_handler.h"
16#include "sync/internal_api/public/sync_manager.h"
17#include "sync/internal_api/public/util/weak_handle.h"
18#include "sync/js/js_backend.h"
19#include "sync/protocol/sync.pb.h"
20#include "sync/sessions/debug_info_getter.h"
21
22namespace syncer {
23
24// In order to track datatype association results, we need at least as many
25// entries as datatypes.
26const unsigned int kMaxEntries = 25;
27
28// Listens to events and records them in a queue. And passes the events to
29// syncer when requested.
30// This class is not thread safe and should only be accessed on the sync thread.
31class SYNC_EXPORT_PRIVATE DebugInfoEventListener
32    : public SyncManager::Observer,
33      public SyncEncryptionHandler::Observer,
34      public sessions::DebugInfoGetter,
35      public DataTypeDebugInfoListener {
36 public:
37  DebugInfoEventListener();
38  virtual ~DebugInfoEventListener();
39
40  // SyncManager::Observer implementation.
41  virtual void OnSyncCycleCompleted(
42    const sessions::SyncSessionSnapshot& snapshot) OVERRIDE;
43  virtual void OnInitializationComplete(
44      const WeakHandle<JsBackend>& js_backend,
45      const WeakHandle<DataTypeDebugInfoListener>& debug_listener,
46      bool success, ModelTypeSet restored_types) OVERRIDE;
47  virtual void OnConnectionStatusChange(
48      ConnectionStatus connection_status) OVERRIDE;
49  virtual void OnStopSyncingPermanently() OVERRIDE;
50  virtual void OnUpdatedToken(const std::string& token) OVERRIDE;
51  virtual void OnActionableError(
52      const SyncProtocolError& sync_error) OVERRIDE;
53
54  // SyncEncryptionHandler::Observer implementation.
55  virtual void OnPassphraseRequired(
56      PassphraseRequiredReason reason,
57      const sync_pb::EncryptedData& pending_keys) OVERRIDE;
58  virtual void OnPassphraseAccepted() OVERRIDE;
59  virtual void OnBootstrapTokenUpdated(
60      const std::string& bootstrap_token,
61      BootstrapTokenType type) OVERRIDE;
62  virtual void OnEncryptedTypesChanged(
63      ModelTypeSet encrypted_types,
64      bool encrypt_everything) OVERRIDE;
65  virtual void OnEncryptionComplete() OVERRIDE;
66  virtual void OnCryptographerStateChanged(
67      Cryptographer* cryptographer) OVERRIDE;
68  virtual void OnPassphraseTypeChanged(
69      PassphraseType type,
70      base::Time explicit_passphrase_time) OVERRIDE;
71
72  // Sync manager events.
73  void OnNudgeFromDatatype(ModelType datatype);
74  void OnIncomingNotification(
75      const ModelTypeInvalidationMap& invalidation_map);
76
77  // DebugInfoGetter implementation.
78  virtual void GetAndClearDebugInfo(sync_pb::DebugInfo* debug_info) OVERRIDE;
79
80  // DataTypeDebugInfoListener implementation.
81  virtual void OnDataTypeAssociationComplete(
82      const DataTypeAssociationStats& association_stats) OVERRIDE;
83  virtual void OnConfigureComplete() 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, VerifyGetAndClearEvents);
92
93  void AddEventToQueue(const sync_pb::DebugEventInfo& event_info);
94  void CreateAndAddEvent(sync_pb::DebugEventInfo::SingletonEventType type);
95  std::queue<sync_pb::DebugEventInfo> events_;
96
97  // True indicates we had to drop one or more events to keep our limit of
98  // |kMaxEntries|.
99  bool events_dropped_;
100
101  // Cryptographer has keys that are not yet decrypted.
102  bool cryptographer_has_pending_keys_;
103
104  // Cryptographer is initialized and does not have pending keys.
105  bool cryptographer_ready_;
106
107  base::WeakPtrFactory<DebugInfoEventListener> weak_ptr_factory_;
108
109  base::ThreadChecker thread_checker_;
110
111  DISALLOW_COPY_AND_ASSIGN(DebugInfoEventListener);
112};
113
114}  // namespace syncer
115
116#endif  // SYNC_INTERNAL_API_DEBUG_INFO_EVENT_LISTENER_H_
117