1 // Copyright 2014 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 COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_
9 #define COMPONENTS_INVALIDATION_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 "components/invalidation/invalidation_export.h"
20 #include "components/invalidation/state_writer.h"
21 #include "components/invalidation/sync_system_resources.h"
22 #include "google/cacheinvalidation/include/invalidation-listener.h"
23 #include "sync/internal_api/public/base/invalidator_state.h"
24 #include "sync/internal_api/public/util/weak_handle.h"
25 #include "sync/notifier/ack_handler.h"
26 #include "sync/notifier/invalidation_state_tracker.h"
27 #include "sync/notifier/unacked_invalidation_set.h"
30 class XmppTaskParentInterface
;
35 } // namespace notifier
39 class ObjectIdInvalidationMap
;
40 class RegistrationManager
;
42 // SyncInvalidationListener is not thread-safe and lives on the sync
44 class INVALIDATION_EXPORT_PRIVATE SyncInvalidationListener
45 : public NON_EXPORTED_BASE(invalidation::InvalidationListener
),
47 public SyncNetworkChannel::Observer
,
49 public base::NonThreadSafe
{
51 typedef base::Callback
<invalidation::InvalidationClient
*(
52 invalidation::SystemResources
*,
54 const invalidation::string
&,
55 const invalidation::string
&,
56 invalidation::InvalidationListener
*)> CreateInvalidationClientCallback
;
58 class INVALIDATION_EXPORT_PRIVATE Delegate
{
62 virtual void OnInvalidate(
63 const ObjectIdInvalidationMap
& invalidations
) = 0;
65 virtual void OnInvalidatorStateChange(InvalidatorState state
) = 0;
68 explicit SyncInvalidationListener(
69 scoped_ptr
<SyncNetworkChannel
> network_channel
);
72 virtual ~SyncInvalidationListener();
74 // Does not take ownership of |delegate| or |state_writer|.
75 // |invalidation_state_tracker| must be initialized.
77 const CreateInvalidationClientCallback
&
78 create_invalidation_client_callback
,
79 const std::string
& client_id
, const std::string
& client_info
,
80 const std::string
& invalidation_bootstrap_data
,
81 const UnackedInvalidationsMap
& initial_object_states
,
82 const WeakHandle
<InvalidationStateTracker
>& invalidation_state_tracker
,
85 void UpdateCredentials(const std::string
& email
, const std::string
& token
);
87 // Update the set of object IDs that we're interested in getting
88 // notifications for. May be called at any time.
89 void UpdateRegisteredIds(const ObjectIdSet
& ids
);
91 // invalidation::InvalidationListener implementation.
93 invalidation::InvalidationClient
* client
) OVERRIDE
;
94 virtual void Invalidate(
95 invalidation::InvalidationClient
* client
,
96 const invalidation::Invalidation
& invalidation
,
97 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
98 virtual void InvalidateUnknownVersion(
99 invalidation::InvalidationClient
* client
,
100 const invalidation::ObjectId
& object_id
,
101 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
102 virtual void InvalidateAll(
103 invalidation::InvalidationClient
* client
,
104 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
105 virtual void InformRegistrationStatus(
106 invalidation::InvalidationClient
* client
,
107 const invalidation::ObjectId
& object_id
,
108 invalidation::InvalidationListener::RegistrationState reg_state
) OVERRIDE
;
109 virtual void InformRegistrationFailure(
110 invalidation::InvalidationClient
* client
,
111 const invalidation::ObjectId
& object_id
,
113 const std::string
& error_message
) OVERRIDE
;
114 virtual void ReissueRegistrations(
115 invalidation::InvalidationClient
* client
,
116 const std::string
& prefix
,
117 int prefix_length
) OVERRIDE
;
118 virtual void InformError(
119 invalidation::InvalidationClient
* client
,
120 const invalidation::ErrorInfo
& error_info
) OVERRIDE
;
122 // AckHandler implementation.
123 virtual void Acknowledge(
124 const invalidation::ObjectId
& id
,
125 const syncer::AckHandle
& handle
) OVERRIDE
;
127 const invalidation::ObjectId
& id
,
128 const syncer::AckHandle
& handle
) OVERRIDE
;
130 // StateWriter implementation.
131 virtual void WriteState(const std::string
& state
) OVERRIDE
;
133 // SyncNetworkChannel::Observer implementation.
134 virtual void OnNetworkChannelStateChanged(
135 InvalidatorState invalidator_state
) OVERRIDE
;
137 void DoRegistrationUpdate();
139 void RequestDetailedStatus(
140 base::Callback
<void(const base::DictionaryValue
&)> callback
) const;
147 InvalidatorState
GetState() const;
149 void EmitStateChange();
151 // Sends invalidations to their appropriate destination.
153 // If there are no observers registered for them, they will be saved for
156 // If there are observers registered, they will be saved (to make sure we
157 // don't drop them until they've been acted on) and emitted to the observers.
158 void DispatchInvalidations(const ObjectIdInvalidationMap
& invalidations
);
160 // Saves invalidations.
162 // This call isn't synchronous so we can't guarantee these invalidations will
163 // be safely on disk by the end of the call, but it should ensure that the
164 // data makes it to disk eventually.
165 void SaveInvalidations(const ObjectIdInvalidationMap
& to_save
);
167 // Emits previously saved invalidations to their registered observers.
168 void EmitSavedInvalidations(const ObjectIdInvalidationMap
& to_emit
);
170 // Generate a Dictionary with all the debugging information.
171 scoped_ptr
<base::DictionaryValue
> CollectDebugData() const;
173 WeakHandle
<AckHandler
> GetThisAsAckHandler();
175 scoped_ptr
<SyncNetworkChannel
> sync_network_channel_
;
176 SyncSystemResources sync_system_resources_
;
177 UnackedInvalidationsMap unacked_invalidations_map_
;
178 WeakHandle
<InvalidationStateTracker
> invalidation_state_tracker_
;
180 scoped_ptr
<invalidation::InvalidationClient
> invalidation_client_
;
181 scoped_ptr
<RegistrationManager
> registration_manager_
;
182 // Stored to pass to |registration_manager_| on start.
183 ObjectIdSet registered_ids_
;
185 // The states of the ticl and the push client.
186 InvalidatorState ticl_state_
;
187 InvalidatorState push_client_state_
;
189 base::WeakPtrFactory
<SyncInvalidationListener
> weak_ptr_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener
);
194 } // namespace syncer
196 #endif // COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_