Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / syncable_prefs / pref_model_associator.cc
blob9379a807407ae0750acee4954441934dae100608
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 "components/syncable_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 "components/syncable_prefs/pref_model_associator_client.h"
17 #include "components/syncable_prefs/pref_service_syncable.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_error_factory.h"
20 #include "sync/protocol/preference_specifics.pb.h"
21 #include "sync/protocol/sync.pb.h"
23 using syncer::PREFERENCES;
24 using syncer::PRIORITY_PREFERENCES;
26 namespace syncable_prefs {
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 } // namespace
54 PrefModelAssociator::PrefModelAssociator(
55 const PrefModelAssociatorClient* client,
56 syncer::ModelType type)
57 : models_associated_(false),
58 processing_syncer_changes_(false),
59 pref_service_(NULL),
60 type_(type),
61 client_(client) {
62 DCHECK(CalledOnValidThread());
63 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES);
66 PrefModelAssociator::~PrefModelAssociator() {
67 DCHECK(CalledOnValidThread());
68 pref_service_ = NULL;
70 STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(),
71 synced_pref_observers_.end());
72 synced_pref_observers_.clear();
75 void PrefModelAssociator::InitPrefAndAssociate(
76 const syncer::SyncData& sync_pref,
77 const std::string& pref_name,
78 syncer::SyncChangeList* sync_changes,
79 SyncDataMap* migrated_preference_list) {
80 const base::Value* user_pref_value = pref_service_->GetUserPrefValue(
81 pref_name.c_str());
82 VLOG(1) << "Associating preference " << pref_name;
84 if (sync_pref.IsValid()) {
85 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
86 std::string old_pref_name;
87 DCHECK(pref_name == preference.name() ||
88 (client_ &&
89 client_->IsMigratedPreference(pref_name, &old_pref_name) &&
90 preference.name() == old_pref_name));
91 base::JSONReader reader;
92 scoped_ptr<base::Value> sync_value(reader.ReadToValue(preference.value()));
93 if (!sync_value.get()) {
94 LOG(ERROR) << "Failed to deserialize preference value: "
95 << reader.GetErrorMessage();
96 return;
99 if (user_pref_value) {
100 DVLOG(1) << "Found user pref value for " << pref_name;
101 // We have both server and local values. Merge them.
102 scoped_ptr<base::Value> new_value(
103 MergePreference(pref_name, *user_pref_value, *sync_value));
105 // Update the local preference based on what we got from the
106 // sync server. Note: this only updates the user value store, which is
107 // ignored if the preference is policy controlled.
108 if (new_value->IsType(base::Value::TYPE_NULL)) {
109 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
110 pref_service_->ClearPref(pref_name.c_str());
111 } else if (!new_value->IsType(user_pref_value->GetType())) {
112 LOG(WARNING) << "Synced value for " << preference.name()
113 << " is of type " << new_value->GetType()
114 << " which doesn't match pref type "
115 << user_pref_value->GetType();
116 } else if (!user_pref_value->Equals(new_value.get())) {
117 pref_service_->Set(pref_name.c_str(), *new_value);
120 // If the merge resulted in an updated value, inform the syncer.
121 if (!sync_value->Equals(new_value.get())) {
122 syncer::SyncData sync_data;
123 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) {
124 LOG(ERROR) << "Failed to update preference.";
125 return;
128 std::string old_pref_name;
129 if (client_ &&
130 client_->IsMigratedPreference(pref_name, &old_pref_name)) {
131 // This preference has been migrated from an old version that must be
132 // kept in sync on older versions of Chrome.
133 if (preference.name() == old_pref_name) {
134 DCHECK(migrated_preference_list);
135 // If the name the syncer has is the old pre-migration value, then
136 // it's possible the new migrated preference name hasn't been synced
137 // yet. In that case the SyncChange should be an ACTION_ADD rather
138 // than an ACTION_UPDATE. Defer the decision of whether to sync with
139 // ACTION_ADD or ACTION_UPDATE until the migrated_preferences phase.
140 if (migrated_preference_list)
141 (*migrated_preference_list)[pref_name] = sync_data;
142 } else {
143 DCHECK_EQ(preference.name(), pref_name);
144 sync_changes->push_back(
145 syncer::SyncChange(FROM_HERE,
146 syncer::SyncChange::ACTION_UPDATE,
147 sync_data));
150 syncer::SyncData old_sync_data;
151 if (!CreatePrefSyncData(old_pref_name, *new_value, &old_sync_data)) {
152 LOG(ERROR) << "Failed to update preference.";
153 return;
155 if (migrated_preference_list)
156 (*migrated_preference_list)[old_pref_name] = old_sync_data;
157 } else {
158 sync_changes->push_back(
159 syncer::SyncChange(FROM_HERE,
160 syncer::SyncChange::ACTION_UPDATE,
161 sync_data));
164 } else if (!sync_value->IsType(base::Value::TYPE_NULL)) {
165 // Only a server value exists. Just set the local user value.
166 pref_service_->Set(pref_name.c_str(), *sync_value);
167 } else {
168 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
170 synced_preferences_.insert(preference.name());
171 } else if (user_pref_value) {
172 // The server does not know about this preference and should be added
173 // to the syncer's database.
174 syncer::SyncData sync_data;
175 if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) {
176 LOG(ERROR) << "Failed to update preference.";
177 return;
179 sync_changes->push_back(
180 syncer::SyncChange(FROM_HERE,
181 syncer::SyncChange::ACTION_ADD,
182 sync_data));
183 synced_preferences_.insert(pref_name);
186 // Else this pref does not have a sync value but also does not have a user
187 // controlled value (either it's a default value or it's policy controlled,
188 // either way it's not interesting). We can ignore it. Once it gets changed,
189 // we'll send the new user controlled value to the syncer.
192 syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing(
193 syncer::ModelType type,
194 const syncer::SyncDataList& initial_sync_data,
195 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
196 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
197 DCHECK_EQ(type_, type);
198 DCHECK(CalledOnValidThread());
199 DCHECK(pref_service_);
200 DCHECK(!sync_processor_.get());
201 DCHECK(sync_processor.get());
202 DCHECK(sync_error_factory.get());
203 syncer::SyncMergeResult merge_result(type);
204 sync_processor_ = sync_processor.Pass();
205 sync_error_factory_ = sync_error_factory.Pass();
207 syncer::SyncChangeList new_changes;
208 std::set<std::string> remaining_preferences = registered_preferences_;
210 // Maintains a list of old migrated preference names that we wish to sync.
211 // Keep track of these in a list such that when the preference iteration
212 // loops below are complete we can go back and determine whether
213 SyncDataMap migrated_preference_list;
215 // Go through and check for all preferences we care about that sync already
216 // knows about.
217 for (syncer::SyncDataList::const_iterator sync_iter =
218 initial_sync_data.begin();
219 sync_iter != initial_sync_data.end();
220 ++sync_iter) {
221 DCHECK_EQ(type_, sync_iter->GetDataType());
223 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
224 std::string sync_pref_name = preference.name();
226 if (remaining_preferences.count(sync_pref_name) == 0) {
227 std::string new_pref_name;
228 if (client_ &&
229 client_->IsOldMigratedPreference(sync_pref_name, &new_pref_name)) {
230 // This old pref name is not syncable locally anymore but we accept
231 // changes from other Chrome installs of previous versions and migrate
232 // them to the new name. Note that we will be merging any differences
233 // between the new and old values and sync'ing them back.
234 sync_pref_name = new_pref_name;
235 } else {
236 // We're not syncing this preference locally, ignore the sync data.
237 // TODO(zea): Eventually we want to be able to have the syncable service
238 // reconstruct all sync data for its datatype (therefore having
239 // GetAllSyncData be a complete representation). We should store this
240 // data somewhere, even if we don't use it.
241 continue;
243 } else {
244 remaining_preferences.erase(sync_pref_name);
246 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes,
247 &migrated_preference_list);
250 // Go through and build sync data for any remaining preferences.
251 for (std::set<std::string>::iterator pref_name_iter =
252 remaining_preferences.begin();
253 pref_name_iter != remaining_preferences.end();
254 ++pref_name_iter) {
255 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes,
256 &migrated_preference_list);
259 // Now go over any migrated preference names and build sync data for them too.
260 for (SyncDataMap::const_iterator migrated_pref_iter =
261 migrated_preference_list.begin();
262 migrated_pref_iter != migrated_preference_list.end();
263 ++migrated_pref_iter) {
264 syncer::SyncChange::SyncChangeType change_type =
265 (synced_preferences_.count(migrated_pref_iter->first) == 0) ?
266 syncer::SyncChange::ACTION_ADD :
267 syncer::SyncChange::ACTION_UPDATE;
268 new_changes.push_back(
269 syncer::SyncChange(FROM_HERE, change_type, migrated_pref_iter->second));
270 synced_preferences_.insert(migrated_pref_iter->first);
273 // Push updates to sync.
274 merge_result.set_error(
275 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
276 if (merge_result.error().IsSet())
277 return merge_result;
279 models_associated_ = true;
280 pref_service_->OnIsSyncingChanged();
281 return merge_result;
284 void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
285 DCHECK_EQ(type_, type);
286 models_associated_ = false;
287 sync_processor_.reset();
288 sync_error_factory_.reset();
289 pref_service_->OnIsSyncingChanged();
292 scoped_ptr<base::Value> PrefModelAssociator::MergePreference(
293 const std::string& name,
294 const base::Value& local_value,
295 const base::Value& server_value) {
296 // This function special cases preferences individually, so don't attempt
297 // to merge for all migrated values.
298 if (client_) {
299 std::string new_pref_name;
300 DCHECK(!client_->IsOldMigratedPreference(name, &new_pref_name));
301 if (client_->IsMergeableListPreference(name))
302 return make_scoped_ptr(MergeListValues(local_value, server_value));
303 if (client_->IsMergeableDictionaryPreference(name))
304 return make_scoped_ptr(MergeDictionaryValues(local_value, server_value));
307 // If this is not a specially handled preference, server wins.
308 return make_scoped_ptr(server_value.DeepCopy());
311 bool PrefModelAssociator::CreatePrefSyncData(
312 const std::string& name,
313 const base::Value& value,
314 syncer::SyncData* sync_data) const {
315 if (value.IsType(base::Value::TYPE_NULL)) {
316 LOG(ERROR) << "Attempting to sync a null pref value for " << name;
317 return false;
320 std::string serialized;
321 // TODO(zea): consider JSONWriter::Write since you don't have to check
322 // failures to deserialize.
323 JSONStringValueSerializer json(&serialized);
324 if (!json.Serialize(value)) {
325 LOG(ERROR) << "Failed to serialize preference value.";
326 return false;
329 sync_pb::EntitySpecifics specifics;
330 sync_pb::PreferenceSpecifics* pref_specifics =
331 GetMutableSpecifics(type_, &specifics);
333 pref_specifics->set_name(name);
334 pref_specifics->set_value(serialized);
335 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
336 return true;
339 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value,
340 const base::Value& to_value) {
341 if (from_value.GetType() == base::Value::TYPE_NULL)
342 return to_value.DeepCopy();
343 if (to_value.GetType() == base::Value::TYPE_NULL)
344 return from_value.DeepCopy();
346 DCHECK(from_value.GetType() == base::Value::TYPE_LIST);
347 DCHECK(to_value.GetType() == base::Value::TYPE_LIST);
348 const base::ListValue& from_list_value =
349 static_cast<const base::ListValue&>(from_value);
350 const base::ListValue& to_list_value =
351 static_cast<const base::ListValue&>(to_value);
352 base::ListValue* result = to_list_value.DeepCopy();
354 for (base::ListValue::const_iterator i = from_list_value.begin();
355 i != from_list_value.end(); ++i) {
356 base::Value* value = (*i)->DeepCopy();
357 result->AppendIfNotPresent(value);
359 return result;
362 base::Value* PrefModelAssociator::MergeDictionaryValues(
363 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();
370 DCHECK_EQ(from_value.GetType(), base::Value::TYPE_DICTIONARY);
371 DCHECK_EQ(to_value.GetType(), base::Value::TYPE_DICTIONARY);
372 const base::DictionaryValue& from_dict_value =
373 static_cast<const base::DictionaryValue&>(from_value);
374 const base::DictionaryValue& to_dict_value =
375 static_cast<const base::DictionaryValue&>(to_value);
376 base::DictionaryValue* result = to_dict_value.DeepCopy();
378 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd();
379 it.Advance()) {
380 const base::Value* from_key_value = &it.value();
381 base::Value* to_key_value;
382 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) {
383 if (from_key_value->GetType() == base::Value::TYPE_DICTIONARY &&
384 to_key_value->GetType() == base::Value::TYPE_DICTIONARY) {
385 base::Value* merged_value =
386 MergeDictionaryValues(*from_key_value, *to_key_value);
387 result->SetWithoutPathExpansion(it.key(), merged_value);
389 // Note that for all other types we want to preserve the "to"
390 // values so we do nothing here.
391 } else {
392 result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy());
395 return result;
398 // Note: This will build a model of all preferences registered as syncable
399 // with user controlled data. We do not track any information for preferences
400 // not registered locally as syncable and do not inform the syncer of
401 // non-user controlled preferences.
402 syncer::SyncDataList PrefModelAssociator::GetAllSyncData(
403 syncer::ModelType type)
404 const {
405 DCHECK_EQ(type_, type);
406 syncer::SyncDataList current_data;
407 for (PreferenceSet::const_iterator iter = synced_preferences_.begin();
408 iter != synced_preferences_.end();
409 ++iter) {
410 std::string name = *iter;
411 const PrefService::Preference* pref =
412 pref_service_->FindPreference(name.c_str());
413 DCHECK(pref);
414 if (!pref->IsUserControlled() || pref->IsDefaultValue())
415 continue; // This is not data we care about.
416 // TODO(zea): plumb a way to read the user controlled value.
417 syncer::SyncData sync_data;
418 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data))
419 continue;
420 current_data.push_back(sync_data);
422 return current_data;
425 syncer::SyncError PrefModelAssociator::ProcessSyncChanges(
426 const tracked_objects::Location& from_here,
427 const syncer::SyncChangeList& change_list) {
428 if (!models_associated_) {
429 syncer::SyncError error(FROM_HERE,
430 syncer::SyncError::DATATYPE_ERROR,
431 "Models not yet associated.",
432 PREFERENCES);
433 return error;
435 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
436 syncer::SyncChangeList::const_iterator iter;
437 for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
438 DCHECK_EQ(type_, iter->sync_data().GetDataType());
440 const sync_pb::PreferenceSpecifics& pref_specifics =
441 GetSpecifics(iter->sync_data());
443 std::string name = pref_specifics.name();
444 // It is possible that we may receive a change to a preference we do not
445 // want to sync. For example if the user is syncing a Mac client and a
446 // Windows client, the Windows client does not support
447 // kConfirmToQuitEnabled. Ignore updates from these preferences.
448 std::string pref_name = pref_specifics.name();
449 std::string new_pref_name;
450 // We migrated this preference name, so do as if the name had not changed.
451 if (client_ && client_->IsOldMigratedPreference(name, &new_pref_name)) {
452 pref_name = new_pref_name;
455 if (!IsPrefRegistered(pref_name.c_str()))
456 continue;
458 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
459 pref_service_->ClearPref(pref_name);
460 continue;
463 scoped_ptr<base::Value> value(ReadPreferenceSpecifics(pref_specifics));
464 if (!value.get()) {
465 // Skip values we can't deserialize.
466 // TODO(zea): consider taking some further action such as erasing the bad
467 // data.
468 continue;
471 // This will only modify the user controlled value store, which takes
472 // priority over the default value but is ignored if the preference is
473 // policy controlled.
474 pref_service_->Set(pref_name, *value);
476 NotifySyncedPrefObservers(pref_specifics.name(), true /*from_sync*/);
478 // Keep track of any newly synced preferences.
479 if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
480 synced_preferences_.insert(pref_specifics.name());
483 return syncer::SyncError();
486 base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
487 const sync_pb::PreferenceSpecifics& preference) {
488 base::JSONReader reader;
489 scoped_ptr<base::Value> value(reader.ReadToValue(preference.value()));
490 if (!value.get()) {
491 std::string err = "Failed to deserialize preference value: " +
492 reader.GetErrorMessage();
493 LOG(ERROR) << err;
494 return NULL;
496 return value.release();
499 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
500 return synced_preferences_.find(name) != synced_preferences_.end();
503 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
504 SyncedPrefObserver* observer) {
505 SyncedPrefObserverList* observers = synced_pref_observers_[name];
506 if (observers == NULL) {
507 observers = new SyncedPrefObserverList;
508 synced_pref_observers_[name] = observers;
510 observers->AddObserver(observer);
513 void PrefModelAssociator::RemoveSyncedPrefObserver(const std::string& name,
514 SyncedPrefObserver* observer) {
515 SyncedPrefObserverMap::iterator observer_iter =
516 synced_pref_observers_.find(name);
517 if (observer_iter == synced_pref_observers_.end())
518 return;
519 SyncedPrefObserverList* observers = observer_iter->second;
520 observers->RemoveObserver(observer);
523 void PrefModelAssociator::SetPrefModelAssociatorClientForTesting(
524 const PrefModelAssociatorClient* client) {
525 DCHECK(!client_);
526 client_ = client;
529 std::set<std::string> PrefModelAssociator::registered_preferences() const {
530 return registered_preferences_;
533 void PrefModelAssociator::RegisterPref(const char* name) {
534 DCHECK(!models_associated_ && registered_preferences_.count(name) == 0);
535 registered_preferences_.insert(name);
538 bool PrefModelAssociator::IsPrefRegistered(const char* name) {
539 return registered_preferences_.count(name) > 0;
542 void PrefModelAssociator::ProcessPrefChange(const std::string& name) {
543 if (processing_syncer_changes_)
544 return; // These are changes originating from us, ignore.
546 // We only process changes if we've already associated models.
547 if (!models_associated_)
548 return;
550 const PrefService::Preference* preference =
551 pref_service_->FindPreference(name.c_str());
552 if (!preference)
553 return;
555 if (!IsPrefRegistered(name.c_str()))
556 return; // We are not syncing this preference.
558 syncer::SyncChangeList changes;
560 if (!preference->IsUserModifiable()) {
561 // If the preference is no longer user modifiable, it must now be controlled
562 // by policy, whose values we do not sync. Just return. If the preference
563 // stops being controlled by policy, it will revert back to the user value
564 // (which we continue to update with sync changes).
565 return;
568 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
570 NotifySyncedPrefObservers(name, false /*from_sync*/);
572 if (synced_preferences_.count(name) == 0) {
573 // Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..)
574 // will determine if we care about its data (e.g. if it has a default value
575 // and hasn't been changed yet we don't) and take care syncing any new data.
576 InitPrefAndAssociate(syncer::SyncData(), name, &changes, NULL);
577 } else {
578 // We are already syncing this preference, just update it's sync node.
579 syncer::SyncData sync_data;
580 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
581 LOG(ERROR) << "Failed to update preference.";
582 return;
584 changes.push_back(
585 syncer::SyncChange(FROM_HERE,
586 syncer::SyncChange::ACTION_UPDATE,
587 sync_data));
588 // This preference has been migrated from an old version that must be kept
589 // in sync on older versions of Chrome.
590 std::string old_pref_name;
591 if (client_ && client_->IsMigratedPreference(name, &old_pref_name)) {
592 if (!CreatePrefSyncData(old_pref_name,
593 *preference->GetValue(),
594 &sync_data)) {
595 LOG(ERROR) << "Failed to update preference.";
596 return;
599 syncer::SyncChange::SyncChangeType change_type =
600 (synced_preferences_.count(old_pref_name) == 0) ?
601 syncer::SyncChange::ACTION_ADD :
602 syncer::SyncChange::ACTION_UPDATE;
603 changes.push_back(
604 syncer::SyncChange(FROM_HERE, change_type, sync_data));
608 syncer::SyncError error =
609 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
612 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
613 DCHECK(pref_service_ == NULL);
614 pref_service_ = pref_service;
617 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
618 bool from_sync) const {
619 SyncedPrefObserverMap::const_iterator observer_iter =
620 synced_pref_observers_.find(path);
621 if (observer_iter == synced_pref_observers_.end())
622 return;
623 SyncedPrefObserverList* observers = observer_iter->second;
624 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers,
625 OnSyncedPrefChanged(path, from_sync));
628 } // namespace syncable_prefs