Skip to content

Commit a4f9c3f

Browse files
committed
Merge pull request #1532 from brave/sync-revert
Revert "Merge pull request #1188 from brave/sync_frequent_initial_update"
1 parent 4599a61 commit a4f9c3f

File tree

4 files changed

+13
-121
lines changed

4 files changed

+13
-121
lines changed

components/brave_sync/brave_sync_service_impl.cc

+11-24
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ void BraveSyncServiceImpl::BackgroundSyncStarted(bool startup) {
290290
if (startup)
291291
bookmark_change_processor_->Start();
292292

293-
const bool waiting_for_second_device =
294-
!sync_prefs_->GetSyncDevices()->has_second_device();
295-
StartLoop(waiting_for_second_device);
293+
StartLoop();
296294
}
297295

298296
void BraveSyncServiceImpl::BackgroundSyncStopped(bool shutdown) {
@@ -468,10 +466,9 @@ BraveSyncServiceImpl::PrepareResolvedPreferences(const RecordsList& records) {
468466
void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
469467
const std::string this_device_id = sync_prefs_->GetThisDeviceId();
470468
bool this_device_deleted = false;
471-
bool at_least_one_deleted = false;
469+
bool contains_only_one_device = false;
472470

473471
auto sync_devices = sync_prefs_->GetSyncDevices();
474-
const bool waiting_for_second_device = !sync_devices->has_second_device();
475472
for (const auto &record : records) {
476473
DCHECK(record->has_device() || record->has_sitesetting());
477474
if (record->has_device()) {
@@ -487,20 +484,18 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
487484
(record->deviceId == this_device_id &&
488485
record->action == jslib::SyncRecord::Action::A_DELETE &&
489486
actually_merged);
490-
at_least_one_deleted = at_least_one_deleted ||
491-
(record->action == jslib::SyncRecord::Action::A_DELETE &&
492-
actually_merged);
487+
contains_only_one_device = sync_devices->size() < 2 &&
488+
record->action == jslib::SyncRecord::Action::A_DELETE &&
489+
actually_merged;
493490
}
494491
} // for each device
495492

496493
sync_prefs_->SetSyncDevices(*sync_devices);
497-
if (this_device_deleted ||
498-
(at_least_one_deleted && !sync_devices->has_second_device())) {
494+
495+
if (this_device_deleted)
499496
ResetSyncInternal();
500-
} else if (waiting_for_second_device && sync_devices->has_second_device()) {
501-
// Restart loop with 30 sec interval
502-
StartLoop(false);
503-
}
497+
if (contains_only_one_device)
498+
OnResetSync();
504499
}
505500

506501
void BraveSyncServiceImpl::OnSyncPrefsChanged(const std::string& pref) {
@@ -615,14 +610,10 @@ void BraveSyncServiceImpl::SendDeviceSyncRecord(
615610
}
616611

617612
static const int64_t kCheckUpdatesIntervalSec = 60;
618-
static const int64_t kCheckInitialUpdatesIntervalSec = 1;
619613

620-
void BraveSyncServiceImpl::StartLoop(const bool use_initial_update_interval) {
614+
void BraveSyncServiceImpl::StartLoop() {
621615
timer_->Start(FROM_HERE,
622-
base::TimeDelta::FromSeconds(
623-
use_initial_update_interval ?
624-
kCheckInitialUpdatesIntervalSec :
625-
kCheckUpdatesIntervalSec),
616+
base::TimeDelta::FromSeconds(kCheckUpdatesIntervalSec),
626617
this,
627618
&BraveSyncServiceImpl::LoopProc);
628619
}
@@ -649,10 +640,6 @@ void BraveSyncServiceImpl::LoopProcThreadAligned() {
649640
RequestSyncData();
650641
}
651642

652-
base::TimeDelta BraveSyncServiceImpl::GetLoopDelay() const {
653-
return timer_->GetCurrentDelay();
654-
}
655-
656643
void BraveSyncServiceImpl::NotifyLogMessage(const std::string& message) {
657644
DLOG(INFO) << message;
658645
}

components/brave_sync/brave_sync_service_impl.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ FORWARD_DECLARE_TEST(BraveSyncServiceTest, OnSyncReadyNewToSync);
3333
FORWARD_DECLARE_TEST(BraveSyncServiceTest, OnGetExistingObjects);
3434
FORWARD_DECLARE_TEST(BraveSyncServiceTest, BackgroundSyncStarted);
3535
FORWARD_DECLARE_TEST(BraveSyncServiceTest, BackgroundSyncStopped);
36-
FORWARD_DECLARE_TEST(BraveSyncServiceTest, LoopDelayVaries);
3736

3837
class BraveSyncServiceTest;
3938

@@ -111,7 +110,6 @@ class BraveSyncServiceImpl
111110
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, OnGetExistingObjects);
112111
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, BackgroundSyncStarted);
113112
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, BackgroundSyncStopped);
114-
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, LoopDelayVaries);
115113
friend class ::BraveSyncServiceTest;
116114

