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. 7 8 #ifndef COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_ 9 #define COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_ 10 11 #include <string> 12 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" 27 28 namespace buzz { 29 class XmppTaskParentInterface; 30 } // namespace buzz 31 32 namespace notifier { 33 class PushClient; 34 } // namespace notifier 35 36 namespace syncer { 37 38 class ObjectIdInvalidationMap; 39 class RegistrationManager; 40 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; 56 57 class INVALIDATION_EXPORT_PRIVATE Delegate { 58 public: 59 virtual ~Delegate(); 60 61 virtual void OnInvalidate( 62 const ObjectIdInvalidationMap& invalidations) = 0; 63 64 virtual void OnInvalidatorStateChange(InvalidatorState state) = 0; 65 }; 66 67 explicit SyncInvalidationListener( 68 scoped_ptr<SyncNetworkChannel> network_channel); 69 70 // Calls Stop(). 71 virtual ~SyncInvalidationListener(); 72 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); 86 87 void UpdateCredentials(const std::string& email, const std::string& token); 88 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); 92 93 // invalidation::InvalidationListener implementation. 94 virtual void Ready( 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, 114 bool is_transient, 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; 123 124 // AckHandler implementation. 125 virtual void Acknowledge( 126 const invalidation::ObjectId& id, 127 const syncer::AckHandle& handle) OVERRIDE; 128 virtual void Drop( 129 const invalidation::ObjectId& id, 130 const syncer::AckHandle& handle) OVERRIDE; 131 132 // StateWriter implementation. 133 virtual void WriteState(const std::string& state) OVERRIDE; 134 135 // SyncNetworkChannel::Observer implementation. 136 virtual void OnNetworkChannelStateChanged( 137 InvalidatorState invalidator_state) OVERRIDE; 138 139 void DoRegistrationUpdate(); 140 141 void RequestDetailedStatus( 142 base::Callback<void(const base::DictionaryValue&)> callback) const; 143 144 void StopForTest(); 145 146 private: 147 void Stop(); 148 149 InvalidatorState GetState() const; 150 151 void EmitStateChange(); 152 153 // Sends invalidations to their appropriate destination. 154 // 155 // If there are no observers registered for them, they will be saved for 156 // later. 157 // 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); 161 162 // Saves invalidations. 163 // 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); 168 169 // Emits previously saved invalidations to their registered observers. 170 void EmitSavedInvalidations(const ObjectIdInvalidationMap& to_emit); 171 172 // Generate a Dictionary with all the debugging information. 173 scoped_ptr<base::DictionaryValue> CollectDebugData() const; 174 175 base::WeakPtr<AckHandler> AsWeakPtr(); 176 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_; 183 Delegate* delegate_; 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_; 188 189 // The states of the ticl and the push client. 190 InvalidatorState ticl_state_; 191 InvalidatorState push_client_state_; 192 193 base::WeakPtrFactory<SyncInvalidationListener> weak_ptr_factory_; 194 195 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); 196 }; 197 198 } // namespace syncer 199 200 #endif // COMPONENTS_INVALIDATION_SYNC_INVALIDATION_LISTENER_H_ 201