Chromium Code Reviews| 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/logging.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/prefs/pref_service.h" | |
| 11 #include "chrome/common/pref_names.h" | |
| 12 | |
| 13 namespace browser_sync { | |
| 14 | |
| 15 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | |
| 16 : pref_service_(pref_service) { | |
| 17 | |
| 18 if (pref_service_) { | |
|
rlarocque
2012/05/29 18:28:24
It looks like there are if branches in several mem
tim (not reviewing)
2012/05/29 23:17:31
As we discussed offline, there are a few gotchas t
| |
| 19 pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions, | |
| 20 PrefService::UNSYNCABLE_PREF); | |
| 21 pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState, | |
| 22 std::string(), | |
| 23 PrefService::UNSYNCABLE_PREF); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 InvalidatorStorage::~InvalidatorStorage() { | |
| 28 } | |
| 29 | |
| 30 sync_notifier::InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() | |
| 31 const { | |
|
rlarocque
2012/05/29 18:28:24
This indent looks odd, though I think it's technic
tim (not reviewing)
2012/05/29 23:17:31
Done.
| |
| 32 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 33 if (!pref_service_) { | |
| 34 return sync_notifier::InvalidationVersionMap(); | |
| 35 } | |
| 36 // Complicated gross code to convert from a string -> string | |
|
rlarocque
2012/05/29 18:28:24
Consider moving this into a 'deserialize()' helper
tim (not reviewing)
2012/05/29 23:17:31
Done.
| |
| 37 // DictionaryValue to a ModelType -> int64 map. | |
| 38 const base::DictionaryValue* max_versions_dict = | |
| 39 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); | |
| 40 CHECK(max_versions_dict); | |
| 41 sync_notifier::InvalidationVersionMap max_versions; | |
| 42 for (base::DictionaryValue::key_iterator it = | |
| 43 max_versions_dict->begin_keys(); | |
| 44 it != max_versions_dict->end_keys(); ++it) { | |
| 45 int model_type_int = 0; | |
| 46 if (!base::StringToInt(*it, &model_type_int)) { | |
| 47 LOG(WARNING) << "Invalid model type key: " << *it; | |
| 48 continue; | |
| 49 } | |
| 50 if ((model_type_int < syncable::FIRST_REAL_MODEL_TYPE) || | |
| 51 (model_type_int >= syncable::MODEL_TYPE_COUNT)) { | |
| 52 LOG(WARNING) << "Out-of-range model type key: " << model_type_int; | |
| 53 continue; | |
| 54 } | |
| 55 const syncable::ModelType model_type = | |
| 56 syncable::ModelTypeFromInt(model_type_int); | |
| 57 std::string max_version_str; | |
| 58 CHECK(max_versions_dict->GetString(*it, &max_version_str)); | |
| 59 int64 max_version = 0; | |
| 60 if (!base::StringToInt64(max_version_str, &max_version)) { | |
| 61 LOG(WARNING) << "Invalid max invalidation version for " | |
| 62 << syncable::ModelTypeToString(model_type) << ": " | |
| 63 << max_version_str; | |
| 64 continue; | |
| 65 } | |
| 66 max_versions[model_type] = max_version; | |
| 67 } | |
| 68 return max_versions; | |
| 69 } | |
| 70 | |
| 71 void InvalidatorStorage::SetMaxVersion(syncable::ModelType model_type, | |
| 72 int64 max_version) { | |
| 73 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 74 DCHECK(syncable::IsRealDataType(model_type)); | |
| 75 CHECK(pref_service_); | |
| 76 sync_notifier::InvalidationVersionMap max_versions = | |
| 77 GetAllMaxVersions(); | |
| 78 sync_notifier::InvalidationVersionMap::iterator it = | |
| 79 max_versions.find(model_type); | |
| 80 if ((it != max_versions.end()) && (max_version <= it->second)) { | |
| 81 NOTREACHED(); | |
| 82 return; | |
| 83 } | |
| 84 max_versions[model_type] = max_version; | |
| 85 | |
| 86 // Gross code to convert from a ModelType -> int64 map to a string | |
|
rlarocque
2012/05/29 18:28:24
Consider moving this to a serialize() helper funct
tim (not reviewing)
2012/05/29 23:17:31
Done.
| |
| 87 // -> string DictionaryValue. | |
| 88 base::DictionaryValue max_versions_dict; | |
| 89 for (sync_notifier::InvalidationVersionMap::const_iterator it = | |
| 90 max_versions.begin(); | |
| 91 it != max_versions.end(); ++it) { | |
| 92 max_versions_dict.SetString( | |
| 93 base::IntToString(it->first), | |
| 94 base::Int64ToString(it->second)); | |
| 95 } | |
| 96 pref_service_->Set(prefs::kSyncMaxInvalidationVersions, max_versions_dict); | |
| 97 } | |
| 98 | |
| 99 std::string InvalidatorStorage::GetInvalidationState() const { | |
| 100 DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140."; | |
| 101 return pref_service_ ? | |
| 102 pref_service_->GetString(prefs::kInvalidatorInvalidationState) : ""; | |
| 103 } | |
| 104 | |
| 105 void InvalidatorStorage::SetInvalidationState(const std::string& state) { | |
| 106 DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140."; | |
| 107 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 108 pref_service_->SetString(prefs::kInvalidatorInvalidationState, | |
| 109 state); | |
| 110 } | |
| 111 | |
| 112 void InvalidatorStorage::Clear() { | |
| 113 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 114 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | |
| 115 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); | |
| 116 } | |
| 117 | |
| 118 } // namespace browser_sync | |
| OLD | NEW |