Commit 95d20683 authored by Yao Xiao's avatar Yao Xiao Committed by Commit Bot

FLoC event logging condition update: log only if the floc has changed

Previously, we log the floc when it's computed and has a valid value.
Change it to be: log whenever the status changes, i.e. valid->invalid,
invalid->valid, or valid->different-valid.

Why:
1) This makes it more natural to understand.
2) This allows the server to know when the floc is disabled. This is not
necessary as of now, because currently the only updating trigger is
the scheduled update, so if the server sees no event after 24 hours
since the last event, the server could infer that the floc is disabled.
However, if we introduce new updating trigger like history-delete, then
the server would have insufficient knowledge. Thus, switch to the new
logging condition.


Bug: 1062736
Change-Id: I8796c2af89a4de5c5f8831e1c00693224927fcd5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2357613
Commit-Queue: Yao Xiao <yaoxia@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799294}
parent 63daf563
...@@ -48,14 +48,11 @@ FlocIdProviderImpl::FlocIdProviderImpl( ...@@ -48,14 +48,11 @@ FlocIdProviderImpl::FlocIdProviderImpl(
FlocIdProviderImpl::~FlocIdProviderImpl() = default; FlocIdProviderImpl::~FlocIdProviderImpl() = default;
void FlocIdProviderImpl::NotifyFlocIdUpdated( void FlocIdProviderImpl::NotifyFlocIdUpdated() {
EventLoggingAction event_logging_action) {
DCHECK(floc_session_count_ > 0); DCHECK(floc_session_count_ > 0);
if (event_logging_action != EventLoggingAction::kAllow || if (!base::FeatureList::IsEnabled(features::kFlocIdComputedEventLogging))
!base::FeatureList::IsEnabled(features::kFlocIdComputedEventLogging)) {
return; return;
}
auto specifics = std::make_unique<sync_pb::UserEventSpecifics>(); auto specifics = std::make_unique<sync_pb::UserEventSpecifics>();
...@@ -68,7 +65,9 @@ void FlocIdProviderImpl::NotifyFlocIdUpdated( ...@@ -68,7 +65,9 @@ void FlocIdProviderImpl::NotifyFlocIdUpdated(
: sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED; : sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED;
floc_id_computed_event->set_event_trigger(event_trigger); floc_id_computed_event->set_event_trigger(event_trigger);
floc_id_computed_event->set_floc_id(floc_id_.ToUint64());
if (floc_id_.IsValid())
floc_id_computed_event->set_floc_id(floc_id_.ToUint64());
user_event_service_->RecordUserEvent(std::move(specifics)); user_event_service_->RecordUserEvent(std::move(specifics));
} }
...@@ -164,7 +163,7 @@ void FlocIdProviderImpl::OnCheckCanComputeFlocIdCompleted( ...@@ -164,7 +163,7 @@ void FlocIdProviderImpl::OnCheckCanComputeFlocIdCompleted(
if (!can_compute_floc) { if (!can_compute_floc) {
if (floc_id_.IsValid()) { if (floc_id_.IsValid()) {
floc_id_ = FlocId(); floc_id_ = FlocId();
NotifyFlocIdUpdated(EventLoggingAction::kDisallow); NotifyFlocIdUpdated();
} }
return; return;
} }
...@@ -201,16 +200,14 @@ void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted( ...@@ -201,16 +200,14 @@ void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted(
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
} }
if (domains.size() < kMinHistoryDomainSizeToReportFlocId) { FlocId floc_id = domains.size() >= kMinHistoryDomainSizeToReportFlocId
if (floc_id_.IsValid()) { ? FlocId::CreateFromHistory(domains)
floc_id_ = FlocId(); : FlocId();
NotifyFlocIdUpdated(EventLoggingAction::kDisallow);
}
return;
}
floc_id_ = FlocId::CreateFromHistory(domains); if (floc_id_ != floc_id) {
NotifyFlocIdUpdated(EventLoggingAction::kAllow); floc_id_ = floc_id;
NotifyFlocIdUpdated();
}
} }
} // namespace federated_learning } // namespace federated_learning
...@@ -41,11 +41,6 @@ class FlocIdProviderImpl : public FlocIdProvider, ...@@ -41,11 +41,6 @@ class FlocIdProviderImpl : public FlocIdProvider,
using GetRecentlyVisitedURLsCallback = using GetRecentlyVisitedURLsCallback =
history::HistoryService::QueryHistoryCallback; history::HistoryService::QueryHistoryCallback;
enum class EventLoggingAction {
kAllow,
kDisallow,
};
FlocIdProviderImpl( FlocIdProviderImpl(
syncer::SyncService* sync_service, syncer::SyncService* sync_service,
scoped_refptr<content_settings::CookieSettings> cookie_settings, scoped_refptr<content_settings::CookieSettings> cookie_settings,
...@@ -58,7 +53,7 @@ class FlocIdProviderImpl : public FlocIdProvider, ...@@ -58,7 +53,7 @@ class FlocIdProviderImpl : public FlocIdProvider,
protected: protected:
// protected virtual for testing. // protected virtual for testing.
virtual void NotifyFlocIdUpdated(EventLoggingAction); virtual void NotifyFlocIdUpdated();
virtual bool IsSyncHistoryEnabled(); virtual bool IsSyncHistoryEnabled();
virtual bool AreThirdPartyCookiesAllowed(); virtual bool AreThirdPartyCookiesAllowed();
virtual void IsSwaaNacAccountEnabled(CanComputeFlocIdCallback callback); virtual void IsSwaaNacAccountEnabled(CanComputeFlocIdCallback callback);
......
...@@ -29,9 +29,9 @@ class MockFlocIdProvider : public FlocIdProviderImpl { ...@@ -29,9 +29,9 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
public: public:
using FlocIdProviderImpl::FlocIdProviderImpl; using FlocIdProviderImpl::FlocIdProviderImpl;
void NotifyFlocIdUpdated(EventLoggingAction event_logging_action) override { void NotifyFlocIdUpdated() override {
++floc_id_notification_count_; ++floc_update_notification_count_;
FlocIdProviderImpl::NotifyFlocIdUpdated(event_logging_action); FlocIdProviderImpl::NotifyFlocIdUpdated();
} }
bool AreThirdPartyCookiesAllowed() override { bool AreThirdPartyCookiesAllowed() override {
...@@ -42,8 +42,8 @@ class MockFlocIdProvider : public FlocIdProviderImpl { ...@@ -42,8 +42,8 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
std::move(callback).Run(swaa_nac_account_enabled_); std::move(callback).Run(swaa_nac_account_enabled_);
} }
size_t floc_id_notification_count() const { size_t floc_update_notification_count() const {
return floc_id_notification_count_; return floc_update_notification_count_;
} }
void set_third_party_cookies_allowed(bool allowed) { void set_third_party_cookies_allowed(bool allowed) {
...@@ -55,7 +55,7 @@ class MockFlocIdProvider : public FlocIdProviderImpl { ...@@ -55,7 +55,7 @@ class MockFlocIdProvider : public FlocIdProviderImpl {
} }
private: private:
size_t floc_id_notification_count_ = 0u; size_t floc_update_notification_count_ = 0u;
bool third_party_cookies_allowed_ = true; bool third_party_cookies_allowed_ = true;
bool swaa_nac_account_enabled_ = true; bool swaa_nac_account_enabled_ = true;
}; };
...@@ -142,7 +142,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -142,7 +142,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
// Expect that the floc session hasn't started, as the floc_id_provider hasn't // Expect that the floc session hasn't started, as the floc_id_provider hasn't
// been notified about state of the sync_service. // been notified about state of the sync_service.
ASSERT_EQ(0u, floc_id_provider_->floc_id_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_EQ(0u, floc_session_count());
...@@ -154,7 +154,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -154,7 +154,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Expect a floc id update notification. // Expect a floc id update notification.
ASSERT_EQ(1u, floc_id_provider_->floc_id_notification_count()); ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
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());
...@@ -164,7 +164,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) { ...@@ -164,7 +164,7 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
// 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.
task_environment_.FastForwardBy(base::TimeDelta::FromDays(1)); task_environment_.FastForwardBy(base::TimeDelta::FromDays(1));
ASSERT_EQ(2u, floc_id_provider_->floc_id_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()); ASSERT_EQ(2u, floc_session_count());
} }
...@@ -183,7 +183,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -183,7 +183,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
// Expect that the floc session hasn't started, as the floc_id_provider hasn't // Expect that the floc session hasn't started, as the floc_id_provider hasn't
// been notified about state of the sync_service. // been notified about state of the sync_service.
ASSERT_EQ(0u, floc_id_provider_->floc_id_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_EQ(0u, floc_session_count());
...@@ -196,7 +196,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -196,7 +196,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
// 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 session should already have started.
ASSERT_EQ(0u, floc_id_provider_->floc_id_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(1u, floc_session_count()); ASSERT_EQ(1u, floc_session_count());
// Add a history entry with a timestamp 6 days back from now. // Add a history entry with a timestamp 6 days back from now.
...@@ -207,7 +207,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -207,7 +207,7 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
// as the id refresh interval is 24 hours. // as the id refresh interval is 24 hours.
task_environment_.FastForwardBy(base::TimeDelta::FromHours(23)); task_environment_.FastForwardBy(base::TimeDelta::FromHours(23));
ASSERT_EQ(0u, floc_id_provider_->floc_id_notification_count()); ASSERT_EQ(0u, floc_id_provider_->floc_update_notification_count());
ASSERT_EQ(1u, floc_session_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
...@@ -215,13 +215,42 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) { ...@@ -215,13 +215,42 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
// days. // days.
task_environment_.FastForwardBy(base::TimeDelta::FromHours(1)); task_environment_.FastForwardBy(base::TimeDelta::FromHours(1));
ASSERT_EQ(1u, floc_id_provider_->floc_id_notification_count()); ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
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()); ASSERT_EQ(2u, floc_session_count());
} }
TEST_F(FlocIdProviderUnitTest, ScheduledUpdateSameFloc_NoNotification) {
std::string domain = "foo.com";
// Add a history entry with a timestamp 2 days back from now.
history::HistoryAddPageArgs add_page_args;
add_page_args.url = GURL(base::StrCat({"https://www.", domain}));
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(2);
add_page_args.publicly_routable = true;
history_service_->AddPage(add_page_args);
task_environment_.RunUntilIdle();
// Trigger the start of the 1st floc session.
test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE);
test_sync_service_->FireStateChanged();
task_environment_.RunUntilIdle();
// Expect a floc id update notification.
ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
// Advance the clock by 1 day. Expect no additional floc id update
// notification, as the floc didn't change.
task_environment_.FastForwardBy(base::TimeDelta::FromDays(1));
ASSERT_EQ(1u, floc_id_provider_->floc_update_notification_count());
}
TEST_F(FlocIdProviderUnitTest, CheckCanComputeFlocId_Success) { TEST_F(FlocIdProviderUnitTest, CheckCanComputeFlocId_Success) {
test_sync_service_->SetTransportState( test_sync_service_->SetTransportState(
syncer::SyncService::TransportState::ACTIVE); syncer::SyncService::TransportState::ACTIVE);
...@@ -275,8 +304,7 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -275,8 +304,7 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
set_floc_session_count(1u); set_floc_session_count(1u);
set_floc_id(FlocId(12345ULL)); set_floc_id(FlocId(12345ULL));
floc_id_provider_->NotifyFlocIdUpdated( floc_id_provider_->NotifyFlocIdUpdated();
FlocIdProviderImpl::EventLoggingAction::kAllow);
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 =
...@@ -292,8 +320,7 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -292,8 +320,7 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
set_floc_session_count(2u); set_floc_session_count(2u);
set_floc_id(FlocId(999ULL)); set_floc_id(FlocId(999ULL));
floc_id_provider_->NotifyFlocIdUpdated( floc_id_provider_->NotifyFlocIdUpdated();
FlocIdProviderImpl::EventLoggingAction::kAllow);
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 =
...@@ -306,6 +333,21 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) { ...@@ -306,6 +333,21 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
EXPECT_EQ(sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED, EXPECT_EQ(sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED,
event2.event_trigger()); event2.event_trigger());
EXPECT_EQ(999ULL, event2.floc_id()); EXPECT_EQ(999ULL, event2.floc_id());
set_floc_id(FlocId());
floc_id_provider_->NotifyFlocIdUpdated();
ASSERT_EQ(3u, fake_user_event_service_->GetRecordedUserEvents().size());
const sync_pb::UserEventSpecifics& specifics3 =
fake_user_event_service_->GetRecordedUserEvents()[2];
EXPECT_EQ(sync_pb::UserEventSpecifics::kFlocIdComputedEvent,
specifics3.event_case());
const sync_pb::UserEventSpecifics_FlocIdComputed& event3 =
specifics3.floc_id_computed_event();
EXPECT_EQ(sync_pb::UserEventSpecifics::FlocIdComputed::REFRESHED,
event3.event_trigger());
EXPECT_FALSE(event3.has_floc_id());
} }
TEST_F(FlocIdProviderUnitTest, HistoryEntriesWithPrivateIP) { TEST_F(FlocIdProviderUnitTest, HistoryEntriesWithPrivateIP) {
...@@ -364,7 +406,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -364,7 +406,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
task_environment_.RunUntilIdle(); task_environment_.RunUntilIdle();
// Expect a floc id update notification. // Expect a floc id update notification.
ASSERT_EQ(1u, floc_id_provider_->floc_id_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()); ASSERT_EQ(1u, floc_session_count());
...@@ -377,7 +419,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -377,7 +419,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
// the sync was turned off so the id has been reset to empty. // the sync was turned off so the id has been reset to empty.
task_environment_.FastForwardBy(base::TimeDelta::FromDays(1)); task_environment_.FastForwardBy(base::TimeDelta::FromDays(1));
ASSERT_EQ(2u, floc_id_provider_->floc_id_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()); ASSERT_EQ(2u, floc_session_count());
...@@ -389,7 +431,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) { ...@@ -389,7 +431,7 @@ TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
// valid floc id. // valid floc id.
task_environment_.FastForwardBy(base::TimeDelta::FromDays(1)); task_environment_.FastForwardBy(base::TimeDelta::FromDays(1));
ASSERT_EQ(3u, floc_id_provider_->floc_id_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()); ASSERT_EQ(3u, floc_session_count());
......
...@@ -45,6 +45,14 @@ bool FlocId::IsValid() const { ...@@ -45,6 +45,14 @@ bool FlocId::IsValid() const {
return id_.has_value(); return id_.has_value();
} }
bool FlocId::operator==(const FlocId& other) const {
return id_ == other.id_;
}
bool FlocId::operator!=(const FlocId& other) const {
return !(*this == other);
}
uint64_t FlocId::ToUint64() const { uint64_t FlocId::ToUint64() const {
DCHECK(id_.has_value()); DCHECK(id_.has_value());
return id_.value(); return id_.value();
......
...@@ -30,6 +30,9 @@ class FlocId { ...@@ -30,6 +30,9 @@ class FlocId {
FlocId& operator=(const FlocId& id); FlocId& operator=(const FlocId& id);
FlocId& operator=(FlocId&& id); FlocId& operator=(FlocId&& id);
bool operator==(const FlocId& other) const;
bool operator!=(const FlocId& other) const;
bool IsValid() const; bool IsValid() const;
uint64_t ToUint64() const; uint64_t ToUint64() const;
......
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