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 if browser was closed before the chain was fully created #3730

Closed
wants to merge 6 commits into from
9 changes: 5 additions & 4 deletions components/brave_sync/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ if (enable_brave_sync) {
"//components/prefs",
"//components/prefs",
"//components/signin/public/identity_manager",
"//components/sync/driver:driver",
"//components/sync:rest_of_sync",
"//components/sync/driver:driver",
"//content/public/browser",
"//extensions/browser",
"//net",
Expand Down Expand Up @@ -64,6 +64,9 @@ source_set("prefs") {
sources = [
"brave_sync_prefs.cc",
"brave_sync_prefs.h",
"brave_sync_prefs_base.h",
"brave_sync_prefs_mem_store.cc",
"brave_sync_prefs_mem_store.h",
"settings.cc",
"settings.h",
"sync_devices.cc",
Expand Down Expand Up @@ -100,9 +103,7 @@ source_set("crypto") {
]

if (is_android) {
deps += [
"//third_party/android_sdk:cpu_features",
]
deps += [ "//third_party/android_sdk:cpu_features" ]
}
}

Expand Down
77 changes: 64 additions & 13 deletions components/brave_sync/brave_profile_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ std::unique_ptr<SyncRecord> CreateDeleteBookmarkByObjectId(
const std::string& object_id) {
auto record = std::make_unique<SyncRecord>();
record->deviceId = brave_sync_prefs->GetThisDeviceId();
DCHECK(!record->deviceId.empty());
record->objectData = jslib_const::SyncObjectData_BOOKMARK;
record->objectId = object_id;
record->action = jslib::SyncRecord::Action::A_DELETE;
Expand Down Expand Up @@ -168,11 +169,39 @@ BraveProfileSyncServiceImpl::BraveProfileSyncServiceImpl(Profile* profile,
: BraveProfileSyncService(std::move(init_params)),
brave_sync_client_(BraveSyncClient::Create(this, profile)) {
brave_sync_words_ = std::string();
brave_sync_prefs_ =
brave_sync_prefs_disk_store_ =
std::make_unique<prefs::Prefs>(sync_client_->GetPrefService());
brave_sync_prefs_ = brave_sync_prefs_disk_store_.get();

if (IsSyncSetupIncomplete()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having IsBraveSyncInitialized, IsBraveSyncConfigured and IsSyncSetupIncomplete is just way too confusing. We need to simplify the possible states and add comments to explain what they are. Also we shouldn't mix the "positive" and "negative" forms of checks because that's also confusing to read. IsSyncSetupComplete is the "positive" form vs IsSyncSetupIncomplete

// We want to reset the chain because it was not fully created
// We cannot make it here directly, because some sync classes may not be
// constructed yet
reseting_ = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to trigger ResetSyncInternal() by ForceCompleteReset()?
If so, then we don't need to clear pref here https://github.com/brave/brave-core/pull/3730/files#diff-ab344835b09fb6ac1c601af787b116bbR184

Copy link
Contributor Author

@AlexeyBarabash AlexeyBarabash Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to trigger ResetSyncInternal() by ForceCompleteReset()?

yes

but if we don't reset devices list, UI page will never trigger BraveProfileSyncServiceImpl::OnSetupSyncHaveCode because device name is not empty https://github.com/brave/brave-core/blob/master/components/brave_sync/ui/components/modals/enterSyncCode.tsx#L92

so instead of clearing all prefs it would be enough co clean only the current device name, but I decided to clean all non-actual data

// Need to clear prefs to allow UI page give us code words
brave_sync_prefs_->Clear();
}

// Moniter syncs prefs required in GetSettingsAndDevices
brave_pref_change_registrar_.Init(sync_client_->GetPrefService());

model_ = BookmarkModelFactory::GetForBrowserContext(profile);

if (!brave_sync_prefs_->GetSeed().empty() &&
!brave_sync_prefs_->GetThisDeviceName().empty()) {
brave_sync_configured_ = true;
SetupDiskPrefsObservers();
} else {
brave_sync_prefs_ = &brave_sync_prefs_mem_store_;
brave_sync_prefs_mem_store_.AddObserver(base::BindRepeating(
&BraveProfileSyncServiceImpl::OnBraveSyncPrefsChanged,
base::Unretained(this)));
}
network_connection_tracker_->AddNetworkConnectionObserver(this);
RecordSyncStateP3A();
}

void BraveProfileSyncServiceImpl::SetupDiskPrefsObservers() {
brave_pref_change_registrar_.Add(
prefs::kSyncEnabled,
base::Bind(&BraveProfileSyncServiceImpl::OnBraveSyncPrefsChanged,
Expand All @@ -197,15 +226,6 @@ BraveProfileSyncServiceImpl::BraveProfileSyncServiceImpl(Profile* profile,
prefs::kSyncHistoryEnabled,
base::Bind(&BraveProfileSyncServiceImpl::OnBraveSyncPrefsChanged,
base::Unretained(this)));

model_ = BookmarkModelFactory::GetForBrowserContext(profile);

if (!brave_sync_prefs_->GetSeed().empty() &&
!brave_sync_prefs_->GetThisDeviceName().empty()) {
brave_sync_configured_ = true;
}
network_connection_tracker_->AddNetworkConnectionObserver(this);
RecordSyncStateP3A();
}

void BraveProfileSyncServiceImpl::OnNudgeSyncCycle(RecordsListPtr records) {
Expand All @@ -214,6 +234,7 @@ void BraveProfileSyncServiceImpl::OnNudgeSyncCycle(RecordsListPtr records) {

for (auto& record : *records) {
record->deviceId = brave_sync_prefs_->GetThisDeviceId();
DCHECK(!record->deviceId.empty());
}
if (!records->empty()) {
if (((!brave_sync::tools::IsTimeEmpty(chain_created_time_) &&
Expand All @@ -232,6 +253,17 @@ BraveProfileSyncServiceImpl::~BraveProfileSyncServiceImpl() {
network_connection_tracker_->RemoveNetworkConnectionObserver(this);
}

bool BraveProfileSyncServiceImpl::IsSyncSetupIncomplete() const {
auto devices = brave_sync_prefs_->GetSyncDevices();
if (devices->size() < 2 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sync setup is not complete is more than one device?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sync setup is not complete when have less than 2 devices

(brave_sync_prefs_->GetSyncEnabled() ||
!brave_sync_prefs_->GetSeed().empty() ||
!brave_sync_prefs_->GetThisDeviceName().empty())) {
return true;
}
return false;
}

void BraveProfileSyncServiceImpl::OnSetupSyncHaveCode(
const std::string& sync_words,
const std::string& device_name) {
Expand Down Expand Up @@ -320,6 +352,7 @@ void BraveProfileSyncServiceImpl::OnResetSync() {
// We have to send delete record and wait for library deleted response then
// we can reset it by ResetSyncInternal()
const std::string device_id = brave_sync_prefs_->GetThisDeviceId();
DCHECK(!device_id.empty());
OnDeleteDevice(device_id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we care about deleting devices if we're resetting sync?

reseting_ = true;
}
Expand Down Expand Up @@ -671,6 +704,11 @@ void BraveProfileSyncServiceImpl::ResetSyncInternal() {
void BraveProfileSyncServiceImpl::ForceCompleteReset() {
if (reseting_) {
ResetSyncInternal();
} else {
// Reset only Chromium part
ProfileSyncService::GetUserSettings()->SetSyncRequested(false);
syncer::SyncPrefs sync_prefs(sync_client_->GetPrefService());
sync_prefs.SetLastSyncedTime(base::Time());
}
}

Expand Down Expand Up @@ -859,6 +897,8 @@ void BraveProfileSyncServiceImpl::SendDeviceSyncRecord(
void BraveProfileSyncServiceImpl::OnResolvedPreferences(
const RecordsList& records) {
const std::string this_device_id = brave_sync_prefs_->GetThisDeviceId();
DCHECK(!this_device_id.empty());

bool this_device_deleted = false;
bool contains_only_one_device = false;

Expand Down Expand Up @@ -887,6 +927,7 @@ void BraveProfileSyncServiceImpl::OnResolvedPreferences(
if (old_devices_size < 2 && sync_devices->size() >= 2) {
// Save chain creation time to send bookmarks 30 sec after
chain_created_time_ = base::Time::Now();
SwitchToDiskPrefs();
}
if (!tools::IsTimeEmpty(chain_created_time_) &&
(base::Time::Now() - chain_created_time_).InSeconds() > 30u) {
Expand All @@ -906,7 +947,8 @@ void BraveProfileSyncServiceImpl::OnResolvedPreferences(
void BraveProfileSyncServiceImpl::OnBraveSyncPrefsChanged(
const std::string& pref) {
if (pref == prefs::kSyncEnabled) {
brave_sync_client_->OnSyncEnabledChanged();
bool is_enabled = brave_sync_prefs_->GetSyncEnabled();
brave_sync_client_->OnSyncEnabledChanged(is_enabled);
RecordSyncStateP3A();
} else if (pref == prefs::kSyncDeviceList) {
RecordSyncStateP3A();
Expand Down Expand Up @@ -1060,8 +1102,8 @@ void BraveProfileSyncServiceImpl::ResendSyncRecords(
if (node) {
records->push_back(BookmarkNodeToSyncBookmark(node));
} else {
records->push_back(
CreateDeleteBookmarkByObjectId(brave_sync_prefs_.get(), object_id));
records->push_back(CreateDeleteBookmarkByObjectId(
brave_sync_prefs_disk_store_.get(), object_id));
}
}
if (!records->empty())
Expand Down Expand Up @@ -1098,4 +1140,13 @@ void BraveProfileSyncServiceImpl::RecordSyncStateP3A() const {
UMA_HISTOGRAM_EXACT_LINEAR("Brave.Sync.Status", result, 2);
}

void BraveProfileSyncServiceImpl::SwitchToDiskPrefs() {
brave_sync_prefs_ = brave_sync_prefs_disk_store_.get();
brave_sync::prefs::CloneMemPrefsToDisk(&brave_sync_prefs_mem_store_,
brave_sync_prefs_disk_store_.get());
SetupDiskPrefsObservers();
brave_sync_prefs_mem_store_.ResetObserver();
brave_sync_prefs_mem_store_.Clear();
}

} // namespace brave_sync
16 changes: 15 additions & 1 deletion components/brave_sync/brave_profile_sync_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <vector>

#include "brave/components/brave_sync/brave_sync_prefs_mem_store.h"
#include "brave/components/brave_sync/brave_sync_service.h"
#include "brave/components/brave_sync/client/brave_sync_client.h"
#include "brave/components/brave_sync/jslib_messages_fwd.h"
Expand Down Expand Up @@ -40,12 +41,15 @@ FORWARD_DECLARE_TEST(BraveSyncServiceTest,
OnSetupSyncHaveCode_Reset_SetupAgain);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, ExponentialResend);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, GetDevicesWithFetchSyncRecords);
FORWARD_DECLARE_TEST(ServiceStartDeviceLeftoverTest,
OnSetupSyncHaveCodeDeviceLeftover);

class BraveSyncServiceTest;

namespace brave_sync {
namespace prefs {
class Prefs;
class PrefsBase;
} // namespace prefs

class BraveProfileSyncServiceImpl
Expand Down Expand Up @@ -146,6 +150,8 @@ class BraveProfileSyncServiceImpl
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, ExponentialResend);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest,
GetDevicesWithFetchSyncRecords);
FRIEND_TEST_ALL_PREFIXES(::ServiceStartDeviceLeftoverTest,
OnSetupSyncHaveCodeDeviceLeftover);
friend class ::BraveSyncServiceTest;

void SignalWaitableEvent();
Expand Down Expand Up @@ -192,12 +198,17 @@ class BraveProfileSyncServiceImpl

void RecordSyncStateP3A() const;

bool IsSyncSetupIncomplete() const;

static base::TimeDelta GetRetryExponentialWaitAmount(int retry_number);
static std::vector<unsigned> GetExponentialWaitsForTests();
static const std::vector<unsigned> kExponentialWaits;
static const int kMaxSendRetries;

std::unique_ptr<brave_sync::prefs::Prefs> brave_sync_prefs_;
brave_sync::prefs::PrefsBase* brave_sync_prefs_ = nullptr;
std::unique_ptr<brave_sync::prefs::Prefs> brave_sync_prefs_disk_store_;
brave_sync::prefs::PrefsMemStore brave_sync_prefs_mem_store_;

// True when is in active sync chain
bool brave_sync_configured_ = false;

Expand Down Expand Up @@ -225,6 +236,9 @@ class BraveProfileSyncServiceImpl
base::Time chain_created_time_;
std::vector<RecordsListPtr> pending_send_records_;

void SwitchToDiskPrefs();
void SetupDiskPrefsObservers();

// Used to ensure that certain operations are performed on the sequence that
// this object was created on.
SEQUENCE_CHECKER(sequence_checker_);
Expand Down
98 changes: 51 additions & 47 deletions components/brave_sync/brave_sync_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "base/macros.h"
#include "base/values.h"
#include "brave/components/brave_sync/brave_sync_prefs_base.h"

class PrefService;
class Profile;
Expand Down Expand Up @@ -70,60 +71,63 @@ extern const char kSyncRecordsToResend[];
// Meta info of kSyncRecordsToResend
extern const char kSyncRecordsToResendMeta[];

class Prefs {
class Prefs : public PrefsBase {
public:
explicit Prefs(PrefService* pref_service);
virtual ~Prefs() = default;

static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

std::string GetSeed() const;
void SetSeed(const std::string& seed);
std::string GetPrevSeed() const;
void SetPrevSeed(const std::string& seed);
std::string GetThisDeviceId() const;
void SetThisDeviceId(const std::string& device_id);
std::string GetThisDeviceName() const;
void SetThisDeviceName(const std::string& device_name);
std::string GetBookmarksBaseOrder();
void SetBookmarksBaseOrder(const std::string& order);

bool GetSyncEnabled() const;
void SetSyncEnabled(const bool sync_this_device);
bool GetSyncBookmarksEnabled() const;
void SetSyncBookmarksEnabled(const bool sync_bookmarks_enabled);
bool GetSyncSiteSettingsEnabled() const;
void SetSyncSiteSettingsEnabled(const bool sync_site_settings);
bool GetSyncHistoryEnabled() const;
void SetSyncHistoryEnabled(const bool sync_history_enabled);

void SetLatestRecordTime(const base::Time &time);
base::Time GetLatestRecordTime();
void SetLatestDeviceRecordTime(const base::Time& time);
base::Time GetLatestDeviceRecordTime();
void SetLastFetchTime(const base::Time &time);
base::Time GetLastFetchTime();

std::unique_ptr<SyncDevices> GetSyncDevices();
void SetSyncDevices(const SyncDevices& sync_devices);

std::string GetApiVersion();
void SetApiVersion(const std::string& api_version);

std::unique_ptr<Settings> GetBraveSyncSettings() const;

int GetMigratedBookmarksVersion();
void SetMigratedBookmarksVersion(const int);

std::vector<std::string> GetRecordsToResend() const;
void AddToRecordsToResend(const std::string& object_id,
std::unique_ptr<base::DictionaryValue> meta);
void RemoveFromRecordsToResend(const std::string& object_id);
std::string GetSeed() const override;
void SetSeed(const std::string& seed) override;
std::string GetPrevSeed() const override;
void SetPrevSeed(const std::string& seed) override;
std::string GetThisDeviceId() const override;
void SetThisDeviceId(const std::string& device_id) override;
std::string GetThisDeviceName() const override;
void SetThisDeviceName(const std::string& device_name) override;
std::string GetBookmarksBaseOrder() override;
void SetBookmarksBaseOrder(const std::string& order) override;

bool GetSyncEnabled() const override;
void SetSyncEnabled(const bool sync_this_device) override;
bool GetSyncBookmarksEnabled() const override;
void SetSyncBookmarksEnabled(const bool sync_bookmarks_enabled) override;
bool GetSyncSiteSettingsEnabled() const override;
void SetSyncSiteSettingsEnabled(const bool sync_site_settings) override;
bool GetSyncHistoryEnabled() const override;
void SetSyncHistoryEnabled(const bool sync_history_enabled) override;

void SetLatestRecordTime(const base::Time& time) override;
base::Time GetLatestRecordTime() override;
void SetLatestDeviceRecordTime(const base::Time& time) override;
base::Time GetLatestDeviceRecordTime() override;
void SetLastFetchTime(const base::Time& time) override;
base::Time GetLastFetchTime() override;

std::unique_ptr<SyncDevices> GetSyncDevices() override;
void SetSyncDevices(const SyncDevices& sync_devices) override;

std::string GetApiVersion() override;
void SetApiVersion(const std::string& api_version) override;

std::unique_ptr<Settings> GetBraveSyncSettings() const override;

int GetMigratedBookmarksVersion() override;
void SetMigratedBookmarksVersion(const int) override;

std::vector<std::string> GetRecordsToResend() const override;
void AddToRecordsToResend(
const std::string& object_id,
std::unique_ptr<base::DictionaryValue> meta) override;
void RemoveFromRecordsToResend(const std::string& object_id) override;
const base::DictionaryValue* GetRecordToResendMeta(
const std::string& object_id) const;
void SetRecordToResendMeta(const std::string& object_id,
std::unique_ptr<base::DictionaryValue> meta);
const std::string& object_id) const override;
void SetRecordToResendMeta(
const std::string& object_id,
std::unique_ptr<base::DictionaryValue> meta) override;

void Clear();
void Clear() override;

private:
// May be null.
Expand Down
Loading