| 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/prefs/pref_model_associator.h" | 5 #include "chrome/browser/prefs/pref_model_associator.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 sync_pb::EntitySpecifics* specifics) { | 41 sync_pb::EntitySpecifics* specifics) { |
| 42 if (type == syncer::PRIORITY_PREFERENCES) { | 42 if (type == syncer::PRIORITY_PREFERENCES) { |
| 43 DCHECK(!specifics->has_preference()); | 43 DCHECK(!specifics->has_preference()); |
| 44 return specifics->mutable_priority_preference()->mutable_preference(); | 44 return specifics->mutable_priority_preference()->mutable_preference(); |
| 45 } else { | 45 } else { |
| 46 DCHECK(!specifics->has_priority_preference()); | 46 DCHECK(!specifics->has_priority_preference()); |
| 47 return specifics->mutable_preference(); | 47 return specifics->mutable_preference(); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 // List of migrated preference name pairs. If a preference is migrated |
| 52 // (meaning renamed) adding the old and new preference names here will ensure |
| 53 // that the sync engine knows how to deal with the synced values coming in |
| 54 // with the old name. Preference migration itself doesn't happen here. It may |
| 55 // happen in session_startup_pref.cc. |
| 56 const struct MigratedPreferences { |
| 57 const char* const old_name; |
| 58 const char* const new_name; |
| 59 } kMigratedPreferences[] = { |
| 60 { prefs::kURLsToRestoreOnStartupOld, prefs::kURLsToRestoreOnStartup }, |
| 61 }; |
| 62 |
| 63 std::string GetOldMigratedPreferenceName(const char* preference_name) { |
| 64 for (size_t i = 0; i < arraysize(kMigratedPreferences); ++i) { |
| 65 if (!strcmp(kMigratedPreferences[i].new_name, preference_name)) |
| 66 return kMigratedPreferences[i].old_name; |
| 67 } |
| 68 return std::string(); |
| 69 } |
| 70 |
| 71 std::string GetNewMigratedPreferenceName(const char* old_preference_name) { |
| 72 for (size_t i = 0; i < arraysize(kMigratedPreferences); ++i) { |
| 73 if (!strcmp(kMigratedPreferences[i].old_name, old_preference_name)) |
| 74 return kMigratedPreferences[i].new_name; |
| 75 } |
| 76 return std::string(); |
| 77 } |
| 78 |
| 79 bool IsMigratedPreference(const char* preference_name) { |
| 80 return !GetOldMigratedPreferenceName(preference_name).empty(); |
| 81 } |
| 82 |
| 83 bool IsOldMigratedPreference(const char* old_preference_name) { |
| 84 return !GetNewMigratedPreferenceName(old_preference_name).empty(); |
| 85 } |
| 86 |
| 51 } // namespace | 87 } // namespace |
| 52 | 88 |
| 53 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type) | 89 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type) |
| 54 : models_associated_(false), | 90 : models_associated_(false), |
| 55 processing_syncer_changes_(false), | 91 processing_syncer_changes_(false), |
| 56 pref_service_(NULL), | 92 pref_service_(NULL), |
| 57 type_(type) { | 93 type_(type) { |
| 58 DCHECK(CalledOnValidThread()); | 94 DCHECK(CalledOnValidThread()); |
| 59 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); | 95 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); |
| 60 } | 96 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 71 void PrefModelAssociator::InitPrefAndAssociate( | 107 void PrefModelAssociator::InitPrefAndAssociate( |
| 72 const syncer::SyncData& sync_pref, | 108 const syncer::SyncData& sync_pref, |
| 73 const std::string& pref_name, | 109 const std::string& pref_name, |
| 74 syncer::SyncChangeList* sync_changes) { | 110 syncer::SyncChangeList* sync_changes) { |
| 75 const Value* user_pref_value = pref_service_->GetUserPrefValue( | 111 const Value* user_pref_value = pref_service_->GetUserPrefValue( |
| 76 pref_name.c_str()); | 112 pref_name.c_str()); |
| 77 VLOG(1) << "Associating preference " << pref_name; | 113 VLOG(1) << "Associating preference " << pref_name; |
| 78 | 114 |
| 79 if (sync_pref.IsValid()) { | 115 if (sync_pref.IsValid()) { |
| 80 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); | 116 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); |
| 81 DCHECK_EQ(pref_name, preference.name()); | 117 DCHECK(pref_name == preference.name() || |
| 118 (IsMigratedPreference(pref_name.c_str()) && |
| 119 preference.name() == |
| 120 GetOldMigratedPreferenceName(pref_name.c_str()))); |
| 82 | 121 |
| 83 base::JSONReader reader; | 122 base::JSONReader reader; |
| 84 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); | 123 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); |
| 85 if (!sync_value.get()) { | 124 if (!sync_value.get()) { |
| 86 LOG(ERROR) << "Failed to deserialize preference value: " | 125 LOG(ERROR) << "Failed to deserialize preference value: " |
| 87 << reader.GetErrorMessage(); | 126 << reader.GetErrorMessage(); |
| 88 return; | 127 return; |
| 89 } | 128 } |
| 90 | 129 |
| 91 if (user_pref_value) { | 130 if (user_pref_value) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 112 if (!sync_value->Equals(new_value.get())) { | 151 if (!sync_value->Equals(new_value.get())) { |
| 113 syncer::SyncData sync_data; | 152 syncer::SyncData sync_data; |
| 114 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { | 153 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { |
| 115 LOG(ERROR) << "Failed to update preference."; | 154 LOG(ERROR) << "Failed to update preference."; |
| 116 return; | 155 return; |
| 117 } | 156 } |
| 118 sync_changes->push_back( | 157 sync_changes->push_back( |
| 119 syncer::SyncChange(FROM_HERE, | 158 syncer::SyncChange(FROM_HERE, |
| 120 syncer::SyncChange::ACTION_UPDATE, | 159 syncer::SyncChange::ACTION_UPDATE, |
| 121 sync_data)); | 160 sync_data)); |
| 161 // This preference has been migrated from an old version that must be |
| 162 // kept in sync on older versions of Chrome. |
| 163 if (IsMigratedPreference(pref_name.c_str())) { |
| 164 if (!CreatePrefSyncData( |
| 165 GetOldMigratedPreferenceName(pref_name.c_str()), |
| 166 *new_value, |
| 167 &sync_data)) { |
| 168 LOG(ERROR) << "Failed to update preference."; |
| 169 return; |
| 170 } |
| 171 sync_changes->push_back( |
| 172 syncer::SyncChange(FROM_HERE, |
| 173 syncer::SyncChange::ACTION_UPDATE, |
| 174 sync_data)); |
| 175 } |
| 122 } | 176 } |
| 123 } else if (!sync_value->IsType(Value::TYPE_NULL)) { | 177 } else if (!sync_value->IsType(Value::TYPE_NULL)) { |
| 124 // Only a server value exists. Just set the local user value. | 178 // Only a server value exists. Just set the local user value. |
| 125 pref_service_->Set(pref_name.c_str(), *sync_value); | 179 pref_service_->Set(pref_name.c_str(), *sync_value); |
| 126 } else { | 180 } else { |
| 127 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 181 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
| 128 } | 182 } |
| 129 } else if (user_pref_value) { | 183 } else if (user_pref_value) { |
| 130 // The server does not know about this preference and should be added | 184 // The server does not know about this preference and should be added |
| 131 // to the syncer's database. | 185 // to the syncer's database. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 226 |
| 173 // Go through and check for all preferences we care about that sync already | 227 // Go through and check for all preferences we care about that sync already |
| 174 // knows about. | 228 // knows about. |
| 175 for (syncer::SyncDataList::const_iterator sync_iter = | 229 for (syncer::SyncDataList::const_iterator sync_iter = |
| 176 initial_sync_data.begin(); | 230 initial_sync_data.begin(); |
| 177 sync_iter != initial_sync_data.end(); | 231 sync_iter != initial_sync_data.end(); |
| 178 ++sync_iter) { | 232 ++sync_iter) { |
| 179 DCHECK_EQ(type_, sync_iter->GetDataType()); | 233 DCHECK_EQ(type_, sync_iter->GetDataType()); |
| 180 | 234 |
| 181 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); | 235 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); |
| 182 const std::string& sync_pref_name = preference.name(); | 236 std::string sync_pref_name = preference.name(); |
| 183 | 237 |
| 184 if (remaining_preferences.count(sync_pref_name) == 0) { | 238 if (remaining_preferences.count(sync_pref_name) == 0) { |
| 185 // We're not syncing this preference locally, ignore the sync data. | 239 if (IsOldMigratedPreference(sync_pref_name.c_str())) { |
| 186 // TODO(zea): Eventually we want to be able to have the syncable service | 240 // This old pref name is not syncable locally anymore but we accept |
| 187 // reconstruct all sync data for it's datatype (therefore having | 241 // changes from other Chrome installs of previous versions and migrate |
| 188 // GetAllSyncData be a complete representation). We should store this data | 242 // them to the new name. Note that we will be merging any differences |
| 189 // somewhere, even if we don't use it. | 243 // between the new and old values and sync'ing them back. |
| 190 continue; | 244 sync_pref_name = GetNewMigratedPreferenceName(sync_pref_name.c_str()); |
| 245 } else { |
| 246 // We're not syncing this preference locally, ignore the sync data. |
| 247 // TODO(zea): Eventually we want to be able to have the syncable service |
| 248 // reconstruct all sync data for its datatype (therefore having |
| 249 // GetAllSyncData be a complete representation). We should store this |
| 250 // data somewhere, even if we don't use it. |
| 251 continue; |
| 252 } |
| 253 } else { |
| 254 remaining_preferences.erase(sync_pref_name); |
| 191 } | 255 } |
| 192 | |
| 193 remaining_preferences.erase(sync_pref_name); | |
| 194 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); | 256 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); |
| 195 } | 257 } |
| 196 | 258 |
| 197 // Go through and build sync data for any remaining preferences. | 259 // Go through and build sync data for any remaining preferences. |
| 198 for (std::set<std::string>::iterator pref_name_iter = | 260 for (std::set<std::string>::iterator pref_name_iter = |
| 199 remaining_preferences.begin(); | 261 remaining_preferences.begin(); |
| 200 pref_name_iter != remaining_preferences.end(); | 262 pref_name_iter != remaining_preferences.end(); |
| 201 ++pref_name_iter) { | 263 ++pref_name_iter) { |
| 202 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); | 264 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); |
| 203 } | 265 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 218 models_associated_ = false; | 280 models_associated_ = false; |
| 219 sync_processor_.reset(); | 281 sync_processor_.reset(); |
| 220 sync_error_factory_.reset(); | 282 sync_error_factory_.reset(); |
| 221 pref_service_->OnIsSyncingChanged(); | 283 pref_service_->OnIsSyncingChanged(); |
| 222 } | 284 } |
| 223 | 285 |
| 224 scoped_ptr<Value> PrefModelAssociator::MergePreference( | 286 scoped_ptr<Value> PrefModelAssociator::MergePreference( |
| 225 const std::string& name, | 287 const std::string& name, |
| 226 const Value& local_value, | 288 const Value& local_value, |
| 227 const Value& server_value) { | 289 const Value& server_value) { |
| 228 if (name == prefs::kURLsToRestoreOnStartup) { | 290 // This function special cases preferences individually, so don't attempt |
| 291 // to merge for all migrated values. |
| 292 if (name == prefs::kURLsToRestoreOnStartup || |
| 293 name == prefs::kURLsToRestoreOnStartupOld) { |
| 229 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); | 294 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); |
| 230 } | 295 } |
| 231 | 296 |
| 232 if (name == prefs::kContentSettingsPatternPairs) { | 297 if (name == prefs::kContentSettingsPatternPairs) { |
| 233 return scoped_ptr<Value>( | 298 return scoped_ptr<Value>( |
| 234 MergeDictionaryValues(local_value, server_value)).Pass(); | 299 MergeDictionaryValues(local_value, server_value)).Pass(); |
| 235 } | 300 } |
| 236 | 301 |
| 237 // If this is not a specially handled preference, server wins. | 302 // If this is not a specially handled preference, server wins. |
| 238 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); | 303 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 // TODO(zea): consider taking some further action such as erasing the bad | 446 // TODO(zea): consider taking some further action such as erasing the bad |
| 382 // data. | 447 // data. |
| 383 if (!value.get()) | 448 if (!value.get()) |
| 384 continue; | 449 continue; |
| 385 | 450 |
| 386 // It is possible that we may receive a change to a preference we do not | 451 // It is possible that we may receive a change to a preference we do not |
| 387 // want to sync. For example if the user is syncing a Mac client and a | 452 // want to sync. For example if the user is syncing a Mac client and a |
| 388 // Windows client, the Windows client does not support | 453 // Windows client, the Windows client does not support |
| 389 // kConfirmToQuitEnabled. Ignore updates from these preferences. | 454 // kConfirmToQuitEnabled. Ignore updates from these preferences. |
| 390 const char* pref_name = name.c_str(); | 455 const char* pref_name = name.c_str(); |
| 456 std::string new_name; |
| 457 // We migrated this preference name, so do as if the name had not changed. |
| 458 if (IsOldMigratedPreference(pref_name)) { |
| 459 new_name = GetNewMigratedPreferenceName(pref_name); |
| 460 pref_name = new_name.c_str(); |
| 461 } |
| 462 |
| 391 if (!IsPrefRegistered(pref_name)) | 463 if (!IsPrefRegistered(pref_name)) |
| 392 continue; | 464 continue; |
| 393 | 465 |
| 394 const PrefService::Preference* pref = | 466 const PrefService::Preference* pref = |
| 395 pref_service_->FindPreference(pref_name); | 467 pref_service_->FindPreference(pref_name); |
| 396 DCHECK(pref); | 468 DCHECK(pref); |
| 397 | 469 |
| 398 // This will only modify the user controlled value store, which takes | 470 // This will only modify the user controlled value store, which takes |
| 399 // priority over the default value but is ignored if the preference is | 471 // priority over the default value but is ignored if the preference is |
| 400 // policy controlled. | 472 // policy controlled. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 // We are already syncing this preference, just update it's sync node. | 573 // We are already syncing this preference, just update it's sync node. |
| 502 syncer::SyncData sync_data; | 574 syncer::SyncData sync_data; |
| 503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 575 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 504 LOG(ERROR) << "Failed to update preference."; | 576 LOG(ERROR) << "Failed to update preference."; |
| 505 return; | 577 return; |
| 506 } | 578 } |
| 507 changes.push_back( | 579 changes.push_back( |
| 508 syncer::SyncChange(FROM_HERE, | 580 syncer::SyncChange(FROM_HERE, |
| 509 syncer::SyncChange::ACTION_UPDATE, | 581 syncer::SyncChange::ACTION_UPDATE, |
| 510 sync_data)); | 582 sync_data)); |
| 583 // This preference has been migrated from an old version that must be kept |
| 584 // in sync on older versions of Chrome. |
| 585 if (IsMigratedPreference(name.c_str())) { |
| 586 if (!CreatePrefSyncData(GetOldMigratedPreferenceName(name.c_str()), |
| 587 *preference->GetValue(), |
| 588 &sync_data)) { |
| 589 LOG(ERROR) << "Failed to update preference."; |
| 590 return; |
| 591 } |
| 592 |
| 593 changes.push_back( |
| 594 syncer::SyncChange(FROM_HERE, |
| 595 syncer::SyncChange::ACTION_UPDATE, |
| 596 sync_data)); |
| 597 } |
| 511 } | 598 } |
| 512 | 599 |
| 513 syncer::SyncError error = | 600 syncer::SyncError error = |
| 514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 601 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 515 } | 602 } |
| 516 | 603 |
| 517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 604 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { |
| 518 DCHECK(pref_service_ == NULL); | 605 DCHECK(pref_service_ == NULL); |
| 519 pref_service_ = pref_service; | 606 pref_service_ = pref_service; |
| 520 } | 607 } |
| 521 | 608 |
| 522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, | 609 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, |
| 523 bool from_sync) const { | 610 bool from_sync) const { |
| 524 SyncedPrefObserverMap::const_iterator observer_iter = | 611 SyncedPrefObserverMap::const_iterator observer_iter = |
| 525 synced_pref_observers_.find(path); | 612 synced_pref_observers_.find(path); |
| 526 if (observer_iter == synced_pref_observers_.end()) | 613 if (observer_iter == synced_pref_observers_.end()) |
| 527 return; | 614 return; |
| 528 SyncedPrefObserverList* observers = observer_iter->second; | 615 SyncedPrefObserverList* observers = observer_iter->second; |
| 529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, | 616 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, |
| 530 OnSyncedPrefChanged(path, from_sync)); | 617 OnSyncedPrefChanged(path, from_sync)); |
| 531 } | 618 } |
| OLD | NEW |