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