Home | History | Annotate | Download | only in prefs
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/prefs/pref_model_associator.h"
      6 
      7 #include "base/auto_reset.h"
      8 #include "base/json/json_reader.h"
      9 #include "base/json/json_string_value_serializer.h"
     10 #include "base/location.h"
     11 #include "base/logging.h"
     12 #include "base/prefs/pref_service.h"
     13 #include "base/stl_util.h"
     14 #include "base/strings/utf_string_conversions.h"
     15 #include "base/values.h"
     16 #include "chrome/browser/chrome_notification_types.h"
     17 #include "chrome/browser/prefs/pref_service_syncable.h"
     18 #include "chrome/common/pref_names.h"
     19 #include "sync/api/sync_change.h"
     20 #include "sync/api/sync_error_factory.h"
     21 #include "sync/protocol/preference_specifics.pb.h"
     22 #include "sync/protocol/sync.pb.h"
     23 
     24 using syncer::PREFERENCES;
     25 using syncer::PRIORITY_PREFERENCES;
     26 
     27 namespace {
     28 
     29 const sync_pb::PreferenceSpecifics& GetSpecifics(const syncer::SyncData& pref) {
     30   DCHECK(pref.GetDataType() == syncer::PREFERENCES ||
     31          pref.GetDataType() == syncer::PRIORITY_PREFERENCES);
     32   if (pref.GetDataType() == syncer::PRIORITY_PREFERENCES) {
     33     return pref.GetSpecifics().priority_preference().preference();
     34   } else {
     35     return pref.GetSpecifics().preference();
     36   }
     37 }
     38 
     39 sync_pb::PreferenceSpecifics* GetMutableSpecifics(
     40     const syncer::ModelType type,
     41     sync_pb::EntitySpecifics* specifics) {
     42   if (type == syncer::PRIORITY_PREFERENCES) {
     43     DCHECK(!specifics->has_preference());
     44     return specifics->mutable_priority_preference()->mutable_preference();
     45   } else {
     46     DCHECK(!specifics->has_priority_preference());
     47     return specifics->mutable_preference();
     48   }
     49 }
     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 }  // namespace
     80 
     81 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type)
     82     : models_associated_(false),
     83       processing_syncer_changes_(false),
     84       pref_service_(NULL),
     85       type_(type) {
     86   DCHECK(CalledOnValidThread());
     87   DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES);
     88 }
     89 
     90 PrefModelAssociator::~PrefModelAssociator() {
     91   DCHECK(CalledOnValidThread());
     92   pref_service_ = NULL;
     93 
     94   STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(),
     95                                        synced_pref_observers_.end());
     96   synced_pref_observers_.clear();
     97 }
     98 
     99 void PrefModelAssociator::InitPrefAndAssociate(
    100     const syncer::SyncData& sync_pref,
    101     const std::string& pref_name,
    102     syncer::SyncChangeList* sync_changes,
    103     SyncDataMap* migrated_preference_list) {
    104   const base::Value* user_pref_value = pref_service_->GetUserPrefValue(
    105       pref_name.c_str());
    106   VLOG(1) << "Associating preference " << pref_name;
    107 
    108   if (sync_pref.IsValid()) {
    109     const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
    110     DCHECK(pref_name == preference.name() ||
    111            (IsMigratedPreference(pref_name.c_str()) &&
    112             preference.name() ==
    113                 GetOldMigratedPreferenceName(pref_name.c_str())));
    114     base::JSONReader reader;
    115     scoped_ptr<base::Value> sync_value(reader.ReadToValue(preference.value()));
    116     if (!sync_value.get()) {
    117       LOG(ERROR) << "Failed to deserialize preference value: "
    118                  << reader.GetErrorMessage();
    119       return;
    120     }
    121 
    122     if (user_pref_value) {
    123       DVLOG(1) << "Found user pref value for " << pref_name;
    124       // We have both server and local values. Merge them.
    125       scoped_ptr<base::Value> new_value(
    126           MergePreference(pref_name, *user_pref_value, *sync_value));
    127 
    128       // Update the local preference based on what we got from the
    129       // sync server. Note: this only updates the user value store, which is
    130       // ignored if the preference is policy controlled.
    131       if (new_value->IsType(base::Value::TYPE_NULL)) {
    132         LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
    133         pref_service_->ClearPref(pref_name.c_str());
    134       } else if (!new_value->IsType(user_pref_value->GetType())) {
    135         LOG(WARNING) << "Synced value for " << preference.name()
    136                      << " is of type " << new_value->GetType()
    137                      << " which doesn't match pref type "
    138                      << user_pref_value->GetType();
    139       } else if (!user_pref_value->Equals(new_value.get())) {
    140         pref_service_->Set(pref_name.c_str(), *new_value);
    141       }
    142 
    143       // If the merge resulted in an updated value, inform the syncer.
    144       if (!sync_value->Equals(new_value.get())) {
    145         syncer::SyncData sync_data;
    146         if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) {
    147           LOG(ERROR) << "Failed to update preference.";
    148           return;
    149         }
    150 
    151         if (IsMigratedPreference(pref_name.c_str())) {
    152           // This preference has been migrated from an old version that must be
    153           // kept in sync on older versions of Chrome.
    154           std::string old_pref_name =
    155               GetOldMigratedPreferenceName(pref_name.c_str());
    156 
    157           if (preference.name() == old_pref_name) {
    158             DCHECK(migrated_preference_list);
    159             // If the name the syncer has is the old pre-migration value, then
    160             // it's possible the new migrated preference name hasn't been synced
    161             // yet. In that case the SyncChange should be an ACTION_ADD rather
    162             // than an ACTION_UPDATE. Defer the decision of whether to sync with
    163             // ACTION_ADD or ACTION_UPDATE until the migrated_preferences phase.
    164             if (migrated_preference_list)
    165               (*migrated_preference_list)[pref_name] = sync_data;
    166           } else {
    167             DCHECK_EQ(preference.name(), pref_name);
    168             sync_changes->push_back(
    169                 syncer::SyncChange(FROM_HERE,
    170                                    syncer::SyncChange::ACTION_UPDATE,
    171                                    sync_data));
    172           }
    173 
    174           syncer::SyncData old_sync_data;
    175           if (!CreatePrefSyncData(old_pref_name, *new_value, &old_sync_data)) {
    176             LOG(ERROR) << "Failed to update preference.";
    177             return;
    178           }
    179           if (migrated_preference_list)
    180             (*migrated_preference_list)[old_pref_name] = old_sync_data;
    181         } else {
    182           sync_changes->push_back(
    183             syncer::SyncChange(FROM_HERE,
    184                                syncer::SyncChange::ACTION_UPDATE,
    185                                sync_data));
    186         }
    187       }
    188     } else if (!sync_value->IsType(base::Value::TYPE_NULL)) {
    189       // Only a server value exists. Just set the local user value.
    190       pref_service_->Set(pref_name.c_str(), *sync_value);
    191     } else {
    192       LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
    193     }
    194     synced_preferences_.insert(preference.name());
    195   } else if (user_pref_value) {
    196     // The server does not know about this preference and should be added
    197     // to the syncer's database.
    198     syncer::SyncData sync_data;
    199     if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) {
    200       LOG(ERROR) << "Failed to update preference.";
    201       return;
    202     }
    203     sync_changes->push_back(
    204         syncer::SyncChange(FROM_HERE,
    205                            syncer::SyncChange::ACTION_ADD,
    206                            sync_data));
    207     synced_preferences_.insert(pref_name);
    208   }
    209 
    210   // Else this pref does not have a sync value but also does not have a user
    211   // controlled value (either it's a default value or it's policy controlled,
    212   // either way it's not interesting). We can ignore it. Once it gets changed,
    213   // we'll send the new user controlled value to the syncer.
    214 }
    215 
    216 syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing(
    217     syncer::ModelType type,
    218     const syncer::SyncDataList& initial_sync_data,
    219     scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
    220     scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
    221   DCHECK_EQ(type_, type);
    222   DCHECK(CalledOnValidThread());
    223   DCHECK(pref_service_);
    224   DCHECK(!sync_processor_.get());
    225   DCHECK(sync_processor.get());
    226   DCHECK(sync_error_factory.get());
    227   syncer::SyncMergeResult merge_result(type);
    228   sync_processor_ = sync_processor.Pass();
    229   sync_error_factory_ = sync_error_factory.Pass();
    230 
    231   syncer::SyncChangeList new_changes;
    232   std::set<std::string> remaining_preferences = registered_preferences_;
    233 
    234   // Maintains a list of old migrated preference names that we wish to sync.
    235   // Keep track of these in a list such that when the preference iteration
    236   // loops below are complete we can go back and determine whether
    237   SyncDataMap migrated_preference_list;
    238 
    239   // Go through and check for all preferences we care about that sync already
    240   // knows about.
    241   for (syncer::SyncDataList::const_iterator sync_iter =
    242            initial_sync_data.begin();
    243        sync_iter != initial_sync_data.end();
    244        ++sync_iter) {
    245     DCHECK_EQ(type_, sync_iter->GetDataType());
    246 
    247     const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
    248     std::string sync_pref_name = preference.name();
    249 
    250     if (remaining_preferences.count(sync_pref_name) == 0) {
    251       if (IsOldMigratedPreference(sync_pref_name.c_str())) {
    252         // This old pref name is not syncable locally anymore but we accept
    253         // changes from other Chrome installs of previous versions and migrate
    254         // them to the new name. Note that we will be merging any differences
    255         // between the new and old values and sync'ing them back.
    256         sync_pref_name = GetNewMigratedPreferenceName(sync_pref_name.c_str());
    257       } else {
    258         // We're not syncing this preference locally, ignore the sync data.
    259         // TODO(zea): Eventually we want to be able to have the syncable service
    260         // reconstruct all sync data for its datatype (therefore having
    261         // GetAllSyncData be a complete representation). We should store this
    262         // data somewhere, even if we don't use it.
    263         continue;
    264       }
    265     } else {
    266       remaining_preferences.erase(sync_pref_name);
    267     }
    268     InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes,
    269                          &migrated_preference_list);
    270   }
    271 
    272   // Go through and build sync data for any remaining preferences.
    273   for (std::set<std::string>::iterator pref_name_iter =
    274           remaining_preferences.begin();
    275        pref_name_iter != remaining_preferences.end();
    276        ++pref_name_iter) {
    277     InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes,
    278                          &migrated_preference_list);
    279   }
    280 
    281   // Now go over any migrated preference names and build sync data for them too.
    282   for (SyncDataMap::const_iterator migrated_pref_iter =
    283           migrated_preference_list.begin();
    284        migrated_pref_iter != migrated_preference_list.end();
    285        ++migrated_pref_iter) {
    286     syncer::SyncChange::SyncChangeType change_type =
    287         (synced_preferences_.count(migrated_pref_iter->first) == 0) ?
    288             syncer::SyncChange::ACTION_ADD :
    289             syncer::SyncChange::ACTION_UPDATE;
    290     new_changes.push_back(
    291         syncer::SyncChange(FROM_HERE, change_type, migrated_pref_iter->second));
    292     synced_preferences_.insert(migrated_pref_iter->first);
    293   }
    294 
    295   // Push updates to sync.
    296   merge_result.set_error(
    297       sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
    298   if (merge_result.error().IsSet())
    299     return merge_result;
    300 
    301   models_associated_ = true;
    302   pref_service_->OnIsSyncingChanged();
    303   return merge_result;
    304 }
    305 
    306 void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
    307   DCHECK_EQ(type_, type);
    308   models_associated_ = false;
    309   sync_processor_.reset();
    310   sync_error_factory_.reset();
    311   pref_service_->OnIsSyncingChanged();
    312 }
    313 
    314 scoped_ptr<base::Value> PrefModelAssociator::MergePreference(
    315     const std::string& name,
    316     const base::Value& local_value,
    317     const base::Value& server_value) {
    318   // This function special cases preferences individually, so don't attempt
    319   // to merge for all migrated values.
    320   if (name == prefs::kURLsToRestoreOnStartup ||
    321       name == prefs::kURLsToRestoreOnStartupOld) {
    322     return scoped_ptr<base::Value>(
    323         MergeListValues(local_value, server_value)).Pass();
    324   }
    325 
    326   if (name == prefs::kContentSettingsPatternPairs) {
    327     return scoped_ptr<base::Value>(
    328         MergeDictionaryValues(local_value, server_value)).Pass();
    329   }
    330 
    331   // If this is not a specially handled preference, server wins.
    332   return scoped_ptr<base::Value>(server_value.DeepCopy()).Pass();
    333 }
    334 
    335 bool PrefModelAssociator::CreatePrefSyncData(
    336     const std::string& name,
    337     const base::Value& value,
    338     syncer::SyncData* sync_data) const {
    339   if (value.IsType(base::Value::TYPE_NULL)) {
    340     LOG(ERROR) << "Attempting to sync a null pref value for " << name;
    341     return false;
    342   }
    343 
    344   std::string serialized;
    345   // TODO(zea): consider JSONWriter::Write since you don't have to check
    346   // failures to deserialize.
    347   JSONStringValueSerializer json(&serialized);
    348   if (!json.Serialize(value)) {
    349     LOG(ERROR) << "Failed to serialize preference value.";
    350     return false;
    351   }
    352 
    353   sync_pb::EntitySpecifics specifics;
    354   sync_pb::PreferenceSpecifics* pref_specifics =
    355       GetMutableSpecifics(type_, &specifics);
    356 
    357   pref_specifics->set_name(name);
    358   pref_specifics->set_value(serialized);
    359   *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
    360   return true;
    361 }
    362 
    363 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value,
    364                                                   const base::Value& to_value) {
    365   if (from_value.GetType() == base::Value::TYPE_NULL)
    366     return to_value.DeepCopy();
    367   if (to_value.GetType() == base::Value::TYPE_NULL)
    368     return from_value.DeepCopy();
    369 
    370   DCHECK(from_value.GetType() == base::Value::TYPE_LIST);
    371   DCHECK(to_value.GetType() == base::Value::TYPE_LIST);
    372   const base::ListValue& from_list_value =
    373       static_cast<const base::ListValue&>(from_value);
    374   const base::ListValue& to_list_value =
    375       static_cast<const base::ListValue&>(to_value);
    376   base::ListValue* result = to_list_value.DeepCopy();
    377 
    378   for (base::ListValue::const_iterator i = from_list_value.begin();
    379        i != from_list_value.end(); ++i) {
    380     base::Value* value = (*i)->DeepCopy();
    381     result->AppendIfNotPresent(value);
    382   }
    383   return result;
    384 }
    385 
    386 base::Value* PrefModelAssociator::MergeDictionaryValues(
    387     const base::Value& from_value,
    388     const base::Value& to_value) {
    389   if (from_value.GetType() == base::Value::TYPE_NULL)
    390     return to_value.DeepCopy();
    391   if (to_value.GetType() == base::Value::TYPE_NULL)
    392     return from_value.DeepCopy();
    393 
    394   DCHECK_EQ(from_value.GetType(), base::Value::TYPE_DICTIONARY);
    395   DCHECK_EQ(to_value.GetType(), base::Value::TYPE_DICTIONARY);
    396   const base::DictionaryValue& from_dict_value =
    397       static_cast<const base::DictionaryValue&>(from_value);
    398   const base::DictionaryValue& to_dict_value =
    399       static_cast<const base::DictionaryValue&>(to_value);
    400   base::DictionaryValue* result = to_dict_value.DeepCopy();
    401 
    402   for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd();
    403        it.Advance()) {
    404     const base::Value* from_value = &it.value();
    405     base::Value* to_key_value;
    406     if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) {
    407       if (to_key_value->GetType() == base::Value::TYPE_DICTIONARY) {
    408         base::Value* merged_value =
    409             MergeDictionaryValues(*from_value, *to_key_value);
    410         result->SetWithoutPathExpansion(it.key(), merged_value);
    411       }
    412       // Note that for all other types we want to preserve the "to"
    413       // values so we do nothing here.
    414     } else {
    415       result->SetWithoutPathExpansion(it.key(), from_value->DeepCopy());
    416     }
    417   }
    418   return result;
    419 }
    420 
    421 // static
    422 bool PrefModelAssociator::IsMigratedPreference(const char* preference_name) {
    423   return !GetOldMigratedPreferenceName(preference_name).empty();
    424 }
    425 
    426 // static
    427 bool PrefModelAssociator::IsOldMigratedPreference(
    428     const char* old_preference_name) {
    429   return !GetNewMigratedPreferenceName(old_preference_name).empty();
    430 }
    431 
    432 // Note: This will build a model of all preferences registered as syncable
    433 // with user controlled data. We do not track any information for preferences
    434 // not registered locally as syncable and do not inform the syncer of
    435 // non-user controlled preferences.
    436 syncer::SyncDataList PrefModelAssociator::GetAllSyncData(
    437     syncer::ModelType type)
    438     const {
    439   DCHECK_EQ(type_, type);
    440   syncer::SyncDataList current_data;
    441   for (PreferenceSet::const_iterator iter = synced_preferences_.begin();
    442        iter != synced_preferences_.end();
    443        ++iter) {
    444     std::string name = *iter;
    445     const PrefService::Preference* pref =
    446       pref_service_->FindPreference(name.c_str());
    447     DCHECK(pref);
    448     if (!pref->IsUserControlled() || pref->IsDefaultValue())
    449       continue;  // This is not data we care about.
    450     // TODO(zea): plumb a way to read the user controlled value.
    451     syncer::SyncData sync_data;
    452     if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data))
    453       continue;
    454     current_data.push_back(sync_data);
    455   }
    456   return current_data;
    457 }
    458 
    459 syncer::SyncError PrefModelAssociator::ProcessSyncChanges(
    460     const tracked_objects::Location& from_here,
    461     const syncer::SyncChangeList& change_list) {
    462   if (!models_associated_) {
    463     syncer::SyncError error(FROM_HERE,
    464                             syncer::SyncError::DATATYPE_ERROR,
    465                             "Models not yet associated.",
    466                             PREFERENCES);
    467     return error;
    468   }
    469   base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
    470   syncer::SyncChangeList::const_iterator iter;
    471   for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
    472     DCHECK_EQ(type_, iter->sync_data().GetDataType());
    473 
    474     const sync_pb::PreferenceSpecifics& pref_specifics =
    475         GetSpecifics(iter->sync_data());
    476 
    477     std::string name = pref_specifics.name();
    478     // It is possible that we may receive a change to a preference we do not
    479     // want to sync. For example if the user is syncing a Mac client and a
    480     // Windows client, the Windows client does not support
    481     // kConfirmToQuitEnabled. Ignore updates from these preferences.
    482     const char* pref_name = name.c_str();
    483     std::string new_name;
    484     // We migrated this preference name, so do as if the name had not changed.
    485     if (IsOldMigratedPreference(pref_name)) {
    486       new_name = GetNewMigratedPreferenceName(pref_name);
    487       pref_name = new_name.c_str();
    488     }
    489 
    490     if (!IsPrefRegistered(pref_name))
    491       continue;
    492 
    493     if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
    494       pref_service_->ClearPref(pref_name);
    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     }
    505 
    506     // This will only modify the user controlled value store, which takes
    507     // priority over the default value but is ignored if the preference is
    508     // policy controlled.
    509     pref_service_->Set(pref_name, *value);
    510 
    511     NotifySyncedPrefObservers(name, true /*from_sync*/);
    512 
    513     // Keep track of any newly synced preferences.
    514     if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
    515       synced_preferences_.insert(name);
    516     }
    517   }
    518   return syncer::SyncError();
    519 }
    520 
    521 base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
    522     const sync_pb::PreferenceSpecifics& preference) {
    523   base::JSONReader reader;
    524   scoped_ptr<base::Value> value(reader.ReadToValue(preference.value()));
    525   if (!value.get()) {
    526     std::string err = "Failed to deserialize preference value: " +
    527         reader.GetErrorMessage();
    528     LOG(ERROR) << err;
    529     return NULL;
    530   }
    531   return value.release();
    532 }
    533 
    534 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
    535   return synced_preferences_.find(name) != synced_preferences_.end();
    536 }
    537 
    538 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
    539     SyncedPrefObserver* observer) {
    540   SyncedPrefObserverList* observers = synced_pref_observers_[name];
    541   if (observers == NULL) {
    542     observers = new SyncedPrefObserverList;
    543     synced_pref_observers_[name] = observers;
    544   }
    545   observers->AddObserver(observer);
    546 }
    547 
    548 void PrefModelAssociator::RemoveSyncedPrefObserver(const std::string& name,
    549     SyncedPrefObserver* observer) {
    550   SyncedPrefObserverMap::iterator observer_iter =
    551       synced_pref_observers_.find(name);
    552   if (observer_iter == synced_pref_observers_.end())
    553     return;
    554   SyncedPrefObserverList* observers = observer_iter->second;
    555   observers->RemoveObserver(observer);
    556 }
    557 
    558 std::set<std::string> PrefModelAssociator::registered_preferences() const {
    559   return registered_preferences_;
    560 }
    561 
    562 void PrefModelAssociator::RegisterPref(const char* name) {
    563   DCHECK(!models_associated_ && registered_preferences_.count(name) == 0);
    564   registered_preferences_.insert(name);
    565 }
    566 
    567 bool PrefModelAssociator::IsPrefRegistered(const char* name) {
    568   return registered_preferences_.count(name) > 0;
    569 }
    570 
    571 void PrefModelAssociator::ProcessPrefChange(const std::string& name) {
    572   if (processing_syncer_changes_)
    573     return;  // These are changes originating from us, ignore.
    574 
    575   // We only process changes if we've already associated models.
    576   if (!models_associated_)
    577     return;
    578 
    579   const PrefService::Preference* preference =
    580       pref_service_->FindPreference(name.c_str());
    581   if (!preference)
    582     return;
    583 
    584   if (!IsPrefRegistered(name.c_str()))
    585     return;  // We are not syncing this preference.
    586 
    587   syncer::SyncChangeList changes;
    588 
    589   if (!preference->IsUserModifiable()) {
    590     // If the preference is no longer user modifiable, it must now be controlled
    591     // by policy, whose values we do not sync. Just return. If the preference
    592     // stops being controlled by policy, it will revert back to the user value
    593     // (which we continue to update with sync changes).
    594     return;
    595   }
    596 
    597   base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
    598 
    599   NotifySyncedPrefObservers(name, false /*from_sync*/);
    600 
    601   if (synced_preferences_.count(name) == 0) {
    602     // Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..)
    603     // will determine if we care about its data (e.g. if it has a default value
    604     // and hasn't been changed yet we don't) and take care syncing any new data.
    605     InitPrefAndAssociate(syncer::SyncData(), name, &changes, NULL);
    606   } else {
    607     // We are already syncing this preference, just update it's sync node.
    608     syncer::SyncData sync_data;
    609     if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
    610       LOG(ERROR) << "Failed to update preference.";
    611       return;
    612     }
    613     changes.push_back(
    614         syncer::SyncChange(FROM_HERE,
    615                            syncer::SyncChange::ACTION_UPDATE,
    616                            sync_data));
    617     // This preference has been migrated from an old version that must be kept
    618     // in sync on older versions of Chrome.
    619     if (IsMigratedPreference(name.c_str())) {
    620       std::string old_pref_name = GetOldMigratedPreferenceName(name.c_str());
    621       if (!CreatePrefSyncData(old_pref_name,
    622                               *preference->GetValue(),
    623                               &sync_data)) {
    624         LOG(ERROR) << "Failed to update preference.";
    625         return;
    626       }
    627 
    628       syncer::SyncChange::SyncChangeType change_type =
    629           (synced_preferences_.count(old_pref_name) == 0) ?
    630               syncer::SyncChange::ACTION_ADD :
    631               syncer::SyncChange::ACTION_UPDATE;
    632       changes.push_back(
    633           syncer::SyncChange(FROM_HERE, change_type, sync_data));
    634     }
    635   }
    636 
    637   syncer::SyncError error =
    638       sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
    639 }
    640 
    641 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
    642   DCHECK(pref_service_ == NULL);
    643   pref_service_ = pref_service;
    644 }
    645 
    646 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
    647                                                     bool from_sync) const {
    648   SyncedPrefObserverMap::const_iterator observer_iter =
    649       synced_pref_observers_.find(path);
    650   if (observer_iter == synced_pref_observers_.end())
    651     return;
    652   SyncedPrefObserverList* observers = observer_iter->second;
    653   FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
    654                     OnSyncedPrefChanged(path, from_sync));
    655 }
    656