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 #include "chrome/browser/invalidation/invalidator_storage.h" | 5 #include "chrome/browser/invalidation/invalidator_storage.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 prefs::kInvalidatorClientId, | 107 prefs::kInvalidatorClientId, |
108 std::string(), | 108 std::string(), |
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
110 registry->RegisterDictionaryPref( | 110 registry->RegisterDictionaryPref( |
111 prefs::kSyncMaxInvalidationVersions, | 111 prefs::kSyncMaxInvalidationVersions, |
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
113 } | 113 } |
114 | 114 |
115 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | 115 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
116 : pref_service_(pref_service) { | 116 : pref_service_(pref_service) { |
117 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case | 117 DCHECK(pref_service); |
118 // throughout this file. This is a problem now due to lack of injection at | 118 MigrateMaxInvalidationVersionsPref(); |
119 // ProfileSyncService. Bug 130176. | |
120 if (pref_service_) | |
121 MigrateMaxInvalidationVersionsPref(); | |
122 } | 119 } |
123 | 120 |
124 InvalidatorStorage::~InvalidatorStorage() { | 121 InvalidatorStorage::~InvalidatorStorage() { |
125 } | 122 } |
126 | 123 |
127 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { | 124 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { |
128 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
129 InvalidationStateMap state_map; | 126 InvalidationStateMap state_map; |
130 if (!pref_service_) { | |
131 return state_map; | |
132 } | |
133 const base::ListValue* state_map_list = | 127 const base::ListValue* state_map_list = |
134 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); | 128 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); |
135 CHECK(state_map_list); | 129 CHECK(state_map_list); |
136 DeserializeFromList(*state_map_list, &state_map); | 130 DeserializeFromList(*state_map_list, &state_map); |
137 return state_map; | 131 return state_map; |
138 } | 132 } |
139 | 133 |
140 void InvalidatorStorage::SetMaxVersionAndPayload( | 134 void InvalidatorStorage::SetMaxVersionAndPayload( |
141 const invalidation::ObjectId& id, | 135 const invalidation::ObjectId& id, |
142 int64 max_version, | 136 int64 max_version, |
143 const std::string& payload) { | 137 const std::string& payload) { |
144 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
145 CHECK(pref_service_); | |
146 InvalidationStateMap state_map = GetAllInvalidationStates(); | 139 InvalidationStateMap state_map = GetAllInvalidationStates(); |
147 InvalidationStateMap::iterator it = state_map.find(id); | 140 InvalidationStateMap::iterator it = state_map.find(id); |
148 if ((it != state_map.end()) && (max_version <= it->second.version)) { | 141 if ((it != state_map.end()) && (max_version <= it->second.version)) { |
149 NOTREACHED(); | 142 NOTREACHED(); |
150 return; | 143 return; |
151 } | 144 } |
152 state_map[id].version = max_version; | 145 state_map[id].version = max_version; |
153 state_map[id].payload = payload; | 146 state_map[id].payload = payload; |
154 | 147 |
155 base::ListValue state_map_list; | 148 base::ListValue state_map_list; |
156 SerializeToList(state_map, &state_map_list); | 149 SerializeToList(state_map, &state_map_list); |
157 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 150 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
158 state_map_list); | 151 state_map_list); |
159 } | 152 } |
160 | 153 |
161 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { | 154 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { |
162 DCHECK(thread_checker_.CalledOnValidThread()); | 155 DCHECK(thread_checker_.CalledOnValidThread()); |
163 CHECK(pref_service_); | |
164 InvalidationStateMap state_map = GetAllInvalidationStates(); | 156 InvalidationStateMap state_map = GetAllInvalidationStates(); |
165 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | 157 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
166 ++it) { | 158 ++it) { |
167 state_map.erase(*it); | 159 state_map.erase(*it); |
168 } | 160 } |
169 | 161 |
170 base::ListValue state_map_list; | 162 base::ListValue state_map_list; |
171 SerializeToList(state_map, &state_map_list); | 163 SerializeToList(state_map, &state_map_list); |
172 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 164 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
173 state_map_list); | 165 state_map_list); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 257 } |
266 } | 258 } |
267 | 259 |
268 void InvalidatorStorage::SetInvalidatorClientId(const std::string& client_id) { | 260 void InvalidatorStorage::SetInvalidatorClientId(const std::string& client_id) { |
269 DCHECK(thread_checker_.CalledOnValidThread()); | 261 DCHECK(thread_checker_.CalledOnValidThread()); |
270 Clear(); // We can't reuse our old invalidation state if the ID changes. | 262 Clear(); // We can't reuse our old invalidation state if the ID changes. |
271 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); | 263 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); |
272 } | 264 } |
273 | 265 |
274 std::string InvalidatorStorage::GetInvalidatorClientId() const { | 266 std::string InvalidatorStorage::GetInvalidatorClientId() const { |
275 return pref_service_ ? | 267 return pref_service_->GetString(prefs::kInvalidatorClientId); |
276 pref_service_->GetString(prefs::kInvalidatorClientId) : | |
277 std::string(); | |
278 } | 268 } |
279 | 269 |
280 void InvalidatorStorage::SetBootstrapData(const std::string& data) { | 270 void InvalidatorStorage::SetBootstrapData(const std::string& data) { |
281 DCHECK(thread_checker_.CalledOnValidThread()); | 271 DCHECK(thread_checker_.CalledOnValidThread()); |
282 std::string base64_data; | 272 std::string base64_data; |
283 base::Base64Encode(data, &base64_data); | 273 base::Base64Encode(data, &base64_data); |
284 pref_service_->SetString(prefs::kInvalidatorInvalidationState, | 274 pref_service_->SetString(prefs::kInvalidatorInvalidationState, |
285 base64_data); | 275 base64_data); |
286 } | 276 } |
287 | 277 |
288 std::string InvalidatorStorage::GetBootstrapData() const { | 278 std::string InvalidatorStorage::GetBootstrapData() const { |
289 std::string base64_data( | 279 std::string base64_data( |
290 pref_service_ | 280 pref_service_->GetString(prefs::kInvalidatorInvalidationState)); |
291 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState) | |
292 : std::string()); | |
293 std::string data; | 281 std::string data; |
294 base::Base64Decode(base64_data, &data); | 282 base::Base64Decode(base64_data, &data); |
295 return data; | 283 return data; |
296 } | 284 } |
297 | 285 |
298 void InvalidatorStorage::Clear() { | 286 void InvalidatorStorage::Clear() { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | 287 DCHECK(thread_checker_.CalledOnValidThread()); |
300 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); | 288 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); |
301 pref_service_->ClearPref(prefs::kInvalidatorClientId); | 289 pref_service_->ClearPref(prefs::kInvalidatorClientId); |
302 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | 290 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
303 } | 291 } |
304 | 292 |
305 void InvalidatorStorage::GenerateAckHandles( | 293 void InvalidatorStorage::GenerateAckHandles( |
306 const syncer::ObjectIdSet& ids, | 294 const syncer::ObjectIdSet& ids, |
307 const scoped_refptr<base::TaskRunner>& task_runner, | 295 const scoped_refptr<base::TaskRunner>& task_runner, |
308 const base::Callback<void(const syncer::AckHandleMap&)> callback) { | 296 const base::Callback<void(const syncer::AckHandleMap&)> callback) { |
309 DCHECK(thread_checker_.CalledOnValidThread()); | 297 DCHECK(thread_checker_.CalledOnValidThread()); |
310 CHECK(pref_service_); | |
311 InvalidationStateMap state_map = GetAllInvalidationStates(); | 298 InvalidationStateMap state_map = GetAllInvalidationStates(); |
312 | 299 |
313 syncer::AckHandleMap ack_handles; | 300 syncer::AckHandleMap ack_handles; |
314 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | 301 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
315 ++it) { | 302 ++it) { |
316 state_map[*it].expected = syncer::AckHandle::CreateUnique(); | 303 state_map[*it].expected = syncer::AckHandle::CreateUnique(); |
317 ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); | 304 ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); |
318 } | 305 } |
319 | 306 |
320 base::ListValue state_map_list; | 307 base::ListValue state_map_list; |
321 SerializeToList(state_map, &state_map_list); | 308 SerializeToList(state_map, &state_map_list); |
322 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 309 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
323 state_map_list); | 310 state_map_list); |
324 | 311 |
325 ignore_result(task_runner->PostTask(FROM_HERE, | 312 ignore_result(task_runner->PostTask(FROM_HERE, |
326 base::Bind(callback, ack_handles))); | 313 base::Bind(callback, ack_handles))); |
327 } | 314 } |
328 | 315 |
329 void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, | 316 void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, |
330 const syncer::AckHandle& ack_handle) { | 317 const syncer::AckHandle& ack_handle) { |
331 DCHECK(thread_checker_.CalledOnValidThread()); | 318 DCHECK(thread_checker_.CalledOnValidThread()); |
332 CHECK(pref_service_); | |
333 InvalidationStateMap state_map = GetAllInvalidationStates(); | 319 InvalidationStateMap state_map = GetAllInvalidationStates(); |
334 | 320 |
335 InvalidationStateMap::iterator it = state_map.find(id); | 321 InvalidationStateMap::iterator it = state_map.find(id); |
336 // This could happen if the acknowledgement is delayed and Forget() has | 322 // This could happen if the acknowledgement is delayed and Forget() has |
337 // already been called. | 323 // already been called. |
338 if (it == state_map.end()) | 324 if (it == state_map.end()) |
339 return; | 325 return; |
340 it->second.current = ack_handle; | 326 it->second.current = ack_handle; |
341 | 327 |
342 base::ListValue state_map_list; | 328 base::ListValue state_map_list; |
343 SerializeToList(state_map, &state_map_list); | 329 SerializeToList(state_map, &state_map_list); |
344 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 330 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
345 state_map_list); | 331 state_map_list); |
346 } | 332 } |
347 | 333 |
348 } // namespace invalidation | 334 } // namespace invalidation |
OLD | NEW |