Commit de22415a authored by Yao Xiao's avatar Yao Xiao Committed by Commit Bot

For floc computation, use a cached swaa/nac/account_type status

Use a cached swaa/nac/account_type status to avoid querying the server
too often. This is to prepare for an upcoming CL that computes the floc
(which will go through all the checks) on history-deletion.

Bug: 1062736
Change-Id: I6d48e102f664c3dd1e673f63f5afaf4d7eadef05
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2363006
Commit-Queue: Yao Xiao <yaoxia@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799666}
parent 13360978
......@@ -28,6 +28,8 @@ constexpr size_t kMinHistoryDomainSizeToReportFlocId = 1;
constexpr base::TimeDelta kFlocScheduledUpdateInterval =
base::TimeDelta::FromDays(1);
constexpr int kQueryHistoryWindowInDays = 7;
constexpr base::TimeDelta kSwaaNacAccountEnabledCachePeriod =
base::TimeDelta::FromHours(12);
} // namespace
......@@ -85,6 +87,14 @@ bool FlocIdProviderImpl::AreThirdPartyCookiesAllowed() {
void FlocIdProviderImpl::IsSwaaNacAccountEnabled(
CanComputeFlocCallback callback) {
if (!last_swaa_nac_account_enabled_query_time_.is_null() &&
last_swaa_nac_account_enabled_query_time_ +
kSwaaNacAccountEnabledCachePeriod >
base::TimeTicks::Now()) {
std::move(callback).Run(cached_swaa_nac_account_enabled_);
return;
}
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation =
net::DefinePartialNetworkTrafficAnnotation(
"floc_id_provider_impl", "floc_remote_permission_service",
......@@ -115,7 +125,9 @@ void FlocIdProviderImpl::IsSwaaNacAccountEnabled(
})");
floc_remote_permission_service_->QueryFlocPermission(
std::move(callback), partial_traffic_annotation);
base::BindOnce(&FlocIdProviderImpl::OnCheckSwaaNacAccountEnabledCompleted,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
partial_traffic_annotation);
}
void FlocIdProviderImpl::Shutdown() {
......@@ -199,6 +211,14 @@ void FlocIdProviderImpl::OnCheckCanComputeFlocCompleted(
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void FlocIdProviderImpl::OnCheckSwaaNacAccountEnabledCompleted(
CanComputeFlocCallback callback,
bool enabled) {
cached_swaa_nac_account_enabled_ = enabled;
last_swaa_nac_account_enabled_query_time_ = base::TimeTicks::Now();
std::move(callback).Run(enabled);
}
void FlocIdProviderImpl::GetRecentlyVisitedURLs(
GetRecentlyVisitedURLsCallback callback) {
history::QueryOptions options;
......
......@@ -87,6 +87,9 @@ class FlocIdProviderImpl : public FlocIdProvider,
void OnCheckCanComputeFlocCompleted(ComputeFlocCompletedCallback callback,
bool can_compute_floc);
void OnCheckSwaaNacAccountEnabledCompleted(CanComputeFlocCallback callback,
bool enabled);
void GetRecentlyVisitedURLs(GetRecentlyVisitedURLsCallback callback);
void OnGetRecentlyVisitedURLsCompleted(ComputeFlocCompletedCallback callback,
history::QueryResults results);
......@@ -95,6 +98,11 @@ class FlocIdProviderImpl : public FlocIdProvider,
bool floc_computation_in_progress_ = false;
bool first_floc_computation_triggered_ = false;
// For the swaa/nac/account_type permission, we will use a cached status to
// avoid querying too often.
bool cached_swaa_nac_account_enabled_ = false;
base::TimeTicks last_swaa_nac_account_enabled_query_time_;
syncer::SyncService* sync_service_;
scoped_refptr<content_settings::CookieSettings> cookie_settings_;
FlocRemotePermissionService* floc_remote_permission_service_;
......
......@@ -8,6 +8,7 @@
#include "base/strings/strcat.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/federated_learning/floc_remote_permission_service.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
......@@ -25,6 +26,24 @@ namespace federated_learning {
namespace {
class FakeFlocRemotePermissionService : public FlocRemotePermissionService {
public:
using FlocRemotePermissionService::FlocRemotePermissionService;
void QueryFlocPermission(QueryFlocPermissionCallback callback,
const net::PartialNetworkTrafficAnnotationTag&
partial_traffic_annotation) override {
std::move(callback).Run(swaa_nac_account_enabled_);
}
void set_swaa_nac_account_enabled(bool enabled) {
swaa_nac_account_enabled_ = enabled;
}
private:
bool swaa_nac_account_enabled_ = true;
};
class MockFlocIdProvider : public FlocIdProviderImpl {
public:
using FlocIdProviderImpl::FlocIdProviderImpl;
......@@ -39,10 +58,6 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
return third_party_cookies_allowed_;
}
void IsSwaaNacAccountEnabled(CanComputeFlocCallback callback) override {
std::move(callback).Run(swaa_nac_account_enabled_);
}
size_t floc_update_notification_count() const {
return floc_update_notification_count_;
}
......@@ -51,14 +66,9 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
third_party_cookies_allowed_ = allowed;
}
void set_swaa_nac_account_enabled(bool enabled) {
swaa_nac_account_enabled_ = enabled;
}
private:
size_t floc_update_notification_count_ = 0u;
bool third_party_cookies_allowed_ = true;
bool swaa_nac_account_enabled_ = true;
};
} // namespace
......@@ -82,10 +92,13 @@ class FlocIdProviderUnitTest : public testing::Test {
syncer::SyncService::TransportState::DISABLED);
fake_user_event_service_ = std::make_unique<syncer::FakeUserEventService>();
fake_floc_remote_permission_service_ =
std::make_unique<FakeFlocRemotePermissionService>(
/*url_loader_factory=*/nullptr);
floc_id_provider_ = std::make_unique<MockFlocIdProvider>(
test_sync_service_.get(), /*cookie_settings=*/nullptr,
/*floc_remote_permission_service=*/nullptr, history_service_.get(),
fake_floc_remote_permission_service_.get(), history_service_.get(),
fake_user_event_service_.get());
task_environment_.RunUntilIdle();
......@@ -96,6 +109,11 @@ class FlocIdProviderUnitTest : public testing::Test {
floc_id_provider_->CheckCanComputeFloc(std::move(callback));
}
void IsSwaaNacAccountEnabled(
FlocIdProviderImpl::CanComputeFlocCallback callback) {
floc_id_provider_->IsSwaaNacAccountEnabled(std::move(callback));
}
void OnGetRecentlyVisitedURLsCompleted(
FlocIdProviderImpl::ComputeFlocTrigger trigger,
history::QueryResults results) {
......@@ -130,12 +148,18 @@ class FlocIdProviderUnitTest : public testing::Test {
floc_id_provider_->first_floc_computation_triggered_ = triggered;
}
void SetRemoteSwaaNacAccountEnabled(bool enabled) {
fake_floc_remote_permission_service_->set_swaa_nac_account_enabled(enabled);
}
protected:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<history::HistoryService> history_service_;
std::unique_ptr<syncer::TestSyncService> test_sync_service_;
std::unique_ptr<syncer::FakeUserEventService> fake_user_event_service_;
std::unique_ptr<FakeFlocRemotePermissionService>
fake_floc_remote_permission_service_;
std::unique_ptr<MockFlocIdProvider> floc_id_provider_;
base::ScopedTempDir temp_dir_;
......@@ -301,7 +325,7 @@ TEST_F(FlocIdProviderUnitTest,
test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE);
floc_id_provider_->set_swaa_nac_account_enabled(false);
SetRemoteSwaaNacAccountEnabled(false);
base::OnceCallback<void(bool)> cb = base::BindOnce(
[](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); });
......@@ -310,6 +334,41 @@ TEST_F(FlocIdProviderUnitTest,
task_environment_.RunUntilIdle();
}
TEST_F(FlocIdProviderUnitTest, SwaaNacAccountEnabledUseCacheStatus) {
base::OnceCallback<void(bool)> assert_enabled_callback_1 = base::BindOnce(
[](bool can_compute_floc) { ASSERT_TRUE(can_compute_floc); });
// The permission status in the fake_floc_remote_premission_service_ is by
// default enabled.
IsSwaaNacAccountEnabled(std::move(assert_enabled_callback_1));
task_environment_.RunUntilIdle();
// Turn off the permission in the fake_floc_remote_premission_service_.
SetRemoteSwaaNacAccountEnabled(false);
base::OnceCallback<void(bool)> assert_enabled_callback_2 = base::BindOnce(
[](bool can_compute_floc) { ASSERT_TRUE(can_compute_floc); });
// Fast forward by 11 hours. The cache is still valid.
task_environment_.FastForwardBy(base::TimeDelta::FromHours(11));
// The permission status is still enabled because it was obtained from the
// cache.
IsSwaaNacAccountEnabled(std::move(assert_enabled_callback_2));
task_environment_.RunUntilIdle();
// Fast forward by 1 hour so the cache becomes invalid.
task_environment_.FastForwardBy(base::TimeDelta::FromHours(1));
base::OnceCallback<void(bool)> assert_disabled_callback = base::BindOnce(
[](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); });
// The permission status should be obtained from the server again, and it's
// now disabled.
IsSwaaNacAccountEnabled(std::move(assert_disabled_callback));
task_environment_.RunUntilIdle();
}
TEST_F(FlocIdProviderUnitTest, EventLogging) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kFlocIdComputedEventLogging);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment