Chromium Code Reviews| 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() || |
| 82 | 118 (IsMigratedPreference(pref_name.c_str()) && |
| 119 preference.name() == | |
| 120 GetOldMigratedPreferenceName(pref_name.c_str()))); | |
| 83 base::JSONReader reader; | 121 base::JSONReader reader; |
| 84 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); | 122 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); |
| 85 if (!sync_value.get()) { | 123 if (!sync_value.get()) { |
| 86 LOG(ERROR) << "Failed to deserialize preference value: " | 124 LOG(ERROR) << "Failed to deserialize preference value: " |
| 87 << reader.GetErrorMessage(); | 125 << reader.GetErrorMessage(); |
| 88 return; | 126 return; |
| 89 } | 127 } |
| 90 | 128 |
| 91 if (user_pref_value) { | 129 if (user_pref_value) { |
| 92 // We have both server and local values. Merge them. | 130 // We have both server and local values. Merge them. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 112 if (!sync_value->Equals(new_value.get())) { | 150 if (!sync_value->Equals(new_value.get())) { |
| 113 syncer::SyncData sync_data; | 151 syncer::SyncData sync_data; |
| 114 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { | 152 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { |
| 115 LOG(ERROR) << "Failed to update preference."; | 153 LOG(ERROR) << "Failed to update preference."; |
| 116 return; | 154 return; |
| 117 } | 155 } |
| 118 sync_changes->push_back( | 156 sync_changes->push_back( |
| 119 syncer::SyncChange(FROM_HERE, | 157 syncer::SyncChange(FROM_HERE, |
| 120 syncer::SyncChange::ACTION_UPDATE, | 158 syncer::SyncChange::ACTION_UPDATE, |
| 121 sync_data)); | 159 sync_data)); |
| 160 // This preference has been migrated from an old version that must be | |
| 161 // kept in sync on older versions of Chrome. | |
| 162 if (IsMigratedPreference(pref_name.c_str())) { | |
| 163 if (!CreatePrefSyncData( | |
| 164 GetOldMigratedPreferenceName(pref_name.c_str()), | |
| 165 *new_value, | |
| 166 &sync_data)) { | |
| 167 LOG(ERROR) << "Failed to update preference."; | |
| 168 return; | |
| 169 } | |
| 170 sync_changes->push_back( | |
| 171 syncer::SyncChange(FROM_HERE, | |
| 172 syncer::SyncChange::ACTION_UPDATE, | |
|
Nicolas Zea
2013/10/09 17:40:52
It occurs to me this isn't always correct (and thi
robertshield
2013/10/10 00:40:18
Done.
| |
| 173 sync_data)); | |
| 174 } | |
| 122 } | 175 } |
| 123 } else if (!sync_value->IsType(Value::TYPE_NULL)) { | 176 } else if (!sync_value->IsType(Value::TYPE_NULL)) { |
| 124 // Only a server value exists. Just set the local user value. | 177 // Only a server value exists. Just set the local user value. |
| 125 pref_service_->Set(pref_name.c_str(), *sync_value); | 178 pref_service_->Set(pref_name.c_str(), *sync_value); |
| 126 } else { | 179 } else { |
| 127 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 180 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
| 128 } | 181 } |
| 129 } else if (user_pref_value) { | 182 } else if (user_pref_value) { |
| 130 // The server does not know about this preference and should be added | 183 // The server does not know about this preference and should be added |
| 131 // to the syncer's database. | 184 // to the syncer's database. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 | 225 |
| 173 // Go through and check for all preferences we care about that sync already | 226 // Go through and check for all preferences we care about that sync already |
| 174 // knows about. | 227 // knows about. |
| 175 for (syncer::SyncDataList::const_iterator sync_iter = | 228 for (syncer::SyncDataList::const_iterator sync_iter = |
| 176 initial_sync_data.begin(); | 229 initial_sync_data.begin(); |
| 177 sync_iter != initial_sync_data.end(); | 230 sync_iter != initial_sync_data.end(); |
| 178 ++sync_iter) { | 231 ++sync_iter) { |
| 179 DCHECK_EQ(type_, sync_iter->GetDataType()); | 232 DCHECK_EQ(type_, sync_iter->GetDataType()); |
| 180 | 233 |
| 181 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); | 234 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); |
| 182 const std::string& sync_pref_name = preference.name(); | 235 std::string sync_pref_name = preference.name(); |
| 183 | 236 |
| 184 if (remaining_preferences.count(sync_pref_name) == 0) { | 237 if (remaining_preferences.count(sync_pref_name) == 0) { |
| 185 // We're not syncing this preference locally, ignore the sync data. | 238 if (IsOldMigratedPreference(sync_pref_name.c_str())) { |
| 186 // TODO(zea): Eventually we want to be able to have the syncable service | 239 // This old pref name is not syncable locally anymore but we accept |
| 187 // reconstruct all sync data for it's datatype (therefore having | 240 // changes from other Chrome installs of previous versions and migrate |
| 188 // GetAllSyncData be a complete representation). We should store this data | 241 // them to the new name. Note that we will be merging any differences |
| 189 // somewhere, even if we don't use it. | 242 // between the new and old values and sync'ing them back. |
| 190 continue; | 243 sync_pref_name = GetNewMigratedPreferenceName(sync_pref_name.c_str()); |
| 244 } else { | |
| 245 // We're not syncing this preference locally, ignore the sync data. | |
| 246 // TODO(zea): Eventually we want to be able to have the syncable service | |
| 247 // reconstruct all sync data for its datatype (therefore having | |
| 248 // GetAllSyncData be a complete representation). We should store this | |
| 249 // data somewhere, even if we don't use it. | |
| 250 continue; | |
| 251 } | |
| 252 } else { | |
| 253 remaining_preferences.erase(sync_pref_name); | |
| 191 } | 254 } |
| 192 | |
| 193 remaining_preferences.erase(sync_pref_name); | |
| 194 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); | 255 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); |
| 195 } | 256 } |
| 196 | 257 |
| 197 // Go through and build sync data for any remaining preferences. | 258 // Go through and build sync data for any remaining preferences. |
| 198 for (std::set<std::string>::iterator pref_name_iter = | 259 for (std::set<std::string>::iterator pref_name_iter = |
| 199 remaining_preferences.begin(); | 260 remaining_preferences.begin(); |
| 200 pref_name_iter != remaining_preferences.end(); | 261 pref_name_iter != remaining_preferences.end(); |
| 201 ++pref_name_iter) { | 262 ++pref_name_iter) { |
| 202 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); | 263 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); |
| 203 } | 264 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 218 models_associated_ = false; | 279 models_associated_ = false; |
| 219 sync_processor_.reset(); | 280 sync_processor_.reset(); |
| 220 sync_error_factory_.reset(); | 281 sync_error_factory_.reset(); |
| 221 pref_service_->OnIsSyncingChanged(); | 282 pref_service_->OnIsSyncingChanged(); |
| 222 } | 283 } |
| 223 | 284 |
| 224 scoped_ptr<Value> PrefModelAssociator::MergePreference( | 285 scoped_ptr<Value> PrefModelAssociator::MergePreference( |
| 225 const std::string& name, | 286 const std::string& name, |
| 226 const Value& local_value, | 287 const Value& local_value, |
| 227 const Value& server_value) { | 288 const Value& server_value) { |
| 228 if (name == prefs::kURLsToRestoreOnStartup) { | 289 // This function special cases preferences individually, so don't attempt |
| 290 // to merge for all migrated values. | |
| 291 if (name == prefs::kURLsToRestoreOnStartup || | |
| 292 name == prefs::kURLsToRestoreOnStartupOld) { | |
| 229 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); | 293 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); |
| 230 } | 294 } |
| 231 | 295 |
| 232 if (name == prefs::kContentSettingsPatternPairs) { | 296 if (name == prefs::kContentSettingsPatternPairs) { |
| 233 return scoped_ptr<Value>( | 297 return scoped_ptr<Value>( |
| 234 MergeDictionaryValues(local_value, server_value)).Pass(); | 298 MergeDictionaryValues(local_value, server_value)).Pass(); |
| 235 } | 299 } |
| 236 | 300 |
| 237 // If this is not a specially handled preference, server wins. | 301 // If this is not a specially handled preference, server wins. |
| 238 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); | 302 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 | 445 // TODO(zea): consider taking some further action such as erasing the bad |
| 382 // data. | 446 // data. |
| 383 if (!value.get()) | 447 if (!value.get()) |
| 384 continue; | 448 continue; |
| 385 | 449 |
| 386 // It is possible that we may receive a change to a preference we do not | 450 // 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 | 451 // want to sync. For example if the user is syncing a Mac client and a |
| 388 // Windows client, the Windows client does not support | 452 // Windows client, the Windows client does not support |
| 389 // kConfirmToQuitEnabled. Ignore updates from these preferences. | 453 // kConfirmToQuitEnabled. Ignore updates from these preferences. |
| 390 const char* pref_name = name.c_str(); | 454 const char* pref_name = name.c_str(); |
| 455 std::string new_name; | |
| 456 // We migrated this preference name, so do as if the name had not changed. | |
| 457 if (IsOldMigratedPreference(pref_name)) { | |
| 458 new_name = GetNewMigratedPreferenceName(pref_name); | |
| 459 pref_name = new_name.c_str(); | |
| 460 } | |
| 461 | |
| 391 if (!IsPrefRegistered(pref_name)) | 462 if (!IsPrefRegistered(pref_name)) |
| 392 continue; | 463 continue; |
| 393 | 464 |
| 394 const PrefService::Preference* pref = | 465 const PrefService::Preference* pref = |
| 395 pref_service_->FindPreference(pref_name); | 466 pref_service_->FindPreference(pref_name); |
| 396 DCHECK(pref); | 467 DCHECK(pref); |
| 397 | 468 |
| 398 // This will only modify the user controlled value store, which takes | 469 // This will only modify the user controlled value store, which takes |
| 399 // priority over the default value but is ignored if the preference is | 470 // priority over the default value but is ignored if the preference is |
| 400 // policy controlled. | 471 // 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. | 572 // We are already syncing this preference, just update it's sync node. |
| 502 syncer::SyncData sync_data; | 573 syncer::SyncData sync_data; |
| 503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 574 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 504 LOG(ERROR) << "Failed to update preference."; | 575 LOG(ERROR) << "Failed to update preference."; |
| 505 return; | 576 return; |
| 506 } | 577 } |
| 507 changes.push_back( | 578 changes.push_back( |
| 508 syncer::SyncChange(FROM_HERE, | 579 syncer::SyncChange(FROM_HERE, |
| 509 syncer::SyncChange::ACTION_UPDATE, | 580 syncer::SyncChange::ACTION_UPDATE, |
| 510 sync_data)); | 581 sync_data)); |
| 582 // This preference has been migrated from an old version that must be kept | |
| 583 // in sync on older versions of Chrome. | |
| 584 if (IsMigratedPreference(name.c_str())) { | |
| 585 if (!CreatePrefSyncData(GetOldMigratedPreferenceName(name.c_str()), | |
| 586 *preference->GetValue(), | |
| 587 &sync_data)) { | |
| 588 LOG(ERROR) << "Failed to update preference."; | |
| 589 return; | |
| 590 } | |
| 591 | |
| 592 changes.push_back( | |
| 593 syncer::SyncChange(FROM_HERE, | |
| 594 syncer::SyncChange::ACTION_UPDATE, | |
|
Nicolas Zea
2013/10/09 17:40:52
here too. We can check synced_preferences_.count(o
robertshield
2013/10/10 00:40:18
Done.
| |
| 595 sync_data)); | |
| 596 } | |
| 511 } | 597 } |
| 512 | 598 |
| 513 syncer::SyncError error = | 599 syncer::SyncError error = |
| 514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 600 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 515 } | 601 } |
| 516 | 602 |
| 517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 603 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { |
| 518 DCHECK(pref_service_ == NULL); | 604 DCHECK(pref_service_ == NULL); |
| 519 pref_service_ = pref_service; | 605 pref_service_ = pref_service; |
| 520 } | 606 } |
| 521 | 607 |
| 522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, | 608 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, |
| 523 bool from_sync) const { | 609 bool from_sync) const { |
| 524 SyncedPrefObserverMap::const_iterator observer_iter = | 610 SyncedPrefObserverMap::const_iterator observer_iter = |
| 525 synced_pref_observers_.find(path); | 611 synced_pref_observers_.find(path); |
| 526 if (observer_iter == synced_pref_observers_.end()) | 612 if (observer_iter == synced_pref_observers_.end()) |
| 527 return; | 613 return; |
| 528 SyncedPrefObserverList* observers = observer_iter->second; | 614 SyncedPrefObserverList* observers = observer_iter->second; |
| 529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, | 615 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, |
| 530 OnSyncedPrefChanged(path, from_sync)); | 616 OnSyncedPrefChanged(path, from_sync)); |
| 531 } | 617 } |
| OLD | NEW |