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

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: Move to using a map for sync data entries. Add synced old pref value names to synced_preferences_. 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 30 matching lines...) Expand all
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 }
61 97
62 PrefModelAssociator::~PrefModelAssociator() { 98 PrefModelAssociator::~PrefModelAssociator() {
63 DCHECK(CalledOnValidThread()); 99 DCHECK(CalledOnValidThread());
64 pref_service_ = NULL; 100 pref_service_ = NULL;
65 101
66 STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(), 102 STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(),
67 synced_pref_observers_.end()); 103 synced_pref_observers_.end());
68 synced_pref_observers_.clear(); 104 synced_pref_observers_.clear();
69 } 105 }
70 106
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,
111 SyncDataMap* migrated_preference_list) {
75 const Value* user_pref_value = pref_service_->GetUserPrefValue( 112 const Value* user_pref_value = pref_service_->GetUserPrefValue(
76 pref_name.c_str()); 113 pref_name.c_str());
77 VLOG(1) << "Associating preference " << pref_name; 114 VLOG(1) << "Associating preference " << pref_name;
78 115
79 if (sync_pref.IsValid()) { 116 if (sync_pref.IsValid()) {
80 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref); 117 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
81 DCHECK_EQ(pref_name, preference.name()); 118 DCHECK(pref_name == preference.name() ||
82 119 (IsMigratedPreference(pref_name.c_str()) &&
120 preference.name() ==
121 GetOldMigratedPreferenceName(pref_name.c_str())));
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) {
92 // We have both server and local values. Merge them. 131 // We have both server and local values. Merge them.
(...skipping 17 matching lines...) Expand all
110 149
111 // If the merge resulted in an updated value, inform the syncer. 150 // If the merge resulted in an updated value, inform the syncer.
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,
Nicolas Zea 2013/10/10 20:35:09 thinking about it some more, ACTION_UPDATE here mi
robertshield 2013/10/11 18:29:31 Done.
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 migrated_preference_list) {
165 std::string old_pref_name =
166 GetOldMigratedPreferenceName(pref_name.c_str());
167 if (!CreatePrefSyncData(old_pref_name, *new_value, &sync_data)) {
168 LOG(ERROR) << "Failed to update preference.";
169 return;
170 }
171
172 (*migrated_preference_list)[old_pref_name] = sync_data;
173 }
122 } 174 }
123 } else if (!sync_value->IsType(Value::TYPE_NULL)) { 175 } else if (!sync_value->IsType(Value::TYPE_NULL)) {
124 // Only a server value exists. Just set the local user value. 176 // Only a server value exists. Just set the local user value.
125 pref_service_->Set(pref_name.c_str(), *sync_value); 177 pref_service_->Set(pref_name.c_str(), *sync_value);
126 } else { 178 } else {
127 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); 179 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
128 } 180 }
129 } else if (user_pref_value) { 181 } else if (user_pref_value) {
130 // The server does not know about this preference and should be added 182 // The server does not know about this preference and should be added
131 // to the syncer's database. 183 // to the syncer's database.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 DCHECK(!sync_processor_.get()); 215 DCHECK(!sync_processor_.get());
164 DCHECK(sync_processor.get()); 216 DCHECK(sync_processor.get());
165 DCHECK(sync_error_factory.get()); 217 DCHECK(sync_error_factory.get());
166 syncer::SyncMergeResult merge_result(type); 218 syncer::SyncMergeResult merge_result(type);
167 sync_processor_ = sync_processor.Pass(); 219 sync_processor_ = sync_processor.Pass();
168 sync_error_factory_ = sync_error_factory.Pass(); 220 sync_error_factory_ = sync_error_factory.Pass();
169 221
170 syncer::SyncChangeList new_changes; 222 syncer::SyncChangeList new_changes;
171 std::set<std::string> remaining_preferences = registered_preferences_; 223 std::set<std::string> remaining_preferences = registered_preferences_;
172 224
225 // Maintains a list of old migrated preference names that we wish to sync.
226 // Keep track of these in a list such that when the preference iteration
227 // loops below are complete we can go back and determine whether
228 SyncDataMap migrated_preference_list;
229
173 // Go through and check for all preferences we care about that sync already 230 // Go through and check for all preferences we care about that sync already
174 // knows about. 231 // knows about.
175 for (syncer::SyncDataList::const_iterator sync_iter = 232 for (syncer::SyncDataList::const_iterator sync_iter =
176 initial_sync_data.begin(); 233 initial_sync_data.begin();
177 sync_iter != initial_sync_data.end(); 234 sync_iter != initial_sync_data.end();
178 ++sync_iter) { 235 ++sync_iter) {
179 DCHECK_EQ(type_, sync_iter->GetDataType()); 236 DCHECK_EQ(type_, sync_iter->GetDataType());
180 237
181 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter); 238 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
182 const std::string& sync_pref_name = preference.name(); 239 std::string sync_pref_name = preference.name();
183 240
184 if (remaining_preferences.count(sync_pref_name) == 0) { 241 if (remaining_preferences.count(sync_pref_name) == 0) {
185 // We're not syncing this preference locally, ignore the sync data. 242 if (IsOldMigratedPreference(sync_pref_name.c_str())) {
186 // TODO(zea): Eventually we want to be able to have the syncable service 243 // This old pref name is not syncable locally anymore but we accept
187 // reconstruct all sync data for it's datatype (therefore having 244 // changes from other Chrome installs of previous versions and migrate
188 // GetAllSyncData be a complete representation). We should store this data 245 // them to the new name. Note that we will be merging any differences
189 // somewhere, even if we don't use it. 246 // between the new and old values and sync'ing them back.
190 continue; 247 sync_pref_name = GetNewMigratedPreferenceName(sync_pref_name.c_str());
248 } else {
249 // We're not syncing this preference locally, ignore the sync data.
250 // TODO(zea): Eventually we want to be able to have the syncable service
251 // reconstruct all sync data for its datatype (therefore having
252 // GetAllSyncData be a complete representation). We should store this
253 // data somewhere, even if we don't use it.
254 continue;
255 }
256 } else {
257 remaining_preferences.erase(sync_pref_name);
191 } 258 }
192 259 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes,
193 remaining_preferences.erase(sync_pref_name); 260 &migrated_preference_list);
194 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes);
195 } 261 }
196 262
197 // Go through and build sync data for any remaining preferences. 263 // Go through and build sync data for any remaining preferences.
198 for (std::set<std::string>::iterator pref_name_iter = 264 for (std::set<std::string>::iterator pref_name_iter =
199 remaining_preferences.begin(); 265 remaining_preferences.begin();
200 pref_name_iter != remaining_preferences.end(); 266 pref_name_iter != remaining_preferences.end();
201 ++pref_name_iter) { 267 ++pref_name_iter) {
202 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes); 268 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes,
269 &migrated_preference_list);
270 }
271
272 // Now go over any migrated preference names and build sync data for them too.
273 for (SyncDataMap::const_iterator migrated_pref_iter =
274 migrated_preference_list.begin();
275 migrated_pref_iter != migrated_preference_list.end();
276 ++migrated_pref_iter) {
277 syncer::SyncChange::SyncChangeType change_type =
278 (synced_preferences_.count(migrated_pref_iter->first) == 0) ?
279 syncer::SyncChange::ACTION_ADD :
280 syncer::SyncChange::ACTION_UPDATE;
281 new_changes.push_back(
282 syncer::SyncChange(FROM_HERE, change_type, migrated_pref_iter->second));
203 } 283 }
204 284
205 // Push updates to sync. 285 // Push updates to sync.
206 merge_result.set_error( 286 merge_result.set_error(
207 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); 287 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
208 if (merge_result.error().IsSet()) 288 if (merge_result.error().IsSet())
209 return merge_result; 289 return merge_result;
210 290
211 models_associated_ = true; 291 models_associated_ = true;
212 pref_service_->OnIsSyncingChanged(); 292 pref_service_->OnIsSyncingChanged();
213 return merge_result; 293 return merge_result;
214 } 294 }
215 295
216 void PrefModelAssociator::StopSyncing(syncer::ModelType type) { 296 void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
217 DCHECK_EQ(type_, type); 297 DCHECK_EQ(type_, type);
218 models_associated_ = false; 298 models_associated_ = false;
219 sync_processor_.reset(); 299 sync_processor_.reset();
220 sync_error_factory_.reset(); 300 sync_error_factory_.reset();
221 pref_service_->OnIsSyncingChanged(); 301 pref_service_->OnIsSyncingChanged();
222 } 302 }
223 303
224 scoped_ptr<Value> PrefModelAssociator::MergePreference( 304 scoped_ptr<Value> PrefModelAssociator::MergePreference(
225 const std::string& name, 305 const std::string& name,
226 const Value& local_value, 306 const Value& local_value,
227 const Value& server_value) { 307 const Value& server_value) {
228 if (name == prefs::kURLsToRestoreOnStartup) { 308 // This function special cases preferences individually, so don't attempt
309 // to merge for all migrated values.
310 if (name == prefs::kURLsToRestoreOnStartup ||
311 name == prefs::kURLsToRestoreOnStartupOld) {
229 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass(); 312 return scoped_ptr<Value>(MergeListValues(local_value, server_value)).Pass();
230 } 313 }
231 314
232 if (name == prefs::kContentSettingsPatternPairs) { 315 if (name == prefs::kContentSettingsPatternPairs) {
233 return scoped_ptr<Value>( 316 return scoped_ptr<Value>(
234 MergeDictionaryValues(local_value, server_value)).Pass(); 317 MergeDictionaryValues(local_value, server_value)).Pass();
235 } 318 }
236 319
237 // If this is not a specially handled preference, server wins. 320 // If this is not a specially handled preference, server wins.
238 return scoped_ptr<Value>(server_value.DeepCopy()).Pass(); 321 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 464 // TODO(zea): consider taking some further action such as erasing the bad
382 // data. 465 // data.
383 if (!value.get()) 466 if (!value.get())
384 continue; 467 continue;
385 468
386 // It is possible that we may receive a change to a preference we do not 469 // 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 470 // want to sync. For example if the user is syncing a Mac client and a
388 // Windows client, the Windows client does not support 471 // Windows client, the Windows client does not support
389 // kConfirmToQuitEnabled. Ignore updates from these preferences. 472 // kConfirmToQuitEnabled. Ignore updates from these preferences.
390 const char* pref_name = name.c_str(); 473 const char* pref_name = name.c_str();
474 std::string new_name;
475 // We migrated this preference name, so do as if the name had not changed.
476 if (IsOldMigratedPreference(pref_name)) {
477 new_name = GetNewMigratedPreferenceName(pref_name);
478 pref_name = new_name.c_str();
479 }
480
391 if (!IsPrefRegistered(pref_name)) 481 if (!IsPrefRegistered(pref_name))
392 continue; 482 continue;
393 483
394 const PrefService::Preference* pref = 484 const PrefService::Preference* pref =
395 pref_service_->FindPreference(pref_name); 485 pref_service_->FindPreference(pref_name);
396 DCHECK(pref); 486 DCHECK(pref);
397 487
398 // This will only modify the user controlled value store, which takes 488 // This will only modify the user controlled value store, which takes
399 // priority over the default value but is ignored if the preference is 489 // priority over the default value but is ignored if the preference is
400 // policy controlled. 490 // policy controlled.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 } 579 }
490 580
491 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 581 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
492 582
493 NotifySyncedPrefObservers(name, false /*from_sync*/); 583 NotifySyncedPrefObservers(name, false /*from_sync*/);
494 584
495 if (synced_preferences_.count(name) == 0) { 585 if (synced_preferences_.count(name) == 0) {
496 // Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..) 586 // Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..)
497 // will determine if we care about its data (e.g. if it has a default value 587 // will determine if we care about its data (e.g. if it has a default value
498 // and hasn't been changed yet we don't) and take care syncing any new data. 588 // and hasn't been changed yet we don't) and take care syncing any new data.
499 InitPrefAndAssociate(syncer::SyncData(), name, &changes); 589 InitPrefAndAssociate(syncer::SyncData(), name, &changes, NULL);
500 } else { 590 } else {
501 // We are already syncing this preference, just update it's sync node. 591 // We are already syncing this preference, just update it's sync node.
502 syncer::SyncData sync_data; 592 syncer::SyncData sync_data;
503 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) { 593 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
504 LOG(ERROR) << "Failed to update preference."; 594 LOG(ERROR) << "Failed to update preference.";
505 return; 595 return;
506 } 596 }
507 changes.push_back( 597 changes.push_back(
508 syncer::SyncChange(FROM_HERE, 598 syncer::SyncChange(FROM_HERE,
509 syncer::SyncChange::ACTION_UPDATE, 599 syncer::SyncChange::ACTION_UPDATE,
510 sync_data)); 600 sync_data));
601 // This preference has been migrated from an old version that must be kept
602 // in sync on older versions of Chrome.
603 if (IsMigratedPreference(name.c_str())) {
604 std::string old_pref_name = GetOldMigratedPreferenceName(name.c_str());
605 if (!CreatePrefSyncData(old_pref_name,
606 *preference->GetValue(),
607 &sync_data)) {
608 LOG(ERROR) << "Failed to update preference.";
609 return;
610 }
611
612 syncer::SyncChange::SyncChangeType change_type =
613 (synced_preferences_.count(name) == 0) ?
614 syncer::SyncChange::ACTION_ADD :
615 syncer::SyncChange::ACTION_UPDATE;
616 changes.push_back(
617 syncer::SyncChange(FROM_HERE, change_type, sync_data));
618 }
511 } 619 }
512 620
513 syncer::SyncError error = 621 syncer::SyncError error =
514 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); 622 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
515 } 623 }
516 624
517 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { 625 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
518 DCHECK(pref_service_ == NULL); 626 DCHECK(pref_service_ == NULL);
519 pref_service_ = pref_service; 627 pref_service_ = pref_service;
520 } 628 }
521 629
522 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, 630 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
523 bool from_sync) const { 631 bool from_sync) const {
524 SyncedPrefObserverMap::const_iterator observer_iter = 632 SyncedPrefObserverMap::const_iterator observer_iter =
525 synced_pref_observers_.find(path); 633 synced_pref_observers_.find(path);
526 if (observer_iter == synced_pref_observers_.end()) 634 if (observer_iter == synced_pref_observers_.end())
527 return; 635 return;
528 SyncedPrefObserverList* observers = observer_iter->second; 636 SyncedPrefObserverList* observers = observer_iter->second;
529 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, 637 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
530 OnSyncedPrefChanged(path, from_sync)); 638 OnSyncedPrefChanged(path, from_sync));
531 } 639 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_model_associator.h ('k') | chrome/browser/prefs/session_startup_pref.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698