Commit 41613d64 authored by Robert Ogden's avatar Robert Ogden Committed by Commit Bot

Cap the maximum number of entries in the PreviewsOfflineHelper pref

The max size is specified by variations.

Bug: 914577
Change-Id: Iddc694ee5b6a4abbd3435956df23a586a918b547
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1577931
Commit-Queue: Robert Ogden <robertogden@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653436}
parent 5f5a597f
...@@ -52,8 +52,8 @@ base::Optional<base::Time> TimeFromDictionaryValue(std::string value) { ...@@ -52,8 +52,8 @@ base::Optional<base::Time> TimeFromDictionaryValue(std::string value) {
// Cleans up the given dictionary by removing all stale (expiry has passed) // Cleans up the given dictionary by removing all stale (expiry has passed)
// entries. // entries.
void RemoveStaleEntries(base::DictionaryValue* dict) { void RemoveStaleEntries(base::DictionaryValue* dict) {
// TODO(crbug.com/914577): Cap the size of the pref by deleting the oldest one base::Time earliest_expiry = base::Time::Max();
// above a certain size. std::string earliest_key;
std::vector<std::string> keys_to_delete; std::vector<std::string> keys_to_delete;
for (const auto& iter : dict->DictItems()) { for (const auto& iter : dict->DictItems()) {
// Check for a corrupted value and throw it out if so. // Check for a corrupted value and throw it out if so.
...@@ -73,12 +73,23 @@ void RemoveStaleEntries(base::DictionaryValue* dict) { ...@@ -73,12 +73,23 @@ void RemoveStaleEntries(base::DictionaryValue* dict) {
time.value() + previews::params::OfflinePreviewFreshnessDuration(); time.value() + previews::params::OfflinePreviewFreshnessDuration();
bool is_expired = expiry <= base::Time::Now(); bool is_expired = expiry <= base::Time::Now();
if (is_expired) if (is_expired) {
keys_to_delete.push_back(iter.first); keys_to_delete.push_back(iter.first);
continue;
}
if (expiry < earliest_expiry)
earliest_key = iter.first;
} }
for (const std::string& key : keys_to_delete) for (const std::string& key : keys_to_delete)
dict->RemoveKey(key); dict->RemoveKey(key);
// RemoveStaleEntries is called for every new added page, so it's fine to just
// remove one at a time to keep the pref size below a threshold.
if (dict->DictSize() > previews::params::OfflinePreviewsHelperMaxPrefSize()) {
dict->RemoveKey(earliest_key);
}
} }
} // namespace } // namespace
......
...@@ -216,3 +216,27 @@ TEST_F(PreviewsOfflineHelperTest, TestAddRemovePages) { ...@@ -216,3 +216,27 @@ TEST_F(PreviewsOfflineHelperTest, TestAddRemovePages) {
} }
} }
} }
TEST_F(PreviewsOfflineHelperTest, TestMaxPrefSize) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
previews::features::kOfflinePreviewsFalsePositivePrevention,
{{"max_pref_entries", "1"}});
PreviewsOfflineHelper* helper = NewHelper();
base::Time first = base::Time::Now();
base::Time second = first + base::TimeDelta::FromMinutes(1);
helper->OfflinePageAdded(
nullptr, MakeAddedPageItem("http://test.first.com", "", first));
EXPECT_TRUE(
helper->ShouldAttemptOfflinePreview(GURL("http://test.first.com")));
helper->OfflinePageAdded(
nullptr, MakeAddedPageItem("http://test.second.com", "", second));
EXPECT_FALSE(
helper->ShouldAttemptOfflinePreview(GURL("http://test.first.com")));
EXPECT_TRUE(
helper->ShouldAttemptOfflinePreview(GURL("http://test.second.com")));
}
...@@ -387,6 +387,12 @@ int ResourceLoadingHintsPreviewsInflationBytes() { ...@@ -387,6 +387,12 @@ int ResourceLoadingHintsPreviewsInflationBytes() {
features::kResourceLoadingHints, kResourceLoadingHintsInflationBytes, 0); features::kResourceLoadingHints, kResourceLoadingHintsInflationBytes, 0);
} }
size_t OfflinePreviewsHelperMaxPrefSize() {
return GetFieldTrialParamByFeatureAsInt(
features::kOfflinePreviewsFalsePositivePrevention, "max_pref_entries",
100);
}
} // namespace params } // namespace params
std::string GetStringNameForType(PreviewsType type) { std::string GetStringNameForType(PreviewsType type) {
......
...@@ -195,6 +195,10 @@ int ResourceLoadingHintsPreviewsInflationPercent(); ...@@ -195,6 +195,10 @@ int ResourceLoadingHintsPreviewsInflationPercent();
// bytes to for inflating the original_bytes count. // bytes to for inflating the original_bytes count.
int ResourceLoadingHintsPreviewsInflationBytes(); int ResourceLoadingHintsPreviewsInflationBytes();
// The maximum number of pref entries that should be kept by
// PreviewsOfflineHelper.
size_t OfflinePreviewsHelperMaxPrefSize();
} // namespace params } // namespace params
} // namespace previews } // namespace previews
......
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