From 1b5d035c420a2883201e7faba7e6a35e23d39439 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Fri, 22 Mar 2019 00:00:51 +0200 Subject: [PATCH] Merge pull request #2016 from brave/sync_dont_send_bookmarks_when_no_chain Sync dont send bookmarks when no chain --- components/brave_sync/brave_sync_prefs.cc | 14 +++++----- components/brave_sync/brave_sync_prefs.h | 24 +++++++++-------- .../brave_sync/brave_sync_service_factory.cc | 6 ++++- .../brave_sync/brave_sync_service_impl.cc | 13 +++++++++- .../brave_sync/brave_sync_service_unittest.cc | 26 +++++++++++++++++-- 5 files changed, 62 insertions(+), 21 deletions(-) diff --git a/components/brave_sync/brave_sync_prefs.cc b/components/brave_sync/brave_sync_prefs.cc index 113ce85324ff..4add483a6808 100644 --- a/components/brave_sync/brave_sync_prefs.cc +++ b/components/brave_sync/brave_sync_prefs.cc @@ -1,6 +1,7 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* Copyright 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/components/brave_sync/brave_sync_prefs.h" @@ -91,7 +92,8 @@ bool Prefs::GetSyncSiteSettingsEnabled() const { } void Prefs::SetSyncSiteSettingsEnabled(const bool sync_site_settings_enabled) { - pref_service_->SetBoolean(kSyncSiteSettingsEnabled, sync_site_settings_enabled); + pref_service_->SetBoolean( + kSyncSiteSettingsEnabled, sync_site_settings_enabled); } bool Prefs::GetSyncHistoryEnabled() const { @@ -170,5 +172,5 @@ void Prefs::Clear() { pref_service_->ClearPref(kSyncApiVersion); } -} // namespace prefs -} // namespace brave_sync +} // namespace prefs +} // namespace brave_sync diff --git a/components/brave_sync/brave_sync_prefs.h b/components/brave_sync/brave_sync_prefs.h index 68dc5a2d9217..66cd6f621ac6 100644 --- a/components/brave_sync/brave_sync_prefs.h +++ b/components/brave_sync/brave_sync_prefs.h @@ -1,8 +1,10 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_COMPONENT_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ -#define BRAVE_COMPONENT_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ +/* Copyright 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ +#define BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ #include #include @@ -54,8 +56,8 @@ extern const char kSyncDeviceList[]; extern const char kSyncApiVersion[]; class Prefs { -public: - Prefs(PrefService* pref_service); + public: + explicit Prefs(PrefService* pref_service); std::string GetSeed() const; void SetSeed(const std::string& seed); @@ -92,14 +94,14 @@ class Prefs { void Clear(); -private: + private: // May be null. PrefService* pref_service_; DISALLOW_COPY_AND_ASSIGN(Prefs); }; -} // namespace prefs -} // namespace brave_sync +} // namespace prefs +} // namespace brave_sync -#endif //BRAVE_COMPONENT_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ +#endif // BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_PREFS_H_ diff --git a/components/brave_sync/brave_sync_service_factory.cc b/components/brave_sync/brave_sync_service_factory.cc index 088e0349da28..3bd033812be4 100644 --- a/components/brave_sync/brave_sync_service_factory.cc +++ b/components/brave_sync/brave_sync_service_factory.cc @@ -1,9 +1,13 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/components/brave_sync/brave_sync_service_factory.h" +#include +#include + #include "base/memory/singleton.h" #include "brave/components/brave_sync/brave_sync_prefs.h" #include "brave/components/brave_sync/brave_sync_service.h" diff --git a/components/brave_sync/brave_sync_service_impl.cc b/components/brave_sync/brave_sync_service_impl.cc index 7a7399844dd6..1c2054f2a191 100644 --- a/components/brave_sync/brave_sync_service_impl.cc +++ b/components/brave_sync/brave_sync_service_impl.cc @@ -537,12 +537,23 @@ void BraveSyncServiceImpl::RequestSyncData() { if (tools::IsTimeEmpty(last_fetch_time)) { SendCreateDevice(); + } + + sync_client_->SendFetchSyncDevices(); + + if (sync_prefs_->GetSyncDevices()->size() <= 1) { + // No sense to fetch or sync bookmarks when there no at least two devices + // in chain + // Set last fetch time here because we had fetched devices at least + sync_prefs_->SetLastFetchTime(base::Time::Now()); + return; + } + if (tools::IsTimeEmpty(last_fetch_time)) { bookmark_change_processor_->InitialSync(); } FetchSyncRecords(bookmarks, history, preferences, 1000); - sync_client_->SendFetchSyncDevices(); } void BraveSyncServiceImpl::FetchSyncRecords(const bool bookmarks, diff --git a/components/brave_sync/brave_sync_service_unittest.cc b/components/brave_sync/brave_sync_service_unittest.cc index 29ff37817dad..9968e2c69260 100644 --- a/components/brave_sync/brave_sync_service_unittest.cc +++ b/components/brave_sync/brave_sync_service_unittest.cc @@ -644,12 +644,34 @@ TEST_F(BraveSyncServiceTest, OnSyncReadyAlreadyWithSync) { EXPECT_FALSE(sync_service()->IsSyncInitialized()); profile()->GetPrefs()->SetString( brave_sync::prefs::kSyncBookmarksBaseOrder, "1.1."); - // OnSyncPrefsChanged => OnSyncStateChanged for kSyncSiteSettingsEnabled - EXPECT_CALL(*observer(), OnSyncStateChanged); + // OnSyncPrefsChanged => OnSyncStateChanged for + // kSyncSiteSettingsEnabled (1) and kSyncDeviceList (2) + EXPECT_CALL(*observer(), OnSyncStateChanged).Times(2); profile()->GetPrefs()->SetBoolean( brave_sync::prefs::kSyncSiteSettingsEnabled, true); profile()->GetPrefs()->SetTime( brave_sync::prefs::kSyncLastFetchTime, base::Time::Now()); + const char* devices_json = R"( + { + "devices":[ + { + "device_id":"0", + "last_active":1552993896717.0, + "name":"Device1", + "object_id":"186, 247, 230, 75, 57, 111, 76, 166, 51, 142, 217, 221, 219, 237, 229, 235" + }, + { + "device_id":"1", + "last_active":1552993909257.0, + "name":"Device2", + "object_id":"36, 138, 200, 221, 191, 81, 214, 65, 134, 48, 55, 119, 162, 93, 33, 226" + } + ] + } + )"; + + profile()->GetPrefs()->SetString( + brave_sync::prefs::kSyncDeviceList, devices_json); EXPECT_CALL(*sync_client(), SendFetchSyncRecords).Times(1); EXPECT_CALL(*sync_client(), SendFetchSyncDevices).Times(1); sync_service()->OnSyncReady();