117115
// SyncMessageHandler overrides
@@ -159,11 +157,10 @@ class BraveSyncServiceImpl
159157
const std::string& deviceId,
160158
const std::string& objectId);
161159

162-
void StartLoop(const bool use_initial_update_interval);
160+
void StartLoop();
163161
void StopLoop();
164162
void LoopProc();
165163
void LoopProcThreadAligned();
166-
base::TimeDelta GetLoopDelay() const; // For tests only
167164

168165
void GetExistingHistoryObjects(
169166
const RecordsList &records,

components/brave_sync/brave_sync_service_unittest.cc

+1-92
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ TEST_F(BraveSyncServiceTest, OnDeleteDeviceWhenSelfDeleted) {
526526
auto resolved_record = SyncRecord::Clone(*records.at(0));
527527
resolved_record->action = jslib::SyncRecord::Action::A_DELETE;
528528
resolved_records.push_back(std::move(resolved_record));
529-
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(AtLeast(3));
529+
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(5);
530530
sync_service()->OnResolvedPreferences(resolved_records);
531531

532532
auto devices_final = sync_service()->sync_prefs_->GetSyncDevices();
@@ -698,94 +698,3 @@ TEST_F(BraveSyncServiceTest, BackgroundSyncStopped) {
698698
sync_service()->BackgroundSyncStopped(false);
699699
EXPECT_FALSE(sync_service()->timer_->IsRunning());
700700
}
701-
702-
TEST_F(BraveSyncServiceTest, LoopDelayVaries) {
703-
// This test is almost the same as BraveSyncServiceTest::OnDeleteDevice
704-
// Loop delay should be:
705-
// 1 sec when there is no sync chain yet
706-
// 60 sec when 2 or more devices
707-
// after sync chain destroy the loop should not be running
708-
709-
sync_service()->BackgroundSyncStarted(false);
710-
711-
EXPECT_TRUE(sync_service()->timer_->IsRunning());
712-
EXPECT_EQ(sync_service()->GetLoopDelay().InSeconds(), 1u);
713-
714-
RecordsList records;
715-
records.push_back(SimpleDeviceRecord(
716-
jslib::SyncRecord::Action::A_CREATE,
717-
"1", "device1"));
718-
records.push_back(SimpleDeviceRecord(
719-
jslib::SyncRecord::Action::A_CREATE,
720-
"2", "device2"));
721-
records.push_back(SimpleDeviceRecord(
722-
jslib::SyncRecord::Action::A_CREATE,
723-
"3", "device3"));
724-
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(1);
725-
sync_service()->OnResolvedPreferences(records);
726-
727-
sync_service()->sync_prefs_->SetThisDeviceId("1");
728-
auto devices = sync_service()->sync_prefs_->GetSyncDevices();
729-
730-
// Have 3 devices now
731-
EXPECT_EQ(sync_service()->GetLoopDelay().InSeconds(), 60u);
732-
733-
EXPECT_TRUE(DevicesContains(devices.get(), "1", "device1"));
734-
EXPECT_TRUE(DevicesContains(devices.get(), "2", "device2"));
735-
EXPECT_TRUE(DevicesContains(devices.get(), "3", "device3"));
736-
737-
using brave_sync::jslib::SyncRecord;
738-
EXPECT_CALL(*sync_client(), SendSyncRecords("PREFERENCES",
739-
ContainsDeviceRecord(SyncRecord::Action::A_DELETE, "device3"))).Times(1);
740-
sync_service()->OnDeleteDevice("3");
741-
742-
RecordsList resolved_records;
743-
auto resolved_record = SyncRecord::Clone(*records.at(2));
744-
resolved_record->action = jslib::SyncRecord::Action::A_DELETE;
745-
resolved_records.push_back(std::move(resolved_record));{
746-
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(1);
747-
sync_service()->OnResolvedPreferences(resolved_records);}
748-
749-
auto devices_final = sync_service()->sync_prefs_->GetSyncDevices();
750-
EXPECT_TRUE(DevicesContains(devices_final.get(), "1", "device1"));
751-
EXPECT_TRUE(DevicesContains(devices_final.get(), "2", "device2"));
752-
EXPECT_FALSE(DevicesContains(devices_final.get(), "3", "device3"));
753-
754-
// Have 2 devices now, still expecting 60 sec delay
755-
EXPECT_EQ(sync_service()->GetLoopDelay().InSeconds(), 60u);
756-
757-
// Delete all remaining devices
758-
resolved_records.clear();
759-
760-
auto resolved_record_0 = SyncRecord::Clone(*records.at(0));
761-
resolved_record_0->action = jslib::SyncRecord::Action::A_DELETE;
762-
resolved_records.push_back(std::move(resolved_record_0));
763-
764-
auto resolved_record_1 = SyncRecord::Clone(*records.at(1));
765-
resolved_record_1->action = jslib::SyncRecord::Action::A_DELETE;
766-
resolved_records.push_back(std::move(resolved_record_1));
767-
768-
{
769-
// Expecting call of OnSyncStateChanged at least 3 times:
770-
// delete "device1"
771-
// delete "device2"
772-
// brave_sync.enabled to false, several times
773-
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(AtLeast(3));
774-
EXPECT_CALL(*sync_client(), OnSyncEnabledChanged).Times(AtLeast(1));
775-
sync_service()->OnResolvedPreferences(resolved_records);
776-
}
777-
778-
devices_final = sync_service()->sync_prefs_->GetSyncDevices();
779-
EXPECT_FALSE(DevicesContains(devices_final.get(), "1", "device1"));
780-
EXPECT_FALSE(DevicesContains(devices_final.get(), "2", "device2"));
781-
EXPECT_FALSE(DevicesContains(devices_final.get(), "3", "device3"));
782-
783-
// We have mock client, so emulate these steps when all devices removed
784-
// 1) brave_sync.enabled goes to false
785-
// 2) BraveSyncClientImpl::OnSyncEnabledChanged
786-
// 3) BraveSyncClientImpl::LoadOrUnloadExtension(false)
787-
// 4) BraveSyncClientImpl::OnExtensionUnloaded
788-
// 5) BraveSyncServiceImpl::BackgroundSyncStopped
789-
sync_service()->BackgroundSyncStopped(true);
790-
EXPECT_FALSE(sync_service()->timer_->IsRunning());
791-
}

components/brave_sync/sync_devices.h

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class SyncDevices {
4343
std::unique_ptr<base::Value> ToValueArrOnly() const;
4444
std::string ToJson() const;
4545
size_t size() const { return devices_.size(); }
46-
bool has_second_device() const { return size() >= 2; }
4746
void FromJson(const std::string &str_json);
4847
void Merge(const SyncDevice& device, int action, bool* actually_merged);
4948

0 commit comments

Comments
 (0)