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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/model/syncable_service_based_bridge.h"
#include <stdint.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/trace_event.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/base/deletion_origin.h"
#include "components/sync/model/conflict_resolution.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/model/sync_change.h"
#include "components/sync/model/syncable_service.h"
#include "components/sync/protocol/data_type_state_helper.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/persisted_entity_data.pb.h"
#include "components/sync/protocol/proto_memory_estimations.h"
namespace syncer {
namespace {
std::unique_ptr<EntityData> ConvertPersistedToEntityData(
const ClientTagHash& client_tag_hash,
sync_pb::PersistedEntityData data) {
DCHECK(!client_tag_hash.value().empty());
auto entity_data = std::make_unique<EntityData>();
entity_data->name = std::move(*data.mutable_name());
entity_data->specifics = std::move(*data.mutable_specifics());
entity_data->client_tag_hash = client_tag_hash;
// Purposefully crash if we have client only data, as this could result in
// sending password in plain text.
CHECK(!entity_data->specifics.password().has_client_only_encrypted_data());
return entity_data;
}
sync_pb::PersistedEntityData CreatePersistedFromRemoteData(
const EntityData& entity_data) {
sync_pb::PersistedEntityData persisted;
persisted.set_name(entity_data.name);
*persisted.mutable_specifics() = entity_data.specifics;
return persisted;
}
sync_pb::PersistedEntityData CreatePersistedFromLocalData(
const SyncData& sync_data) {
DCHECK(sync_data.IsValid());
DCHECK(!sync_data.GetTitle().empty());
sync_pb::PersistedEntityData persisted;
persisted.set_name(sync_data.GetTitle());
*persisted.mutable_specifics() = sync_data.GetSpecifics();
return persisted;
}
SyncChange::SyncChangeType ConvertToSyncChangeType(
EntityChange::ChangeType type) {
switch (type) {
case EntityChange::ACTION_DELETE:
return SyncChange::ACTION_DELETE;
case EntityChange::ACTION_ADD:
return SyncChange::ACTION_ADD;
case EntityChange::ACTION_UPDATE:
return SyncChange::ACTION_UPDATE;
}
NOTREACHED();
}
// Parses the content of `record_list` into `*in_memory_store`. The output
// parameter is first for binding purposes.
std::optional<ModelError> ParseInMemoryStoreOnBackendSequence(
SyncableServiceBasedBridge::InMemoryStore* in_memory_store,
std::unique_ptr<DataTypeStore::RecordList> record_list) {
DCHECK(in_memory_store);
DCHECK(in_memory_store->empty());
DCHECK(record_list);
for (const DataTypeStore::Record& record : *record_list) {
sync_pb::PersistedEntityData persisted_entity;
if (!persisted_entity.ParseFromString(record.value)) {
return ModelError(FROM_HERE, "Failed deserializing data.");
}
in_memory_store->emplace(record.id, std::move(persisted_entity));
}
return std::nullopt;
}
// Object to propagate local changes to the bridge, which will ultimately
// propagate them to the server.
class LocalChangeProcessor : public SyncChangeProcessor {
public:
LocalChangeProcessor(
DataType type,
const base::RepeatingCallback<void(const std::optional<ModelError>&)>&
error_callback,
DataTypeStore* store,
SyncableServiceBasedBridge::InMemoryStore* in_memory_store,
DataTypeLocalChangeProcessor* other)
: type_(type),
error_callback_(error_callback),
store_(store),
in_memory_store_(in_memory_store),
other_(other) {
DCHECK(store);
DCHECK(other);
}
LocalChangeProcessor(const LocalChangeProcessor&) = delete;
LocalChangeProcessor& operator=(const LocalChangeProcessor&) = delete;
~LocalChangeProcessor() override = default;
std::optional<ModelError> ProcessSyncChanges(
const base::Location& from_here,
const SyncChangeList& change_list) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Reject changes if the processor has already experienced errors.
std::optional<ModelError> processor_error = other_->GetError();
if (processor_error) {
return processor_error;
}
std::unique_ptr<DataTypeStore::WriteBatch> batch =
store_->CreateWriteBatch();
for (const SyncChange& change : change_list) {
switch (change.change_type()) {
case SyncChange::ACTION_ADD:
case SyncChange::ACTION_UPDATE: {
DCHECK_EQ(type_, change.sync_data().GetDataType());
DCHECK(change.sync_data().IsValid())
<< " from " << change.location().ToString();
// Local adds and updates must have a non-unique-title.
DCHECK(!change.sync_data().GetTitle().empty())
<< " from " << change.location().ToString();
const ClientTagHash client_tag_hash =
change.sync_data().GetClientTagHash();
const std::string storage_key = client_tag_hash.value();
DCHECK(!storage_key.empty());
sync_pb::PersistedEntityData persisted_entity =
CreatePersistedFromLocalData(change.sync_data());
(*in_memory_store_)[storage_key] = persisted_entity;
batch->WriteData(storage_key, persisted_entity.SerializeAsString());
other_->Put(storage_key,
ConvertPersistedToEntityData(
/*client_tag_hash=*/client_tag_hash,
std::move(persisted_entity)),
batch->GetMetadataChangeList());
break;
}
case SyncChange::ACTION_DELETE: {
const std::string storage_key =
change.sync_data().GetClientTagHash().value();
DCHECK(!storage_key.empty())
<< " from " << change.location().ToString();
if (IsActOnceDataType(type_)) {
if (other_->IsEntityUnsynced(storage_key)) {
// Ignore the local deletion if the entity hasn't been committed
// yet, similarly to how WriteNode::Drop() does it.
continue;
}
batch->GetMetadataChangeList()->ClearMetadata(storage_key);
other_->UntrackEntityForStorageKey(storage_key);
} else {
other_->Delete(storage_key, DeletionOrigin::Unspecified(),
batch->GetMetadataChangeList());
}
in_memory_store_->erase(storage_key);
batch->DeleteData(storage_key);
break;
}
}
}
store_->CommitWriteBatch(std::move(batch), error_callback_);
return std::nullopt;
}
private:
const DataType type_;
const base::RepeatingCallback<void(const std::optional<ModelError>&)>
error_callback_;
const raw_ptr<DataTypeStore> store_;
const raw_ptr<SyncableServiceBasedBridge::InMemoryStore, DanglingUntriaged>
in_memory_store_;
const raw_ptr<DataTypeLocalChangeProcessor> other_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace
SyncableServiceBasedBridge::SyncableServiceBasedBridge(
DataType type,
OnceDataTypeStoreFactory store_factory,
std::unique_ptr<DataTypeLocalChangeProcessor> change_processor,
SyncableService* syncable_service)
: DataTypeSyncBridge(std::move(change_processor)),
type_(type),
syncable_service_(syncable_service) {
DCHECK(syncable_service_);
init_start_time_ = base::Time::Now();
std::move(store_factory)
.Run(type_, base::BindOnce(&SyncableServiceBasedBridge::OnStoreCreated,
weak_ptr_factory_.GetWeakPtr()));
}
SyncableServiceBasedBridge::~SyncableServiceBasedBridge() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TRACE_EVENT0("sync",
"SyncableServiceBasedBridge::~SyncableServiceBasedBridge");
// Inform the syncable service to make sure instances of LocalChangeProcessor
// are not continued to be used.
if (syncable_service_started_) {
syncable_service_->OnBrowserShutdown(type_);
}
}
std::unique_ptr<MetadataChangeList>
SyncableServiceBasedBridge::CreateMetadataChangeList() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return DataTypeStore::WriteBatch::CreateMetadataChangeList();
}
std::optional<ModelError> SyncableServiceBasedBridge::MergeFullSyncData(
std::unique_ptr<MetadataChangeList> metadata_change_list,
EntityChangeList entity_change_list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(store_);
DCHECK(change_processor()->IsTrackingMetadata());
DCHECK(!syncable_service_started_);
DCHECK(in_memory_store_.empty());
StoreAndConvertRemoteChanges(std::move(metadata_change_list),
std::move(entity_change_list));
syncable_service_->WillStartInitialSync();
// We ignore the output of previous call of StoreAndConvertRemoteChanges() at
// this point and let StartSyncableService() read from `in_memory_store_`,
// which has been updated above as part of StoreAndConvertRemoteChanges().
return StartSyncableService();
}
std::optional<ModelError>
SyncableServiceBasedBridge::ApplyIncrementalSyncChanges(
std::unique_ptr<MetadataChangeList> metadata_change_list,
EntityChangeList entity_change_list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(store_);
DCHECK(change_processor()->IsTrackingMetadata());
DCHECK(syncable_service_started_);
SyncChangeList sync_change_list = StoreAndConvertRemoteChanges(
std::move(metadata_change_list), std::move(entity_change_list));
if (sync_change_list.empty()) {
return std::nullopt;
}
return syncable_service_->ProcessSyncChanges(FROM_HERE, sync_change_list);
}
std::unique_ptr<DataBatch> SyncableServiceBasedBridge::GetDataForCommit(
StorageKeyList storage_keys) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto batch = std::make_unique<MutableDataBatch>();
for (const std::string& storage_key : storage_keys) {
auto it = in_memory_store_.find(storage_key);
if (it != in_memory_store_.end()) {
// Note that client tag hash is used as storage key too.
batch->Put(storage_key,
ConvertPersistedToEntityData(
ClientTagHash::FromHashed(it->first), it->second));
}
}
return batch;
}
std::unique_ptr<DataBatch>
SyncableServiceBasedBridge::GetAllDataForDebugging() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto batch = std::make_unique<MutableDataBatch>();
for (const auto& [storage_key, persisted_entity_data] : in_memory_store_) {
// Note that client tag hash is used as storage key too.
batch->Put(storage_key, ConvertPersistedToEntityData(
ClientTagHash::FromHashed(storage_key),
persisted_entity_data));
}
return batch;
}
std::string SyncableServiceBasedBridge::GetClientTag(
const EntityData& entity_data) const {
// Not supported as per SupportsGetClientTag().
NOTREACHED();
}
std::string SyncableServiceBasedBridge::GetStorageKey(
const EntityData& entity_data) const {
// Not supported as per SupportsGetStorageKey().
NOTREACHED();
}
bool SyncableServiceBasedBridge::IsEntityDataValid(
const EntityData& entity_data) const {
// Implementation is trivial as this bridge is meant to cache locally a copy
// of the server-side data in proto format as-is.
return true;
}
bool SyncableServiceBasedBridge::SupportsGetClientTag() const {
return false;
}
bool SyncableServiceBasedBridge::SupportsGetStorageKey() const {
return false;
}
ConflictResolution SyncableServiceBasedBridge::ResolveConflict(
const std::string& storage_key,
const EntityData& remote_data) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!remote_data.is_deleted()) {
return ConflictResolution::kUseRemote;
}
// Ignore local changes for extensions/apps when server had a delete, to
// avoid unwanted reinstall of an uninstalled extension.
if (type_ == EXTENSIONS || type_ == APPS) {
DVLOG(1) << "Resolving conflict, ignoring local changes for extension/app";
return ConflictResolution::kUseRemote;
}
return ConflictResolution::kUseLocal;
}
void SyncableServiceBasedBridge::ApplyDisableSyncChanges(
std::unique_ptr<MetadataChangeList> delete_metadata_change_list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(store_);
in_memory_store_.clear();
store_->DeleteAllDataAndMetadata(base::DoNothing());
if (syncable_service_started_) {
syncable_service_->StopSyncing(type_);
syncable_service_started_ = false;
}
}
size_t SyncableServiceBasedBridge::EstimateSyncOverheadMemoryUsage() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::trace_event::EstimateMemoryUsage(in_memory_store_);
}
// static
std::unique_ptr<SyncChangeProcessor>
SyncableServiceBasedBridge::CreateLocalChangeProcessorForTesting(
DataType type,
DataTypeStore* store,
InMemoryStore* in_memory_store,
DataTypeLocalChangeProcessor* other) {
return std::make_unique<LocalChangeProcessor>(
type, /*error_callback=*/base::DoNothing(), store, in_memory_store,
other);
}
void SyncableServiceBasedBridge::OnStoreCreated(
const std::optional<ModelError>& error,
std::unique_ptr<DataTypeStore> store) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error) {
change_processor()->ReportError(*error);
return;
}
DCHECK(store);
store_ = std::move(store);
auto in_memory_store = std::make_unique<InMemoryStore>();
InMemoryStore* raw_in_memory_store = in_memory_store.get();
store_->ReadAllDataAndPreprocess(
base::BindOnce(&ParseInMemoryStoreOnBackendSequence,
base::Unretained(raw_in_memory_store)),
base::BindOnce(&SyncableServiceBasedBridge::OnReadAllDataForInit,
weak_ptr_factory_.GetWeakPtr(),
std::move(in_memory_store)));
}
void SyncableServiceBasedBridge::OnReadAllDataForInit(
std::unique_ptr<InMemoryStore> in_memory_store,
const std::optional<ModelError>& error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(in_memory_store.get());
DCHECK(in_memory_store_.empty());
if (error) {
change_processor()->ReportError(*error);
return;
}
in_memory_store_ = std::move(*in_memory_store);
store_->ReadAllMetadata(
base::BindOnce(&SyncableServiceBasedBridge::OnReadAllMetadataForInit,
weak_ptr_factory_.GetWeakPtr()));
}
void SyncableServiceBasedBridge::OnReadAllMetadataForInit(
const std::optional<ModelError>& error,
std::unique_ptr<MetadataBatch> metadata_batch) {
TRACE_EVENT0("sync", "SyncableServiceBasedBridge::OnReadAllMetadataForInit");
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!syncable_service_started_);
if (error) {
change_processor()->ReportError(*error);
return;
}
syncable_service_->WaitUntilReadyToSync(base::BindOnce(
&SyncableServiceBasedBridge::OnSyncableServiceReady,
weak_ptr_factory_.GetWeakPtr(), std::move(metadata_batch)));
}
void SyncableServiceBasedBridge::OnSyncableServiceReady(
std::unique_ptr<MetadataBatch> metadata_batch) {
TRACE_EVENT0("sync", "SyncableServiceBasedBridge::OnSyncableServiceReady");
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!syncable_service_started_);
// Guard against inconsistent state, and recover from it by starting from
// scratch, which will cause the eventual refetching of all entities from the
// server.
if (!IsInitialSyncDone(
metadata_batch->GetDataTypeState().initial_sync_state()) &&
!in_memory_store_.empty()) {
in_memory_store_.clear();
store_->DeleteAllDataAndMetadata(base::DoNothing());
change_processor()->ModelReadyToSync(std::make_unique<MetadataBatch>());
DCHECK(!change_processor()->IsTrackingMetadata());
return;
}
change_processor()->ModelReadyToSync(std::move(metadata_batch));
// If sync was previously enabled according to the loaded metadata, then
// immediately start the SyncableService to track as many local changes as
// possible (regardless of whether sync actually starts or not). Otherwise,
// the SyncableService will be started from MergeFullSyncData().
if (change_processor()->IsTrackingMetadata()) {
if (auto error = StartSyncableService()) {
change_processor()->ReportError(*error);
} else {
// Using the same range as Sync.DataTypeConfigurationTime.* metric.
base::UmaHistogramCustomTimes(
base::StringPrintf("Sync.SyncableServiceStartTime.%s",
DataTypeToHistogramSuffix(type_)),
base::Time::Now() - init_start_time_,
/*min=*/base::Milliseconds(1),
/*max=*/base::Seconds(60), /*buckets=*/50);
}
}
}
std::optional<ModelError> SyncableServiceBasedBridge::StartSyncableService() {
DCHECK(store_);
DCHECK(!syncable_service_started_);
DCHECK(change_processor()->IsTrackingMetadata());
// Sync enabled, so exercise MergeDataAndStartSyncing() immediately, since
// this function is reached only if sync is starting already.
SyncDataList initial_sync_data;
initial_sync_data.reserve(in_memory_store_.size());
for (const auto& [storage_key, persisted_entity_data] : in_memory_store_) {
// Note that client tag hash is used as storage key too.
initial_sync_data.push_back(
SyncData::CreateRemoteData(persisted_entity_data.specifics(),
ClientTagHash::FromHashed(storage_key)));
}
auto error_callback =
base::BindRepeating(&SyncableServiceBasedBridge::ReportErrorIfSet,
weak_ptr_factory_.GetWeakPtr());
auto local_change_processor = std::make_unique<LocalChangeProcessor>(
type_, error_callback, store_.get(), &in_memory_store_,
change_processor());
const std::optional<ModelError> merge_error =
syncable_service_->MergeDataAndStartSyncing(
type_, initial_sync_data, std::move(local_change_processor));
if (!merge_error) {
syncable_service_started_ = true;
}
return merge_error;
}
SyncChangeList SyncableServiceBasedBridge::StoreAndConvertRemoteChanges(
std::unique_ptr<MetadataChangeList> initial_metadata_change_list,
EntityChangeList input_entity_change_list) {
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
batch->TakeMetadataChangesFrom(std::move(initial_metadata_change_list));
SyncChangeList output_sync_change_list;
output_sync_change_list.reserve(input_entity_change_list.size());
for (const std::unique_ptr<EntityChange>& change : input_entity_change_list) {
switch (change->type()) {
case EntityChange::ACTION_DELETE: {
const std::string& storage_key = change->storage_key();
DCHECK_NE(0U, in_memory_store_.count(storage_key));
DVLOG(1) << DataTypeToDebugString(type_)
<< ": Processing deletion with storage key: " << storage_key;
output_sync_change_list.emplace_back(
FROM_HERE, SyncChange::ACTION_DELETE,
SyncData::CreateRemoteData(
in_memory_store_[storage_key].specifics(),
ClientTagHash::FromHashed(storage_key)));
// For tombstones, there is no actual data, which means no client tag
// hash either, but the processor provides the storage key.
DCHECK(!storage_key.empty());
batch->DeleteData(storage_key);
in_memory_store_.erase(storage_key);
break;
}
case EntityChange::ACTION_ADD:
// Because we use the client tag hash as storage key, let the processor
// know.
change_processor()->UpdateStorageKey(
change->data(),
/*storage_key=*/change->data().client_tag_hash.value(),
batch->GetMetadataChangeList());
[[fallthrough]];
case EntityChange::ACTION_UPDATE: {
const std::string& storage_key = change->data().client_tag_hash.value();
DVLOG(1) << DataTypeToDebugString(type_)
<< ": Processing add/update with key: " << storage_key;
output_sync_change_list.emplace_back(
FROM_HERE, ConvertToSyncChangeType(change->type()),
SyncData::CreateRemoteData(change->data().specifics,
change->data().client_tag_hash));
sync_pb::PersistedEntityData persisted_entity_data =
CreatePersistedFromRemoteData(change->data());
batch->WriteData(storage_key,
persisted_entity_data.SerializeAsString());
in_memory_store_[storage_key] = std::move(persisted_entity_data);
break;
}
}
}
store_->CommitWriteBatch(
std::move(batch),
base::BindOnce(&SyncableServiceBasedBridge::ReportErrorIfSet,
weak_ptr_factory_.GetWeakPtr()));
return output_sync_change_list;
}
void SyncableServiceBasedBridge::ReportErrorIfSet(
const std::optional<ModelError>& error) {
if (error) {
change_processor()->ReportError(*error);
}
}
} // namespace syncer
|