Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: chrome/browser/prefs/pref_model_associator.cc

Issue 24930003: Migrate startup URLs pref. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with sync support. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 12 matching lines...) Expand all
104 << " is of type " << new_value->GetType() 106 << " is of type " << new_value->GetType()
105 << " which doesn't match pref type " 107 << " which doesn't match pref type "
106 << user_pref_value->GetType(); 108 << user_pref_value->GetType();
107 } else if (!user_pref_value->Equals(new_value.get())) { 109 } else if (!user_pref_value->Equals(new_value.get())) {
108 pref_service_->Set(pref_name.c_str(), *new_value); 110 pref_service_->Set(pref_name.c_str(), *new_value);
109 } 111 }
110 112
111 // If the merge resulted in an updated value, inform the syncer. 113 // If the merge resulted in an updated value, inform the syncer.
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)) {
Nicolas Zea 2013/10/04 17:45:01 not that this is only going to update the new sync
MAD 2013/10/04 19:31:22 Done.
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));
122 } 124 }
123 } else if (!sync_value->IsType(Value::TYPE_NULL)) { 125 } else if (!sync_value->IsType(Value::TYPE_NULL)) {
124 // Only a server value exists. Just set the local user value. 126 // Only a server value exists. Just set the local user value.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 174
173 // Go through and check for all preferences we care about that sync already 175 // Go through and check for all preferences we care about that sync already
174 // knows about. 176 // knows about.
175 for (syncer::SyncDataList::const_iterator sync_iter = 177 for (syncer::SyncDataList::const_iterator sync_iter =
176 initial_sync_data.begin(); 178 initial_sync_data.begin();
177 sync_iter != initial_sync_data.end(); 179 sync_iter != initial_sync_data.end();
178 ++sync_iter) { 180 ++sync_iter) {
179 DCHECK_EQ(type_, sync_iter->GetDataType()); 181 DCHECK_EQ(type_, sync_iter->GetDataType());
180 182
181 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); 183 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
182 const std::string& sync_pref_name = preference.name(); 184 std::string sync_pref_name = preference.name();
183 185
184 if (remaining_preferences.count(sync_pref_name) == 0) { 186 if (remaining_preferences.count(sync_pref_name) == 0) {
185 // We're not syncing this preference locally, ignore the sync data. 187 if (sync_pref_name == prefs::kURLsToRestoreOnStartupOld) {
186 // TODO(zea): Eventually we want to be able to have the syncable service 188 // This old pref name is not syncable locally anymore but we accept
187 // reconstruct all sync data for it's datatype (therefore having 189 // changes from other Chrome installs of previous versions and migrate
188 // GetAllSyncData be a complete representation). We should store this data 190 // them to the new name.
189 // somewhere, even if we don't use it. 191 sync_pref_name = prefs::kURLsToRestoreOnStartup;
Nicolas Zea 2013/10/04 17:45:01 Maybe mention that we'll be merging any difference
MAD 2013/10/04 19:31:22 Done.
190 continue; 192 } else {
193 // We're not syncing this preference locally, ignore the sync data.
194 // TODO(zea): Eventually we want to be able to have the syncable service
195 // reconstruct all sync data for it's datatype (therefore having
grt (UTC plus 2) 2013/10/04 18:24:07 it's -> its
MAD 2013/10/04 19:31:22 Done.
196 // GetAllSyncData be a complete representation). We should store this
197 // data somewhere, even if we don't use it.
198 continue;
199 }
200 } else {
201 remaining_preferences.erase(sync_pref_name);
191 } 202 }
192
193 remaining_preferences.erase(sync_pref_name);
194 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes); 203 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes);
195 } 204 }
196 205
197 // Go through and build sync data for any remaining preferences. 206 // Go through and build sync data for any remaining preferences.
198 for (std::set<std::string>::iterator pref_name_iter = 207 for (std::set<std::string>::iterator pref_name_iter =
199 remaining_preferences.begin(); 208 remaining_preferences.begin();
200 pref_name_iter != remaining_preferences.end(); 209 pref_name_iter != remaining_preferences.end();
201 ++pref_name_iter) { 210 ++pref_name_iter) {
202 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); 211 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes);
203 } 212 }
(...skipping 14 matching lines...) Expand all
218 models_associated_ = false; 227 models_associated_ = false;
219 sync_processor_.reset(); 228 sync_processor_.reset();
220 sync_error_factory_.reset(); 229 sync_error_factory_.reset();
221 pref_service_->OnIsSyncingChanged(); 230 pref_service_->OnIsSyncingChanged();
222 } 231 }
223 232
224 scoped_ptr<Value> PrefModelAssociator::MergePreference( 233 scoped_ptr<Value> PrefModelAssociator::MergePreference(
225 const std::string& name, 234 const std::string& name,
226 const Value& local_value, 235 const Value& local_value,
227 const Value& server_value) { 236 const Value& server_value) {
228 if (name == prefs::kURLsToRestoreOnStartup) { 237 if (name == prefs::kURLsToRestoreOnStartup ||
238 name == prefs::kURLsToRestoreOnStartupOld) {
229 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); 239 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass();
230 } 240 }
231 241
232 if (name == prefs::kContentSettingsPatternPairs) { 242 if (name == prefs::kContentSettingsPatternPairs) {
233 return scoped_ptr<Value>( 243 return scoped_ptr<Value>(
234 MergeDictionaryValues(local_value, server_value)).Pass(); 244 MergeDictionaryValues(local_value, server_value)).Pass();
235 } 245 }
236 246
237 // If this is not a specially handled preference, server wins. 247 // If this is not a specially handled preference, server wins.
238 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); 248 return scoped_ptr<Value>(server_value.DeepCopy()).Pass();
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 // TODO(zea): consider taking some further action such as erasing the bad 391 // TODO(zea): consider taking some further action such as erasing the bad
382 // data. 392 // data.
383 if (!value.get()) 393 if (!value.get())
384 continue; 394 continue;
385 395
386 // It is possible that we may receive a change to a preference we do not 396 // 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 397 // want to sync. For example if the user is syncing a Mac client and a
388 // Windows client, the Windows client does not support 398 // Windows client, the Windows client does not support
389 // kConfirmToQuitEnabled. Ignore updates from these preferences. 399 // kConfirmToQuitEnabled. Ignore updates from these preferences.
390 const char* pref_name = name.c_str(); 400 const char* pref_name = name.c_str();
391 if (!IsPrefRegistered(pref_name)) 401 // We migrated this preference name, so do as if the name had not changed.
402 if (name == prefs::kURLsToRestoreOnStartupOld)
403 pref_name = prefs::kURLsToRestoreOnStartup;
404 else if (!IsPrefRegistered(pref_name))
392 continue; 405 continue;
393 406
394 const PrefService::Preference* pref = 407 const PrefService::Preference* pref =
395 pref_service_->FindPreference(pref_name); 408 pref_service_->FindPreference(pref_name);
396 DCHECK(pref); 409 DCHECK(pref);
397 410
398 // This will only modify the user controlled value store, which takes 411 // This will only modify the user controlled value store, which takes
399 // priority over the default value but is ignored if the preference is 412 // priority over the default value but is ignored if the preference is
400 // policy controlled. 413 // policy controlled.
401 pref_service_->Set(pref_name, *value); 414 pref_service_->Set(pref_name, *value);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 // We are already syncing this preference, just update it's sync node. 514 // We are already syncing this preference, just update it's sync node.
502 syncer::SyncData sync_data; 515 syncer::SyncData sync_data;
503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { 516 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
504 LOG(ERROR) << "Failed to update preference."; 517 LOG(ERROR) << "Failed to update preference.";
505 return; 518 return;
506 } 519 }
507 changes.push_back( 520 changes.push_back(
508 syncer::SyncChange(FROM_HERE, 521 syncer::SyncChange(FROM_HERE,
509 syncer::SyncChange::ACTION_UPDATE, 522 syncer::SyncChange::ACTION_UPDATE,
510 sync_data)); 523 sync_data));
524 // This preference has been migrated from an old version that must be kept
525 // in sync on older versions of Chrome.
526 if (name == prefs::kURLsToRestoreOnStartup) {
527 syncer::SyncData sync_data;
528 if (!CreatePrefSyncData(prefs::kURLsToRestoreOnStartupOld,
529 *preference->GetValue(),
530 &sync_data)) {
531 LOG(ERROR) << "Failed to update preference.";
532 return;
533 }
534
535 changes.push_back(
536 syncer::SyncChange(FROM_HERE,
537 syncer::SyncChange::ACTION_UPDATE,
538 sync_data));
539 }
511 } 540 }
512 541
513 syncer::SyncError error = 542 syncer::SyncError error =
514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); 543 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
515 } 544 }
516 545
517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { 546 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
518 DCHECK(pref_service_ == NULL); 547 DCHECK(pref_service_ == NULL);
519 pref_service_ = pref_service; 548 pref_service_ = pref_service;
520 } 549 }
521 550
522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, 551 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
523 bool from_sync) const { 552 bool from_sync) const {
524 SyncedPrefObserverMap::const_iterator observer_iter = 553 SyncedPrefObserverMap::const_iterator observer_iter =
525 synced_pref_observers_.find(path); 554 synced_pref_observers_.find(path);
526 if (observer_iter == synced_pref_observers_.end()) 555 if (observer_iter == synced_pref_observers_.end())
527 return; 556 return;
528 SyncedPrefObserverList* observers = observer_iter->second; 557 SyncedPrefObserverList* observers = observer_iter->second;
529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, 558 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
530 OnSyncedPrefChanged(path, from_sync)); 559 OnSyncedPrefChanged(path, from_sync));
531 } 560 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/prefs/session_startup_pref.cc » ('j') | chrome/browser/prefs/session_startup_pref.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698