Commit 6673c997 authored by Doug Arnett's avatar Doug Arnett Committed by Commit Bot

Surfaces the max_ect_trigger value in PreviewsHints::IsWhitelisted

This is a step toward supporting Slow Page Triggering as may be indicated
in the optmization guide hints. A follow-on CL will use this surfaced
ECT threshold.

Bug: 895581
Change-Id: Ic8cca4c03eb1700223a83980e9376e7612197290
Reviewed-on: https://chromium-review.googlesource.com/c/1289150Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Doug Arnett <dougarnett@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600934}
parent 984fa474
......@@ -111,8 +111,7 @@ enum class PreviewsOptimizationFilterStatus {
};
// Returns base::nullopt if |optimization_type| can't be converted.
base::Optional<PreviewsType>
ConvertProtoOptimizationTypeToPreviewsOptimizationType(
base::Optional<PreviewsType> ConvertProtoOptimizationTypeToPreviewsType(
optimization_guide::proto::OptimizationType optimization_type) {
switch (optimization_type) {
case optimization_guide::proto::TYPE_UNSPECIFIED:
......@@ -126,6 +125,30 @@ ConvertProtoOptimizationTypeToPreviewsOptimizationType(
}
}
net::EffectiveConnectionType ConvertProtoEffectiveConnectionType(
optimization_guide::proto::EffectiveConnectionType proto_ect) {
switch (proto_ect) {
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_UNKNOWN:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_OFFLINE:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_2G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_3G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G;
case optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_4G:
return net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G;
}
}
// Returns whether any features using page hints are enabled.
bool ShouldProcessPageHints() {
return previews::params::IsResourceLoadingHintsEnabled();
......@@ -230,7 +253,7 @@ std::unique_ptr<PreviewsHints> PreviewsHints::CreateFromConfig(
continue;
}
base::Optional<PreviewsType> previews_type =
ConvertProtoOptimizationTypeToPreviewsOptimizationType(
ConvertProtoOptimizationTypeToPreviewsType(
optimization.optimization_type());
if (!previews_type.has_value()) {
continue;
......@@ -263,7 +286,7 @@ std::unique_ptr<PreviewsHints> PreviewsHints::CreateFromConfig(
continue;
}
base::Optional<PreviewsType> previews_type =
ConvertProtoOptimizationTypeToPreviewsOptimizationType(
ConvertProtoOptimizationTypeToPreviewsType(
optimization.optimization_type());
if (!previews_type ||
......@@ -310,7 +333,7 @@ void PreviewsHints::ParseOptimizationFilters(
const optimization_guide::proto::Configuration& config) {
for (const auto blacklist : config.optimization_blacklists()) {
base::Optional<PreviewsType> previews_type =
ConvertProtoOptimizationTypeToPreviewsOptimizationType(
ConvertProtoOptimizationTypeToPreviewsType(
blacklist.optimization_type());
if (previews_type == PreviewsType::LITE_PAGE_REDIRECT &&
previews::params::IsLitePageServerPreviewsEnabled() &&
......@@ -403,16 +426,19 @@ void PreviewsHints::Initialize() {
}
}
bool PreviewsHints::IsWhitelisted(const GURL& url,
PreviewsType type,
int* out_inflation_percent) const {
bool PreviewsHints::IsWhitelisted(
const GURL& url,
PreviewsType type,
int* out_inflation_percent,
net::EffectiveConnectionType* out_ect_threshold) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!url.has_host())
return false;
return IsWhitelistedAtTopLevel(url, type, out_inflation_percent) ||
IsWhitelistedInPageHints(url, type, out_inflation_percent);
IsWhitelistedInPageHints(url, type, out_inflation_percent,
out_ect_threshold);
}
bool PreviewsHints::IsWhitelistedAtTopLevel(const GURL& url,
......@@ -450,9 +476,11 @@ bool PreviewsHints::IsWhitelistedAtTopLevel(const GURL& url,
return false;
}
bool PreviewsHints::IsWhitelistedInPageHints(const GURL& url,
PreviewsType type,
int* out_inflation_percent) const {
bool PreviewsHints::IsWhitelistedInPageHints(
const GURL& url,
PreviewsType type,
int* out_inflation_percent,
net::EffectiveConnectionType* out_ect_threshold) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!hint_cache_)
......@@ -475,14 +503,19 @@ bool PreviewsHints::IsWhitelistedInPageHints(const GURL& url,
for (const auto& optimization :
matched_page_hint->whitelisted_optimizations()) {
if (ConvertProtoOptimizationTypeToPreviewsOptimizationType(
if (ConvertProtoOptimizationTypeToPreviewsType(
optimization.optimization_type()) == type) {
if (IsDisabledExperimentalOptimization(optimization)) {
// This is an experimental optimization that is not enabled so continue
// in case there is a non-experimental one.
continue;
}
// Found whitelisted optimization.
*out_inflation_percent = optimization.inflation_percent();
if (matched_page_hint->has_max_ect_trigger()) {
*out_ect_threshold = ConvertProtoEffectiveConnectionType(
matched_page_hint->max_ect_trigger());
}
return true;
}
}
......
......@@ -49,13 +49,14 @@ class PreviewsHints {
void Initialize();
// Whether the URL is whitelisted for the given previews type. If so,
// |out_inflation_percent| will be populated if metadata is available for it.
// This first checks the top-level whitelist and, if not whitelisted there,
// it will check the HintCache for having a loaded, matching PageHint that
// whitelists it.
// |out_inflation_percent| and |out_ect_threshold| will be populated if
// metadata is available for them. This first checks the top-level whitelist
// and, if not whitelisted there, it will check the HintCache for having a
// loaded, matching PageHint that whitelists it.
bool IsWhitelisted(const GURL& url,
PreviewsType type,
int* out_inflation_percent) const;
int* out_inflation_percent,
net::EffectiveConnectionType* out_ect_threshold) const;
// Whether the URL is blacklisted for the given previews type.
bool IsBlacklisted(const GURL& url, PreviewsType type) const;
......@@ -86,11 +87,13 @@ class PreviewsHints {
PreviewsType type,
int* out_inflation_percent) const;
// Returns whether |url| is whitelisted in the page hints contained within
// |hint_cache_|. If it is, then |out_inflation_percent| will be populated if
// metadata is available for it.
bool IsWhitelistedInPageHints(const GURL& url,
PreviewsType type,
int* out_inflation_percent) const;
// |hint_cache_|. If it is, then |out_inflation_percent| and
// |out_ect_threshold| will be populated if metadata is available for them.
bool IsWhitelistedInPageHints(
const GURL& url,
PreviewsType type,
int* out_inflation_percent,
net::EffectiveConnectionType* out_ect_threshold) const;
// Parses optimization filters from |config| and populates corresponding
// supported blacklists in this object.
......
......@@ -278,6 +278,61 @@ TEST_F(PreviewsHintsTest, ParseConfigWithTooLargeBlacklist) {
GURL("https://black.com/path"), PreviewsType::LITE_PAGE_REDIRECT));
}
TEST_F(PreviewsHintsTest, IsWhitelistedOutParams) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeature(features::kResourceLoadingHints);
optimization_guide::proto::Configuration config;
optimization_guide::proto::Hint* hint1 = config.add_hints();
hint1->set_key("somedomain.org");
hint1->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
// Page hint for "/has_inflation_percent/"
optimization_guide::proto::PageHint* page_hint1 = hint1->add_page_hints();
page_hint1->set_page_pattern("/has_inflation_percent/");
optimization_guide::proto::Optimization* optimization_with_inflation_percent =
page_hint1->add_whitelisted_optimizations();
optimization_with_inflation_percent->set_inflation_percent(55);
optimization_with_inflation_percent->set_optimization_type(
optimization_guide::proto::RESOURCE_LOADING);
// Page hint for "/has_max_ect_trigger/"
optimization_guide::proto::PageHint* page_hint2 = hint1->add_page_hints();
page_hint2->set_page_pattern("/has_max_ect_trigger/");
page_hint2->set_max_ect_trigger(
optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_4G);
optimization_guide::proto::Optimization*
optimization_without_inflation_percent =
page_hint2->add_whitelisted_optimizations();
optimization_without_inflation_percent->set_optimization_type(
optimization_guide::proto::RESOURCE_LOADING);
ParseConfig(config);
// Verify optimization providing inflation_percent.
int inflation_percent = 0;
net::EffectiveConnectionType ect_threshold =
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/has_inflation_percent/"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent,
&ect_threshold));
EXPECT_EQ(55, inflation_percent);
EXPECT_EQ(net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
ect_threshold);
// Verify page hint providing ECT trigger.
inflation_percent = 0;
ect_threshold =
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/has_max_ect_trigger/"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent,
&ect_threshold));
EXPECT_EQ(0, inflation_percent);
EXPECT_EQ(net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G,
ect_threshold);
}
TEST_F(PreviewsHintsTest, IsWhitelistedForExperimentalPreview) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeature(features::kResourceLoadingHints);
......@@ -290,6 +345,9 @@ TEST_F(PreviewsHintsTest, IsWhitelistedForExperimentalPreview) {
// Page hint for "/experimental_preview/"
optimization_guide::proto::PageHint* page_hint1 = hint1->add_page_hints();
page_hint1->set_page_pattern("/experimental_preview/");
page_hint1->set_max_ect_trigger(
optimization_guide::proto::EffectiveConnectionType::
EFFECTIVE_CONNECTION_TYPE_3G);
// First add experimental PageHint optimization.
optimization_guide::proto::Optimization* experimental_optimization =
page_hint1->add_whitelisted_optimizations();
......@@ -316,12 +374,17 @@ TEST_F(PreviewsHintsTest, IsWhitelistedForExperimentalPreview) {
ParseConfig(config);
// Verify default resource hint whitelisted (via inflation_percent).
int inflation_percent;
int inflation_percent = 0;
net::EffectiveConnectionType ect_threshold =
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/experimental_preview/"
"experimental_resource.js"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent));
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent,
&ect_threshold));
EXPECT_EQ(33, inflation_percent);
EXPECT_EQ(net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G,
ect_threshold);
// Now enable the experiment and verify experimental resource hint chosen.
{
......@@ -330,11 +393,16 @@ TEST_F(PreviewsHintsTest, IsWhitelistedForExperimentalPreview) {
features::kOptimizationHintsExperiments,
{{"experiment_name", "foo_experiment"}});
int inflation_percent;
net::EffectiveConnectionType ect_threshold =
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_2G;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/experimental_preview/"
"experimental_resource.js"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent));
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent,
&ect_threshold));
EXPECT_EQ(99, inflation_percent);
EXPECT_EQ(net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G,
ect_threshold);
}
}
......
......@@ -39,7 +39,9 @@ bool PreviewsOptimizationGuide::IsWhitelisted(PreviewsUserData* previews_data,
return false;
int inflation_percent = 0;
if (!hints_->IsWhitelisted(url, type, &inflation_percent))
net::EffectiveConnectionType ect_threshold =
params::GetECTThresholdForPreview(type);
if (!hints_->IsWhitelisted(url, type, &inflation_percent, &ect_threshold))
return false;
if (inflation_percent != 0 && previews_data)
......
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