Commit d532c3d0 authored by Robert Ogden's avatar Robert Ogden Committed by Commit Bot

Limit number of IsolatedPrerender prefetches on experiment params

Defaults to 1.

Bug: 1023485
Change-Id: Ia76fe2e0150b60f3a4e15b5bcdb674de63899c5f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2025570
Commit-Queue: Robert Ogden <robertogden@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736194}
parent 3508db9d
...@@ -36,3 +36,18 @@ bool IsolatedPrerenderShouldReplaceDataReductionCustomProxy() { ...@@ -36,3 +36,18 @@ bool IsolatedPrerenderShouldReplaceDataReductionCustomProxy() {
DCHECK(!replace); DCHECK(!replace);
return replace; return replace;
} }
base::Optional<size_t> IsolatedPrerenderMaximumNumberOfPrefetches() {
if (!base::FeatureList::IsEnabled(
features::kPrefetchSRPNavigationPredictions_HTMLOnly)) {
return 0;
}
int max = base::GetFieldTrialParamByFeatureAsInt(
features::kPrefetchSRPNavigationPredictions_HTMLOnly,
"max_srp_prefetches", 1);
if (max < 0) {
return base::nullopt;
}
return max;
}
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_ #ifndef CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_
#define CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_ #define CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_
#include <stdint.h>
#include "base/optional.h" #include "base/optional.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -18,4 +20,9 @@ base::Optional<GURL> IsolatedPrerenderProxyServer(); ...@@ -18,4 +20,9 @@ base::Optional<GURL> IsolatedPrerenderProxyServer();
// custom proxy. // custom proxy.
bool IsolatedPrerenderShouldReplaceDataReductionCustomProxy(); bool IsolatedPrerenderShouldReplaceDataReductionCustomProxy();
// The maximum number of prefetches that should be done from predictions on a
// Google SRP. nullopt is returned for unlimited. Negative values given by the
// field trial return nullopt.
base::Optional<size_t> IsolatedPrerenderMaximumNumberOfPrefetches();
#endif // CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_ #endif // CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_PARAMS_H_
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/feature_list.h" #include "base/feature_list.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h" #include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h"
#include "chrome/browser/prerender/isolated/isolated_prerender_features.h" #include "chrome/browser/prerender/isolated/isolated_prerender_features.h"
#include "chrome/browser/prerender/isolated/isolated_prerender_params.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "components/google/core/common/google_util.h" #include "components/google/core/common/google_util.h"
...@@ -88,6 +89,7 @@ void IsolatedPrerenderTabHelper::DidFinishNavigation( ...@@ -88,6 +89,7 @@ void IsolatedPrerenderTabHelper::DidFinishNavigation(
DCHECK(!PrefetchingActive()); DCHECK(!PrefetchingActive());
urls_to_prefetch_.clear(); urls_to_prefetch_.clear();
prefetched_responses_.clear(); prefetched_responses_.clear();
num_prefetches_attempted_ = 0;
} }
std::unique_ptr<PrefetchedResponseContainer> std::unique_ptr<PrefetchedResponseContainer>
...@@ -115,6 +117,13 @@ void IsolatedPrerenderTabHelper::Prefetch() { ...@@ -115,6 +117,13 @@ void IsolatedPrerenderTabHelper::Prefetch() {
if (urls_to_prefetch_.empty()) if (urls_to_prefetch_.empty())
return; return;
if (IsolatedPrerenderMaximumNumberOfPrefetches().has_value() &&
num_prefetches_attempted_ >=
IsolatedPrerenderMaximumNumberOfPrefetches().value()) {
return;
}
num_prefetches_attempted_++;
GURL url = urls_to_prefetch_[0]; GURL url = urls_to_prefetch_[0];
urls_to_prefetch_.erase(urls_to_prefetch_.begin()); urls_to_prefetch_.erase(urls_to_prefetch_.begin());
...@@ -230,6 +239,15 @@ void IsolatedPrerenderTabHelper::OnPredictionUpdated( ...@@ -230,6 +239,15 @@ void IsolatedPrerenderTabHelper::OnPredictionUpdated(
return; return;
} }
// This is also checked before prefetching from the network, but checking
// again here allows us to skip querying for cookies if we won't be
// prefetching the url anyways.
if (IsolatedPrerenderMaximumNumberOfPrefetches().has_value() &&
num_prefetches_attempted_ >=
IsolatedPrerenderMaximumNumberOfPrefetches().value()) {
return;
}
if (!prediction.has_value()) { if (!prediction.has_value()) {
return; return;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_TAB_HELPER_H_ #ifndef CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_TAB_HELPER_H_
#define CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_TAB_HELPER_H_ #define CHROME_BROWSER_PRERENDER_ISOLATED_ISOLATED_PRERENDER_TAB_HELPER_H_
#include <stdint.h>
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
...@@ -96,6 +97,9 @@ class IsolatedPrerenderTabHelper ...@@ -96,6 +97,9 @@ class IsolatedPrerenderTabHelper
// An ordered list of the URLs to prefetch. // An ordered list of the URLs to prefetch.
std::vector<GURL> urls_to_prefetch_; std::vector<GURL> urls_to_prefetch_;
// The number of prefetches that have been attempted on this page.
size_t num_prefetches_attempted_ = 0;
// All prefetched responses by URL. This is cleared every time a mainframe // All prefetched responses by URL. This is cleared every time a mainframe
// navigation commits. // navigation commits.
std::map<GURL, std::unique_ptr<PrefetchedResponseContainer>> std::map<GURL, std::unique_ptr<PrefetchedResponseContainer>>
......
...@@ -394,3 +394,67 @@ TEST_F(IsolatedPrerenderTabHelperTest, SuccessCase) { ...@@ -394,3 +394,67 @@ TEST_F(IsolatedPrerenderTabHelperTest, SuccessCase) {
network::mojom::URLResponseHeadPtr head = resp->TakeHead(); network::mojom::URLResponseHeadPtr head = resp->TakeHead();
EXPECT_TRUE(head->headers->HasHeaderValue("X-Testing", "Hello World")); EXPECT_TRUE(head->headers->HasHeaderValue("X-Testing", "Hello World"));
} }
TEST_F(IsolatedPrerenderTabHelperTest, LimitedNumberOfPrefetches_Zero) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPrefetchSRPNavigationPredictions_HTMLOnly,
{{"max_srp_prefetches", "0"}});
GURL doc_url("https://www.google.com/search?q=cats");
GURL prediction_url("https://www.cat-food.com/");
MakeNavigationPrediction(web_contents(), doc_url, {prediction_url});
base::RunLoop().RunUntilIdle();
EXPECT_EQ(RequestCount(), 0);
}
TEST_F(IsolatedPrerenderTabHelperTest, LimitedNumberOfPrefetches_Unlimited) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPrefetchSRPNavigationPredictions_HTMLOnly,
{{"max_srp_prefetches", "-1"}});
GURL doc_url("https://www.google.com/search?q=cats");
GURL prediction_url_1("https://www.cat-food.com/");
GURL prediction_url_2("https://www.dogs-r-dumb.com/");
GURL prediction_url_3("https://www.catz-rule.com/");
MakeNavigationPrediction(
web_contents(), doc_url,
{prediction_url_1, prediction_url_2, prediction_url_3});
VerifyCommonRequestState(prediction_url_1);
MakeResponseAndWait(net::HTTP_OK, net::OK, kHTMLMimeType, {}, kHTMLBody);
VerifyCommonRequestState(prediction_url_2);
// Failed responses do not retry or attempt more requests in the list.
MakeResponseAndWait(net::HTTP_OK, net::ERR_FAILED, kHTMLMimeType, {},
kHTMLBody);
VerifyCommonRequestState(prediction_url_3);
MakeResponseAndWait(net::HTTP_OK, net::OK, kHTMLMimeType, {}, kHTMLBody);
EXPECT_EQ(RequestCount(), 0);
}
TEST_F(IsolatedPrerenderTabHelperTest, LimitedNumberOfPrefetches) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPrefetchSRPNavigationPredictions_HTMLOnly,
{{"max_srp_prefetches", "2"}});
GURL doc_url("https://www.google.com/search?q=cats");
GURL prediction_url_1("https://www.cat-food.com/");
GURL prediction_url_2("https://www.dogs-r-dumb.com/");
GURL prediction_url_3("https://www.catz-rule.com/");
MakeNavigationPrediction(
web_contents(), doc_url,
{prediction_url_1, prediction_url_2, prediction_url_3});
VerifyCommonRequestState(prediction_url_1);
// Failed responses do not retry or attempt more requests in the list.
MakeResponseAndWait(net::HTTP_OK, net::ERR_FAILED, kHTMLMimeType, {},
kHTMLBody);
VerifyCommonRequestState(prediction_url_2);
MakeResponseAndWait(net::HTTP_OK, net::OK, kHTMLMimeType, {}, kHTMLBody);
EXPECT_EQ(RequestCount(), 0);
}
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