| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/invalidations/invalidator_storage.h" | 5 #include "chrome/browser/sync/invalidations/invalidator_storage.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
| 15 | 15 |
| 16 using syncer::InvalidationVersionMap; | 16 using syncer::InvalidationStateMap; |
| 17 | 17 |
| 18 namespace browser_sync { | 18 namespace browser_sync { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kSourceKey[] = "source"; | 22 const char kSourceKey[] = "source"; |
| 23 const char kNameKey[] = "name"; | 23 const char kNameKey[] = "name"; |
| 24 const char kMaxVersionKey[] = "max-version"; | 24 const char kMaxVersionKey[] = "max-version"; |
| 25 | 25 |
| 26 bool ValueToObjectIdAndVersion(const DictionaryValue& value, | 26 bool ValueToObjectIdAndState(const DictionaryValue& value, |
| 27 invalidation::ObjectId* id, | 27 invalidation::ObjectId* id, |
| 28 int64* max_version) { | 28 syncer::InvalidationState* state) { |
| 29 std::string source_str; | 29 std::string source_str; |
| 30 int source = 0; | 30 int source = 0; |
| 31 std::string name; | 31 std::string name; |
| 32 std::string max_version_str; | 32 std::string max_version_str; |
| 33 if (!value.GetString(kSourceKey, &source_str)) { | 33 if (!value.GetString(kSourceKey, &source_str)) { |
| 34 DLOG(WARNING) << "Unable to deserialize source"; | 34 DLOG(WARNING) << "Unable to deserialize source"; |
| 35 return false; | 35 return false; |
| 36 } | 36 } |
| 37 if (!value.GetString(kNameKey, &name)) { | 37 if (!value.GetString(kNameKey, &name)) { |
| 38 DLOG(WARNING) << "Unable to deserialize name"; | 38 DLOG(WARNING) << "Unable to deserialize name"; |
| 39 return false; | 39 return false; |
| 40 } | 40 } |
| 41 if (!value.GetString(kMaxVersionKey, &max_version_str)) { | 41 if (!value.GetString(kMaxVersionKey, &max_version_str)) { |
| 42 DLOG(WARNING) << "Unable to deserialize max version"; | 42 DLOG(WARNING) << "Unable to deserialize max version"; |
| 43 return false; | 43 return false; |
| 44 } | 44 } |
| 45 if (!base::StringToInt(source_str, &source)) { | 45 if (!base::StringToInt(source_str, &source)) { |
| 46 DLOG(WARNING) << "Invalid source: " << source_str; | 46 DLOG(WARNING) << "Invalid source: " << source_str; |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 if (!base::StringToInt64(max_version_str, max_version)) { | 49 if (!base::StringToInt64(max_version_str, &state->version)) { |
| 50 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; | 50 DLOG(WARNING) << "Invalid max invalidation version: " << max_version_str; |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| 53 *id = invalidation::ObjectId(source, name); | 53 *id = invalidation::ObjectId(source, name); |
| 54 return true; | 54 return true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // The caller owns the returned value. | 57 // The caller owns the returned value. |
| 58 DictionaryValue* ObjectIdAndVersionToValue(const invalidation::ObjectId& id, | 58 DictionaryValue* ObjectIdAndStateToValue( |
| 59 int64 max_version) { | 59 const invalidation::ObjectId& id, const syncer::InvalidationState& state) { |
| 60 DictionaryValue* value = new DictionaryValue; | 60 DictionaryValue* value = new DictionaryValue; |
| 61 value->SetString(kSourceKey, base::IntToString(id.source())); | 61 value->SetString(kSourceKey, base::IntToString(id.source())); |
| 62 value->SetString(kNameKey, id.name()); | 62 value->SetString(kNameKey, id.name()); |
| 63 value->SetString(kMaxVersionKey, base::Int64ToString(max_version)); | 63 value->SetString(kMaxVersionKey, base::Int64ToString(state.version)); |
| 64 return value; | 64 return value; |
| 65 } | 65 } |
| 66 | 66 |
| 67 } // namespace | 67 } // namespace |
| 68 | 68 |
| 69 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) | 69 InvalidatorStorage::InvalidatorStorage(PrefService* pref_service) |
| 70 : pref_service_(pref_service) { | 70 : pref_service_(pref_service) { |
| 71 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case | 71 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case |
| 72 // throughout this file. This is a problem now due to lack of injection at | 72 // throughout this file. This is a problem now due to lack of injection at |
| 73 // ProfileSyncService. Bug 130176. | 73 // ProfileSyncService. Bug 130176. |
| 74 if (pref_service_) { | 74 if (pref_service_) { |
| 75 pref_service_->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions, | 75 pref_service_->RegisterListPref(prefs::kInvalidatorMaxInvalidationVersions, |
| 76 PrefService::UNSYNCABLE_PREF); | 76 PrefService::UNSYNCABLE_PREF); |
| 77 pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState, | 77 pref_service_->RegisterStringPref(prefs::kInvalidatorInvalidationState, |
| 78 std::string(), | 78 std::string(), |
| 79 PrefService::UNSYNCABLE_PREF); | 79 PrefService::UNSYNCABLE_PREF); |
| 80 | 80 |
| 81 MigrateMaxInvalidationVersionsPref(); | 81 MigrateMaxInvalidationVersionsPref(); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 InvalidatorStorage::~InvalidatorStorage() { | 85 InvalidatorStorage::~InvalidatorStorage() { |
| 86 } | 86 } |
| 87 | 87 |
| 88 InvalidationVersionMap InvalidatorStorage::GetAllMaxVersions() const { | 88 InvalidationStateMap InvalidatorStorage::GetAllInvalidationStates() const { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 InvalidationVersionMap max_versions; | 90 InvalidationStateMap state_map; |
| 91 if (!pref_service_) { | 91 if (!pref_service_) { |
| 92 return max_versions; | 92 return state_map; |
| 93 } | 93 } |
| 94 const base::ListValue* max_versions_list = | 94 const base::ListValue* state_map_list = |
| 95 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); | 95 pref_service_->GetList(prefs::kInvalidatorMaxInvalidationVersions); |
| 96 CHECK(max_versions_list); | 96 CHECK(state_map_list); |
| 97 DeserializeFromList(*max_versions_list, &max_versions); | 97 DeserializeFromList(*state_map_list, &state_map); |
| 98 return max_versions; | 98 return state_map; |
| 99 } | 99 } |
| 100 | 100 |
| 101 void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, | 101 void InvalidatorStorage::SetMaxVersion(const invalidation::ObjectId& id, |
| 102 int64 max_version) { | 102 int64 max_version) { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 CHECK(pref_service_); | 104 CHECK(pref_service_); |
| 105 InvalidationVersionMap max_versions = GetAllMaxVersions(); | 105 InvalidationStateMap state_map = GetAllInvalidationStates(); |
| 106 InvalidationVersionMap::iterator it = max_versions.find(id); | 106 InvalidationStateMap::iterator it = state_map.find(id); |
| 107 if ((it != max_versions.end()) && (max_version <= it->second)) { | 107 if ((it != state_map.end()) && (max_version <= it->second.version)) { |
| 108 NOTREACHED(); | 108 NOTREACHED(); |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 max_versions[id] = max_version; | 111 state_map[id].version = max_version; |
| 112 | 112 |
| 113 base::ListValue max_versions_list; | 113 base::ListValue state_map_list; |
| 114 SerializeToList(max_versions, &max_versions_list); | 114 SerializeToList(state_map, &state_map_list); |
| 115 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 115 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
| 116 max_versions_list); | 116 state_map_list); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { | 119 void InvalidatorStorage::Forget(const syncer::ObjectIdSet& ids) { |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 CHECK(pref_service_); | 121 CHECK(pref_service_); |
| 122 InvalidationVersionMap max_versions = GetAllMaxVersions(); | 122 InvalidationStateMap state_map = GetAllInvalidationStates(); |
| 123 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); | 123 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
| 124 ++it) { | 124 ++it) { |
| 125 max_versions.erase(*it); | 125 state_map.erase(*it); |
| 126 } | 126 } |
| 127 | 127 |
| 128 base::ListValue max_versions_list; | 128 base::ListValue state_map_list; |
| 129 SerializeToList(max_versions, &max_versions_list); | 129 SerializeToList(state_map, &state_map_list); |
| 130 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 130 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
| 131 max_versions_list); | 131 state_map_list); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // static | 134 // static |
| 135 void InvalidatorStorage::DeserializeFromList( | 135 void InvalidatorStorage::DeserializeFromList( |
| 136 const base::ListValue& max_versions_list, | 136 const base::ListValue& state_map_list, |
| 137 InvalidationVersionMap* max_versions_map) { | 137 InvalidationStateMap* state_map) { |
| 138 max_versions_map->clear(); | 138 state_map->clear(); |
| 139 for (size_t i = 0; i < max_versions_list.GetSize(); ++i) { | 139 for (size_t i = 0; i < state_map_list.GetSize(); ++i) { |
| 140 const DictionaryValue* value = NULL; | 140 const DictionaryValue* value = NULL; |
| 141 if (!max_versions_list.GetDictionary(i, &value)) { | 141 if (!state_map_list.GetDictionary(i, &value)) { |
| 142 DLOG(WARNING) << "Unable to deserialize entry " << i; | 142 DLOG(WARNING) << "Unable to deserialize entry " << i; |
| 143 continue; | 143 continue; |
| 144 } | 144 } |
| 145 invalidation::ObjectId id; | 145 invalidation::ObjectId id; |
| 146 int64 max_version = 0; | 146 syncer::InvalidationState state; |
| 147 if (!ValueToObjectIdAndVersion(*value, &id, &max_version)) { | 147 if (!ValueToObjectIdAndState(*value, &id, &state)) { |
| 148 DLOG(WARNING) << "Error while deserializing entry " << i; | 148 DLOG(WARNING) << "Error while deserializing entry " << i; |
| 149 continue; | 149 continue; |
| 150 } | 150 } |
| 151 (*max_versions_map)[id] = max_version; | 151 (*state_map)[id] = state; |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 // static | 155 // static |
| 156 void InvalidatorStorage::SerializeToList( | 156 void InvalidatorStorage::SerializeToList( |
| 157 const InvalidationVersionMap& max_versions_map, | 157 const InvalidationStateMap& state_map, |
| 158 base::ListValue* max_versions_list) { | 158 base::ListValue* state_map_list) { |
| 159 for (InvalidationVersionMap::const_iterator it = max_versions_map.begin(); | 159 for (InvalidationStateMap::const_iterator it = state_map.begin(); |
| 160 it != max_versions_map.end(); ++it) { | 160 it != state_map.end(); ++it) { |
| 161 max_versions_list->Append(ObjectIdAndVersionToValue(it->first, it->second)); | 161 state_map_list->Append(ObjectIdAndStateToValue(it->first, it->second)); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 // Legacy migration code. | 165 // Legacy migration code. |
| 166 void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() { | 166 void InvalidatorStorage::MigrateMaxInvalidationVersionsPref() { |
| 167 pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions, | 167 pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions, |
| 168 PrefService::UNSYNCABLE_PREF); | 168 PrefService::UNSYNCABLE_PREF); |
| 169 const base::DictionaryValue* max_versions_dict = | 169 const base::DictionaryValue* max_versions_dict = |
| 170 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); | 170 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); |
| 171 CHECK(max_versions_dict); | 171 CHECK(max_versions_dict); |
| 172 if (!max_versions_dict->empty()) { | 172 if (!max_versions_dict->empty()) { |
| 173 InvalidationVersionMap max_versions; | 173 InvalidationStateMap state_map; |
| 174 DeserializeMap(max_versions_dict, &max_versions); | 174 DeserializeMap(max_versions_dict, &state_map); |
| 175 base::ListValue max_versions_list; | 175 base::ListValue state_map_list; |
| 176 SerializeToList(max_versions, &max_versions_list); | 176 SerializeToList(state_map, &state_map_list); |
| 177 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, | 177 pref_service_->Set(prefs::kInvalidatorMaxInvalidationVersions, |
| 178 max_versions_list); | 178 state_map_list); |
| 179 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", | 179 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", |
| 180 true); | 180 true); |
| 181 } else { | 181 } else { |
| 182 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", | 182 UMA_HISTOGRAM_BOOLEAN("InvalidatorStorage.MigrateInvalidationVersionsPref", |
| 183 false); | 183 false); |
| 184 } | 184 } |
| 185 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); | 185 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); |
| 186 } | 186 } |
| 187 | 187 |
| 188 // Legacy migration code. | 188 // Legacy migration code. |
| 189 // static | 189 // static |
| 190 void InvalidatorStorage::DeserializeMap( | 190 void InvalidatorStorage::DeserializeMap( |
| 191 const base::DictionaryValue* max_versions_dict, | 191 const base::DictionaryValue* max_versions_dict, |
| 192 InvalidationVersionMap* map) { | 192 InvalidationStateMap* map) { |
| 193 map->clear(); | 193 map->clear(); |
| 194 // Convert from a string -> string DictionaryValue to a | 194 // Convert from a string -> string DictionaryValue to a |
| 195 // ModelType -> int64 map. | 195 // ModelType -> int64 map. |
| 196 for (base::DictionaryValue::key_iterator it = | 196 for (base::DictionaryValue::key_iterator it = |
| 197 max_versions_dict->begin_keys(); | 197 max_versions_dict->begin_keys(); |
| 198 it != max_versions_dict->end_keys(); ++it) { | 198 it != max_versions_dict->end_keys(); ++it) { |
| 199 int model_type_int = 0; | 199 int model_type_int = 0; |
| 200 if (!base::StringToInt(*it, &model_type_int)) { | 200 if (!base::StringToInt(*it, &model_type_int)) { |
| 201 LOG(WARNING) << "Invalid model type key: " << *it; | 201 LOG(WARNING) << "Invalid model type key: " << *it; |
| 202 continue; | 202 continue; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 215 LOG(WARNING) << "Invalid max invalidation version for " | 215 LOG(WARNING) << "Invalid max invalidation version for " |
| 216 << syncer::ModelTypeToString(model_type) << ": " | 216 << syncer::ModelTypeToString(model_type) << ": " |
| 217 << max_version_str; | 217 << max_version_str; |
| 218 continue; | 218 continue; |
| 219 } | 219 } |
| 220 invalidation::ObjectId id; | 220 invalidation::ObjectId id; |
| 221 if (!syncer::RealModelTypeToObjectId(model_type, &id)) { | 221 if (!syncer::RealModelTypeToObjectId(model_type, &id)) { |
| 222 DLOG(WARNING) << "Invalid model type: " << model_type; | 222 DLOG(WARNING) << "Invalid model type: " << model_type; |
| 223 continue; | 223 continue; |
| 224 } | 224 } |
| 225 (*map)[id] = max_version; | 225 (*map)[id].version = max_version; |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 | 228 |
| 229 void InvalidatorStorage::SetBootstrapData(const std::string& data) { | 229 void InvalidatorStorage::SetBootstrapData(const std::string& data) { |
| 230 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
| 231 std::string base64_data; | 231 std::string base64_data; |
| 232 base::Base64Encode(data, &base64_data); | 232 base::Base64Encode(data, &base64_data); |
| 233 pref_service_->SetString(prefs::kInvalidatorInvalidationState, | 233 pref_service_->SetString(prefs::kInvalidatorInvalidationState, |
| 234 base64_data); | 234 base64_data); |
| 235 } | 235 } |
| 236 | 236 |
| 237 std::string InvalidatorStorage::GetBootstrapData() const { | 237 std::string InvalidatorStorage::GetBootstrapData() const { |
| 238 std::string base64_data(pref_service_ ? | 238 std::string base64_data(pref_service_ ? |
| 239 pref_service_->GetString(prefs::kInvalidatorInvalidationState) : ""); | 239 pref_service_->GetString(prefs::kInvalidatorInvalidationState) : ""); |
| 240 std::string data; | 240 std::string data; |
| 241 base::Base64Decode(base64_data, &data); | 241 base::Base64Decode(base64_data, &data); |
| 242 return data; | 242 return data; |
| 243 } | 243 } |
| 244 | 244 |
| 245 void InvalidatorStorage::Clear() { | 245 void InvalidatorStorage::Clear() { |
| 246 DCHECK(thread_checker_.CalledOnValidThread()); | 246 DCHECK(thread_checker_.CalledOnValidThread()); |
| 247 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); | 247 pref_service_->ClearPref(prefs::kInvalidatorMaxInvalidationVersions); |
| 248 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); | 248 pref_service_->ClearPref(prefs::kInvalidatorInvalidationState); |
| 249 } | 249 } |
| 250 | 250 |
| 251 } // namespace browser_sync | 251 } // namespace browser_sync |
| OLD | NEW |