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.
5 // A simple wrapper around invalidation::InvalidationClient that
6 // handles all the startup/shutdown details and hookups.
8 #ifndef SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_
9 #define SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_
13 #include "base/basictypes.h"
14 #include "base/callback_forward.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "google/cacheinvalidation/include/invalidation-listener.h"
20 #include "jingle/notifier/listener/push_client_observer.h"
21 #include "sync/base/sync_export.h"
22 #include "sync/internal_api/public/util/weak_handle.h"
23 #include "sync/notifier/ack_tracker.h"
24 #include "sync/notifier/invalidation_state_tracker.h"
25 #include "sync/notifier/invalidator_state.h"
26 #include "sync/notifier/object_id_invalidation_map.h"
27 #include "sync/notifier/state_writer.h"
28 #include "sync/notifier/sync_system_resources.h"
35 class XmppTaskParentInterface
;
40 } // namespace notifier
44 class RegistrationManager
;
46 // SyncInvalidationListener is not thread-safe and lives on the sync
48 class SYNC_EXPORT_PRIVATE SyncInvalidationListener
49 : public NON_EXPORTED_BASE(invalidation::InvalidationListener
),
51 public NON_EXPORTED_BASE(notifier::PushClientObserver
),
52 public base::NonThreadSafe
,
53 public AckTracker::Delegate
{
55 typedef base::Callback
<invalidation::InvalidationClient
*(
56 invalidation::SystemResources
*,
58 const invalidation::string
&,
59 const invalidation::string
&,
60 invalidation::InvalidationListener
*)> CreateInvalidationClientCallback
;
62 class SYNC_EXPORT_PRIVATE Delegate
{
66 virtual void OnInvalidate(
67 const ObjectIdInvalidationMap
& invalidation_map
) = 0;
69 virtual void OnInvalidatorStateChange(InvalidatorState state
) = 0;
72 explicit SyncInvalidationListener(
73 base::TickClock
* tick_clock
,
74 scoped_ptr
<notifier::PushClient
> push_client
);
77 virtual ~SyncInvalidationListener();
79 // Does not take ownership of |delegate| or |state_writer|.
80 // |invalidation_state_tracker| must be initialized.
82 const CreateInvalidationClientCallback
&
83 create_invalidation_client_callback
,
84 const std::string
& client_id
, const std::string
& client_info
,
85 const std::string
& invalidation_bootstrap_data
,
86 const InvalidationStateMap
& initial_invalidation_state_map
,
87 const WeakHandle
<InvalidationStateTracker
>& invalidation_state_tracker
,
90 void UpdateCredentials(const std::string
& email
, const std::string
& token
);
92 // Update the set of object IDs that we're interested in getting
93 // notifications for. May be called at any time.
94 void UpdateRegisteredIds(const ObjectIdSet
& ids
);
95 // Acknowledge that an invalidation for |id| was handled.
96 void Acknowledge(const invalidation::ObjectId
& id
,
97 const AckHandle
& ack_handle
);
99 // invalidation::InvalidationListener implementation.
101 invalidation::InvalidationClient
* client
) OVERRIDE
;
102 virtual void Invalidate(
103 invalidation::InvalidationClient
* client
,
104 const invalidation::Invalidation
& invalidation
,
105 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
106 virtual void InvalidateUnknownVersion(
107 invalidation::InvalidationClient
* client
,
108 const invalidation::ObjectId
& object_id
,
109 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
110 virtual void InvalidateAll(
111 invalidation::InvalidationClient
* client
,
112 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
113 virtual void InformRegistrationStatus(
114 invalidation::InvalidationClient
* client
,
115 const invalidation::ObjectId
& object_id
,
116 invalidation::InvalidationListener::RegistrationState reg_state
) OVERRIDE
;
117 virtual void InformRegistrationFailure(
118 invalidation::InvalidationClient
* client
,
119 const invalidation::ObjectId
& object_id
,
121 const std::string
& error_message
) OVERRIDE
;
122 virtual void ReissueRegistrations(
123 invalidation::InvalidationClient
* client
,
124 const std::string
& prefix
,
125 int prefix_length
) OVERRIDE
;
126 virtual void InformError(
127 invalidation::InvalidationClient
* client
,
128 const invalidation::ErrorInfo
& error_info
) OVERRIDE
;
130 // StateWriter implementation.
131 virtual void WriteState(const std::string
& state
) OVERRIDE
;
133 // notifier::PushClientObserver implementation.
134 virtual void OnNotificationsEnabled() OVERRIDE
;
135 virtual void OnNotificationsDisabled(
136 notifier::NotificationsDisabledReason reason
) OVERRIDE
;
137 virtual void OnIncomingNotification(
138 const notifier::Notification
& notification
) OVERRIDE
;
140 void DoRegistrationUpdate();
143 InvalidationStateMap
GetStateMapForTest() const;
144 AckTracker
* GetAckTrackerForTest();
149 InvalidatorState
GetState() const;
151 void EmitStateChange();
153 void PrepareInvalidation(const ObjectIdSet
& ids
,
155 const std::string
& payload
,
156 invalidation::InvalidationClient
* client
,
157 const invalidation::AckHandle
& ack_handle
);
158 void EmitInvalidation(const ObjectIdSet
& ids
,
160 const std::string
& payload
,
161 invalidation::InvalidationClient
* client
,
162 const invalidation::AckHandle
& ack_handle
,
163 const AckHandleMap
& local_ack_handles
);
165 // AckTracker::Delegate implementation.
166 virtual void OnTimeout(const ObjectIdSet
& ids
) OVERRIDE
;
168 AckTracker ack_tracker_
;
170 // Owned by |sync_system_resources_|.
171 notifier::PushClient
* const push_client_
;
172 SyncSystemResources sync_system_resources_
;
173 InvalidationStateMap invalidation_state_map_
;
174 WeakHandle
<InvalidationStateTracker
> invalidation_state_tracker_
;
176 scoped_ptr
<invalidation::InvalidationClient
> invalidation_client_
;
177 scoped_ptr
<RegistrationManager
> registration_manager_
;
178 // Stored to pass to |registration_manager_| on start.
179 ObjectIdSet registered_ids_
;
181 // The states of the ticl and the push client.
182 InvalidatorState ticl_state_
;
183 InvalidatorState push_client_state_
;
185 base::WeakPtrFactory
<SyncInvalidationListener
> weak_ptr_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener
);
190 } // namespace syncer
192 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_