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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/preloading/prerender/prerender_host_registry.h"
#include <cstdint>
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/preloading/preload_pipeline_info_impl.h"
#include "content/browser/preloading/preloading.h"
#include "content/browser/preloading/preloading_confidence.h"
#include "content/browser/preloading/preloading_config.h"
#include "content/browser/preloading/prerender/prerender_features.h"
#include "content/browser/preloading/prerender/prerender_final_status.h"
#include "content/browser/preloading/prerender/prerender_host.h"
#include "content/browser/preloading/prerender/prerender_metrics.h"
#include "content/browser/preloading/speculation_rules/speculation_host_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/public/browser/preload_pipeline_info.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/preloading_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_browser_context.h"
#include "content/test/mock_commit_deferring_condition.h"
#include "content/test/navigation_simulator_impl.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "net/base/load_flags.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/loader/mixed_content.mojom.h"
#include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom.h"
namespace content {
namespace {
blink::mojom::SpeculationCandidatePtr CreatePrerenderCandidate(
const GURL& url) {
auto candidate = blink::mojom::SpeculationCandidate::New();
candidate->action = blink::mojom::SpeculationAction::kPrerender;
candidate->url = url;
candidate->referrer = blink::mojom::Referrer::New();
candidate->eagerness = blink::mojom::SpeculationEagerness::kEager;
candidate->tags = {std::nullopt};
return candidate;
}
void SendCandidates(const std::vector<GURL>& urls,
mojo::Remote<blink::mojom::SpeculationHost>& remote) {
std::vector<blink::mojom::SpeculationCandidatePtr> candidates;
candidates.resize(urls.size());
std::ranges::transform(urls, candidates.begin(), &CreatePrerenderCandidate);
remote->UpdateSpeculationCandidates(std::move(candidates));
remote.FlushForTesting();
}
void SendCandidate(const GURL& url,
mojo::Remote<blink::mojom::SpeculationHost>& remote) {
SendCandidates({url}, remote);
}
std::unique_ptr<NavigationSimulatorImpl> CreateActivation(
const GURL& prerendering_url,
WebContentsImpl& web_contents) {
std::unique_ptr<NavigationSimulatorImpl> navigation =
NavigationSimulatorImpl::CreateRendererInitiated(
prerendering_url, web_contents.GetPrimaryMainFrame());
navigation->SetReferrer(blink::mojom::Referrer::New(
web_contents.GetPrimaryMainFrame()->GetLastCommittedURL(),
network::mojom::ReferrerPolicy::kStrictOriginWhenCrossOrigin));
return navigation;
}
// Finish a prerendering navigation that was already started with
// CreateAndStartHost().
void CommitPrerenderNavigation(PrerenderHost& host) {
// Normally we could use EmbeddedTestServer to provide a response, but these
// tests use RenderViewHostImplTestHarness so the load goes through a
// TestNavigationURLLoader which we don't have access to in order to
// complete. Use NavigationSimulator to finish the navigation.
FrameTreeNode* ftn = FrameTreeNode::From(host.GetPrerenderedMainFrameHost());
std::unique_ptr<NavigationSimulator> sim =
NavigationSimulatorImpl::CreateFromPendingInFrame(ftn);
sim->Commit();
EXPECT_TRUE(host.is_ready_for_activation());
}
class PrerenderHostRegistryTest : public RenderViewHostImplTestHarness {
public:
PrerenderHostRegistryTest() = default;
~PrerenderHostRegistryTest() override = default;
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
web_contents_delegate_ =
std::make_unique<test::ScopedPrerenderWebContentsDelegate>(*contents());
contents()->NavigateAndCommit(GURL("https://example.com/"));
}
RenderFrameHostImpl* NavigatePrimaryPage(TestWebContents* web_contents,
const GURL& dest_url) {
std::unique_ptr<NavigationSimulatorImpl> navigation =
NavigationSimulatorImpl::CreateRendererInitiated(
dest_url, web_contents->GetPrimaryMainFrame());
navigation->SetTransition(ui::PAGE_TRANSITION_LINK);
navigation->Start();
navigation->Commit();
RenderFrameHostImpl* render_frame_host =
web_contents->GetPrimaryMainFrame();
EXPECT_EQ(render_frame_host->GetLastCommittedURL(), dest_url);
return render_frame_host;
}
// Helper method to test the navigation param matching logic which allows a
// prerender host to be used in a potential activation navigation only if its
// params match the potential activation navigation params. Use setup_callback
// to set the parameters. Returns true if the host was selected as a
// potential candidate for activation, and false otherwise.
[[nodiscard]] bool CheckIsActivatedForParams(
base::OnceCallback<void(NavigationSimulatorImpl*)> setup_callback) {
RenderFrameHostImpl* render_frame_host = contents()->GetPrimaryMainFrame();
const GURL kPrerenderingUrl("https://example.com/next");
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager, render_frame_host));
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
std::unique_ptr<NavigationSimulatorImpl> navigation =
NavigationSimulatorImpl::CreateRendererInitiated(kPrerenderingUrl,
render_frame_host);
// Set a default referrer policy that matches the initial prerender
// navigation.
// TODO(falken): Fix NavigationSimulatorImpl to do this itself.
navigation->SetReferrer(blink::mojom::Referrer::New(
contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
network::mojom::ReferrerPolicy::kStrictOriginWhenCrossOrigin));
// Change a parameter to differentiate the activation request from the
// prerendering request.
std::move(setup_callback).Run(navigation.get());
navigation->Start();
NavigationRequest* navigation_request = navigation->GetNavigationHandle();
// Use is_running_potential_prerender_activation_checks() instead of
// IsPrerenderedPageActivation() because the NavigationSimulator does not
// proceed past CommitDeferringConditions on potential activations,
// so IsPrerenderedPageActivation() will fail with a CHECK because
// prerender_frame_tree_node_id_ is not populated.
// TODO(crbug.com/40784651): Fix NavigationSimulator to wait for
// commit deferring conditions as it does throttles.
return navigation_request
->is_running_potential_prerender_activation_checks();
}
// Helper method to perform a prerender activation that includes specialized
// handling or setup on the initial prerender navigation via the
// setup_callback parameter.
void SetupPrerenderAndCommit(
base::OnceCallback<void(NavigationSimulatorImpl*)> setup_callback) {
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindNonReservedHostById(prerender_frame_tree_node_id);
// Complete the initial prerender navigation.
FrameTreeNode* ftn =
FrameTreeNode::From(prerender_host->GetPrerenderedMainFrameHost());
std::unique_ptr<NavigationSimulatorImpl> sim =
NavigationSimulatorImpl::CreateFromPendingInFrame(ftn);
std::move(setup_callback).Run(sim.get());
sim->Commit();
EXPECT_TRUE(prerender_host->is_ready_for_activation());
// Activate the prerendered page.
contents()->ActivatePrerenderedPage(kPrerenderingUrl);
}
PrerenderAttributes GeneratePrerenderAttributes(
const GURL& url,
PreloadingTriggerType trigger_type,
const std::string& embedder_histogram_suffix,
std::optional<blink::mojom::SpeculationEagerness> eagerness,
RenderFrameHostImpl* rfh) {
switch (trigger_type) {
case PreloadingTriggerType::kSpeculationRule:
case PreloadingTriggerType::kSpeculationRuleFromIsolatedWorld:
case PreloadingTriggerType::kSpeculationRuleFromAutoSpeculationRules:
return PrerenderAttributes(
url, trigger_type, embedder_histogram_suffix,
std::make_optional(SpeculationRulesParams(
blink::mojom::SpeculationTargetHint::kNoHint,
eagerness.value_or(blink::mojom::SpeculationEagerness::kEager),
SpeculationRulesTags())),
Referrer(),
/*no_vary_search_hint=*/std::nullopt, rfh, contents()->GetWeakPtr(),
ui::PAGE_TRANSITION_LINK,
/*should_warm_up_compositor=*/false,
/*should_prepare_paint_tree=*/false,
/*url_match_predicate=*/{},
/*prerender_navigation_handle_callback=*/{},
PreloadPipelineInfoImpl::Create(
/*planned_max_preloading_type=*/PreloadingType::kPrerender));
case PreloadingTriggerType::kEmbedder:
return PrerenderAttributes(
url, trigger_type, embedder_histogram_suffix,
/*speculation_rules_params=*/std::nullopt, Referrer(),
/*no_vary_search_hint=*/std::nullopt,
/*initiator_render_frame_host=*/nullptr, contents()->GetWeakPtr(),
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
/*should_warm_up_compositor=*/false,
/*should_prepare_paint_tree=*/false,
/*url_match_predicate=*/{},
/*prerender_navigation_handle_callback=*/{},
PreloadPipelineInfoImpl::Create(
/*planned_max_preloading_type=*/PreloadingType::kPrerender));
}
}
void ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus status,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectUniqueSample(
"Prerender.Experimental.PrerenderHostFinalStatus.SpeculationRule",
status, count);
}
void ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus status,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectBucketCount(
"Prerender.Experimental.PrerenderHostFinalStatus.SpeculationRule",
status, count);
}
void ExpectUniqueSampleOfEmbedderFinalStatus(
PrerenderFinalStatus status,
const std::string& embedder_histogram_suffix,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectUniqueSample(
"Prerender.Experimental.PrerenderHostFinalStatus.Embedder_" +
embedder_histogram_suffix,
status, count);
}
void ExpectBucketCountOfEmbedderFinalStatus(
PrerenderFinalStatus status,
const std::string& embedder_histogram_suffix,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectBucketCount(
"Prerender.Experimental.PrerenderHostFinalStatus.Embedder_" +
embedder_histogram_suffix,
status, count);
}
void ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch result,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectUniqueSample(
"Prerender.Experimental.ActivationNavigationParamsMatch."
"SpeculationRule",
result, count);
}
void ExpectBucketCountOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch result,
base::HistogramBase::Count32 count = 1) {
histogram_tester_.ExpectBucketCount(
"Prerender.Experimental.ActivationNavigationParamsMatch."
"SpeculationRule",
result, count);
}
PrerenderHostRegistry& registry() {
return *contents()->GetPrerenderHostRegistry();
}
base::HistogramTester& histogram_tester() { return histogram_tester_; }
private:
test::ScopedPrerenderFeatureList scoped_feature_list_;
base::HistogramTester histogram_tester_;
std::unique_ptr<test::ScopedPrerenderWebContentsDelegate>
web_contents_delegate_;
};
TEST_F(PrerenderHostRegistryTest, CreateAndStartHost_SpeculationRule) {
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
contents()->ActivatePrerenderedPage(kPrerenderingUrl);
// "Navigation.TimeToActivatePrerender.SpeculationRule" histogram should be
// recorded on every prerender activation.
histogram_tester().ExpectTotalCount(
"Navigation.TimeToActivatePrerender.SpeculationRule", 1u);
}
TEST_F(PrerenderHostRegistryTest, CreateAndStartHost_Embedder_DirectURLInput) {
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kEmbedder, "DirectURLInput",
std::nullopt, contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
contents()->ActivatePrerenderedPageFromAddressBar(kPrerenderingUrl);
// "Navigation.TimeToActivatePrerender.Embedder_DirectURLInput" histogram
// should be recorded on every prerender activation.
histogram_tester().ExpectTotalCount(
"Navigation.TimeToActivatePrerender.Embedder_DirectURLInput", 1u);
}
TEST_F(PrerenderHostRegistryTest, CreateAndStartHost_PreloadingConfigHoldback) {
content::test::PreloadingConfigOverride preloading_config_override;
preloading_config_override.SetHoldback(
PreloadingType::kPrerender,
content_preloading_predictor::kSpeculationRules, true);
const GURL kPrerenderingUrl("https://example.com/next");
auto* preloading_data = PreloadingData::GetOrCreateForWebContents(contents());
PreloadingURLMatchCallback same_url_matcher =
PreloadingData::GetSameURLMatcher(kPrerenderingUrl);
PreloadingAttempt* preloading_attempt = preloading_data->AddPreloadingAttempt(
content_preloading_predictor::kSpeculationRules,
PreloadingType::kPrerender, std::move(same_url_matcher),
contents()->GetPrimaryMainFrame()->GetPageUkmSourceId());
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(
GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()),
preloading_attempt);
EXPECT_TRUE(prerender_frame_tree_node_id.is_null());
}
TEST_F(PrerenderHostRegistryTest,
CreateAndStartHost_HoldbackOverride_Holdback) {
const GURL kPrerenderingUrl("https://example.com/next");
auto* preloading_data = PreloadingData::GetOrCreateForWebContents(contents());
PreloadingURLMatchCallback same_url_matcher =
PreloadingData::GetSameURLMatcher(kPrerenderingUrl);
PreloadingAttempt* preloading_attempt = preloading_data->AddPreloadingAttempt(
content_preloading_predictor::kSpeculationRules,
PreloadingType::kPrerender, std::move(same_url_matcher),
contents()->GetPrimaryMainFrame()->GetPageUkmSourceId());
auto attributes = GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame());
attributes.holdback_status_override = PreloadingHoldbackStatus::kHoldback;
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(attributes, preloading_attempt);
EXPECT_TRUE(prerender_frame_tree_node_id.is_null());
}
TEST_F(PrerenderHostRegistryTest, CreateAndStartHost_HoldbackOverride_Allowed) {
content::test::PreloadingConfigOverride preloading_config_override;
preloading_config_override.SetHoldback(
PreloadingType::kPrerender,
content_preloading_predictor::kSpeculationRules, true);
const GURL kPrerenderingUrl("https://example.com/next");
auto* preloading_data = PreloadingData::GetOrCreateForWebContents(contents());
PreloadingURLMatchCallback same_url_matcher =
PreloadingData::GetSameURLMatcher(kPrerenderingUrl);
PreloadingAttempt* preloading_attempt = preloading_data->AddPreloadingAttempt(
content_preloading_predictor::kSpeculationRules,
PreloadingType::kPrerender, std::move(same_url_matcher),
contents()->GetPrimaryMainFrame()->GetPageUkmSourceId());
auto attributes = GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame());
attributes.holdback_status_override = PreloadingHoldbackStatus::kAllowed;
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(attributes, preloading_attempt);
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
contents()->ActivatePrerenderedPage(kPrerenderingUrl);
// "Navigation.TimeToActivatePrerender.SpeculationRule" histogram should be
// recorded on every prerender activation.
histogram_tester().ExpectTotalCount(
"Navigation.TimeToActivatePrerender.SpeculationRule", 1u);
}
TEST_F(PrerenderHostRegistryTest, CreateAndStartHostForSameURL) {
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId frame_tree_node_id1 =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_TRUE(frame_tree_node_id1);
PrerenderHost* prerender_host1 =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
// Start the prerender host for the same URL. This second host should be
// ignored, and the first host should still be findable.
const FrameTreeNodeId frame_tree_node_id2 =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_TRUE(frame_tree_node_id2.is_null());
EXPECT_EQ(registry().FindHostByUrlForTesting(kPrerenderingUrl),
prerender_host1);
CommitPrerenderNavigation(*prerender_host1);
contents()->ActivatePrerenderedPage(kPrerenderingUrl);
}
class PrerenderHostRegistryLimitTest : public PrerenderHostRegistryTest {
public:
PrerenderHostRegistryLimitTest() {
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kPrerender2NewLimitAndScheduler,
{{"max_num_of_running_speculation_rules_eager_prerenders",
base::NumberToString(MaxNumOfRunningSpeculationRules())}}}},
{});
}
int MaxNumOfRunningSpeculationRules() { return 2; }
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests that PrerenderHostRegistry limits the number of started prerenders
// to a specific number, and after once the prerender page was activated,
// PrerenderHostRegistry can start prerendering a new one.
TEST_F(PrerenderHostRegistryLimitTest, NumberLimit_Activation) {
std::vector<FrameTreeNodeId> frame_tree_node_ids;
std::vector<GURL> prerendering_ulrs;
for (int i = 0; i < MaxNumOfRunningSpeculationRules() + 1; i++) {
const GURL prerendering_url("https://example.com/next" +
base::NumberToString(i));
FrameTreeNodeId frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
prerendering_url, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
frame_tree_node_ids.push_back(frame_tree_node_id);
prerendering_ulrs.push_back(prerendering_url);
}
// PrerenderHostRegistry should only start prerendering within the limit.
for (int i = 0; i < MaxNumOfRunningSpeculationRules(); i++) {
EXPECT_TRUE(frame_tree_node_ids[i]);
}
EXPECT_TRUE(frame_tree_node_ids[MaxNumOfRunningSpeculationRules()].is_null());
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
// Activate the first prerender.
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(prerendering_ulrs[0]);
CommitPrerenderNavigation(*prerender_host);
contents()->ActivatePrerenderedPage(prerendering_ulrs[0]);
// After the first prerender page was activated, PrerenderHostRegistry can
// start prerendering a new one.
FrameTreeNodeId frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
prerendering_ulrs[MaxNumOfRunningSpeculationRules()],
PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_TRUE(frame_tree_node_id);
ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
}
// Tests that PrerenderHostRegistry limits the number of started prerenders
// to a specific number, and new candidates can be processed after the initiator
// page navigates to a new same-origin page.
TEST_F(PrerenderHostRegistryLimitTest, NumberLimit_SameOriginNavigateAway) {
RenderFrameHostImpl* render_frame_host = contents()->GetPrimaryMainFrame();
ASSERT_TRUE(render_frame_host);
mojo::Remote<blink::mojom::SpeculationHost> remote1;
SpeculationHostImpl::Bind(render_frame_host,
remote1.BindNewPipeAndPassReceiver());
ASSERT_TRUE(remote1.is_connected());
std::vector<GURL> prerendering_urls;
for (int i = 0; i < MaxNumOfRunningSpeculationRules() + 1; i++) {
prerendering_urls.emplace_back("https://example.com/next" +
base::NumberToString(i));
}
SendCandidates(prerendering_urls, remote1);
// PrerenderHostRegistry should only start prerenderings within the limit.
for (int i = 0; i < MaxNumOfRunningSpeculationRules(); i++) {
ASSERT_NE(registry().FindHostByUrlForTesting(prerendering_urls[i]),
nullptr);
}
ASSERT_EQ(registry().FindHostByUrlForTesting(
prerendering_urls[MaxNumOfRunningSpeculationRules()]),
nullptr);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
// The initiator document navigates away.
render_frame_host =
NavigatePrimaryPage(contents(), GURL("https://example.com/elsewhere"));
// After the initiator page navigates away, the started prerendering should be
// cancelled, and PrerenderHostRegistry can start prerendering a new one.
for (int i = 0; i < MaxNumOfRunningSpeculationRules() + 1; i++) {
EXPECT_EQ(registry().FindHostByUrlForTesting(prerendering_urls[i]),
nullptr);
}
mojo::Remote<blink::mojom::SpeculationHost> remote2;
SpeculationHostImpl::Bind(render_frame_host,
remote2.BindNewPipeAndPassReceiver());
SendCandidate(prerendering_urls[MaxNumOfRunningSpeculationRules()], remote2);
EXPECT_NE(registry().FindHostByUrlForTesting(
prerendering_urls[MaxNumOfRunningSpeculationRules()]),
nullptr);
ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
}
// Tests that PrerenderHostRegistry limits the number of started prerenders
// to a specific number, and new candidates can be processed after the initiator
// page navigates to a new cross-origin page.
TEST_F(PrerenderHostRegistryLimitTest, NumberLimit_CrossOriginNavigateAway) {
RenderFrameHostImpl* render_frame_host = contents()->GetPrimaryMainFrame();
ASSERT_TRUE(render_frame_host);
mojo::Remote<blink::mojom::SpeculationHost> remote1;
SpeculationHostImpl::Bind(render_frame_host,
remote1.BindNewPipeAndPassReceiver());
ASSERT_TRUE(remote1.is_connected());
std::vector<GURL> prerendering_urls;
for (int i = 0; i < MaxNumOfRunningSpeculationRules() + 1; i++) {
prerendering_urls.emplace_back("https://example.com/next" +
base::NumberToString(i));
}
SendCandidates(prerendering_urls, remote1);
// PrerenderHostRegistry should only start prerenderings within the limit.
for (int i = 0; i < MaxNumOfRunningSpeculationRules(); i++) {
ASSERT_NE(registry().FindHostByUrlForTesting(prerendering_urls[i]),
nullptr);
}
ASSERT_EQ(registry().FindHostByUrlForTesting(
prerendering_urls[MaxNumOfRunningSpeculationRules()]),
nullptr);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
// The initiator document navigates away to a cross-origin page.
render_frame_host =
NavigatePrimaryPage(contents(), GURL("https://example.org/"));
// After the initiator page navigates away, the started prerendering should be
// cancelled, and PrerenderHostRegistry can start prerendering a new one.
for (int i = 0; i < MaxNumOfRunningSpeculationRules() + 1; i++) {
EXPECT_EQ(registry().FindHostByUrlForTesting(prerendering_urls[i]),
nullptr);
}
mojo::Remote<blink::mojom::SpeculationHost> remote2;
SpeculationHostImpl::Bind(render_frame_host,
remote2.BindNewPipeAndPassReceiver());
const GURL prerendering_url("https://example.org/next");
SendCandidate(prerendering_url, remote2);
EXPECT_NE(registry().FindHostByUrlForTesting(prerendering_url), nullptr);
ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded);
}
class PrerenderHostRegistryNewLimitAndSchedulerTest
: public PrerenderHostRegistryTest,
public testing::WithParamInterface<bool> {
public:
using PrerenderLimitGroup = PrerenderHostRegistry::PrerenderLimitGroup;
PrerenderHostRegistryNewLimitAndSchedulerTest() {
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kPrerender2NewLimitAndScheduler,
{{"max_num_of_running_speculation_rules_eager_prerenders",
base::NumberToString(
MaxNumOfRunningSpeculationRulesEagerPrerenders())},
{"max_num_of_running_speculation_rules_non_eager_prerenders",
base::NumberToString(
MaxNumOfRunningSpeculationRulesNonEagerPrerenders())},
{"max_num_of_running_embedder_prerenders",
base::NumberToString(MaxNumOfRunningEmbedderPrerenders())}}}},
{});
}
int MaxNumOfRunningSpeculationRulesEagerPrerenders() { return 2; }
int MaxNumOfRunningSpeculationRulesNonEagerPrerenders() { return 2; }
int MaxNumOfRunningEmbedderPrerenders() { return 2; }
const std::string embedder_histogram_suffix = "EmbedderSuffixForTest";
bool IsNewTabTrigger(PrerenderLimitGroup limit_group) {
return GetParam() && limit_group != PrerenderLimitGroup::kEmbedder;
}
FrameTreeNodeId CreateAndStartHostByLimitGroup(
PrerenderLimitGroup limit_group) {
static int unique_id = 0;
const GURL prerendering_url("https://example.com/next_" +
base::NumberToString(unique_id));
unique_id++;
auto prerender_attributes = [&] {
switch (limit_group) {
case PrerenderLimitGroup::kSpeculationRulesEager:
return GeneratePrerenderAttributes(
prerendering_url, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame());
case PrerenderLimitGroup::kSpeculationRulesNonEager:
return GeneratePrerenderAttributes(
prerendering_url, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kModerate,
contents()->GetPrimaryMainFrame());
case PrerenderLimitGroup::kEmbedder:
return GeneratePrerenderAttributes(
prerendering_url, PreloadingTriggerType::kEmbedder,
embedder_histogram_suffix, std::nullopt, nullptr);
}
}();
PreloadingPredictor embedder_predictor(100, "Embedder");
PreloadingPredictor creating_predictor = [&] {
switch (limit_group) {
case PrerenderLimitGroup::kSpeculationRulesEager:
case PrerenderLimitGroup::kSpeculationRulesNonEager:
return content_preloading_predictor::kSpeculationRules;
case PrerenderLimitGroup::kEmbedder:
return embedder_predictor;
}
}();
PreloadingPredictor enacting_predictor = [&] {
switch (limit_group) {
case PrerenderLimitGroup::kSpeculationRulesEager:
return content_preloading_predictor::kSpeculationRules;
case PrerenderLimitGroup::kSpeculationRulesNonEager:
// Arbitrarily chosen non-eager predictor.
return preloading_predictor::kUrlPointerDownOnAnchor;
case PrerenderLimitGroup::kEmbedder:
return embedder_predictor;
}
}();
return IsNewTabTrigger(limit_group)
? registry().CreateAndStartHostForNewTab(
prerender_attributes, creating_predictor,
enacting_predictor, PreloadingConfidence{100})
: registry().CreateAndStartHost(prerender_attributes);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
PrerenderHostRegistryNewLimitAndSchedulerTest,
testing::Bool());
// Tests the behavior of eager prerenders with the new limit and scheduler.
TEST_P(PrerenderHostRegistryNewLimitAndSchedulerTest,
NewLimitAndScheduler_Eager) {
// Starts the eager prerenders as many times as the specific limit.
for (int i = 0; i < MaxNumOfRunningSpeculationRulesEagerPrerenders(); i++) {
FrameTreeNodeId frame_tree_node_id = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesEager);
EXPECT_TRUE(frame_tree_node_id);
}
// If we try to start eager prerenders after reaching the limit, that should
// be canceled with kMaxNumOfRunningEagerPrerendersExceeded.
FrameTreeNodeId frame_tree_node_id_eager_exceeded =
CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesEager);
EXPECT_TRUE(frame_tree_node_id_eager_exceeded.is_null());
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded, 1);
// On the other hand, prerenders belonging to different limit
// group(non-eager, embedder) can still be started.
FrameTreeNodeId frame_tree_node_id_non_eager = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesNonEager);
FrameTreeNodeId frame_tree_node_id_embedder =
CreateAndStartHostByLimitGroup(PrerenderLimitGroup::kEmbedder);
EXPECT_TRUE(frame_tree_node_id_non_eager);
EXPECT_TRUE(frame_tree_node_id_embedder);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded, 1);
ExpectUniqueSampleOfEmbedderFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEmbedderPrerendersExceeded,
embedder_histogram_suffix, 0);
}
// Tests the behavior of non-eager prerenders with the new limit and scheduler.
TEST_P(PrerenderHostRegistryNewLimitAndSchedulerTest,
NewLimitAndScheduler_NonEager) {
std::vector<FrameTreeNodeId> started_prerender_ids;
// Starts the non-eager prerenders as many times as the specific limit.
for (int i = 0; i < MaxNumOfRunningSpeculationRulesNonEagerPrerenders();
i++) {
FrameTreeNodeId frame_tree_node_id = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesNonEager);
started_prerender_ids.push_back(frame_tree_node_id);
EXPECT_TRUE(frame_tree_node_id);
}
// Even after the limit of non-eager speculation rules is reached, it is
// permissible to start a new prerender. Instead, the oldest prerender will be
// canceled with kMaxNumOfRunningNonEagerPrerendersExceeded to make room for a
// new one.
FrameTreeNodeId frame_tree_node_id_non_eager_exceeded =
CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesNonEager);
ASSERT_TRUE(frame_tree_node_id_non_eager_exceeded);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningNonEagerPrerendersExceeded, 1);
for (auto id : started_prerender_ids) {
auto* web_contents_impl =
static_cast<WebContentsImpl*>(WebContents::FromFrameTreeNodeId(id));
PrerenderHost* prerender_host = nullptr;
if (web_contents_impl) {
prerender_host = web_contents_impl->GetPrerenderHostRegistry()
->FindNonReservedHostById(id);
}
if (id == started_prerender_ids[0]) {
// The oldest prerender has been canceled.
EXPECT_EQ(prerender_host, nullptr);
} else {
EXPECT_NE(prerender_host, nullptr);
}
}
// On the other hand, prerenders belonging to different limit group(eager,
// embedder) can still be started and not invoke cancellation, as these limits
// are separated.
FrameTreeNodeId frame_tree_node_id_eager = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesEager);
FrameTreeNodeId frame_tree_node_id_embedder =
CreateAndStartHostByLimitGroup(PrerenderLimitGroup::kEmbedder);
EXPECT_TRUE(frame_tree_node_id_eager);
EXPECT_TRUE(frame_tree_node_id_embedder);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningNonEagerPrerendersExceeded, 1);
ExpectUniqueSampleOfEmbedderFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEmbedderPrerendersExceeded,
embedder_histogram_suffix, 0);
}
// Tests the behavior of embedder prerenders with the limit.
TEST_P(PrerenderHostRegistryNewLimitAndSchedulerTest,
NewLimitAndScheduler_Embedder) {
// Starts the embedder prerenders as many times as the specific limit.
for (int i = 0; i < MaxNumOfRunningEmbedderPrerenders(); i++) {
FrameTreeNodeId frame_tree_node_id =
CreateAndStartHostByLimitGroup(PrerenderLimitGroup::kEmbedder);
EXPECT_TRUE(frame_tree_node_id);
}
// If we try to start embedder prerenders after reaching the limit, that
// should be canceled with kMaxNumOfRunningEmbedderPrerendersExceeded.
FrameTreeNodeId frame_tree_node_id_embedder_exceeded =
CreateAndStartHostByLimitGroup(PrerenderLimitGroup::kEmbedder);
EXPECT_TRUE(frame_tree_node_id_embedder_exceeded.is_null());
ExpectUniqueSampleOfEmbedderFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEmbedderPrerendersExceeded,
embedder_histogram_suffix, 1);
// On the other hand, prerenders belonging to different limit group(eager,
// non-egaer) can still be started.
FrameTreeNodeId frame_tree_node_id_eager = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesEager);
FrameTreeNodeId frame_tree_node_id_non_eager = CreateAndStartHostByLimitGroup(
PrerenderLimitGroup::kSpeculationRulesNonEager);
EXPECT_TRUE(frame_tree_node_id_eager);
EXPECT_TRUE(frame_tree_node_id_non_eager);
ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEagerPrerendersExceeded, 0);
ExpectBucketCountOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningNonEagerPrerendersExceeded, 0);
ExpectUniqueSampleOfEmbedderFinalStatus(
PrerenderFinalStatus::kMaxNumOfRunningEmbedderPrerendersExceeded,
embedder_histogram_suffix, 1);
}
TEST_F(PrerenderHostRegistryTest,
ReserveHostToActivateBeforeReadyForActivation) {
const GURL original_url = contents()->GetLastCommittedURL();
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
FrameTreeNode* ftn =
FrameTreeNode::From(prerender_host->GetPrerenderedMainFrameHost());
std::unique_ptr<NavigationSimulatorImpl> sim =
NavigationSimulatorImpl::CreateFromPendingInFrame(ftn);
// Ensure that navigation in prerendering frame tree does not commit and
// PrerenderHost doesn't become ready for activation.
sim->SetAutoAdvance(false);
EXPECT_FALSE(prerender_host->is_ready_for_activation());
test::PrerenderHostObserver prerender_host_observer(*contents(),
kPrerenderingUrl);
// Start activation.
std::unique_ptr<NavigationSimulatorImpl> navigation =
CreateActivation(kPrerenderingUrl, *contents());
navigation->Start();
// Wait until PrerenderCommitDeferringCondition runs.
// TODO(nhiroki): Avoid using base::RunUntilIdle() and instead use some
// explicit signal of the running condition.
base::RunLoop().RunUntilIdle();
// The activation should be deferred by PrerenderCommitDeferringCondition
// until the main frame navigation in the prerendering frame tree finishes.
NavigationRequest* navigation_request = navigation->GetNavigationHandle();
EXPECT_TRUE(
navigation_request->IsCommitDeferringConditionDeferredForTesting());
EXPECT_FALSE(prerender_host_observer.was_activated());
EXPECT_EQ(contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
original_url);
// Finish the main frame navigation.
sim->Commit();
// Finish the activation.
prerender_host_observer.WaitForDestroyed();
EXPECT_TRUE(prerender_host_observer.was_activated());
EXPECT_EQ(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
EXPECT_EQ(contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
kPrerenderingUrl);
}
TEST_F(PrerenderHostRegistryTest, CancelHost) {
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_NE(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
registry().CancelHost(prerender_frame_tree_node_id,
PrerenderFinalStatus::kDestroyed);
EXPECT_EQ(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
}
// Test cancelling a prerender while a CommitDeferringCondition is running.
// This activation should fall back to a regular navigation.
TEST_F(PrerenderHostRegistryTest,
CancelHostWhileCommitDeferringConditionIsRunning) {
const GURL original_url = contents()->GetLastCommittedURL();
// Start prerendering.
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
test::PrerenderHostObserver prerender_host_observer(*contents(),
kPrerenderingUrl);
// Now navigate the primary page to the prerendered URL so that we activate
// the prerender. Use a CommitDeferringCondition to pause activation
// before it completes.
std::unique_ptr<NavigationSimulatorImpl> navigation;
{
MockCommitDeferringConditionInstaller installer(
kPrerenderingUrl, CommitDeferringCondition::Result::kDefer);
// Start trying to activate the prerendered page.
navigation = CreateActivation(kPrerenderingUrl, *contents());
navigation->Start();
// Wait for the condition to pause the activation.
installer.WaitUntilInstalled();
installer.condition().WaitUntilInvoked();
// The request should be deferred by the condition.
auto* navigation_request =
static_cast<NavigationRequest*>(navigation->GetNavigationHandle());
EXPECT_TRUE(
navigation_request->IsCommitDeferringConditionDeferredForTesting());
// The primary page should still be the original page.
EXPECT_EQ(contents()->GetLastCommittedURL(), original_url);
// Cancel the prerender while the CommitDeferringCondition is running.
registry().CancelHost(prerender_frame_tree_node_id,
PrerenderFinalStatus::kDestroyed);
prerender_host_observer.WaitForDestroyed();
EXPECT_FALSE(prerender_host_observer.was_activated());
EXPECT_EQ(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
// Resume the activation. This should fall back to a regular navigation.
installer.condition().CallResumeClosure();
}
navigation->Commit();
EXPECT_EQ(contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
kPrerenderingUrl);
}
// Test cancelling a prerender and then starting a new prerender for the same
// URL while a CommitDeferringCondition is running. This activation should not
// reserve the second prerender and should fall back to a regular navigation.
TEST_F(PrerenderHostRegistryTest,
CancelAndStartHostWhileCommitDeferringConditionIsRunning) {
const GURL original_url = contents()->GetLastCommittedURL();
const GURL kPrerenderingUrl("https://example.com/next");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
test::PrerenderHostObserver prerender_host_observer(*contents(),
kPrerenderingUrl);
// Now navigate the primary page to the prerendered URL so that we activate
// the prerender. Use a CommitDeferringCondition to pause activation
// before it completes.
std::unique_ptr<NavigationSimulatorImpl> navigation;
base::OnceClosure resume_navigation;
{
MockCommitDeferringConditionInstaller installer(
kPrerenderingUrl, CommitDeferringCondition::Result::kDefer);
// Start trying to activate the prerendered page.
navigation = CreateActivation(kPrerenderingUrl, *contents());
navigation->Start();
// Wait for the condition to pause the activation.
installer.WaitUntilInstalled();
installer.condition().WaitUntilInvoked();
resume_navigation = installer.condition().TakeResumeClosure();
// The request should be deferred by the condition.
auto* navigation_request =
static_cast<NavigationRequest*>(navigation->GetNavigationHandle());
EXPECT_TRUE(
navigation_request->IsCommitDeferringConditionDeferredForTesting());
// The primary page should still be the original page.
EXPECT_EQ(contents()->GetLastCommittedURL(), original_url);
// Cancel the prerender while the CommitDeferringCondition is running.
registry().CancelHost(prerender_frame_tree_node_id,
PrerenderFinalStatus::kDestroyed);
prerender_host_observer.WaitForDestroyed();
EXPECT_FALSE(prerender_host_observer.was_activated());
EXPECT_EQ(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
}
{
// Start the second prerender for the same URL.
const FrameTreeNodeId prerender_frame_tree_node_id2 =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id2);
PrerenderHost* prerender_host2 =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host2);
EXPECT_NE(prerender_frame_tree_node_id, prerender_frame_tree_node_id2);
}
// Resume the initial activation. This should not reserve the second
// prerender and should fall back to a regular navigation.
std::move(resume_navigation).Run();
navigation->Commit();
EXPECT_EQ(contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
kPrerenderingUrl);
// The second prerender should still exist.
EXPECT_NE(registry().FindHostByUrlForTesting(kPrerenderingUrl), nullptr);
}
// Tests that prerendering should be canceled if the trigger is in the
// background and its type is kEmbedder.
// For the case where the trigger type is speculation rules,
// browsertests `TestSequentialPrerenderingInBackground` covers it.
TEST_F(PrerenderHostRegistryTest,
DontStartPrerenderWhenEmbedderTriggerIsAlreadyHidden) {
// The visibility state to be HIDDEN will cause prerendering not started when
// trigger type is kEmbedder.
contents()->WasHidden();
const GURL kPrerenderingUrl = GURL("https://example.com/empty.html");
RenderFrameHostImpl* initiator_rfh = contents()->GetPrimaryMainFrame();
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kEmbedder, "DirectURLInput",
std::nullopt, initiator_rfh));
EXPECT_TRUE(prerender_frame_tree_node_id.is_null());
PrerenderHost* prerender_host =
registry().FindNonReservedHostById(prerender_frame_tree_node_id);
EXPECT_EQ(prerender_host, nullptr);
histogram_tester().ExpectUniqueSample(
"Prerender.Experimental.PrerenderHostFinalStatus.Embedder_DirectURLInput",
PrerenderFinalStatus::kTriggerBackgrounded, 1u);
}
// -------------------------------------------------
// Activation navigation parameter matching unit tests.
// These tests change a parameter to differentiate the activation request from
// the prerendering request.
// A positive test to show that if the navigation params are equal then the
// prerender host is selected for activation.
TEST_F(PrerenderHostRegistryTest, SameInitialAndActivationParams) {
EXPECT_TRUE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
// Do not change any params, so activation happens.
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kOk);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_InitiatorFrameToken) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
const GURL kOriginalUrl("https://example.com/");
navigation->SetInitiatorFrame(nullptr);
navigation->set_initiator_origin(url::Origin::Create(kOriginalUrl));
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kInitiatorFrameToken);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_Headers) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_request_headers("User-Agent: Test");
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kHttpRequestHeader);
}
// Tests that the Purpose header is ignored when comparing request headers.
TEST_F(PrerenderHostRegistryTest, PurposeHeaderIsIgnoredForParamMatching) {
EXPECT_TRUE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_request_headers("Purpose: Test");
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kOk);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_LoadFlags) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_load_flags(net::LOAD_ONLY_FROM_CACHE);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kLoadFlags);
// If the potential activation request requires validation or bypass of the
// browser cache, the prerendered page should not be activated.
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_load_flags(net::LOAD_VALIDATE_CACHE);
})));
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_load_flags(net::LOAD_BYPASS_CACHE);
})));
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_load_flags(net::LOAD_DISABLE_CACHE);
})));
ExpectBucketCountOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kCacheLoadFlags, 3);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_SkipServiceWorker) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_skip_service_worker(true);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kSkipServiceWorker);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_MixedContentContextType) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_mixed_content_context_type(
blink::mojom::MixedContentContextType::kNotMixedContent);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kMixedContentContextType);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_IsFormSubmission) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->SetIsFormSubmission(true);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kIsFormSubmission);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_SearchableFormUrl) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
const GURL kOriginalUrl("https://example.com/");
navigation->set_searchable_form_url(kOriginalUrl);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kSearchableFormUrl);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationBeginParams_SearchableFormEncoding) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_searchable_form_encoding("Test encoding");
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kSearchableFormEncoding);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_InitiatorOrigin) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_initiator_origin(url::Origin());
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kInitiatorOrigin);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_ShouldNotCheckMainWorldCSP) {
// Initial navigation blocked by the main world CSP cancels prerendering.
// So, it's safe to match the page for CSP bypassing requests from isolated
// worlds (e.g., extensions).
EXPECT_TRUE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_should_check_main_world_csp(
network::mojom::CSPDisposition::DO_NOT_CHECK);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kOk);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_Method) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->SetMethod("POST");
})));
// The method parameter change is detected as a HTTP request header change.
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kHttpRequestHeader);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_HrefTranslate) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_href_translate("test");
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kHrefTranslate);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_Transition) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->SetTransition(ui::PAGE_TRANSITION_FORM_SUBMIT);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kTransition);
histogram_tester().ExpectUniqueSample(
"Prerender.Experimental.ActivationTransitionMismatch.SpeculationRule",
ui::PAGE_TRANSITION_FORM_SUBMIT, 1);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_RequestContextType) {
EXPECT_FALSE(CheckIsActivatedForParams(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_request_context_type(
blink::mojom::RequestContextType::AUDIO);
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kRequestContextType);
}
TEST_F(PrerenderHostRegistryTest,
CompareInitialAndActivationCommonParams_ReferrerPolicy) {
EXPECT_TRUE(CheckIsActivatedForParams(
base::BindLambdaForTesting([&](NavigationSimulatorImpl* navigation) {
navigation->SetReferrer(blink::mojom::Referrer::New(
contents()->GetPrimaryMainFrame()->GetLastCommittedURL(),
network::mojom::ReferrerPolicy::kAlways));
})));
ExpectUniqueSampleOfActivationNavigationParamsMatch(
PrerenderHost::ActivationNavigationParamsMatch::kOk);
}
// End navigation parameter matching tests ---------
// Begin replication state matching tests ----------
TEST_F(PrerenderHostRegistryTest, InsecureRequestPolicyIsSetWhilePrerendering) {
SetupPrerenderAndCommit(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_insecure_request_policy(
blink::mojom::InsecureRequestPolicy::kBlockAllMixedContent);
}));
EXPECT_EQ(contents()
->GetPrimaryMainFrame()
->frame_tree_node()
->current_replication_state()
.insecure_request_policy,
blink::mojom::InsecureRequestPolicy::kBlockAllMixedContent);
}
TEST_F(PrerenderHostRegistryTest,
InsecureNavigationsSetIsSetWhilePrerendering) {
SetupPrerenderAndCommit(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
const std::vector<uint32_t> insecure_navigations = {1, 2};
navigation->set_insecure_navigations_set(insecure_navigations);
}));
const std::vector<uint32_t> insecure_navigations = {1, 2};
EXPECT_EQ(contents()
->GetPrimaryMainFrame()
->frame_tree_node()
->current_replication_state()
.insecure_navigations_set,
insecure_navigations);
}
TEST_F(PrerenderHostRegistryTest,
HasPotentiallyTrustworthyUniqueOriginIsSetWhilePrerendering) {
SetupPrerenderAndCommit(
base::BindLambdaForTesting([](NavigationSimulatorImpl* navigation) {
navigation->set_has_potentially_trustworthy_unique_origin(true);
}));
EXPECT_TRUE(contents()
->GetPrimaryMainFrame()
->frame_tree_node()
->current_replication_state()
.has_potentially_trustworthy_unique_origin);
}
// End replication state matching tests ------------
TEST_F(PrerenderHostRegistryTest, OneTaskToDeleteAllHosts) {
std::vector<FrameTreeNodeId> frame_tree_node_ids;
std::vector<std::unique_ptr<test::PrerenderHostObserver>>
prerender_host_observers;
for (int i = 0; i < 2; i++) {
const GURL prerendering_url("https://example.com/next" +
base::NumberToString(i));
FrameTreeNodeId frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
prerendering_url, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
prerender_host_observers.emplace_back(
std::make_unique<test::PrerenderHostObserver>(*contents(),
frame_tree_node_id));
frame_tree_node_ids.push_back(frame_tree_node_id);
}
int pending_task_before_posting_abandon_task =
task_environment()->GetPendingMainThreadTaskCount();
registry().CancelHosts(
frame_tree_node_ids,
PrerenderCancellationReason(PrerenderFinalStatus::kDestroyed));
int pending_task_after_posting_abandon_task =
task_environment()->GetPendingMainThreadTaskCount();
// Only one task was posted.
EXPECT_EQ(pending_task_before_posting_abandon_task + 1,
pending_task_after_posting_abandon_task);
for (auto& observer : prerender_host_observers) {
// All PrerenderHosts were deleted, so it should not timeout.
observer->WaitForDestroyed();
}
}
TEST_F(PrerenderHostRegistryTest, DisallowPageHavingEffectiveUrl_TriggerUrl) {
const GURL original_url = contents()->GetLastCommittedURL();
const GURL kModifiedSiteUrl("custom-scheme://custom");
// Let the trigger's URL have the effective URL.
EffectiveURLContentBrowserClient modified_client(
original_url, kModifiedSiteUrl,
/*requires_dedicated_process=*/false);
ContentBrowserClient* old_client =
SetBrowserClientForTesting(&modified_client);
// Start prerendering. This should fail as the initiator's URL has the
// effective URL.
const GURL kPrerenderingUrl("https://example.com/empty.html");
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_TRUE(prerender_frame_tree_node_id.is_null());
PrerenderHost* prerender_host =
registry().FindNonReservedHostById(prerender_frame_tree_node_id);
EXPECT_EQ(prerender_host, nullptr);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kTriggerUrlHasEffectiveUrl);
SetBrowserClientForTesting(old_client);
}
TEST_F(PrerenderHostRegistryTest,
DisallowPageHavingEffectiveUrl_PrerenderingUrl) {
const GURL original_url = contents()->GetLastCommittedURL();
const GURL kPrerenderingUrl("https://example.com/empty.html");
const GURL kModifiedSiteUrl("custom-scheme://custom");
// Let the prerendering URL have the effective URL.
EffectiveURLContentBrowserClient modified_client(
kPrerenderingUrl, kModifiedSiteUrl,
/*requires_dedicated_process=*/false);
ContentBrowserClient* old_client =
SetBrowserClientForTesting(&modified_client);
// Start prerendering. This should fail as the prerendering URL has the
// effective URL.
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
EXPECT_TRUE(prerender_frame_tree_node_id.is_null());
PrerenderHost* prerender_host =
registry().FindNonReservedHostById(prerender_frame_tree_node_id);
EXPECT_EQ(prerender_host, nullptr);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kPrerenderingUrlHasEffectiveUrl);
SetBrowserClientForTesting(old_client);
}
TEST_F(PrerenderHostRegistryTest,
DisallowPageHavingEffectiveUrl_ActivationUrl) {
const GURL original_url = contents()->GetLastCommittedURL();
const GURL kPrerenderingUrl("https://example.com/empty.html");
const GURL kModifiedSiteUrl("custom-scheme://custom");
// Start prerendering.
const FrameTreeNodeId prerender_frame_tree_node_id =
registry().CreateAndStartHost(GeneratePrerenderAttributes(
kPrerenderingUrl, PreloadingTriggerType::kSpeculationRule, "",
blink::mojom::SpeculationEagerness::kEager,
contents()->GetPrimaryMainFrame()));
ASSERT_TRUE(prerender_frame_tree_node_id);
PrerenderHost* prerender_host =
registry().FindHostByUrlForTesting(kPrerenderingUrl);
CommitPrerenderNavigation(*prerender_host);
// Let the prerendering URL have the effective URL after prerendering.
EffectiveURLContentBrowserClient modified_client(
kPrerenderingUrl, kModifiedSiteUrl,
/*requires_dedicated_process=*/false);
ContentBrowserClient* old_client =
SetBrowserClientForTesting(&modified_client);
// Navigate the primary page to the prerendering URL that has the effective
// URL. This should fail to activate the prerendered page.
contents()->NavigateAndCommit(kPrerenderingUrl);
ExpectUniqueSampleOfSpeculationRuleFinalStatus(
PrerenderFinalStatus::kActivationUrlHasEffectiveUrl);
SetBrowserClientForTesting(old_client);
}
} // namespace
} // namespace content
|