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.
5 #include "chrome/browser/invalidation/invalidator_storage.h"
7 #include "base/base64.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"
21 using syncer::InvalidationStateMap;
25 const char kSourceKey[] = "source";
26 const char kNameKey[] = "name";
27 const char kMaxVersionKey[] = "max-version";
28 const char kPayloadKey[] = "payload";
29 const char kCurrentAckHandleKey[] = "current-ack";
30 const char kExpectedAckHandleKey[] = "expected-ack";
32 bool ValueToObjectIdAndState(const DictionaryValue& value,
33 invalidation::ObjectId* id,
34 syncer::InvalidationState* state) {
35 std::string source_str;
36 if (!value.GetString(kSourceKey, &source_str)) {
37 DLOG(WARNING) << "Unable to deserialize source";
41 if (!base::StringToInt(source_str, &source)) {
42 DLOG(WARNING) << "Invalid source: " << source_str;
46 if (!value.GetString(kNameKey, &name)) {
47 DLOG(WARNING) << "Unable to deserialize name";
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";
56 if (!base::StringToInt64(max_version_str, &state->version)) {
57 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str;
60 value.GetString(kPayloadKey, &state->payload);
61 // The ack handle fields won't be set if upgrading from previous versions of
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);
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);
71 // In this case, we should never have a valid current value set.
72 DCHECK(!state->current.IsValid());
73 state->current = syncer::AckHandle::InvalidAckHandle();
78 // The caller owns the returned value.
79 DictionaryValue* ObjectIdAndStateToValue(
80 const invalidation::ObjectId& id, const syncer::InvalidationState& state) {
81 DictionaryValue* value = new DictionaryValue;
82 value->SetString(kSourceKey, base::IntToString(id.source()));
83 value->SetString(kNameKey, id.name());
84 value->SetString(kMaxVersionKey, base::Int64ToString(state.version));
85 value->SetString(kPayloadKey, state.payload);
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());
95 namespace invalidation {
98 void InvalidatorStorage::RegisterProfilePrefs(
99 user_prefs::PrefRegistrySyncable* registry) {
100 registry->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
102 registry->RegisterStringPref(
103 prefs::kInvalidatorInvalidationState,
105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
106 registry->RegisterStringPref(
107 prefs::kInvalidatorClientId,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110 registry->RegisterDictionaryPref(
111 prefs::kSyncMaxInvalidationVersions,
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
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.
121 MigrateMaxInvalidationVersionsPref();
124 InvalidatorStorage::~InvalidatorStorage() {
127 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 InvalidationStateMap state_map;
130 if (!pref_service_) {
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);
140 void InvalidatorStorage::SetMaxVersionAndPayload(
141 const invalidation::ObjectId& id,
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)) {
152 state_map[id].version = max_version;
153 state_map[id].payload = payload;
155 base::ListValue state_map_list;
156 SerializeToList(state_map, &state_map_list);
157 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
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();
167 state_map.erase(*it);
170 base::ListValue state_map_list;
171 SerializeToList(state_map, &state_map_list);
172 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
177 void InvalidatorStorage::DeserializeFromList(
178 const base::ListValue& state_map_list,
179 InvalidationStateMap* state_map) {
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;
187 invalidation::ObjectId id;
188 syncer::InvalidationState state;
189 if (!ValueToObjectIdAndState(*value, &id, &state)) {
190 DLOG(WARNING) << "Error while deserializing entry " << i;
193 (*state_map)[id] = state;
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));
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,
219 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
222 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
225 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions);
228 // Legacy migration code.
230 void InvalidatorStorage::DeserializeMap(
231 const base::DictionaryValue* max_versions_dict,
232 InvalidationStateMap* map) {
234 // Convert from a string -> string DictionaryValue to a
235 // ModelType -> int64 map.
236 for (base::DictionaryValue::Iterator it(*max_versions_dict); !it.IsAtEnd();
238 int model_type_int = 0;
239 if (!base::StringToInt(it.key(), &model_type_int)) {
240 LOG(WARNING) << "Invalid model type key: " << it.key();
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;
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) << ": "
259 invalidation::ObjectId id;
260 if (!syncer::RealModelTypeToObjectId(model_type, &id)) {
261 DLOG(WARNING) << "Invalid model type: " << model_type;
264 (*map)[id].version = max_version;
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);
274 std::string InvalidatorStorage::GetInvalidatorClientId() const {
275 return pref_service_ ?
276 pref_service_->GetString(prefs::kInvalidatorClientId) :
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,
288 std::string InvalidatorStorage::GetBootstrapData() const {
289 std::string base64_data(
291 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState)
294 base::Base64Decode(base64_data, &data);
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);
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();
313 syncer::AckHandleMap ack_handles;
314 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end();
316 state_map[*it].expected = syncer::AckHandle::CreateUnique();
317 ack_handles.insert(std::make_pair(*it, state_map[*it].expected));
320 base::ListValue state_map_list;
321 SerializeToList(state_map, &state_map_list);
322 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
325 ignore_result(task_runner->PostTask(FROM_HERE,
326 base::Bind(callback, ack_handles)));
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();
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())
340 it->second.current = ack_handle;
342 base::ListValue state_map_list;
343 SerializeToList(state_map, &state_map_list);
344 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
348 } // namespace invalidation