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

Pass last_fetch_time to fetch-sync-records #4231

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deps = {
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@13bb40a215248cfbdd87d0a6b425c8397402e9e6",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@e3742ba3e8942eea9e4755d91532491871bd3116",
"vendor/bat-native-tweetnacl": "https://github.com/brave-intl/bat-native-tweetnacl.git@800f9d40b7409239ff192e0be634764e747c7a75",
"components/brave_sync/extension/brave-sync": "https://github.com/brave/sync.git@bde4bf8dcd56e41cc064d613219dc3382c178856",
"components/brave_sync/extension/brave-sync": "https://github.com/brave/sync.git@e36acfa8cbff5ae283407f6fcc6b8916351fd8e0",
"vendor/bat-native-usermodel": "https://github.com/brave-intl/bat-native-usermodel.git@45e32155af9897dbe1d5534dd36697ec4728bb75",
"vendor/challenge_bypass_ristretto_ffi": "https://github.com/brave-intl/challenge-bypass-ristretto-ffi.git@c396fb4eb9e9bf63b89ae5a0ec0b5f201d43c7c5",
}
Expand Down
12 changes: 7 additions & 5 deletions browser/extensions/api/brave_sync_event_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ void BraveSyncEventRouter::GotInitData(

void BraveSyncEventRouter::FetchSyncRecords(
const std::vector<std::string>& category_names,
const base::Time& startAt,
const int max_records) {
const base::Time& start_at,
const int max_records,
const base::Time& previous_fetch_time) {
std::unique_ptr<base::ListValue> args(
extensions::api::brave_sync::OnFetchSyncRecords::Create(category_names,
startAt.ToJsTime(), static_cast<double>(max_records))
.release());
extensions::api::brave_sync::OnFetchSyncRecords::Create(
category_names, start_at.ToJsTime(), static_cast<double>(max_records),
previous_fetch_time.ToJsTime())
.release());
std::unique_ptr<Event> event(
new Event(extensions::events::FOR_TEST,
extensions::api::brave_sync::OnFetchSyncRecords::kEventName,
Expand Down
8 changes: 4 additions & 4 deletions browser/extensions/api/brave_sync_event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class BraveSyncEventRouter {
const extensions::api::brave_sync::Config& config,
const std::string& device_id_v2);

void FetchSyncRecords(
const std::vector<std::string>& category_names,
const base::Time& startAt,
const int max_records);
void FetchSyncRecords(const std::vector<std::string>& category_names,
const base::Time& start_at,
const int max_records,
const base::Time& previous_fetch_time);

void ResolveSyncRecords(const std::string &category_name,
const std::vector<RecordAndExistingObject>& records_and_existing_objects);
Expand Down
4 changes: 4 additions & 0 deletions common/extensions/api/brave_sync.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@
{
"type": "number",
"name": "maxRecords"
},
{
"type": "number",
"name": "previousFetchTime"
}
]
},
Expand Down
75 changes: 44 additions & 31 deletions components/brave_sync/brave_profile_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -836,40 +836,39 @@ bool BraveProfileSyncServiceImpl::IsSQSReady() const {
}
}

