Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <map> | |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 14 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
| 15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 18 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
| 20 #include "base/time.h" | |
| 21 #include "base/timer.h" | |
| 19 #include "google/cacheinvalidation/include/invalidation-listener.h" | 22 #include "google/cacheinvalidation/include/invalidation-listener.h" |
| 20 #include "jingle/notifier/listener/push_client_observer.h" | 23 #include "jingle/notifier/listener/push_client_observer.h" |
| 21 #include "sync/internal_api/public/util/weak_handle.h" | 24 #include "sync/internal_api/public/util/weak_handle.h" |
| 22 #include "sync/notifier/invalidation_state_tracker.h" | 25 #include "sync/notifier/invalidation_state_tracker.h" |
| 23 #include "sync/notifier/invalidator_state.h" | 26 #include "sync/notifier/invalidator_state.h" |
| 24 #include "sync/notifier/object_id_invalidation_map.h" | 27 #include "sync/notifier/object_id_invalidation_map.h" |
| 25 #include "sync/notifier/state_writer.h" | 28 #include "sync/notifier/state_writer.h" |
| 26 #include "sync/notifier/sync_system_resources.h" | 29 #include "sync/notifier/sync_system_resources.h" |
| 27 | 30 |
| 28 namespace buzz { | 31 namespace buzz { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 // Calls Stop(). | 71 // Calls Stop(). |
| 69 virtual ~SyncInvalidationListener(); | 72 virtual ~SyncInvalidationListener(); |
| 70 | 73 |
| 71 // Does not take ownership of |delegate| or |state_writer|. | 74 // Does not take ownership of |delegate| or |state_writer|. |
| 72 // |invalidation_state_tracker| must be initialized. | 75 // |invalidation_state_tracker| must be initialized. |
| 73 void Start( | 76 void Start( |
| 74 const CreateInvalidationClientCallback& | 77 const CreateInvalidationClientCallback& |
| 75 create_invalidation_client_callback, | 78 create_invalidation_client_callback, |
| 76 const std::string& client_id, const std::string& client_info, | 79 const std::string& client_id, const std::string& client_info, |
| 77 const std::string& invalidation_bootstrap_data, | 80 const std::string& invalidation_bootstrap_data, |
| 78 const InvalidationVersionMap& initial_max_invalidation_versions, | 81 const InvalidationStateMap& initial_invalidation_state_map, |
| 79 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 82 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 80 Delegate* delegate); | 83 Delegate* delegate); |
| 81 | 84 |
| 82 void UpdateCredentials(const std::string& email, const std::string& token); | 85 void UpdateCredentials(const std::string& email, const std::string& token); |
| 83 | 86 |
| 84 // Update the set of object IDs that we're interested in getting | 87 // Update the set of object IDs that we're interested in getting |
| 85 // notifications for. May be called at any time. | 88 // notifications for. May be called at any time. |
| 86 void UpdateRegisteredIds(const ObjectIdSet& ids); | 89 void UpdateRegisteredIds(const ObjectIdSet& ids); |
| 90 // TODO(dcheng): Add a comment. | |
|
akalin
2012/10/19 13:27:16
!
dcheng
2012/10/19 19:38:11
Done.
| |
| 91 void Acknowledge(const invalidation::ObjectId& id, | |
| 92 const AckHandle& ack_handle); | |
| 87 | 93 |
| 88 // invalidation::InvalidationListener implementation. | 94 // invalidation::InvalidationListener implementation. |
| 89 virtual void Ready( | 95 virtual void Ready( |
| 90 invalidation::InvalidationClient* client) OVERRIDE; | 96 invalidation::InvalidationClient* client) OVERRIDE; |
| 91 virtual void Invalidate( | 97 virtual void Invalidate( |
| 92 invalidation::InvalidationClient* client, | 98 invalidation::InvalidationClient* client, |
| 93 const invalidation::Invalidation& invalidation, | 99 const invalidation::Invalidation& invalidation, |
| 94 const invalidation::AckHandle& ack_handle) OVERRIDE; | 100 const invalidation::AckHandle& ack_handle) OVERRIDE; |
| 95 virtual void InvalidateUnknownVersion( | 101 virtual void InvalidateUnknownVersion( |
| 96 invalidation::InvalidationClient* client, | 102 invalidation::InvalidationClient* client, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 119 // StateWriter implementation. | 125 // StateWriter implementation. |
| 120 virtual void WriteState(const std::string& state) OVERRIDE; | 126 virtual void WriteState(const std::string& state) OVERRIDE; |
| 121 | 127 |
| 122 // notifier::PushClientObserver implementation. | 128 // notifier::PushClientObserver implementation. |
| 123 virtual void OnNotificationsEnabled() OVERRIDE; | 129 virtual void OnNotificationsEnabled() OVERRIDE; |
| 124 virtual void OnNotificationsDisabled( | 130 virtual void OnNotificationsDisabled( |
| 125 notifier::NotificationsDisabledReason reason) OVERRIDE; | 131 notifier::NotificationsDisabledReason reason) OVERRIDE; |
| 126 virtual void OnIncomingNotification( | 132 virtual void OnIncomingNotification( |
| 127 const notifier::Notification& notification) OVERRIDE; | 133 const notifier::Notification& notification) OVERRIDE; |
| 128 | 134 |
| 129 void StopForTest(); | |
| 130 | |
| 131 void DoRegistrationUpdate(); | 135 void DoRegistrationUpdate(); |
| 132 | 136 |
| 137 void StopForTest(); | |
| 138 // Immediately triggers notifications for unacknowledged invalidations | |
| 139 // at the beginning of the queue. |next_invalidation_time| is set to the | |
| 140 // expiration time of the first entry in the queue, all unacknowledged | |
| 141 // invalidations with the same expiration time are immediately triggered, | |
| 142 // and we return true. | |
| 143 // If there are no unacknowledged invalidations, then we do nothing and | |
| 144 // simply return false. | |
| 145 bool TriggerNextTimeoutForTest(base::TimeTicks* next_invalidation_time); | |
| 146 base::TimeDelta GetCurrentDelayForTest() const { | |
| 147 return timer_.GetCurrentDelay(); | |
| 148 } | |
| 149 InvalidationStateMap GetStateMapForTest() const { | |
| 150 return invalidation_state_map_; | |
| 151 } | |
| 152 | |
| 133 private: | 153 private: |
| 154 struct QueueEntry { | |
| 155 QueueEntry(const invalidation::ObjectId& id, | |
| 156 const std::string& payload, | |
| 157 int retry_count) | |
| 158 : id(id), payload(payload), retry_count(retry_count) { | |
| 159 } | |
| 160 | |
| 161 invalidation::ObjectId id; | |
| 162 std::string payload; | |
| 163 int retry_count; | |
| 164 }; | |
| 165 typedef std::multimap<base::TimeTicks, QueueEntry> TimerQueue; | |
| 166 | |
| 134 void Stop(); | 167 void Stop(); |
| 135 | 168 |
| 136 InvalidatorState GetState() const; | 169 InvalidatorState GetState() const; |
| 137 | 170 |
| 138 void EmitStateChange(); | 171 void EmitStateChange(); |
| 139 | 172 |
| 140 void EmitInvalidation(const ObjectIdInvalidationMap& invalidation_map); | 173 void PrepareInvalidation(const ObjectIdSet& ids, |
|
akalin
2012/10/19 13:27:16
I feel we want to decomp out the queue-managing lo
dcheng
2012/10/19 19:38:11
I didn't do it originally because there were alrea
| |
| 174 const std::string& payload, | |
| 175 invalidation::InvalidationClient* client, | |
| 176 const invalidation::AckHandle& ack_handle); | |
| 177 void EmitInvalidation(const ObjectIdSet& ids, | |
| 178 const std::string& payload, | |
| 179 invalidation::InvalidationClient* client, | |
| 180 const invalidation::AckHandle& ack_handle, | |
| 181 const AckHandleMap& local_ack_handles); | |
| 182 void ResendUnacknowledgedInvalidations(); | |
| 183 void ResendUnacknowledgedInvalidationsAt(base::TimeTicks now); | |
| 184 | |
| 185 // Various helpers to manage the queue of items that have pending acks. | |
| 186 void InsertId(base::TimeTicks expiration_time, | |
| 187 const invalidation::ObjectId& id, | |
| 188 const std::string& payload, | |
| 189 int retry_count); | |
| 190 void RemoveId(const invalidation::ObjectId& id); | |
| 191 void RemoveIds(const ObjectIdSet& ids); | |
| 192 QueueEntry Pop(); | |
| 193 void UpdateTimer(base::TimeTicks now); | |
| 194 | |
| 195 base::WeakPtrFactory<SyncInvalidationListener> weak_ptr_factory_; | |
| 196 base::OneShotTimer<SyncInvalidationListener> timer_; | |
| 197 TimerQueue timer_queue_; | |
| 141 | 198 |
| 142 // Owned by |sync_system_resources_|. | 199 // Owned by |sync_system_resources_|. |
| 143 notifier::PushClient* const push_client_; | 200 notifier::PushClient* const push_client_; |
| 144 SyncSystemResources sync_system_resources_; | 201 SyncSystemResources sync_system_resources_; |
| 145 InvalidationVersionMap max_invalidation_versions_; | 202 InvalidationStateMap invalidation_state_map_; |
| 146 WeakHandle<InvalidationStateTracker> invalidation_state_tracker_; | 203 WeakHandle<InvalidationStateTracker> invalidation_state_tracker_; |
| 147 Delegate* delegate_; | 204 Delegate* delegate_; |
| 148 scoped_ptr<invalidation::InvalidationClient> invalidation_client_; | 205 scoped_ptr<invalidation::InvalidationClient> invalidation_client_; |
| 149 scoped_ptr<RegistrationManager> registration_manager_; | 206 scoped_ptr<RegistrationManager> registration_manager_; |
| 150 // Stored to pass to |registration_manager_| on start. | 207 // Stored to pass to |registration_manager_| on start. |
| 151 ObjectIdSet registered_ids_; | 208 ObjectIdSet registered_ids_; |
| 152 | 209 |
| 153 // The states of the ticl and the push client. | 210 // The states of the ticl and the push client. |
| 154 InvalidatorState ticl_state_; | 211 InvalidatorState ticl_state_; |
| 155 InvalidatorState push_client_state_; | 212 InvalidatorState push_client_state_; |
| 156 | 213 |
| 157 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); | 214 DISALLOW_COPY_AND_ASSIGN(SyncInvalidationListener); |
| 158 }; | 215 }; |
| 159 | 216 |
| 160 } // namespace syncer | 217 } // namespace syncer |
| 161 | 218 |
| 162 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ | 219 #endif // SYNC_NOTIFIER_SYNC_INVALIDATION_LISTENER_H_ |
| OLD | NEW |