OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/sync/invalidations/invalidator_storage.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/bind.h" | |
9 #include "base/callback.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/task_runner.h" | |
16 #include "base/values.h" | |
17 #include "chrome/common/pref_names.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 | |
23 namespace browser_sync { | |
24 | |
25 namespace { | |
26 | |
27 const char kSourceKey[] = "source"; | |
28 const char kNameKey[] = "name"; | |
29 const char kMaxVersionKey[] = "max-version"; | |
30 const char kPayloadKey[] = "payload"; | |
31 const char kCurrentAckHandleKey[] = "current-ack"; | |
32 const char kExpectedAckHandleKey[] = "expected-ack"; | |
33 | |
34 bool ValueToObjectIdAndState(const DictionaryValue& value, | |
35 invalidation::ObjectId* id, | |
36 syncer::InvalidationState* state) { | |
37 std::string source_str; | |
38 if (!value.GetString(kSourceKey, &source_str)) { | |
39 DLOG(WARNING) << "Unable to deserialize source"; | |
40 return false; | |
41 } | |
42 int source = 0; | |
43 if (!base::StringToInt(source_str, &source)) { | |
44 DLOG(WARNING) << "Invalid source: " << source_str; | |
45 return false; | |
46 } | |
47 std::string name; | |
48 if (!value.GetString(kNameKey, &name)) { | |
49 DLOG(WARNING) << "Unable to deserialize name"; | |
50 return false; | |
51 } | |
52 *id = invalidation::ObjectId(source, name); | |
53 std::string max_version_str; | |
54 if (!value.GetString(kMaxVersionKey, &max_version_str)) { | |
55 DLOG(WARNING) << "Unable to deserialize max version"; | |
56 return false; | |
57 } | |
58 if (!base::StringToInt64(max_version_str, &state->version)) { | |
59 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | |
60 return false; | |
61 } | |
62 value.GetString(kPayloadKey, &state->payload); | |
63 // The ack handle fields won't be set if upgrading from previous versions of | |
64 // Chrome. | |
65 const base::DictionaryValue* current_ack_handle_value = NULL; | |
66 if (value.GetDictionary(kCurrentAckHandleKey, ¤t_ack_handle_value)) { | |
67 state->current.ResetFromValue(*current_ack_handle_value); | |
68 } | |
69 const base::DictionaryValue* expected_ack_handle_value = NULL; | |
70 if (value.GetDictionary(kExpectedAckHandleKey, &expected_ack_handle_value)) { | |
71 state->expected.ResetFromValue(*expected_ack_handle_value); | |
72 } else { | |
73 // In this case, we should never have a valid current value set. | |
74 DCHECK(!state->current.IsValid()); | |
75 state->current = syncer::AckHandle::InvalidAckHandle(); | |
76 } | |
77 return true; | |
78 } | |
79 | |
80 // The caller owns the returned value. | |
81 DictionaryValue* ObjectIdAndStateToValue( | |
82 const invalidation::ObjectId& id, const syncer::InvalidationState& state) { | |
83 DictionaryValue* value = new DictionaryValue; | |
84 value->SetString(kSourceKey, base::IntToString(id.source())); | |
85 value->SetString(kNameKey, id.name()); | |
86 value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); | |
87 value->SetString(kPayloadKey, state.payload); | |
88 if (state.current.IsValid()) | |
89 value->Set(kCurrentAckHandleKey, state.current.ToValue().release()); | |
90 if (state.expected.IsValid()) | |
91 value->Set(kExpectedAckHandleKey, state.expected.ToValue().release()); | |
92 return value; | |
93 } | |
94 | |
95 } // namespace | |
96 | |
97 // static | |
98 void InvalidatorStorage::RegisterUserPrefs( | |
99 user_prefs::PrefRegistrySyncable* registry) { | |
100 registry->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions, | |
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
102 registry->RegisterStringPref( | |
103 prefs::kInvalidatorInvalidationState, | |
104 std::string(), | |
105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
106 registry->RegisterStringPref( | |
107 prefs::kInvalidatorClientId, | |
108 std::string(), | |
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
110 registry->RegisterDictionaryPref( | |
111 prefs::kSyncMaxInvalidationVersions, | |
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
113 } | |
114 | |
115 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | |
116 : pref_service_(pref_service) { | |
117 // 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 | |
119 // ProfileSyncService. Bug 130176. | |
120 if (pref_service_) | |
121 MigrateMaxInvalidationVersionsPref(); | |
122 } | |
123 | |
124 InvalidatorStorage::~InvalidatorStorage() { | |
125 } | |
126 | |
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) { | |
269 DCHECK(thread_checker_.CalledOnValidThread()); | |
270 Clear(); // We can't reuse our old invalidation state if the ID changes. | |
271 pref_service_->SetString(prefs::kInvalidatorClientId, client_id); | |
272 } | |
273 | |
274 std::string InvalidatorStorage::GetInvalidatorClientId() const { | |
275 return pref_service_ ? | |
276 pref_service_->GetString(prefs::kInvalidatorClientId) : | |
277 std::string(); | |
278 } | |
279 | |
280 void InvalidatorStorage::SetBootstrapData(const std::string& data) { | |
281 DCHECK(thread_checker_.CalledOnValidThread()); | |
282 std::string base64_data; | |
283 base::Base64Encode(data, &base64_data); | |
284 pref_service_->SetString(prefs::kInvalidatorInvalidationState, | |
285 base64_data); | |
286 } | |
287 | |
288 std::string InvalidatorStorage::GetBootstrapData() const { | |
289 std::string base64_data( | |
290 pref_service_ | |
291 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState) | |
292 : std::string()); | |
293 std::string data; | |
294 base::Base64Decode(base64_data, &data); | |
295 return data; | |
296 } | |
297 | |
298 void InvalidatorStorage::Clear() { | |
299 DCHECK(thread_checker_.CalledOnValidThread()); | |
300 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); | |
301 pref_service_->ClearPref(prefs::kInvalidatorClientId); | |
302 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | |
303 } | |
304 | |
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 browser_sync | |
OLD | NEW |