OLD | NEW |
(Empty) | |
| 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. |
| 4 // |
| 5 // A simple wrapper around invalidation::InvalidationClient that |
| 6 // handles all the startup/shutdown details and hookups. |
| 7 |
| 8 #ifndef SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
| 9 #define SYNC_NOTIFIER_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 "google/cacheinvalidation/include/invalidation-listener.h" |
| 20 #include "sync/base/sync_export.h" |
| 21 #include "sync/internal_api/public/util/weak_handle.h" |
| 22 #include "sync/notifier/ack_handler.h" |
| 23 #include "sync/notifier/invalidation_state_tracker.h" |
| 24 #include "sync/notifier/invalidator_state.h" |
| 25 #include "sync/notifier/state_writer.h" |
| 26 #include "sync/notifier/sync_system_resources.h" |
| 27 #include "sync/notifier/unacked_invalidation_set.h" |
| 28 |
| 29 namespace buzz { |
| 30 class XmppTaskParentInterface; |
| 31 } // namespace buzz |
| 32 |
| 33 namespace notifier { |
| 34 class PushClient; |
| 35 } // namespace notifier |
| 36 |
| 37 namespace syncer { |
| 38 |
| 39 class ObjectIdInvalidationMap; |
| 40 class RegistrationManager; |
| 41 |
| 42 // SyncInvalidationListener is not thread-safe and lives on the sync |
| 43 // thread. |
| 44 class SYNC_EXPORT_PRIVATE SyncInvalidationListener |
| 45 : public NON_EXPORTED_BASE(invalidation::InvalidationListener), |
| 46 public StateWriter, |
| 47 public SyncNetworkChannel::Observer, |
| 48 public AckHandler, |
| 49 public base::NonThreadSafe { |
| 50 public: |
| 51 typedef base::Callback<invalidation::InvalidationClient*( |
| 52 invalidation::SystemResources*, |
| 53 int, |
| 54 const invalidation::string&, |
| 55 const invalidation::string&, |
| 56 invalidation::InvalidationListener*)> CreateInvalidationClientCallback; |
| 57 |
| 58 class SYNC_EXPORT_PRIVATE Delegate { |
| 59 public: |
| 60 virtual ~Delegate(); |
| 61 |
| 62 virtual void OnInvalidate( |
| 63 const ObjectIdInvalidationMap& invalidations) = 0; |
| 64 |
| 65 virtual void OnInvalidatorStateChange(InvalidatorState state) = 0; |
| 66 }; |
| 67 |
| 68 explicit SyncInvalidationListener( |
| 69 scoped_ptr<SyncNetworkChannel> network_channel); |
| 70 |
| 71 // Calls Stop(). |
| 72 virtual ~SyncInvalidationListener(); |
| 73 |
| 74 // Does not take ownership of |delegate| or |state_writer|. |
| 75 // |invalidation_state_tracker| must be initialized. |
| 76 void Start( |
| 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, |
| 83 Delegate* delegate); |
| 84 |
| 85 void UpdateCredentials(const std::string& email, const std::string& token); |
| 86 |
| 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); |
| 90 |
| 91 // invalidation::InvalidationListener implementation. |
| 92 virtual void Ready( |
| 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, |
| 112 bool is_transient, |
| 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; |
| 121 |
| 122 // AckHandler implementation. |
| 123 virtual void Acknowledge( |
| 124 const invalidation::ObjectId& id, |
| 125 const syncer::AckHandle& handle) OVERRIDE; |
| 126 virtual void Drop( |
| 127 const invalidation::ObjectId& id, |
| 128 const syncer::AckHandle& handle) OVERRIDE; |
| 129 |
| 130 // StateWriter implementation. |
| 131 virtual void WriteState(const std::string& state) OVERRIDE; |
| 132 |
| 133 // SyncNetworkChannel::Observer implementation. |
| 134 virtual void OnNetworkChannelStateChanged( |
| 135 InvalidatorState invalidator_state) OVERRIDE; |
| 136 |
| 137 void DoRegistrationUpdate(); |
| 138 |
| 139 void RequestDetailedStatus( |
| 140 base::Callback<void(const base::DictionaryValue&)> callback) const; |
| 141 |
| 142 void StopForTest(); |
| 143 |
| 144 private: |
| 145 void Stop(); |
| 146 |
| 147 InvalidatorState GetState() const; |
| 148 |
| 149 void EmitStateChange(); |
| 150 |
| 151 // Sends invalidations to their appropriate destination. |
| 152 // |
| 153 // If there are no observers registered for them, they will be saved for |
| 154 // later. |
| 155 // |
| 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); |
| 159 |
| 160 // Saves invalidations. |
| 161 // |
| 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); |
| 166 |
| 167 // Emits previously saved invalidations to their registered observers. |
| 168 void EmitSavedInvalidations(const ObjectIdInvalidationMap& to_emit); |
| 169 |
| 170 // Generate a Dictionary with all the debugging information. |
| 171 scoped_ptr<base::DictionaryValue> CollectDebugData() const; |
| 172 |
| 173 WeakHandle<AckHandler> GetThisAsAckHandler(); |
| 174 |
| 175 scoped_ptr<SyncNetworkChannel> sync_network_channel_; |
| 176 SyncSystemResources sync_system_resources_; |
| 177 UnackedInvalidationsMap unacked_invalidations_map_; |
| 178 WeakHandle<InvalidationStateTracker> invalidation_state_tracker_; |
| 179 Delegate* delegate_; |
| 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_; |
| 184 |
| 185 // The states of the ticl and the push client. |
| 186 InvalidatorState ticl_state_; |
| 187 InvalidatorState push_client_state_; |
| 188 |
| 189 base::WeakPtrFactory<SyncInvalidationListener> weak_ptr_factory_; |
| 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); |
| 192 }; |
| 193 |
| 194 } // namespace syncer |
| 195 |
| 196 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
OLD | NEW |