Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / prefs / pref_model_associator.cc
blob0ba05abdaa3274ac605187d636527c02c35085a1
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 (const content_settings::WebsiteSettingsInfo* info : *registry) {
329 if (info->pref_name() == name)
330 return make_scoped_ptr(MergeDictionaryValues(local_value, server_value));
333 // If this is not a specially handled preference, server wins.
334 return make_scoped_ptr(server_value.DeepCopy());
337 bool PrefModelAssociator::CreatePrefSyncData(
338 const std::string& name,
339 const base::Value& value,
340 syncer::SyncData* sync_data) const {
341 if (value.IsType(base::Value::TYPE_NULL)) {
342 LOG(ERROR) << "Attempting to sync a null pref value for " << name;
343 return false;
346 std::string serialized;
347 // TODO(zea): consider JSONWriter::Write since you don't have to check
348 // failures to deserialize.
349 JSONStringValueSerializer json(&serialized);
350 if (!json.Serialize(value)) {
351 LOG(ERROR) << "Failed to serialize preference value.";
352 return false;
355 sync_pb::EntitySpecifics specifics;
356 sync_pb::PreferenceSpecifics* pref_specifics =
357 GetMutableSpecifics(type_, &specifics);
359 pref_specifics->set_name(name);
360 pref_specifics->set_value(serialized);
361 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
362 return true;
365 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value,
366 const base::Value& to_value) {
367 if (from_value.GetType() == base::Value::TYPE_NULL)
368 return to_value.DeepCopy();
369 if (to_value.GetType() == base::Value::TYPE_NULL)
370 return from_value.DeepCopy();
372 DCHECK(from_value.GetType() == base::Value::TYPE_LIST);
373 DCHECK(to_value.GetType() == base::Value::TYPE_LIST);
374 const base::ListValue& from_list_value =
375 static_cast<const base::ListValue&>(from_value);
376 const base::ListValue& to_list_value =
377 static_cast<const base::ListValue&>(to_value);
378 base::ListValue* result = to_list_value.DeepCopy();
380 for (base::ListValue::const_iterator i = from_list_value.begin();
381 i != from_list_value.end(); ++i) {
382 base::Value* value = (*i)->DeepCopy();
383 result->AppendIfNotPresent(value);
385 return result;
388 base::Value* PrefModelAssociator::MergeDictionaryValues(
389 const base::Value& from_value,
390 const base::Value& to_value) {
391 if (from_value.GetType() == base::Value::TYPE_NULL)
392 return to_value.DeepCopy();
393 if (to_value.GetType() == base::Value::TYPE_NULL)
394 return from_value.DeepCopy();
396 DCHECK_EQ(from_value.GetType(), base::Value::TYPE_DICTIONARY);
397 DCHECK_EQ(to_value.GetType(), base::Value::TYPE_DICTIONARY);
398 const base::DictionaryValue& from_dict_value =
399 static_cast<const base::DictionaryValue&>(from_value);
400 const base::DictionaryValue& to_dict_value =
401 static_cast<const base::DictionaryValue&>(to_value);
402 base::DictionaryValue* result = to_dict_value.DeepCopy();
404 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd();
405 it.Advance()) {
406 const base::Value* from_key_value = &it.value();
407 base::Value* to_key_value;
408 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) {
409 if (from_key_value->GetType() == base::Value::TYPE_DICTIONARY &&
410 to_key_value->GetType() == base::Value::TYPE_DICTIONARY) {
411 base::Value* merged_value =
412 MergeDictionaryValues(*from_key_value, *to_key_value);
413 result->SetWithoutPathExpansion(it.key(), merged_value);
415 // Note that for all other types we want to preserve the "to"
416 // values so we do nothing here.
417 } else {
418 result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy());
421 return result;
424 // static
425 bool PrefModelAssociator::IsMigratedPreference(const char* preference_name) {
426 return !GetOldMigratedPreferenceName(preference_name).empty();
429 // static
430 bool PrefModelAssociator::IsOldMigratedPreference(
431 const char* old_preference_name) {
432 return !GetNewMigratedPreferenceName(old_preference_name).empty();
435 // Note: This will build a model of all preferences registered as syncable
436 // with user controlled data. We do not track any information for preferences
437 // not registered locally as syncable and do not inform the syncer of
438 // non-user controlled preferences.
439 syncer::SyncDataList PrefModelAssociator::GetAllSyncData(
440 syncer::ModelType type)
441 const {
442 DCHECK_EQ(type_, type);
443 syncer::SyncDataList current_data;
444 for (PreferenceSet::const_iterator iter = synced_preferences_.begin();
445 iter != synced_preferences_.end();
446 ++iter) {
447 std::string name = *iter;
448 const PrefService::Preference* pref =
449 pref_service_->FindPreference(name.c_str());
450 DCHECK(pref);
451 if (!pref->IsUserControlled() || pref->IsDefaultValue())
452 continue; // This is not data we care about.
453 // TODO(zea): plumb a way to read the user controlled value.
454 syncer::SyncData sync_data;
455 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data))
456 continue;
457 current_data.push_back(sync_data);
459 return current_data;
462 syncer::SyncError PrefModelAssociator::ProcessSyncChanges(
463 const tracked_objects::Location& from_here,
464 const syncer::SyncChangeList& change_list) {
465 if (!models_associated_) {
466 syncer::SyncError error(FROM_HERE,
467 syncer::SyncError::DATATYPE_ERROR,
468 "Models not yet associated.",
469 PREFERENCES);
470 return error;
472 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
473 syncer::SyncChangeList::const_iterator iter;
474 for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
475 DCHECK_EQ(type_, iter->sync_data().GetDataType());
477 const sync_pb::PreferenceSpecifics& pref_specifics =
478 GetSpecifics(iter->sync_data());
480 std::string name = pref_specifics.name();
481 // It is possible that we may receive a change to a preference we do not
482 // want to sync. For example if the user is syncing a Mac client and a
483 // Windows client, the Windows client does not support
484 // kConfirmToQuitEnabled. Ignore updates from these preferences.
485 const char* pref_name = name.c_str();
486 std::string new_name;
487 // We migrated this preference name, so do as if the name had not changed.
488 if (IsOldMigratedPreference(pref_name)) {
489 new_name = GetNewMigratedPreferenceName(pref_name);
490 pref_name = new_name.c_str();
493 if (!IsPrefRegistered(pref_name))
494 continue;
496 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
497 pref_service_->ClearPref(pref_name);
498 continue;
501 scoped_ptr<base::Value> value(ReadPreferenceSpecifics(pref_specifics));
502 if (!value.get()) {
503 // Skip values we can't deserialize.
504 // TODO(zea): consider taking some further action such as erasing the bad
505 // data.
506 continue;
509 // This will only modify the user controlled value store, which takes
510 // priority over the default value but is ignored if the preference is
511 // policy controlled.
512 pref_service_->Set(pref_name, *value);
514 NotifySyncedPrefObservers(name, true /*from_sync*/);
516 // Keep track of any newly synced preferences.
517 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
518 synced_preferences_.insert(name);
521 return syncer::SyncError();
524 base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
525 const sync_pb::PreferenceSpecifics& preference) {
526 base::JSONReader reader;
527 scoped_ptr<base::Value> value(reader.ReadToValue(preference.value()));
528 if (!value.get()) {
529 std::string err = "Failed to deserialize preference value: " +
530 reader.GetErrorMessage();
531 LOG(ERROR) << err;
532 return NULL;
534 return value.release();
537 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
538 return synced_preferences_.find(name) != synced_preferences_.end();
541 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
542 SyncedPrefObserver* observer) {
543 SyncedPrefObserverList* observers = synced_pref_observers_[name];
544 if (observers == NULL) {
545 observers = new SyncedPrefObserverList;
546 synced_pref_observers_[name] = observers;
548 observers->AddObserver(observer);
551 void PrefModelAssociator::RemoveSyncedPrefObserver(const std::string& name,
552 SyncedPrefObserver* observer) {
553 SyncedPrefObserverMap::iterator observer_iter =
554 synced_pref_observers_.find(name);
555 if (observer_iter == synced_pref_observers_.end())
556 return;
557 SyncedPrefObserverList* observers = observer_iter->second;
558 observers->RemoveObserver(observer);
561 std::set<std::string> PrefModelAssociator::registered_preferences() const {
562 return registered_preferences_;
565 void PrefModelAssociator::RegisterPref(const char* name) {
566 DCHECK(!models_associated_ && registered_preferences_.count(name) == 0);
567 registered_preferences_.insert(name);
570 bool PrefModelAssociator::IsPrefRegistered(const char* name) {
571 return registered_preferences_.count(name) > 0;
574 void PrefModelAssociator::ProcessPrefChange(const std::string& name) {
575 if (processing_syncer_changes_)
576 return; // These are changes originating from us, ignore.
578 // We only process changes if we've already associated models.
579 if (!models_associated_)
580 return;
582 const PrefService::Preference* preference =
583 pref_service_->FindPreference(name.c_str());
584 if (!preference)
585 return;
587 if (!IsPrefRegistered(name.c_str()))
588 return; // We are not syncing this preference.
590 syncer::SyncChangeList changes;
592 if (!preference->IsUserModifiable()) {
593 // If the preference is no longer user modifiable, it must now be controlled
594 // by policy, whose values we do not sync. Just return. If the preference
595 // stops being controlled by policy, it will revert back to the user value
596 // (which we continue to update with sync changes).
597 return;
600 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
602 NotifySyncedPrefObservers(name, false /*from_sync*/);
604 if (synced_preferences_.count(name) == 0) {
605 // Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..)
606 // will determine if we care about its data (e.g. if it has a default value
607 // and hasn't been changed yet we don't) and take care syncing any new data.
608 InitPrefAndAssociate(syncer::SyncData(), name, &changes, NULL);
609 } else {
610 // We are already syncing this preference, just update it's sync node.
611 syncer::SyncData sync_data;
612 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
613 LOG(ERROR) << "Failed to update preference.";
614 return;
616 changes.push_back(
617 syncer::SyncChange(FROM_HERE,
618 syncer::SyncChange::ACTION_UPDATE,
619 sync_data));
620 // This preference has been migrated from an old version that must be kept
621 // in sync on older versions of Chrome.
622 if (IsMigratedPreference(name.c_str())) {
623 std::string old_pref_name = GetOldMigratedPreferenceName(name.c_str());
624 if (!CreatePrefSyncData(old_pref_name,
625 *preference->GetValue(),
626 &sync_data)) {
627 LOG(ERROR) << "Failed to update preference.";
628 return;
631 syncer::SyncChange::SyncChangeType change_type =
632 (synced_preferences_.count(old_pref_name) == 0) ?
633 syncer::SyncChange::ACTION_ADD :
634 syncer::SyncChange::ACTION_UPDATE;
635 changes.push_back(
636 syncer::SyncChange(FROM_HERE, change_type, sync_data));
640 syncer::SyncError error =
641 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
644 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
645 DCHECK(pref_service_ == NULL);
646 pref_service_ = pref_service;
649 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
650 bool from_sync) const {
651 SyncedPrefObserverMap::const_iterator observer_iter =
652 synced_pref_observers_.find(path);
653 if (observer_iter == synced_pref_observers_.end())
654 return;
655 SyncedPrefObserverList* observers = observer_iter->second;
656 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
657 OnSyncedPrefChanged(path, from_sync));