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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
18 #include "components/user_prefs/pref_registry_syncable.h" | 18 #include "components/user_prefs/pref_registry_syncable.h" |
19 #include "sync/internal_api/public/base/model_type.h" | |
20 | |
21 using syncer::InvalidationStateMap; | |
22 | 19 |
23 namespace { | 20 namespace { |
24 | 21 |
25 const char kSourceKey[] = "source"; | 22 const char kInvalidatorMaxInvalidationVersions[] = |
26 const char kNameKey[] = "name"; | 23 "invalidator.max_invalidation_versions"; |
27 const char kMaxVersionKey[] = "max-version"; | |
28 const char kPayloadKey[] = "payload"; | |
29 const char kCurrentAckHandleKey[] = "current-ack"; | |
30 const char kExpectedAckHandleKey[] = "expected-ack"; | |
31 | 24 |
32 bool ValueToObjectIdAndState(const DictionaryValue& value, | 25 |
33 invalidation::ObjectId* id, | 26 bool ValueToUnackedInvalidationStorageMap( |
34 syncer::InvalidationState* state) { | 27 const ListValue& value, |
35 std::string source_str; | 28 syncer::UnackedInvalidationsMap* map) { |
36 if (!value.GetString(kSourceKey, &source_str)) { | 29 for (size_t i = 0; i != value.GetSize(); ++i) { |
37 DLOG(WARNING) << "Unable to deserialize source"; | 30 invalidation::ObjectId invalid_id; |
38 return false; | 31 syncer::UnackedInvalidationSet storage(invalid_id); |
39 } | 32 const base::DictionaryValue* dict; |
40 int source = 0; | 33 if (!value.GetDictionary(i, &dict) || !storage.ResetFromValue(*dict)) { |
41 if (!base::StringToInt(source_str, &source)) { | 34 DLOG(WARNING) << "Failed to parse ObjectState at position " << i; |
42 DLOG(WARNING) << "Invalid source: " << source_str; | 35 return false; |
43 return false; | 36 } |
44 } | 37 map->insert(std::make_pair(storage.object_id(), storage)); |
45 std::string name; | |
46 if (!value.GetString(kNameKey, &name)) { | |
47 DLOG(WARNING) << "Unable to deserialize name"; | |
48 return false; | |
49 } | |
50 *id = invalidation::ObjectId(source, name); | |
51 std::string max_version_str; | |
52 if (!value.GetString(kMaxVersionKey, &max_version_str)) { | |
53 DLOG(WARNING) << "Unable to deserialize max version"; | |
54 return false; | |
55 } | |
56 if (!base::StringToInt64(max_version_str, &state->version)) { | |
57 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | |
58 return false; | |
59 } | |
60 value.GetString(kPayloadKey, &state->payload); | |
61 // The ack handle fields won't be set if upgrading from previous versions of | |
62 // Chrome. | |
63 const base::DictionaryValue* current_ack_handle_value = NULL; | |
64 if (value.GetDictionary(kCurrentAckHandleKey, ¤t_ack_handle_value)) { | |
65 state->current.ResetFromValue(*current_ack_handle_value); | |
66 } | |
67 const base::DictionaryValue* expected_ack_handle_value = NULL; | |
68 if (value.GetDictionary(kExpectedAckHandleKey, &expected_ack_handle_value)) { | |
69 state->expected.ResetFromValue(*expected_ack_handle_value); | |
70 } else { | |
71 // In this case, we should never have a valid current value set. | |
72 DCHECK(!state->current.IsValid()); | |
73 state->current = syncer::AckHandle::InvalidAckHandle(); | |
74 } | 38 } |
75 return true; | 39 return true; |
76 } | 40 } |
77 | 41 |
78 // The caller owns the returned value. | 42 scoped_ptr<base::ListValue> UnackedInvalidationStorageMapToValue( |
79 DictionaryValue* ObjectIdAndStateToValue( | 43 const syncer::UnackedInvalidationsMap& map) { |
tim (not reviewing)
2013/11/18 21:59:05
nit - indent.
rlarocque
2013/11/19 00:35:49
Done.
| |
80 const invalidation::ObjectId& id, const syncer::InvalidationState& state) { | 44 scoped_ptr<base::ListValue> value(new base::ListValue); |
81 DictionaryValue* value = new DictionaryValue; | 45 for (syncer::UnackedInvalidationsMap::const_iterator it = map.begin(); |
82 value->SetString(kSourceKey, base::IntToString(id.source())); | 46 it != map.end(); ++it) { |
83 value->SetString(kNameKey, id.name()); | 47 value->Append(it->second.ToValue().release()); |
84 value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); | 48 } |
85 value->SetString(kPayloadKey, state.payload); | 49 return value.Pass(); |
86 if (state.current.IsValid()) | |
87 value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); | |
88 if (state.expected.IsValid()) | |
89 value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); | |
90 return value; | |
91 } | 50 } |
92 | 51 |
93 } // namespace | 52 } // namespace |
94 | 53 |
95 namespace invalidation { | 54 namespace invalidation { |
96 | 55 |
97 // static | 56 // static |
98 void InvalidatorStorage::RegisterProfilePrefs( | 57 void InvalidatorStorage::RegisterProfilePrefs( |
99 user_prefs::PrefRegistrySyncable* registry) { | 58 user_prefs::PrefRegistrySyncable* registry) { |
100 registry->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions, | 59 registry->RegisterListPref(prefs::kInvalidatorSavedInvalidations, |
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
102 registry->RegisterStringPref( | 61 registry->RegisterStringPref( |
103 prefs::kInvalidatorInvalidationState, | 62 prefs::kInvalidatorInvalidationState, |
104 std::string(), | 63 std::string(), |
105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
106 registry->RegisterStringPref( | 65 registry->RegisterStringPref( |
107 prefs::kInvalidatorClientId, | 66 prefs::kInvalidatorClientId, |
108 std::string(), | 67 std::string(), |
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 68 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
110 registry->RegisterDictionaryPref( | 69 |
111 prefs::kSyncMaxInvalidationVersions, | 70 // This prefs is obsolete. We register it so we can clear it. |
tim (not reviewing)
2013/11/18 21:59:05
nit - 'this pref'.
| |
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 71 // At some point in the future, it will be safe to remove this. |
72 registry->RegisterListPref(kInvalidatorMaxInvalidationVersions, | |
73 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
113 } | 74 } |
114 | 75 |
115 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | 76 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
116 : pref_service_(pref_service) { | 77 : pref_service_(pref_service) { |
117 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case | 78 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case |
118 // throughout this file. This is a problem now due to lack of injection at | 79 // throughout this file. This is a problem now due to lack of injection at |
119 // ProfileSyncService. Bug 130176. | 80 // ProfileSyncService. Bug 130176. |
120 if (pref_service_) | 81 if (pref_service) |
121 MigrateMaxInvalidationVersionsPref(); | 82 pref_service_->ClearPref(kInvalidatorMaxInvalidationVersions); |
122 } | 83 } |
123 | 84 |
124 InvalidatorStorage::~InvalidatorStorage() { | 85 InvalidatorStorage::~InvalidatorStorage() { |
125 } | 86 } |
126 | 87 |
127 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { | |
128 DCHECK(thread_checker_.CalledOnValidThread()); | |
129 InvalidationStateMap state_map; | |
130 if (!pref_service_) { | |
131 return state_map; | |
132 } | |
133 const base::ListValue* state_map_list = | |
134 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); | |
135 CHECK(state_map_list); | |
136 DeserializeFromList(*state_map_list, &state_map); | |
137 return state_map; | |
138 } | |
139 | |
140 void InvalidatorStorage::SetMaxVersionAndPayload( | |
141 const invalidation::ObjectId& id, | |
142 int64 max_version, | |
143 const std::string& payload) { | |
144 DCHECK(thread_checker_.CalledOnValidThread()); | |
145 CHECK(pref_service_); | |
146 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
147 InvalidationStateMap::iterator it = state_map.find(id); | |
148 if ((it != state_map.end()) && (max_version <= it->second.version)) { | |
149 NOTREACHED(); | |
150 return; | |
151 } | |
152 state_map[id].version = max_version; | |
153 state_map[id].payload = payload; | |
154 | |
155 base::ListValue state_map_list; | |
156 SerializeToList(state_map, &state_map_list); | |
157 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
158 state_map_list); | |
159 } | |
160 | |
161 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { | |
162 DCHECK(thread_checker_.CalledOnValidThread()); | |
163 CHECK(pref_service_); | |
164 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
165 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | |
166 ++it) { | |
167 state_map.erase(*it); | |
168 } | |
169 | |
170 base::ListValue state_map_list; | |
171 SerializeToList(state_map, &state_map_list); | |
172 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
173 state_map_list); | |
174 } | |
175 | |
176 // static | |
177 void InvalidatorStorage::DeserializeFromList( | |
178 const base::ListValue& state_map_list, | |
179 InvalidationStateMap* state_map) { | |
180 state_map->clear(); | |
181 for (size_t i = 0; i < state_map_list.GetSize(); ++i) { | |
182 const DictionaryValue* value = NULL; | |
183 if (!state_map_list.GetDictionary(i, &value)) { | |
184 DLOG(WARNING) << "Unable to deserialize entry " << i; | |
185 continue; | |
186 } | |
187 invalidation::ObjectId id; | |
188 syncer::InvalidationState state; | |
189 if (!ValueToObjectIdAndState(*value, &id, &state)) { | |
190 DLOG(WARNING) << "Error while deserializing entry " << i; | |
191 continue; | |
192 } | |
193 (*state_map)[id] = state; | |
194 } | |
195 } | |
196 | |
197 // static | |
198 void InvalidatorStorage::SerializeToList( | |
199 const InvalidationStateMap& state_map, | |
200 base::ListValue* state_map_list) { | |
201 for (InvalidationStateMap::const_iterator it = state_map.begin(); | |
202 it != state_map.end(); ++it) { | |
203 state_map_list->Append(ObjectIdAndStateToValue(it->first, it->second)); | |
204 } | |
205 } | |
206 | |
207 // Legacy migration code. | |
208 void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() { | |
209 const base::DictionaryValue* max_versions_dict = | |
210 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); | |
211 CHECK(max_versions_dict); | |
212 if (!max_versions_dict->empty()) { | |
213 InvalidationStateMap state_map; | |
214 DeserializeMap(max_versions_dict, &state_map); | |
215 base::ListValue state_map_list; | |
216 SerializeToList(state_map, &state_map_list); | |
217 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
218 state_map_list); | |
219 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", | |
220 true); | |
221 } else { | |
222 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", | |
223 false); | |
224 } | |
225 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); | |
226 } | |
227 | |
228 // Legacy migration code. | |
229 // static | |
230 void InvalidatorStorage::DeserializeMap( | |
231 const base::DictionaryValue* max_versions_dict, | |
232 InvalidationStateMap* map) { | |
233 map->clear(); | |
234 // Convert from a string -> string DictionaryValue to a | |
235 // ModelType -> int64 map. | |
236 for (base::DictionaryValue::Iterator it(*max_versions_dict); !it.IsAtEnd(); | |
237 it.Advance()) { | |
238 int model_type_int = 0; | |
239 if (!base::StringToInt(it.key(), &model_type_int)) { | |
240 LOG(WARNING) << "Invalid model type key: " << it.key(); | |
241 continue; | |
242 } | |
243 if ((model_type_int < syncer::FIRST_REAL_MODEL_TYPE) || | |
244 (model_type_int >= syncer::MODEL_TYPE_COUNT)) { | |
245 LOG(WARNING) << "Out-of-range model type key: " << model_type_int; | |
246 continue; | |
247 } | |
248 const syncer::ModelType model_type = | |
249 syncer::ModelTypeFromInt(model_type_int); | |
250 std::string max_version_str; | |
251 CHECK(it.value().GetAsString(&max_version_str)); | |
252 int64 max_version = 0; | |
253 if (!base::StringToInt64(max_version_str, &max_version)) { | |
254 LOG(WARNING) << "Invalid max invalidation version for " | |
255 << syncer::ModelTypeToString(model_type) << ": " | |
256 << max_version_str; | |
257 continue; | |
258 } | |
259 invalidation::ObjectId id; | |
260 if (!syncer::RealModelTypeToObjectId(model_type, &id)) { | |
261 DLOG(WARNING) << "Invalid model type: " << model_type; | |
262 continue; | |
263 } | |
264 (*map)[id].version = max_version; | |
265 } | |
266 } | |
267 | |
268 void InvalidatorStorage::SetInvalidatorClientId(const std::string& client_id) { | 88 void InvalidatorStorage::SetInvalidatorClientId(const std::string& client_id) { |
269 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
270 Clear(); // We can't reuse our old invalidation state if the ID changes. | 90 Clear(); // We can't reuse our old invalidation state if the ID changes. |
271 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); | 91 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); |
272 } | 92 } |
273 | 93 |
274 std::string InvalidatorStorage::GetInvalidatorClientId() const { | 94 std::string InvalidatorStorage::GetInvalidatorClientId() const { |
275 return pref_service_ ? | 95 return pref_service_ ? |
276 pref_service_->GetString(prefs::kInvalidatorClientId) : | 96 pref_service_->GetString(prefs::kInvalidatorClientId) : |
277 std::string(); | 97 std::string(); |
(...skipping 10 matching lines...) Expand all Loading... | |
288 std::string InvalidatorStorage::GetBootstrapData() const { | 108 std::string InvalidatorStorage::GetBootstrapData() const { |
289 std::string base64_data( | 109 std::string base64_data( |
290 pref_service_ | 110 pref_service_ |
291 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState) | 111 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState) |
292 : std::string()); | 112 : std::string()); |
293 std::string data; | 113 std::string data; |
294 base::Base64Decode(base64_data, &data); | 114 base::Base64Decode(base64_data, &data); |
295 return data; | 115 return data; |
296 } | 116 } |
297 | 117 |
118 void InvalidatorStorage::SetSavedInvalidations( | |
119 const syncer::UnackedInvalidationsMap& map) { | |
120 scoped_ptr<base::ListValue> value(UnackedInvalidationStorageMapToValue(map)); | |
121 pref_service_->Set(prefs::kInvalidatorSavedInvalidations, *value.get()); | |
122 } | |
123 | |
124 syncer::UnackedInvalidationsMap | |
125 InvalidatorStorage::GetSavedInvalidations() const { | |
126 syncer::UnackedInvalidationsMap map; | |
127 const base::ListValue* value = | |
128 pref_service_->GetList(prefs::kInvalidatorSavedInvalidations); | |
129 if (!ValueToUnackedInvalidationStorageMap(*value, &map)) { | |
130 return syncer::UnackedInvalidationsMap(); | |
131 } else { | |
132 return map; | |
133 } | |
134 } | |
135 | |
298 void InvalidatorStorage::Clear() { | 136 void InvalidatorStorage::Clear() { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | 137 DCHECK(thread_checker_.CalledOnValidThread()); |
300 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); | 138 pref_service_->ClearPref(prefs::kInvalidatorSavedInvalidations); |
301 pref_service_->ClearPref(prefs::kInvalidatorClientId); | 139 pref_service_->ClearPref(prefs::kInvalidatorClientId); |
302 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | 140 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
303 } | 141 } |
304 | 142 |
305 void InvalidatorStorage::GenerateAckHandles( | |
306 const syncer::ObjectIdSet& ids, | |
307 const scoped_refptr<base::TaskRunner>& task_runner, | |
308 const base::Callback<void(const syncer::AckHandleMap&)> callback) { | |
309 DCHECK(thread_checker_.CalledOnValidThread()); | |
310 CHECK(pref_service_); | |
311 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
312 | |
313 syncer::AckHandleMap ack_handles; | |
314 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | |
315 ++it) { | |
316 state_map[*it].expected = syncer::AckHandle::CreateUnique(); | |
317 ack_handles.insert(std::make_pair(*it, state_map[*it].expected)); | |
318 } | |
319 | |
320 base::ListValue state_map_list; | |
321 SerializeToList(state_map, &state_map_list); | |
322 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
323 state_map_list); | |
324 | |
325 ignore_result(task_runner->PostTask(FROM_HERE, | |
326 base::Bind(callback, ack_handles))); | |
327 } | |
328 | |
329 void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id, | |
330 const syncer::AckHandle& ack_handle) { | |
331 DCHECK(thread_checker_.CalledOnValidThread()); | |
332 CHECK(pref_service_); | |
333 InvalidationStateMap state_map = GetAllInvalidationStates(); | |
334 | |
335 InvalidationStateMap::iterator it = state_map.find(id); | |
336 // This could happen if the acknowledgement is delayed and Forget() has | |
337 // already been called. | |
338 if (it == state_map.end()) | |
339 return; | |
340 it->second.current = ack_handle; | |
341 | |
342 base::ListValue state_map_list; | |
343 SerializeToList(state_map, &state_map_list); | |
344 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | |
345 state_map_list); | |
346 } | |
347 | |
348 } // namespace invalidation | 143 } // namespace invalidation |
OLD | NEW |