Commit 7a218129 authored by Michael Crouse's avatar Michael Crouse Committed by Commit Bot

Only allow hints fetch if there are registered optimization types.

Bug: 1045258
Change-Id: Ieee1048e058beb39f8beca6b04dc993153bfdb67
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2018483
Commit-Queue: Michael Crouse <mcrouse@chromium.org>
Reviewed-by: default avatarSophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735030}
parent 0f7ce8b6
......@@ -541,6 +541,9 @@ void OptimizationGuideHintsManager::ScheduleTopHostsHintsFetch() {
void OptimizationGuideHintsManager::FetchTopHostsHints() {
DCHECK(top_host_provider_);
if (registered_optimization_types_.empty())
return;
std::vector<std::string> top_hosts = top_host_provider_->GetTopHosts();
if (top_hosts.empty())
return;
......@@ -675,6 +678,9 @@ void OptimizationGuideHintsManager::OnPredictionUpdated(
prediction) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (registered_optimization_types_.empty())
return;
if (!prediction.has_value())
return;
......@@ -973,7 +979,7 @@ void OptimizationGuideHintsManager::OnNavigationStartOrRedirect(
void OptimizationGuideHintsManager::MaybeFetchHintsForNavigation(
content::NavigationHandle* navigation_handle) {
if (registered_optimization_types_.size() == 0)
if (registered_optimization_types_.empty())
return;
if (!IsAllowedToFetchNavigationHints(navigation_handle->GetURL()))
......
......@@ -148,6 +148,8 @@ class OptimizationGuideHintsManager
private:
FRIEND_TEST_ALL_PREFIXES(OptimizationGuideHintsManagerTest, IsGoogleURL);
FRIEND_TEST_ALL_PREFIXES(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_AtSRP_NoRegisteredOptimizationTypes);
FRIEND_TEST_ALL_PREFIXES(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_AtSRP_ECT_SLOW_2G);
FRIEND_TEST_ALL_PREFIXES(OptimizationGuideHintsManagerFetchingTest,
......
......@@ -1908,6 +1908,9 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest,
SetUserPermissions(/*data_saver_enabled=*/true, /*has_seen_infobar=*/false);
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithHostHints));
InitializeWithDefaultConfig("1.0.0");
......@@ -1928,6 +1931,9 @@ TEST_F(
SetUserPermissions(/*data_saver_enabled=*/true, /*has_seen_infobar=*/true);
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithHostHints));
InitializeWithDefaultConfig("1.0.0");
......@@ -1938,6 +1944,26 @@ TEST_F(
EXPECT_TRUE(hints_fetcher()->hints_fetched());
}
TEST_F(OptimizationGuideHintsManagerFetchingTest,
NoRegisteredOptimizationTypesAndHintsFetchNotAttempted) {
std::unique_ptr<FakeTopHostProvider> top_host_provider =
std::make_unique<FakeTopHostProvider>(
std::vector<std::string>({"example1.com", "example2.com"}));
SetUserPermissions(/*data_saver_enabled=*/true, /*has_seen_infobar=*/true);
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithHostHints));
InitializeWithDefaultConfig("1.0.0");
// Force timer to expire and schedule a hints fetch but the fetch is not made.
MoveClockForwardBy(base::TimeDelta::FromSeconds(kTestFetchRetryDelaySecs));
EXPECT_EQ(0, top_host_provider->get_num_top_hosts_called());
EXPECT_FALSE(hints_fetcher()->hints_fetched());
}
TEST_F(OptimizationGuideHintsManagerFetchingTest,
HintsFetcherEnabledNoHostsToFetch) {
SetUserPermissions(/*data_saver_enabled=*/true, /*has_seen_infobar=*/true);
......@@ -1945,6 +1971,9 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest,
std::make_unique<FakeTopHostProvider>(std::vector<std::string>({}));
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithHostHints));
InitializeWithDefaultConfig("1.0.0");
......@@ -1963,6 +1992,8 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest,
std::vector<std::string>({"example1.com", "example2.com"}));
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithNoHints));
InitializeWithDefaultConfig("1.0.0");
......@@ -1988,6 +2019,8 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest, HintsFetcherTimerRetryDelay) {
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchFailed));
InitializeWithDefaultConfig("1.0.0");
......@@ -2016,6 +2049,8 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest,
// Force hints fetch scheduling.
CreateServiceAndHintsManager(/*optimization_types_at_initialization=*/{},
top_host_provider.get());
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
hints_manager()->SetHintsFetcherForTesting(
BuildTestHintsFetcher(HintsFetcherEndState::kFetchSuccessWithHostHints));
InitializeWithDefaultConfig("1.0.0");
......@@ -2059,6 +2094,27 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest,
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 1, 1);
}
TEST_F(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_AtSRP_NoRegisteredOptimizationTypes) {
InitializeWithDefaultConfig("1.0.0.0");
// Set ECT estimate so hint is activated.
hints_manager()->OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_without_hints());
base::HistogramTester histogram_tester;
std::vector<GURL> sorted_predicted_urls;
sorted_predicted_urls.push_back(GURL("https://foo.com/"));
NavigationPredictorKeyedService::Prediction prediction(
nullptr, GURL("https://www.google.com/"), sorted_predicted_urls);
hints_manager()->OnPredictionUpdated(prediction);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 0);
}
TEST_F(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_AtSRP_ECT_SLOW_2G_DuplicatesRemoved) {
hints_manager()->RegisterOptimizationTypes(
......
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