| 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 using sync_notifier::InvalidationVersionMap; |
| 14 |
| 15 namespace browser_sync { |
| 16 |
| 17 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
| 18 : pref_service_(pref_service) { |
| 19 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case |
| 20 // throughout this file. This is a problem now due to lack of injection at |
| 21 // ProfileSyncService. Bug 130176. |
| 22 if (pref_service_) { |
| 23 pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions, |
| 24 PrefService::UNSYNCABLE_PREF); |
| 25 pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState, |
| 26 std::string(), |
| 27 PrefService::UNSYNCABLE_PREF); |
| 28 } |
| 29 } |
| 30 |
| 31 InvalidatorStorage::~InvalidatorStorage() { |
| 32 } |
| 33 |
| 34 InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() const { |
| 35 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 36 if (!pref_service_) { |
| 37 return InvalidationVersionMap(); |
| 38 } |
| 39 |
| 40 const base::DictionaryValue* max_versions_dict = |
| 41 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); |
| 42 CHECK(max_versions_dict); |
| 43 InvalidationVersionMap max_versions; |
| 44 DeserializeMap(max_versions_dict, &max_versions); |
| 45 return max_versions; |
| 46 } |
| 47 |
| 48 void InvalidatorStorage::SetMaxVersion(syncable::ModelType model_type, |
| 49 int64 max_version) { |
| 50 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 51 DCHECK(syncable::IsRealDataType(model_type)); |
| 52 CHECK(pref_service_); |
| 53 InvalidationVersionMap max_versions = |
| 54 GetAllMaxVersions(); |
| 55 InvalidationVersionMap::iterator it = |
| 56 max_versions.find(model_type); |
| 57 if ((it != max_versions.end()) && (max_version <= it->second)) { |
| 58 NOTREACHED(); |
| 59 return; |
| 60 } |
| 61 max_versions[model_type] = max_version; |
| 62 |
| 63 base::DictionaryValue max_versions_dict; |
| 64 SerializeMap(max_versions, &max_versions_dict); |
| 65 pref_service_->Set(prefs::kSyncMaxInvalidationVersions, max_versions_dict); |
| 66 } |
| 67 |
| 68 // static |
| 69 void InvalidatorStorage::DeserializeMap( |
| 70 const base::DictionaryValue* max_versions_dict, |
| 71 InvalidationVersionMap* map) { |
| 72 map->clear(); |
| 73 // Convert from a string -> string DictionaryValue to a |
| 74 // ModelType -> int64 map |
| 75 // . |
| 76 for (base::DictionaryValue::key_iterator it = |
| 77 max_versions_dict->begin_keys(); |
| 78 it != max_versions_dict->end_keys(); ++it) { |
| 79 int model_type_int = 0; |
| 80 if (!base::StringToInt(*it, &model_type_int)) { |
| 81 LOG(WARNING) << "Invalid model type key: " << *it; |
| 82 continue; |
| 83 } |
| 84 if ((model_type_int < syncable::FIRST_REAL_MODEL_TYPE) || |
| 85 (model_type_int >= syncable::MODEL_TYPE_COUNT)) { |
| 86 LOG(WARNING) << "Out-of-range model type key: " << model_type_int; |
| 87 continue; |
| 88 } |
| 89 const syncable::ModelType model_type = |
| 90 syncable::ModelTypeFromInt(model_type_int); |
| 91 std::string max_version_str; |
| 92 CHECK(max_versions_dict->GetString(*it, &max_version_str)); |
| 93 int64 max_version = 0; |
| 94 if (!base::StringToInt64(max_version_str, &max_version)) { |
| 95 LOG(WARNING) << "Invalid max invalidation version for " |
| 96 << syncable::ModelTypeToString(model_type) << ": " |
| 97 << max_version_str; |
| 98 continue; |
| 99 } |
| 100 (*map)[model_type] = max_version; |
| 101 } |
| 102 } |
| 103 |
| 104 // static |
| 105 void InvalidatorStorage::SerializeMap( |
| 106 const InvalidationVersionMap& map, base::DictionaryValue* to_dict) { |
| 107 // Convert from a ModelType -> int64 map to a string -> string |
| 108 // DictionaryValue. |
| 109 for (InvalidationVersionMap::const_iterator it = map.begin(); |
| 110 it != map.end(); ++it) { |
| 111 to_dict->SetString( |
| 112 base::IntToString(it->first), |
| 113 base::Int64ToString(it->second)); |
| 114 } |
| 115 } |
| 116 |
| 117 std::string InvalidatorStorage::GetInvalidationState() const { |
| 118 DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140."; |
| 119 return pref_service_ ? |
| 120 pref_service_->GetString(prefs::kInvalidatorInvalidationState) : ""; |
| 121 } |
| 122 |
| 123 void InvalidatorStorage::SetInvalidationState(const std::string& state) { |
| 124 DLOG(WARNING) << "TODO(tim): Wire this up. Bug 124140."; |
| 125 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 126 pref_service_->SetString(prefs::kInvalidatorInvalidationState, |
| 127 state); |
| 128 } |
| 129 |
| 130 void InvalidatorStorage::Clear() { |
| 131 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 132 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
| 133 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); |
| 134 } |
| 135 |
| 136 } // namespace browser_sync |
| OLD | NEW |