Skip to content

Commit

Permalink
Uplift of #21623 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Feb 7, 2024
1 parent 6d32ba4 commit 6da45eb
Show file tree
Hide file tree
Showing 23 changed files with 335 additions and 34 deletions.
1 change: 1 addition & 0 deletions browser/sync/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ source_set("sync") {

deps = [
"//base",
"//components/history/core/browser",
"//components/sync/service",
"//components/sync_device_info",
]
Expand Down
16 changes: 15 additions & 1 deletion browser/sync/brave_sync_service_impl_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "brave/components/sync/service/brave_sync_service_impl.h"
#include "components/history/core/browser/history_service.h"
#include "components/sync_device_info/device_info_sync_service.h"
#include "components/sync_device_info/device_info_tracker.h"
#include "components/sync_device_info/local_device_info_provider.h"

namespace syncer {

BraveSyncServiceImplDelegate::BraveSyncServiceImplDelegate(
DeviceInfoSyncService* device_info_sync_service)
DeviceInfoSyncService* device_info_sync_service,
history::HistoryService* history_service)
: device_info_sync_service_(device_info_sync_service),
history_service_(history_service),
weak_ptr_factory_(this) {
DCHECK(device_info_sync_service_);

Expand Down Expand Up @@ -99,4 +102,15 @@ void BraveSyncServiceImplDelegate::SetLocalDeviceAppearedCallback(
local_device_appeared_callback_ = std::move(local_device_appeared_callback);
}

void BraveSyncServiceImplDelegate::GetKnownToSyncHistoryCount(
base::OnceCallback<void(std::pair<bool, int>)> callback) {
history_service_->GetKnownToSyncCount(base::BindOnce(
[](base::OnceCallback<void(std::pair<bool, int>)> callback,
history::HistoryCountResult known_to_sync) {
std::move(callback).Run(
std::pair(known_to_sync.success, known_to_sync.count));
},
std::move(callback)));
}

} // namespace syncer
14 changes: 11 additions & 3 deletions browser/sync/brave_sync_service_impl_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
#ifndef BRAVE_BROWSER_SYNC_BRAVE_SYNC_SERVICE_IMPL_DELEGATE_H_
#define BRAVE_BROWSER_SYNC_BRAVE_SYNC_SERVICE_IMPL_DELEGATE_H_

#include "brave/components/sync/service/sync_service_impl_delegate.h"
#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "brave/components/sync/service/sync_service_impl_delegate.h"
#include "components/sync_device_info/device_info_tracker.h"

class Profile;
namespace history {
class HistoryService;
} // namespace history

namespace syncer {

Expand All @@ -29,7 +32,8 @@ class BraveSyncServiceImplDelegate
public syncer::DeviceInfoTracker::Observer {
public:
explicit BraveSyncServiceImplDelegate(
DeviceInfoSyncService* device_info_sync_service);
DeviceInfoSyncService* device_info_sync_service,
history::HistoryService* history_service);
~BraveSyncServiceImplDelegate() override;

void SuspendDeviceObserverForOwnReset() override;
Expand All @@ -38,6 +42,9 @@ class BraveSyncServiceImplDelegate
void SetLocalDeviceAppearedCallback(
base::OnceCallback<void()> local_device_appeared_callback) override;

void GetKnownToSyncHistoryCount(
base::OnceCallback<void(std::pair<bool, int>)> callback) override;

private:
// syncer::DeviceInfoTracker::Observer:
void OnDeviceInfoChange() override;
Expand All @@ -54,6 +61,7 @@ class BraveSyncServiceImplDelegate
device_info_observer_{this};

raw_ptr<DeviceInfoSyncService> device_info_sync_service_ = nullptr;
raw_ptr<history::HistoryService> history_service_ = nullptr;

// This is triggered once after SetLocalDeviceAppearedCallback
// when the local device first appears in the changed synced devices list
Expand Down
13 changes: 8 additions & 5 deletions chromium_src/chrome/browser/sync/sync_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

#include "brave/browser/sync/brave_sync_service_impl_delegate.h"
#include "brave/components/sync/service/brave_sync_service_impl.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/sync/device_info_sync_service_factory.h"

#define BRAVE_BUILD_SERVICE_INSTANCE_FOR \
std::make_unique<syncer::BraveSyncServiceImpl>( \
std::move(init_params), \
std::make_unique<syncer::BraveSyncServiceImplDelegate>( \
DeviceInfoSyncServiceFactory::GetForProfile(profile)));
#define BRAVE_BUILD_SERVICE_INSTANCE_FOR \
std::make_unique<syncer::BraveSyncServiceImpl>( \
std::move(init_params), \
std::make_unique<syncer::BraveSyncServiceImplDelegate>( \
DeviceInfoSyncServiceFactory::GetForProfile(profile), \
HistoryServiceFactory::GetForProfile( \
profile, ServiceAccessType::IMPLICIT_ACCESS)));

#include "src/chrome/browser/sync/sync_service_factory.cc"

Expand Down
15 changes: 15 additions & 0 deletions chromium_src/components/history/core/browser/history_backend.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */

#include "src/components/history/core/browser/history_backend.cc"

namespace history {

HistoryCountResult HistoryBackend::GetKnownToSyncCount() {
int count = 0;
return {db_ && db_->GetKnownToSyncCount(&count), count};
}

} // namespace history
17 changes: 17 additions & 0 deletions chromium_src/components/history/core/browser/history_backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_BACKEND_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_BACKEND_H_

#define GetHistoryCount \
GetKnownToSyncCount(); \
HistoryCountResult GetHistoryCount

#include "src/components/history/core/browser/history_backend.h" // IWYU pragma: export

#undef GetHistoryCount

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_BACKEND_H_
18 changes: 18 additions & 0 deletions chromium_src/components/history/core/browser/history_service.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */

#include "src/components/history/core/browser/history_service.cc"

namespace history {

void HistoryService::GetKnownToSyncCount(
base::OnceCallback<void(HistoryCountResult)> callback) {
backend_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&HistoryBackend::GetKnownToSyncCount, history_backend_),
std::move(callback));
}

} // namespace history
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ class BraveHistoryQuickProviderTest;
friend class ::BraveHistoryQuickProviderTest; \
void CleanupUnused

