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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
// 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/send_tab_to_self/send_tab_to_self_bridge.h"
#include <algorithm>
#include <optional>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/observer_list.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/uuid.h"
#include "components/history/core/browser/history_service.h"
#include "components/send_tab_to_self/features.h"
#include "components/send_tab_to_self/metrics_util.h"
#include "components/send_tab_to_self/pref_names.h"
#include "components/send_tab_to_self/proto/send_tab_to_self.pb.h"
#include "components/send_tab_to_self/target_device_info.h"
#include "components/sync/base/deletion_origin.h"
#include "components/sync/model/data_type_local_change_processor.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync_device_info/local_device_info_util.h"
namespace send_tab_to_self {
namespace {
using syncer::DataTypeStore;
const base::TimeDelta kDedupeTime = base::Seconds(5);
const base::TimeDelta kDeviceExpiration = base::Days(10);
// Converts a time field from sync protobufs to a time object.
base::Time ProtoTimeToTime(int64_t proto_t) {
return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(proto_t));
}
// Allocate a EntityData and copies |specifics| into it.
std::unique_ptr<syncer::EntityData> CopyToEntityData(
const sync_pb::SendTabToSelfSpecifics& specifics) {
auto entity_data = std::make_unique<syncer::EntityData>();
*entity_data->specifics.mutable_send_tab_to_self() = specifics;
entity_data->name = specifics.url();
entity_data->creation_time = ProtoTimeToTime(specifics.shared_time_usec());
return entity_data;
}
// Parses the content of |record_list| into |*initial_data|. The output
// parameter is first for binding purposes.
std::optional<syncer::ModelError> ParseLocalEntriesOnBackendSequence(
base::Time now,
std::map<std::string, std::unique_ptr<SendTabToSelfEntry>>* entries,
std::string* local_personalizable_device_name,
std::unique_ptr<DataTypeStore::RecordList> record_list) {
DCHECK(entries);
DCHECK(entries->empty());
DCHECK(local_personalizable_device_name);
DCHECK(record_list);
*local_personalizable_device_name =
syncer::GetPersonalizableDeviceNameBlocking();
for (const syncer::DataTypeStore::Record& r : *record_list) {
auto specifics = std::make_unique<SendTabToSelfLocal>();
if (specifics->ParseFromString(r.value)) {
(*entries)[specifics->specifics().guid()] =
SendTabToSelfEntry::FromLocalProto(*specifics, now);
} else {
return syncer::ModelError(FROM_HERE, "Failed to deserialize specifics.");
}
}
return std::nullopt;
}
} // namespace
SendTabToSelfBridge::SendTabToSelfBridge(
std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
base::Clock* clock,
syncer::OnceDataTypeStoreFactory create_store_callback,
history::HistoryService* history_service,
syncer::DeviceInfoTracker* device_info_tracker,
PrefService* pref_service)
: DataTypeSyncBridge(std::move(change_processor)),
clock_(clock),
history_service_(history_service),
device_info_tracker_(device_info_tracker),
pref_service_(pref_service),
mru_entry_(nullptr) {
DCHECK(clock_);
DCHECK(device_info_tracker_);
if (history_service) {
history_service_observation_.Observe(history_service);
}
device_info_tracker_observation_.Observe(device_info_tracker);
ComputeTargetDeviceInfoSortedList();
std::move(create_store_callback)
.Run(syncer::SEND_TAB_TO_SELF,
base::BindOnce(&SendTabToSelfBridge::OnStoreCreated,
weak_ptr_factory_.GetWeakPtr()));
}
SendTabToSelfBridge::~SendTabToSelfBridge() {
if (history_service_) {
history_service_observation_.Reset();
}
}
std::unique_ptr<syncer::MetadataChangeList>
SendTabToSelfBridge::CreateMetadataChangeList() {
return DataTypeStore::WriteBatch::CreateMetadataChangeList();
}
std::optional<syncer::ModelError> SendTabToSelfBridge::MergeFullSyncData(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList entity_data) {
DCHECK(entries_.empty());
return ApplyIncrementalSyncChanges(std::move(metadata_change_list),
std::move(entity_data));
}
std::optional<syncer::ModelError>
SendTabToSelfBridge::ApplyIncrementalSyncChanges(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList entity_changes) {
std::vector<const SendTabToSelfEntry*> added;
// The opened vector will accumulate both added entries that are already
// opened as well as existing entries that have been updated to be marked as
// opened.
std::vector<const SendTabToSelfEntry*> opened;
std::vector<std::string> removed;
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
for (const std::unique_ptr<syncer::EntityChange>& change : entity_changes) {
const std::string& guid = change->storage_key();
if (change->type() == syncer::EntityChange::ACTION_DELETE) {
if (entries_.find(guid) != entries_.end()) {
EraseEntryInBatch(guid, batch.get());
removed.push_back(change->storage_key());
}
} else {
const sync_pb::SendTabToSelfSpecifics& specifics =
change->data().specifics.send_tab_to_self();
std::unique_ptr<SendTabToSelfEntry> remote_entry =
SendTabToSelfEntry::FromProto(specifics, clock_->Now());
if (!remote_entry) {
continue; // Skip invalid entries.
}
if (remote_entry->IsExpired(clock_->Now())) {
// Remove expired data from server.
change_processor()->Delete(guid, syncer::DeletionOrigin::Unspecified(),
batch->GetMetadataChangeList());
} else {
SendTabToSelfEntry* local_entry =
GetMutableEntryByGUID(remote_entry->GetGUID());
SendTabToSelfLocal remote_entry_pb = remote_entry->AsLocalProto();
if (local_entry == nullptr) {
if (unknown_opened_entries_.contains(remote_entry->GetGUID())) {
unknown_opened_entries_.erase(remote_entry->GetGUID());
remote_entry->MarkOpened();
// Reupload the entry to the server. This operation is safe because
// it is happening at most once per entry.
change_processor()->Put(
remote_entry->GetGUID(),
CopyToEntityData(remote_entry->AsLocalProto().specifics()),
batch->GetMetadataChangeList());
}
// This remote_entry is new. Add it to the model.
added.push_back(remote_entry.get());
if (remote_entry->IsOpened()) {
opened.push_back(remote_entry.get());
}
entries_[remote_entry->GetGUID()] = std::move(remote_entry);
} else {
// Update existing model if entries have been opened.
if (remote_entry->IsOpened() && !local_entry->IsOpened()) {
local_entry->MarkOpened();
opened.push_back(local_entry);
}
}
// Write to the store.
batch->WriteData(guid, remote_entry_pb.SerializeAsString());
}
}
}
batch->TakeMetadataChangesFrom(std::move(metadata_change_list));
Commit(std::move(batch));
NotifyRemoteSendTabToSelfEntryDeleted(removed);
NotifyRemoteSendTabToSelfEntryAdded(added);
NotifyRemoteSendTabToSelfEntryOpened(opened);
return std::nullopt;
}
std::unique_ptr<syncer::DataBatch> SendTabToSelfBridge::GetDataForCommit(
StorageKeyList storage_keys) {
auto batch = std::make_unique<syncer::MutableDataBatch>();
for (const std::string& guid : storage_keys) {
const SendTabToSelfEntry* entry = GetEntryByGUID(guid);
if (!entry) {
continue;
}
batch->Put(guid, CopyToEntityData(entry->AsLocalProto().specifics()));
}
return batch;
}
std::unique_ptr<syncer::DataBatch>
SendTabToSelfBridge::GetAllDataForDebugging() {
auto batch = std::make_unique<syncer::MutableDataBatch>();
for (const auto& it : entries_) {
batch->Put(it.first,
CopyToEntityData(it.second->AsLocalProto().specifics()));
}
return batch;
}
std::string SendTabToSelfBridge::GetClientTag(
const syncer::EntityData& entity_data) const {
return GetStorageKey(entity_data);
}
std::string SendTabToSelfBridge::GetStorageKey(
const syncer::EntityData& entity_data) const {
return entity_data.specifics.send_tab_to_self().guid();
}
bool SendTabToSelfBridge::IsEntityDataValid(
const syncer::EntityData& entity_data) const {
CHECK(entity_data.specifics.has_send_tab_to_self());
sync_pb::SendTabToSelfSpecifics specifics =
entity_data.specifics.send_tab_to_self();
return !specifics.guid().empty() && GURL(specifics.url()).is_valid();
}
void SendTabToSelfBridge::ApplyDisableSyncChanges(
std::unique_ptr<syncer::MetadataChangeList> delete_metadata_change_list) {
DCHECK(store_);
store_->DeleteAllDataAndMetadata(base::DoNothing());
std::vector<std::string> all_guids = GetAllGuids();
entries_.clear();
mru_entry_ = nullptr;
NotifyRemoteSendTabToSelfEntryDeleted(all_guids);
}
std::vector<std::string> SendTabToSelfBridge::GetAllGuids() const {
std::vector<std::string> keys;
for (const auto& it : entries_) {
DCHECK_EQ(it.first, it.second->GetGUID());
keys.push_back(it.first);
}
return keys;
}
const SendTabToSelfEntry* SendTabToSelfBridge::GetEntryByGUID(
const std::string& guid) const {
auto it = entries_.find(guid);
if (it == entries_.end()) {
return nullptr;
}
return it->second.get();
}
const SendTabToSelfEntry* SendTabToSelfBridge::AddEntry(
const GURL& url,
const std::string& title,
const std::string& target_device_cache_guid) {
if (!change_processor()->IsTrackingMetadata()) {
// TODO(crbug.com/40617641) handle failure case.
return nullptr;
}
if (!url.is_valid()) {
return nullptr;
}
// In the case where the user has attempted to send an identical URL to the
// same device within the last |kDedupeTime| we think it is likely that user
// still has the first sent tab in progress, and so we will not attempt to
// resend.
base::Time shared_time = clock_->Now();
if (mru_entry_ && url == mru_entry_->GetURL() &&
target_device_cache_guid == mru_entry_->GetTargetDeviceSyncCacheGuid() &&
shared_time - mru_entry_->GetSharedTime() < kDedupeTime) {
send_tab_to_self::RecordNotificationThrottled();
return mru_entry_;
}
std::string guid = base::Uuid::GenerateRandomV4().AsLowercaseString();
// Assure that we don't have a guid collision.
DCHECK_EQ(GetEntryByGUID(guid), nullptr);
std::string trimmed_title = "";
if (base::IsStringUTF8(title)) {
trimmed_title = base::UTF16ToUTF8(
base::CollapseWhitespace(base::UTF8ToUTF16(title), false));
}
auto entry = std::make_unique<SendTabToSelfEntry>(
guid, url, trimmed_title, shared_time, local_device_name_,
target_device_cache_guid);
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
// This entry is new. Add it to the store and model.
auto entity_data = CopyToEntityData(entry->AsLocalProto().specifics());
change_processor()->Put(guid, std::move(entity_data),
batch->GetMetadataChangeList());
for (SendTabToSelfModelObserver& observer : observers_) {
observer.EntryAddedLocally(entry.get());
}
const SendTabToSelfEntry* result =
entries_.emplace(guid, std::move(entry)).first->second.get();
batch->WriteData(guid, result->AsLocalProto().SerializeAsString());
Commit(std::move(batch));
mru_entry_ = result;
return result;
}
void SendTabToSelfBridge::DeleteEntry(const std::string& guid) {
// Assure that an entry with that guid exists.
if (GetEntryByGUID(guid) == nullptr) {
return;
}
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
DeleteEntryWithBatch(guid, batch.get());
Commit(std::move(batch));
}
void SendTabToSelfBridge::DismissEntry(const std::string& guid) {
SendTabToSelfEntry* entry = GetMutableEntryByGUID(guid);
// Assure that an entry with that guid exists.
if (!entry) {
return;
}
DCHECK(change_processor()->IsTrackingMetadata());
entry->SetNotificationDismissed(true);
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
auto entity_data = CopyToEntityData(entry->AsLocalProto().specifics());
change_processor()->Put(guid, std::move(entity_data),
batch->GetMetadataChangeList());
batch->WriteData(guid, entry->AsLocalProto().SerializeAsString());
Commit(std::move(batch));
}
void SendTabToSelfBridge::MarkEntryOpened(const std::string& guid) {
SendTabToSelfEntry* entry = GetMutableEntryByGUID(guid);
// Assure that an entry with that guid exists.
if (!entry) {
unknown_opened_entries_.insert(guid);
return;
}
DCHECK(change_processor()->IsTrackingMetadata());
entry->MarkOpened();
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
auto entity_data = CopyToEntityData(entry->AsLocalProto().specifics());
change_processor()->Put(guid, std::move(entity_data),
batch->GetMetadataChangeList());
batch->WriteData(guid, entry->AsLocalProto().SerializeAsString());
Commit(std::move(batch));
}
void SendTabToSelfBridge::OnHistoryDeletions(
history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) {
// We only care about actual user (or sync) deletions.
if (!change_processor()->IsTrackingMetadata()) {
return; // Sync processor not yet ready, don't sync.
}
if (deletion_info.is_from_expiration()) {
return;
}
if (!deletion_info.IsAllHistory()) {
std::vector<GURL> urls;
for (const history::URLRow& row : deletion_info.deleted_rows()) {
urls.push_back(row.url());
}
DeleteEntries(urls);
return;
}
// All history was cleared: just delete all entries.
DeleteAllEntries();
}
void SendTabToSelfBridge::OnDeviceInfoChange() {
TRACE_EVENT0("ui", "SendTabToSelfBridge::OnDeviceInfoChange");
ComputeTargetDeviceInfoSortedList();
}
bool SendTabToSelfBridge::IsReady() {
return change_processor()->IsTrackingMetadata();
}
bool SendTabToSelfBridge::HasValidTargetDevice() {
return GetTargetDeviceInfoSortedList().size() > 0;
}
std::vector<TargetDeviceInfo>
SendTabToSelfBridge::GetTargetDeviceInfoSortedList() {
// Filter expired devices (some timestamps in the cached list may now be too
// old). |target_device_info_sorted_list_| is copied here to avoid mutations
// inside a getter.
// TODO(crbug.com/40200734): Consider having a timer that fires on the next
// expiry and removes the corresponding device(s) then.
std::vector<TargetDeviceInfo> non_expired_devices =
target_device_info_sorted_list_;
const base::Time now = clock_->Now();
std::erase_if(non_expired_devices, [now](const TargetDeviceInfo& device) {
return now - device.last_updated_timestamp > kDeviceExpiration;
});
return non_expired_devices;
}
// static
std::unique_ptr<syncer::DataTypeStore>
SendTabToSelfBridge::DestroyAndStealStoreForTest(
std::unique_ptr<SendTabToSelfBridge> bridge) {
return std::move(bridge->store_);
}
void SendTabToSelfBridge::SetLocalDeviceNameForTest(
const std::string& local_device_name) {
local_device_name_ = local_device_name;
}
void SendTabToSelfBridge::NotifyRemoteSendTabToSelfEntryAdded(
const std::vector<const SendTabToSelfEntry*>& new_entries) {
if (new_entries.empty()) {
return;
}
std::vector<const SendTabToSelfEntry*> new_local_entries;
// Only pass along entries that are not dismissed or opened, and are
// targeted at this device, which is determined by comparing the cache guid
// associated with the entry to each device's local list of recently used
// cache_guids
DCHECK(!change_processor()->TrackedCacheGuid().empty());
for (const SendTabToSelfEntry* entry : new_entries) {
if (device_info_tracker_->IsRecentLocalCacheGuid(
entry->GetTargetDeviceSyncCacheGuid()) &&
!entry->GetNotificationDismissed() && !entry->IsOpened()) {
new_local_entries.push_back(entry);
}
}
for (SendTabToSelfModelObserver& observer : observers_) {
observer.EntriesAddedRemotely(new_local_entries);
}
#if BUILDFLAG(IS_IOS)
if (IsSendTabIOSPushNotificationsEnabledWithMagicStackCard() &&
!new_local_entries.empty()) {
pref_service_->SetString(prefs::kIOSSendTabToSelfLastReceivedTabURLPref,
new_local_entries.back()->GetURL().spec());
}
#endif
}
void SendTabToSelfBridge::NotifyRemoteSendTabToSelfEntryDeleted(
const std::vector<std::string>& guids) {
if (guids.empty()) {
return;
}
// TODO(crbug.com/40624520): Only send the entries that targeted this device.
for (SendTabToSelfModelObserver& observer : observers_) {
observer.EntriesRemovedRemotely(guids);
}
}
void SendTabToSelfBridge::NotifyRemoteSendTabToSelfEntryOpened(
const std::vector<const SendTabToSelfEntry*>& opened_entries) {
if (opened_entries.empty()) {
return;
}
for (SendTabToSelfModelObserver& observer : observers_) {
observer.EntriesOpenedRemotely(opened_entries);
}
}
void SendTabToSelfBridge::NotifySendTabToSelfModelLoaded() {
for (SendTabToSelfModelObserver& observer : observers_) {
observer.SendTabToSelfModelLoaded();
}
}
void SendTabToSelfBridge::OnStoreCreated(
const std::optional<syncer::ModelError>& error,
std::unique_ptr<syncer::DataTypeStore> store) {
if (error) {
change_processor()->ReportError(*error);
return;
}
auto initial_entries = std::make_unique<SendTabToSelfEntries>();
SendTabToSelfEntries* initial_entries_copy = initial_entries.get();
auto local_device_name = std::make_unique<std::string>();
std::string* local_device_name_copy = local_device_name.get();
store_ = std::move(store);
store_->ReadAllDataAndPreprocess(
base::BindOnce(&ParseLocalEntriesOnBackendSequence, clock_->Now(),
base::Unretained(initial_entries_copy),
base::Unretained(local_device_name_copy)),
base::BindOnce(&SendTabToSelfBridge::OnReadAllData,
weak_ptr_factory_.GetWeakPtr(), std::move(initial_entries),
std::move(local_device_name)));
}
void SendTabToSelfBridge::OnReadAllData(
std::unique_ptr<SendTabToSelfEntries> initial_entries,
std::unique_ptr<std::string> local_device_name,
const std::optional<syncer::ModelError>& error) {
DCHECK(initial_entries);
DCHECK(local_device_name);
if (error) {
change_processor()->ReportError(*error);
return;
}
entries_ = std::move(*initial_entries);
local_device_name_ = std::move(*local_device_name);
store_->ReadAllMetadata(base::BindOnce(
&SendTabToSelfBridge::OnReadAllMetadata, weak_ptr_factory_.GetWeakPtr()));
}
void SendTabToSelfBridge::OnReadAllMetadata(
const std::optional<syncer::ModelError>& error,
std::unique_ptr<syncer::MetadataBatch> metadata_batch) {
TRACE_EVENT0("ui", "SendTabToSelfBridge::OnReadAllMetadata");
if (error) {
change_processor()->ReportError(*error);
return;
}
change_processor()->ModelReadyToSync(std::move(metadata_batch));
NotifySendTabToSelfModelLoaded();
DoGarbageCollection();
}
void SendTabToSelfBridge::OnCommit(
const std::optional<syncer::ModelError>& error) {
if (error) {
change_processor()->ReportError(*error);
}
}
void SendTabToSelfBridge::Commit(
std::unique_ptr<DataTypeStore::WriteBatch> batch) {
store_->CommitWriteBatch(std::move(batch),
base::BindOnce(&SendTabToSelfBridge::OnCommit,
weak_ptr_factory_.GetWeakPtr()));
}
SendTabToSelfEntry* SendTabToSelfBridge::GetMutableEntryByGUID(
const std::string& guid) const {
auto it = entries_.find(guid);
if (it == entries_.end()) {
return nullptr;
}
return it->second.get();
}
void SendTabToSelfBridge::DoGarbageCollection() {
std::vector<std::string> removed;
auto entry = entries_.begin();
while (entry != entries_.end()) {
DCHECK_EQ(entry->first, entry->second->GetGUID());
std::string guid = entry->first;
bool expired = entry->second->IsExpired(clock_->Now());
entry++;
if (expired) {
DeleteEntry(guid);
removed.push_back(guid);
}
}
NotifyRemoteSendTabToSelfEntryDeleted(removed);
}
void SendTabToSelfBridge::ComputeTargetDeviceInfoSortedList() {
TRACE_EVENT0("ui", "SendTabToSelfBridge::ComputeTargetDeviceInfoSortedList");
if (!device_info_tracker_->IsSyncing()) {
return;
}
std::vector<const syncer::DeviceInfo*> all_devices =
device_info_tracker_->GetAllDeviceInfo();
// Sort the DeviceInfo vector so the most recently modified devices are first.
std::stable_sort(
all_devices.begin(), all_devices.end(),
[](const syncer::DeviceInfo* device1, const syncer::DeviceInfo* device2) {
return device1->last_updated_timestamp() >
device2->last_updated_timestamp();
});
target_device_info_sorted_list_.clear();
std::set<std::string> unique_device_names;
std::unordered_map<std::string, int> short_names_counter;
for (const syncer::DeviceInfo* device : all_devices) {
// If the current device is considered expired for our purposes, stop here
// since the next devices in the vector are at least as expired than this
// one.
if (clock_->Now() - device->last_updated_timestamp() > kDeviceExpiration) {
break;
}
// Don't include this device if it is the local device.
if (device_info_tracker_->IsRecentLocalCacheGuid(device->guid())) {
continue;
}
DCHECK_NE(device->guid(), change_processor()->TrackedCacheGuid());
// Don't include devices that have disabled the send tab to self receiving
// feature.
if (!device->send_tab_to_self_receiving_enabled()) {
continue;
}
SharingDeviceNames device_names = GetSharingDeviceNames(device);
// Don't include this device if it has the same name as the local device.
if (device_names.full_name == local_device_name_) {
continue;
}
// Only keep one device per device name. We only keep the first occurrence
// which is the most recent.
if (unique_device_names.insert(device_names.full_name).second) {
TargetDeviceInfo target_device_info(
device_names.full_name, device_names.short_name, device->guid(),
device->form_factor(), device->last_updated_timestamp());
target_device_info_sorted_list_.push_back(target_device_info);
short_names_counter[device_names.short_name]++;
}
}
for (auto& device_info : target_device_info_sorted_list_) {
bool unique_short_name = short_names_counter[device_info.short_name] == 1;
device_info.device_name =
(unique_short_name ? device_info.short_name : device_info.full_name);
}
}
void SendTabToSelfBridge::DeleteEntryWithBatch(
const std::string& guid,
DataTypeStore::WriteBatch* batch) {
// Assure that an entry with that guid exists.
DCHECK(GetEntryByGUID(guid) != nullptr);
DCHECK(change_processor()->IsTrackingMetadata());
change_processor()->Delete(guid, syncer::DeletionOrigin::Unspecified(),
batch->GetMetadataChangeList());
EraseEntryInBatch(guid, batch);
}
void SendTabToSelfBridge::DeleteEntries(const std::vector<GURL>& urls) {
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
std::vector<std::string> removed_guids;
for (const GURL& url : urls) {
auto entry = entries_.begin();
while (entry != entries_.end()) {
bool to_delete =
(entry->second == nullptr || url == entry->second->GetURL());
std::string guid = entry->first;
entry++;
if (to_delete) {
removed_guids.push_back(guid);
DeleteEntryWithBatch(guid, batch.get());
}
}
}
Commit(std::move(batch));
// To err on the side of completeness this notifies all clients that these
// entries have been removed. Regardless of if these entries were removed
// "remotely".
NotifyRemoteSendTabToSelfEntryDeleted(removed_guids);
}
void SendTabToSelfBridge::DeleteAllEntries() {
if (!change_processor()->IsTrackingMetadata()) {
DCHECK_EQ(0ul, entries_.size());
return;
}
std::unique_ptr<DataTypeStore::WriteBatch> batch = store_->CreateWriteBatch();
std::vector<std::string> all_guids = GetAllGuids();
for (const auto& guid : all_guids) {
change_processor()->Delete(guid, syncer::DeletionOrigin::Unspecified(),
batch->GetMetadataChangeList());
batch->DeleteData(guid);
}
entries_.clear();
mru_entry_ = nullptr;
NotifyRemoteSendTabToSelfEntryDeleted(all_guids);
}
void SendTabToSelfBridge::EraseEntryInBatch(const std::string& guid,
DataTypeStore::WriteBatch* batch) {
if (mru_entry_ && mru_entry_->GetGUID() == guid) {
mru_entry_ = nullptr;
}
entries_.erase(guid);
unknown_opened_entries_.erase(guid);
batch->DeleteData(guid);
}
} // namespace send_tab_to_self
|