void BraveProfileSyncServiceImpl::FetchSyncRecords(const bool bookmarks,
const bool history,
const bool preferences,
int max_records) {
void BraveProfileSyncServiceImpl::FetchBookmarks(int max_records) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(bookmarks || history || preferences);
if (!(bookmarks || history || preferences)) {
return;
}

std::vector<std::string> category_names;
if (history) {
category_names.push_back(kHistorySites); // "HISTORY_SITES";
}
if (bookmarks) {
category_names.push_back(kBookmarks); // "BOOKMARKS";

base::Time last_compact_time =
brave_sync_prefs_->GetLastCompactTimeBookmarks();
if (tools::IsTimeEmpty(last_compact_time) ||
base::Time::Now() - last_compact_time >
base::TimeDelta::FromDays(kCompactPeriodInDays)) {
brave_sync_client_->SendCompact(kBookmarks);
}
}
if (preferences) {
category_names.push_back(kPreferences); // "PREFERENCES";
base::Time last_compact_time =
brave_sync_prefs_->GetLastCompactTimeBookmarks();
if (tools::IsTimeEmpty(last_compact_time) ||
base::Time::Now() - last_compact_time >
base::TimeDelta::FromDays(kCompactPeriodInDays)) {
brave_sync_client_->SendCompact(kBookmarks);
}

base::Time start_at_time =
IsSQSReady() ? brave_sync_prefs_->GetLatestRecordTime() : base::Time();

brave_sync_client_->SendFetchSyncRecords(category_names, start_at_time,
max_records);
base::Time last_fetch_time = brave_sync_prefs_->GetLastFetchTime();
brave_sync_prefs_->SetLastFetchTime(base::Time::Now());

brave_sync_client_->SendFetchSyncRecords({kBookmarks}, start_at_time,
max_records, last_fetch_time);
}

void BraveProfileSyncServiceImpl::FetchHistory(int max_records) {
NOTIMPLEMENTED();
}

void BraveProfileSyncServiceImpl::FetchPreferences(int max_records) {
// For now "PREFERENCES" category stores only devices list
base::Time start_at_time =
IsSQSReady() ? brave_sync_prefs_->GetLatestDeviceRecordTime()
: base::Time();
base::Time last_fetch_time = brave_sync_prefs_->GetLastPreferencesFetchTime();
brave_sync_client_->SendFetchSyncRecords({kPreferences}, start_at_time,
max_records, last_fetch_time);
}

void BraveProfileSyncServiceImpl::SendCreateDevice() {
Expand Down Expand Up @@ -985,22 +984,25 @@ bool BraveProfileSyncServiceImpl::IsBraveSyncEnabled() const {

void BraveProfileSyncServiceImpl::FetchDevices() {
DCHECK(sync_client_);
brave_sync_prefs_->SetLastFetchTime(base::Time::Now());

const auto last_fetch_time = brave_sync_prefs_->GetLastPreferencesFetchTime();
brave_sync_prefs_->SetLastPreferencesFetchTime(base::Time::Now());

base::Time start_at_time =
IsSQSReady() ? brave_sync_prefs_->GetLatestDeviceRecordTime()
: base::Time();

brave_sync_client_->SendFetchSyncRecords(
{brave_sync::jslib_const::kPreferences}, start_at_time, 1000);
{brave_sync::jslib_const::kPreferences}, start_at_time, 1000,
last_fetch_time);
}

void BraveProfileSyncServiceImpl::OnPollSyncCycle(GetRecordsCallback cb,
base::WaitableEvent* wevent) {
if (!brave_sync_prefs_->GetSyncEnabled())
return;

if (IsTimeEmpty(brave_sync_prefs_->GetLastFetchTime())) {
if (IsTimeEmpty(brave_sync_prefs_->GetLastPreferencesFetchTime())) {
SendCreateDevice();
this_device_created_time_ = base::Time::Now();
}
Expand All @@ -1025,9 +1027,20 @@ void BraveProfileSyncServiceImpl::OnPollSyncCycle(GetRecordsCallback cb,
wevent_ = wevent;

const bool bookmarks = brave_sync_prefs_->GetSyncBookmarksEnabled();
if (bookmarks) {
FetchBookmarks(1000);
}

const bool history = brave_sync_prefs_->GetSyncHistoryEnabled();
if (history) {
FetchHistory(1000);
}

const bool preferences = brave_sync_prefs_->GetSyncSiteSettingsEnabled();
FetchSyncRecords(bookmarks, history, preferences, 1000);
if (preferences) {
FetchPreferences(1000);
}

ResendSyncRecords(jslib_const::SyncRecordType_BOOKMARKS);
}

Expand Down
13 changes: 9 additions & 4 deletions components/brave_sync/brave_profile_sync_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ FORWARD_DECLARE_TEST(BraveSyncServiceTest, SetThisDeviceCreatedTime);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, InitialFetchesStartWithZero);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, DeviceIdV2Migration);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, DeviceIdV2MigrationDupDeviceId);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, LastFetchBookmarksTime);
FORWARD_DECLARE_TEST(BraveSyncServiceTest, LastPreferencesFetchTime);

class BraveSyncServiceTest;

