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 adding | |
| 52 // the old and new preference names here will do the right thing by the sync | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
what is "the right thing"? maybe spell out that it
robertshield
2013/10/08 01:52:13
Done.
| |
| 53 // engine. Preference migration itself doesn't happen here. It may happen in | |
| 54 // session_startup_pref.cc. | |
| 55 struct MigratedPreferences { | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
const struct
mega nit: this type doesn't need a na
robertshield
2013/10/08 01:52:13
Done.
| |
| 56 const char* old_name; | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
const char* const
robertshield
2013/10/08 01:52:13
Done.
| |
| 57 const char* new_name; | |
| 58 } kMigratedPreferences[] = { | |
| 59 { prefs::kURLsToRestoreOnStartupOld, prefs::kURLsToRestoreOnStartup }, | |
| 60 }; | |
| 61 | |
| 62 std::string GetOldMigratedPreferenceName(const char* preference_name) { | |
| 63 for (int i = 0; i < arraysize(kMigratedPreferences); ++i) { | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
int -> size_t since you're comparing with arraysiz
robertshield
2013/10/08 01:52:13
Done.
| |
| 64 if (!strcmp(kMigratedPreferences[i].new_name, preference_name)) | |
| 65 return kMigratedPreferences[i].old_name; | |
| 66 } | |
| 67 return ""; | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
"" -> std::string() here and line 75
robertshield
2013/10/08 01:52:13
Done.
| |
| 68 } | |
| 69 | |
| 70 std::string GetNewMigratedPreferenceName(const char* old_preference_name) { | |
| 71 for (int i = 0; i < arraysize(kMigratedPreferences); ++i) { | |
| 72 if (!strcmp(kMigratedPreferences[i].old_name, old_preference_name)) | |
| 73 return kMigratedPreferences[i].new_name; | |
| 74 } | |
| 75 return ""; | |
| 76 } | |
| 77 | |
| 78 bool IsMigratedPreference(const char* preference_name) { | |
| 79 return !GetOldMigratedPreferenceName(preference_name).empty(); | |
| 80 } | |
| 81 | |
| 82 bool IsOldMigratedPreference(const char* old_preference_name) { | |
| 83 return !GetNewMigratedPreferenceName(old_preference_name).empty(); | |
| 84 } | |
| 85 | |
| 51 } // namespace | 86 } // namespace |
| 52 | 87 |
| 53 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type) | 88 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type) |
| 54 : models_associated_(false), | 89 : models_associated_(false), |
| 55 processing_syncer_changes_(false), | 90 processing_syncer_changes_(false), |
| 56 pref_service_(NULL), | 91 pref_service_(NULL), |
| 57 type_(type) { | 92 type_(type) { |
| 58 DCHECK(CalledOnValidThread()); | 93 DCHECK(CalledOnValidThread()); |
| 59 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); | 94 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); |
| 60 } | 95 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 71 void PrefModelAssociator::InitPrefAndAssociate( | 106 void PrefModelAssociator::InitPrefAndAssociate( |
| 72 const syncer::SyncData& sync_pref, | 107 const syncer::SyncData& sync_pref, |
| 73 const std::string& pref_name, | 108 const std::string& pref_name, |
| 74 syncer::SyncChangeList* sync_changes) { | 109 syncer::SyncChangeList* sync_changes) { |
| 75 const Value* user_pref_value = pref_service_->GetUserPrefValue( | 110 const Value* user_pref_value = pref_service_->GetUserPrefValue( |
| 76 pref_name.c_str()); | 111 pref_name.c_str()); |
| 77 VLOG(1) << "Associating preference " << pref_name; | 112 VLOG(1) << "Associating preference " << pref_name; |
| 78 | 113 |
| 79 if (sync_pref.IsValid()) { | 114 if (sync_pref.IsValid()) { |
| 80 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); | 115 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); |
| 81 DCHECK_EQ(pref_name, preference.name()); | 116 DCHECK(pref_name == preference.name() || |
| 117 (IsMigratedPreference(pref_name.c_str()) && | |
| 118 preference.name() == | |
| 119 GetOldMigratedPreferenceName(pref_name.c_str()))); | |
| 82 | 120 |
| 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) { |
| (...skipping 20 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, | |
| 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(); |
| 391 if (!IsPrefRegistered(pref_name)) | 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 } else if (!IsPrefRegistered(pref_name)) { | |
|
grt (UTC plus 2)
2013/10/07 20:15:16
in the block above, it's safe to assume that new_n
robertshield
2013/10/08 01:52:13
That's interesting. MAD's patch was doing so, but
| |
| 392 continue; | 461 continue; |
| 462 } | |
| 393 | 463 |
| 394 const PrefService::Preference* pref = | 464 const PrefService::Preference* pref = |
| 395 pref_service_->FindPreference(pref_name); | 465 pref_service_->FindPreference(pref_name); |
| 396 DCHECK(pref); | 466 DCHECK(pref); |
| 397 | 467 |
| 398 // This will only modify the user controlled value store, which takes | 468 // This will only modify the user controlled value store, which takes |
| 399 // priority over the default value but is ignored if the preference is | 469 // priority over the default value but is ignored if the preference is |
| 400 // policy controlled. | 470 // policy controlled. |
| 401 pref_service_->Set(pref_name, *value); | 471 pref_service_->Set(pref_name, *value); |
| 402 | 472 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 // We are already syncing this preference, just update it's sync node. | 571 // We are already syncing this preference, just update it's sync node. |
| 502 syncer::SyncData sync_data; | 572 syncer::SyncData sync_data; |
| 503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 573 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
| 504 LOG(ERROR) << "Failed to update preference."; | 574 LOG(ERROR) << "Failed to update preference."; |
| 505 return; | 575 return; |
| 506 } | 576 } |
| 507 changes.push_back( | 577 changes.push_back( |
| 508 syncer::SyncChange(FROM_HERE, | 578 syncer::SyncChange(FROM_HERE, |
| 509 syncer::SyncChange::ACTION_UPDATE, | 579 syncer::SyncChange::ACTION_UPDATE, |
| 510 sync_data)); | 580 sync_data)); |
| 581 // This preference has been migrated from an old version that must be kept | |
| 582 // in sync on older versions of Chrome. | |
| 583 if (IsMigratedPreference(name.c_str())) { | |
| 584 if (!CreatePrefSyncData(GetOldMigratedPreferenceName(name.c_str()), | |
| 585 *preference->GetValue(), | |
| 586 &sync_data)) { | |
| 587 LOG(ERROR) << "Failed to update preference."; | |
| 588 return; | |
| 589 } | |
| 590 | |
| 591 changes.push_back( | |
| 592 syncer::SyncChange(FROM_HERE, | |
| 593 syncer::SyncChange::ACTION_UPDATE, | |
| 594 sync_data)); | |
| 595 } | |
| 511 } | 596 } |
| 512 | 597 |
| 513 syncer::SyncError error = | 598 syncer::SyncError error = |
| 514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 599 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 515 } | 600 } |
| 516 | 601 |
| 517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 602 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { |
| 518 DCHECK(pref_service_ == NULL); | 603 DCHECK(pref_service_ == NULL); |
| 519 pref_service_ = pref_service; | 604 pref_service_ = pref_service; |
| 520 } | 605 } |
| 521 | 606 |
| 522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, | 607 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, |
| 523 bool from_sync) const { | 608 bool from_sync) const { |
| 524 SyncedPrefObserverMap::const_iterator observer_iter = | 609 SyncedPrefObserverMap::const_iterator observer_iter = |
| 525 synced_pref_observers_.find(path); | 610 synced_pref_observers_.find(path); |
| 526 if (observer_iter == synced_pref_observers_.end()) | 611 if (observer_iter == synced_pref_observers_.end()) |
| 527 return; | 612 return; |
| 528 SyncedPrefObserverList* observers = observer_iter->second; | 613 SyncedPrefObserverList* observers = observer_iter->second; |
| 529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, | 614 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, |
| 530 OnSyncedPrefChanged(path, from_sync)); | 615 OnSyncedPrefChanged(path, from_sync)); |
| 531 } | 616 } |
| OLD | NEW |