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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 void PrefModelAssociator::InitPrefAndAssociate( | 71 void PrefModelAssociator::InitPrefAndAssociate( |
72 const syncer::SyncData& sync_pref, | 72 const syncer::SyncData& sync_pref, |
73 const std::string& pref_name, | 73 const std::string& pref_name, |
74 syncer::SyncChangeList* sync_changes) { | 74 syncer::SyncChangeList* sync_changes) { |
75 const Value* user_pref_value = pref_service_->GetUserPrefValue( | 75 const Value* user_pref_value = pref_service_->GetUserPrefValue( |
76 pref_name.c_str()); | 76 pref_name.c_str()); |
77 VLOG(1) << "Associating preference " << pref_name; | 77 VLOG(1) << "Associating preference " << pref_name; |
78 | 78 |
79 if (sync_pref.IsValid()) { | 79 if (sync_pref.IsValid()) { |
80 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); | 80 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); |
81 DCHECK_EQ(pref_name, preference.name()); | 81 DCHECK(pref_name == preference.name() || |
82 (pref_name == prefs::kURLsToRestoreOnStartup && | |
83 preference.name() == prefs::kURLsToRestoreOnStartupOld)); | |
82 | 84 |
83 base::JSONReader reader; | 85 base::JSONReader reader; |
84 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); | 86 scoped_ptr<Value> sync_value(reader.ReadToValue(preference.value())); |
85 if (!sync_value.get()) { | 87 if (!sync_value.get()) { |
86 LOG(ERROR) << "Failed to deserialize preference value: " | 88 LOG(ERROR) << "Failed to deserialize preference value: " |
87 << reader.GetErrorMessage(); | 89 << reader.GetErrorMessage(); |
88 return; | 90 return; |
89 } | 91 } |
90 | 92 |
91 if (user_pref_value) { | 93 if (user_pref_value) { |
(...skipping 20 matching lines...) Expand all Loading... | |
112 if (!sync_value->Equals(new_value.get())) { | 114 if (!sync_value->Equals(new_value.get())) { |
113 syncer::SyncData sync_data; | 115 syncer::SyncData sync_data; |
114 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { | 116 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { |
115 LOG(ERROR) << "Failed to update preference."; | 117 LOG(ERROR) << "Failed to update preference."; |
116 return; | 118 return; |
117 } | 119 } |
118 sync_changes->push_back( | 120 sync_changes->push_back( |
119 syncer::SyncChange(FROM_HERE, | 121 syncer::SyncChange(FROM_HERE, |
120 syncer::SyncChange::ACTION_UPDATE, | 122 syncer::SyncChange::ACTION_UPDATE, |
121 sync_data)); | 123 sync_data)); |
124 // This preference has been migrated from an old version that must be | |
125 // kept in sync on older versions of Chrome. | |
126 if (pref_name == prefs::kURLsToRestoreOnStartup) { | |
127 syncer::SyncData sync_data; | |
Nicolas Zea
2013/10/04 19:37:36
nit: CreatePrefSyncData will overwrite a SyncData
| |
128 if (!CreatePrefSyncData(prefs::kURLsToRestoreOnStartupOld, | |
129 *new_value, | |
130 &sync_data)) { | |
131 LOG(ERROR) << "Failed to update preference."; | |
132 return; | |
133 } | |
134 sync_changes->push_back( | |
135 syncer::SyncChange(FROM_HERE, | |
136 syncer::SyncChange::ACTION_UPDATE, | |
137 sync_data)); | |
138 } | |
122 } | 139 } |
123 } else if (!sync_value->IsType(Value::TYPE_NULL)) { | 140 } else if (!sync_value->IsType(Value::TYPE_NULL)) { |
124 // Only a server value exists. Just set the local user value. | 141 // Only a server value exists. Just set the local user value. |
125 pref_service_->Set(pref_name.c_str(), *sync_value); | 142 pref_service_->Set(pref_name.c_str(), *sync_value); |
126 } else { | 143 } else { |
127 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 144 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
128 } | 145 } |
129 } else if (user_pref_value) { | 146 } else if (user_pref_value) { |
130 // The server does not know about this preference and should be added | 147 // The server does not know about this preference and should be added |
131 // to the syncer's database. | 148 // to the syncer's database. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 | 189 |
173 // Go through and check for all preferences we care about that sync already | 190 // Go through and check for all preferences we care about that sync already |
174 // knows about. | 191 // knows about. |
175 for (syncer::SyncDataList::const_iterator sync_iter = | 192 for (syncer::SyncDataList::const_iterator sync_iter = |
176 initial_sync_data.begin(); | 193 initial_sync_data.begin(); |
177 sync_iter != initial_sync_data.end(); | 194 sync_iter != initial_sync_data.end(); |
178 ++sync_iter) { | 195 ++sync_iter) { |
179 DCHECK_EQ(type_, sync_iter->GetDataType()); | 196 DCHECK_EQ(type_, sync_iter->GetDataType()); |
180 | 197 |
181 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); | 198 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); |
182 const std::string& sync_pref_name = preference.name(); | 199 std::string sync_pref_name = preference.name(); |
183 | 200 |
184 if (remaining_preferences.count(sync_pref_name) == 0) { | 201 if (remaining_preferences.count(sync_pref_name) == 0) { |
185 // We're not syncing this preference locally, ignore the sync data. | 202 if (sync_pref_name == prefs::kURLsToRestoreOnStartupOld) { |
186 // TODO(zea): Eventually we want to be able to have the syncable service | 203 // This old pref name is not syncable locally anymore but we accept |
187 // reconstruct all sync data for it's datatype (therefore having | 204 // changes from other Chrome installs of previous versions and migrate |
188 // GetAllSyncData be a complete representation). We should store this data | 205 // them to the new name. Note that we will be merging any differences |
189 // somewhere, even if we don't use it. | 206 // between the new and old values and sync'ing them back. |
190 continue; | 207 sync_pref_name = prefs::kURLsToRestoreOnStartup; |
208 } else { | |
209 // We're not syncing this preference locally, ignore the sync data. | |
210 // TODO(zea): Eventually we want to be able to have the syncable service | |
211 // reconstruct all sync data for its datatype (therefore having | |
212 // GetAllSyncData be a complete representation). We should store this | |
213 // data somewhere, even if we don't use it. | |
214 continue; | |
215 } | |
216 } else { | |
217 remaining_preferences.erase(sync_pref_name); | |
191 } | 218 } |
192 | |
193 remaining_preferences.erase(sync_pref_name); | |
194 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); | 219 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); |
195 } | 220 } |
196 | 221 |
197 // Go through and build sync data for any remaining preferences. | 222 // Go through and build sync data for any remaining preferences. |
198 for (std::set<std::string>::iterator pref_name_iter = | 223 for (std::set<std::string>::iterator pref_name_iter = |
199 remaining_preferences.begin(); | 224 remaining_preferences.begin(); |
200 pref_name_iter != remaining_preferences.end(); | 225 pref_name_iter != remaining_preferences.end(); |
201 ++pref_name_iter) { | 226 ++pref_name_iter) { |
202 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); | 227 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); |
203 } | 228 } |
(...skipping 14 matching lines...) Expand all Loading... | |
218 models_associated_ = false; | 243 models_associated_ = false; |
219 sync_processor_.reset(); | 244 sync_processor_.reset(); |
220 sync_error_factory_.reset(); | 245 sync_error_factory_.reset(); |
221 pref_service_->OnIsSyncingChanged(); | 246 pref_service_->OnIsSyncingChanged(); |
222 } | 247 } |
223 | 248 |
224 scoped_ptr<Value> PrefModelAssociator::MergePreference( | 249 scoped_ptr<Value> PrefModelAssociator::MergePreference( |
225 const std::string& name, | 250 const std::string& name, |
226 const Value& local_value, | 251 const Value& local_value, |
227 const Value& server_value) { | 252 const Value& server_value) { |
228 if (name == prefs::kURLsToRestoreOnStartup) { | 253 if (name == prefs::kURLsToRestoreOnStartup || |
254 name == prefs::kURLsToRestoreOnStartupOld) { | |
229 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); | 255 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); |
230 } | 256 } |
231 | 257 |
232 if (name == prefs::kContentSettingsPatternPairs) { | 258 if (name == prefs::kContentSettingsPatternPairs) { |
233 return scoped_ptr<Value>( | 259 return scoped_ptr<Value>( |
234 MergeDictionaryValues(local_value, server_value)).Pass(); | 260 MergeDictionaryValues(local_value, server_value)).Pass(); |
235 } | 261 } |
236 | 262 |
237 // If this is not a specially handled preference, server wins. | 263 // If this is not a specially handled preference, server wins. |
238 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); | 264 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 | 407 // TODO(zea): consider taking some further action such as erasing the bad |
382 // data. | 408 // data. |
383 if (!value.get()) | 409 if (!value.get()) |
384 continue; | 410 continue; |
385 | 411 |
386 // It is possible that we may receive a change to a preference we do not | 412 // 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 | 413 // want to sync. For example if the user is syncing a Mac client and a |
388 // Windows client, the Windows client does not support | 414 // Windows client, the Windows client does not support |
389 // kConfirmToQuitEnabled. Ignore updates from these preferences. | 415 // kConfirmToQuitEnabled. Ignore updates from these preferences. |
390 const char* pref_name = name.c_str(); | 416 const char* pref_name = name.c_str(); |
391 if (!IsPrefRegistered(pref_name)) | 417 // We migrated this preference name, so do as if the name had not changed. |
418 if (name == prefs::kURLsToRestoreOnStartupOld) | |
419 pref_name = prefs::kURLsToRestoreOnStartup; | |
420 else if (!IsPrefRegistered(pref_name)) | |
392 continue; | 421 continue; |
393 | 422 |
394 const PrefService::Preference* pref = | 423 const PrefService::Preference* pref = |
395 pref_service_->FindPreference(pref_name); | 424 pref_service_->FindPreference(pref_name); |
396 DCHECK(pref); | 425 DCHECK(pref); |
397 | 426 |
398 // This will only modify the user controlled value store, which takes | 427 // This will only modify the user controlled value store, which takes |
399 // priority over the default value but is ignored if the preference is | 428 // priority over the default value but is ignored if the preference is |
400 // policy controlled. | 429 // policy controlled. |
401 pref_service_->Set(pref_name, *value); | 430 pref_service_->Set(pref_name, *value); |
402 | 431 |
403 NotifySyncedPrefObservers(name, true /*from_sync*/); | 432 NotifySyncedPrefObservers(name, true /*from_sync*/); |
robertshield
2013/10/07 16:42:57
@Nicolas: it looks like this uses the old "name" r
Nicolas Zea
2013/10/07 17:08:07
I missed this, but as it happens it's okay to leav
| |
404 | 433 |
405 // Keep track of any newly synced preferences. | 434 // Keep track of any newly synced preferences. |
406 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { | 435 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { |
407 synced_preferences_.insert(name); | 436 synced_preferences_.insert(name); |
408 } | 437 } |
409 } | 438 } |
410 return syncer::SyncError(); | 439 return syncer::SyncError(); |
411 } | 440 } |
412 | 441 |
413 Value* PrefModelAssociator::ReadPreferenceSpecifics( | 442 Value* PrefModelAssociator::ReadPreferenceSpecifics( |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
501 // We are already syncing this preference, just update it's sync node. | 530 // We are already syncing this preference, just update it's sync node. |
502 syncer::SyncData sync_data; | 531 syncer::SyncData sync_data; |
503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { | 532 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { |
504 LOG(ERROR) << "Failed to update preference."; | 533 LOG(ERROR) << "Failed to update preference."; |
505 return; | 534 return; |
506 } | 535 } |
507 changes.push_back( | 536 changes.push_back( |
508 syncer::SyncChange(FROM_HERE, | 537 syncer::SyncChange(FROM_HERE, |
509 syncer::SyncChange::ACTION_UPDATE, | 538 syncer::SyncChange::ACTION_UPDATE, |
510 sync_data)); | 539 sync_data)); |
540 // This preference has been migrated from an old version that must be kept | |
541 // in sync on older versions of Chrome. | |
542 if (name == prefs::kURLsToRestoreOnStartup) { | |
543 syncer::SyncData sync_data; | |
544 if (!CreatePrefSyncData(prefs::kURLsToRestoreOnStartupOld, | |
545 *preference->GetValue(), | |
546 &sync_data)) { | |
547 LOG(ERROR) << "Failed to update preference."; | |
548 return; | |
549 } | |
550 | |
551 changes.push_back( | |
552 syncer::SyncChange(FROM_HERE, | |
553 syncer::SyncChange::ACTION_UPDATE, | |
554 sync_data)); | |
555 } | |
511 } | 556 } |
512 | 557 |
513 syncer::SyncError error = | 558 syncer::SyncError error = |
514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 559 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
515 } | 560 } |
516 | 561 |
517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 562 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { |
518 DCHECK(pref_service_ == NULL); | 563 DCHECK(pref_service_ == NULL); |
519 pref_service_ = pref_service; | 564 pref_service_ = pref_service; |
520 } | 565 } |
521 | 566 |
522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, | 567 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, |
523 bool from_sync) const { | 568 bool from_sync) const { |
524 SyncedPrefObserverMap::const_iterator observer_iter = | 569 SyncedPrefObserverMap::const_iterator observer_iter = |
525 synced_pref_observers_.find(path); | 570 synced_pref_observers_.find(path); |
526 if (observer_iter == synced_pref_observers_.end()) | 571 if (observer_iter == synced_pref_observers_.end()) |
527 return; | 572 return; |
528 SyncedPrefObserverList* observers = observer_iter->second; | 573 SyncedPrefObserverList* observers = observer_iter->second; |
529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, | 574 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, |
530 OnSyncedPrefChanged(path, from_sync)); | 575 OnSyncedPrefChanged(path, from_sync)); |
531 } | 576 } |
OLD | NEW |