Cancel on-going scan when change directory is requested.
[chromium-blink-merge.git] / components / invalidation / sync_invalidation_listener.h
blob37fa7f104757160a2b479439312f2ef635baaeda
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.
4 //
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_
11 #include <string>
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"
28 namespace buzz {
29 class XmppTaskParentInterface;
30 } // namespace buzz
32 namespace notifier {
33 class PushClient;
34 } // namespace notifier
36 namespace syncer {
38 class ObjectIdInvalidationMap;
39 class RegistrationManager;
41 // SyncInvalidationListener is not thread-safe and lives on the sync
42 // thread.
43 class INVALIDATION_EXPORT_PRIVATE SyncInvalidationListener
44 : public NON_EXPORTED_BASE(invalidation::InvalidationListener),
45 public StateWriter,
46 public SyncNetworkChannel::Observer,
47 public AckHandler,
48 public base::NonThreadSafe {
49 public:
50 typedef base::Callback<invalidation::InvalidationClient*(
51 invalidation::SystemResources*,
52 int,
53 const invalidation::string&,
54 const invalidation::string&,
55 invalidation::InvalidationListener*)> CreateInvalidationClientCallback;
57 class INVALIDATION_EXPORT_PRIVATE Delegate {
58 public:
59 virtual ~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);
70 // Calls Stop().
71 ~SyncInvalidationListener() override;
73 // Does not take ownership of |delegate| or |state_writer|.
74 // |invalidation_state_tracker| must be initialized.
75 void Start(
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,
85 Delegate* delegate);
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.
94 void Ready(invalidation::InvalidationClient* client) override;
95 void Invalidate(invalidation::InvalidationClient* client,
96 const invalidation::Invalidation& invalidation,
97 const invalidation::AckHandle& ack_handle) override;
98 void InvalidateUnknownVersion(
99 invalidation::InvalidationClient* client,
100 const invalidation::ObjectId& object_id,
101 const invalidation::AckHandle& ack_handle) override;
102 void InvalidateAll(invalidation::InvalidationClient* client,
103 const invalidation::AckHandle& ack_handle) override;
104 void InformRegistrationStatus(
105 invalidation::InvalidationClient* client,
106 const invalidation::ObjectId& object_id,
107 invalidation::InvalidationListener::RegistrationState reg_state) override;
108 void InformRegistrationFailure(invalidation::InvalidationClient* client,
109 const invalidation::ObjectId& object_id,
110 bool is_transient,
111 const std::string& error_message) override;
112 void ReissueRegistrations(invalidation::InvalidationClient* client,
113 const std::string& prefix,
114 int prefix_length) override;
115 void InformError(invalidation::InvalidationClient* client,
116 const invalidation::ErrorInfo& error_info) override;
118 // AckHandler implementation.
119 void Acknowledge(const invalidation::ObjectId& id,
120 const syncer::AckHandle& handle) override;
121 void Drop(const invalidation::ObjectId& id,
122 const syncer::AckHandle& handle) override;
124 // StateWriter implementation.
125 void WriteState(const std::string& state) override;
127 // SyncNetworkChannel::Observer implementation.
128 void OnNetworkChannelStateChanged(
129 InvalidatorState invalidator_state) override;
131 void DoRegistrationUpdate();
133 void RequestDetailedStatus(
134 base::Callback<void(const base::DictionaryValue&)> callback) const;
136 void StopForTest();
138 private:
139 void Stop();
141 InvalidatorState GetState() const;
143 void EmitStateChange();
145 // Sends invalidations to their appropriate destination.
147 // If there are no observers registered for them, they will be saved for
148 // later.
150 // If there are observers registered, they will be saved (to make sure we
151 // don't drop them until they've been acted on) and emitted to the observers.
152 void DispatchInvalidations(const ObjectIdInvalidationMap& invalidations);
154 // Saves invalidations.
156 // This call isn't synchronous so we can't guarantee these invalidations will
157 // be safely on disk by the end of the call, but it should ensure that the
158 // data makes it to disk eventually.
159 void SaveInvalidations(const ObjectIdInvalidationMap& to_save);
161 // Emits previously saved invalidations to their registered observers.
162 void EmitSavedInvalidations(const ObjectIdInvalidationMap& to_emit);
164 // Generate a Dictionary with all the debugging information.
165 scoped_ptr<base::DictionaryValue> CollectDebugData() const;
167 base::WeakPtr<AckHandler> AsWeakPtr();
169 scoped_ptr<SyncNetworkChannel> sync_network_channel_;
170 SyncSystemResources sync_system_resources_;
171 UnackedInvalidationsMap unacked_invalidations_map_;
172 base::WeakPtr<InvalidationStateTracker> invalidation_state_tracker_;
173 scoped_refptr<base::SequencedTaskRunner>
174 invalidation_state_tracker_task_runner_;
175 Delegate* delegate_;
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 // COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_