Expand Down Expand Up @@ -168,14 +170,17 @@ class BraveProfileSyncServiceImpl
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, DeviceIdV2Migration);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest,
DeviceIdV2MigrationDupDeviceId);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, LastFetchBookmarksTime);
FRIEND_TEST_ALL_PREFIXES(::BraveSyncServiceTest, LastPreferencesFetchTime);

friend class ::BraveSyncServiceTest;

void SignalWaitableEvent();
void FetchSyncRecords(const bool bookmarks,
const bool history,
const bool preferences,
int max_records);

void FetchBookmarks(int max_records);
void FetchHistory(int max_records);
void FetchPreferences(int max_records);

void FetchDevices();
void SendCreateDevice();
void SendDeleteDevice();
Expand Down
12 changes: 12 additions & 0 deletions components/brave_sync/brave_sync_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const char kSyncLatestRecordTime[] = "brave_sync.latest_record_time";
const char kSyncLatestDeviceRecordTime[] =
"brave_sync.latest_device_record_time";
const char kSyncLastFetchTime[] = "brave_sync.last_fetch_time";
const char kSyncLastPreferencesFetchTime[] = "brave_sync.last_prefs_fetch_time";
const char kSyncLastCompactTimeBookmarks[] =
"brave_sync.last_compact_time.bookmarks";
const char kSyncDeviceList[] = "brave_sync.device_list";
Expand Down Expand Up @@ -63,6 +64,8 @@ void Prefs::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterTimePref(prefs::kSyncLatestRecordTime, base::Time());
registry->RegisterTimePref(prefs::kSyncLatestDeviceRecordTime, base::Time());
registry->RegisterTimePref(prefs::kSyncLastFetchTime, base::Time());
registry->RegisterTimePref(prefs::kSyncLastPreferencesFetchTime,
base::Time());
registry->RegisterTimePref(prefs::kSyncLastCompactTimeBookmarks,
base::Time());

Expand Down Expand Up @@ -197,6 +200,14 @@ base::Time Prefs::GetLastFetchTime() {
return pref_service_->GetTime(kSyncLastFetchTime);
}

void Prefs::SetLastPreferencesFetchTime(const base::Time& time) {
pref_service_->SetTime(kSyncLastPreferencesFetchTime, time);
}

base::Time Prefs::GetLastPreferencesFetchTime() {
return pref_service_->GetTime(kSyncLastPreferencesFetchTime);
}

void Prefs::SetLastCompactTimeBookmarks(const base::Time &time) {
pref_service_->SetTime(kSyncLastCompactTimeBookmarks, time);
}
Expand Down Expand Up @@ -289,6 +300,7 @@ void Prefs::Clear() {
pref_service_->ClearPref(kSyncLatestRecordTime);
pref_service_->ClearPref(kSyncLatestDeviceRecordTime);
pref_service_->ClearPref(kSyncLastFetchTime);
pref_service_->ClearPref(kSyncLastPreferencesFetchTime);
pref_service_->ClearPref(kSyncDeviceList);
pref_service_->ClearPref(kSyncApiVersion);
pref_service_->ClearPref(kSyncMigrateBookmarksVersion);
Expand Down
6 changes: 5 additions & 1 deletion components/brave_sync/brave_sync_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ extern const char kSyncHistoryEnabled[];
extern const char kSyncLatestRecordTime[];
// The latest time of synced device record
extern const char kSyncLatestDeviceRecordTime[];
// The time of latest fetch records operation
// The time of latest fetch records operation for bookmarks
extern const char kSyncLastFetchTime[];
// The time of latest fetch records operation for preferences
extern const char kSyncLastPreferencesFetchTime[];
// the list of all known sync devices
// TODO(bridiver) - this should be a dictionary - not raw json
extern const char kSyncDeviceList[];
Expand Down Expand Up @@ -107,6 +109,8 @@ class Prefs {
base::Time GetLatestDeviceRecordTime();
void SetLastFetchTime(const base::Time &time);
base::Time GetLastFetchTime();
void SetLastPreferencesFetchTime(const base::Time& time);
base::Time GetLastPreferencesFetchTime();
void SetLastCompactTimeBookmarks(const base::Time &time);
base::Time GetLastCompactTimeBookmarks();

Expand Down
Loading