- add sources.
[platform/framework/web/crosswalk.git] / src / sync / internal_api / debug_info_event_listener.h
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
22 namespace syncer {
23
24 // In order to track datatype association results, we need at least as many
25 // entries as datatypes.
26 const 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.
31 class 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 OnActionableError(
51       const SyncProtocolError& sync_error) OVERRIDE;
52
53   // SyncEncryptionHandler::Observer implementation.
54   virtual void OnPassphraseRequired(
55       PassphraseRequiredReason reason,
56       const sync_pb::EncryptedData& pending_keys) OVERRIDE;
57   virtual void OnPassphraseAccepted() OVERRIDE;
58   virtual void OnBootstrapTokenUpdated(
59       const std::string& bootstrap_token,
60       BootstrapTokenType type) OVERRIDE;
61   virtual void OnEncryptedTypesChanged(
62       ModelTypeSet encrypted_types,
63       bool encrypt_everything) OVERRIDE;
64   virtual void OnEncryptionComplete() OVERRIDE;
65   virtual void OnCryptographerStateChanged(
66       Cryptographer* cryptographer) OVERRIDE;
67   virtual void OnPassphraseTypeChanged(
68       PassphraseType type,
69       base::Time explicit_passphrase_time) OVERRIDE;
70
71   // Sync manager events.
72   void OnNudgeFromDatatype(ModelType datatype);
73   void OnIncomingNotification(const ObjectIdInvalidationMap& invalidations);
74
75   // DebugInfoGetter implementation.
76   virtual void GetAndClearDebugInfo(sync_pb::DebugInfo* debug_info) OVERRIDE;
77
78   // DataTypeDebugInfoListener implementation.
79   virtual void OnDataTypeConfigureComplete(
80       const std::vector<DataTypeConfigurationStats>& configuration_stats)
81       OVERRIDE;
82
83   // Returns a weak pointer to this object.
84   base::WeakPtr<DataTypeDebugInfoListener> GetWeakPtr();
85
86  private:
87   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyEventsAdded);
88   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyQueueSize);
89   FRIEND_TEST_ALL_PREFIXES(DebugInfoEventListenerTest, VerifyGetAndClearEvents);
90
91   void AddEventToQueue(const sync_pb::DebugEventInfo& event_info);
92   void CreateAndAddEvent(sync_pb::DebugEventInfo::SingletonEventType type);
93   std::queue<sync_pb::DebugEventInfo> events_;
94
95   // True indicates we had to drop one or more events to keep our limit of
96   // |kMaxEntries|.
97   bool events_dropped_;
98
99   // Cryptographer has keys that are not yet decrypted.
100   bool cryptographer_has_pending_keys_;
101
102   // Cryptographer is initialized and does not have pending keys.
103   bool cryptographer_ready_;
104
105   base::ThreadChecker thread_checker_;
106
107   base::WeakPtrFactory<DebugInfoEventListener> weak_ptr_factory_;
108
109   DISALLOW_COPY_AND_ASSIGN(DebugInfoEventListener);
110 };
111
112 }  // namespace syncer
113
114 #endif  // SYNC_INTERNAL_API_DEBUG_INFO_EVENT_LISTENER_H_