Skip to content

Commit

Permalink
Merge pull request #1191 from brave/sync-reset-fix
Browse files Browse the repository at this point in the history
Reset sync right away if devices size <= 1
  • Loading branch information
darkdh authored Dec 28, 2018
2 parents 8fe1d8c + a1fd921 commit 4c5d9d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
19 changes: 13 additions & 6 deletions components/brave_sync/brave_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ void BraveSyncServiceImpl::OnDeleteDevice(const std::string& device_id) {
void BraveSyncServiceImpl::OnResetSync() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

const std::string device_id = sync_prefs_->GetThisDeviceId();

// We have to send delete record and wait for library deleted response then we
// can reset it by ResetInternal()
OnDeleteDevice(device_id);
auto sync_devices = sync_prefs_->GetSyncDevices();
// If there is only one or no devices left, we won't get back resolved sync
// record back.
if (sync_devices->size() <= 1)
ResetSyncInternal();
else {
// We have to send delete record and wait for library deleted response then
// we can reset it by ResetInternal()
const std::string device_id = sync_prefs_->GetThisDeviceId();
OnDeleteDevice(device_id);
}
}

void BraveSyncServiceImpl::GetSettingsAndDevices(
Expand Down Expand Up @@ -479,7 +485,8 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) {
record->action == jslib::SyncRecord::Action::A_DELETE &&
actually_merged);
contains_only_one_device = sync_devices->size() < 2 &&
record->action == jslib::SyncRecord::Action::A_DELETE;
record->action == jslib::SyncRecord::Action::A_DELETE &&
actually_merged;
}
} // for each device

Expand Down
12 changes: 2 additions & 10 deletions components/brave_sync/brave_sync_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -483,21 +483,13 @@ TEST_F(BraveSyncServiceTest, OnDeleteDeviceWhenOneDevice) {
EXPECT_TRUE(DevicesContains(devices.get(), "2", "device2"));

using brave_sync::jslib::SyncRecord;
// device 2 => device 1
EXPECT_CALL(*sync_client(), SendSyncRecords).Times(2);
EXPECT_CALL(*sync_client(), SendSyncRecords).Times(1);
sync_service()->OnDeleteDevice("2");

RecordsList resolved_records;
auto resolved_record = SyncRecord::Clone(*records.at(1));
resolved_record->action = jslib::SyncRecord::Action::A_DELETE;
resolved_records.push_back(std::move(resolved_record));
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(1);
sync_service()->OnResolvedPreferences(resolved_records);

resolved_records.clear();
resolved_record = SyncRecord::Clone(*records.at(0));
resolved_record->action = jslib::SyncRecord::Action::A_DELETE;
resolved_records.push_back(std::move(resolved_record));
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(3);
sync_service()->OnResolvedPreferences(resolved_records);

Expand Down Expand Up @@ -534,7 +526,7 @@ TEST_F(BraveSyncServiceTest, OnDeleteDeviceWhenSelfDeleted) {
auto resolved_record = SyncRecord::Clone(*records.at(0));
resolved_record->action = jslib::SyncRecord::Action::A_DELETE;
resolved_records.push_back(std::move(resolved_record));
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(3);
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(5);
sync_service()->OnResolvedPreferences(resolved_records);

auto devices_final = sync_service()->sync_prefs_->GetSyncDevices();
Expand Down

0 comments on commit 4c5d9d8

Please sign in to comment.