| 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/sync_prefs.h" | 5 #include "chrome/browser/sync/sync_prefs.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
| 15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
| 16 | 16 |
| 17 namespace browser_sync { | 17 namespace browser_sync { |
| 18 | 18 |
| 19 SyncPrefObserver::~SyncPrefObserver() {} | 19 SyncPrefObserver::~SyncPrefObserver() {} |
| 20 | 20 |
| 21 SyncPrefs::SyncPrefs(PrefService* pref_service) | 21 SyncPrefs::SyncPrefs(PrefService* pref_service) |
| 22 : pref_service_(pref_service) { | 22 : pref_service_(pref_service) { |
| 23 RegisterPrefGroups(); | 23 RegisterPrefGroups(); |
| 24 // TODO(tim): Create a Mock instead of maintaining the if(!pref_service_) case |
| 25 // throughout this file. This is a problem now due to lack of injection at |
| 26 // ProfileSyncService. Bug 130176. |
| 24 if (pref_service_) { | 27 if (pref_service_) { |
| 25 RegisterPreferences(); | 28 RegisterPreferences(); |
| 26 // Watch the preference that indicates sync is managed so we can take | 29 // Watch the preference that indicates sync is managed so we can take |
| 27 // appropriate action. | 30 // appropriate action. |
| 28 pref_sync_managed_.Init(prefs::kSyncManaged, pref_service_, this); | 31 pref_sync_managed_.Init(prefs::kSyncManaged, pref_service_, this); |
| 29 } | 32 } |
| 30 } | 33 } |
| 31 | 34 |
| 32 SyncPrefs::~SyncPrefs() { | 35 SyncPrefs::~SyncPrefs() { |
| 33 DCHECK(non_thread_safe_.CalledOnValidThread()); | 36 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 45 | 48 |
| 46 void SyncPrefs::ClearPreferences() { | 49 void SyncPrefs::ClearPreferences() { |
| 47 DCHECK(non_thread_safe_.CalledOnValidThread()); | 50 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 48 CHECK(pref_service_); | 51 CHECK(pref_service_); |
| 49 pref_service_->ClearPref(prefs::kSyncLastSyncedTime); | 52 pref_service_->ClearPref(prefs::kSyncLastSyncedTime); |
| 50 pref_service_->ClearPref(prefs::kSyncHasSetupCompleted); | 53 pref_service_->ClearPref(prefs::kSyncHasSetupCompleted); |
| 51 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken); | 54 pref_service_->ClearPref(prefs::kSyncEncryptionBootstrapToken); |
| 52 | 55 |
| 53 // TODO(nick): The current behavior does not clear | 56 // TODO(nick): The current behavior does not clear |
| 54 // e.g. prefs::kSyncBookmarks. Is that really what we want? | 57 // e.g. prefs::kSyncBookmarks. Is that really what we want? |
| 55 | |
| 56 pref_service_->ClearPref(prefs::kSyncMaxInvalidationVersions); | |
| 57 } | 58 } |
| 58 | 59 |
| 59 bool SyncPrefs::HasSyncSetupCompleted() const { | 60 bool SyncPrefs::HasSyncSetupCompleted() const { |
| 60 DCHECK(non_thread_safe_.CalledOnValidThread()); | 61 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 61 return | 62 return |
| 62 pref_service_ && | 63 pref_service_ && |
| 63 pref_service_->GetBoolean(prefs::kSyncHasSetupCompleted); | 64 pref_service_->GetBoolean(prefs::kSyncHasSetupCompleted); |
| 64 } | 65 } |
| 65 | 66 |
| 66 void SyncPrefs::SetSyncSetupCompleted() { | 67 void SyncPrefs::SetSyncSetupCompleted() { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 return pref_service_ ? | 183 return pref_service_ ? |
| 183 pref_service_->GetString(prefs::kSyncSpareBootstrapToken) : ""; | 184 pref_service_->GetString(prefs::kSyncSpareBootstrapToken) : ""; |
| 184 } | 185 } |
| 185 | 186 |
| 186 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) { | 187 void SyncPrefs::SetSpareBootstrapToken(const std::string& token) { |
| 187 DCHECK(non_thread_safe_.CalledOnValidThread()); | 188 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 188 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token); | 189 pref_service_->SetString(prefs::kSyncSpareBootstrapToken, token); |
| 189 } | 190 } |
| 190 #endif | 191 #endif |
| 191 | 192 |
| 192 sync_notifier::InvalidationVersionMap SyncPrefs::GetAllMaxVersions() const { | |
| 193 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 194 if (!pref_service_) { | |
| 195 return sync_notifier::InvalidationVersionMap(); | |
| 196 } | |
| 197 // Complicated gross code to convert from a string -> string | |
| 198 // DictionaryValue to a ModelType -> int64 map. | |
| 199 const base::DictionaryValue* max_versions_dict = | |
| 200 pref_service_->GetDictionary(prefs::kSyncMaxInvalidationVersions); | |
| 201 CHECK(max_versions_dict); | |
| 202 sync_notifier::InvalidationVersionMap max_versions; | |
| 203 for (base::DictionaryValue::key_iterator it = | |
| 204 max_versions_dict->begin_keys(); | |
| 205 it != max_versions_dict->end_keys(); ++it) { | |
| 206 int model_type_int = 0; | |
| 207 if (!base::StringToInt(*it, &model_type_int)) { | |
| 208 LOG(WARNING) << "Invalid model type key: " << *it; | |
| 209 continue; | |
| 210 } | |
| 211 if ((model_type_int < syncable::FIRST_REAL_MODEL_TYPE) || | |
| 212 (model_type_int >= syncable::MODEL_TYPE_COUNT)) { | |
| 213 LOG(WARNING) << "Out-of-range model type key: " << model_type_int; | |
| 214 continue; | |
| 215 } | |
| 216 const syncable::ModelType model_type = | |
| 217 syncable::ModelTypeFromInt(model_type_int); | |
| 218 std::string max_version_str; | |
| 219 CHECK(max_versions_dict->GetString(*it, &max_version_str)); | |
| 220 int64 max_version = 0; | |
| 221 if (!base::StringToInt64(max_version_str, &max_version)) { | |
| 222 LOG(WARNING) << "Invalid max invalidation version for " | |
| 223 << syncable::ModelTypeToString(model_type) << ": " | |
| 224 << max_version_str; | |
| 225 continue; | |
| 226 } | |
| 227 max_versions[model_type] = max_version; | |
| 228 } | |
| 229 return max_versions; | |
| 230 } | |
| 231 | |
| 232 void SyncPrefs::SetMaxVersion(syncable::ModelType model_type, | |
| 233 int64 max_version) { | |
| 234 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 235 DCHECK(syncable::IsRealDataType(model_type)); | |
| 236 CHECK(pref_service_); | |
| 237 sync_notifier::InvalidationVersionMap max_versions = | |
| 238 GetAllMaxVersions(); | |
| 239 sync_notifier::InvalidationVersionMap::iterator it = | |
| 240 max_versions.find(model_type); | |
| 241 if ((it != max_versions.end()) && (max_version <= it->second)) { | |
| 242 NOTREACHED(); | |
| 243 return; | |
| 244 } | |
| 245 max_versions[model_type] = max_version; | |
| 246 | |
| 247 // Gross code to convert from a ModelType -> int64 map to a string | |
| 248 // -> string DictionaryValue. | |
| 249 base::DictionaryValue max_versions_dict; | |
| 250 for (sync_notifier::InvalidationVersionMap::const_iterator it = | |
| 251 max_versions.begin(); | |
| 252 it != max_versions.end(); ++it) { | |
| 253 max_versions_dict.SetString( | |
| 254 base::IntToString(it->first), | |
| 255 base::Int64ToString(it->second)); | |
| 256 } | |
| 257 pref_service_->Set(prefs::kSyncMaxInvalidationVersions, max_versions_dict); | |
| 258 } | |
| 259 | 193 |
| 260 void SyncPrefs::AcknowledgeSyncedTypes( | 194 void SyncPrefs::AcknowledgeSyncedTypes( |
| 261 syncable::ModelTypeSet types) { | 195 syncable::ModelTypeSet types) { |
| 262 DCHECK(non_thread_safe_.CalledOnValidThread()); | 196 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 263 CHECK(pref_service_); | 197 CHECK(pref_service_); |
| 264 // Add the types to the current set of acknowledged | 198 // Add the types to the current set of acknowledged |
| 265 // types, and then store the resulting set in prefs. | 199 // types, and then store the resulting set in prefs. |
| 266 const syncable::ModelTypeSet acknowledged_types = | 200 const syncable::ModelTypeSet acknowledged_types = |
| 267 Union(types, | 201 Union(types, |
| 268 syncable::ModelTypeSetFromValue( | 202 syncable::ModelTypeSetFromValue( |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 model_set.Put(syncable::THEMES); | 362 model_set.Put(syncable::THEMES); |
| 429 model_set.Put(syncable::EXTENSIONS); | 363 model_set.Put(syncable::EXTENSIONS); |
| 430 model_set.Put(syncable::NIGORI); | 364 model_set.Put(syncable::NIGORI); |
| 431 model_set.Put(syncable::SEARCH_ENGINES); | 365 model_set.Put(syncable::SEARCH_ENGINES); |
| 432 model_set.Put(syncable::APPS); | 366 model_set.Put(syncable::APPS); |
| 433 model_set.Put(syncable::TYPED_URLS); | 367 model_set.Put(syncable::TYPED_URLS); |
| 434 model_set.Put(syncable::SESSIONS); | 368 model_set.Put(syncable::SESSIONS); |
| 435 pref_service_->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes, | 369 pref_service_->RegisterListPref(prefs::kSyncAcknowledgedSyncTypes, |
| 436 syncable::ModelTypeSetToValue(model_set), | 370 syncable::ModelTypeSetToValue(model_set), |
| 437 PrefService::UNSYNCABLE_PREF); | 371 PrefService::UNSYNCABLE_PREF); |
| 438 | |
| 439 pref_service_->RegisterDictionaryPref(prefs::kSyncMaxInvalidationVersions, | |
| 440 PrefService::UNSYNCABLE_PREF); | |
| 441 } | 372 } |
| 442 | 373 |
| 443 void SyncPrefs::RegisterDataTypePreferredPref(syncable::ModelType type, | 374 void SyncPrefs::RegisterDataTypePreferredPref(syncable::ModelType type, |
| 444 bool is_preferred) { | 375 bool is_preferred) { |
| 445 DCHECK(non_thread_safe_.CalledOnValidThread()); | 376 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 446 CHECK(pref_service_); | 377 CHECK(pref_service_); |
| 447 const char* pref_name = GetPrefNameForDataType(type); | 378 const char* pref_name = GetPrefNameForDataType(type); |
| 448 if (!pref_name) { | 379 if (!pref_name) { |
| 449 NOTREACHED(); | 380 NOTREACHED(); |
| 450 return; | 381 return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 if (types.Has(i->first)) | 420 if (types.Has(i->first)) |
| 490 types_with_groups.PutAll(i->second); | 421 types_with_groups.PutAll(i->second); |
| 491 else | 422 else |
| 492 types_with_groups.RemoveAll(i->second); | 423 types_with_groups.RemoveAll(i->second); |
| 493 } | 424 } |
| 494 types_with_groups.RetainAll(registered_types); | 425 types_with_groups.RetainAll(registered_types); |
| 495 return types_with_groups; | 426 return types_with_groups; |
| 496 } | 427 } |
| 497 | 428 |
| 498 } // namespace browser_sync | 429 } // namespace browser_sync |
| OLD | NEW |