Commit 0e2f7cb7 authored by Yao Xiao's avatar Yao Xiao Committed by Commit Bot

Update the floc computation scheduling pattern

Before: Schedule the computation events using a RepeatingTimer and use a
session number to address potential race conditions.

After: Use a flag |floc_computation_in_progress_| to guard the process
from ComputeFloc to OnComputeFlocCompleted. Incoming computation will be
ignored if there’s one ongoing. After each OnComputeFlocCompleted, a
ComputeFloc event will be scheduled 24 hours later / override the
existing scheduled event.

Why: The new pattern would be more suitable for a future plan where more
computation triggers are introduced (e.g. kHistoryDelete). In that case,
the caller can simply call
ComputeFloc(ComputeFlocTrigger::kHistoryDelete) without needing to reset
the timer.


Bug: 1062736
Change-Id: Ic128262272c71be3bc8998bb192ff7c7b615faa8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2359394
Commit-Queue: Yao Xiao <yaoxia@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799547}
parent d8d68438
...@@ -278,7 +278,7 @@ IN_PROC_BROWSER_TEST_F(FlocIdProviderWithCustomizedServicesBrowserTest, ...@@ -278,7 +278,7 @@ IN_PROC_BROWSER_TEST_F(FlocIdProviderWithCustomizedServicesBrowserTest,
EXPECT_EQ(GetFlocId().ToDebugHeaderValue(), FlocId().ToDebugHeaderValue()); EXPECT_EQ(GetFlocId().ToDebugHeaderValue(), FlocId().ToDebugHeaderValue());
// Turn on sync-history to trigger the start of the 1st floc session. // Turn on sync-history to trigger the 1st floc computation.
sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All()); sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All());
sync_service()->FireStateChanged(); sync_service()->FireStateChanged();
...@@ -313,7 +313,7 @@ IN_PROC_BROWSER_TEST_F(FlocIdProviderWithCustomizedServicesBrowserTest, ...@@ -313,7 +313,7 @@ IN_PROC_BROWSER_TEST_F(FlocIdProviderWithCustomizedServicesBrowserTest,
EXPECT_EQ(GetFlocId().ToDebugHeaderValue(), FlocId().ToDebugHeaderValue()); EXPECT_EQ(GetFlocId().ToDebugHeaderValue(), FlocId().ToDebugHeaderValue());
// Turn on sync-history to trigger the start of the 1st floc session. // Turn on sync-history to trigger the 1st floc computation.
sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All()); sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All());
sync_service()->FireStateChanged(); sync_service()->FireStateChanged();
......
...@@ -25,7 +25,7 @@ namespace federated_learning { ...@@ -25,7 +25,7 @@ namespace federated_learning {
namespace { namespace {
constexpr size_t kMinHistoryDomainSizeToReportFlocId = 1; constexpr size_t kMinHistoryDomainSizeToReportFlocId = 1;
constexpr base::TimeDelta kFlocSessionRenewInterval = constexpr base::TimeDelta kFlocScheduledUpdateInterval =
base::TimeDelta::FromDays(1); base::TimeDelta::FromDays(1);
constexpr int kQueryHistoryWindowInDays = 7; constexpr int kQueryHistoryWindowInDays = 7;
...@@ -48,9 +48,7 @@ FlocIdProviderImpl::FlocIdProviderImpl( ...@@ -48,9 +48,7 @@ FlocIdProviderImpl::FlocIdProviderImpl(
FlocIdProviderImpl::~FlocIdProviderImpl() = default; FlocIdProviderImpl::~FlocIdProviderImpl() = default;
void FlocIdProviderImpl::NotifyFlocIdUpdated() { void FlocIdProviderImpl::NotifyFlocUpdated(ComputeFlocTrigger trigger) {
DCHECK(floc_session_count_ > 0);
if (!base::FeatureList::IsEnabled(features::kFlocIdComputedEventLogging)) if (!base::FeatureList::IsEnabled(features::kFlocIdComputedEventLogging))
return; return;
...@@ -60,9 +58,9 @@ void FlocIdProviderImpl::NotifyFlocIdUpdated() { ...@@ -60,9 +58,9 @@ void FlocIdProviderImpl::NotifyFlocIdUpdated() {
specifics->mutable_floc_id_computed_event(); specifics->mutable_floc_id_computed_event();
sync_pb::UserEventSpecifics_FlocIdComputed_EventTrigger event_trigger = sync_pb::UserEventSpecifics_FlocIdComputed_EventTrigger event_trigger =
(floc_session_count_ == 1u) (trigger == ComputeFlocTrigger::kBrowserStart)
? sync_pb::UserEventSpecifics::FlocIdComputed::NEW ? sync_pb::UserEventSpecifics_FlocIdComputed_EventTrigger_NEW
: sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED; : sync_pb::UserEventSpecifics_FlocIdComputed_EventTrigger_REFRESHED;
floc_id_computed_event->set_event_trigger(event_trigger); floc_id_computed_event->set_event_trigger(event_trigger);
...@@ -86,7 +84,7 @@ bool FlocIdProviderImpl::AreThirdPartyCookiesAllowed() { ...@@ -86,7 +84,7 @@ bool FlocIdProviderImpl::AreThirdPartyCookiesAllowed() {
} }
void FlocIdProviderImpl::IsSwaaNacAccountEnabled( void FlocIdProviderImpl::IsSwaaNacAccountEnabled(
CanComputeFlocIdCallback callback) { CanComputeFlocCallback callback) {
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation = net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation =
net::DefinePartialNetworkTrafficAnnotation( net::DefinePartialNetworkTrafficAnnotation(
"floc_id_provider_impl", "floc_remote_permission_service", "floc_id_provider_impl", "floc_remote_permission_service",
...@@ -127,29 +125,59 @@ void FlocIdProviderImpl::Shutdown() { ...@@ -127,29 +125,59 @@ void FlocIdProviderImpl::Shutdown() {
} }
void FlocIdProviderImpl::OnStateChanged(syncer::SyncService* sync_service) { void FlocIdProviderImpl::OnStateChanged(syncer::SyncService* sync_service) {
if (floc_session_count_ > 0) if (first_floc_computation_triggered_)
return; return;
if (!IsSyncHistoryEnabled()) if (!IsSyncHistoryEnabled())
return; return;
CalculateFloc(); ComputeFloc(ComputeFlocTrigger::kBrowserStart);
}
void FlocIdProviderImpl::ComputeFloc(ComputeFlocTrigger trigger) {
DCHECK_NE(trigger == ComputeFlocTrigger::kBrowserStart,
first_floc_computation_triggered_);
floc_session_start_timer_.Start( DCHECK(trigger != ComputeFlocTrigger::kBrowserStart ||
FROM_HERE, kFlocSessionRenewInterval, !floc_computation_in_progress_);
base::BindRepeating(&FlocIdProviderImpl::CalculateFloc,
weak_ptr_factory_.GetWeakPtr())); // Skip computing as long as there's one still in progress.
if (floc_computation_in_progress_)
return;
floc_computation_in_progress_ = true;
first_floc_computation_triggered_ = true;
auto compute_floc_completed_callback =
base::BindOnce(&FlocIdProviderImpl::OnComputeFlocCompleted,
weak_ptr_factory_.GetWeakPtr(), trigger);
CheckCanComputeFloc(
base::BindOnce(&FlocIdProviderImpl::OnCheckCanComputeFlocCompleted,
weak_ptr_factory_.GetWeakPtr(),
std::move(compute_floc_completed_callback)));
} }
void FlocIdProviderImpl::CalculateFloc() { void FlocIdProviderImpl::OnComputeFlocCompleted(ComputeFlocTrigger trigger,
floc_session_count_ += 1; FlocId floc_id) {
CheckCanComputeFlocId( DCHECK(floc_computation_in_progress_);
base::BindOnce(&FlocIdProviderImpl::OnCheckCanComputeFlocIdCompleted, floc_computation_in_progress_ = false;
weak_ptr_factory_.GetWeakPtr()));
if (floc_id_ != floc_id) {
floc_id_ = floc_id;
NotifyFlocUpdated(trigger);
}
// Abandon the scheduled task if any, and schedule a new compute-floc task
// that is |kFlocScheduledUpdateInterval| from now.
compute_floc_timer_.Start(
FROM_HERE, kFlocScheduledUpdateInterval,
base::BindOnce(&FlocIdProviderImpl::ComputeFloc,
weak_ptr_factory_.GetWeakPtr(),
ComputeFlocTrigger::kScheduledUpdate));
} }
void FlocIdProviderImpl::CheckCanComputeFlocId( void FlocIdProviderImpl::CheckCanComputeFloc(CanComputeFlocCallback callback) {
CanComputeFlocIdCallback callback) {
if (!IsSyncHistoryEnabled() || !AreThirdPartyCookiesAllowed()) { if (!IsSyncHistoryEnabled() || !AreThirdPartyCookiesAllowed()) {
std::move(callback).Run(false); std::move(callback).Run(false);
return; return;
...@@ -158,19 +186,17 @@ void FlocIdProviderImpl::CheckCanComputeFlocId( ...@@ -158,19 +186,17 @@ void FlocIdProviderImpl::CheckCanComputeFlocId(
IsSwaaNacAccountEnabled(std::move(callback)); IsSwaaNacAccountEnabled(std::move(callback));
} }
void FlocIdProviderImpl::OnCheckCanComputeFlocIdCompleted( void FlocIdProviderImpl::OnCheckCanComputeFlocCompleted(
ComputeFlocCompletedCallback callback,
bool can_compute_floc) { bool can_compute_floc) {
if (!can_compute_floc) { if (!can_compute_floc) {
if (floc_id_.IsValid()) { std::move(callback).Run(FlocId());
floc_id_ = FlocId();
NotifyFlocIdUpdated();
}
return; return;
} }
GetRecentlyVisitedURLs( GetRecentlyVisitedURLs(
base::BindOnce(&FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted, base::BindOnce(&FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted,
weak_ptr_factory_.GetWeakPtr(), floc_session_count_)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
void FlocIdProviderImpl::GetRecentlyVisitedURLs( void FlocIdProviderImpl::GetRecentlyVisitedURLs(
...@@ -184,12 +210,8 @@ void FlocIdProviderImpl::GetRecentlyVisitedURLs( ...@@ -184,12 +210,8 @@ void FlocIdProviderImpl::GetRecentlyVisitedURLs(
} }
void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted( void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted(
size_t floc_session_count, ComputeFlocCompletedCallback callback,
history::QueryResults results) { history::QueryResults results) {
DCHECK_LE(floc_session_count, floc_session_count_);
if (floc_session_count < floc_session_count_)
return;
std::unordered_set<std::string> domains; std::unordered_set<std::string> domains;
for (const history::URLResult& url_result : results) { for (const history::URLResult& url_result : results) {
if (!url_result.publicly_routable()) if (!url_result.publicly_routable())
...@@ -204,10 +226,7 @@ void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted( ...@@ -204,10 +226,7 @@ void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted(
? FlocId::CreateFromHistory(domains) ? FlocId::CreateFromHistory(domains)
: FlocId(); : FlocId();
if (floc_id_ != floc_id) { std::move(callback).Run(floc_id);
floc_id_ = floc_id;
NotifyFlocIdUpdated();
}
} }
} // namespace federated_learning } // namespace federated_learning
...@@ -25,19 +25,31 @@ namespace federated_learning { ...@@ -25,19 +25,31 @@ namespace federated_learning {
class FlocRemotePermissionService; class FlocRemotePermissionService;
// A service that regularly computes the floc id and logs it in a user event. // A service that regularly computes the floc id and logs it in a user event. A
// computed floc can be in either a valid or invalid state, based on whether all
// the prerequisites are met:
// 1) Sync & sync-history are enabled.
// 2) 3rd party cookies are NOT blocked.
// 3) Supplemental Web and App Activity is enabled.
// 4) Supplemental Ad Personalization is enabled.
// 5) The account type is NOT a child account.
// //
// A floc session starts when sync & sync-history is first enabled. We validate // When all the prerequisites are met, the floc will be computed by sim-hashing
// the following conditions in sequence before computing the floc id: user // navigation URL domains in the last 7 days; otherwise, an invalid floc will be
// doesn’t block 3rd party cookies; all 3 of swaa & nac & account_type are // given.
// enabled; the user has visited at least 1 domain with publicly routable ip in //
// the past 7 days. Every 24 hours, it'll go over the conditions above again // The floc will be first computed after sync & sync-history are enabled. After
// (including the sync & sync-history check), and reset or recompute the id // each computation, another computation will be scheduled 24 hours later.
// accordingly.
class FlocIdProviderImpl : public FlocIdProvider, class FlocIdProviderImpl : public FlocIdProvider,
public syncer::SyncServiceObserver { public syncer::SyncServiceObserver {
public: public:
using CanComputeFlocIdCallback = base::OnceCallback<void(bool)>; enum class ComputeFlocTrigger {
kBrowserStart,
kScheduledUpdate,
};
using CanComputeFlocCallback = base::OnceCallback<void(bool)>;
using ComputeFlocCompletedCallback = base::OnceCallback<void(FlocId)>;
using GetRecentlyVisitedURLsCallback = using GetRecentlyVisitedURLsCallback =
history::HistoryService::QueryHistoryCallback; history::HistoryService::QueryHistoryCallback;
...@@ -53,10 +65,10 @@ class FlocIdProviderImpl : public FlocIdProvider, ...@@ -53,10 +65,10 @@ class FlocIdProviderImpl : public FlocIdProvider,
protected: protected:
// protected virtual for testing. // protected virtual for testing.
virtual void NotifyFlocIdUpdated(); virtual void NotifyFlocUpdated(ComputeFlocTrigger trigger);
virtual bool IsSyncHistoryEnabled(); virtual bool IsSyncHistoryEnabled();
virtual bool AreThirdPartyCookiesAllowed(); virtual bool AreThirdPartyCookiesAllowed();
virtual void IsSwaaNacAccountEnabled(CanComputeFlocIdCallback callback); virtual void IsSwaaNacAccountEnabled(CanComputeFlocCallback callback);
private: private:
friend class FlocIdProviderUnitTest; friend class FlocIdProviderUnitTest;
...@@ -68,17 +80,20 @@ class FlocIdProviderImpl : public FlocIdProvider, ...@@ -68,17 +80,20 @@ class FlocIdProviderImpl : public FlocIdProvider,
// syncer::SyncServiceObserver: // syncer::SyncServiceObserver:
void OnStateChanged(syncer::SyncService* sync_service) override; void OnStateChanged(syncer::SyncService* sync_service) override;
void CalculateFloc(); void ComputeFloc(ComputeFlocTrigger trigger);
void OnComputeFlocCompleted(ComputeFlocTrigger trigger, FlocId floc_id);
void CheckCanComputeFlocId(CanComputeFlocIdCallback callback); void CheckCanComputeFloc(CanComputeFlocCallback callback);
void OnCheckCanComputeFlocIdCompleted(bool can_compute_floc); void OnCheckCanComputeFlocCompleted(ComputeFlocCompletedCallback callback,
bool can_compute_floc);
void GetRecentlyVisitedURLs(GetRecentlyVisitedURLsCallback callback); void GetRecentlyVisitedURLs(GetRecentlyVisitedURLsCallback callback);
void OnGetRecentlyVisitedURLsCompleted(size_t floc_session_count, void OnGetRecentlyVisitedURLsCompleted(ComputeFlocCompletedCallback callback,
history::QueryResults results); history::QueryResults results);
FlocId floc_id_; FlocId floc_id_;
size_t floc_session_count_ = 0; bool floc_computation_in_progress_ = false;
bool first_floc_computation_triggered_ = false;
syncer::SyncService* sync_service_; syncer::SyncService* sync_service_;
scoped_refptr<content_settings::CookieSettings> cookie_settings_; scoped_refptr<content_settings::CookieSettings> cookie_settings_;
...@@ -89,8 +104,8 @@ class FlocIdProviderImpl : public FlocIdProvider, ...@@ -89,8 +104,8 @@ class FlocIdProviderImpl : public FlocIdProvider,
// Used for the async tasks querying the HistoryService. // Used for the async tasks querying the HistoryService.
base::CancelableTaskTracker history_task_tracker_; base::CancelableTaskTracker history_task_tracker_;
// The timer used to start the floc session at regular intervals. // The timer used to schedule a floc computation.
base::RepeatingTimer floc_session_start_timer_; base::OneShotTimer compute_floc_timer_;
base::WeakPtrFactory<FlocIdProviderImpl> weak_ptr_factory_{this}; base::WeakPtrFactory<FlocIdProviderImpl> weak_ptr_factory_{this};
}; };
......
...@@ -29,16 +29,17 @@ class MockFlocIdProvider : public FlocIdProviderImpl { ...@@ -29,16 +29,17 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
public: public:
using FlocIdProviderImpl::FlocIdProviderImpl; using FlocIdProviderImpl::FlocIdProviderImpl;
void NotifyFlocIdUpdated() override { void NotifyFlocUpdated(
FlocIdProviderImpl::ComputeFlocTrigger trigger) override {
++floc_update_notification_count_; ++floc_update_notification_count_;
FlocIdProviderImpl::NotifyFlocIdUpdated(); FlocIdProviderImpl::NotifyFlocUpdated(trigger);
} }
bool AreThirdPartyCookiesAllowed() override { bool AreThirdPartyCookiesAllowed() override {
return third_party_cookies_allowed_; return third_party_cookies_allowed_;
} }
void IsSwaaNacAccountEnabled(CanComputeFlocIdCallback callback) override { void IsSwaaNacAccountEnabled(CanComputeFlocCallback callback) override {
std::move(callback).Run(swaa_nac_account_enabled_); std::move(callback).Run(swaa_nac_account_enabled_);
} }
...@@ -90,15 +91,20 @@ class FlocIdProviderUnitTest : public testing::Test { ...@@ -90,15 +91,20 @@ class FlocIdProviderUnitTest : public testing::Test {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
void CheckCanComputeFlocId( void CheckCanComputeFloc(
FlocIdProviderImpl::CanComputeFlocIdCallback callback) { FlocIdProviderImpl::CanComputeFlocCallback callback) {
floc_id_provider_->CheckCanComputeFlocId(std::move(callback)); floc_id_provider_->CheckCanComputeFloc(std::move(callback));
} }
void OnGetRecentlyVisitedURLsCompleted(size_t floc_session_count, void OnGetRecentlyVisitedURLsCompleted(
history::QueryResults results) { FlocIdProviderImpl::ComputeFlocTrigger trigger,
floc_id_provider_->OnGetRecentlyVisitedURLsCompleted(floc_session_count, history::QueryResults results) {
std::move(results)); auto compute_floc_completed_callback =
base::BindOnce(&FlocIdProviderImpl::OnComputeFlocCompleted,
base::Unretained(floc_id_provider_.get()), trigger);
floc_id_provider_->OnGetRecentlyVisitedURLsCompleted(
std::move(compute_floc_completed_callback), std::move(results));
} }
FlocId floc_id() const { return floc_id_provider_->floc_id_; } FlocId floc_id() const { return floc_id_provider_->floc_id_; }
...@@ -107,12 +113,21 @@ class FlocIdProviderUnitTest : public testing::Test { ...@@ -107,12 +113,21 @@ class FlocIdProviderUnitTest : public testing::Test {
floc_id_provider_->floc_id_ = floc_id; floc_id_provider_->floc_id_ = floc_id;
} }
size_t floc_session_count() const { bool floc_computation_in_progress() const {
return floc_id_provider_->floc_session_count_; return floc_id_provider_->floc_computation_in_progress_;
}
void set_floc_computation_in_progress(bool floc_computation_in_progress) {
floc_id_provider_->floc_computation_in_progress_ =
floc_computation_in_progress;
} }
void set_floc_session_count(size_t count) { bool first_floc_computation_triggered() const {
floc_id_provider_->floc_session_count_ = count; return floc_id_provider_->first_floc_computation_triggered_;
}
void set_first_floc_computation_triggered(bool triggered) {
floc_id_provider_->first_floc_computation_triggered_ = triggered;
} }
protected: protected:
...@@ -140,13 +155,13 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -140,13 +155,13 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Expect that the floc session hasn't started, as the floc_id_provider hasn't // Expect that the floc computation hasn't started, as the floc_id_provider
// been notified about state of the sync_service. // hasn't been notified about state of the sync_service.
ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_FALSE(floc_id().IsValid()); ASSERT_FALSE(floc_id().IsValid());
ASSERT_EQ(0u, floc_session_count()); ASSERT_FALSE(first_floc_computation_triggered());
// Trigger the start of the 1st floc session. // Trigger the 1st floc computation.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
test_sync_service_->FireStateChanged(); test_sync_service_->FireStateChanged();
...@@ -158,7 +173,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -158,7 +173,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
ASSERT_TRUE(floc_id().IsValid()); ASSERT_TRUE(floc_id().IsValid());
ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(), ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(),
floc_id().ToDebugHeaderValue()); floc_id().ToDebugHeaderValue());
ASSERT_EQ(1u, floc_session_count()); ASSERT_TRUE(first_floc_computation_triggered());
// Advance the clock by 1 day. Expect a floc id update notification, as // Advance the clock by 1 day. Expect a floc id update notification, as
// there's no history in the last 7 days so the id has been reset to empty. // there's no history in the last 7 days so the id has been reset to empty.
...@@ -166,7 +181,6 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -166,7 +181,6 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
ASSERT_EQ(2u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(2u, floc_id_provider_->floc_update_notification_count());
ASSERT_FALSE(floc_id().IsValid()); ASSERT_FALSE(floc_id().IsValid());
ASSERT_EQ(2u, floc_session_count());
} }
TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
...@@ -181,13 +195,13 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -181,13 +195,13 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Expect that the floc session hasn't started, as the floc_id_provider hasn't // Expect that the floc computation hasn't started, as the floc_id_provider
// been notified about state of the sync_service. // hasn't been notified about state of the sync_service.
ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_FALSE(floc_id().IsValid()); ASSERT_FALSE(floc_id().IsValid());
ASSERT_EQ(0u, floc_session_count()); ASSERT_FALSE(first_floc_computation_triggered());
// Trigger the start of the 1st floc session. // Trigger the 1st floc computation.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
test_sync_service_->FireStateChanged(); test_sync_service_->FireStateChanged();
...@@ -195,9 +209,9 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -195,9 +209,9 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Expect no floc id update notification, as there is no qualified history // Expect no floc id update notification, as there is no qualified history
// entry. However, the 1st session should already have started. // entry. However, the 1st computation should already have started.
ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(1u, floc_session_count()); ASSERT_TRUE(first_floc_computation_triggered());
// Add a history entry with a timestamp 6 days back from now. // Add a history entry with a timestamp 6 days back from now.
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(6); add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(6);
...@@ -208,7 +222,6 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -208,7 +222,6 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
task_environment_.FastForwardBy(base::TimeDelta::FromHours(23)); task_environment_.FastForwardBy(base::TimeDelta::FromHours(23));
ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(1u, floc_session_count());
// Advance the clock by 1 hour. Expect a floc id update notification, as the // Advance the clock by 1 hour. Expect a floc id update notification, as the
// refresh time is reached and there's a valid history entry in the last 7 // refresh time is reached and there's a valid history entry in the last 7
...@@ -219,7 +232,6 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -219,7 +232,6 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
ASSERT_TRUE(floc_id().IsValid()); ASSERT_TRUE(floc_id().IsValid());
ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(), ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(),
floc_id().ToDebugHeaderValue()); floc_id().ToDebugHeaderValue());
ASSERT_EQ(2u, floc_session_count());
} }
TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) { TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) {
...@@ -234,7 +246,7 @@ TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) { ...@@ -234,7 +246,7 @@ TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Trigger the start of the 1st floc session. // Trigger the 1st floc computation.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
test_sync_service_->FireStateChanged(); test_sync_service_->FireStateChanged();
...@@ -251,27 +263,27 @@ TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) { ...@@ -251,27 +263,27 @@ TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) {
ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
} }
TEST_F(FlocIdProviderUnitTest, CheckCanComputeFlocId_Success) { TEST_F(FlocIdProviderUnitTest, CheckCanComputeFloc_Success) {
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
base::OnceCallback<void(bool)> cb = base::BindOnce( base::OnceCallback<void(bool)> cb = base::BindOnce(
[](bool can_compute_floc) { ASSERT_TRUE(can_compute_floc); }); [](bool can_compute_floc) { ASSERT_TRUE(can_compute_floc); });
CheckCanComputeFlocId(std::move(cb)); CheckCanComputeFloc(std::move(cb));
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
TEST_F(FlocIdProviderUnitTest, CheckCanComputeFlocId_Failure_SyncDisabled) { TEST_F(FlocIdProviderUnitTest, CheckCanComputeFloc_Failure_SyncDisabled) {
base::OnceCallback<void(bool)> cb = base::BindOnce( base::OnceCallback<void(bool)> cb = base::BindOnce(
[](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); }); [](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); });
CheckCanComputeFlocId(std::move(cb)); CheckCanComputeFloc(std::move(cb));
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
TEST_F(FlocIdProviderUnitTest, TEST_F(FlocIdProviderUnitTest,
CheckCanComputeFlocId_Failure_BlockThirdPartyCookies) { CheckCanComputeFloc_Failure_BlockThirdPartyCookies) {
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
...@@ -280,12 +292,12 @@ TEST_F(FlocIdProviderUnitTest, ...@@ -280,12 +292,12 @@ TEST_F(FlocIdProviderUnitTest,
base::OnceCallback<void(bool)> cb = base::BindOnce( base::OnceCallback<void(bool)> cb = base::BindOnce(
[](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); }); [](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); });
CheckCanComputeFlocId(std::move(cb)); CheckCanComputeFloc(std::move(cb));
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
TEST_F(FlocIdProviderUnitTest, TEST_F(FlocIdProviderUnitTest,
CheckCanComputeFlocId_Failure_SwaaNacAccountDisabled) { CheckCanComputeFloc_Failure_SwaaNacAccountDisabled) {
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
...@@ -294,7 +306,7 @@ TEST_F(FlocIdProviderUnitTest, ...@@ -294,7 +306,7 @@ TEST_F(FlocIdProviderUnitTest,
base::OnceCallback<void(bool)> cb = base::BindOnce( base::OnceCallback<void(bool)> cb = base::BindOnce(
[](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); }); [](bool can_compute_floc) { ASSERT_FALSE(can_compute_floc); });
CheckCanComputeFlocId(std::move(cb)); CheckCanComputeFloc(std::move(cb));
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
} }
...@@ -302,9 +314,9 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -302,9 +314,9 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kFlocIdComputedEventLogging); feature_list.InitAndEnableFeature(features::kFlocIdComputedEventLogging);
set_floc_session_count(1u);
set_floc_id(FlocId(12345ULL)); set_floc_id(FlocId(12345ULL));
floc_id_provider_->NotifyFlocIdUpdated(); floc_id_provider_->NotifyFlocUpdated(
FlocIdProviderImpl::ComputeFlocTrigger::kBrowserStart);
ASSERT_EQ(1u, fake_user_event_service_->GetRecordedUserEvents().size()); ASSERT_EQ(1u, fake_user_event_service_->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& specifics1 = const sync_pb::UserEventSpecifics& specifics1 =
...@@ -318,9 +330,9 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -318,9 +330,9 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
event1.event_trigger()); event1.event_trigger());
EXPECT_EQ(12345ULL, event1.floc_id()); EXPECT_EQ(12345ULL, event1.floc_id());
set_floc_session_count(2u);
set_floc_id(FlocId(999ULL)); set_floc_id(FlocId(999ULL));
floc_id_provider_->NotifyFlocIdUpdated(); floc_id_provider_->NotifyFlocUpdated(
FlocIdProviderImpl::ComputeFlocTrigger::kScheduledUpdate);
ASSERT_EQ(2u, fake_user_event_service_->GetRecordedUserEvents().size()); ASSERT_EQ(2u, fake_user_event_service_->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& specifics2 = const sync_pb::UserEventSpecifics& specifics2 =
...@@ -335,7 +347,8 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -335,7 +347,8 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
EXPECT_EQ(999ULL, event2.floc_id()); EXPECT_EQ(999ULL, event2.floc_id());
set_floc_id(FlocId()); set_floc_id(FlocId());
floc_id_provider_->NotifyFlocIdUpdated(); floc_id_provider_->NotifyFlocUpdated(
FlocIdProviderImpl::ComputeFlocTrigger::kScheduledUpdate);
ASSERT_EQ(3u, fake_user_event_service_->GetRecordedUserEvents().size()); ASSERT_EQ(3u, fake_user_event_service_->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& specifics3 = const sync_pb::UserEventSpecifics& specifics3 =
...@@ -356,8 +369,12 @@ TEST_F(FlocIdProviderUnitTest, HistoryEntriesWithPrivateIP) { ...@@ -356,8 +369,12 @@ TEST_F(FlocIdProviderUnitTest, HistoryEntriesWithPrivateIP) {
{history::URLResult(GURL("https://a.test"), {history::URLResult(GURL("https://a.test"),
base::Time::Now() - base::TimeDelta::FromDays(1))}); base::Time::Now() - base::TimeDelta::FromDays(1))});
set_floc_session_count(1u); set_first_floc_computation_triggered(true);
OnGetRecentlyVisitedURLsCompleted(1u, std::move(query_results)); set_floc_computation_in_progress(true);
OnGetRecentlyVisitedURLsCompleted(
FlocIdProviderImpl::ComputeFlocTrigger::kBrowserStart,
std::move(query_results));
ASSERT_FALSE(floc_id().IsValid()); ASSERT_FALSE(floc_id().IsValid());
} }
...@@ -379,8 +396,12 @@ TEST_F(FlocIdProviderUnitTest, MultipleHistoryEntries) { ...@@ -379,8 +396,12 @@ TEST_F(FlocIdProviderUnitTest, MultipleHistoryEntries) {
history::QueryResults query_results; history::QueryResults query_results;
query_results.SetURLResults(std::move(url_results)); query_results.SetURLResults(std::move(url_results));
set_floc_session_count(1u); set_first_floc_computation_triggered(true);
OnGetRecentlyVisitedURLsCompleted(1u, std::move(query_results)); set_floc_computation_in_progress(true);
OnGetRecentlyVisitedURLsCompleted(
FlocIdProviderImpl::ComputeFlocTrigger::kBrowserStart,
std::move(query_results));
ASSERT_EQ( ASSERT_EQ(
FlocId::CreateFromHistory({"a.test", "b.test"}).ToDebugHeaderValue(), FlocId::CreateFromHistory({"a.test", "b.test"}).ToDebugHeaderValue(),
...@@ -398,7 +419,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -398,7 +419,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Trigger the start of the 1st floc session. // Trigger the 1st floc computation.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
test_sync_service_->FireStateChanged(); test_sync_service_->FireStateChanged();
...@@ -409,7 +430,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -409,7 +430,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(), ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(),
floc_id().ToDebugHeaderValue()); floc_id().ToDebugHeaderValue());
ASSERT_EQ(1u, floc_session_count());
// Turn off sync. // Turn off sync.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
...@@ -421,7 +441,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -421,7 +441,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
ASSERT_EQ(2u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(2u, floc_id_provider_->floc_update_notification_count());
ASSERT_FALSE(floc_id().IsValid()); ASSERT_FALSE(floc_id().IsValid());
ASSERT_EQ(2u, floc_session_count());
// Turn on sync. // Turn on sync.
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
...@@ -434,7 +453,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -434,7 +453,6 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
ASSERT_EQ(3u, floc_id_provider_->floc_update_notification_count()); ASSERT_EQ(3u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(), ASSERT_EQ(FlocId::CreateFromHistory({domain}).ToDebugHeaderValue(),
floc_id().ToDebugHeaderValue()); floc_id().ToDebugHeaderValue());
ASSERT_EQ(3u, floc_session_count());
} }
} // namespace federated_learning } // namespace federated_learning
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