Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Side by Side Diff: chrome/browser/sync/invalidations/invalidator_storage.cc

Issue 13991017: Commit InvalidationService implementations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update OWNERS file Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, &current_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(PrefRegistrySyncable* registry) {
99 registry->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions,
100 PrefRegistrySyncable::UNSYNCABLE_PREF);
101 registry->RegisterStringPref(prefs::kInvalidatorInvalidationState,
102 std::string(),
103 PrefRegistrySyncable::UNSYNCABLE_PREF);
104 registry->RegisterStringPref(prefs::kInvalidatorClientId,
105 std::string(),
106 PrefRegistrySyncable::UNSYNCABLE_PREF);
107 registry->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions,
108 PrefRegistrySyncable::UNSYNCABLE_PREF);
109 }
110
111 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service)
112 : pref_service_(pref_service) {
113 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case
114 // throughout this file. This is a problem now due to lack of injection at
115 // ProfileSyncService. Bug 130176.
116 if (pref_service_)
117 MigrateMaxInvalidationVersionsPref();
118 }
119
120 InvalidatorStorage::~InvalidatorStorage() {
121 }
122
123 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const {
124 DCHECK(thread_checker_.CalledOnValidThread());
125 InvalidationStateMap state_map;
126 if (!pref_service_) {
127 return state_map;
128 }
129 const base::ListValue* state_map_list =
130 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions);
131 CHECK(state_map_list);
132 DeserializeFromList(*state_map_list, &state_map);
133 return state_map;
134 }
135
136 void InvalidatorStorage::SetMaxVersionAndPayload(
137 const invalidation::ObjectId& id,
138 int64 max_version,
139 const std::string& payload) {
140 DCHECK(thread_checker_.CalledOnValidThread());
141 CHECK(pref_service_);
142 InvalidationStateMap state_map = GetAllInvalidationStates();
143 InvalidationStateMap::iterator it = state_map.find(id);
144 if ((it != state_map.end()) && (max_version <= it->second.version)) {
145 NOTREACHED();
146 return;
147 }
148 state_map[id].version = max_version;
149 state_map[id].payload = payload;
150
151 base::ListValue state_map_list;
152 SerializeToList(state_map, &state_map_list);
153 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
154 state_map_list);
155 }
156
157 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) {
158 DCHECK(thread_checker_.CalledOnValidThread());
159 CHECK(pref_service_);
160 InvalidationStateMap state_map = GetAllInvalidationStates();
161 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end();
162 ++it) {
163 state_map.erase(*it);
164 }
165
166 base::ListValue state_map_list;
167 SerializeToList(state_map, &state_map_list);
168 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
169 state_map_list);
170 }
171
172 // static
173 void InvalidatorStorage::DeserializeFromList(
174 const base::ListValue& state_map_list,
175 InvalidationStateMap* state_map) {
176 state_map->clear();
177 for (size_t i = 0; i < state_map_list.GetSize(); ++i) {
178 const DictionaryValue* value = NULL;
179 if (!state_map_list.GetDictionary(i, &value)) {
180 DLOG(WARNING) << "Unable to deserialize entry " << i;
181 continue;
182 }
183 invalidation::ObjectId id;
184 syncer::InvalidationState state;
185 if (!ValueToObjectIdAndState(*value, &id, &state)) {
186 DLOG(WARNING) << "Error while deserializing entry " << i;
187 continue;
188 }
189 (*state_map)[id] = state;
190 }
191 }
192
193 // static
194 void InvalidatorStorage::SerializeToList(
195 const InvalidationStateMap& state_map,
196 base::ListValue* state_map_list) {
197 for (InvalidationStateMap::const_iterator it = state_map.begin();
198 it != state_map.end(); ++it) {
199 state_map_list->Append(ObjectIdAndStateToValue(it->first, it->second));
200 }
201 }
202
203 // Legacy migration code.
204 void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() {
205 const base::DictionaryValue* max_versions_dict =
206 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions);
207 CHECK(max_versions_dict);
208 if (!max_versions_dict->empty()) {
209 InvalidationStateMap state_map;
210 DeserializeMap(max_versions_dict, &state_map);
211 base::ListValue state_map_list;
212 SerializeToList(state_map, &state_map_list);
213 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
214 state_map_list);
215 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
216 true);
217 } else {
218 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref",
219 false);
220 }
221 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions);
222 }
223
224 // Legacy migration code.
225 // static
226 void InvalidatorStorage::DeserializeMap(
227 const base::DictionaryValue* max_versions_dict,
228 InvalidationStateMap* map) {
229 map->clear();
230 // Convert from a string -> string DictionaryValue to a
231 // ModelType -> int64 map.
232 for (base::DictionaryValue::Iterator it(*max_versions_dict); !it.IsAtEnd();
233 it.Advance()) {
234 int model_type_int = 0;
235 if (!base::StringToInt(it.key(), &model_type_int)) {
236 LOG(WARNING) << "Invalid model type key: " << it.key();
237 continue;
238 }
239 if ((model_type_int < syncer::FIRST_REAL_MODEL_TYPE) ||
240 (model_type_int >= syncer::MODEL_TYPE_COUNT)) {
241 LOG(WARNING) << "Out-of-range model type key: " << model_type_int;
242 continue;
243 }
244 const syncer::ModelType model_type =
245 syncer::ModelTypeFromInt(model_type_int);
246 std::string max_version_str;
247 CHECK(it.value().GetAsString(&max_version_str));
248 int64 max_version = 0;
249 if (!base::StringToInt64(max_version_str, &max_version)) {
250 LOG(WARNING) << "Invalid max invalidation version for "
251 << syncer::ModelTypeToString(model_type) << ": "
252 << max_version_str;
253 continue;
254 }
255 invalidation::ObjectId id;
256 if (!syncer::RealModelTypeToObjectId(model_type, &id)) {
257 DLOG(WARNING) << "Invalid model type: " << model_type;
258 continue;
259 }
260 (*map)[id].version = max_version;
261 }
262 }
263
264 void InvalidatorStorage::SetInvalidatorClientId(const std::string& client_id) {
265 DCHECK(thread_checker_.CalledOnValidThread());
266 Clear(); // We can't reuse our old invalidation state if the ID changes.
267 pref_service_->SetString(prefs::kInvalidatorClientId, client_id);
268 }
269
270 std::string InvalidatorStorage::GetInvalidatorClientId() const {
271 return pref_service_ ?
272 pref_service_->GetString(prefs::kInvalidatorClientId) :
273 std::string();
274 }
275
276 void InvalidatorStorage::SetBootstrapData(const std::string& data) {
277 DCHECK(thread_checker_.CalledOnValidThread());
278 std::string base64_data;
279 base::Base64Encode(data, &base64_data);
280 pref_service_->SetString(prefs::kInvalidatorInvalidationState,
281 base64_data);
282 }
283
284 std::string InvalidatorStorage::GetBootstrapData() const {
285 std::string base64_data(
286 pref_service_
287 ? pref_service_->GetString(prefs::kInvalidatorInvalidationState)
288 : std::string());
289 std::string data;
290 base::Base64Decode(base64_data, &data);
291 return data;
292 }
293
294 void InvalidatorStorage::Clear() {
295 DCHECK(thread_checker_.CalledOnValidThread());
296 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions);
297 pref_service_->ClearPref(prefs::kInvalidatorClientId);
298 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState);
299 }
300
301 void InvalidatorStorage::GenerateAckHandles(
302 const syncer::ObjectIdSet& ids,
303 const scoped_refptr<base::TaskRunner>& task_runner,
304 const base::Callback<void(const syncer::AckHandleMap&)> callback) {
305 DCHECK(thread_checker_.CalledOnValidThread());
306 CHECK(pref_service_);
307 InvalidationStateMap state_map = GetAllInvalidationStates();
308
309 syncer::AckHandleMap ack_handles;
310 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end();
311 ++it) {
312 state_map[*it].expected = syncer::AckHandle::CreateUnique();
313 ack_handles.insert(std::make_pair(*it, state_map[*it].expected));
314 }
315
316 base::ListValue state_map_list;
317 SerializeToList(state_map, &state_map_list);
318 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
319 state_map_list);
320
321 ignore_result(task_runner->PostTask(FROM_HERE,
322 base::Bind(callback, ack_handles)));
323 }
324
325 void InvalidatorStorage::Acknowledge(const invalidation::ObjectId& id,
326 const syncer::AckHandle& ack_handle) {
327 DCHECK(thread_checker_.CalledOnValidThread());
328 CHECK(pref_service_);
329 InvalidationStateMap state_map = GetAllInvalidationStates();
330
331 InvalidationStateMap::iterator it = state_map.find(id);
332 // This could happen if the acknowledgement is delayed and Forget() has
333 // already been called.
334 if (it == state_map.end())
335 return;
336 it->second.current = ack_handle;
337
338 base::ListValue state_map_list;
339 SerializeToList(state_map, &state_map_list);
340 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions,
341 state_map_list);
342 }
343
344 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698