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