| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A simple wrapper around invalidation::InvalidationClient that | 5 // A simple wrapper around invalidation::InvalidationClient that |
| 6 // handles all the startup/shutdown details and hookups. | 6 // handles all the startup/shutdown details and hookups. |
| 7 | 7 |
| 8 #ifndef SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ | 8 #ifndef SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
| 9 #define SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ | 9 #define SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/threading/non_thread_safe.h" | 18 #include "base/threading/non_thread_safe.h" |
| 19 #include "google/cacheinvalidation/include/invalidation-listener.h" | 19 #include "google/cacheinvalidation/include/invalidation-listener.h" |
| 20 #include "jingle/notifier/listener/push_client_observer.h" | 20 #include "jingle/notifier/listener/push_client_observer.h" |
| 21 #include "sync/base/sync_export.h" | 21 #include "sync/base/sync_export.h" |
| 22 #include "sync/internal_api/public/util/weak_handle.h" | 22 #include "sync/internal_api/public/util/weak_handle.h" |
| 23 #include "sync/notifier/ack_tracker.h" | 23 #include "sync/notifier/ack_handler.h" |
| 24 #include "sync/notifier/invalidation_state_tracker.h" | 24 #include "sync/notifier/invalidation_state_tracker.h" |
| 25 #include "sync/notifier/invalidator_state.h" | 25 #include "sync/notifier/invalidator_state.h" |
| 26 #include "sync/notifier/object_id_invalidation_map.h" | |
| 27 #include "sync/notifier/state_writer.h" | 26 #include "sync/notifier/state_writer.h" |
| 28 #include "sync/notifier/sync_system_resources.h" | 27 #include "sync/notifier/sync_system_resources.h" |
| 29 | 28 #include "sync/notifier/unacked_invalidation_set.h" |
| 30 namespace base { | |
| 31 class TickClock; | |
| 32 } // namespace base | |
| 33 | 29 |
| 34 namespace buzz { | 30 namespace buzz { |
| 35 class XmppTaskParentInterface; | 31 class XmppTaskParentInterface; |
| 36 } // namespace buzz | 32 } // namespace buzz |
| 37 | 33 |
| 38 namespace notifier { | 34 namespace notifier { |
| 39 class PushClient; | 35 class PushClient; |
| 40 } // namespace notifier | 36 } // namespace notifier |
| 41 | 37 |
| 42 namespace syncer { | 38 namespace syncer { |
| 43 | 39 |
| 40 class ObjectIdInvalidationMap; |
| 44 class RegistrationManager; | 41 class RegistrationManager; |
| 45 | 42 |
| 46 // SyncInvalidationListener is not thread-safe and lives on the sync | 43 // SyncInvalidationListener is not thread-safe and lives on the sync |
| 47 // thread. | 44 // thread. |
| 48 class SYNC_EXPORT_PRIVATE SyncInvalidationListener | 45 class SYNC_EXPORT_PRIVATE SyncInvalidationListener |
| 49 : public NON_EXPORTED_BASE(invalidation::InvalidationListener), | 46 : public NON_EXPORTED_BASE(invalidation::InvalidationListener), |
| 50 public StateWriter, | 47 public StateWriter, |
| 51 public NON_EXPORTED_BASE(notifier::PushClientObserver), | 48 public NON_EXPORTED_BASE(notifier::PushClientObserver), |
| 52 public base::NonThreadSafe, | 49 public AckHandler, |
| 53 public AckTracker::Delegate { | 50 public base::NonThreadSafe { |
| 54 public: | 51 public: |
| 55 typedef base::Callback<invalidation::InvalidationClient*( | 52 typedef base::Callback<invalidation::InvalidationClient*( |
| 56 invalidation::SystemResources*, | 53 invalidation::SystemResources*, |
| 57 int, | 54 int, |
| 58 const invalidation::string&, | 55 const invalidation::string&, |
| 59 const invalidation::string&, | 56 const invalidation::string&, |
| 60 invalidation::InvalidationListener*)> CreateInvalidationClientCallback; | 57 invalidation::InvalidationListener*)> CreateInvalidationClientCallback; |
| 61 | 58 |
| 62 class SYNC_EXPORT_PRIVATE Delegate { | 59 class SYNC_EXPORT_PRIVATE Delegate { |
| 63 public: | 60 public: |
| 64 virtual ~Delegate(); | 61 virtual ~Delegate(); |
| 65 | 62 |
| 66 virtual void OnInvalidate( | 63 virtual void OnInvalidate( |
| 67 const ObjectIdInvalidationMap& invalidation_map) = 0; | 64 const ObjectIdInvalidationMap& invalidations) = 0; |
| 68 | 65 |
| 69 virtual void OnInvalidatorStateChange(InvalidatorState state) = 0; | 66 virtual void OnInvalidatorStateChange(InvalidatorState state) = 0; |
| 70 }; | 67 }; |
| 71 | 68 |
| 72 explicit SyncInvalidationListener( | 69 explicit SyncInvalidationListener( |
| 73 base::TickClock* tick_clock, | |
| 74 scoped_ptr<notifier::PushClient> push_client); | 70 scoped_ptr<notifier::PushClient> push_client); |
| 75 | 71 |
| 76 // Calls Stop(). | 72 // Calls Stop(). |
| 77 virtual ~SyncInvalidationListener(); | 73 virtual ~SyncInvalidationListener(); |
| 78 | 74 |
| 79 // Does not take ownership of |delegate| or |state_writer|. | 75 // Does not take ownership of |delegate| or |state_writer|. |
| 80 // |invalidation_state_tracker| must be initialized. | 76 // |invalidation_state_tracker| must be initialized. |
| 81 void Start( | 77 void Start( |
| 82 const CreateInvalidationClientCallback& | 78 const CreateInvalidationClientCallback& |
| 83 create_invalidation_client_callback, | 79 create_invalidation_client_callback, |
| 84 const std::string& client_id, const std::string& client_info, | 80 const std::string& client_id, const std::string& client_info, |
| 85 const std::string& invalidation_bootstrap_data, | 81 const std::string& invalidation_bootstrap_data, |
| 86 const InvalidationStateMap& initial_invalidation_state_map, | 82 const UnackedInvalidationsMap& initial_object_states, |
| 87 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 83 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 88 Delegate* delegate); | 84 Delegate* delegate); |
| 89 | 85 |
| 90 void UpdateCredentials(const std::string& email, const std::string& token); | 86 void UpdateCredentials(const std::string& email, const std::string& token); |
| 91 | 87 |
| 92 // Update the set of object IDs that we're interested in getting | 88 // Update the set of object IDs that we're interested in getting |
| 93 // notifications for. May be called at any time. | 89 // notifications for. May be called at any time. |
| 94 void UpdateRegisteredIds(const ObjectIdSet& ids); | 90 void UpdateRegisteredIds(const ObjectIdSet& ids); |
| 95 // Acknowledge that an invalidation for |id| was handled. | |
| 96 void Acknowledge(const invalidation::ObjectId& id, | |
| 97 const AckHandle& ack_handle); | |
| 98 | 91 |
| 99 // invalidation::InvalidationListener implementation. | 92 // invalidation::InvalidationListener implementation. |
| 100 virtual void Ready( | 93 virtual void Ready( |
| 101 invalidation::InvalidationClient* client) OVERRIDE; | 94 invalidation::InvalidationClient* client) OVERRIDE; |
| 102 virtual void Invalidate( | 95 virtual void Invalidate( |
| 103 invalidation::InvalidationClient* client, | 96 invalidation::InvalidationClient* client, |
| 104 const invalidation::Invalidation& invalidation, | 97 const invalidation::Invalidation& invalidation, |
| 105 const invalidation::AckHandle& ack_handle) OVERRIDE; | 98 const invalidation::AckHandle& ack_handle) OVERRIDE; |
| 106 virtual void InvalidateUnknownVersion( | 99 virtual void InvalidateUnknownVersion( |
| 107 invalidation::InvalidationClient* client, | 100 invalidation::InvalidationClient* client, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 bool is_transient, | 113 bool is_transient, |
| 121 const std::string& error_message) OVERRIDE; | 114 const std::string& error_message) OVERRIDE; |
| 122 virtual void ReissueRegistrations( | 115 virtual void ReissueRegistrations( |
| 123 invalidation::InvalidationClient* client, | 116 invalidation::InvalidationClient* client, |
| 124 const std::string& prefix, | 117 const std::string& prefix, |
| 125 int prefix_length) OVERRIDE; | 118 int prefix_length) OVERRIDE; |
| 126 virtual void InformError( | 119 virtual void InformError( |
| 127 invalidation::InvalidationClient* client, | 120 invalidation::InvalidationClient* client, |
| 128 const invalidation::ErrorInfo& error_info) OVERRIDE; | 121 const invalidation::ErrorInfo& error_info) OVERRIDE; |
| 129 | 122 |
| 123 // AckHandler implementation. |
| 124 virtual void Acknowledge( |
| 125 const invalidation::ObjectId& id, |
| 126 const syncer::AckHandle& handle) OVERRIDE; |
| 127 virtual void Drop( |
| 128 const invalidation::ObjectId& id, |
| 129 const syncer::AckHandle& handle) OVERRIDE; |
| 130 |
| 130 // StateWriter implementation. | 131 // StateWriter implementation. |
| 131 virtual void WriteState(const std::string& state) OVERRIDE; | 132 virtual void WriteState(const std::string& state) OVERRIDE; |
| 132 | 133 |
| 133 // notifier::PushClientObserver implementation. | 134 // notifier::PushClientObserver implementation. |
| 134 virtual void OnNotificationsEnabled() OVERRIDE; | 135 virtual void OnNotificationsEnabled() OVERRIDE; |
| 135 virtual void OnNotificationsDisabled( | 136 virtual void OnNotificationsDisabled( |
| 136 notifier::NotificationsDisabledReason reason) OVERRIDE; | 137 notifier::NotificationsDisabledReason reason) OVERRIDE; |
| 137 virtual void OnIncomingNotification( | 138 virtual void OnIncomingNotification( |
| 138 const notifier::Notification& notification) OVERRIDE; | 139 const notifier::Notification& notification) OVERRIDE; |
| 139 | 140 |
| 140 void DoRegistrationUpdate(); | 141 void DoRegistrationUpdate(); |
| 141 | 142 |
| 142 void StopForTest(); | 143 void StopForTest(); |
| 143 InvalidationStateMap GetStateMapForTest() const; | |
| 144 AckTracker* GetAckTrackerForTest(); | |
| 145 | 144 |
| 146 private: | 145 private: |
| 147 void Stop(); | 146 void Stop(); |
| 148 | 147 |
| 149 InvalidatorState GetState() const; | 148 InvalidatorState GetState() const; |
| 150 | 149 |
| 151 void EmitStateChange(); | 150 void EmitStateChange(); |
| 151 void DispatchInvalidations(const ObjectIdInvalidationMap& invalidations); |
| 152 void SaveInvalidations(const ObjectIdInvalidationMap& to_save); |
| 153 void EmitInvalidations(const ObjectIdInvalidationMap& to_emit); |
| 152 | 154 |
| 153 void PrepareInvalidation(const ObjectIdSet& ids, | 155 WeakHandle<AckHandler> GetThisAsAckHandler(); |
| 154 int64 version, | |
| 155 const std::string& payload, | |
| 156 invalidation::InvalidationClient* client, | |
| 157 const invalidation::AckHandle& ack_handle); | |
| 158 void EmitInvalidation(const ObjectIdSet& ids, | |
| 159 int64 version, | |
| 160 const std::string& payload, | |
| 161 invalidation::InvalidationClient* client, | |
| 162 const invalidation::AckHandle& ack_handle, | |
| 163 const AckHandleMap& local_ack_handles); | |
| 164 | |
| 165 // AckTracker::Delegate implementation. | |
| 166 virtual void OnTimeout(const ObjectIdSet& ids) OVERRIDE; | |
| 167 | |
| 168 AckTracker ack_tracker_; | |
| 169 | 156 |
| 170 // Owned by |sync_system_resources_|. | 157 // Owned by |sync_system_resources_|. |
| 171 notifier::PushClient* const push_client_; | 158 notifier::PushClient* const push_client_; |
| 172 SyncSystemResources sync_system_resources_; | 159 SyncSystemResources sync_system_resources_; |
| 173 InvalidationStateMap invalidation_state_map_; | 160 UnackedInvalidationsMap unacked_invalidations_map_; |
| 174 WeakHandle<InvalidationStateTracker> invalidation_state_tracker_; | 161 WeakHandle<InvalidationStateTracker> invalidation_state_tracker_; |
| 175 Delegate* delegate_; | 162 Delegate* delegate_; |
| 176 scoped_ptr<invalidation::InvalidationClient> invalidation_client_; | 163 scoped_ptr<invalidation::InvalidationClient> invalidation_client_; |
| 177 scoped_ptr<RegistrationManager> registration_manager_; | 164 scoped_ptr<RegistrationManager> registration_manager_; |
| 178 // Stored to pass to |registration_manager_| on start. | 165 // Stored to pass to |registration_manager_| on start. |
| 179 ObjectIdSet registered_ids_; | 166 ObjectIdSet registered_ids_; |
| 180 | 167 |
| 181 // The states of the ticl and the push client. | 168 // The states of the ticl and the push client. |
| 182 InvalidatorState ticl_state_; | 169 InvalidatorState ticl_state_; |
| 183 InvalidatorState push_client_state_; | 170 InvalidatorState push_client_state_; |
| 184 | 171 |
| 185 base::WeakPtrFactory<SyncInvalidationListener> weak_ptr_factory_; | 172 base::WeakPtrFactory<SyncInvalidationListener> weak_ptr_factory_; |
| 186 | 173 |
| 187 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); | 174 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); |
| 188 }; | 175 }; |
| 189 | 176 |
| 190 } // namespace syncer | 177 } // namespace syncer |
| 191 | 178 |
| 192 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ | 179 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
| OLD | NEW |