Commit 94eb8290 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Optimization guide: Fetch hints from remote service

Fetch hints from remote service at the time of navigation.
Hints are fetched only if connection is slow.

Change-Id: I37e13072b17e1adfe9b3365e2dca04ae9bdfc246
Bug: 933898
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829474
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarSophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702116}
parent 23301d6f
...@@ -490,6 +490,8 @@ void OptimizationGuideHintsManager::OnHintsFetched( ...@@ -490,6 +490,8 @@ void OptimizationGuideHintsManager::OnHintsFetched(
OnTopHostsHintsFetched(std::move(get_hints_response)); OnTopHostsHintsFetched(std::move(get_hints_response));
return; return;
case optimization_guide::proto::CONTEXT_PAGE_NAVIGATION: case optimization_guide::proto::CONTEXT_PAGE_NAVIGATION:
OnPageNavigationHintsFetched(std::move(get_hints_response));
return;
case optimization_guide::proto::CONTEXT_UNSPECIFIED: case optimization_guide::proto::CONTEXT_UNSPECIFIED:
NOTREACHED(); NOTREACHED();
} }
...@@ -517,6 +519,19 @@ void OptimizationGuideHintsManager::OnTopHostsHintsFetched( ...@@ -517,6 +519,19 @@ void OptimizationGuideHintsManager::OnTopHostsHintsFetched(
} }
} }
void OptimizationGuideHintsManager::OnPageNavigationHintsFetched(
base::Optional<std::unique_ptr<optimization_guide::proto::GetHintsResponse>>
get_hints_response) {
if (!get_hints_response.has_value() || !get_hints_response.value())
return;
hint_cache_->UpdateFetchedHints(
std::move(*get_hints_response), clock_->Now() + kUpdateFetchedHintsDelay,
base::BindOnce(
&OptimizationGuideHintsManager::OnFetchedPageNavigationHintsStored,
ui_weak_ptr_factory_.GetWeakPtr()));
}
void OptimizationGuideHintsManager::OnFetchedTopHostsHintsStored() { void OptimizationGuideHintsManager::OnFetchedTopHostsHintsStored() {
LOCAL_HISTOGRAM_BOOLEAN("OptimizationGuide.FetchedHints.Stored", true); LOCAL_HISTOGRAM_BOOLEAN("OptimizationGuide.FetchedHints.Stored", true);
...@@ -526,6 +541,11 @@ void OptimizationGuideHintsManager::OnFetchedTopHostsHintsStored() { ...@@ -526,6 +541,11 @@ void OptimizationGuideHintsManager::OnFetchedTopHostsHintsStored() {
&OptimizationGuideHintsManager::ScheduleTopHostsHintsFetch); &OptimizationGuideHintsManager::ScheduleTopHostsHintsFetch);
} }
void OptimizationGuideHintsManager::OnFetchedPageNavigationHintsStored() {
for (const auto& url : navigation_urls_last_fetched_real_time_)
LoadHintForURL(url, base::DoNothing());
}
base::Time OptimizationGuideHintsManager::GetLastHintsFetchAttemptTime() const { base::Time OptimizationGuideHintsManager::GetLastHintsFetchAttemptTime() const {
return base::Time::FromDeltaSinceWindowsEpoch( return base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(pref_service_->GetInt64( base::TimeDelta::FromMicroseconds(pref_service_->GetInt64(
...@@ -561,6 +581,14 @@ void OptimizationGuideHintsManager::LoadHintForNavigation( ...@@ -561,6 +581,14 @@ void OptimizationGuideHintsManager::LoadHintForNavigation(
} }
} }
LoadHintForURL(url, std::move(callback));
}
void OptimizationGuideHintsManager::LoadHintForURL(const GURL& url,
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(url.has_host());
hint_cache_->LoadHint( hint_cache_->LoadHint(
url.host(), url.host(),
base::BindOnce(&OptimizationGuideHintsManager::OnHintLoaded, base::BindOnce(&OptimizationGuideHintsManager::OnHintLoaded,
...@@ -780,6 +808,58 @@ void OptimizationGuideHintsManager::OnEffectiveConnectionTypeChanged( ...@@ -780,6 +808,58 @@ void OptimizationGuideHintsManager::OnEffectiveConnectionTypeChanged(
current_effective_connection_type_ = effective_connection_type; current_effective_connection_type_ = effective_connection_type;
} }
bool OptimizationGuideHintsManager::IsAllowedToFetchNavigationHints(
const GURL& url) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!IsUserPermittedToFetchHints(profile_))
return false;
if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme))
return false;
base::Optional<net::EffectiveConnectionType> ect_max_threshold =
optimization_guide::features::
GetMaxEffectiveConnectionTypeForNavigationHintsFetch();
// If the threshold is unavailable, return early since there is no safe way to
// proceed.
if (!ect_max_threshold.has_value())
return false;
if (current_effective_connection_type_ <
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G ||
current_effective_connection_type_ > ect_max_threshold.value()) {
return false;
}
return true;
}
void OptimizationGuideHintsManager::OnNavigationStartOrRedirect(
content::NavigationHandle* navigation_handle,
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (IsAllowedToFetchNavigationHints(navigation_handle->GetURL())) {
std::vector<std::string> hosts{navigation_handle->GetURL().host()};
navigation_urls_last_fetched_real_time_.clear();
navigation_urls_last_fetched_real_time_.push_back(
navigation_handle->GetURL());
if (!hints_fetcher_) {
hints_fetcher_ = std::make_unique<optimization_guide::HintsFetcher>(
url_loader_factory_,
optimization_guide::features::GetOptimizationGuideServiceURL(),
pref_service_);
}
hints_fetcher_->FetchOptimizationGuideServiceHints(
hosts, optimization_guide::proto::CONTEXT_PAGE_NAVIGATION,
base::BindOnce(&OptimizationGuideHintsManager::OnHintsFetched,
ui_weak_ptr_factory_.GetWeakPtr()));
}
LoadHintForNavigation(navigation_handle, std::move(callback));
}
void OptimizationGuideHintsManager::ClearFetchedHints() { void OptimizationGuideHintsManager::ClearFetchedHints() {
hint_cache_->ClearFetchedHints(); hint_cache_->ClearFetchedHints();
optimization_guide::HintsFetcher::ClearHostsSuccessfullyFetched( optimization_guide::HintsFetcher::ClearHostsSuccessfullyFetched(
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "components/optimization_guide/hints_component_info.h" #include "components/optimization_guide/hints_component_info.h"
#include "components/optimization_guide/optimization_guide_service_observer.h" #include "components/optimization_guide/optimization_guide_service_observer.h"
#include "components/optimization_guide/proto/hints.pb.h" #include "components/optimization_guide/proto/hints.pb.h"
#include "net/nqe/effective_connection_type.h"
#include "services/network/public/cpp/network_quality_tracker.h" #include "services/network/public/cpp/network_quality_tracker.h"
namespace base { namespace base {
...@@ -82,13 +83,6 @@ class OptimizationGuideHintsManager ...@@ -82,13 +83,6 @@ class OptimizationGuideHintsManager
// is called and the corresponding hints have been updated. // is called and the corresponding hints have been updated.
void ListenForNextUpdateForTesting(base::OnceClosure next_update_closure); void ListenForNextUpdateForTesting(base::OnceClosure next_update_closure);
// Loads the hint if available.
// |callback| is run when the request has finished regardless of whether there
// was actually a hint for that load or not. The callback can be used as a
// signal for tests.
void LoadHintForNavigation(content::NavigationHandle* navigation_handle,
base::OnceClosure callback);
// Registers the optimization types that have the potential for hints to be // Registers the optimization types that have the potential for hints to be
// called by consumers of the Optimization Guide. // called by consumers of the Optimization Guide.
void RegisterOptimizationTypes( void RegisterOptimizationTypes(
...@@ -136,6 +130,13 @@ class OptimizationGuideHintsManager ...@@ -136,6 +130,13 @@ class OptimizationGuideHintsManager
void OnEffectiveConnectionTypeChanged( void OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) override; net::EffectiveConnectionType type) override;
// Notifies |this| that a navigation with |navigation_handle| has started.
// |callback| is run when the request has finished regardless of whether there
// was actually a hint for that load or not. The callback can be used as a
// signal for tests.
void OnNavigationStartOrRedirect(content::NavigationHandle* navigation_handle,
base::OnceClosure callback);
private: private:
// Processes the hints component. // Processes the hints component.
// //
...@@ -194,16 +195,29 @@ class OptimizationGuideHintsManager ...@@ -194,16 +195,29 @@ class OptimizationGuideHintsManager
get_hints_response); get_hints_response);
// Called when the hints for the top hosts have been fetched from the remote // Called when the hints for the top hosts have been fetched from the remote
// Optimization Guide Service and are ready for parsing. // Optimization Guide Service and are ready for parsing. This is used when
// fetching hints in batch mode.
void OnTopHostsHintsFetched( void OnTopHostsHintsFetched(
base::Optional< base::Optional<
std::unique_ptr<optimization_guide::proto::GetHintsResponse>> std::unique_ptr<optimization_guide::proto::GetHintsResponse>>
get_hints_response); get_hints_response);
// Called when the hints for a navigation have been fetched from the remote
// Optimization Guide Service and are ready for parsing. This is used when
// fetching hints in real-time.
void OnPageNavigationHintsFetched(
base::Optional<
std::unique_ptr<optimization_guide::proto::GetHintsResponse>>
get_hints_response);
// Called when the fetched hints have been stored in |hint_cache| and are // Called when the fetched hints have been stored in |hint_cache| and are
// ready to be used. // ready to be used. This is used when hints were fetched in batch mode.
void OnFetchedTopHostsHintsStored(); void OnFetchedTopHostsHintsStored();
// Called when the fetched hints have been stored in |hint_cache| and are
// ready to be used. This is used when hints were fetched in real-time.
void OnFetchedPageNavigationHintsStored();
// Returns the time when a hints fetch request was last attempted. // Returns the time when a hints fetch request was last attempted.
base::Time GetLastHintsFetchAttemptTime() const; base::Time GetLastHintsFetchAttemptTime() const;
...@@ -214,6 +228,23 @@ class OptimizationGuideHintsManager ...@@ -214,6 +228,23 @@ class OptimizationGuideHintsManager
void OnHintLoaded(base::OnceClosure callback, void OnHintLoaded(base::OnceClosure callback,
const optimization_guide::proto::Hint* loaded_hint) const; const optimization_guide::proto::Hint* loaded_hint) const;
// Returns true if |this| is allowed to fetch hints at the navigation time for
// |url|.
bool IsAllowedToFetchNavigationHints(const GURL& url) const;
// Loads the hint if available.
// |callback| is run when the request has finished regardless of whether there
// was actually a hint for that load or not. The callback can be used as a
// signal for tests.
void LoadHintForNavigation(content::NavigationHandle* navigation_handle,
base::OnceClosure callback);
// Loads the hint for |url| if available.
// |callback| is run when the request has finished regardless of whether there
// was actually a hint for that |url| or not. The callback can be used as a
// signal for tests.
void LoadHintForURL(const GURL& url, base::OnceClosure callback);
// The OptimizationGuideService that this guide is listening to. Not owned. // The OptimizationGuideService that this guide is listening to. Not owned.
optimization_guide::OptimizationGuideService* const optimization_guide::OptimizationGuideService* const
optimization_guide_service_; optimization_guide_service_;
...@@ -282,6 +313,9 @@ class OptimizationGuideHintsManager ...@@ -282,6 +313,9 @@ class OptimizationGuideHintsManager
// Used in testing to subscribe to an update event in this class. // Used in testing to subscribe to an update event in this class.
base::OnceClosure next_update_closure_; base::OnceClosure next_update_closure_;
// URLs for which hints were last fetched in the real-time.
std::vector<GURL> navigation_urls_last_fetched_real_time_;
// Used to get |weak_ptr_| to self on the UI thread. // Used to get |weak_ptr_| to self on the UI thread.
base::WeakPtrFactory<OptimizationGuideHintsManager> ui_weak_ptr_factory_{ base::WeakPtrFactory<OptimizationGuideHintsManager> ui_weak_ptr_factory_{
this}; this};
......
...@@ -20,9 +20,11 @@ ...@@ -20,9 +20,11 @@
#include "chrome/browser/previews/previews_service_factory.h" #include "chrome/browser/previews/previews_service_factory.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
#include "components/optimization_guide/bloom_filter.h" #include "components/optimization_guide/bloom_filter.h"
#include "components/optimization_guide/hints_component_util.h" #include "components/optimization_guide/hints_component_util.h"
#include "components/optimization_guide/hints_fetcher.h" #include "components/optimization_guide/hints_fetcher.h"
#include "components/optimization_guide/optimization_guide_constants.h"
#include "components/optimization_guide/optimization_guide_decider.h" #include "components/optimization_guide/optimization_guide_decider.h"
#include "components/optimization_guide/optimization_guide_enums.h" #include "components/optimization_guide/optimization_guide_enums.h"
#include "components/optimization_guide/optimization_guide_features.h" #include "components/optimization_guide/optimization_guide_features.h"
...@@ -217,8 +219,9 @@ class OptimizationGuideHintsManagerTest ...@@ -217,8 +219,9 @@ class OptimizationGuideHintsManagerTest
data_reduction_proxy::DataReductionProxyTestContext::Builder() data_reduction_proxy::DataReductionProxyTestContext::Builder()
.WithMockConfig() .WithMockConfig()
.Build(); .Build();
drp_test_context_->DisableWarmupURLFetch();
CreateServiceAndHintsManager(/*top_host_provider=*/nullptr); CreateServiceAndHintsManager(/*top_host_provider=*/nullptr);
base::CommandLine::ForCurrentProcess()->AppendSwitch(
data_reduction_proxy::switches::kEnableDataReductionProxy);
} }
void TearDown() override { void TearDown() override {
...@@ -381,6 +384,10 @@ class OptimizationGuideHintsManagerTest ...@@ -381,6 +384,10 @@ class OptimizationGuideHintsManagerTest
return GURL("https://somedomain.org/news/whatever"); return GURL("https://somedomain.org/news/whatever");
} }
GURL url_without_hints() const {
return GURL("https://url_without_hints.org/");
}
base::FilePath temp_dir() const { return temp_dir_.GetPath(); } base::FilePath temp_dir() const { return temp_dir_.GetPath(); }
TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); }
...@@ -687,8 +694,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -687,8 +694,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
navigation_handle->set_has_committed(true); navigation_handle->set_has_committed(true);
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result", histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result",
...@@ -711,8 +718,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationWithHint) { ...@@ -711,8 +718,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationWithHint) {
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result", histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result",
...@@ -735,8 +742,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationNoHint) { ...@@ -735,8 +742,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationNoHint) {
GURL("https://notinhints.com")); GURL("https://notinhints.com"));
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result", histogram_tester.ExpectUniqueSample("OptimizationGuide.LoadedHint.Result",
...@@ -759,8 +766,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationNoHost) { ...@@ -759,8 +766,8 @@ TEST_F(OptimizationGuideHintsManagerTest, LoadHintForNavigationNoHost) {
GURL("blargh")); GURL("blargh"));
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
histogram_tester.ExpectTotalCount("OptimizationGuide.LoadedHint.Result", 0); histogram_tester.ExpectTotalCount("OptimizationGuide.LoadedHint.Result", 0);
...@@ -1470,8 +1477,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1470,8 +1477,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1509,8 +1516,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1509,8 +1516,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1548,8 +1555,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1548,8 +1555,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1585,8 +1592,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1585,8 +1592,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1624,8 +1631,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1624,8 +1631,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1663,8 +1670,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1663,8 +1670,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_with_hints()); url_with_hints());
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
// Purposely set the page hint to be null to show that we override the page // Purposely set the page hint to be null to show that we override the page
...@@ -1695,6 +1702,54 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1695,6 +1702,54 @@ TEST_F(OptimizationGuideHintsManagerTest,
EXPECT_EQ(nullptr, navigation_data->page_hint()); EXPECT_EQ(nullptr, navigation_data->page_hint());
} }
TEST_F(OptimizationGuideHintsManagerTest,
HintsFetchedAtNavigationTime_ECT_SLOW_2G) {
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeature(
optimization_guide::features::kOptimizationHintsFetching);
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::RunLoop run_loop;
base::HistogramTester histogram_tester;
hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure());
run_loop.Run();
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 1, 1);
}
TEST_F(OptimizationGuideHintsManagerTest,
HintsNotFetchedAtNavigationTime_ECT_4G) {
hints_manager()->RegisterOptimizationTypes(
{optimization_guide::proto::DEFER_ALL_SCRIPT});
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeature(
optimization_guide::features::kOptimizationHintsFetching);
InitializeWithDefaultConfig("1.0.0.0");
// Set ECT estimate so hint is activated.
hints_manager()->OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_4G);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
url_without_hints());
base::HistogramTester histogram_tester;
base::RunLoop run_loop;
hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure());
run_loop.Run();
histogram_tester.ExpectTotalCount(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 0);
}
TEST_F(OptimizationGuideHintsManagerTest, TEST_F(OptimizationGuideHintsManagerTest,
CanApplyOptimizationNoMatchingPageHint) { CanApplyOptimizationNoMatchingPageHint) {
InitializeWithDefaultConfig("1.0.0.0"); InitializeWithDefaultConfig("1.0.0.0");
...@@ -1706,8 +1761,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1706,8 +1761,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver( CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
GURL("https://somedomain.org/nomatch")); GURL("https://somedomain.org/nomatch"));
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
optimization_guide::OptimizationTargetDecision optimization_target_decision; optimization_guide::OptimizationTargetDecision optimization_target_decision;
...@@ -1836,8 +1891,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1836,8 +1891,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
ProcessHints(config, "1.0.0.0"); ProcessHints(config, "1.0.0.0");
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
// Set ECT estimate so hint is activated. // Set ECT estimate so hint is activated.
...@@ -1901,8 +1956,8 @@ TEST_F(OptimizationGuideHintsManagerTest, ...@@ -1901,8 +1956,8 @@ TEST_F(OptimizationGuideHintsManagerTest,
ProcessHints(config, "1.0.0.0"); ProcessHints(config, "1.0.0.0");
base::RunLoop run_loop; base::RunLoop run_loop;
hints_manager()->LoadHintForNavigation(navigation_handle.get(), hints_manager()->OnNavigationStartOrRedirect(navigation_handle.get(),
run_loop.QuitClosure()); run_loop.QuitClosure());
run_loop.Run(); run_loop.Run();
hints_manager()->OnEffectiveConnectionTypeChanged( hints_manager()->OnEffectiveConnectionTypeChanged(
......
...@@ -143,8 +143,12 @@ void OptimizationGuideKeyedService::Initialize( ...@@ -143,8 +143,12 @@ void OptimizationGuideKeyedService::Initialize(
void OptimizationGuideKeyedService::MaybeLoadHintForNavigation( void OptimizationGuideKeyedService::MaybeLoadHintForNavigation(
content::NavigationHandle* navigation_handle) { content::NavigationHandle* navigation_handle) {
if (hints_manager_ && hints_manager_->HasRegisteredOptimizationTypes()) DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
hints_manager_->LoadHintForNavigation(navigation_handle, base::DoNothing());
if (hints_manager_ && hints_manager_->HasRegisteredOptimizationTypes()) {
hints_manager_->OnNavigationStartOrRedirect(navigation_handle,
base::DoNothing());
}
} }
void OptimizationGuideKeyedService::RegisterOptimizationTypes( void OptimizationGuideKeyedService::RegisterOptimizationTypes(
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task/thread_pool/thread_pool_instance.h" #include "base/task/thread_pool/thread_pool_instance.h"
...@@ -81,6 +82,20 @@ int RetryForHistogramUntilCountReached( ...@@ -81,6 +82,20 @@ int RetryForHistogramUntilCountReached(
} }
} }
int GetCountBucketSamples(const base::HistogramTester* histogram_tester,
const std::string& histogram_name,
size_t bucket_min) {
std::vector<base::Bucket> buckets =
histogram_tester->GetAllSamples(histogram_name);
for (const auto& bucket : buckets) {
if (bucket_min == static_cast<size_t>(bucket.min))
return bucket.count;
}
return 0;
}
enum class HintsFetcherRemoteResponseType { enum class HintsFetcherRemoteResponseType {
kSuccessful = 0, kSuccessful = 0,
kUnsuccessful = 1, kUnsuccessful = 1,
...@@ -100,6 +115,8 @@ class HintsFetcherDisabledBrowserTest ...@@ -100,6 +115,8 @@ class HintsFetcherDisabledBrowserTest
HintsFetcherDisabledBrowserTest() = default; HintsFetcherDisabledBrowserTest() = default;
~HintsFetcherDisabledBrowserTest() override = default; ~HintsFetcherDisabledBrowserTest() override = default;
bool IsOptimizationGuideKeyedServiceEnabled() const { return GetParam(); }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
content::NetworkConnectionChangeSimulator().SetConnectionType( content::NetworkConnectionChangeSimulator().SetConnectionType(
network::mojom::ConnectionType::CONNECTION_2G); network::mojom::ConnectionType::CONNECTION_2G);
...@@ -129,7 +146,7 @@ class HintsFetcherDisabledBrowserTest ...@@ -129,7 +146,7 @@ class HintsFetcherDisabledBrowserTest
ASSERT_TRUE(hints_server_->Start()); ASSERT_TRUE(hints_server_->Start());
if (GetParam()) { if (IsOptimizationGuideKeyedServiceEnabled()) {
param_feature_list_.InitWithFeatures( param_feature_list_.InitWithFeatures(
{optimization_guide::features::kOptimizationGuideKeyedService}, {}); {optimization_guide::features::kOptimizationGuideKeyedService}, {});
} else { } else {
...@@ -158,7 +175,7 @@ class HintsFetcherDisabledBrowserTest ...@@ -158,7 +175,7 @@ class HintsFetcherDisabledBrowserTest
"example1.com, example2.com"); "example1.com, example2.com");
cmd->AppendSwitch(previews::switches::kDoNotRequireLitePageRedirectInfoBar); cmd->AppendSwitch(previews::switches::kDoNotRequireLitePageRedirectInfoBar);
if (GetParam()) if (IsOptimizationGuideKeyedServiceEnabled())
cmd->AppendSwitch(optimization_guide::switches::kFetchHintsOverrideTimer); cmd->AppendSwitch(optimization_guide::switches::kFetchHintsOverrideTimer);
} }
...@@ -276,6 +293,17 @@ class HintsFetcherDisabledBrowserTest ...@@ -276,6 +293,17 @@ class HintsFetcherDisabledBrowserTest
return &histogram_tester_; return &histogram_tester_;
} }
void SetExpectedHintsRequestForHosts(
const base::flat_set<std::string>& hosts) {
base::AutoLock lock(lock_);
expect_hints_request_for_hosts_ = hosts;
}
size_t count_hints_requests_received() {
base::AutoLock lock(lock_);
return count_hints_requests_received_;
}
protected: protected:
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<net::EmbeddedTestServer> origin_server_; std::unique_ptr<net::EmbeddedTestServer> origin_server_;
...@@ -296,12 +324,22 @@ class HintsFetcherDisabledBrowserTest ...@@ -296,12 +324,22 @@ class HintsFetcherDisabledBrowserTest
std::unique_ptr<net::test_server::HttpResponse> HandleGetHintsRequest( std::unique_ptr<net::test_server::HttpResponse> HandleGetHintsRequest(
const net::test_server::HttpRequest& request) { const net::test_server::HttpRequest& request) {
base::AutoLock lock(lock_);
++count_hints_requests_received_;
std::unique_ptr<net::test_server::BasicHttpResponse> response; std::unique_ptr<net::test_server::BasicHttpResponse> response;
response.reset(new net::test_server::BasicHttpResponse); response.reset(new net::test_server::BasicHttpResponse);
// If the request is a GET, it corresponds to a navigation so return a // If the request is a GET, it corresponds to a navigation so return a
// normal response. // normal response.
EXPECT_EQ(request.method, net::test_server::METHOD_POST); EXPECT_EQ(request.method, net::test_server::METHOD_POST);
optimization_guide::proto::GetHintsRequest hints_request;
EXPECT_TRUE(hints_request.ParseFromString(request.content));
EXPECT_FALSE(hints_request.hosts().empty());
VerifyHintsMatchExpectedHosts(hints_request);
if (response_type_ == HintsFetcherRemoteResponseType::kSuccessful) { if (response_type_ == HintsFetcherRemoteResponseType::kSuccessful) {
response->set_code(net::HTTP_OK); response->set_code(net::HTTP_OK);
...@@ -333,6 +371,27 @@ class HintsFetcherDisabledBrowserTest ...@@ -333,6 +371,27 @@ class HintsFetcherDisabledBrowserTest
return std::move(response); return std::move(response);
} }
// Verifies that the hosts present in |hints_request| match the expected set
// of hosts present in |expect_hints_request_for_hosts_|. The ordering of the
// hosts in not matched.
void VerifyHintsMatchExpectedHosts(
const optimization_guide::proto::GetHintsRequest& hints_request) const {
if (!expect_hints_request_for_hosts_)
return;
base::flat_set<std::string> hosts_requested;
for (const auto& host : hints_request.hosts()) {
hosts_requested.insert(host.host());
}
EXPECT_EQ(expect_hints_request_for_hosts_.value().size(),
hosts_requested.size());
for (const auto& host : expect_hints_request_for_hosts_.value()) {
hosts_requested.erase(host);
}
EXPECT_EQ(0u, hosts_requested.size());
}
void TearDownOnMainThread() override { void TearDownOnMainThread() override {
EXPECT_TRUE(origin_server_->ShutdownAndWaitUntilComplete()); EXPECT_TRUE(origin_server_->ShutdownAndWaitUntilComplete());
EXPECT_TRUE(hints_server_->ShutdownAndWaitUntilComplete()); EXPECT_TRUE(hints_server_->ShutdownAndWaitUntilComplete());
...@@ -349,6 +408,18 @@ class HintsFetcherDisabledBrowserTest ...@@ -349,6 +408,18 @@ class HintsFetcherDisabledBrowserTest
optimization_guide::testing::TestHintsComponentCreator optimization_guide::testing::TestHintsComponentCreator
test_hints_component_creator_; test_hints_component_creator_;
base::Lock lock_;
// Guarded by |lock_|.
// Count of hints requests received so far by |hints_server_|.
size_t count_hints_requests_received_ = 0;
// Guarded by |lock_|.
// Set of hosts for which a hints request is expected to arrive. This set is
// verified to match with the set of hosts present in the hints request. If
// null, then the verification is not done.
base::Optional<base::flat_set<std::string>> expect_hints_request_for_hosts_;
DISALLOW_COPY_AND_ASSIGN(HintsFetcherDisabledBrowserTest); DISALLOW_COPY_AND_ASSIGN(HintsFetcherDisabledBrowserTest);
}; };
...@@ -660,18 +731,17 @@ IN_PROC_BROWSER_TEST_P( ...@@ -660,18 +731,17 @@ IN_PROC_BROWSER_TEST_P(
// Verifies that the fetched hint is loaded and not the component hint as // Verifies that the fetched hint is loaded and not the component hint as
// fetched hints are prioritized. // fetched hints are prioritized.
EXPECT_LE(1,
histogram_tester->ExpectBucketCount( GetCountBucketSamples(
"OptimizationGuide.HintCache.HintType.Loaded", histogram_tester, "OptimizationGuide.HintCache.HintType.Loaded",
static_cast<int>( static_cast<int>(optimization_guide::HintCacheStore::
optimization_guide::HintCacheStore::StoreEntryType::kFetchedHint), StoreEntryType::kFetchedHint)));
1);
EXPECT_EQ(0,
histogram_tester->ExpectBucketCount( GetCountBucketSamples(
"OptimizationGuide.HintCache.HintType.Loaded", histogram_tester, "OptimizationGuide.HintCache.HintType.Loaded",
static_cast<int>( static_cast<int>(optimization_guide::HintCacheStore::
optimization_guide::HintCacheStore::StoreEntryType::kComponentHint), StoreEntryType::kComponentHint)));
0);
// Wipe the browser history - clear all the fetched hints. // Wipe the browser history - clear all the fetched hints.
browser()->profile()->Wipe(); browser()->profile()->Wipe();
...@@ -682,18 +752,17 @@ IN_PROC_BROWSER_TEST_P( ...@@ -682,18 +752,17 @@ IN_PROC_BROWSER_TEST_P(
ui_test_utils::NavigateToURL(browser(), https_url()); ui_test_utils::NavigateToURL(browser(), https_url());
// Fetched Hints count should not change. // Fetched Hints count should not change.
histogram_tester->ExpectBucketCount( EXPECT_LE(1,
"OptimizationGuide.HintCache.HintType.Loaded", GetCountBucketSamples(
static_cast<int>( histogram_tester, "OptimizationGuide.HintCache.HintType.Loaded",
optimization_guide::HintCacheStore::StoreEntryType::kFetchedHint), static_cast<int>(optimization_guide::HintCacheStore::
1); StoreEntryType::kFetchedHint)));
// Component Hints count should increase. EXPECT_LE(IsOptimizationGuideKeyedServiceEnabled() ? 0 : 1,
histogram_tester->ExpectBucketCount( GetCountBucketSamples(
"OptimizationGuide.HintCache.HintType.Loaded", histogram_tester, "OptimizationGuide.HintCache.HintType.Loaded",
static_cast<int>( static_cast<int>(optimization_guide::HintCacheStore::
optimization_guide::HintCacheStore::StoreEntryType::kComponentHint), StoreEntryType::kComponentHint)));
1);
} }
IN_PROC_BROWSER_TEST_P( IN_PROC_BROWSER_TEST_P(
...@@ -865,6 +934,163 @@ IN_PROC_BROWSER_TEST_P( ...@@ -865,6 +934,163 @@ IN_PROC_BROWSER_TEST_P(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false, 1); "OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false, 1);
} }
// Test that the hints are fetched at the time of the navigation.
IN_PROC_BROWSER_TEST_P(
HintsFetcherBrowserTest,
DISABLE_ON_WIN_MAC_CHROMESOS(HintsFetcher_NavigationFetch_ECT)) {
const base::HistogramTester* histogram_tester = GetHistogramTester();
// Whitelist NoScript for https_url()'s' host.
SetUpComponentUpdateHints(https_url());
RetryForHistogramUntilCountReached(
histogram_tester,
optimization_guide::kComponentHintsUpdatedResultHistogramString, 1);
// Expect that the browser initialization will record at least one sample
// in each of the following histograms as One Platform Hints are enabled.
EXPECT_GE(RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 1),
1);
EXPECT_GE(RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.GetHintsRequest.Status", 1),
1);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.GetHintsRequest.Status", net::HTTP_OK, 1);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.GetHintsRequest.NetErrorCode", net::OK,
1);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HintCount", 1, 1);
EXPECT_EQ(1u, count_hints_requests_received());
// Change ECT to a low value. Hints should be fetched at the time of
// navigation.
{
g_browser_process->network_quality_tracker()
->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_2G);
// Navigate to a host not in the seeded site engagement service; it
// should be recorded as not covered by the hints fetcher.
base::flat_set<std::string> expected_hosts_2g;
std::string host_2g("https://unseenhost_2g.com/");
expected_hosts_2g.insert(GURL(host_2g).host());
SetExpectedHintsRequestForHosts(expected_hosts_2g);
ui_test_utils::NavigateToURL(browser(), GURL(host_2g));
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", 1);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false,
1);
EXPECT_EQ(IsOptimizationGuideKeyedServiceEnabled() ? 2u : 1u,
count_hints_requests_received());
RetryForHistogramUntilCountReached(
histogram_tester, optimization_guide::kLoadedHintLocalHistogramString,
IsOptimizationGuideKeyedServiceEnabled() ? 2 : 1);
}
// Change ECT to a high value. Hints should not be fetched at the time of
// navigation.
{
g_browser_process->network_quality_tracker()
->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_4G);
base::flat_set<std::string> expected_hosts_4g;
std::string host_4g("https://unseenhost_4g.com/");
expected_hosts_4g.insert((GURL(host_4g).host()));
SetExpectedHintsRequestForHosts(expected_hosts_4g);
ui_test_utils::NavigateToURL(browser(), GURL(host_4g));
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", 2);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false,
2);
EXPECT_EQ(IsOptimizationGuideKeyedServiceEnabled() ? 2u : 1u,
count_hints_requests_received());
RetryForHistogramUntilCountReached(
histogram_tester, optimization_guide::kLoadedHintLocalHistogramString,
IsOptimizationGuideKeyedServiceEnabled() ? 2 : 1);
}
// Change ECT back to a low value. Hints should be fetched at the time of
// navigation.
{
g_browser_process->network_quality_tracker()
->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_3G);
// Navigate to a host not in the seeded site engagement service; it
// should be recorded as not covered by the hints fetcher.
base::flat_set<std::string> expected_hosts_3g;
std::string host_3g("https://unseenhost_3g.com/");
expected_hosts_3g.insert(GURL(host_3g).host());
SetExpectedHintsRequestForHosts(expected_hosts_3g);
ui_test_utils::NavigateToURL(browser(), GURL(host_3g));
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", 3);
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false,
3);
EXPECT_EQ(IsOptimizationGuideKeyedServiceEnabled() ? 3u : 1u,
count_hints_requests_received());
RetryForHistogramUntilCountReached(
histogram_tester, optimization_guide::kLoadedHintLocalHistogramString,
IsOptimizationGuideKeyedServiceEnabled() ? 3 : 1);
}
// Navigate again to a webpage with the
// same host. Hints should be available at the time of
// navigation.
{
// Navigate to a host that was recently fetched. It
// should be recorded as covered by the hints fetcher.
base::flat_set<std::string> expected_hosts_3g;
std::string host_3g("https://unseenhost_3g.com");
expected_hosts_3g.insert(GURL(host_3g).host());
SetExpectedHintsRequestForHosts(expected_hosts_3g);
ui_test_utils::NavigateToURL(browser(),
GURL("https://unseenhost_3g.com/test1.html"));
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", 4);
if (IsOptimizationGuideKeyedServiceEnabled()) {
// Hints should be available this time for the navigation.
histogram_tester->ExpectBucketCount(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false,
3);
histogram_tester->ExpectBucketCount(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", true,
1);
} else {
histogram_tester->ExpectUniqueSample(
"OptimizationGuide.HintsFetcher.NavigationHostCoveredByFetch", false,
4);
}
EXPECT_EQ(IsOptimizationGuideKeyedServiceEnabled() ? 4u : 1u,
count_hints_requests_received());
RetryForHistogramUntilCountReached(
histogram_tester, optimization_guide::kLoadedHintLocalHistogramString,
IsOptimizationGuideKeyedServiceEnabled() ? 4 : 1);
}
}
IN_PROC_BROWSER_TEST_P( IN_PROC_BROWSER_TEST_P(
HintsFetcherBrowserTest, HintsFetcherBrowserTest,
DISABLE_ON_WIN_MAC_CHROMESOS(HintsFetcherHostCoveredNotHTTPS)) { DISABLE_ON_WIN_MAC_CHROMESOS(HintsFetcherHostCoveredNotHTTPS)) {
...@@ -913,7 +1139,7 @@ class HintsFetcherChangeDefaultBlacklistSizeBrowserTest ...@@ -913,7 +1139,7 @@ class HintsFetcherChangeDefaultBlacklistSizeBrowserTest
optimization_hints_fetching_params["top_host_blacklist_size_multiplier"] = optimization_hints_fetching_params["top_host_blacklist_size_multiplier"] =
"5"; "5";
if (GetParam()) { if (IsOptimizationGuideKeyedServiceEnabled()) {
scoped_feature_list_.InitWithFeaturesAndParameters( scoped_feature_list_.InitWithFeaturesAndParameters(
{ {
/* vector of enabled features along with params */ /* vector of enabled features along with params */
......
...@@ -149,5 +149,18 @@ int MaxServerBloomFilterByteSize() { ...@@ -149,5 +149,18 @@ int MaxServerBloomFilterByteSize() {
250 * 1024 /* 250KB */); 250 * 1024 /* 250KB */);
} }
base::Optional<net::EffectiveConnectionType>
GetMaxEffectiveConnectionTypeForNavigationHintsFetch() {
std::string param_value = base::GetFieldTrialParamValueByFeature(
features::kOptimizationHintsFetching,
"max_effective_connection_type_for_navigation_hints_fetch");
// Use a default value.
if (param_value.empty())
return net::EFFECTIVE_CONNECTION_TYPE_3G;
return net::GetEffectiveConnectionTypeForName(param_value);
}
} // namespace features } // namespace features
} // namespace optimization_guide } // namespace optimization_guide
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
#include <utility> #include <utility>
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/optional.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/nqe/effective_connection_type.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace optimization_guide { namespace optimization_guide {
...@@ -73,6 +75,12 @@ bool IsOptimizationGuideKeyedServiceEnabled(); ...@@ -73,6 +75,12 @@ bool IsOptimizationGuideKeyedServiceEnabled();
// a bloom filter. // a bloom filter.
int MaxServerBloomFilterByteSize(); int MaxServerBloomFilterByteSize();
// Maximum effective connection type at which hints can be fetched for
// navigations in real-time. Returns null if the hints fetching for navigations
// is disabled.
base::Optional<net::EffectiveConnectionType>
GetMaxEffectiveConnectionTypeForNavigationHintsFetch();
} // namespace features } // namespace features
} // namespace optimization_guide } // namespace optimization_guide
......
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