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/sync/invalidations/invalidator_storage.h" | 5 #include "chrome/browser/sync/invalidations/invalidator_storage.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | |
9 #include "base/callback.h" | |
10 #include "base/location.h" | |
8 #include "base/logging.h" | 11 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
10 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/task_runner.h" | |
11 #include "base/values.h" | 15 #include "base/values.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
14 #include "sync/internal_api/public/base/model_type.h" | 18 #include "sync/internal_api/public/base/model_type.h" |
15 | 19 |
16 using syncer::InvalidationStateMap; | 20 using syncer::InvalidationStateMap; |
17 | 21 |
18 namespace browser_sync { | 22 namespace browser_sync { |
19 | 23 |
20 namespace { | 24 namespace { |
21 | 25 |
22 const char kSourceKey[] = "source"; | 26 const char kSourceKey[] = "source"; |
23 const char kNameKey[] = "name"; | 27 const char kNameKey[] = "name"; |
24 const char kMaxVersionKey[] = "max-version"; | 28 const char kMaxVersionKey[] = "max-version"; |
29 const char kPayloadKey[] = "payload"; | |
30 const char kCurrentAckHandleKey[] = "current-ack"; | |
31 const char kExpectedAckHandleKey[] = "expected-ack"; | |
25 | 32 |
26 bool ValueToObjectIdAndState(const DictionaryValue& value, | 33 bool ValueToObjectIdAndState(const DictionaryValue& value, |
27 invalidation::ObjectId* id, | 34 invalidation::ObjectId* id, |
28 syncer::InvalidationState* state) { | 35 syncer::InvalidationState* state) { |
29 std::string source_str; | 36 std::string source_str; |
30 int source = 0; | |
31 std::string name; | |
32 std::string max_version_str; | |
33 if (!value.GetString(kSourceKey, &source_str)) { | 37 if (!value.GetString(kSourceKey, &source_str)) { |
34 DLOG(WARNING) << "Unable to deserialize source"; | 38 DLOG(WARNING) << "Unable to deserialize source"; |
35 return false; | 39 return false; |
36 } | 40 } |
41 int source = 0; | |
42 if (!base::StringToInt(source_str, &source)) { | |
43 DLOG(WARNING) << "Invalid source: " << source_str; | |
44 return false; | |
45 } | |
46 std::string name; | |
37 if (!value.GetString(kNameKey, &name)) { | 47 if (!value.GetString(kNameKey, &name)) { |
38 DLOG(WARNING) << "Unable to deserialize name"; | 48 DLOG(WARNING) << "Unable to deserialize name"; |
39 return false; | 49 return false; |
40 } | 50 } |
51 *id = invalidation::ObjectId(source, name); | |
52 std::string max_version_str; | |
41 if (!value.GetString(kMaxVersionKey, &max_version_str)) { | 53 if (!value.GetString(kMaxVersionKey, &max_version_str)) { |
42 DLOG(WARNING) << "Unable to deserialize max version"; | 54 DLOG(WARNING) << "Unable to deserialize max version"; |
43 return false; | 55 return false; |
44 } | 56 } |
45 if (!base::StringToInt(source_str, &source)) { | |
46 DLOG(WARNING) << "Invalid source: " << source_str; | |
47 return false; | |
48 } | |
49 if (!base::StringToInt64(max_version_str, &state->version)) { | 57 if (!base::StringToInt64(max_version_str, &state->version)) { |
50 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | 58 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; |
51 return false; | 59 return false; |
52 } | 60 } |
53 *id = invalidation::ObjectId(source, name); | 61 value.GetString(kPayloadKey, &state->payload); |
62 // The ack handle fields won't be set if upgrading from previous versions of | |
63 // Chrome. | |
64 const base::DictionaryValue* current_ack_handle_value = NULL; | |
65 if (value.GetDictionary(kCurrentAckHandleKey, ¤t_ack_handle_value)) { | |
66 state->current.ResetFromValue(*current_ack_handle_value); | |
67 } | |
68 const base::DictionaryValue* expected_ack_handle_value = NULL; | |
69 if (value.GetDictionary(kExpectedAckHandleKey, &expected_ack_handle_value)) { | |
70 state->expected.ResetFromValue(*expected_ack_handle_value); | |
71 } else { | |
72 // In this case, we should never have a valid current value set. | |
73 DCHECK(!state->current.IsValid()); | |
74 state->current = syncer::AckHandle::InvalidAckHandle(); | |
75 } | |
54 return true; | 76 return true; |
55 } | 77 } |
56 | 78 |
57 // The caller owns the returned value. | 79 // The caller owns the returned value. |
58 DictionaryValue* ObjectIdAndStateToValue( | 80 DictionaryValue* ObjectIdAndStateToValue( |
59 const invalidation::ObjectId& id, const syncer::InvalidationState& state) { | 81 const invalidation::ObjectId& id, const syncer::InvalidationState& state) { |
60 DictionaryValue* value = new DictionaryValue; | 82 DictionaryValue* value = new DictionaryValue; |
61 value->SetString(kSourceKey, base::IntToString(id.source())); | 83 value->SetString(kSourceKey, base::IntToString(id.source())); |
62 value->SetString(kNameKey, id.name()); | 84 value->SetString(kNameKey, id.name()); |
63 value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); | 85 value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); |
86 value->SetString(kPayloadKey, state.payload); | |
87 if (state.current.IsValid()) | |
88 value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); | |
89 if (state.expected.IsValid()) | |
90 value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); | |
64 return value; | 91 return value; |
65 } | 92 } |
66 | 93 |
67 } // namespace | 94 } // namespace |
68 | 95 |
69 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | 96 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
70 : pref_service_(pref_service) { | 97 : pref_service_(pref_service) { |
71 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case | 98 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case |
72 // throughout this file. This is a problem now due to lack of injection at | 99 // throughout this file. This is a problem now due to lack of injection at |
73 // ProfileSyncService. Bug 130176. | 100 // ProfileSyncService. Bug 130176. |
(...skipping 17 matching lines...) Expand all Loading... | |
91 if (!pref_service_) { | 118 if (!pref_service_) { |
92 return state_map; | 119 return state_map; |
93 } | 120 } |
94 const base::ListValue* state_map_list = | 121 const base::ListValue* state_map_list = |
95 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); | 122 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); |
96 CHECK(state_map_list); | 123 CHECK(state_map_list); |
97 DeserializeFromList(*state_map_list, &state_map); | 124 DeserializeFromList(*state_map_list, &state_map); |
98 return state_map; | 125 return state_map; |
99 } | 126 } |
100 | 127 |
101 void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, | 128 void InvalidatorStorage::SetMaxVersionAndPayload( |
102 int64 max_version) { | 129 const invalidation::ObjectId& id, |
130 int64 max_version, | |
131 const std::string& payload) { | |
103 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
104 CHECK(pref_service_); | 133 CHECK(pref_service_); |
105 InvalidationStateMap state_map = GetAllInvalidationStates(); | 134 InvalidationStateMap state_map = GetAllInvalidationStates(); |
106 InvalidationStateMap::iterator it = state_map.find(id); | 135 InvalidationStateMap::iterator it = state_map.find(id); |
107 if ((it != state_map.end()) && (max_version <= it->second.version)) { | 136 if ((it != state_map.end()) && (max_version <= it->second.version)) { |
108 NOTREACHED(); | 137 NOTREACHED(); |
109 return; | 138 return; |
110 } | 139 } |
111 state_map[id].version = max_version; | 140 state_map[id].version = max_version; |
141 state_map[id].payload = payload; | |
112 | 142 |
113 base::ListValue state_map_list; | 143 base::ListValue state_map_list; |
114 SerializeToList(state_map, &state_map_list); | 144 SerializeToList(state_map, &state_map_list); |
115 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 145 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
116 state_map_list); | 146 state_map_list); |
117 } | 147 } |
118 | 148 |
119 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { | 149 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { |
120 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
121 CHECK(pref_service_); | 151 CHECK(pref_service_); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 base::Base64Decode(base64_data, &data); | 271 base::Base64Decode(base64_data, &data); |
242 return data; | 272 return data; |
243 } | 273 } |
244 | 274 |
245 void InvalidatorStorage::Clear() { | 275 void InvalidatorStorage::Clear() { |
246 DCHECK(thread_checker_.CalledOnValidThread()); | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
247 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); | 277 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); |
248 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | 278 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
249 } | 279 } |
250 | 280 |
281 void InvalidatorStorage::GenerateAckHandles( | |
282 const syncer::ObjectIdSet& ids, | |
283 const scoped_refptr<base::TaskRunner>& task_runner, | |
284 const base::Callback<void(const syncer::AckHandleMap&)> callback) { | |
285 DCHECK(thread_checker_.CalledOnValidThread()); | |
286 CHECK(pref_service_); | |
287 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
288 | |
289 syncer::AckHandleMap ack_handles; | |
290 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | |
291 ++it) { | |
292 state_map[*it].expected = syncer::AckHandle::CreateUnique(); | |
293 ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); | |
294 } | |
295 | |
296 base::ListValue state_map_list; | |
297 SerializeToList(state_map, &state_map_list); | |
298 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
299 state_map_list); | |
300 | |
301 ignore_result(task_runner->PostTask(FROM_HERE, | |
akalin
2012/11/30 02:53:02
compiler_specific.h for ignore_result
dcheng
2012/11/30 23:44:54
This is defined in basictypes.h.
| |
302 base::Bind(callback, ack_handles))); | |
303 } | |
304 | |
305 void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, | |
306 const syncer::AckHandle& ack_handle) { | |
307 DCHECK(thread_checker_.CalledOnValidThread()); | |
308 CHECK(pref_service_); | |
309 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
310 | |
311 InvalidationStateMap::iterator it = state_map.find(id); | |
312 // This could happen if the acknowledgement is delayed and Forget() has | |
313 // already been called. | |
314 if (it == state_map.end()) | |
315 return; | |
316 it->second.current = ack_handle; | |
317 | |
318 base::ListValue state_map_list; | |
319 SerializeToList(state_map, &state_map_list); | |
320 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
321 state_map_list); | |
322 } | |
323 | |
251 } // namespace browser_sync | 324 } // namespace browser_sync |
OLD | NEW |