OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/invalidation/impl/registration_manager.h" | 5 #include "components/invalidation/impl/registration_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cstddef> | 10 #include <cstddef> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // 1 hour. | 58 // 1 hour. |
59 const int RegistrationManager::kMaxRegistrationDelaySeconds = 60 * 60; | 59 const int RegistrationManager::kMaxRegistrationDelaySeconds = 60 * 60; |
60 | 60 |
61 RegistrationManager::RegistrationManager( | 61 RegistrationManager::RegistrationManager( |
62 invalidation::InvalidationClient* invalidation_client) | 62 invalidation::InvalidationClient* invalidation_client) |
63 : invalidation_client_(invalidation_client) { | 63 : invalidation_client_(invalidation_client) { |
64 DCHECK(invalidation_client_); | 64 DCHECK(invalidation_client_); |
65 } | 65 } |
66 | 66 |
67 RegistrationManager::~RegistrationManager() { | 67 RegistrationManager::~RegistrationManager() { |
68 DCHECK(CalledOnValidThread()); | 68 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
69 } | 69 } |
70 | 70 |
71 ObjectIdSet RegistrationManager::UpdateRegisteredIds(const ObjectIdSet& ids) { | 71 ObjectIdSet RegistrationManager::UpdateRegisteredIds(const ObjectIdSet& ids) { |
72 DCHECK(CalledOnValidThread()); | 72 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
73 | 73 |
74 const ObjectIdSet& old_ids = GetRegisteredIds(); | 74 const ObjectIdSet& old_ids = GetRegisteredIds(); |
75 const ObjectIdSet& to_register = ids; | 75 const ObjectIdSet& to_register = ids; |
76 ObjectIdSet to_unregister; | 76 ObjectIdSet to_unregister; |
77 std::set_difference(old_ids.begin(), old_ids.end(), | 77 std::set_difference(old_ids.begin(), old_ids.end(), |
78 ids.begin(), ids.end(), | 78 ids.begin(), ids.end(), |
79 std::inserter(to_unregister, to_unregister.begin()), | 79 std::inserter(to_unregister, to_unregister.begin()), |
80 ObjectIdLessThan()); | 80 ObjectIdLessThan()); |
81 | 81 |
82 for (ObjectIdSet::const_iterator it = to_unregister.begin(); | 82 for (ObjectIdSet::const_iterator it = to_unregister.begin(); |
(...skipping 10 matching lines...) Expand all Loading... |
93 if (!IsIdRegistered(*it)) { | 93 if (!IsIdRegistered(*it)) { |
94 TryRegisterId(*it, false /* is-retry */); | 94 TryRegisterId(*it, false /* is-retry */); |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 return to_unregister; | 98 return to_unregister; |
99 } | 99 } |
100 | 100 |
101 void RegistrationManager::MarkRegistrationLost( | 101 void RegistrationManager::MarkRegistrationLost( |
102 const invalidation::ObjectId& id) { | 102 const invalidation::ObjectId& id) { |
103 DCHECK(CalledOnValidThread()); | 103 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
104 auto it = registration_statuses_.find(id); | 104 auto it = registration_statuses_.find(id); |
105 if (it == registration_statuses_.end()) { | 105 if (it == registration_statuses_.end()) { |
106 DVLOG(1) << "Attempt to mark non-existent registration for " | 106 DVLOG(1) << "Attempt to mark non-existent registration for " |
107 << ObjectIdToString(id) << " as lost"; | 107 << ObjectIdToString(id) << " as lost"; |
108 return; | 108 return; |
109 } | 109 } |
110 if (!it->second->enabled) { | 110 if (!it->second->enabled) { |
111 return; | 111 return; |
112 } | 112 } |
113 it->second->state = invalidation::InvalidationListener::UNREGISTERED; | 113 it->second->state = invalidation::InvalidationListener::UNREGISTERED; |
114 bool is_retry = !it->second->last_registration_request.is_null(); | 114 bool is_retry = !it->second->last_registration_request.is_null(); |
115 TryRegisterId(id, is_retry); | 115 TryRegisterId(id, is_retry); |
116 } | 116 } |
117 | 117 |
118 void RegistrationManager::MarkAllRegistrationsLost() { | 118 void RegistrationManager::MarkAllRegistrationsLost() { |
119 DCHECK(CalledOnValidThread()); | 119 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
120 for (auto it = registration_statuses_.begin(); | 120 for (auto it = registration_statuses_.begin(); |
121 it != registration_statuses_.end(); ++it) { | 121 it != registration_statuses_.end(); ++it) { |
122 if (IsIdRegistered(it->first)) { | 122 if (IsIdRegistered(it->first)) { |
123 MarkRegistrationLost(it->first); | 123 MarkRegistrationLost(it->first); |
124 } | 124 } |
125 } | 125 } |
126 } | 126 } |
127 | 127 |
128 void RegistrationManager::DisableId(const invalidation::ObjectId& id) { | 128 void RegistrationManager::DisableId(const invalidation::ObjectId& id) { |
129 DCHECK(CalledOnValidThread()); | 129 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
130 auto it = registration_statuses_.find(id); | 130 auto it = registration_statuses_.find(id); |
131 if (it == registration_statuses_.end()) { | 131 if (it == registration_statuses_.end()) { |
132 DVLOG(1) << "Attempt to disable non-existent registration for " | 132 DVLOG(1) << "Attempt to disable non-existent registration for " |
133 << ObjectIdToString(id); | 133 << ObjectIdToString(id); |
134 return; | 134 return; |
135 } | 135 } |
136 it->second->Disable(); | 136 it->second->Disable(); |
137 } | 137 } |
138 | 138 |
139 // static | 139 // static |
(...skipping 14 matching lines...) Expand all Loading... |
154 return std::max(min_retry_interval, | 154 return std::max(min_retry_interval, |
155 std::min(max_retry_interval, new_retry_interval)); | 155 std::min(max_retry_interval, new_retry_interval)); |
156 } | 156 } |
157 | 157 |
158 ObjectIdSet RegistrationManager::GetRegisteredIdsForTest() const { | 158 ObjectIdSet RegistrationManager::GetRegisteredIdsForTest() const { |
159 return GetRegisteredIds(); | 159 return GetRegisteredIds(); |
160 } | 160 } |
161 | 161 |
162 RegistrationManager::PendingRegistrationMap | 162 RegistrationManager::PendingRegistrationMap |
163 RegistrationManager::GetPendingRegistrationsForTest() const { | 163 RegistrationManager::GetPendingRegistrationsForTest() const { |
164 DCHECK(CalledOnValidThread()); | 164 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
165 PendingRegistrationMap pending_registrations; | 165 PendingRegistrationMap pending_registrations; |
166 for (const auto& status_pair : registration_statuses_) { | 166 for (const auto& status_pair : registration_statuses_) { |
167 const invalidation::ObjectId& id = status_pair.first; | 167 const invalidation::ObjectId& id = status_pair.first; |
168 RegistrationStatus* status = status_pair.second.get(); | 168 RegistrationStatus* status = status_pair.second.get(); |
169 if (status->registration_timer.IsRunning()) { | 169 if (status->registration_timer.IsRunning()) { |
170 pending_registrations[id].last_registration_request = | 170 pending_registrations[id].last_registration_request = |
171 status->last_registration_request; | 171 status->last_registration_request; |
172 pending_registrations[id].registration_attempt = | 172 pending_registrations[id].registration_attempt = |
173 status->last_registration_attempt; | 173 status->last_registration_attempt; |
174 pending_registrations[id].delay = status->delay; | 174 pending_registrations[id].delay = status->delay; |
175 pending_registrations[id].actual_delay = | 175 pending_registrations[id].actual_delay = |
176 status->registration_timer.GetCurrentDelay(); | 176 status->registration_timer.GetCurrentDelay(); |
177 } | 177 } |
178 } | 178 } |
179 return pending_registrations; | 179 return pending_registrations; |
180 } | 180 } |
181 | 181 |
182 void RegistrationManager::FirePendingRegistrationsForTest() { | 182 void RegistrationManager::FirePendingRegistrationsForTest() { |
183 DCHECK(CalledOnValidThread()); | 183 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
184 for (const auto& status_pair : registration_statuses_) { | 184 for (const auto& status_pair : registration_statuses_) { |
185 if (status_pair.second->registration_timer.IsRunning()) { | 185 if (status_pair.second->registration_timer.IsRunning()) { |
186 status_pair.second->DoRegister(); | 186 status_pair.second->DoRegister(); |
187 } | 187 } |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 double RegistrationManager::GetJitter() { | 191 double RegistrationManager::GetJitter() { |
192 // |jitter| lies in [-1.0, 1.0), which is low-biased, but only | 192 // |jitter| lies in [-1.0, 1.0), which is low-biased, but only |
193 // barely. | 193 // barely. |
194 // | 194 // |
195 // TODO(akalin): Fix the bias. | 195 // TODO(akalin): Fix the bias. |
196 return 2.0 * base::RandDouble() - 1.0; | 196 return 2.0 * base::RandDouble() - 1.0; |
197 } | 197 } |
198 | 198 |
199 void RegistrationManager::TryRegisterId(const invalidation::ObjectId& id, | 199 void RegistrationManager::TryRegisterId(const invalidation::ObjectId& id, |
200 bool is_retry) { | 200 bool is_retry) { |
201 DCHECK(CalledOnValidThread()); | 201 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
202 auto it = registration_statuses_.find(id); | 202 auto it = registration_statuses_.find(id); |
203 if (it == registration_statuses_.end()) { | 203 if (it == registration_statuses_.end()) { |
204 NOTREACHED() << "TryRegisterId called on " << ObjectIdToString(id) | 204 NOTREACHED() << "TryRegisterId called on " << ObjectIdToString(id) |
205 << " which is not in the registration map"; | 205 << " which is not in the registration map"; |
206 return; | 206 return; |
207 } | 207 } |
208 RegistrationStatus* status = it->second.get(); | 208 RegistrationStatus* status = it->second.get(); |
209 if (!status->enabled) { | 209 if (!status->enabled) { |
210 // Disabled, so do nothing. | 210 // Disabled, so do nothing. |
211 return; | 211 return; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } else { | 244 } else { |
245 DVLOG(2) << "Not a retry -- registering " | 245 DVLOG(2) << "Not a retry -- registering " |
246 << ObjectIdToString(id) << " immediately"; | 246 << ObjectIdToString(id) << " immediately"; |
247 status->delay = base::TimeDelta(); | 247 status->delay = base::TimeDelta(); |
248 status->next_delay = base::TimeDelta(); | 248 status->next_delay = base::TimeDelta(); |
249 status->DoRegister(); | 249 status->DoRegister(); |
250 } | 250 } |
251 } | 251 } |
252 | 252 |
253 void RegistrationManager::DoRegisterId(const invalidation::ObjectId& id) { | 253 void RegistrationManager::DoRegisterId(const invalidation::ObjectId& id) { |
254 DCHECK(CalledOnValidThread()); | 254 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
255 invalidation_client_->Register(id); | 255 invalidation_client_->Register(id); |
256 auto it = registration_statuses_.find(id); | 256 auto it = registration_statuses_.find(id); |
257 if (it == registration_statuses_.end()) { | 257 if (it == registration_statuses_.end()) { |
258 NOTREACHED() << "DoRegisterId called on " << ObjectIdToString(id) | 258 NOTREACHED() << "DoRegisterId called on " << ObjectIdToString(id) |
259 << " which is not in the registration map"; | 259 << " which is not in the registration map"; |
260 return; | 260 return; |
261 } | 261 } |
262 it->second->state = invalidation::InvalidationListener::REGISTERED; | 262 it->second->state = invalidation::InvalidationListener::REGISTERED; |
263 it->second->last_registration_request = base::Time::Now(); | 263 it->second->last_registration_request = base::Time::Now(); |
264 } | 264 } |
265 | 265 |
266 void RegistrationManager::UnregisterId(const invalidation::ObjectId& id) { | 266 void RegistrationManager::UnregisterId(const invalidation::ObjectId& id) { |
267 DCHECK(CalledOnValidThread()); | 267 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
268 invalidation_client_->Unregister(id); | 268 invalidation_client_->Unregister(id); |
269 auto it = registration_statuses_.find(id); | 269 auto it = registration_statuses_.find(id); |
270 if (it == registration_statuses_.end()) { | 270 if (it == registration_statuses_.end()) { |
271 NOTREACHED() << "UnregisterId called on " << ObjectIdToString(id) | 271 NOTREACHED() << "UnregisterId called on " << ObjectIdToString(id) |
272 << " which is not in the registration map"; | 272 << " which is not in the registration map"; |
273 return; | 273 return; |
274 } | 274 } |
275 registration_statuses_.erase(it); | 275 registration_statuses_.erase(it); |
276 } | 276 } |
277 | 277 |
278 | 278 |
279 ObjectIdSet RegistrationManager::GetRegisteredIds() const { | 279 ObjectIdSet RegistrationManager::GetRegisteredIds() const { |
280 DCHECK(CalledOnValidThread()); | 280 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
281 ObjectIdSet ids; | 281 ObjectIdSet ids; |
282 for (const auto& status_pair : registration_statuses_) { | 282 for (const auto& status_pair : registration_statuses_) { |
283 if (IsIdRegistered(status_pair.first)) { | 283 if (IsIdRegistered(status_pair.first)) { |
284 ids.insert(status_pair.first); | 284 ids.insert(status_pair.first); |
285 } | 285 } |
286 } | 286 } |
287 return ids; | 287 return ids; |
288 } | 288 } |
289 | 289 |
290 bool RegistrationManager::IsIdRegistered( | 290 bool RegistrationManager::IsIdRegistered( |
291 const invalidation::ObjectId& id) const { | 291 const invalidation::ObjectId& id) const { |
292 DCHECK(CalledOnValidThread()); | 292 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
293 auto it = registration_statuses_.find(id); | 293 auto it = registration_statuses_.find(id); |
294 return it != registration_statuses_.end() && | 294 return it != registration_statuses_.end() && |
295 it->second->state == invalidation::InvalidationListener::REGISTERED; | 295 it->second->state == invalidation::InvalidationListener::REGISTERED; |
296 } | 296 } |
297 | 297 |
298 } // namespace syncer | 298 } // namespace syncer |
OLD | NEW |