Commit f2bf6209 authored by Sebastien Marchand's avatar Sebastien Marchand Committed by Commit Bot

LocalDB - Ignore events happening just after backgrounding a tab

Also rename some of the existing params while I'm here to make them more
explicit.

Bug: 1014976
Change-Id: I6fade2127172343f22084b762a500309a418920b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1865599
Commit-Queue: Sébastien Marchand <sebmarchand@chromium.org>
Commit-Queue: François Doray <fdoray@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706960}
parent f45b55f8
...@@ -68,8 +68,8 @@ base::TimeDelta GetLongestObservationWindow() { ...@@ -68,8 +68,8 @@ base::TimeDelta GetLongestObservationWindow() {
base::TimeDelta GetLongestGracePeriod() { base::TimeDelta GetLongestGracePeriod() {
const SiteCharacteristicsDatabaseParams& params = const SiteCharacteristicsDatabaseParams& params =
GetStaticSiteCharacteristicsDatabaseParams(); GetStaticSiteCharacteristicsDatabaseParams();
return std::max({params.title_or_favicon_change_grace_period, return std::max({params.title_or_favicon_change_post_load_grace_period,
params.audio_usage_grace_period}); params.feature_usage_post_background_grace_period});
} }
// Returns the LocalSiteCharacteristicsDataImpl that backs |reader|. // Returns the LocalSiteCharacteristicsDataImpl that backs |reader|.
...@@ -226,8 +226,8 @@ class LocalSiteCharacteristicsDatabaseTest : public InProcessBrowserTest { ...@@ -226,8 +226,8 @@ class LocalSiteCharacteristicsDatabaseTest : public InProcessBrowserTest {
// Background the tab and reload it so the audio will stop playing if it's // Background the tab and reload it so the audio will stop playing if it's
// still playing. // still playing.
GetActiveWebContents()->WasHidden(); GetActiveWebContents()->WasHidden();
test_clock_.Advance( test_clock_.Advance(GetStaticSiteCharacteristicsDatabaseParams()
GetStaticSiteCharacteristicsDatabaseParams().audio_usage_grace_period); .feature_usage_post_background_grace_period);
} }
// Ensure that the current tab is allowed to display non-persistent // Ensure that the current tab is allowed to display non-persistent
...@@ -243,7 +243,7 @@ class LocalSiteCharacteristicsDatabaseTest : public InProcessBrowserTest { ...@@ -243,7 +243,7 @@ class LocalSiteCharacteristicsDatabaseTest : public InProcessBrowserTest {
void ExpireTitleOrFaviconGracePeriod() { void ExpireTitleOrFaviconGracePeriod() {
test_clock_.Advance(GetStaticSiteCharacteristicsDatabaseParams() test_clock_.Advance(GetStaticSiteCharacteristicsDatabaseParams()
.title_or_favicon_change_grace_period); .title_or_favicon_change_post_load_grace_period);
} }
base::SimpleTestTickClock& test_clock() { return test_clock_; } base::SimpleTestTickClock& test_clock() { return test_clock_; }
......
...@@ -259,18 +259,22 @@ bool LocalSiteCharacteristicsWebContentsObserver::ShouldIgnoreFeatureUsageEvent( ...@@ -259,18 +259,22 @@ bool LocalSiteCharacteristicsWebContentsObserver::ShouldIgnoreFeatureUsageEvent(
if (feature_type == FeatureType::kTitleChange || if (feature_type == FeatureType::kTitleChange ||
feature_type == FeatureType::kFaviconChange) { feature_type == FeatureType::kFaviconChange) {
DCHECK(!loaded_time_.is_null()); DCHECK(!loaded_time_.is_null());
if (NowTicks() - loaded_time_ < GetStaticSiteCharacteristicsDatabaseParams() if (NowTicks() - loaded_time_ <
.title_or_favicon_change_grace_period) { GetStaticSiteCharacteristicsDatabaseParams()
return true; .title_or_favicon_change_post_load_grace_period) {
}
} else if (feature_type == FeatureType::kAudioUsage) {
DCHECK(!backgrounded_time_.is_null());
if (NowTicks() - backgrounded_time_ <
GetStaticSiteCharacteristicsDatabaseParams().audio_usage_grace_period) {
return true; return true;
} }
} }
// Ignore events happening shortly after the tab being backgrounded, they're
// usually false positives.
DCHECK(!backgrounded_time_.is_null());
if (NowTicks() - backgrounded_time_ <
GetStaticSiteCharacteristicsDatabaseParams()
.feature_usage_post_background_grace_period) {
return true;
}
return false; return false;
} }
......
...@@ -211,18 +211,13 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest, ...@@ -211,18 +211,13 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest,
web_contents()->WasHidden(); web_contents()->WasHidden();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
// Notification usage events always get forwarded.
EXPECT_CALL(*mock_writer, NotifyUsesNotificationsInBackground());
observer()->OnNonPersistentNotificationCreated();
::testing::Mock::VerifyAndClear(mock_writer);
auto params = GetStaticSiteCharacteristicsDatabaseParams(); auto params = GetStaticSiteCharacteristicsDatabaseParams();
// Title and Favicon should be ignored during the post-loading grace period. // Title and Favicon should be ignored during the post-loading grace period.
observer()->DidUpdateFaviconURL({}); observer()->DidUpdateFaviconURL({});
observer()->TitleWasSet(nullptr); observer()->TitleWasSet(nullptr);
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
test_clock().Advance(params.title_or_favicon_change_grace_period); test_clock().Advance(params.title_or_favicon_change_post_load_grace_period);
EXPECT_CALL(*mock_writer, NotifyUpdatesFaviconInBackground()); EXPECT_CALL(*mock_writer, NotifyUpdatesFaviconInBackground());
observer()->DidUpdateFaviconURL({}); observer()->DidUpdateFaviconURL({});
...@@ -242,14 +237,22 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest, ...@@ -242,14 +237,22 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest,
web_contents()->WasHidden(); web_contents()->WasHidden();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
// Audio usage events should be ignored during the post-background grace // Events should be ignored during the post-background grace period.
// period.
observer()->OnAudioStateChanged(true); observer()->OnAudioStateChanged(true);
observer()->DidUpdateFaviconURL({});
observer()->TitleWasSet(nullptr);
observer()->OnNonPersistentNotificationCreated();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
test_clock().Advance(params.audio_usage_grace_period); test_clock().Advance(params.feature_usage_post_background_grace_period);
EXPECT_CALL(*mock_writer, NotifyUsesAudioInBackground()); EXPECT_CALL(*mock_writer, NotifyUsesAudioInBackground());
EXPECT_CALL(*mock_writer, NotifyUpdatesFaviconInBackground());
EXPECT_CALL(*mock_writer, NotifyUpdatesTitleInBackground());
EXPECT_CALL(*mock_writer, NotifyUsesNotificationsInBackground());
observer()->OnAudioStateChanged(true); observer()->OnAudioStateChanged(true);
observer()->DidUpdateFaviconURL({});
observer()->TitleWasSet(nullptr);
observer()->OnNonPersistentNotificationCreated();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
EXPECT_CALL(*mock_writer, OnDestroy()); EXPECT_CALL(*mock_writer, OnDestroy());
...@@ -296,6 +299,12 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest, ...@@ -296,6 +299,12 @@ TEST_F(LocalSiteCharacteristicsWebContentsObserverTest,
web_contents()->WasHidden(); web_contents()->WasHidden();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
// Events should be ignored during the post-background grace period.
observer()->OnNonPersistentNotificationCreated();
::testing::Mock::VerifyAndClear(mock_writer);
test_clock().Advance(GetStaticSiteCharacteristicsDatabaseParams()
.feature_usage_post_background_grace_period);
EXPECT_CALL(*mock_writer, NotifyUsesNotificationsInBackground()); EXPECT_CALL(*mock_writer, NotifyUsesNotificationsInBackground());
observer()->OnNonPersistentNotificationCreated(); observer()->OnNonPersistentNotificationCreated();
::testing::Mock::VerifyAndClear(mock_writer); ::testing::Mock::VerifyAndClear(mock_writer);
......
...@@ -121,9 +121,9 @@ constexpr base::FeatureParam<int> ...@@ -121,9 +121,9 @@ constexpr base::FeatureParam<int>
constexpr base::FeatureParam<int> constexpr base::FeatureParam<int>
SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow; SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow;
constexpr base::FeatureParam<int> constexpr base::FeatureParam<int>
SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangeGracePeriod; SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangePostLoadGracePeriod;
constexpr base::FeatureParam<int> constexpr base::FeatureParam<int>
SiteCharacteristicsDatabaseParams::kAudioUsageGracePeriod; SiteCharacteristicsDatabaseParams::kFeatureUsagePostBackgroundGracePeriod;
ProactiveTabFreezeAndDiscardParams::ProactiveTabFreezeAndDiscardParams() = ProactiveTabFreezeAndDiscardParams::ProactiveTabFreezeAndDiscardParams() =
default; default;
...@@ -235,12 +235,15 @@ SiteCharacteristicsDatabaseParams GetSiteCharacteristicsDatabaseParams() { ...@@ -235,12 +235,15 @@ SiteCharacteristicsDatabaseParams GetSiteCharacteristicsDatabaseParams() {
SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow
.Get()); .Get());
params.title_or_favicon_change_grace_period = base::TimeDelta::FromSeconds( params.title_or_favicon_change_post_load_grace_period =
SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangeGracePeriod base::TimeDelta::FromSeconds(
.Get()); SiteCharacteristicsDatabaseParams::
kTitleOrFaviconChangePostLoadGracePeriod.Get());
params.audio_usage_grace_period = base::TimeDelta::FromSeconds( params.feature_usage_post_background_grace_period =
SiteCharacteristicsDatabaseParams::kAudioUsageGracePeriod.Get()); base::TimeDelta::FromSeconds(
SiteCharacteristicsDatabaseParams::
kFeatureUsagePostBackgroundGracePeriod.Get());
return params; return params;
} }
......
...@@ -210,12 +210,14 @@ struct SiteCharacteristicsDatabaseParams { ...@@ -210,12 +210,14 @@ struct SiteCharacteristicsDatabaseParams {
static constexpr base::FeatureParam<int> kNotificationsUsageObservationWindow{ static constexpr base::FeatureParam<int> kNotificationsUsageObservationWindow{
&features::kSiteCharacteristicsDatabase, &features::kSiteCharacteristicsDatabase,
"NotificationsUsageObservationWindow", 2 * base::Time::kSecondsPerHour}; "NotificationsUsageObservationWindow", 2 * base::Time::kSecondsPerHour};
static constexpr base::FeatureParam<int> kTitleOrFaviconChangeGracePeriod{ static constexpr base::FeatureParam<int>
&features::kSiteCharacteristicsDatabase, kTitleOrFaviconChangePostLoadGracePeriod{
"TitleOrFaviconChangeGracePeriod", 20 /* 20 seconds */}; &features::kSiteCharacteristicsDatabase,
static constexpr base::FeatureParam<int> kAudioUsageGracePeriod{ "TitleOrFaviconChangePostLoadGracePeriod", 20 /* 20 seconds */};
&features::kSiteCharacteristicsDatabase, "AudioUsageGracePeriod", static constexpr base::FeatureParam<int>
10 /* 10 seconds */}; kFeatureUsagePostBackgroundGracePeriod{
&features::kSiteCharacteristicsDatabase,
"FeatureUsagePostBackgroundGracePeriod", 10 /* 10 seconds */};
// Minimum observation window before considering that this website doesn't // Minimum observation window before considering that this website doesn't
// update its favicon while in background. // update its favicon while in background.
...@@ -233,12 +235,18 @@ struct SiteCharacteristicsDatabaseParams { ...@@ -233,12 +235,18 @@ struct SiteCharacteristicsDatabaseParams {
// change events. It's possible for some site that are loaded in background to // change events. It's possible for some site that are loaded in background to
// use some of these features without this being an attempt to communicate // use some of these features without this being an attempt to communicate
// with the user (e.g. the tab is just really finishing to load). // with the user (e.g. the tab is just really finishing to load).
base::TimeDelta title_or_favicon_change_grace_period; base::TimeDelta title_or_favicon_change_post_load_grace_period;
// The period of time during which we ignore audio usage gets ignored after a // The period of time during which we ignore events after a tab gets
// tab gets backgrounded. It's necessary because there might be a delay // backgrounded. It's necessary because some events might happen shortly after
// between a media request gets initiated and the time the audio actually // backgrounding a tab without this being an attempt to communicate with the
// starts. // user:
base::TimeDelta audio_usage_grace_period; // - There might be a delay between a media request gets initiated and the
// time the audio actually starts.
// - Same-document navigation can cause the title or favicon to change, if
// the user switch tab before this completes this will be recorded as a
// background communication event while in reality it's just a navigation
// event.
base::TimeDelta feature_usage_post_background_grace_period;
}; };
// Gets parameters for the proactive tab discarding feature. This does no // Gets parameters for the proactive tab discarding feature. This does no
......
...@@ -85,8 +85,8 @@ class TabManagerFeaturesTest : public testing::Test { ...@@ -85,8 +85,8 @@ class TabManagerFeaturesTest : public testing::Test {
base::TimeDelta title_update_observation_window, base::TimeDelta title_update_observation_window,
base::TimeDelta audio_usage_observation_window, base::TimeDelta audio_usage_observation_window,
base::TimeDelta notifications_usage_observation_window, base::TimeDelta notifications_usage_observation_window,
base::TimeDelta title_or_favicon_change_grace_period, base::TimeDelta title_or_favicon_change_post_load_grace_period,
base::TimeDelta audio_usage_grace_period) { base::TimeDelta feature_usage_post_background_grace_period) {
SiteCharacteristicsDatabaseParams params = SiteCharacteristicsDatabaseParams params =
GetSiteCharacteristicsDatabaseParams(); GetSiteCharacteristicsDatabaseParams();
...@@ -98,9 +98,10 @@ class TabManagerFeaturesTest : public testing::Test { ...@@ -98,9 +98,10 @@ class TabManagerFeaturesTest : public testing::Test {
params.audio_usage_observation_window); params.audio_usage_observation_window);
EXPECT_EQ(notifications_usage_observation_window, EXPECT_EQ(notifications_usage_observation_window,
params.notifications_usage_observation_window); params.notifications_usage_observation_window);
EXPECT_EQ(title_or_favicon_change_grace_period, EXPECT_EQ(title_or_favicon_change_post_load_grace_period,
params.title_or_favicon_change_grace_period); params.title_or_favicon_change_post_load_grace_period);
EXPECT_EQ(audio_usage_grace_period, params.audio_usage_grace_period); EXPECT_EQ(feature_usage_post_background_grace_period,
params.feature_usage_post_background_grace_period);
} }
void ExpectDefaultProactiveTabFreezeAndDiscardParams() { void ExpectDefaultProactiveTabFreezeAndDiscardParams() {
...@@ -152,11 +153,11 @@ class TabManagerFeaturesTest : public testing::Test { ...@@ -152,11 +153,11 @@ class TabManagerFeaturesTest : public testing::Test {
SiteCharacteristicsDatabaseParams:: SiteCharacteristicsDatabaseParams::
kNotificationsUsageObservationWindow.default_value), kNotificationsUsageObservationWindow.default_value),
base::TimeDelta::FromSeconds( base::TimeDelta::FromSeconds(
SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangeGracePeriod SiteCharacteristicsDatabaseParams::
.default_value), kTitleOrFaviconChangePostLoadGracePeriod.default_value),
base::TimeDelta::FromSeconds( base::TimeDelta::FromSeconds(
SiteCharacteristicsDatabaseParams::kAudioUsageGracePeriod SiteCharacteristicsDatabaseParams::
.default_value)); kFeatureUsagePostBackgroundGracePeriod.default_value));
} }
private: private:
...@@ -286,11 +287,13 @@ TEST_F(TabManagerFeaturesTest, ...@@ -286,11 +287,13 @@ TEST_F(TabManagerFeaturesTest,
SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow
.name, .name,
"abc"); "abc");
SetParam(SiteCharacteristicsDatabaseParams::
kTitleOrFaviconChangePostLoadGracePeriod.name,
"bleh");
SetParam( SetParam(
SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangeGracePeriod.name, SiteCharacteristicsDatabaseParams::kFeatureUsagePostBackgroundGracePeriod
"bleh"); .name,
SetParam(SiteCharacteristicsDatabaseParams::kAudioUsageGracePeriod.name, "!!!");
"!!!");
EnableSiteCharacteristicsDatabase(); EnableSiteCharacteristicsDatabase();
ExpectDefaultSiteCharacteristicsDatabaseParams(); ExpectDefaultSiteCharacteristicsDatabaseParams();
} }
...@@ -308,11 +311,13 @@ TEST_F(TabManagerFeaturesTest, GetSiteCharacteristicsDatabaseParams) { ...@@ -308,11 +311,13 @@ TEST_F(TabManagerFeaturesTest, GetSiteCharacteristicsDatabaseParams) {
SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow SiteCharacteristicsDatabaseParams::kNotificationsUsageObservationWindow
.name, .name,
"3600000"); "3600000");
SetParam(SiteCharacteristicsDatabaseParams::
kTitleOrFaviconChangePostLoadGracePeriod.name,
"42");
SetParam( SetParam(
SiteCharacteristicsDatabaseParams::kTitleOrFaviconChangeGracePeriod.name, SiteCharacteristicsDatabaseParams::kFeatureUsagePostBackgroundGracePeriod
"42"); .name,
SetParam(SiteCharacteristicsDatabaseParams::kAudioUsageGracePeriod.name, "43");
"43");
EnableSiteCharacteristicsDatabase(); EnableSiteCharacteristicsDatabase();
......
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