#define AddRelatedSearchesForVisit \
GetKnownToSyncCount( \
base::OnceCallback<void(history::HistoryCountResult)> callback); \
void AddRelatedSearchesForVisit

#include "src/components/history/core/browser/history_service.h" // IWYU pragma: export

#undef AddRelatedSearchesForVisit

#undef Cleanup

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_SERVICE_H_
18 changes: 18 additions & 0 deletions chromium_src/components/history/core/browser/visit_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@
#include "src/components/history/core/browser/visit_database.cc"

#undef SOURCE_SAFARI_IMPORTED

namespace history {

bool VisitDatabase::GetKnownToSyncCount(int* count) {
sql::Statement statement(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"SELECT COUNT(*) "
"FROM visits "
"WHERE is_known_to_sync == TRUE"));

*count = 0;
if (statement.Step()) {
*count = statement.ColumnInt(0);
}
return true;
}

} // namespace history
17 changes: 17 additions & 0 deletions chromium_src/components/history/core/browser/visit_database.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_VISIT_DATABASE_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_VISIT_DATABASE_H_

#define GetHistoryCount \
GetKnownToSyncCount(int* count); \
bool GetHistoryCount

#include "src/components/history/core/browser/visit_database.h" // IWYU pragma: export

#undef GetHistoryCount

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_HISTORY_CORE_BROWSER_VISIT_DATABASE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

#include "brave/browser/sync/brave_sync_service_impl_delegate.h"
#include "brave/components/sync/service/brave_sync_service_impl.h"
#include "ios/chrome/browser/history/model/history_service_factory.h"
#include "ios/chrome/browser/sync/model/device_info_sync_service_factory.h"

#define BRAVE_BUILD_SERVICE_INSTANCE_FOR \
std::make_unique<syncer::BraveSyncServiceImpl>( \
std::move(init_params), \
std::make_unique<syncer::BraveSyncServiceImplDelegate>( \
DeviceInfoSyncServiceFactory::GetForBrowserState(browser_state)));
#define BRAVE_BUILD_SERVICE_INSTANCE_FOR \
std::make_unique<syncer::BraveSyncServiceImpl>( \
std::move(init_params), \
std::make_unique<syncer::BraveSyncServiceImplDelegate>( \
DeviceInfoSyncServiceFactory::GetForBrowserState(browser_state), \
ios::HistoryServiceFactory::GetForBrowserState( \
browser_state, ServiceAccessType::IMPLICIT_ACCESS)));

#include "src/ios/chrome/browser/sync/model/sync_service_factory.mm"

Expand Down
4 changes: 2 additions & 2 deletions components/brave_sync/brave_sync_p3a.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ void RecordEnabledTypes(bool sync_everything_enabled,
}

void RecordSyncedObjectsCount(int total_entities) {
// "Brave.Sync.SyncedObjectsCount"
// "Brave.Sync.SyncedObjectsCount.2"
// 0 - 0..1000
// 1 - 1001..10000
// 2 - 10001..49000
// 3 - >= 49001
p3a_utils::RecordToHistogramBucket(kSyncedObjectsCountHistogramName,
p3a_utils::RecordToHistogramBucket(kSyncedObjectsCountHistogramNameV2,
{1000, 10000, 49000}, total_entities);
}

Expand Down
5 changes: 3 additions & 2 deletions components/brave_sync/brave_sync_p3a.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace p3a {
// TODO(alexeybarabash): move here also "Brave.Sync.Status.2" and
// "Brave.Sync.ProgressTokenEverReset"
inline constexpr char kEnabledTypesHistogramName[] = "Brave.Sync.EnabledTypes";
inline constexpr char kSyncedObjectsCountHistogramName[] =
"Brave.Sync.SyncedObjectsCount";
// Improved version of metric which includes count of synced History objects
inline constexpr char kSyncedObjectsCountHistogramNameV2[] =
"Brave.Sync.SyncedObjectsCount.2";

enum class EnabledTypesAnswer {
kEmptyOrBookmarksOnly = 0,
Expand Down
14 changes: 7 additions & 7 deletions components/brave_sync/brave_sync_p3a_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ TEST(BraveSyncP3ATest, TestSyncedObjectsCount) {
base::HistogramTester histogram_tester;

RecordSyncedObjectsCount(0);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 0, 1);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 0, 1);
RecordSyncedObjectsCount(1000);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 0, 2);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 0, 2);

RecordSyncedObjectsCount(1001);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 1, 1);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 1, 1);
RecordSyncedObjectsCount(10000);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 1, 2);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 1, 2);

RecordSyncedObjectsCount(10001);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 2, 1);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 2, 1);
RecordSyncedObjectsCount(49000);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 2, 2);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 2, 2);

RecordSyncedObjectsCount(49001);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramName, 3, 1);
histogram_tester.ExpectBucketCount(kSyncedObjectsCountHistogramNameV2, 3, 1);
}

} // namespace p3a
Expand Down
16 changes: 16 additions & 0 deletions components/history/core/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

source_set("unit_tests") {
testonly = true
sources = [
"//brave/components/history/core/browser/brave_visit_database_unittest.cc",
]
deps = [
"//base",
"//components/history/core/browser",
"//testing/gtest",
]
}
Loading

0 comments on commit 6da45eb

Please sign in to comment.