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

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

Issue 444763005: Clear a preference when sync tries to delete a preference (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 syncer::SyncError::DATATYPE_ERROR, 464 syncer::SyncError::DATATYPE_ERROR,
465 "Models not yet associated.", 465 "Models not yet associated.",
466 PREFERENCES); 466 PREFERENCES);
467 return error; 467 return error;
468 } 468 }
469 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true); 469 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
470 syncer::SyncChangeList::const_iterator iter; 470 syncer::SyncChangeList::const_iterator iter;
471 for (iter = change_list.begin(); iter != change_list.end(); ++iter) { 471 for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
472 DCHECK_EQ(type_, iter->sync_data().GetDataType()); 472 DCHECK_EQ(type_, iter->sync_data().GetDataType());
473 473
474 std::string name;
475 const sync_pb::PreferenceSpecifics& pref_specifics = 474 const sync_pb::PreferenceSpecifics& pref_specifics =
476 GetSpecifics(iter->sync_data()); 475 GetSpecifics(iter->sync_data());
477 476
478 scoped_ptr<base::Value> value(ReadPreferenceSpecifics(pref_specifics, 477 std::string name = pref_specifics.name();
479 &name));
480
481 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
482 // We never delete preferences.
483 NOTREACHED() << "Attempted to process sync delete change for " << name
484 << ". Skipping.";
485 continue;
486 }
487
488 // Skip values we can't deserialize.
489 // TODO(zea): consider taking some further action such as erasing the bad
490 // data.
491 if (!value.get())
492 continue;
493
494 // It is possible that we may receive a change to a preference we do not 478 // It is possible that we may receive a change to a preference we do not
495 // want to sync. For example if the user is syncing a Mac client and a 479 // want to sync. For example if the user is syncing a Mac client and a
496 // Windows client, the Windows client does not support 480 // Windows client, the Windows client does not support
497 // kConfirmToQuitEnabled. Ignore updates from these preferences. 481 // kConfirmToQuitEnabled. Ignore updates from these preferences.
498 const char* pref_name = name.c_str(); 482 const char* pref_name = name.c_str();
499 std::string new_name; 483 std::string new_name;
500 // We migrated this preference name, so do as if the name had not changed. 484 // We migrated this preference name, so do as if the name had not changed.
501 if (IsOldMigratedPreference(pref_name)) { 485 if (IsOldMigratedPreference(pref_name)) {
502 new_name = GetNewMigratedPreferenceName(pref_name); 486 new_name = GetNewMigratedPreferenceName(pref_name);
503 pref_name = new_name.c_str(); 487 pref_name = new_name.c_str();
504 } 488 }
505 489
506 if (!IsPrefRegistered(pref_name)) 490 if (!IsPrefRegistered(pref_name))
507 continue; 491 continue;
508 492
509 const PrefService::Preference* pref = 493 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
510 pref_service_->FindPreference(pref_name); 494 pref_service_->ClearPref(pref_name);
511 DCHECK(pref); 495 continue;
496 }
497
498 scoped_ptr<base::Value> value(ReadPreferenceSpecifics(pref_specifics));
499 if (!value.get()) {
500 // Skip values we can't deserialize.
501 // TODO(zea): consider taking some further action such as erasing the bad
502 // data.
503 continue;
504 }
512 505
513 // This will only modify the user controlled value store, which takes 506 // This will only modify the user controlled value store, which takes
514 // priority over the default value but is ignored if the preference is 507 // priority over the default value but is ignored if the preference is
515 // policy controlled. 508 // policy controlled.
516 pref_service_->Set(pref_name, *value); 509 pref_service_->Set(pref_name, *value);
517 510
518 NotifySyncedPrefObservers(name, true /*from_sync*/); 511 NotifySyncedPrefObservers(name, true /*from_sync*/);
519 512
520 // Keep track of any newly synced preferences. 513 // Keep track of any newly synced preferences.
521 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) { 514 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
522 synced_preferences_.insert(name); 515 synced_preferences_.insert(name);
523 } 516 }
524 } 517 }
525 return syncer::SyncError(); 518 return syncer::SyncError();
526 } 519 }
527 520
528 base::Value* PrefModelAssociator::ReadPreferenceSpecifics( 521 base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
529 const sync_pb::PreferenceSpecifics& preference, 522 const sync_pb::PreferenceSpecifics& preference) {
530 std::string* name) {
531 base::JSONReader reader; 523 base::JSONReader reader;
532 scoped_ptr<base::Value> value(reader.ReadToValue(preference.value())); 524 scoped_ptr<base::Value> value(reader.ReadToValue(preference.value()));
533 if (!value.get()) { 525 if (!value.get()) {
534 std::string err = "Failed to deserialize preference value: " + 526 std::string err = "Failed to deserialize preference value: " +
535 reader.GetErrorMessage(); 527 reader.GetErrorMessage();
536 LOG(ERROR) << err; 528 LOG(ERROR) << err;
537 return NULL; 529 return NULL;
538 } 530 }
539 *name = preference.name();
540 return value.release(); 531 return value.release();
541 } 532 }
542 533
543 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const { 534 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
544 return synced_preferences_.find(name) != synced_preferences_.end(); 535 return synced_preferences_.find(name) != synced_preferences_.end();
545 } 536 }
546 537
547 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name, 538 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
548 SyncedPrefObserver* observer) { 539 SyncedPrefObserver* observer) {
549 SyncedPrefObserverList* observers = synced_pref_observers_[name]; 540 SyncedPrefObserverList* observers = synced_pref_observers_[name];
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, 646 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
656 bool from_sync) const { 647 bool from_sync) const {
657 SyncedPrefObserverMap::const_iterator observer_iter = 648 SyncedPrefObserverMap::const_iterator observer_iter =
658 synced_pref_observers_.find(path); 649 synced_pref_observers_.find(path);
659 if (observer_iter == synced_pref_observers_.end()) 650 if (observer_iter == synced_pref_observers_.end())
660 return; 651 return;
661 SyncedPrefObserverList* observers = observer_iter->second; 652 SyncedPrefObserverList* observers = observer_iter->second;
662 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, 653 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
663 OnSyncedPrefChanged(path, from_sync)); 654 OnSyncedPrefChanged(path, from_sync));
664 } 655 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_model_associator.h ('k') | chrome/browser/prefs/prefs_syncable_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698