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/ack_handler.h"
20 #include "components/invalidation/invalidation_export.h"
21 #include "components/invalidation/invalidation_state_tracker.h"
22 #include "components/invalidation/invalidator_state.h"
23 #include "components/invalidation/state_writer.h"
24 #include "components/invalidation/sync_system_resources.h"
25 #include "components/invalidation/unacked_invalidation_set.h"
26 #include "google/cacheinvalidation/include/invalidation-listener.h"
29 class XmppTaskParentInterface
;
34 } // namespace notifier
38 class ObjectIdInvalidationMap
;
39 class RegistrationManager
;
41 // SyncInvalidationListener is not thread-safe and lives on the sync
43 class INVALIDATION_EXPORT_PRIVATE SyncInvalidationListener
44 : public NON_EXPORTED_BASE(invalidation::InvalidationListener
),
46 public SyncNetworkChannel::Observer
,
48 public base::NonThreadSafe
{
50 typedef base::Callback
<invalidation::InvalidationClient
*(
51 invalidation::SystemResources
*,
53 const invalidation::string
&,
54 const invalidation::string
&,
55 invalidation::InvalidationListener
*)> CreateInvalidationClientCallback
;
57 class INVALIDATION_EXPORT_PRIVATE Delegate
{
61 virtual void OnInvalidate(
62 const ObjectIdInvalidationMap
& invalidations
) = 0;
64 virtual void OnInvalidatorStateChange(InvalidatorState state
) = 0;
67 explicit SyncInvalidationListener(
68 scoped_ptr
<SyncNetworkChannel
> network_channel
);
71 virtual ~SyncInvalidationListener();
73 // Does not take ownership of |delegate| or |state_writer|.
74 // |invalidation_state_tracker| must be initialized.
76 const CreateInvalidationClientCallback
&
77 create_invalidation_client_callback
,
78 const std::string
& client_id
,
79 const std::string
& client_info
,
80 const std::string
& invalidation_bootstrap_data
,
81 const UnackedInvalidationsMap
& initial_object_states
,
82 const base::WeakPtr
<InvalidationStateTracker
>& invalidation_state_tracker
,
83 const scoped_refptr
<base::SequencedTaskRunner
>&
84 invalidation_state_tracker_task_runner
,
87 void UpdateCredentials(const std::string
& email
, const std::string
& token
);
89 // Update the set of object IDs that we're interested in getting
90 // notifications for. May be called at any time.
91 void UpdateRegisteredIds(const ObjectIdSet
& ids
);
93 // invalidation::InvalidationListener implementation.
95 invalidation::InvalidationClient
* client
) OVERRIDE
;
96 virtual void Invalidate(
97 invalidation::InvalidationClient
* client
,
98 const invalidation::Invalidation
& invalidation
,
99 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
100 virtual void InvalidateUnknownVersion(
101 invalidation::InvalidationClient
* client
,
102 const invalidation::ObjectId
& object_id
,
103 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
104 virtual void InvalidateAll(
105 invalidation::InvalidationClient
* client
,
106 const invalidation::AckHandle
& ack_handle
) OVERRIDE
;
107 virtual void InformRegistrationStatus(
108 invalidation::InvalidationClient
* client
,
109 const invalidation::ObjectId
& object_id
,
110 invalidation::InvalidationListener::RegistrationState reg_state
) OVERRIDE
;
111 virtual void InformRegistrationFailure(
112 invalidation::InvalidationClient
* client
,
113 const invalidation::ObjectId
& object_id
,
115 const std::string
& error_message
) OVERRIDE
;
116 virtual void ReissueRegistrations(
117 invalidation::InvalidationClient
* client
,
118 const std::string
& prefix
,
119 int prefix_length
) OVERRIDE
;
120 virtual void InformError(
121 invalidation::InvalidationClient
* client
,
122 const invalidation::ErrorInfo
& error_info
) OVERRIDE
;
124 // AckHandler implementation.
125 virtual void Acknowledge(
126 const invalidation::ObjectId
& id
,
127 const syncer::AckHandle
& handle
) OVERRIDE
;
129 const invalidation::ObjectId
& id
,
130 const syncer::AckHandle
& handle
) OVERRIDE
;
132 // StateWriter implementation.
133 virtual void WriteState(const std::string
& state
) OVERRIDE
;
135 // SyncNetworkChannel::Observer implementation.
136 virtual void OnNetworkChannelStateChanged(
137 InvalidatorState invalidator_state
) OVERRIDE
;
139 void DoRegistrationUpdate();
141 void RequestDetailedStatus(
142 base::Callback
<void(const base::DictionaryValue
&)> callback
) const;
149 InvalidatorState
GetState() const;
151 void EmitStateChange();
153 // Sends invalidations to their appropriate destination.
155 // If there are no observers registered for them, they will be saved for
158 // If there are observers registered, they will be saved (to make sure we
159 // don't drop them until they've been acted on) and emitted to the observers.
160 void DispatchInvalidations(const ObjectIdInvalidationMap
& invalidations
);
162 // Saves invalidations.
164 // This call isn't synchronous so we can't guarantee these invalidations will
165 // be safely on disk by the end of the call, but it should ensure that the
166 // data makes it to disk eventually.
167 void SaveInvalidations(const ObjectIdInvalidationMap
& to_save
);
169 // Emits previously saved invalidations to their registered observers.
170 void EmitSavedInvalidations(const ObjectIdInvalidationMap
& to_emit
);
172 // Generate a Dictionary with all the debugging information.
173 scoped_ptr
<base::DictionaryValue
> CollectDebugData() const;
175 base::WeakPtr
<AckHandler
> AsWeakPtr();
177 scoped_ptr
<SyncNetworkChannel
> sync_network_channel_
;
178 SyncSystemResources sync_system_resources_
;
179 UnackedInvalidationsMap unacked_invalidations_map_
;
180 base::WeakPtr
<InvalidationStateTracker
> invalidation_state_tracker_
;
181 scoped_refptr
<base::SequencedTaskRunner
>
182 invalidation_state_tracker_task_runner_
;
184 scoped_ptr
<invalidation::InvalidationClient
> invalidation_client_
;
185 scoped_ptr
<RegistrationManager
> registration_manager_
;
186 // Stored to pass to |registration_manager_| on start.
187 ObjectIdSet registered_ids_
;
189 // The states of the ticl and the push client.
190 InvalidatorState ticl_state_
;
191 InvalidatorState push_client_state_
;
193 base::WeakPtrFactory
<SyncInvalidationListener
> weak_ptr_factory_
;
195 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener
);
198 } // namespace syncer
200 #endif // COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_