Commit c408dc77 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Do not show client side previews when effective connection type is OFFLINE

This does not affect OFFLINE preview type.

Change-Id: Ia7dbec31a7eb1434fc1b46bc6895eab5d32f9086
Bug: 838969
Reviewed-on: https://chromium-review.googlesource.com/1039160
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555624}
parent 8643cea3
...@@ -255,9 +255,17 @@ bool PreviewsIOData::ShouldAllowPreviewAtECT( ...@@ -255,9 +255,17 @@ bool PreviewsIOData::ShouldAllowPreviewAtECT(
net::EFFECTIVE_CONNECTION_TYPE_LAST) { net::EFFECTIVE_CONNECTION_TYPE_LAST) {
net::NetworkQualityEstimator* network_quality_estimator = net::NetworkQualityEstimator* network_quality_estimator =
request.context()->network_quality_estimator(); request.context()->network_quality_estimator();
if (!network_quality_estimator || const net::EffectiveConnectionType observed_effective_connection_type =
network_quality_estimator->GetEffectiveConnectionType() < network_quality_estimator
net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { ? network_quality_estimator->GetEffectiveConnectionType()
: net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
// Network quality estimator may sometimes return effective connection type
// as offline when the Android APIs incorrectly return device connectivity
// as null. See https://crbug.com/838969. So, we do not trigger previews
// when |observed_effective_connection_type| is
// net::EFFECTIVE_CONNECTION_TYPE_OFFLINE.
if (observed_effective_connection_type <=
net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) {
LogPreviewDecisionMade( LogPreviewDecisionMade(
PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, request.url(), PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, request.url(),
base::Time::Now(), type, std::move(passed_reasons), page_id); base::Time::Now(), type, std::move(passed_reasons), page_id);
...@@ -266,7 +274,7 @@ bool PreviewsIOData::ShouldAllowPreviewAtECT( ...@@ -266,7 +274,7 @@ bool PreviewsIOData::ShouldAllowPreviewAtECT(
passed_reasons.push_back( passed_reasons.push_back(
PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE); PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE);
if (network_quality_estimator->GetEffectiveConnectionType() > if (observed_effective_connection_type >
effective_connection_type_threshold) { effective_connection_type_threshold) {
LogPreviewDecisionMade(PreviewsEligibilityReason::NETWORK_NOT_SLOW, LogPreviewDecisionMade(PreviewsEligibilityReason::NETWORK_NOT_SLOW,
request.url(), base::Time::Now(), type, request.url(), base::Time::Now(), type,
......
...@@ -553,15 +553,35 @@ TEST_F(PreviewsIODataTest, TestAllowOffline) { ...@@ -553,15 +553,35 @@ TEST_F(PreviewsIODataTest, TestAllowOffline) {
scoped_feature_list.InitAndEnableFeature(features::kPreviews); scoped_feature_list.InitAndEnableFeature(features::kPreviews);
InitializeUIService(); InitializeUIService();
network_quality_estimator()->set_effective_connection_type( const struct {
net::EFFECTIVE_CONNECTION_TYPE_2G); net::EffectiveConnectionType effective_connection_type;
bool expected_offline_allowed;
} tests[] = {
{net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, false},
{net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, false},
{net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, true},
{net::EFFECTIVE_CONNECTION_TYPE_2G, true},
{net::EFFECTIVE_CONNECTION_TYPE_3G, false},
};
for (const auto& test : tests) {
network_quality_estimator()->set_effective_connection_type(
test.effective_connection_type);
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
EXPECT_TRUE( EXPECT_EQ(
io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE)); test.expected_offline_allowed,
histogram_tester.ExpectUniqueSample( io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE))
"Previews.EligibilityReason.Offline", << " effective_connection_type=" << test.effective_connection_type;
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); if (test.expected_offline_allowed) {
histogram_tester.ExpectUniqueSample(
"Previews.EligibilityReason.Offline",
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1);
} else {
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.Offline",
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 0);
}
}
} }
TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenFeatureDisabled) { TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenFeatureDisabled) {
...@@ -632,17 +652,39 @@ TEST_F(PreviewsIODataTest, ClientLoFiAllowed) { ...@@ -632,17 +652,39 @@ TEST_F(PreviewsIODataTest, ClientLoFiAllowed) {
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G,
params::EffectiveConnectionTypeThresholdForClientLoFi()); params::EffectiveConnectionTypeThresholdForClientLoFi());
network_quality_estimator()->set_effective_connection_type(
net::EFFECTIVE_CONNECTION_TYPE_2G);
base::HistogramTester histogram_tester; const struct {
EXPECT_TRUE(io_data()->ShouldAllowPreviewAtECT( net::EffectiveConnectionType effective_connection_type;
*CreateRequest(), PreviewsType::LOFI, bool expected_client_lofi_allowed;
params::EffectiveConnectionTypeThresholdForClientLoFi(), } tests[] = {
params::GetBlackListedHostsForClientLoFiFieldTrial())); {net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, false},
histogram_tester.ExpectUniqueSample( {net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, false},
"Previews.EligibilityReason.LoFi", {net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, true},
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); {net::EFFECTIVE_CONNECTION_TYPE_2G, true},
{net::EFFECTIVE_CONNECTION_TYPE_3G, false},
};
for (const auto& test : tests) {
network_quality_estimator()->set_effective_connection_type(
test.effective_connection_type);
base::HistogramTester histogram_tester;
EXPECT_EQ(test.expected_client_lofi_allowed,
io_data()->ShouldAllowPreviewAtECT(
*CreateRequest(), PreviewsType::LOFI,
params::EffectiveConnectionTypeThresholdForClientLoFi(),
params::GetBlackListedHostsForClientLoFiFieldTrial()))
<< " effective_connection_type=" << test.effective_connection_type;
if (test.expected_client_lofi_allowed) {
histogram_tester.ExpectUniqueSample(
"Previews.EligibilityReason.LoFi",
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1);
} else {
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.LoFi",
static_cast<int>(PreviewsEligibilityReason::ALLOWED), 0);
}
}
} }
TEST_F(PreviewsIODataTest, MissingHostDisallowed) { TEST_F(PreviewsIODataTest, MissingHostDisallowed) {
...@@ -759,20 +801,43 @@ TEST_F(PreviewsIODataTest, NoScriptAllowedByFeature) { ...@@ -759,20 +801,43 @@ TEST_F(PreviewsIODataTest, NoScriptAllowedByFeature) {
{features::kPreviews, features::kNoScriptPreviews}, {}); {features::kPreviews, features::kNoScriptPreviews}, {});
InitializeUIService(); InitializeUIService();
network_quality_estimator()->set_effective_connection_type( const struct {
net::EFFECTIVE_CONNECTION_TYPE_2G); net::EffectiveConnectionType effective_connection_type;
bool expected_noscript_allowed;
} tests[] = {
{net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, false},
{net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, false},
{net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, true},
{net::EFFECTIVE_CONNECTION_TYPE_2G, true},
{net::EFFECTIVE_CONNECTION_TYPE_3G, false},
};
base::HistogramTester histogram_tester; for (const auto& test : tests) {
EXPECT_TRUE(io_data()->ShouldAllowPreviewAtECT( network_quality_estimator()->set_effective_connection_type(
*CreateHttpsRequest(), PreviewsType::NOSCRIPT, test.effective_connection_type);
previews::params::GetECTThresholdForPreview(
previews::PreviewsType::NOSCRIPT), base::HistogramTester histogram_tester;
std::vector<std::string>())); EXPECT_EQ(test.expected_noscript_allowed,
histogram_tester.ExpectUniqueSample( io_data()->ShouldAllowPreviewAtECT(
"Previews.EligibilityReason.NoScript", *CreateHttpsRequest(), PreviewsType::NOSCRIPT,
static_cast<int>( previews::params::GetECTThresholdForPreview(
PreviewsEligibilityReason::ALLOWED_WITHOUT_OPTIMIZATION_HINTS), previews::PreviewsType::NOSCRIPT),
1); std::vector<std::string>()))
<< " effective_connection_type=" << test.effective_connection_type;
if (test.expected_noscript_allowed) {
histogram_tester.ExpectUniqueSample(
"Previews.EligibilityReason.NoScript",
static_cast<int>(
PreviewsEligibilityReason::ALLOWED_WITHOUT_OPTIMIZATION_HINTS),
1);
} else {
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.NoScript",
static_cast<int>(
PreviewsEligibilityReason::ALLOWED_WITHOUT_OPTIMIZATION_HINTS),
0);
}
}
} }
TEST_F(PreviewsIODataTest, NoScriptAllowedByFeatureWithWhitelist) { TEST_F(PreviewsIODataTest, NoScriptAllowedByFeatureWithWhitelist) {
......
...@@ -33,9 +33,12 @@ enum EffectiveConnectionType { ...@@ -33,9 +33,12 @@ enum EffectiveConnectionType {
// Effective connection type reported when the network quality is unknown. // Effective connection type reported when the network quality is unknown.
EFFECTIVE_CONNECTION_TYPE_UNKNOWN = 0, EFFECTIVE_CONNECTION_TYPE_UNKNOWN = 0,
// Effective connection type reported when the Internet is unreachable, either // Effective connection type reported when the Internet is unreachable
// because the device does not have a connection or because the // because the device does not have a connection (as reported by underlying
// connection is too slow to be usable. // platform APIs). Note that due to rare but potential bugs in the platform
// APIs, it is possible that effective connection type is reported as
// EFFECTIVE_CONNECTION_TYPE_OFFLINE. Callers must use caution when using
// acting on this.
EFFECTIVE_CONNECTION_TYPE_OFFLINE, EFFECTIVE_CONNECTION_TYPE_OFFLINE,
// Effective connection type reported when the network has the quality of a // Effective connection type reported when the network has the quality of a
......
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