blob: 0fe87465d7562a60468d1745b44654eecf0d5865 [file] [log] [blame]
[email protected]ffbec692012-02-26 20:26:421// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d36f941b2011-05-09 06:19:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
maxbogueea16ff412016-10-28 16:35:295#include "components/sync_preferences/pref_model_associator.h"
[email protected]d36f941b2011-05-09 06:19:166
jdoerrie89aa57212017-05-22 08:36:037#include <algorithm>
8#include <iterator>
Gyuyoung Kim8b084c02018-01-23 13:34:379#include <memory>
dcheng51ace48a2015-12-26 22:45:1710#include <utility>
11
[email protected]d36f941b2011-05-09 06:19:1612#include "base/auto_reset.h"
13#include "base/json/json_reader.h"
[email protected]ffbec692012-02-26 20:26:4214#include "base/json/json_string_value_serializer.h"
[email protected]c62dd9d2011-09-21 18:05:4115#include "base/location.h"
[email protected]d36f941b2011-05-09 06:19:1616#include "base/logging.h"
danakj4d55af59c2016-04-22 22:42:0217#include "base/memory/ptr_util.h"
Tim Schumann13ef1e82018-05-30 01:37:1518#include "base/metrics/histogram_macros.h"
Hwanseung Lee2e9a2532018-06-19 06:22:2819#include "base/stl_util.h"
[email protected]e309f312013-06-07 21:50:0820#include "base/strings/utf_string_conversions.h"
[email protected]d36f941b2011-05-09 06:19:1621#include "base/values.h"
Tim Schumann13ef1e82018-05-30 01:37:1522#include "components/prefs/persistent_pref_store.h"
brettwf00b9b42016-02-01 22:11:3823#include "components/prefs/pref_service.h"
skym71603842016-10-10 18:17:3124#include "components/sync/model/sync_change.h"
25#include "components/sync/model/sync_error_factory.h"
Max Boguefef332d2016-07-28 22:09:0926#include "components/sync/protocol/preference_specifics.pb.h"
27#include "components/sync/protocol/sync.pb.h"
maxbogueea16ff412016-10-28 16:35:2928#include "components/sync_preferences/pref_model_associator_client.h"
29#include "components/sync_preferences/pref_service_syncable.h"
Tim Schumann13ef1e82018-05-30 01:37:1530#include "components/sync_preferences/synced_pref_observer.h"
[email protected]d36f941b2011-05-09 06:19:1631
[email protected]a4a147652012-07-03 23:41:3232using syncer::PREFERENCES;
[email protected]43fe640e2013-03-27 22:18:5533using syncer::PRIORITY_PREFERENCES;
[email protected]d36f941b2011-05-09 06:19:1634
maxbogueea16ff412016-10-28 16:35:2935namespace sync_preferences {
sdefresne50c1e522015-09-18 09:47:5136
[email protected]43fe640e2013-03-27 22:18:5537namespace {
38
39const sync_pb::PreferenceSpecifics& GetSpecifics(const syncer::SyncData& pref) {
40 DCHECK(pref.GetDataType() == syncer::PREFERENCES ||
41 pref.GetDataType() == syncer::PRIORITY_PREFERENCES);
42 if (pref.GetDataType() == syncer::PRIORITY_PREFERENCES) {
43 return pref.GetSpecifics().priority_preference().preference();
44 } else {
45 return pref.GetSpecifics().preference();
46 }
47}
48
49sync_pb::PreferenceSpecifics* GetMutableSpecifics(
50 const syncer::ModelType type,
51 sync_pb::EntitySpecifics* specifics) {
52 if (type == syncer::PRIORITY_PREFERENCES) {
53 DCHECK(!specifics->has_preference());
54 return specifics->mutable_priority_preference()->mutable_preference();
55 } else {
56 DCHECK(!specifics->has_priority_preference());
57 return specifics->mutable_preference();
58 }
59}
60
61} // namespace
62
sdefresne96bc5cc2015-09-15 15:47:5463PrefModelAssociator::PrefModelAssociator(
64 const PrefModelAssociatorClient* client,
Tim Schumann13ef1e82018-05-30 01:37:1565 syncer::ModelType type,
66 UnknownUserPrefAccessor* accessor)
67 : pref_accessor_(accessor), type_(type), client_(client) {
gabfebb8e82017-05-30 08:50:3868 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]43fe640e2013-03-27 22:18:5569 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES);
[email protected]d36f941b2011-05-09 06:19:1670}
71
72PrefModelAssociator::~PrefModelAssociator() {
gabfebb8e82017-05-30 08:50:3873 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Ivan Kotenkov75b1c3a2017-10-24 14:47:2474 pref_service_ = nullptr;
[email protected]0ebc4032013-08-10 07:07:1175
[email protected]0ebc4032013-08-10 07:07:1176 synced_pref_observers_.clear();
[email protected]d36f941b2011-05-09 06:19:1677}
78
[email protected]52c788232011-05-23 22:51:3379void PrefModelAssociator::InitPrefAndAssociate(
[email protected]65f173552012-06-28 22:43:5880 const syncer::SyncData& sync_pref,
[email protected]52c788232011-05-23 22:51:3381 const std::string& pref_name,
sdefresne39dc3a1f2015-12-03 15:06:1782 syncer::SyncChangeList* sync_changes) {
Tim Schumann13ef1e82018-05-30 01:37:1583 UnknownUserPrefAccessor::PreferenceState local_pref_state =
Tim Schumann6787d062018-06-14 14:10:3384 pref_accessor_->GetPreferenceState(type_, pref_name);
Tim Schumann13ef1e82018-05-30 01:37:1585 if (local_pref_state.registration_state ==
86 UnknownUserPrefAccessor::RegistrationState::kUnknown ||
87 local_pref_state.registration_state ==
88 UnknownUserPrefAccessor::RegistrationState::kNotSyncable) {
89 // Only process syncable prefs and unknown prefs if whitelisted.
90 return;
91 }
[email protected]52c788232011-05-23 22:51:3392 VLOG(1) << "Associating preference " << pref_name;
[email protected]d36f941b2011-05-09 06:19:1693
[email protected]52c788232011-05-23 22:51:3394 if (sync_pref.IsValid()) {
[email protected]43fe640e2013-03-27 22:18:5595 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
sdefresne39dc3a1f2015-12-03 15:06:1796 DCHECK(pref_name == preference.name());
[email protected]fa0dde7d2012-11-20 00:52:4397 base::JSONReader reader;
danakj4d55af59c2016-04-22 22:42:0298 std::unique_ptr<base::Value> sync_value(
99 reader.ReadToValue(preference.value()));
[email protected]fa0dde7d2012-11-20 00:52:43100 if (!sync_value.get()) {
[email protected]d36f941b2011-05-09 06:19:16101 LOG(ERROR) << "Failed to deserialize preference value: "
102 << reader.GetErrorMessage();
[email protected]52c788232011-05-23 22:51:33103 return;
[email protected]d36f941b2011-05-09 06:19:16104 }
105
Tim Schumann13ef1e82018-05-30 01:37:15106 if (local_pref_state.persisted_value) {
[email protected]9c255032013-10-29 00:37:33107 DVLOG(1) << "Found user pref value for " << pref_name;
[email protected]fa0dde7d2012-11-20 00:52:43108 // We have both server and local values. Merge them.
Tim Schumann13ef1e82018-05-30 01:37:15109 std::unique_ptr<base::Value> new_value(MergePreference(
110 pref_name, *local_pref_state.persisted_value, *sync_value));
[email protected]d36f941b2011-05-09 06:19:16111
[email protected]fa0dde7d2012-11-20 00:52:43112 // Update the local preference based on what we got from the
113 // sync server. Note: this only updates the user value store, which is
114 // ignored if the preference is policy controlled.
jdoerrief39c2a72017-11-22 10:39:32115 if (new_value->is_none()) {
[email protected]ac1da2f2012-12-13 02:09:57116 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
Tim Schumann13ef1e82018-05-30 01:37:15117 pref_accessor_->ClearPref(pref_name, local_pref_state);
118 } else if (!local_pref_state.persisted_value->Equals(new_value.get())) {
119 pref_accessor_->SetPref(pref_name, local_pref_state, *new_value);
[email protected]52c788232011-05-23 22:51:33120 }
[email protected]fa0dde7d2012-11-20 00:52:43121
122 // If the merge resulted in an updated value, inform the syncer.
123 if (!sync_value->Equals(new_value.get())) {
124 syncer::SyncData sync_data;
125 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) {
126 LOG(ERROR) << "Failed to update preference.";
127 return;
128 }
[email protected]990ebf12013-10-19 10:33:21129
maxbogueea16ff412016-10-28 16:35:29130 sync_changes->push_back(syncer::SyncChange(
131 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
[email protected]fa0dde7d2012-11-20 00:52:43132 }
jdoerrief39c2a72017-11-22 10:39:32133 } else if (!sync_value->is_none()) {
[email protected]fa0dde7d2012-11-20 00:52:43134 // Only a server value exists. Just set the local user value.
Tim Schumann13ef1e82018-05-30 01:37:15135 pref_accessor_->SetPref(pref_name, local_pref_state, *sync_value);
[email protected]ac1da2f2012-12-13 02:09:57136 } else {
137 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
[email protected]d36f941b2011-05-09 06:19:16138 }
[email protected]9c255032013-10-29 00:37:33139 synced_preferences_.insert(preference.name());
Tim Schumann13ef1e82018-05-30 01:37:15140 } else if (local_pref_state.persisted_value) {
141 DCHECK_EQ(local_pref_state.registration_state,
142 UnknownUserPrefAccessor::RegistrationState::kSyncable);
[email protected]52c788232011-05-23 22:51:33143 // The server does not know about this preference and should be added
144 // to the syncer's database.
[email protected]65f173552012-06-28 22:43:58145 syncer::SyncData sync_data;
Tim Schumann13ef1e82018-05-30 01:37:15146 if (!CreatePrefSyncData(pref_name, *local_pref_state.persisted_value,
147 &sync_data)) {
[email protected]52c788232011-05-23 22:51:33148 LOG(ERROR) << "Failed to update preference.";
149 return;
[email protected]d36f941b2011-05-09 06:19:16150 }
maxbogueea16ff412016-10-28 16:35:29151 sync_changes->push_back(syncer::SyncChange(
152 FROM_HERE, syncer::SyncChange::ACTION_ADD, sync_data));
[email protected]9c255032013-10-29 00:37:33153 synced_preferences_.insert(pref_name);
[email protected]d36f941b2011-05-09 06:19:16154 }
155
[email protected]9c255032013-10-29 00:37:33156 // Else this pref does not have a sync value but also does not have a user
157 // controlled value (either it's a default value or it's policy controlled,
158 // either way it's not interesting). We can ignore it. Once it gets changed,
159 // we'll send the new user controlled value to the syncer.
[email protected]d36f941b2011-05-09 06:19:16160}
161
lshang2cf5c572016-07-15 01:36:32162void PrefModelAssociator::RegisterMergeDataFinishedCallback(
163 const base::Closure& callback) {
164 if (!models_associated_)
165 callback_list_.push_back(callback);
166 else
167 callback.Run();
168}
169
[email protected]3f98fd92012-11-17 15:54:29170syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:32171 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:58172 const syncer::SyncDataList& initial_sync_data,
danakj4d55af59c2016-04-22 22:42:02173 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
174 std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]43fe640e2013-03-27 22:18:55175 DCHECK_EQ(type_, type);
gabfebb8e82017-05-30 08:50:38176 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]361d37f62011-11-22 10:37:02177 DCHECK(pref_service_);
[email protected]0677c6952012-03-23 21:59:00178 DCHECK(!sync_processor_.get());
179 DCHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:28180 DCHECK(sync_error_factory.get());
[email protected]3f98fd92012-11-17 15:54:29181 syncer::SyncMergeResult merge_result(type);
dcheng51ace48a2015-12-26 22:45:17182 sync_processor_ = std::move(sync_processor);
183 sync_error_factory_ = std::move(sync_error_factory);
[email protected]d36f941b2011-05-09 06:19:16184
[email protected]65f173552012-06-28 22:43:58185 syncer::SyncChangeList new_changes;
[email protected]52c788232011-05-23 22:51:33186 std::set<std::string> remaining_preferences = registered_preferences_;
187
188 // Go through and check for all preferences we care about that sync already
189 // knows about.
[email protected]65f173552012-06-28 22:43:58190 for (syncer::SyncDataList::const_iterator sync_iter =
[email protected]cb02f612012-06-27 03:15:50191 initial_sync_data.begin();
maxbogueea16ff412016-10-28 16:35:29192 sync_iter != initial_sync_data.end(); ++sync_iter) {
[email protected]43fe640e2013-03-27 22:18:55193 DCHECK_EQ(type_, sync_iter->GetDataType());
194
195 const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
[email protected]990ebf12013-10-19 10:33:21196 std::string sync_pref_name = preference.name();
sdefresne39dc3a1f2015-12-03 15:06:17197 remaining_preferences.erase(sync_pref_name);
198 InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes);
[email protected]d36f941b2011-05-09 06:19:16199 }
200
[email protected]52c788232011-05-23 22:51:33201 // Go through and build sync data for any remaining preferences.
202 for (std::set<std::string>::iterator pref_name_iter =
maxbogueea16ff412016-10-28 16:35:29203 remaining_preferences.begin();
204 pref_name_iter != remaining_preferences.end(); ++pref_name_iter) {
sdefresne39dc3a1f2015-12-03 15:06:17205 InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes);
[email protected]d36f941b2011-05-09 06:19:16206 }
207
Tim Schumann13ef1e82018-05-30 01:37:15208 UMA_HISTOGRAM_COUNTS_1000("Sync.Preferences.SyncingUnknownPrefs",
209 pref_accessor_->GetNumberOfSyncingUnknownPrefs());
210
[email protected]52c788232011-05-23 22:51:33211 // Push updates to sync.
[email protected]3f98fd92012-11-17 15:54:29212 merge_result.set_error(
213 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
214 if (merge_result.error().IsSet())
215 return merge_result;
[email protected]de7d78c72011-07-26 23:41:50216
lshang2cf5c572016-07-15 01:36:32217 for (const auto& callback : callback_list_)
218 callback.Run();
219 callback_list_.clear();
220
[email protected]d36f941b2011-05-09 06:19:16221 models_associated_ = true;
[email protected]33db5322012-10-26 23:14:36222 pref_service_->OnIsSyncingChanged();
[email protected]3f98fd92012-11-17 15:54:29223 return merge_result;
[email protected]d36f941b2011-05-09 06:19:16224}
225
[email protected]a4a147652012-07-03 23:41:32226void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
[email protected]43fe640e2013-03-27 22:18:55227 DCHECK_EQ(type_, type);
[email protected]d36f941b2011-05-09 06:19:16228 models_associated_ = false;
[email protected]0677c6952012-03-23 21:59:00229 sync_processor_.reset();
[email protected]5d36e052012-04-20 20:12:28230 sync_error_factory_.reset();
[email protected]33db5322012-10-26 23:14:36231 pref_service_->OnIsSyncingChanged();
[email protected]d36f941b2011-05-09 06:19:16232}
233
danakj4d55af59c2016-04-22 22:42:02234std::unique_ptr<base::Value> PrefModelAssociator::MergePreference(
[email protected]fa0dde7d2012-11-20 00:52:43235 const std::string& name,
[email protected]cb1078de2013-12-23 20:04:22236 const base::Value& local_value,
237 const base::Value& server_value) {
[email protected]990ebf12013-10-19 10:33:21238 // This function special cases preferences individually, so don't attempt
239 // to merge for all migrated values.
sdefresne96bc5cc2015-09-15 15:47:54240 if (client_) {
241 std::string new_pref_name;
sdefresne96bc5cc2015-09-15 15:47:54242 if (client_->IsMergeableListPreference(name))
jdoerrie89aa57212017-05-22 08:36:03243 return MergeListValues(local_value, server_value);
jdoerriecc9f5732017-08-23 14:12:30244 if (client_->IsMergeableDictionaryPreference(name)) {
Gyuyoung Kim8b084c02018-01-23 13:34:37245 return std::make_unique<base::Value>(
jdoerriecc9f5732017-08-23 14:12:30246 MergeDictionaryValues(local_value, server_value));
247 }
[email protected]d36f941b2011-05-09 06:19:16248 }
249
250 // If this is not a specially handled preference, server wins.
danakj4d55af59c2016-04-22 22:42:02251 return base::WrapUnique(server_value.DeepCopy());
[email protected]d36f941b2011-05-09 06:19:16252}
253
[email protected]52c788232011-05-23 22:51:33254bool PrefModelAssociator::CreatePrefSyncData(
[email protected]d36f941b2011-05-09 06:19:16255 const std::string& name,
[email protected]cb1078de2013-12-23 20:04:22256 const base::Value& value,
[email protected]43fe640e2013-03-27 22:18:55257 syncer::SyncData* sync_data) const {
jdoerrief39c2a72017-11-22 10:39:32258 if (value.is_none()) {
[email protected]ac1da2f2012-12-13 02:09:57259 LOG(ERROR) << "Attempting to sync a null pref value for " << name;
260 return false;
261 }
262
[email protected]d36f941b2011-05-09 06:19:16263 std::string serialized;
[email protected]52c788232011-05-23 22:51:33264 // TODO(zea): consider JSONWriter::Write since you don't have to check
265 // failures to deserialize.
[email protected]d36f941b2011-05-09 06:19:16266 JSONStringValueSerializer json(&serialized);
267 if (!json.Serialize(value)) {
268 LOG(ERROR) << "Failed to serialize preference value.";
269 return false;
270 }
271
[email protected]52c788232011-05-23 22:51:33272 sync_pb::EntitySpecifics specifics;
[email protected]43fe640e2013-03-27 22:18:55273 sync_pb::PreferenceSpecifics* pref_specifics =
274 GetMutableSpecifics(type_, &specifics);
275
[email protected]52c788232011-05-23 22:51:33276 pref_specifics->set_name(name);
277 pref_specifics->set_value(serialized);
[email protected]65f173552012-06-28 22:43:58278 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
[email protected]d36f941b2011-05-09 06:19:16279 return true;
280}
281
jdoerrie89aa57212017-05-22 08:36:03282std::unique_ptr<base::Value> PrefModelAssociator::MergeListValues(
283 const base::Value& from_value,
284 const base::Value& to_value) {
jdoerrief39c2a72017-11-22 10:39:32285 if (from_value.is_none())
Lei Zhang4a9c3562017-10-26 19:46:44286 return base::Value::ToUniquePtrValue(to_value.Clone());
jdoerrief39c2a72017-11-22 10:39:32287 if (to_value.is_none())
Lei Zhang4a9c3562017-10-26 19:46:44288 return base::Value::ToUniquePtrValue(from_value.Clone());
[email protected]d36f941b2011-05-09 06:19:16289
jdoerrie76cee9c2017-10-06 22:42:42290 DCHECK(from_value.type() == base::Value::Type::LIST);
291 DCHECK(to_value.type() == base::Value::Type::LIST);
[email protected]d36f941b2011-05-09 06:19:16292
jdoerriecc9f5732017-08-23 14:12:30293 base::Value result = to_value.Clone();
294 base::Value::ListStorage& list = result.GetList();
295 for (const auto& value : from_value.GetList()) {
Hwanseung Lee2e9a2532018-06-19 06:22:28296 if (!base::ContainsValue(list, value))
jdoerriecc9f5732017-08-23 14:12:30297 list.emplace_back(value.Clone());
298 }
299
Lei Zhang4a9c3562017-10-26 19:46:44300 return base::Value::ToUniquePtrValue(std::move(result));
[email protected]d36f941b2011-05-09 06:19:16301}
302
jdoerriecc9f5732017-08-23 14:12:30303base::Value PrefModelAssociator::MergeDictionaryValues(
[email protected]cb1078de2013-12-23 20:04:22304 const base::Value& from_value,
305 const base::Value& to_value) {
jdoerriecc9f5732017-08-23 14:12:30306 if (from_value.is_none())
307 return to_value.Clone();
308 if (to_value.is_none())
309 return from_value.Clone();
[email protected]d36f941b2011-05-09 06:19:16310
jdoerriecc9f5732017-08-23 14:12:30311 DCHECK(from_value.is_dict());
312 DCHECK(to_value.is_dict());
313 base::Value result = to_value.Clone();
[email protected]d36f941b2011-05-09 06:19:16314
jdoerriecc9f5732017-08-23 14:12:30315 for (const auto& it : from_value.DictItems()) {
316 const base::Value* from_key_value = &it.second;
317 base::Value* to_key_value = result.FindKey(it.first);
318 if (to_key_value) {
319 if (from_key_value->is_dict() && to_key_value->is_dict()) {
320 *to_key_value = MergeDictionaryValues(*from_key_value, *to_key_value);
[email protected]d36f941b2011-05-09 06:19:16321 }
322 // Note that for all other types we want to preserve the "to"
323 // values so we do nothing here.
324 } else {
jdoerriecc9f5732017-08-23 14:12:30325 result.SetKey(it.first, from_key_value->Clone());
[email protected]d36f941b2011-05-09 06:19:16326 }
327 }
jdoerriecc9f5732017-08-23 14:12:30328 return result;
[email protected]d36f941b2011-05-09 06:19:16329}
330
[email protected]65f173552012-06-28 22:43:58331syncer::SyncDataList PrefModelAssociator::GetAllSyncData(
maxbogueea16ff412016-10-28 16:35:29332 syncer::ModelType type) const {
[email protected]43fe640e2013-03-27 22:18:55333 DCHECK_EQ(type_, type);
[email protected]65f173552012-06-28 22:43:58334 syncer::SyncDataList current_data;
[email protected]52c788232011-05-23 22:51:33335 for (PreferenceSet::const_iterator iter = synced_preferences_.begin();
maxbogueea16ff412016-10-28 16:35:29336 iter != synced_preferences_.end(); ++iter) {
[email protected]52c788232011-05-23 22:51:33337 std::string name = *iter;
Tim Schumann6787d062018-06-14 14:10:33338 if (pref_accessor_->GetPreferenceState(type_, name).registration_state !=
Tim Schumann13ef1e82018-05-30 01:37:15339 UnknownUserPrefAccessor::RegistrationState::kSyncable) {
340 continue;
341 }
iceman4179f17c2017-03-29 19:50:09342 const PrefService::Preference* pref = pref_service_->FindPreference(name);
[email protected]7ff7c322011-06-22 20:28:18343 DCHECK(pref);
[email protected]7640df12011-06-20 21:51:46344 if (!pref->IsUserControlled() || pref->IsDefaultValue())
345 continue; // This is not data we care about.
346 // TODO(zea): plumb a way to read the user controlled value.
[email protected]65f173552012-06-28 22:43:58347 syncer::SyncData sync_data;
[email protected]52c788232011-05-23 22:51:33348 if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data))
349 continue;
350 current_data.push_back(sync_data);
351 }
[email protected]52c788232011-05-23 22:51:33352 return current_data;
[email protected]d36f941b2011-05-09 06:19:16353}
354
[email protected]65f173552012-06-28 22:43:58355syncer::SyncError PrefModelAssociator::ProcessSyncChanges(
Brett Wilson1c990022017-09-12 20:11:15356 const base::Location& from_here,
[email protected]65f173552012-06-28 22:43:58357 const syncer::SyncChangeList& change_list) {
[email protected]de7d78c72011-07-26 23:41:50358 if (!models_associated_) {
maxbogueea16ff412016-10-28 16:35:29359 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
360 "Models not yet associated.", PREFERENCES);
[email protected]de7d78c72011-07-26 23:41:50361 return error;
362 }
[email protected]997ec9f2012-11-21 04:44:14363 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
[email protected]65f173552012-06-28 22:43:58364 syncer::SyncChangeList::const_iterator iter;
[email protected]52c788232011-05-23 22:51:33365 for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
[email protected]43fe640e2013-03-27 22:18:55366 DCHECK_EQ(type_, iter->sync_data().GetDataType());
[email protected]d36f941b2011-05-09 06:19:16367
[email protected]43fe640e2013-03-27 22:18:55368 const sync_pb::PreferenceSpecifics& pref_specifics =
369 GetSpecifics(iter->sync_data());
370
Tim Schumann13ef1e82018-05-30 01:37:15371 UnknownUserPrefAccessor::PreferenceState local_pref_state =
Tim Schumann6787d062018-06-14 14:10:33372 pref_accessor_->GetPreferenceState(type_, pref_specifics.name());
Tim Schumann13ef1e82018-05-30 01:37:15373 if (local_pref_state.registration_state ==
374 UnknownUserPrefAccessor::RegistrationState::kUnknown) {
375 // It is possible that we may receive a change to a preference we do not
376 // want to sync. For example if the user is syncing a Mac client and a
377 // Windows client, the Windows client does not support
378 // kConfirmToQuitEnabled. Ignore updates from these preferences.
379 // We only sync such prefs if they are whitelisted.
[email protected]d36f941b2011-05-09 06:19:16380 continue;
Tim Schumann13ef1e82018-05-30 01:37:15381 }
382 if (local_pref_state.registration_state ==
383 UnknownUserPrefAccessor::RegistrationState::kNotSyncable) {
384 // Don't process remote changes for prefs this client doesn't want synced.
385 continue;
386 }
[email protected]c3f5a252014-08-12 19:28:26387 if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
Tim Schumann13ef1e82018-05-30 01:37:15388 pref_accessor_->ClearPref(pref_specifics.name(), local_pref_state);
[email protected]c3f5a252014-08-12 19:28:26389 continue;
390 }
391
Tim Schumann13ef1e82018-05-30 01:37:15392 std::unique_ptr<base::Value> new_value(
393 ReadPreferenceSpecifics(pref_specifics));
394 if (!new_value.get()) {
[email protected]c3f5a252014-08-12 19:28:26395 // Skip values we can't deserialize.
Tim Schumann13ef1e82018-05-30 01:37:15396 // TODO(zea): consider taking some further action such as erasing the
397 // bad data.
[email protected]c3f5a252014-08-12 19:28:26398 continue;
399 }
[email protected]d36f941b2011-05-09 06:19:16400
[email protected]7640df12011-06-20 21:51:46401 // This will only modify the user controlled value store, which takes
402 // priority over the default value but is ignored if the preference is
403 // policy controlled.
Tim Schumann13ef1e82018-05-30 01:37:15404 pref_accessor_->SetPref(pref_specifics.name(), local_pref_state,
405 *new_value);
[email protected]d36f941b2011-05-09 06:19:16406
sdefresne96bc5cc2015-09-15 15:47:54407 NotifySyncedPrefObservers(pref_specifics.name(), true /*from_sync*/);
[email protected]0ebc4032013-08-10 07:07:11408
Tim Schumann13ef1e82018-05-30 01:37:15409 // Keep track of any newly synced preferences. This can happen if a
410 // preference was late registered or remotely added (ACTION_ADD).
411 synced_preferences_.insert(pref_specifics.name());
[email protected]d36f941b2011-05-09 06:19:16412 }
[email protected]65f173552012-06-28 22:43:58413 return syncer::SyncError();
[email protected]d36f941b2011-05-09 06:19:16414}
415
Tim Schumann978e7502018-05-07 13:46:52416// static
[email protected]cb1078de2013-12-23 20:04:22417base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
[email protected]c3f5a252014-08-12 19:28:26418 const sync_pb::PreferenceSpecifics& preference) {
[email protected]d36f941b2011-05-09 06:19:16419 base::JSONReader reader;
danakj4d55af59c2016-04-22 22:42:02420 std::unique_ptr<base::Value> value(reader.ReadToValue(preference.value()));
[email protected]d36f941b2011-05-09 06:19:16421 if (!value.get()) {
maxbogueea16ff412016-10-28 16:35:29422 std::string err =
423 "Failed to deserialize preference value: " + reader.GetErrorMessage();
[email protected]d36f941b2011-05-09 06:19:16424 LOG(ERROR) << err;
Ivan Kotenkov75b1c3a2017-10-24 14:47:24425 return nullptr;
[email protected]d36f941b2011-05-09 06:19:16426 }
[email protected]d36f941b2011-05-09 06:19:16427 return value.release();
428}
429
[email protected]f3ca9092013-05-26 02:25:15430bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
431 return synced_preferences_.find(name) != synced_preferences_.end();
432}
433
[email protected]0ebc4032013-08-10 07:07:11434void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
maxbogueea16ff412016-10-28 16:35:29435 SyncedPrefObserver* observer) {
Tim Schumann978e7502018-05-07 13:46:52436 std::unique_ptr<base::ObserverList<SyncedPrefObserver>>& observers =
avi82554752016-09-23 17:48:50437 synced_pref_observers_[name];
438 if (!observers)
Tim Schumann978e7502018-05-07 13:46:52439 observers = std::make_unique<base::ObserverList<SyncedPrefObserver>>();
avi82554752016-09-23 17:48:50440
[email protected]0ebc4032013-08-10 07:07:11441 observers->AddObserver(observer);
442}
443
maxbogueea16ff412016-10-28 16:35:29444void PrefModelAssociator::RemoveSyncedPrefObserver(
445 const std::string& name,
[email protected]0ebc4032013-08-10 07:07:11446 SyncedPrefObserver* observer) {
avi82554752016-09-23 17:48:50447 auto observer_iter = synced_pref_observers_.find(name);
[email protected]0ebc4032013-08-10 07:07:11448 if (observer_iter == synced_pref_observers_.end())
449 return;
Tim Schumann978e7502018-05-07 13:46:52450 observer_iter->second->RemoveObserver(observer);
[email protected]52c788232011-05-23 22:51:33451}
[email protected]d36f941b2011-05-09 06:19:16452
Tim Schumann13ef1e82018-05-30 01:37:15453void PrefModelAssociator::RegisterPref(const std::string& name) {
454 DCHECK(!registered_preferences_.count(name));
[email protected]52c788232011-05-23 22:51:33455 registered_preferences_.insert(name);
Tim Schumann13ef1e82018-05-30 01:37:15456
457 // This pref might be registered after sync started. Make sure data in the
458 // local store matches the registered type.
459 // If this results in a modification of the local pref store, we don't want
460 // to tell ChromeSync about these -- it's a local anomaly,
461 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
462 pref_accessor_->EnforceRegisteredTypeInStore(name);
[email protected]d36f941b2011-05-09 06:19:16463}
464
Tim Schumann978e7502018-05-07 13:46:52465bool PrefModelAssociator::IsPrefRegistered(const std::string& name) const {
[email protected]52c788232011-05-23 22:51:33466 return registered_preferences_.count(name) > 0;
[email protected]d36f941b2011-05-09 06:19:16467}
468
469void PrefModelAssociator::ProcessPrefChange(const std::string& name) {
470 if (processing_syncer_changes_)
471 return; // These are changes originating from us, ignore.
472
473 // We only process changes if we've already associated models.
Tim Schumann13ef1e82018-05-30 01:37:15474 // This also filters out local changes during the initial merge.
[email protected]52c788232011-05-23 22:51:33475 if (!models_associated_)
[email protected]d36f941b2011-05-09 06:19:16476 return;
477
Tim Schumann13ef1e82018-05-30 01:37:15478 // From now on, this method does not have to deal with lazily registered
479 // prefs, as local changes can only happen after they were registered.
480
[email protected]d36f941b2011-05-09 06:19:16481 const PrefService::Preference* preference =
iceman4179f17c2017-03-29 19:50:09482 pref_service_->FindPreference(name);
Tim Schumann13ef1e82018-05-30 01:37:15483 // TODO(tschumann): When can this ever happen? Should this be a DCHECK?
[email protected]7ff7c322011-06-22 20:28:18484 if (!preference)
485 return;
486
Tim Schumann13ef1e82018-05-30 01:37:15487 if (!IsPrefRegistered(name)) {
488 // We are not syncing this preference -- this also filters out synced
489 // preferences of the wrong type (priority preference are handled by a
490 // separate associator).
491 return;
492 }
[email protected]d36f941b2011-05-09 06:19:16493
[email protected]65f173552012-06-28 22:43:58494 syncer::SyncChangeList changes;
[email protected]d36f941b2011-05-09 06:19:16495
[email protected]d36f941b2011-05-09 06:19:16496 if (!preference->IsUserModifiable()) {
Tim Schumann13ef1e82018-05-30 01:37:15497 // If the preference is no longer user modifiable, it must now be
498 // controlled by policy, whose values we do not sync. Just return. If the
499 // preference stops being controlled by policy, it will revert back to the
500 // user value (which we continue to update with sync changes).
[email protected]d36f941b2011-05-09 06:19:16501 return;
502 }
503
[email protected]997ec9f2012-11-21 04:44:14504 base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
[email protected]d36f941b2011-05-09 06:19:16505
[email protected]0ebc4032013-08-10 07:07:11506 NotifySyncedPrefObservers(name, false /*from_sync*/);
507
[email protected]52c788232011-05-23 22:51:33508 if (synced_preferences_.count(name) == 0) {
Tim Schumann13ef1e82018-05-30 01:37:15509 // Not in synced_preferences_ means no synced data.
510 // InitPrefAndAssociate(..) will determine if we care about its data (e.g.
511 // if it has a default value and hasn't been changed yet we don't) and
512 // take care syncing any new data.
sdefresne39dc3a1f2015-12-03 15:06:17513 InitPrefAndAssociate(syncer::SyncData(), name, &changes);
[email protected]d36f941b2011-05-09 06:19:16514 } else {
[email protected]7640df12011-06-20 21:51:46515 // We are already syncing this preference, just update it's sync node.
[email protected]65f173552012-06-28 22:43:58516 syncer::SyncData sync_data;
[email protected]52c788232011-05-23 22:51:33517 if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
518 LOG(ERROR) << "Failed to update preference.";
[email protected]d36f941b2011-05-09 06:19:16519 return;
520 }
maxbogueea16ff412016-10-28 16:35:29521 changes.push_back(syncer::SyncChange(
522 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
[email protected]d36f941b2011-05-09 06:19:16523 }
[email protected]de7d78c72011-07-26 23:41:50524
[email protected]65f173552012-06-28 22:43:58525 syncer::SyncError error =
[email protected]de7d78c72011-07-26 23:41:50526 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
[email protected]d36f941b2011-05-09 06:19:16527}
[email protected]361d37f62011-11-22 10:37:02528
[email protected]5b199522012-12-22 17:24:44529void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
Ivan Kotenkov75b1c3a2017-10-24 14:47:24530 DCHECK(pref_service_ == nullptr);
[email protected]361d37f62011-11-22 10:37:02531 pref_service_ = pref_service;
532}
[email protected]0ebc4032013-08-10 07:07:11533
534void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
535 bool from_sync) const {
avi82554752016-09-23 17:48:50536 auto observer_iter = synced_pref_observers_.find(path);
[email protected]0ebc4032013-08-10 07:07:11537 if (observer_iter == synced_pref_observers_.end())
538 return;
Tim Schumann978e7502018-05-07 13:46:52539 for (auto& observer : *observer_iter->second)
ericwilligersff2af332016-10-19 00:16:50540 observer.OnSyncedPrefChanged(path, from_sync);
[email protected]0ebc4032013-08-10 07:07:11541}
sdefresne50c1e522015-09-18 09:47:51542
maxbogueea16ff412016-10-28 16:35:29543} // namespace sync_preferences