Commit cb64f5a3 authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Do not fetch if all registered opt types are covered by opt filters

Change-Id: I985b9706318daf29b0d030666294d2d464046989
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533733Reviewed-by: default avatarMichael Crouse <mcrouse@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826551}
parent 1b70e5c2
...@@ -1208,11 +1208,25 @@ void OptimizationGuideHintsManager::OnEffectiveConnectionTypeChanged( ...@@ -1208,11 +1208,25 @@ void OptimizationGuideHintsManager::OnEffectiveConnectionTypeChanged(
current_effective_connection_type_ = effective_connection_type; current_effective_connection_type_ = effective_connection_type;
} }
bool OptimizationGuideHintsManager::HasOptimizationTypeToFetchFor() {
if (registered_optimization_types_.empty())
return false;
base::AutoLock lock(optimization_filters_lock_);
for (const auto& optimization_type : registered_optimization_types_) {
if (optimization_types_with_filter_.find(optimization_type) ==
optimization_types_with_filter_.end()) {
return true;
}
}
return false;
}
bool OptimizationGuideHintsManager::IsAllowedToFetchNavigationHints( bool OptimizationGuideHintsManager::IsAllowedToFetchNavigationHints(
const GURL& url) const { const GURL& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (registered_optimization_types_.empty()) if (!HasOptimizationTypeToFetchFor())
return false; return false;
if (!IsUserPermittedToFetchFromRemoteOptimizationGuide(profile_)) if (!IsUserPermittedToFetchFromRemoteOptimizationGuide(profile_))
......
...@@ -324,7 +324,7 @@ class OptimizationGuideHintsManager ...@@ -324,7 +324,7 @@ class OptimizationGuideHintsManager
// Returns true if |this| is allowed to fetch hints at the navigation time for // Returns true if |this| is allowed to fetch hints at the navigation time for
// |url|. // |url|.
bool IsAllowedToFetchNavigationHints(const GURL& url) const; bool IsAllowedToFetchNavigationHints(const GURL& url);
// Loads the hint if available. // Loads the hint if available.
// |callback| is run when the request has finished regardless of whether there // |callback| is run when the request has finished regardless of whether there
...@@ -353,6 +353,11 @@ class OptimizationGuideHintsManager ...@@ -353,6 +353,11 @@ class OptimizationGuideHintsManager
const base::Optional<NavigationPredictorKeyedService::Prediction> const base::Optional<NavigationPredictorKeyedService::Prediction>
prediction) override; prediction) override;
// Returns whether there is an optimization type to fetch for. Will return
// false if no optimization types are registered or if all registered
// optimization types are covered by optimization filters.
bool HasOptimizationTypeToFetchFor();
// Creates a hints fetch for |navigation_handle| if it is allowed. The // Creates a hints fetch for |navigation_handle| if it is allowed. The
// fetch will include the host and URL of the |navigation_handle| if the // fetch will include the host and URL of the |navigation_handle| if the
// associated hints for each are not already in the cache. // associated hints for each are not already in the cache.
......
...@@ -2511,6 +2511,42 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest, HintsFetched_AtSRP_ECT_4G) { ...@@ -2511,6 +2511,42 @@ TEST_F(OptimizationGuideHintsManagerFetchingTest, HintsFetched_AtSRP_ECT_4G) {
"OptimizationGuide.HintsFetcher.GetHintsRequest.UrlCount", 1); "OptimizationGuide.HintsFetcher.GetHintsRequest.UrlCount", 1);
} }
TEST_F(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_RegisteredOptimizationTypes_AllWithOptFilter) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
optimization_guide::switches::kDisableCheckingUserPermissionsForTesting);
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::LITE_PAGE_REDIRECT});
optimization_guide::proto::Configuration config;
optimization_guide::BloomFilter allowlist_bloom_filter(
kDefaultHostBloomFilterNumHashFunctions, kDefaultHostBloomFilterNumBits);
PopulateBloomFilterWithDefaultHost(&allowlist_bloom_filter);
AddBloomFilterToConfig(
optimization_guide::proto::LITE_PAGE_REDIRECT, allowlist_bloom_filter,
kDefaultHostBloomFilterNumHashFunctions, kDefaultHostBloomFilterNumBits,
/*is_allowlist=*/true, &config);
ProcessHints(config, "1.0.0.0");
// Set ECT estimate so fetch 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;
hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
base::DoNothing());
RunUntilIdle();
histogram_tester.ExpectTotalCount(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 0);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.HintsFetcher.GetHintsRequest.UrlCount", 0);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.HintsManager.RaceNavigationFetchAttemptStatus", 0);
}
TEST_F(OptimizationGuideHintsManagerFetchingTest, TEST_F(OptimizationGuideHintsManagerFetchingTest,
HintsFetched_AtNonSRP_ECT_SLOW_2G) { HintsFetched_AtNonSRP_ECT_SLOW_2G) {
base::CommandLine::ForCurrentProcess()->AppendSwitch( base::CommandLine::ForCurrentProcess()->AppendSwitch(
......
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