1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/navigation_predictor/navigation_predictor_preconnect_client.h"
#include <memory>
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_features.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h"
#include "chrome/browser/navigation_predictor/search_engine_preconnector.h"
#include "chrome/browser/navigation_predictor/search_engine_preconnector_keyed_service_factory.h"
#include "chrome/browser/predictors/loading_predictor.h"
#include "chrome/browser/predictors/loading_predictor_factory.h"
#include "chrome/browser/predictors/preconnect_manager.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "net/base/features.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/origin.h"
namespace {
class NavigationPredictorPreconnectClientBrowserTest
: public subresource_filter::SubresourceFilterBrowserTest,
public predictors::PreconnectManager::Observer {
public:
NavigationPredictorPreconnectClientBrowserTest()
: subresource_filter::SubresourceFilterBrowserTest() {
feature_list_.InitFromCommandLine(
std::string(),
"NavigationPredictorPreconnectHoldback,PreconnectToSearch");
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
}
NavigationPredictorPreconnectClientBrowserTest(
const NavigationPredictorPreconnectClientBrowserTest&) = delete;
NavigationPredictorPreconnectClientBrowserTest& operator=(
const NavigationPredictorPreconnectClientBrowserTest&) = delete;
SearchEnginePreconnector* GetSearchEnginePreconnector() {
if (SearchEnginePreconnector::ShouldBeEnabledAsKeyedService()) {
return SearchEnginePreconnectorKeyedServiceFactory::GetForProfile(
browser()->profile());
}
NavigationPredictorKeyedService* navigation_predictor_keyed_service =
NavigationPredictorKeyedServiceFactory::GetForProfile(
browser()->profile());
EXPECT_TRUE(navigation_predictor_keyed_service);
return navigation_predictor_keyed_service->search_engine_preconnector();
}
void SetUp() override {
https_server_->ServeFilesFromSourceDirectory(
"chrome/test/data/navigation_predictor");
ASSERT_TRUE(https_server_->Start());
subresource_filter::SubresourceFilterBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
subresource_filter::SubresourceFilterBrowserTest::SetUpOnMainThread();
host_resolver()->ClearRules();
NavigationPredictorPreconnectClient::EnablePreconnectsForLocalIPsForTesting(
true);
// Get notified for Loading predictor's preconnect observer.
auto* loading_predictor =
predictors::LoadingPredictorFactory::GetForProfile(
browser()->profile());
ASSERT_TRUE(loading_predictor);
loading_predictor->preconnect_manager()->SetObserverForTesting(this);
// Also get notified for the SearchEnginePreconnect's preconnect observer.
SearchEnginePreconnector* preconnector = GetSearchEnginePreconnector();
ASSERT_TRUE(preconnector);
preconnector->GetPreconnectManager().SetObserverForTesting(this);
}
const GURL GetTestURL(const char* file) const {
return https_server_->GetURL(file);
}
void OnPreresolveFinished(
const GURL& url,
const net::NetworkAnonymizationKey& network_anonymization_key,
mojo::PendingRemote<network::mojom::ConnectionChangeObserverClient>&
observer,
bool success) override {
// The tests do not care about preresolves to non-test server (e.g., hard
// coded preconnects to google.com).
if (url::Origin::Create(url) !=
url::Origin::Create(https_server_->base_url())) {
return;
}
if (observer.is_valid()) {
observer_.Bind(std::move(observer));
}
EXPECT_TRUE(success);
preresolve_done_count_++;
if (run_loop_)
run_loop_->Quit();
}
void WaitForPreresolveCount(int expected_count) {
while (preresolve_done_count_ < expected_count) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
run_loop_.reset();
}
}
protected:
int preresolve_done_count_ = 0;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
mojo::Remote<network::mojom::ConnectionChangeObserverClient> observer_;
private:
base::test::ScopedFeatureList feature_list_;
std::unique_ptr<base::RunLoop> run_loop_;
};
IN_PROC_BROWSER_TEST_F(NavigationPredictorPreconnectClientBrowserTest,
NoPreconnectSearch) {
static const char16_t kShortName[] = u"test";
static const char kSearchURL[] =
"/anchors_different_area.html?q={searchTerms}";
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
ASSERT_TRUE(model);
search_test_utils::WaitForTemplateURLServiceToLoad(model);
ASSERT_TRUE(model->loaded());
TemplateURLData data;
data.SetShortName(kShortName);
data.SetKeyword(data.short_name());
data.SetURL(GetTestURL(kSearchURL).spec());
TemplateURL* template_url = model->Add(std::make_unique<TemplateURL>(data));
ASSERT_TRUE(template_url);
model->SetUserSelectedDefaultSearchProvider(template_url);
const GURL& url = GetTestURL("/anchors_different_area.html?q=cats");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be preconnect from navigation, but not preconnect client.
EXPECT_EQ(1, preresolve_done_count_);
}
IN_PROC_BROWSER_TEST_F(NavigationPredictorPreconnectClientBrowserTest,
PreconnectNotSearch) {
base::HistogramTester histogram_tester;
const GURL& url = GetTestURL("/anchors_different_area.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be one preconnect from navigation and one from preconnect
// client.
WaitForPreresolveCount(2);
EXPECT_EQ(2, preresolve_done_count_);
histogram_tester.ExpectUniqueSample("NavigationPredictor.IsPubliclyRoutable",
true, 1);
}
IN_PROC_BROWSER_TEST_F(NavigationPredictorPreconnectClientBrowserTest,
PreconnectNotSearchBackgroundForeground) {
const GURL& url = GetTestURL("/anchors_different_area.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be one preconnect from navigation and one from preconnect
// client.
WaitForPreresolveCount(2);
EXPECT_EQ(2, preresolve_done_count_);
browser()->tab_strip_model()->GetActiveWebContents()->WasHidden();
browser()->tab_strip_model()->GetActiveWebContents()->WasShown();
// After showing the contents again, there should be another preconnect client
// preconnect.
WaitForPreresolveCount(3);
EXPECT_EQ(3, preresolve_done_count_);
}
class NavigationPredictorPreconnectClientBrowserTestWithUnusedIdleSocketTimeout
: public NavigationPredictorPreconnectClientBrowserTest {
public:
NavigationPredictorPreconnectClientBrowserTestWithUnusedIdleSocketTimeout() =
default;
private:
void SetUpOnMainThread() override {
NavigationPredictorPreconnectClientBrowserTest::SetUpOnMainThread();
NavigationPredictorPreconnectClient::SetPreconnectIntervalForTesting(0);
}
};
// Test that we preconnect after the last preconnect timed out.
IN_PROC_BROWSER_TEST_F(
NavigationPredictorPreconnectClientBrowserTestWithUnusedIdleSocketTimeout,
ActionAccuracy_timeout) {
const GURL& url = GetTestURL("/page_with_same_host_anchor_element.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
WaitForPreresolveCount(3);
EXPECT_LE(3, preresolve_done_count_);
// Expect another one.
WaitForPreresolveCount(4);
EXPECT_LE(4, preresolve_done_count_);
}
// Test that we preconnect after the last preconnect timed out.
IN_PROC_BROWSER_TEST_F(
NavigationPredictorPreconnectClientBrowserTestWithUnusedIdleSocketTimeout,
CappedAtFiveAttempts) {
const GURL& url = GetTestURL("/page_with_same_host_anchor_element.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// Expect 1 navigation preresolve and 5 repeated onLoad calls.
WaitForPreresolveCount(6);
EXPECT_EQ(6, preresolve_done_count_);
// We should not see additional preresolves.
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout());
run_loop.Run();
EXPECT_EQ(6, preresolve_done_count_);
// By default, same document navigation should not trigger new preconnects.
const GURL& same_document_url =
GetTestURL("/page_with_same_host_anchor_element.html#foobar");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), same_document_url));
// Expect another one.
WaitForPreresolveCount(6);
EXPECT_EQ(6, preresolve_done_count_);
}
class NavigationPredictorPreconnectClientBrowserTestWithHoldback
: public NavigationPredictorPreconnectClientBrowserTest {
public:
NavigationPredictorPreconnectClientBrowserTestWithHoldback()
: NavigationPredictorPreconnectClientBrowserTest() {
feature_list_.InitFromCommandLine("NavigationPredictorPreconnectHoldback",
"PreconnectToSearch");
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(
NavigationPredictorPreconnectClientBrowserTestWithHoldback,
NoPreconnectHoldback) {
const GURL& url = GetTestURL("/anchors_different_area.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be preconnect from navigation, but not preconnect client.
EXPECT_EQ(1, preresolve_done_count_);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be 2 preconnects from navigation, but not any from preconnect
// client.
EXPECT_EQ(2, preresolve_done_count_);
}
namespace {
// Feature to control preconnect to search.
BASE_FEATURE(kPreconnectToSearchTest,
"PreconnectToSearch",
base::FEATURE_DISABLED_BY_DEFAULT);
} // namespace
class NavigationPredictorPreconnectClientBrowserTestWithSearch
: public NavigationPredictorPreconnectClientBrowserTest {
public:
NavigationPredictorPreconnectClientBrowserTestWithSearch()
: NavigationPredictorPreconnectClientBrowserTest() {
feature_list_.InitWithFeatures({kPreconnectToSearchTest}, {});
}
private:
base::test::ScopedFeatureList feature_list_;
};
// TODO(crbug.com/40702352): Re-enable this test.
#if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER)
#define MAYBE_PreconnectSearchWithFeature DISABLED_PreconnectSearchWithFeature
#else
#define MAYBE_PreconnectSearchWithFeature PreconnectSearchWithFeature
#endif
IN_PROC_BROWSER_TEST_F(NavigationPredictorPreconnectClientBrowserTestWithSearch,
MAYBE_PreconnectSearchWithFeature) {
static const char16_t kShortName[] = u"test";
static const char kSearchURL[] =
"/anchors_different_area.html?q={searchTerms}";
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
ASSERT_TRUE(model);
search_test_utils::WaitForTemplateURLServiceToLoad(model);
ASSERT_TRUE(model->loaded());
TemplateURLData data;
data.SetShortName(kShortName);
data.SetKeyword(data.short_name());
data.SetURL(GetTestURL(kSearchURL).spec());
data.preconnect_to_search_url = true;
TemplateURL* template_url = model->Add(std::make_unique<TemplateURL>(data));
ASSERT_TRUE(template_url);
model->SetUserSelectedDefaultSearchProvider(template_url);
const GURL& url = GetTestURL("/anchors_different_area.html?q=cats");
SearchEnginePreconnector* preconnector = GetSearchEnginePreconnector();
ASSERT_TRUE(preconnector);
preconnector->StartPreconnecting(/*with_startup_delay=*/false);
// There should be a DSE preconnect.
WaitForPreresolveCount(1);
EXPECT_EQ(1, preresolve_done_count_);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// Now there should be an onload preconnect as well as a navigation
// preconnect. If `SearchEnginePreconnect2` is enabled, we will have one
// additional preresolve count because navigating to a URL will start a
// preconnect due to a change in web visibility (i.e. the app is foregrounded)
// for the first navigation.
int preresolve_count =
SearchEnginePreconnector::SearchEnginePreconnect2Enabled() ? 4 : 3;
WaitForPreresolveCount(preresolve_count);
EXPECT_EQ(preresolve_count, preresolve_done_count_);
}
class NavigationPredictorPreconnectClientLocalURLBrowserTest
: public NavigationPredictorPreconnectClientBrowserTest {
public:
NavigationPredictorPreconnectClientLocalURLBrowserTest() = default;
NavigationPredictorPreconnectClientLocalURLBrowserTest(
const NavigationPredictorPreconnectClientLocalURLBrowserTest&) = delete;
NavigationPredictorPreconnectClientLocalURLBrowserTest& operator=(
const NavigationPredictorPreconnectClientLocalURLBrowserTest&) = delete;
private:
void SetUpOnMainThread() override {
NavigationPredictorPreconnectClientBrowserTest::SetUpOnMainThread();
NavigationPredictorPreconnectClient::EnablePreconnectsForLocalIPsForTesting(
false);
}
};
IN_PROC_BROWSER_TEST_F(NavigationPredictorPreconnectClientLocalURLBrowserTest,
NoPreconnectSearch) {
base::HistogramTester histogram_tester;
const GURL& url = GetTestURL("/anchors_different_area.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should not be any preconnects to non-public addresses.
histogram_tester.ExpectUniqueSample("NavigationPredictor.IsPubliclyRoutable",
false, 1);
}
class NavigationPredictorPreconnectClientFencedFrameBrowserTest
: public NavigationPredictorPreconnectClientBrowserTest {
public:
NavigationPredictorPreconnectClientFencedFrameBrowserTest() = default;
~NavigationPredictorPreconnectClientFencedFrameBrowserTest() override =
default;
NavigationPredictorPreconnectClientFencedFrameBrowserTest(
const NavigationPredictorPreconnectClientFencedFrameBrowserTest&) =
delete;
NavigationPredictorPreconnectClientFencedFrameBrowserTest& operator=(
const NavigationPredictorPreconnectClientFencedFrameBrowserTest&) =
delete;
content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
return fenced_frame_helper_;
}
content::WebContents* GetWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
private:
content::test::FencedFrameTestHelper fenced_frame_helper_;
};
IN_PROC_BROWSER_TEST_F(
NavigationPredictorPreconnectClientFencedFrameBrowserTest,
FencedFrameDoesNotCountIsPubliclyRoutable) {
base::HistogramTester histogram_tester;
const GURL& url = GetTestURL("/anchors_different_area.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// There should be one preconnect from navigation and one from preconnect
// client.
WaitForPreresolveCount(2);
EXPECT_EQ(2, preresolve_done_count_);
histogram_tester.ExpectTotalCount("NavigationPredictor.IsPubliclyRoutable",
1);
// Create a fenced frame.
const GURL& fenced_frame_url =
GetTestURL("/fenced_frames/anchors_different_area.html");
content::RenderFrameHost* fenced_frame_host =
fenced_frame_test_helper().CreateFencedFrame(
web_contents()->GetPrimaryMainFrame(), fenced_frame_url);
// The count should not increase in DidFinishLoad method.
histogram_tester.ExpectTotalCount("NavigationPredictor.IsPubliclyRoutable",
1);
fenced_frame_test_helper().NavigateFrameInFencedFrameTree(fenced_frame_host,
fenced_frame_url);
// Histogram count should not increase after navigating the fenced frame.
histogram_tester.ExpectTotalCount("NavigationPredictor.IsPubliclyRoutable",
1);
}
} // namespace
|