[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / prefs / pref_model_associator.cc
blob0c51a136d40ef6fc32273ad7204e45d475349e01
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/chrome_notification_types.h"
17 #include "chrome/browser/prefs/pref_service_syncable.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/content_settings/core/browser/website_settings_info.h"
20 #include "components/content_settings/core/browser/website_settings_registry.h"
21 #include "sync/api/sync_change.h"
22 #include "sync/api/sync_error_factory.h"
23 #include "sync/protocol/preference_specifics.pb.h"
24 #include "sync/protocol/sync.pb.h"
26 using syncer::PREFERENCES;
27 using syncer::PRIORITY_PREFERENCES;
29 namespace {
31 const sync_pb::PreferenceSpecifics& GetSpecifics(const syncer::SyncData& pref) {
32 DCHECK(pref.GetDataType() == syncer::PREFERENCES ||
33 pref.GetDataType() == syncer::PRIORITY_PREFERENCES);
34 if (pref.GetDataType() == syncer::PRIORITY_PREFERENCES) {
35 return pref.GetSpecifics().priority_preference().preference();
36 } else {
37 return pref.GetSpecifics().preference();
41 sync_pb::PreferenceSpecifics* GetMutableSpecifics(
42 const syncer::ModelType type,
43 sync_pb::EntitySpecifics* specifics) {
44 if (type == syncer::PRIORITY_PREFERENCES) {
45 DCHECK(!specifics->has_preference());
46 return specifics->mutable_priority_preference()->mutable_preference();
47 } else {
48 DCHECK(!specifics->has_priority_preference());
49 return specifics->mutable_preference();
53 // List of migrated preference name pairs. If a preference is migrated
54 // (meaning renamed) adding the old and new preference names here will ensure
55 // that the sync engine knows how to deal with the synced values coming in
56 // with the old name. Preference migration itself doesn't happen here. It may
57 // happen in session_startup_pref.cc.
58 const struct MigratedPreferences {
59 const char* const old_name;
60 const char* const new_name;
61 } kMigratedPreferences[] = {
62 { prefs::kURLsToRestoreOnStartupOld, prefs::kURLsToRestoreOnStartup },
65 std::string GetOldMigratedPreferenceName(const char* preference_name) {
66 for (size_t i = 0; i < arraysize(kMigratedPreferences); ++i) {
67 if (!strcmp(kMigratedPreferences[i].new_name, preference_name))
68 return kMigratedPreferences[i].old_name;
70 return std::string();
73 std::string GetNewMigratedPreferenceName(const char* old_preference_name) {
74 for (size_t i = 0; i < arraysize(kMigratedPreferences); ++i) {
75 if (!strcmp(kMigratedPreferences[i].old_name, old_preference_name))
76 return kMigratedPreferences[i].new_name;
78 return std::string();
81 } // namespace
83 PrefModelAssociator::PrefModelAssociator(syncer::ModelType type)
84 : models_associated_(false),
85 processing_syncer_changes_(false),
86 pref_service_(NULL),
87 type_(type) {
88 DCHECK(CalledOnValidThread());
89 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES);
92 PrefModelAssociator::~PrefModelAssociator() {
93 DCHECK(CalledOnValidThread());
94 pref_service_ = NULL;
96 STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(),
97 synced_pref_observers_.end());
98 synced_pref_observers_.clear();
101 void PrefModelAssociator::InitPrefAndAssociate(
102 const syncer::SyncData& sync_pref,
103 const std::string& pref_name,
104 syncer::SyncChangeList* sync_changes,
105 SyncDataMap* migrated_preference_list) {
106 const base::Value* user_pref_value = pref_service_->GetUserPrefValue(
107 pref_name.c_str());
108 VLOG(1) << "Associating preference " << pref_name;
110 if (sync_pref.IsValid()) {
111 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
112 DCHECK(pref_name == preference.name() ||
113 (IsMigratedPreference(pref_name.c_str()) &&
114 preference.name() ==
115 GetOldMigratedPreferenceName(pref_name.c_str())));
116 base::JSONReader reader;
117 scoped_ptr<base::Value> sync_value(reader.ReadToValue(preference.value()));
118 if (!sync_value.get()) {
119 LOG(ERROR) << "Failed to deserialize preference value: "
120 << reader.GetErrorMessage();
121 return;
124 if (user_pref_value) {
125 DVLOG(1) << "Found user pref value for " << pref_name;
126 // We have both server and local values. Merge them.
127 scoped_ptr<base::Value> new_value(
128 MergePreference(pref_name, *user_pref_value, *sync_value));
130 // Update the local preference based on what we got from the
131 // sync server. Note: this only updates the user value store, which is
132 // ignored if the preference is policy controlled.
133 if (new_value->IsType(base::Value::TYPE_NULL)) {
134 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
135 pref_service_->ClearPref(pref_name.c_str());
136 } else if (!new_value->IsType(user_pref_value->GetType())) {
137 LOG(WARNING) << "Synced value for " << preference.name()
138 << " is of type " << new_value->GetType()
139 << " which doesn't match pref type "
140 << user_pref_value->GetType();
141 } else if (!user_pref_value->Equals(new_value.get())) {
142 pref_service_->Set(pref_name.c_str(), *new_value);
145 // If the merge resulted in an updated value, inform the syncer.
146 if (!sync_value->Equals(new_value.get())) {
147 syncer::SyncData sync_data;
148 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) {
149 LOG(ERROR) << "Failed to update preference.";
150 return;
153 if (IsMigratedPreference(pref_name.c_str())) {
154 // This preference has been migrated from an old version that must be
155 // kept in sync on older versions of Chrome.
156 std::string old_pref_name =
157 GetOldMigratedPreferenceName(pref_name.c_str());
159 if (preference.name() == old_pref_name) {
160 DCHECK(migrated_preference_list);
161 // If the name the syncer has is the old pre-migration value, then
162 // it's possible the new migrated preference name hasn't been synced
163 // yet. In that case the SyncChange should be an ACTION_ADD rather
164 // than an ACTION_UPDATE. Defer the decision of whether to sync with
165 // ACTION_ADD or ACTION_UPDATE until the migrated_preferences phase.
166 if (migrated_preference_list)
167 (*migrated_preference_list)[pref_name] = sync_data;
168 } else {
169 DCHECK_EQ(preference.name(), pref_name);
170 sync_changes->push_back(
171 syncer::SyncChange(FROM_HERE,
172 syncer::SyncChange::ACTION_UPDATE,
173 sync_data));
176 syncer::SyncData old_sync_data;
177 if (!CreatePrefSyncData(old_pref_name, *new_value, &old_sync_data)) {
178 LOG(ERROR) << "Failed to update preference.";
179 return;
181 if (migrated_preference_list)
182 (*migrated_preference_list)[old_pref_name] = old_sync_data;
183 } else {
184 sync_changes->push_back(
185 syncer::SyncChange(FROM_HERE,
186 syncer::SyncChange::ACTION_UPDATE,
187 sync_data));
190 } else if (!sync_value->IsType(base::Value::TYPE_NULL)) {
191 // Only a server value exists. Just set the local user value.
192 pref_service_->Set(pref_name.c_str(), *sync_value);
193 } else {
194 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
196 synced_preferences_.insert(preference.name());
197 } else if (user_pref_value) {
198 // The server does not know about this preference and should be added
199 // to the syncer's database.
200 syncer::SyncData sync_data;
201 if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) {
202 LOG(ERROR) << "Failed to update preference.";
203 return;
205 sync_changes->push_back(
206 syncer::SyncChange(FROM_HERE,
207 syncer::SyncChange::ACTION_ADD,
208 sync_data));
209 synced_preferences_.insert(pref_name);
212 // Else this pref does not have a sync value but also does not have a user
213 // controlled value (either it's a default value or it's policy controlled,
214 // either way it's not interesting). We can ignore it. Once it gets changed,
215 // we'll send the new user controlled value to the syncer.
218 syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing(
219 syncer::ModelType type,
220 const syncer::SyncDataList& initial_sync_data,
221 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
222 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
223 DCHECK_EQ(type_, type);
224 DCHECK(CalledOnValidThread());
225 DCHECK(pref_service_);
226 DCHECK(!sync_processor_.get());
227 DCHECK(sync_processor.get());
228 DCHECK(sync_error_factory.get());
229 syncer::SyncMergeResult merge_result(type);
230 sync_processor_ = sync_processor.Pass();
231 sync_error_factory_ = sync_error_factory.Pass();
233 syncer::SyncChangeList new_changes;
234 std::set<std::string> remaining_preferences = registered_preferences_;
236 // Maintains a list of old migrated preference names that we wish to sync.
237 // Keep track of these in a list such that when the preference iteration
238 // loops below are complete we can go back and determine whether
239 SyncDataMap migrated_preference_list;
241 // Go through and check for all preferences we care about that sync already
242 // knows about.
243 for (syncer::SyncDataList::const_iterator sync_iter =
244 initial_sync_data.begin();
245 sync_iter != initial_sync_data.end();
246 ++sync_iter) {
247 DCHECK_EQ(type_, sync_iter->GetDataType());
249 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
250 std::string sync_pref_name = preference.name();
252 if (remaining_preferences.count(sync_pref_name) == 0) {
253 if (IsOldMigratedPreference(sync_pref_name.c_str())) {
254 // This old pref name is not syncable locally anymore but we accept
255 // changes from other Chrome installs of previous versions and migrate
256 // them to the new name. Note that we will be merging any differences
257 // between the new and old values and sync'ing them back.
258 sync_pref_name = GetNewMigratedPreferenceName(sync_pref_name.c_str());
259 } else {
260 // We're not syncing this preference locally, ignore the sync data.
261 // TODO(zea): Eventually we want to be able to have the syncable service
262 // reconstruct all sync data for its datatype (therefore having
263 // GetAllSyncData be a complete representation). We should store this
264 // data somewhere, even if we don't use it.
265 continue;
267 } else {
268 remaining_preferences.erase(sync_pref_name);
270 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes,
271 &migrated_preference_list);
274 // Go through and build sync data for any remaining preferences.
275 for (std::set<std::string>::iterator pref_name_iter =
276 remaining_preferences.begin();
277 pref_name_iter != remaining_preferences.end();
278 ++pref_name_iter) {
279 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes,
280 &migrated_preference_list);
283 // Now go over any migrated preference names and build sync data for them too.
284 for (SyncDataMap::const_iterator migrated_pref_iter =
285 migrated_preference_list.begin();
286 migrated_pref_iter != migrated_preference_list.end();
287 ++migrated_pref_iter) {
288 syncer::SyncChange::SyncChangeType change_type =
289 (synced_preferences_.count(migrated_pref_iter->first) == 0) ?
290 syncer::SyncChange::ACTION_ADD :
291 syncer::SyncChange::ACTION_UPDATE;
292 new_changes.push_back(
293 syncer::SyncChange(FROM_HERE, change_type, migrated_pref_iter->second));
294 synced_preferences_.insert(migrated_pref_iter->first);
297 // Push updates to sync.
298 merge_result.set_error(
299 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
300 if (merge_result.error().IsSet())
301 return merge_result;
303 models_associated_ = true;
304 pref_service_->OnIsSyncingChanged();
305 return merge_result;
308 void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
309 DCHECK_EQ(type_, type);
310 models_associated_ = false;
311 sync_processor_.reset();
312 sync_error_factory_.reset();
313 pref_service_->OnIsSyncingChanged();
316 scoped_ptr<base::Value> PrefModelAssociator::MergePreference(
317 const std::string& name,
318 const base::Value& local_value,
319 const base::Value& server_value) {
320 // This function special cases preferences individually, so don't attempt
321 // to merge for all migrated values.
322 if (name == prefs::kURLsToRestoreOnStartup ||
323 name == prefs::kURLsToRestoreOnStartupOld) {
324 return make_scoped_ptr(MergeListValues(local_value, server_value));
327 content_settings::WebsiteSettingsRegistry* registry =
328 content_settings::WebsiteSettingsRegistry::GetInstance();
329 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
330 ContentSettingsType type = static_cast<ContentSettingsType>(i);
331 if (registry->Get(type)->pref_name() == name)
332 return make_scoped_ptr(MergeDictionaryValues(local_value, server_value));
335 // If this is not a specially handled preference, server wins.
336 return make_scoped_ptr(server_value.DeepCopy());
339 bool PrefModelAssociator::CreatePrefSyncData(
340 const std::string& name,
341 const base::Value& value,
342 syncer::SyncData* sync_data) const {
343 if (value.IsType(base::Value::TYPE_NULL)) {
344 LOG(ERROR) << "Attempting to sync a null pref value for " << name;
345 return false;
348 std::string serialized;
349 // TODO(zea): consider JSONWriter::Write since you don't have to check
350 // failures to deserialize.
351 JSONStringValueSerializer json(&serialized);
352 if (!json.Serialize(value)) {
353 LOG(ERROR) << "Failed to serialize preference value.";
354 return false;
357 sync_pb::EntitySpecifics specifics;
358 sync_pb::PreferenceSpecifics* pref_specifics =
359 GetMutableSpecifics(type_, &specifics);
361 pref_specifics->set_name(name);
362 pref_specifics->set_value(serialized);
363 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
364 return true;
367 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value,
368 const base::Value& to_value) {
369 if (from_value.GetType() == base::Value::TYPE_NULL)
370 return to_value.DeepCopy();
371 if (to_value.GetType() == base::Value::TYPE_NULL)
372 return from_value.DeepCopy();
374 DCHECK(from_value.GetType() == base::Value::TYPE_LIST);
375 DCHECK(to_value.GetType() == base::Value::TYPE_LIST);
376 const base::ListValue& from_list_value =
377 static_cast<const base::ListValue&>(from_value);
378 const base::ListValue& to_list_value =
379 static_cast<const base::ListValue&>(to_value);
380 base::ListValue* result = to_list_value.DeepCopy();
382 for (base::ListValue::const_iterator i = from_list_value.begin();
383 i != from_list_value.end(); ++i) {
384 base::Value* value = (*i)->DeepCopy();
385 result->AppendIfNotPresent(value);
387 return result;
390 base::Value* PrefModelAssociator::MergeDictionaryValues(
391 const base::Value& from_value,
392 const base::Value& to_value) {
393 if (from_value.GetType() == base::Value::TYPE_NULL)
394 return to_value.DeepCopy();
395 if (to_value.GetType() == base::Value::TYPE_NULL)
396 return from_value.DeepCopy();
398 DCHECK_EQ(from_value.GetType(), base::Value::TYPE_DICTIONARY);
399 DCHECK_EQ(to_value.GetType(), base::Value::TYPE_DICTIONARY);
400 const base::DictionaryValue& from_dict_value =
401 static_cast<const base::DictionaryValue&>(from_value);
402 const base::DictionaryValue& to_dict_value =
403 static_cast<const base::DictionaryValue&>(to_value);
404 base::DictionaryValue* result = to_dict_value.DeepCopy();
406 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd();
407 it.Advance()) {
408 const base::Value* from_value = &it.value();
409 base::Value* to_key_value;
410 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) {
411 if (to_key_value->GetType() == base::Value::TYPE_DICTIONARY) {
412 base::Value* merged_value =
413 MergeDictionaryValues(*from_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_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));