1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync_preferences/pref_model_associator.h"
#include <utility>
#include "base/auto_reset.h"
#include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
#include "components/sync/model/sync_change.h"
#include "components/sync/model/sync_error_factory.h"
#include "components/sync/protocol/preference_specifics.pb.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync_preferences/pref_model_associator_client.h"
#include "components/sync_preferences/pref_service_syncable.h"
using syncer::PREFERENCES;
using syncer::PRIORITY_PREFERENCES;
namespace sync_preferences {
namespace {
const sync_pb::PreferenceSpecifics& GetSpecifics(const syncer::SyncData& pref) {
DCHECK(pref.GetDataType() == syncer::PREFERENCES ||
pref.GetDataType() == syncer::PRIORITY_PREFERENCES);
if (pref.GetDataType() == syncer::PRIORITY_PREFERENCES) {
return pref.GetSpecifics().priority_preference().preference();
} else {
return pref.GetSpecifics().preference();
}
}
sync_pb::PreferenceSpecifics* GetMutableSpecifics(
const syncer::ModelType type,
sync_pb::EntitySpecifics* specifics) {
if (type == syncer::PRIORITY_PREFERENCES) {
DCHECK(!specifics->has_preference());
return specifics->mutable_priority_preference()->mutable_preference();
} else {
DCHECK(!specifics->has_priority_preference());
return specifics->mutable_preference();
}
}
} // namespace
PrefModelAssociator::PrefModelAssociator(
const PrefModelAssociatorClient* client,
syncer::ModelType type)
: models_associated_(false),
processing_syncer_changes_(false),
pref_service_(NULL),
type_(type),
client_(client) {
DCHECK(CalledOnValidThread());
DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES);
}
PrefModelAssociator::~PrefModelAssociator() {
DCHECK(CalledOnValidThread());
pref_service_ = NULL;
synced_pref_observers_.clear();
}
void PrefModelAssociator::InitPrefAndAssociate(
const syncer::SyncData& sync_pref,
const std::string& pref_name,
syncer::SyncChangeList* sync_changes) {
const base::Value* user_pref_value =
pref_service_->GetUserPrefValue(pref_name.c_str());
VLOG(1) << "Associating preference " << pref_name;
if (sync_pref.IsValid()) {
const sync_pb::PreferenceSpecifics& preference = GetSpecifics(sync_pref);
DCHECK(pref_name == preference.name());
base::JSONReader reader;
std::unique_ptr<base::Value> sync_value(
reader.ReadToValue(preference.value()));
if (!sync_value.get()) {
LOG(ERROR) << "Failed to deserialize preference value: "
<< reader.GetErrorMessage();
return;
}
if (user_pref_value) {
DVLOG(1) << "Found user pref value for " << pref_name;
// We have both server and local values. Merge them.
std::unique_ptr<base::Value> new_value(
MergePreference(pref_name, *user_pref_value, *sync_value));
// Update the local preference based on what we got from the
// sync server. Note: this only updates the user value store, which is
// ignored if the preference is policy controlled.
if (new_value->IsType(base::Value::Type::NONE)) {
LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
pref_service_->ClearPref(pref_name.c_str());
} else if (!new_value->IsType(user_pref_value->GetType())) {
LOG(WARNING) << "Synced value for " << preference.name()
<< " is of type " << new_value->GetType()
<< " which doesn't match pref type "
<< user_pref_value->GetType();
} else if (!user_pref_value->Equals(new_value.get())) {
pref_service_->Set(pref_name.c_str(), *new_value);
}
// If the merge resulted in an updated value, inform the syncer.
if (!sync_value->Equals(new_value.get())) {
syncer::SyncData sync_data;
if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) {
LOG(ERROR) << "Failed to update preference.";
return;
}
sync_changes->push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
}
} else if (!sync_value->IsType(base::Value::Type::NONE)) {
// Only a server value exists. Just set the local user value.
pref_service_->Set(pref_name.c_str(), *sync_value);
} else {
LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str();
}
synced_preferences_.insert(preference.name());
} else if (user_pref_value) {
// The server does not know about this preference and should be added
// to the syncer's database.
syncer::SyncData sync_data;
if (!CreatePrefSyncData(pref_name, *user_pref_value, &sync_data)) {
LOG(ERROR) << "Failed to update preference.";
return;
}
sync_changes->push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_ADD, sync_data));
synced_preferences_.insert(pref_name);
}
// Else this pref does not have a sync value but also does not have a user
// controlled value (either it's a default value or it's policy controlled,
// either way it's not interesting). We can ignore it. Once it gets changed,
// we'll send the new user controlled value to the syncer.
}
void PrefModelAssociator::RegisterMergeDataFinishedCallback(
const base::Closure& callback) {
if (!models_associated_)
callback_list_.push_back(callback);
else
callback.Run();
}
syncer::SyncMergeResult PrefModelAssociator::MergeDataAndStartSyncing(
syncer::ModelType type,
const syncer::SyncDataList& initial_sync_data,
std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) {
DCHECK_EQ(type_, type);
DCHECK(CalledOnValidThread());
DCHECK(pref_service_);
DCHECK(!sync_processor_.get());
DCHECK(sync_processor.get());
DCHECK(sync_error_factory.get());
syncer::SyncMergeResult merge_result(type);
sync_processor_ = std::move(sync_processor);
sync_error_factory_ = std::move(sync_error_factory);
syncer::SyncChangeList new_changes;
std::set<std::string> remaining_preferences = registered_preferences_;
// Go through and check for all preferences we care about that sync already
// knows about.
for (syncer::SyncDataList::const_iterator sync_iter =
initial_sync_data.begin();
sync_iter != initial_sync_data.end(); ++sync_iter) {
DCHECK_EQ(type_, sync_iter->GetDataType());
const sync_pb::PreferenceSpecifics& preference = GetSpecifics(*sync_iter);
std::string sync_pref_name = preference.name();
if (remaining_preferences.count(sync_pref_name) == 0) {
// We're not syncing this preference locally, ignore the sync data.
// TODO(zea): Eventually we want to be able to have the syncable service
// reconstruct all sync data for its datatype (therefore having
// GetAllSyncData be a complete representation). We should store this
// data somewhere, even if we don't use it.
continue;
}
remaining_preferences.erase(sync_pref_name);
InitPrefAndAssociate(*sync_iter, sync_pref_name, &new_changes);
}
// Go through and build sync data for any remaining preferences.
for (std::set<std::string>::iterator pref_name_iter =
remaining_preferences.begin();
pref_name_iter != remaining_preferences.end(); ++pref_name_iter) {
InitPrefAndAssociate(syncer::SyncData(), *pref_name_iter, &new_changes);
}
// Push updates to sync.
merge_result.set_error(
sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
if (merge_result.error().IsSet())
return merge_result;
for (const auto& callback : callback_list_)
callback.Run();
callback_list_.clear();
models_associated_ = true;
pref_service_->OnIsSyncingChanged();
return merge_result;
}
void PrefModelAssociator::StopSyncing(syncer::ModelType type) {
DCHECK_EQ(type_, type);
models_associated_ = false;
sync_processor_.reset();
sync_error_factory_.reset();
pref_service_->OnIsSyncingChanged();
}
std::unique_ptr<base::Value> PrefModelAssociator::MergePreference(
const std::string& name,
const base::Value& local_value,
const base::Value& server_value) {
// This function special cases preferences individually, so don't attempt
// to merge for all migrated values.
if (client_) {
std::string new_pref_name;
if (client_->IsMergeableListPreference(name))
return base::WrapUnique(MergeListValues(local_value, server_value));
if (client_->IsMergeableDictionaryPreference(name))
return base::WrapUnique(MergeDictionaryValues(local_value, server_value));
}
// If this is not a specially handled preference, server wins.
return base::WrapUnique(server_value.DeepCopy());
}
bool PrefModelAssociator::CreatePrefSyncData(
const std::string& name,
const base::Value& value,
syncer::SyncData* sync_data) const {
if (value.IsType(base::Value::Type::NONE)) {
LOG(ERROR) << "Attempting to sync a null pref value for " << name;
return false;
}
std::string serialized;
// TODO(zea): consider JSONWriter::Write since you don't have to check
// failures to deserialize.
JSONStringValueSerializer json(&serialized);
if (!json.Serialize(value)) {
LOG(ERROR) << "Failed to serialize preference value.";
return false;
}
sync_pb::EntitySpecifics specifics;
sync_pb::PreferenceSpecifics* pref_specifics =
GetMutableSpecifics(type_, &specifics);
pref_specifics->set_name(name);
pref_specifics->set_value(serialized);
*sync_data = syncer::SyncData::CreateLocalData(name, name, specifics);
return true;
}
base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value,
const base::Value& to_value) {
if (from_value.GetType() == base::Value::Type::NONE)
return to_value.DeepCopy();
if (to_value.GetType() == base::Value::Type::NONE)
return from_value.DeepCopy();
DCHECK(from_value.GetType() == base::Value::Type::LIST);
DCHECK(to_value.GetType() == base::Value::Type::LIST);
const base::ListValue& from_list_value =
static_cast<const base::ListValue&>(from_value);
const base::ListValue& to_list_value =
static_cast<const base::ListValue&>(to_value);
base::ListValue* result = to_list_value.DeepCopy();
for (const auto& value : from_list_value) {
result->AppendIfNotPresent(value->CreateDeepCopy());
}
return result;
}
base::Value* PrefModelAssociator::MergeDictionaryValues(
const base::Value& from_value,
const base::Value& to_value) {
if (from_value.GetType() == base::Value::Type::NONE)
return to_value.DeepCopy();
if (to_value.GetType() == base::Value::Type::NONE)
return from_value.DeepCopy();
DCHECK_EQ(from_value.GetType(), base::Value::Type::DICTIONARY);
DCHECK_EQ(to_value.GetType(), base::Value::Type::DICTIONARY);
const base::DictionaryValue& from_dict_value =
static_cast<const base::DictionaryValue&>(from_value);
const base::DictionaryValue& to_dict_value =
static_cast<const base::DictionaryValue&>(to_value);
base::DictionaryValue* result = to_dict_value.DeepCopy();
for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd();
it.Advance()) {
const base::Value* from_key_value = &it.value();
base::Value* to_key_value;
if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) {
if (from_key_value->GetType() == base::Value::Type::DICTIONARY &&
to_key_value->GetType() == base::Value::Type::DICTIONARY) {
base::Value* merged_value =
MergeDictionaryValues(*from_key_value, *to_key_value);
result->SetWithoutPathExpansion(it.key(), merged_value);
}
// Note that for all other types we want to preserve the "to"
// values so we do nothing here.
} else {
result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy());
}
}
return result;
}
// Note: This will build a model of all preferences registered as syncable
// with user controlled data. We do not track any information for preferences
// not registered locally as syncable and do not inform the syncer of
// non-user controlled preferences.
syncer::SyncDataList PrefModelAssociator::GetAllSyncData(
syncer::ModelType type) const {
DCHECK_EQ(type_, type);
syncer::SyncDataList current_data;
for (PreferenceSet::const_iterator iter = synced_preferences_.begin();
iter != synced_preferences_.end(); ++iter) {
std::string name = *iter;
const PrefService::Preference* pref =
pref_service_->FindPreference(name.c_str());
DCHECK(pref);
if (!pref->IsUserControlled() || pref->IsDefaultValue())
continue; // This is not data we care about.
// TODO(zea): plumb a way to read the user controlled value.
syncer::SyncData sync_data;
if (!CreatePrefSyncData(name, *pref->GetValue(), &sync_data))
continue;
current_data.push_back(sync_data);
}
return current_data;
}
syncer::SyncError PrefModelAssociator::ProcessSyncChanges(
const tracked_objects::Location& from_here,
const syncer::SyncChangeList& change_list) {
if (!models_associated_) {
syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
"Models not yet associated.", PREFERENCES);
return error;
}
base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
syncer::SyncChangeList::const_iterator iter;
for (iter = change_list.begin(); iter != change_list.end(); ++iter) {
DCHECK_EQ(type_, iter->sync_data().GetDataType());
const sync_pb::PreferenceSpecifics& pref_specifics =
GetSpecifics(iter->sync_data());
std::string name = pref_specifics.name();
// It is possible that we may receive a change to a preference we do not
// want to sync. For example if the user is syncing a Mac client and a
// Windows client, the Windows client does not support
// kConfirmToQuitEnabled. Ignore updates from these preferences.
std::string pref_name = pref_specifics.name();
if (!IsPrefRegistered(pref_name.c_str()))
continue;
if (iter->change_type() == syncer::SyncChange::ACTION_DELETE) {
pref_service_->ClearPref(pref_name);
continue;
}
std::unique_ptr<base::Value> value(ReadPreferenceSpecifics(pref_specifics));
if (!value.get()) {
// Skip values we can't deserialize.
// TODO(zea): consider taking some further action such as erasing the bad
// data.
continue;
}
// This will only modify the user controlled value store, which takes
// priority over the default value but is ignored if the preference is
// policy controlled.
pref_service_->Set(pref_name, *value);
NotifySyncedPrefObservers(pref_specifics.name(), true /*from_sync*/);
// Keep track of any newly synced preferences.
if (iter->change_type() == syncer::SyncChange::ACTION_ADD) {
synced_preferences_.insert(pref_specifics.name());
}
}
return syncer::SyncError();
}
base::Value* PrefModelAssociator::ReadPreferenceSpecifics(
const sync_pb::PreferenceSpecifics& preference) {
base::JSONReader reader;
std::unique_ptr<base::Value> value(reader.ReadToValue(preference.value()));
if (!value.get()) {
std::string err =
"Failed to deserialize preference value: " + reader.GetErrorMessage();
LOG(ERROR) << err;
return NULL;
}
return value.release();
}
bool PrefModelAssociator::IsPrefSynced(const std::string& name) const {
return synced_preferences_.find(name) != synced_preferences_.end();
}
void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name,
SyncedPrefObserver* observer) {
std::unique_ptr<SyncedPrefObserverList>& observers =
synced_pref_observers_[name];
if (!observers)
observers = base::MakeUnique<SyncedPrefObserverList>();
observers->AddObserver(observer);
}
void PrefModelAssociator::RemoveSyncedPrefObserver(
const std::string& name,
SyncedPrefObserver* observer) {
auto observer_iter = synced_pref_observers_.find(name);
if (observer_iter == synced_pref_observers_.end())
return;
SyncedPrefObserverList* observers = observer_iter->second.get();
observers->RemoveObserver(observer);
}
void PrefModelAssociator::SetPrefModelAssociatorClientForTesting(
const PrefModelAssociatorClient* client) {
DCHECK(!client_);
client_ = client;
}
std::set<std::string> PrefModelAssociator::registered_preferences() const {
return registered_preferences_;
}
void PrefModelAssociator::RegisterPref(const char* name) {
DCHECK(!models_associated_ && registered_preferences_.count(name) == 0);
registered_preferences_.insert(name);
}
bool PrefModelAssociator::IsPrefRegistered(const char* name) {
return registered_preferences_.count(name) > 0;
}
void PrefModelAssociator::ProcessPrefChange(const std::string& name) {
if (processing_syncer_changes_)
return; // These are changes originating from us, ignore.
// We only process changes if we've already associated models.
if (!models_associated_)
return;
const PrefService::Preference* preference =
pref_service_->FindPreference(name.c_str());
if (!preference)
return;
if (!IsPrefRegistered(name.c_str()))
return; // We are not syncing this preference.
syncer::SyncChangeList changes;
if (!preference->IsUserModifiable()) {
// If the preference is no longer user modifiable, it must now be controlled
// by policy, whose values we do not sync. Just return. If the preference
// stops being controlled by policy, it will revert back to the user value
// (which we continue to update with sync changes).
return;
}
base::AutoReset<bool> processing_changes(&processing_syncer_changes_, true);
NotifySyncedPrefObservers(name, false /*from_sync*/);
if (synced_preferences_.count(name) == 0) {
// Not in synced_preferences_ means no synced data. InitPrefAndAssociate(..)
// will determine if we care about its data (e.g. if it has a default value
// and hasn't been changed yet we don't) and take care syncing any new data.
InitPrefAndAssociate(syncer::SyncData(), name, &changes);
} else {
// We are already syncing this preference, just update it's sync node.
syncer::SyncData sync_data;
if (!CreatePrefSyncData(name, *preference->GetValue(), &sync_data)) {
LOG(ERROR) << "Failed to update preference.";
return;
}
changes.push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
}
syncer::SyncError error =
sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
}
void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) {
DCHECK(pref_service_ == NULL);
pref_service_ = pref_service;
}
void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path,
bool from_sync) const {
auto observer_iter = synced_pref_observers_.find(path);
if (observer_iter == synced_pref_observers_.end())
return;
SyncedPrefObserverList* observers = observer_iter->second.get();
for (auto& observer : *observers)
observer.OnSyncedPrefChanged(path, from_sync);
}
} // namespace sync_preferences
|