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

Unified Diff: chrome/browser/sync/invalidations/invalidator_storage.cc

Issue 10451058: sync: move invalidation version prefs out of SyncPrefs into InvalidatorStorage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sync/invalidations/invalidator_storage.cc
diff --git a/chrome/browser/sync/invalidations/invalidator_storage.cc b/chrome/browser/sync/invalidations/invalidator_storage.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e2eb055701f1c67c8781d6c5f750db58dee28932
--- /dev/null
+++ b/chrome/browser/sync/invalidations/invalidator_storage.cc
@@ -0,0 +1,136 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/sync/invalidations/invalidator_storage.h"
+
+#include "base/logging.h"
+#include "base/string_number_conversions.h"
+#include "base/values.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/common/pref_names.h"
+
+using sync_notifier::InvalidationVersionMap;
+
+namespace browser_sync {
+
+InvalidatorStorage::InvalidatorStorage(PrefService* pref_service)
+ : pref_service_(pref_service) {
+ // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case
+ // throughout this file. This is a problem now due to lack of injection at
+ // ProfileSyncService. Bug 130176.
+ if (pref_service_) {
+ pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions,
+ PrefService::UNSYNCABLE_PREF);
+ pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState,
+ std::string(),
+ PrefService::UNSYNCABLE_PREF);
+ }
+}
+
+InvalidatorStorage::~InvalidatorStorage() {
+}
+
+InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() const {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ if (!pref_service_) {
+ return InvalidationVersionMap();
+ }
+
+ const base::DictionaryValue* max_versions_dict =
+ pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions);
+ CHECK(max_versions_dict);
+ InvalidationVersionMap max_versions;
+ DeserializeMap(max_versions_dict, &max_versions);
+ return max_versions;
+}
+
+void InvalidatorStorage::SetMaxVersion(syncable::ModelType model_type,
+ int64 max_version) {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ DCHECK(syncable::IsRealDataType(model_type));
+ CHECK(pref_service_);
+ InvalidationVersionMap max_versions =
+ GetAllMaxVersions();
+ InvalidationVersionMap::iterator it =
+ max_versions.find(model_type);
+ if ((it != max_versions.end()) && (max_version <= it->second)) {
+ NOTREACHED();
+ return;
+ }
+ max_versions[model_type] = max_version;
+
+ base::DictionaryValue max_versions_dict;
+ SerializeMap(max_versions, &max_versions_dict);
+ pref_service_->Set(prefs::kSyncMaxInvalidationVersions, max_versions_dict);
+}
+
+// static
+void InvalidatorStorage::DeserializeMap(
+ const base::DictionaryValue* max_versions_dict,
+ InvalidationVersionMap* map) {
+ map->clear();
+ // Convert from a string -> string DictionaryValue to a
+ // ModelType -> int64 map
+ // .
+ for (base::DictionaryValue::key_iterator it =
+ max_versions_dict->begin_keys();
+ it != max_versions_dict->end_keys(); ++it) {
+ int model_type_int = 0;
+ if (!base::StringToInt(*it, &model_type_int)) {
+ LOG(WARNING) << "Invalid model type key: " << *it;
+ continue;
+ }
+ if ((model_type_int < syncable::FIRST_REAL_MODEL_TYPE) ||
+ (model_type_int >= syncable::MODEL_TYPE_COUNT)) {
+ LOG(WARNING) << "Out-of-range model type key: " << model_type_int;
+ continue;
+ }
+ const syncable::ModelType model_type =
+ syncable::ModelTypeFromInt(model_type_int);
+ std::string max_version_str;
+ CHECK(max_versions_dict->GetString(*it, &max_version_str));
+ int64 max_version = 0;
+ if (!base::StringToInt64(max_version_str, &max_version)) {
+ LOG(WARNING) << "Invalid max invalidation version for "
+ << syncable::ModelTypeToString(model_type) << ": "
+ << max_version_str;
+ continue;
+ }
+ (*map)[model_type] = max_version;
+ }
+}
+
+// static
+void InvalidatorStorage::SerializeMap(
+ const InvalidationVersionMap& map, base::DictionaryValue* to_dict) {
+ // Convert from a ModelType -> int64 map to a string -> string
+ // DictionaryValue.
+ for (InvalidationVersionMap::const_iterator it = map.begin();
+ it != map.end(); ++it) {
+ to_dict->SetString(
+ base::IntToString(it->first),
+ base::Int64ToString(it->second));
+ }
+}
+
+std::string InvalidatorStorage::GetInvalidationState() const {
+ DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140.";
+ return pref_service_ ?
+ pref_service_->GetString(prefs::kInvalidatorInvalidationState) : "";
+}
+
+void InvalidatorStorage::SetInvalidationState(const std::string& state) {
+ DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140.";
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ pref_service_->SetString(prefs::kInvalidatorInvalidationState,
+ state);
+}
+
+void InvalidatorStorage::Clear() {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ pref_service_->ClearPref(prefs::kInvalidatorInvalidationState);
+ pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions);
+}
+
+} // namespace browser_sync

Powered by Google App Engine
This is Rietveld 408576698