Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync reset reenable #3038

Merged
merged 2 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion components/brave_sync/brave_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ BraveSyncClient* BraveSyncServiceImpl::GetSyncClient() {
}

bool BraveSyncServiceImpl::IsSyncConfigured() {
return sync_configured_;
return sync_configured_ &&
// When there is 0 or 1 device, it means chain is not completely created,
// so we should give a chance to make force reset in |OnSetupSyncHaveCode|
// or in |OnSetupSyncNewToSync|
(sync_prefs_->GetSyncDevices()->size() >= 2);
}

bool BraveSyncServiceImpl::IsSyncInitialized() {
Expand Down Expand Up @@ -165,6 +169,8 @@ void BraveSyncServiceImpl::OnSetupSyncHaveCode(const std::string& sync_words,
return;
}

ForceCompleteReset();
DCHECK(!sync_prefs_->GetSyncEnabled());
SetDeviceName(device_name);
initializing_ = true;

Expand All @@ -186,6 +192,10 @@ void BraveSyncServiceImpl::OnSetupSyncNewToSync(
return;
}

ForceCompleteReset();

DCHECK(!sync_prefs_->GetSyncEnabled());

sync_words_.clear(); // If the previous attempt was connect to sync chain
// and failed to receive save-init-data
SetDeviceName(device_name);
Expand Down Expand Up @@ -222,6 +232,7 @@ void BraveSyncServiceImpl::OnResetSync() {
// we can reset it by ResetInternal()
const std::string device_id = sync_prefs_->GetThisDeviceId();
OnDeleteDevice(device_id);
reseting_ = true;
}
}

Expand Down Expand Up @@ -690,6 +701,14 @@ void BraveSyncServiceImpl::ResetSyncInternal() {
sync_initialized_ = false;

sync_prefs_->SetSyncEnabled(false);

reseting_ = false;
}

void BraveSyncServiceImpl::ForceCompleteReset() {
if (reseting_) {
ResetSyncInternal();
}
}

void BraveSyncServiceImpl::SetDeviceName(const std::string& name) {
Expand Down
9 changes: 9 additions & 0 deletions components/brave_sync/brave_sync_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ FORWARD_DECLARE_TEST(BraveSyncServiceTest, OnSyncReadyNewToSync);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, OnGetExistingObjects);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, BackgroundSyncStarted);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, BackgroundSyncStopped);
FORWARD_DECLARE_TEST(BraveSyncServiceTest,
OnSetupSyncHaveCode_Reset_SetupAgain);

class BraveSyncServiceTest;

Expand Down Expand Up @@ -108,6 +110,9 @@ class BraveSyncServiceImpl
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, OnGetExistingObjects);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, BackgroundSyncStarted);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, BackgroundSyncStopped);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest,
OnSetupSyncHaveCode_Reset_SetupAgain);

friend class ::BraveSyncServiceTest;

// SyncMessageHandler overrides
Expand Down Expand Up @@ -173,6 +178,10 @@ class BraveSyncServiceImpl
void NotifyHaveSyncWords(const std::string& sync_words);

void ResetSyncInternal();
void ForceCompleteReset();
bool GetResettingForTest() const {
return reseting_;
}

void SetDeviceName(const std::string& name);

Expand Down
67 changes: 67 additions & 0 deletions components/brave_sync/brave_sync_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,70 @@ TEST_F(BraveSyncServiceTest, BackgroundSyncStopped) {
sync_service()->BackgroundSyncStopped(false);
EXPECT_FALSE(sync_service()->timer_->IsRunning());
}

TEST_F(BraveSyncServiceTest, OnSetupSyncHaveCode_Reset_SetupAgain) {
EXPECT_FALSE(sync_service()->GetResettingForTest());
EXPECT_CALL(*sync_client(), OnSyncEnabledChanged).Times(1);
// Expecting sync state changed twice: for enabled state and for device name
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(2);
sync_service()->OnSetupSyncHaveCode("word1 word2 word3", "test_device");
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
brave_sync::prefs::kSyncEnabled));

brave_sync::Uint8Array seed
= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
brave_sync::Uint8Array device_id = {0};
// These settings are changed:
// bookmarks_enabled, site_settings_enabled, history_enabled
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(3);
sync_service()->OnSaveInitData(seed, device_id);

RecordsList records;
records.push_back(SimpleDeviceRecord(
SyncRecord::Action::A_CREATE,
"0", "this_device"));
EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(1);
sync_service()->OnResolvedPreferences(records);

auto devices = sync_service()->sync_prefs_->GetSyncDevices();

ASSERT_EQ(devices->size(), 1u);
EXPECT_TRUE(DevicesContains(devices.get(), "0", "this_device"));
EXPECT_CALL(*sync_client(), SendSyncRecords("PREFERENCES",
ContainsDeviceRecord(SyncRecord::Action::A_DELETE,
"this_device"))).Times(1);
sync_service()->OnResetSync();
// Here we have marked service as in resetting state
// Actual kSyncEnabled will go to false after receiving confirmation of
// this_device DELETE
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
brave_sync::prefs::kSyncEnabled));

EXPECT_TRUE(sync_service()->GetResettingForTest());

// OnSyncStateChanged actually is invoked 9 times:
// ForceCompleteReset
// ResetSyncInternal
// brave_sync::Prefs::Clear()
// device_name
// enabled => false
// bookmarks_enabled
// site_settings_enabled
// history_enabled
// device_list
// enabled => false
// OnSetupSyncHaveCode()
// device_name
// enabled => true

// OnSyncEnabledChanged is actually invoked 3 times:
// see preference enabled in abobe list
EXPECT_CALL(*observer(),
OnSyncStateChanged(sync_service())).Times(AtLeast(1));
EXPECT_CALL(*sync_client(), OnSyncEnabledChanged).Times(AtLeast(1));
sync_service()->OnSetupSyncHaveCode("word1 word2 word5", "test_device");
EXPECT_FALSE(sync_service()->GetResettingForTest());

EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
brave_sync::prefs::kSyncEnabled));
}