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 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <limits>
#include <memory>
#include <vector>
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/spare_render_process_host_manager_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/scoped_web_ui_controller_factory_registration.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_ui_browsertest_util.h"
#include "content/test/storage_partition_test_helpers.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/frame/frame_policy.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom.h"
#include "ui/webui/untrusted_web_ui_browsertest_util.h"
namespace content {
class RenderProcessHostUnitTest : public RenderViewHostImplTestHarness {
public:
scoped_refptr<SiteInstanceImpl> CreateForUrl(const GURL& url) {
return SiteInstanceImpl::CreateForTesting(browser_context(), url);
}
scoped_refptr<SiteInstanceImpl> CreateForServiceWorker(
const GURL& url,
bool can_reuse_process = false) {
return SiteInstanceImpl::CreateForServiceWorker(
browser_context(),
UrlInfo::CreateForTesting(url,
CreateStoragePartitionConfigForTesting()),
can_reuse_process);
}
};
// Tests that guest RenderProcessHosts are not considered suitable hosts when
// searching for RenderProcessHost.
TEST_F(RenderProcessHostUnitTest, GuestsAreNotSuitableHosts) {
GURL test_url("http://foo.com");
MockRenderProcessHost guest_host(browser_context(),
/*is_for_guest_only=*/true);
scoped_refptr<SiteInstanceImpl> site_instance = CreateForUrl(test_url);
EXPECT_FALSE(RenderProcessHostImpl::IsSuitableHost(
&guest_host, site_instance->GetIsolationContext(),
site_instance->GetSiteInfo()));
EXPECT_TRUE(RenderProcessHostImpl::IsSuitableHost(
process(), site_instance->GetIsolationContext(),
site_instance->GetSiteInfo()));
EXPECT_EQ(process(),
RenderProcessHostImpl::GetExistingProcessHost(site_instance.get()));
}
// Test that an overridden process limit takes effect on all platforms.
TEST_F(RenderProcessHostUnitTest, RendererProcessLimitOverride) {
const size_t overridden_limit = 50;
RenderProcessHost::SetMaxRendererProcessCount(overridden_limit);
// Add dummy process hosts to saturate the limit.
std::vector<std::unique_ptr<MockRenderProcessHost>> hosts;
for (size_t i = 0; i < overridden_limit; ++i) {
hosts.push_back(std::make_unique<MockRenderProcessHost>(browser_context()));
}
// Verify that the renderer sharing will happen.
EXPECT_TRUE(RenderProcessHostImpl::IsProcessLimitReached());
// Reset the override to avoid interfering with other tests.
RenderProcessHost::SetMaxRendererProcessCount(0);
}
#if !BUILDFLAG(IS_ANDROID)
// Test that using more than half of the system's process limit will be
// considered going over Chrome's process limit, on non-Android platforms.
TEST_F(RenderProcessHostUnitTest, RendererProcessLimit) {
// This is half of the system's process limit, or a fallback value (82) if the
// system's limit is unknown.
const size_t max_platform_renderer_process_count =
RenderProcessHostImpl::GetPlatformMaxRendererProcessCount();
// Verify that Chrome's process limit is between 1 and
// |max_platform_renderer_process_count|.
EXPECT_GT(RenderProcessHostImpl::GetMaxRendererProcessCount(), 0u);
EXPECT_LE(RenderProcessHostImpl::GetMaxRendererProcessCount(),
max_platform_renderer_process_count);
// Add dummy process hosts to saturate the limit.
ASSERT_NE(0u, max_platform_renderer_process_count);
std::vector<std::unique_ptr<MockRenderProcessHost>> hosts;
for (size_t i = 0; i < max_platform_renderer_process_count; ++i) {
hosts.push_back(std::make_unique<MockRenderProcessHost>(browser_context()));
}
if (base::FeatureList::IsEnabled(features::kRemoveRendererProcessLimit)) {
// The RemoveRendererProcessLimit feature allows the full system process
// limit to be used, so creating half of the limit above is not enough to
// reach the limit.
EXPECT_FALSE(RenderProcessHostImpl::IsProcessLimitReached());
// If there is no known limit on this platform, no limit will ever be
// imposed, so return early.
if (RenderProcessHostImpl::IsPlatformProcessLimitUnknownForTesting()) {
return;
}
// Otherwise, create enough processes to reach the other half of the limit.
for (size_t i = 0; i < max_platform_renderer_process_count; ++i) {
hosts.push_back(
std::make_unique<MockRenderProcessHost>(browser_context()));
}
}
// Verify that the renderer sharing will happen.
EXPECT_TRUE(RenderProcessHostImpl::IsProcessLimitReached());
}
#endif
#if BUILDFLAG(IS_ANDROID)
// Test that Android does not use a renderer process limit.
TEST_F(RenderProcessHostUnitTest, NoRendererProcessLimitOnAndroid) {
// Add a few dummy process hosts.
static constexpr size_t kMaxRendererProcessCountForTesting = 82;
std::vector<std::unique_ptr<MockRenderProcessHost>> hosts;
for (size_t i = 0; i < kMaxRendererProcessCountForTesting; ++i) {
hosts.push_back(std::make_unique<MockRenderProcessHost>(browser_context()));
}
// Verify that the renderer sharing still won't happen.
EXPECT_FALSE(RenderProcessHostImpl::IsProcessLimitReached());
}
#endif
// Tests that RenderProcessHost reuse considers committed sites correctly.
TEST_F(RenderProcessHostUnitTest, ReuseCommittedSite) {
const GURL kUrl1("http://foo.com");
const GURL kUrl2("http://bar.com");
// BFCache is disabled for this test because the process for |kUrl1| is
// cached and reused after the navigation to |kUrl2| with BFCache enabled. The
// test expects that a new process (either spare or created) is used instead.
contents()->GetController().GetBackForwardCache().DisableForTesting(
BackForwardCache::TEST_REQUIRES_NO_CACHING);
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
site_instance->GetLastProcessAssignmentOutcome());
// Have the main frame navigate to the first url. Getting a RenderProcessHost
// with the REUSE_PENDING_OR_COMMITTED_SITE policy should now return the
// process of the main RFH.
NavigateAndCommit(kUrl1);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance->GetLastProcessAssignmentOutcome());
// Navigate away. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should again return a new process.
NavigateAndCommit(kUrl2);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_EQ(RenderProcessHostImpl::IsSpareProcessKeptAtAllTimes()
? SiteInstanceProcessAssignment::USED_SPARE_PROCESS
: SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
site_instance->GetLastProcessAssignmentOutcome());
// Now add a subframe that navigates to kUrl1. Getting a RenderProcessHost
// with the REUSE_PENDING_OR_COMMITTED_SITE policy for kUrl1 should now
// return the process of the subframe RFH.
std::string unique_name("uniqueName0");
main_test_rfh()->OnCreateChildFrame(
process()->GetNextRoutingID(),
TestRenderFrameHost::CreateStubFrameRemote(),
TestRenderFrameHost::CreateStubBrowserInterfaceBrokerReceiver(),
TestRenderFrameHost::CreateStubPolicyContainerBindParams(),
TestRenderFrameHost::CreateStubAssociatedInterfaceProviderReceiver(),
blink::mojom::TreeScopeType::kDocument, std::string(), unique_name, false,
blink::LocalFrameToken(), base::UnguessableToken::Create(),
blink::DocumentToken(), blink::FramePolicy(),
blink::mojom::FrameOwnerProperties(),
blink::FrameOwnerElementType::kIframe, ukm::kInvalidSourceId);
TestRenderFrameHost* subframe =
static_cast<TestRenderFrameHost*>(contents()
->GetPrimaryFrameTree()
.root()
->child_at(0)
->current_frame_host());
subframe = static_cast<TestRenderFrameHost*>(
NavigationSimulator::NavigateAndCommitFromDocument(kUrl1, subframe));
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_EQ(subframe->GetProcess(), site_instance->GetProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance->GetLastProcessAssignmentOutcome());
}
// Check that only new processes that haven't yet hosted any web content are
// allowed to be reused to host a site requiring a dedicated process.
TEST_F(RenderProcessHostUnitTest, IsUnused) {
const GURL kUrl1("http://foo.com");
// A process for a SiteInstance that has no site should be able to host any
// site that requires a dedicated process.
EXPECT_FALSE(main_test_rfh()->GetSiteInstance()->HasSite());
EXPECT_TRUE(main_test_rfh()->GetProcess()->IsUnused());
{
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::Create(browser_context());
EXPECT_FALSE(site_instance->HasSite());
EXPECT_TRUE(site_instance->GetOrCreateProcess()->IsUnused());
}
// Navigation should mark the process as unable to become a dedicated process
// for arbitrary sites.
NavigateAndCommit(kUrl1);
EXPECT_FALSE(main_test_rfh()->GetProcess()->IsUnused());
// A process for a SiteInstance with a preassigned site should be considered
// "used" from the point the process is created via GetProcess().
{
scoped_refptr<SiteInstanceImpl> site_instance = CreateForUrl(kUrl1);
EXPECT_FALSE(site_instance->GetOrCreateProcess()->IsUnused());
}
}
TEST_F(RenderProcessHostUnitTest, ReuseUnmatchedServiceWorkerProcess) {
const GURL kUrl("https://foo.com");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker with DEFAULT reuse policy
// should not reuse the existing service worker's process. We create this
// second service worker to test the "find the newest process" logic later.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host2);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site must reuse
// the newest unmatched service worker's process (i.e., sw_host2).
scoped_refptr<SiteInstanceImpl> site_instance1 = CreateForUrl(kUrl);
EXPECT_EQ(sw_host2, site_instance1->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site must reuse
// the newest unmatched service worker's process (i.e., sw_host1). sw_host2
// is no longer unmatched, so sw_host1 is now the newest (and only) process
// with a corresponding unmatched service worker.
scoped_refptr<SiteInstanceImpl> site_instance2 = CreateForUrl(kUrl);
EXPECT_EQ(sw_host1, site_instance2->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation should return a new process
// because there is no unmatched service worker's process.
scoped_refptr<SiteInstanceImpl> site_instance3 = CreateForUrl(kUrl);
EXPECT_NE(sw_host1, site_instance3->GetOrCreateProcess());
EXPECT_NE(sw_host2, site_instance3->GetProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
site_instance3->GetLastProcessAssignmentOutcome());
}
class UnsuitableHostContentBrowserClient : public ContentBrowserClient {
public:
UnsuitableHostContentBrowserClient() {}
~UnsuitableHostContentBrowserClient() override {}
private:
bool IsSuitableHost(RenderProcessHost* process_host,
const GURL& site_url) override {
return false;
}
};
// Check that an unmatched ServiceWorker process is not reused when it's not a
// suitable host for the destination URL. See https://crbug.com/782349.
TEST_F(RenderProcessHostUnitTest,
DontReuseUnsuitableUnmatchedServiceWorkerProcess) {
const GURL kUrl("https://foo.com");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host = sw_site_instance->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance->GetLastProcessAssignmentOutcome());
// Simulate a situation where |sw_host| won't be considered suitable for
// future navigations to |kUrl|. In https://crbug.com/782349, this happened
// when |kUrl| corresponded to a nonexistent extension, but
// chrome-extension:// URLs can't be tested inside content/. Instead,
// install a ContentBrowserClient which will return false when IsSuitableHost
// is consulted.
UnsuitableHostContentBrowserClient modified_client;
ContentBrowserClient* regular_client =
SetBrowserClientForTesting(&modified_client);
// Discard the spare, so it cannot be considered by the GetProcess call below.
SpareRenderProcessHostManagerImpl::Get().CleanupSparesForTesting();
// Now, getting a RenderProcessHost for a navigation to the same site should
// not reuse the unmatched service worker's process (i.e., |sw_host|), as
// it's unsuitable.
scoped_refptr<SiteInstanceImpl> site_instance = CreateForUrl(kUrl);
EXPECT_NE(sw_host, site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance->GetLastProcessAssignmentOutcome());
SetBrowserClientForTesting(regular_client);
}
TEST_F(RenderProcessHostUnitTest, ReuseServiceWorkerProcessForServiceWorker) {
const GURL kUrl("https://foo.com");
// Gets a RenderProcessHost for a service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker with DEFAULT reuse policy
// should not reuse the existing service worker's process. This is because
// we use DEFAULT reuse policy for a service worker when we have failed to
// start the service worker and want to use a new process. We create this
// second service worker to test the "find the newest process" logic later.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host2);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of the same site with
// REUSE_PENDING_OR_COMMITTED_SITE reuse policy should reuse the newest
// unmatched service worker's process (i.e., sw_host2).
scoped_refptr<SiteInstanceImpl> sw_site_instance3 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host3 = sw_site_instance3->GetOrCreateProcess();
EXPECT_EQ(sw_host2, sw_host3);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance3->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of the same site with
// REUSE_PENDING_OR_COMMITTED_SITE reuse policy should reuse the newest
// unmatched service worker's process (i.e., sw_host2). sw_host3 doesn't cause
// sw_host2 to be considered matched, so we can keep putting more service
// workers in that process.
scoped_refptr<SiteInstanceImpl> sw_site_instance4 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host4 = sw_site_instance4->GetOrCreateProcess();
EXPECT_EQ(sw_host2, sw_host4);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance4->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site must reuse
// the newest unmatched service worker's process (i.e., sw_host2).
scoped_refptr<SiteInstanceImpl> site_instance1 = CreateForUrl(kUrl);
EXPECT_EQ(sw_host2, site_instance1->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site must reuse
// the newest unmatched service worker's process (i.e., sw_host1). sw_host2
// is no longer unmatched, so sw_host1 is now the newest (and only) process
// with a corresponding unmatched service worker.
scoped_refptr<SiteInstanceImpl> site_instance2 = CreateForUrl(kUrl);
EXPECT_EQ(sw_host1, site_instance2->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
site_instance2->GetLastProcessAssignmentOutcome());
}
TEST_F(RenderProcessHostUnitTest,
ReuseServiceWorkerProcessInProcessPerSitePolicy) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kProcessPerSite);
const GURL kUrl("http://foo.com");
// Gets a RenderProcessHost for a service worker with process-per-site flag.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of the same site with
// process-per-site flag should reuse the unmatched service worker's process.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_EQ(sw_host1, sw_host2);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site with
// process-per-site flag should reuse the unmatched service worker's process.
scoped_refptr<SiteInstanceImpl> sw_site_instance3 = CreateForUrl(kUrl);
RenderProcessHost* sw_host3 = sw_site_instance3->GetOrCreateProcess();
EXPECT_EQ(sw_host1, sw_host3);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance3->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same site again with
// process-per-site flag should reuse the unmatched service worker's process.
scoped_refptr<SiteInstanceImpl> sw_site_instance4 = CreateForUrl(kUrl);
RenderProcessHost* sw_host4 = sw_site_instance4->GetOrCreateProcess();
EXPECT_EQ(sw_host1, sw_host4);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance4->GetLastProcessAssignmentOutcome());
}
TEST_F(RenderProcessHostUnitTest, DoNotReuseOtherSiteServiceWorkerProcess) {
const GURL kUrl1("https://foo.com");
const GURL kUrl2("https://bar.com");
// Gets a RenderProcessHost for a service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl1);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of a different site should
// return a new process because there is no reusable process.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 = CreateForUrl(kUrl2);
EXPECT_NE(sw_host1, sw_site_instance2->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
}
class RenderProcessHostWebUIUnitTest : public RenderProcessHostUnitTest {
public:
void SetUp() override {
RenderProcessHostUnitTest::SetUp();
scoped_feature_list_.InitAndEnableFeature(
features::kEnableServiceWorkersForChromeScheme);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(RenderProcessHostWebUIUnitTest,
DontReuseServiceWorkerProcessForDifferentWebUI) {
ScopedWebUIConfigRegistration config_registration1(
std::make_unique<TestWebUIConfig>("test-host"));
ScopedWebUIConfigRegistration config_registration2(
std::make_unique<TestWebUIConfig>("second-host"));
const GURL kWebUI1("chrome://test-host/");
const GURL kWebUI2("chrome://second-host/");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kWebUI1);
RenderProcessHost* sw_host = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting RenderProcessHost for a service worker for a different WebUI
// should return a new process because there is no reusable process.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 = CreateForUrl(kWebUI2);
EXPECT_NE(sw_host, sw_site_instance2->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
}
TEST_F(RenderProcessHostWebUIUnitTest, DontReuseServiceWorkerProcessForWebUrl) {
ScopedWebUIConfigRegistration config_registration1(
std::make_unique<TestWebUIConfig>("test-host"));
const GURL kWebUI1("chrome://test-host/");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kWebUI1);
RenderProcessHost* sw_host = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
const GURL kWebUrl("https://test.example/");
// Getting RenderProcessHost for a service worker for a regular site should
// return a new process because there is no reusable process.
scoped_refptr<SiteInstanceImpl> web_sw_site_instance =
CreateForServiceWorker(kWebUrl);
EXPECT_NE(sw_host, web_sw_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
web_sw_site_instance->GetLastProcessAssignmentOutcome());
// Getting RenderProcessHost for a navigation to a regular site should
// re-use the Web Service Worker process and not the WebUI one.
scoped_refptr<SiteInstanceImpl> web_site_instance = CreateForUrl(kWebUrl);
EXPECT_NE(sw_host, web_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
web_site_instance->GetLastProcessAssignmentOutcome());
}
// Tests that Service Worker processes for WebUIs are not re-used even
// for the same WebUI. Ideally we would re-use the process if it's for
// the same WebUI but we currently don't because of crbug.com/1158277.
TEST_F(RenderProcessHostWebUIUnitTest,
DontReuseServiceWorkerProcessForSameWebUI) {
ScopedWebUIConfigRegistration config_registration(
std::make_unique<TestWebUIConfig>("test-host"));
const GURL kUrl("chrome://test-host");
// Gets a RenderProcessHost for a service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker with DEFAULT reuse policy
// should not reuse the existing service worker's process. This is because
// we use DEFAULT reuse policy for a service worker when we have failed to
// start the service worker and want to use a new process. We create this
// second service worker to test the "find the newest process" logic later.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host2);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of the same WebUI with
// the same WebUI and allow process reuse policy doesn't reuse any service
// worker processes.
scoped_refptr<SiteInstanceImpl> sw_site_instance3 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host3 = sw_site_instance3->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host3);
EXPECT_NE(sw_host2, sw_host3);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance3->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same WebUI doesn't
// reuse any service worker's processes.
scoped_refptr<SiteInstanceImpl> site_instance1 = CreateForUrl(kUrl);
EXPECT_NE(sw_host1, site_instance1->GetOrCreateProcess());
EXPECT_NE(sw_host2, site_instance1->GetOrCreateProcess());
EXPECT_NE(sw_host3, site_instance1->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to a web URL doesn't reuse any
// service worker's processes.
const GURL kWebUrl("https://test.example");
scoped_refptr<SiteInstanceImpl> web_site_instance = CreateForUrl(kWebUrl);
EXPECT_NE(sw_host1, web_site_instance->GetOrCreateProcess());
EXPECT_NE(sw_host2, web_site_instance->GetOrCreateProcess());
EXPECT_NE(sw_host3, web_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
web_site_instance->GetLastProcessAssignmentOutcome());
}
class RenderProcessHostUntrustedWebUIUnitTest
: public RenderProcessHostUnitTest {
public:
void SetUp() override {
RenderProcessHostUnitTest::SetUp();
scoped_feature_list_.InitAndEnableFeature(
features::kEnableServiceWorkersForChromeUntrusted);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(RenderProcessHostUntrustedWebUIUnitTest,
DontReuseServiceWorkerProcessForDifferentWebUI) {
ScopedWebUIConfigRegistration config_registration1(
std::make_unique<ui::TestUntrustedWebUIConfig>("test-host"));
ScopedWebUIConfigRegistration config_registration2(
std::make_unique<ui::TestUntrustedWebUIConfig>("second-host"));
const GURL kWebUI1("chrome-untrusted://test-host/");
const GURL kWebUI2("chrome-untrusted://second-host/");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kWebUI1);
RenderProcessHost* sw_host = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting RenderProcessHost for a service worker for a different WebUI
// should return a new process because there is no reusable process.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 = CreateForUrl(kWebUI2);
EXPECT_NE(sw_host, sw_site_instance2->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
}
TEST_F(RenderProcessHostUntrustedWebUIUnitTest,
DontReuseServiceWorkerProcessForWebUrl) {
ScopedWebUIConfigRegistration config_registration1(
std::make_unique<ui::TestUntrustedWebUIConfig>("test-host"));
const GURL kWebUI1("chrome-untrusted://test-host/");
// Gets a RenderProcessHost for an unmatched service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kWebUI1);
RenderProcessHost* sw_host = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
const GURL kWebUrl("https://test.example/");
// Getting RenderProcessHost for a service worker for a regular site should
// return a new process because there is no reusable process.
scoped_refptr<SiteInstanceImpl> web_sw_site_instance =
CreateForServiceWorker(kWebUrl);
EXPECT_NE(sw_host, web_sw_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
web_sw_site_instance->GetLastProcessAssignmentOutcome());
// Getting RenderProcessHost for a navigation to a regular site should
// re-use the Web Service Worker process and not the WebUI one.
scoped_refptr<SiteInstanceImpl> web_site_instance = CreateForUrl(kWebUrl);
EXPECT_NE(sw_host, web_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
web_site_instance->GetLastProcessAssignmentOutcome());
}
// Tests that Service Worker processes for WebUIs are not re-used even
// for the same WebUI. Ideally we would re-use the process if it's for
// the same WebUI but we currently don't because of crbug.com/1158277.
TEST_F(RenderProcessHostUntrustedWebUIUnitTest,
DontReuseServiceWorkerProcessForSameWebUI) {
ScopedWebUIConfigRegistration config_registration(
std::make_unique<ui::TestUntrustedWebUIConfig>("test-host"));
const GURL kUrl("chrome-untrusted://test-host");
// Gets a RenderProcessHost for a service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance1 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host1 = sw_site_instance1->GetOrCreateProcess();
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker with DEFAULT reuse policy
// should not reuse the existing service worker's process. This is because
// we use DEFAULT reuse policy for a service worker when we have failed to
// start the service worker and want to use a new process. We create this
// second service worker to test the "find the newest process" logic later.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_host2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host2);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a service worker of the same WebUI with
// REUSE_PENDING_OR_COMMITTED_SITE reuse policy doesn't reuse any service
// worker processes.
scoped_refptr<SiteInstanceImpl> sw_site_instance3 =
CreateForServiceWorker(kUrl,
/*can_reuse_process=*/true);
RenderProcessHost* sw_host3 = sw_site_instance3->GetOrCreateProcess();
EXPECT_NE(sw_host1, sw_host3);
EXPECT_NE(sw_host2, sw_host3);
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
sw_site_instance3->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to the same WebUI doesn't
// reuse any service worker's processes.
scoped_refptr<SiteInstanceImpl> site_instance1 = CreateForUrl(kUrl);
EXPECT_NE(sw_host1, site_instance1->GetOrCreateProcess());
EXPECT_NE(sw_host2, site_instance1->GetOrCreateProcess());
EXPECT_NE(sw_host3, site_instance1->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
site_instance1->GetLastProcessAssignmentOutcome());
// Getting a RenderProcessHost for a navigation to a web URL doesn't reuse any
// service worker's processes.
const GURL kWebUrl("https://test.example");
scoped_refptr<SiteInstanceImpl> web_site_instance = CreateForUrl(kWebUrl);
EXPECT_NE(sw_host1, web_site_instance->GetOrCreateProcess());
EXPECT_NE(sw_host2, web_site_instance->GetOrCreateProcess());
EXPECT_NE(sw_host3, web_site_instance->GetOrCreateProcess());
EXPECT_EQ(SiteInstanceProcessAssignment::CREATED_NEW_PROCESS,
web_site_instance->GetLastProcessAssignmentOutcome());
}
// Tests that RenderProcessHost will not consider reusing a process that has
// committed an error page.
TEST_F(RenderProcessHostUnitTest, DoNotReuseError) {
// This test depends on a network error occurring on back navigation.
// This cannot happen if the page is restored from the back-forward
// cache, because no network requests would be made.
contents()->GetController().GetBackForwardCache().DisableForTesting(
BackForwardCache::TEST_REQUIRES_NO_CACHING);
const GURL kUrl1("http://foo.com");
const GURL kUrl2("http://bar.com");
// Isolate |kUrl1| so we can't get a default SiteInstance for it.
ChildProcessSecurityPolicyImpl::GetInstance()->AddFutureIsolatedOrigins(
{url::Origin::Create(kUrl1)},
ChildProcessSecurityPolicy::IsolatedOriginSource::TEST,
browser_context());
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Have the main frame navigate to the first url. Getting a RenderProcessHost
// with the REUSE_PENDING_OR_COMMITTED_SITE policy should now return the
// process of the main RFH.
NavigateAndCommit(kUrl1);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Navigate away. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should again return a new process.
NavigateAndCommit(kUrl2);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Navigate back and simulate an error. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
NavigationSimulator::GoBackAndFail(contents(), net::ERR_TIMED_OUT);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
}
// Tests that RenderProcessHost reuse considers navigations correctly.
TEST_F(RenderProcessHostUnitTest, ReuseNavigationProcess) {
const GURL kUrl1("http://foo.com");
const GURL kUrl2("http://bar.com");
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl1);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Start a navigation. Now Getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process.
auto navigation =
NavigationSimulator::CreateRendererInitiated(kUrl1, main_test_rfh());
navigation->Start();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl1);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Finish the navigation and start a new cross-site one. Getting
// RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy should
// return the process of the speculative RenderFrameHost.
navigation->Commit();
navigation = NavigationSimulator::CreateBrowserInitiated(kUrl2, contents());
navigation->Start();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl2);
EXPECT_EQ(contents()->GetSpeculativePrimaryMainFrame()->GetProcess(),
site_instance->GetProcess());
// Remember the process id and cancel the navigation. Getting
// RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy should
// no longer return the process of the speculative RenderFrameHost.
int speculative_process_host_id = contents()
->GetSpeculativePrimaryMainFrame()
->GetProcess()
->GetDeprecatedID();
navigation->Fail(net::ERR_ABORTED);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl2);
EXPECT_NE(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
}
// Tests that RenderProcessHost reuse considers navigations correctly during
// redirects in a renderer-initiated navigation. Also ensures that with
// --site-per-process, there's no mismatch in origin locks for
// https://crbug.com/773809.
TEST_F(RenderProcessHostUnitTest,
ReuseNavigationProcessRedirectsRendererInitiated) {
const GURL kUrl("http://foo.com");
const GURL kRedirectUrl1("http://foo.com/redirect");
const GURL kRedirectUrl2("http://bar.com");
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Start a navigation. Now getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process.
auto simulator =
NavigationSimulator::CreateRendererInitiated(kUrl, main_test_rfh());
simulator->Start();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
// Note that with --site-per-process, the GetProcess() call on
// |site_instance| will also lock the current process to http://foo.com.
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Simulate a same-site redirect. Getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process.
simulator->Redirect(kRedirectUrl1);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Simulate a cross-site redirect. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy for the initial foo.com site should
// no longer return the original process. Getting a RenderProcessHost with
// the REUSE_PENDING_OR_COMMITTED_SITE policy for the new bar.com site should
// return the the original process, unless we're in --site-per-process mode.
simulator->Redirect(kRedirectUrl2);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kRedirectUrl2);
if (AreAllSitesIsolatedForTesting()) {
// In --site-per-process, we should've recognized that we will need to swap
// to a new process; however, the new process won't be created until
// ready-to-commit time, when the final response comes from bar.com. Thus,
// we should have neither foo.com nor bar.com in the original process's
// list of pending sites, and |site_instance| should create a brand new
// process that does not match any existing one. In
// https://crbug.com/773809, the site_instance->GetProcess() call tried to
// reuse the original process (already locked to foo.com), leading to
// origin lock mismatch.
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
} else {
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
}
// Once the navigation is ready to commit, getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the current
// process for the final site, but not the initial one.
simulator->ReadyToCommit();
RenderProcessHost* post_redirect_process =
simulator->GetFinalRenderFrameHost()->GetProcess();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_NE(post_redirect_process, site_instance->GetProcess());
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kRedirectUrl2);
EXPECT_EQ(post_redirect_process, site_instance->GetProcess());
}
// Tests that RenderProcessHost reuse considers navigations correctly during
// redirects in a browser-initiated navigation.
TEST_F(RenderProcessHostUnitTest,
ReuseNavigationProcessRedirectsBrowserInitiated) {
const GURL kInitialUrl("http://google.com");
const GURL kUrl("http://foo.com");
const GURL kRedirectUrl1("http://foo.com/redirect");
const GURL kRedirectUrl2("http://bar.com");
NavigateAndCommit(kInitialUrl);
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Start a navigation. Now getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the speculative
// process.
contents()->GetController().LoadURL(kUrl, Referrer(),
ui::PAGE_TRANSITION_TYPED, std::string());
main_test_rfh()->SimulateBeforeUnloadCompleted(true);
int speculative_process_host_id = contents()
->GetSpeculativePrimaryMainFrame()
->GetProcess()
->GetDeprecatedID();
bool speculative_is_default_site_instance =
contents()
->GetSpeculativePrimaryMainFrame()
->GetSiteInstance()
->IsDefaultSiteInstance();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
// Simulate a same-site redirect. Getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the speculative
// process.
main_test_rfh()->SimulateRedirect(kRedirectUrl1);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
// Simulate a cross-site redirect. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should no longer return the
// speculative process: neither for the new site nor for the initial site we
// were trying to navigate to. It shouldn't return the current process either.
main_test_rfh()->SimulateRedirect(kRedirectUrl2);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_NE(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kRedirectUrl2);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
if (AreStrictSiteInstancesEnabled()) {
EXPECT_NE(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
} else {
EXPECT_TRUE(speculative_is_default_site_instance);
// The process ID should be the same as the default SiteInstance because
// kRedirectUrl1 and kRedirectUrl2 do not require a dedicated process.
EXPECT_EQ(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
}
// Once the navigation is ready to commit, Getting RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the new speculative
// process for the final site, but not the initial one. The current process
// shouldn't be returned either.
main_test_rfh()->PrepareForCommit();
speculative_process_host_id = contents()
->GetSpeculativePrimaryMainFrame()
->GetProcess()
->GetDeprecatedID();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
EXPECT_NE(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kRedirectUrl2);
EXPECT_EQ(speculative_process_host_id,
site_instance->GetProcess()->GetDeprecatedID());
}
// Tests that RenderProcessHost reuse works correctly even if the site URL of a
// URL we're navigating to changes.
TEST_F(RenderProcessHostUnitTest, ReuseExpectedSiteURLChanges) {
if (AreAllSitesIsolatedForTesting())
return;
const GURL kUrl("http://foo.com");
const GURL kModifiedSiteUrl("custom-scheme://custom");
// At first, trying to get a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Start a navigation. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the
// main RFH.
auto navigation =
NavigationSimulator::CreateRendererInitiated(kUrl, main_test_rfh());
navigation->Start();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Install the custom ContentBrowserClient. Site URLs are now modified.
// Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy
// should no longer return the process of the main RFH, as the RFH is
// registered with the normal site URL.
EffectiveURLContentBrowserClient modified_client(
kUrl, kModifiedSiteUrl, /* requires_dedicated_process */ false);
ContentBrowserClient* regular_client =
SetBrowserClientForTesting(&modified_client);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Have the navigation commit. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the
// main RFH, as it was registered with the modified site URL at commit time.
navigation->Commit();
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Start a reload. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should return the process of the
// main RFH.
contents()->GetController().Reload(ReloadType::NORMAL, false);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Remove the custom ContentBrowserClient. Site URLs are back to normal.
// Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy
// should no longer return the process of the main RFH, as it is registered
// with the modified site URL.
SetBrowserClientForTesting(regular_client);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess());
// Finish the reload. Getting a RenderProcessHost with the
// REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the
// main RFH, as it was registered with the regular site URL when it committed.
main_test_rfh()->PrepareForCommit();
main_test_rfh()->SendNavigate(0, true, kUrl);
site_instance = SiteInstanceImpl::CreateReusableInstanceForTesting(
browser_context(), kUrl);
EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess());
}
// Helper test class to modify the StoragePartition returned for a particular
// site URL.
class StoragePartitionContentBrowserClient : public ContentBrowserClient {
public:
StoragePartitionContentBrowserClient(const GURL& site,
const std::string& partition_domain,
const std::string& partition_name)
: site_(site),
partition_domain_(partition_domain),
partition_name_(partition_name) {}
~StoragePartitionContentBrowserClient() override {}
private:
StoragePartitionConfig GetStoragePartitionConfigForSite(
BrowserContext* browser_context,
const GURL& site) override {
if (site == site_) {
return StoragePartitionConfig::Create(browser_context, partition_domain_,
partition_name_,
false /* in_memory */);
}
return StoragePartitionConfig::CreateDefault(browser_context);
}
GURL site_;
std::string partition_domain_;
std::string partition_name_;
};
// Check that a SiteInstance cannot reuse a RenderProcessHost in a different
// StoragePartition.
TEST_F(RenderProcessHostUnitTest,
DoNotReuseProcessInDifferentStoragePartition) {
const GURL kUrl("https://foo.com");
NavigateAndCommit(kUrl);
// Change foo.com SiteInstances to use a different StoragePartition.
StoragePartitionContentBrowserClient modified_client(kUrl, "foo_domain",
"foo_name");
ContentBrowserClient* regular_client =
SetBrowserClientForTesting(&modified_client);
// Create a foo.com SiteInstance and check that its process does not
// reuse the foo process from the first navigation, since it's now in a
// different StoragePartition.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
RenderProcessHost* process = site_instance->GetProcess();
EXPECT_NE(main_test_rfh()->GetProcess(), process);
EXPECT_NE(main_test_rfh()->GetProcess()->GetStoragePartition(),
process->GetStoragePartition());
// Commit a navigation to foo.com in a new WebContents, so that there is a
// reusable foo.com process in the new StoragePartition.
std::unique_ptr<WebContents> contents(CreateTestWebContents());
static_cast<TestWebContents*>(contents.get())->NavigateAndCommit(kUrl);
RenderProcessHost* foo_process_in_new_partition =
contents->GetPrimaryMainFrame()->GetProcess();
// Create another reusable foo.com SiteInstance in the new StoragePartition,
// and ensure that this SiteInstance reuse the process just created in that
// same StoragePartition.
scoped_refptr<SiteInstanceImpl> site_instance2 =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
RenderProcessHost* process2 = site_instance2->GetProcess();
EXPECT_EQ(foo_process_in_new_partition, process2);
EXPECT_EQ(foo_process_in_new_partition->GetStoragePartition(),
process2->GetStoragePartition());
EXPECT_NE(main_test_rfh()->GetProcess(), process2);
EXPECT_NE(main_test_rfh()->GetProcess()->GetStoragePartition(),
process2->GetStoragePartition());
SetBrowserClientForTesting(regular_client);
}
// Check that a SiteInstance cannot reuse a ServiceWorker process in a
// different StoragePartition.
TEST_F(RenderProcessHostUnitTest,
DoNotReuseServiceWorkerProcessInDifferentStoragePartition) {
const GURL kUrl("https://foo.com");
// Create a RenderProcessHost for a service worker.
scoped_refptr<SiteInstanceImpl> sw_site_instance =
CreateForServiceWorker(kUrl);
RenderProcessHost* sw_process = sw_site_instance->GetOrCreateProcess();
// Change foo.com SiteInstances to use a different StoragePartition.
StoragePartitionContentBrowserClient modified_client(kUrl, "foo_domain",
"foo_name");
ContentBrowserClient* regular_client =
SetBrowserClientForTesting(&modified_client);
// Create a foo.com SiteInstance and check that its process does not reuse
// the ServiceWorker foo.com process, since it's now in a different
// StoragePartition.
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateReusableInstanceForTesting(browser_context(),
kUrl);
RenderProcessHost* process = site_instance->GetProcess();
EXPECT_NE(sw_process, process);
// Commit a navigation to foo.com in a new WebContents, so that there is a
// reusable foo.com process in the new StoragePartition.
std::unique_ptr<WebContents> contents(CreateTestWebContents());
static_cast<TestWebContents*>(contents.get())->NavigateAndCommit(kUrl);
RenderProcessHost* foo_process_in_new_partition =
contents->GetPrimaryMainFrame()->GetProcess();
// Create a second foo.com service worker, this time in the new
// StoragePartition. Ensure that it reuses the process registered for foo.com
// in that same StoragePartition.
scoped_refptr<SiteInstanceImpl> sw_site_instance2 =
SiteInstanceImpl::CreateForServiceWorker(
browser_context(),
UrlInfo::CreateForTesting(kUrl,
site_instance->GetStoragePartitionConfig()),
/*can_reuse_process=*/true);
RenderProcessHost* sw_process2 = sw_site_instance2->GetOrCreateProcess();
EXPECT_EQ(sw_process2, foo_process_in_new_partition);
EXPECT_NE(sw_process2, sw_process);
EXPECT_EQ(SiteInstanceProcessAssignment::REUSED_EXISTING_PROCESS,
sw_site_instance2->GetLastProcessAssignmentOutcome());
SetBrowserClientForTesting(regular_client);
}
// Check whether we notify the renderer that it has been locked to a site or
// not. This should depend on the URL from the SiteInstance.
TEST_F(RenderProcessHostUnitTest, RendererLockedToSite) {
struct TestData {
GURL test_url;
bool should_lock_renderer;
} tests[] = {{GURL("http://"), false},
{GURL("http://foo.com"), true},
{GURL("http://bar.foo.com"), true},
{GURL(""), false},
{GURL("google.com"), false},
{GURL("http:"), false},
{GURL("http://user:pass@google.com:99/foo;bar?q=a#ref"), true}};
for (const auto& test : tests) {
scoped_refptr<SiteInstanceImpl> site_instance = CreateForUrl(test.test_url);
auto* host = static_cast<MockRenderProcessHost*>(
site_instance->GetOrCreateProcess());
if (AreAllSitesIsolatedForTesting())
EXPECT_EQ(test.should_lock_renderer, host->is_renderer_locked_to_site());
else
EXPECT_EQ(false, host->is_renderer_locked_to_site());
}
}
// Checks that SiteInstanceProcessAssignment::UNKNOWN is used as the zero-value
// when no renderer process has been assigned to the SiteInstance yet.
TEST_F(RenderProcessHostUnitTest, ProcessAssignmentDefault) {
const GURL kUrl("https://foo.com");
scoped_refptr<SiteInstanceImpl> site_instance = CreateForUrl(kUrl);
EXPECT_EQ(SiteInstanceProcessAssignment::UNKNOWN,
site_instance->GetLastProcessAssignmentOutcome());
EXPECT_FALSE(site_instance->HasProcess());
}
// Tests the GetPendingReuseRefCount() function.
TEST_F(RenderProcessHostUnitTest, GetPendingReuseRefCount) {
MockRenderProcessHost mock_host(browser_context());
EXPECT_EQ(0, mock_host.GetPendingReuseRefCountForTesting());
mock_host.IncrementPendingReuseRefCount();
EXPECT_EQ(1, mock_host.GetPendingReuseRefCountForTesting());
mock_host.DecrementPendingReuseRefCount();
EXPECT_EQ(0, mock_host.GetPendingReuseRefCountForTesting());
}
class SpareRenderProcessHostUnitTest : public RenderViewHostImplTestHarness {
public:
SpareRenderProcessHostUnitTest() {}
SpareRenderProcessHostUnitTest(const SpareRenderProcessHostUnitTest&) =
delete;
SpareRenderProcessHostUnitTest& operator=(
const SpareRenderProcessHostUnitTest&) = delete;
~SpareRenderProcessHostUnitTest() override = default;
protected:
void SetUp() override {
SetRenderProcessHostFactory(&rph_factory_);
RenderViewHostImplTestHarness::SetUp();
SetContents(nullptr); // Start with no renderers.
SpareRenderProcessHostManagerImpl::Get().CleanupSparesForTesting();
while (!rph_factory_.GetProcesses()->empty()) {
rph_factory_.Remove(rph_factory_.GetProcesses()->back().get());
}
}
void TearDown() override {
// Important: Reset the max renderer count to leave this process in a
// pristine state.
DeleteContents();
rph_factory_.GetProcesses()->clear();
RenderProcessHost::SetMaxRendererProcessCount(0);
RenderViewHostImplTestHarness::TearDown();
}
void PruneDeadRenderProcessHosts() {
std::list<MockRenderProcessHost*> to_remove;
for (auto& host : *rph_factory_.GetProcesses()) {
if (!host->IsInitializedAndNotDead()) {
to_remove.push_back(host.get());
}
}
for (auto* host : to_remove) {
rph_factory_.Remove(host);
}
}
MockRenderProcessHostFactory rph_factory_;
};
using SpareProcessMaybeTakeAction =
RenderProcessHostImpl::SpareProcessMaybeTakeAction;
void ExpectSpareProcessMaybeTakeActionBucket(
const base::HistogramTester& histograms,
SpareProcessMaybeTakeAction expected_action) {
EXPECT_THAT(
histograms.GetAllSamples(
"BrowserRenderProcessHost.SpareProcessMaybeTakeAction"),
testing::ElementsAre(base::Bucket(static_cast<int>(expected_action), 1)));
}
using SpareProcessRefusedByEmbedderReason =
content::ContentBrowserClient::SpareProcessRefusedByEmbedderReason;
void ExpectSpareProcessRefusedByEmbedderReason(
const base::HistogramTester& histograms,
SpareProcessRefusedByEmbedderReason reason) {
EXPECT_THAT(
histograms.GetAllSamples(
"BrowserRenderProcessHost.SpareProcessRefusedByEmbedderReason"),
testing::ElementsAre(base::Bucket(static_cast<int>(reason), 1)));
}
TEST_F(SpareRenderProcessHostUnitTest, TestRendererTaken) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
spare_manager.WarmupSpare(browser_context());
ASSERT_EQ(1U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(1U, spare_manager.GetSpares().size());
RenderProcessHost* spare_rph = spare_manager.GetSpares()[0];
EXPECT_EQ(spare_rph, rph_factory_.GetProcesses()->at(0).get());
const GURL kUrl1("http://foo.com");
base::HistogramTester histograms;
SetContents(CreateTestWebContents());
NavigateAndCommit(kUrl1);
EXPECT_EQ(spare_rph, main_test_rfh()->GetProcess());
ExpectSpareProcessMaybeTakeActionBucket(
histograms, SpareProcessMaybeTakeAction::kSpareTaken);
if (RenderProcessHostImpl::IsSpareProcessKeptAtAllTimes()) {
ASSERT_EQ(1U, spare_manager.GetSpares().size());
EXPECT_NE(spare_rph, spare_manager.GetSpares()[0]);
EXPECT_EQ(2U, rph_factory_.GetProcesses()->size());
} else {
EXPECT_EQ(0U, spare_manager.GetSpares().size());
EXPECT_EQ(1U, rph_factory_.GetProcesses()->size());
}
}
TEST_F(SpareRenderProcessHostUnitTest, TestRendererNotTaken) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
std::unique_ptr<BrowserContext> alternate_context(new TestBrowserContext());
spare_manager.WarmupSpare(alternate_context.get());
ASSERT_EQ(1U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(1U, spare_manager.GetSpares().size());
RenderProcessHost* old_spare = spare_manager.GetSpares()[0];
EXPECT_EQ(alternate_context.get(), old_spare->GetBrowserContext());
EXPECT_EQ(old_spare, rph_factory_.GetProcesses()->at(0).get());
// Remember the ID of the spare, so as to not compare a pointer of a deleted
// RenderProcessHost at the end of the test.
int old_spare_id = old_spare->GetDeprecatedID();
const GURL kUrl1("http://foo.com");
base::HistogramTester histograms;
SetContents(CreateTestWebContents());
NavigateAndCommit(kUrl1);
EXPECT_NE(old_spare_id, main_test_rfh()->GetProcess()->GetDeprecatedID());
ExpectSpareProcessMaybeTakeActionBucket(
histograms, SpareProcessMaybeTakeAction::kMismatchedBrowserContext);
// Pumping the message loop here accounts for the delay between calling
// RPH::Cleanup on the spare and the time when the posted delete actually
// happens. Without pumping, the spare would still be present in
// rph_factory_.GetProcesses().
base::RunLoop().RunUntilIdle();
PruneDeadRenderProcessHosts();
if (RenderProcessHostImpl::IsSpareProcessKeptAtAllTimes()) {
EXPECT_EQ(2U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(1U, spare_manager.GetSpares().size());
RenderProcessHost* new_spare = spare_manager.GetSpares()[0];
ASSERT_NE(old_spare_id, new_spare->GetDeprecatedID());
EXPECT_EQ(GetBrowserContext(), new_spare->GetBrowserContext());
} else {
EXPECT_EQ(1U, rph_factory_.GetProcesses()->size());
EXPECT_EQ(0U, spare_manager.GetSpares().size());
}
}
// A mock ContentBrowserClient that returns the
// SpareProcessRefusedByEmbedderReason as set by the user.
class SpareProcessRejectBrowserClient : public ContentBrowserClient {
public:
SpareProcessRejectBrowserClient() = default;
SpareProcessRejectBrowserClient(const SpareProcessRejectBrowserClient&) =
delete;
SpareProcessRejectBrowserClient& operator=(
const SpareProcessRejectBrowserClient&) = delete;
void SetSpareProcessRefuseReason(SpareProcessRefusedByEmbedderReason reason) {
refuse_reason_ = reason;
}
bool ShouldUseSpareRenderProcessHost(
BrowserContext* browser_context,
const GURL& site_url,
std::optional<SpareProcessRefusedByEmbedderReason>& refused_reason)
override {
refused_reason = refuse_reason_;
return false;
}
private:
SpareProcessRefusedByEmbedderReason refuse_reason_ =
SpareProcessRefusedByEmbedderReason::DefaultDisabled;
};
TEST_F(SpareRenderProcessHostUnitTest,
TestSpareProcessRefusedByEmbedderReason) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
spare_manager.WarmupSpare(browser_context());
ASSERT_EQ(1U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(1U, spare_manager.GetSpares().size());
RenderProcessHost* spare_rph = spare_manager.GetSpares()[0];
EXPECT_EQ(spare_rph, rph_factory_.GetProcesses()->at(0).get());
std::vector<SpareProcessRefusedByEmbedderReason> test_reasons = {
SpareProcessRefusedByEmbedderReason::DefaultDisabled,
SpareProcessRefusedByEmbedderReason::NoProfile,
SpareProcessRefusedByEmbedderReason::TopFrameChromeWebUI,
SpareProcessRefusedByEmbedderReason::InstantRendererForNewTabPage,
SpareProcessRefusedByEmbedderReason::ExtensionProcess,
SpareProcessRefusedByEmbedderReason::JitDisabled,
};
SpareProcessRejectBrowserClient test_client;
ContentBrowserClient* regular_client =
SetBrowserClientForTesting(&test_client);
for (auto reason : test_reasons) {
base::HistogramTester histograms;
test_client.SetSpareProcessRefuseReason(reason);
SiteInstanceImpl::Create(GetBrowserContext())->GetOrCreateProcess();
ExpectSpareProcessRefusedByEmbedderReason(histograms, reason);
PruneDeadRenderProcessHosts();
spare_manager.WarmupSpare(browser_context());
}
SetBrowserClientForTesting(regular_client);
}
TEST_F(SpareRenderProcessHostUnitTest, SpareMissing) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
spare_manager.CleanupSparesForTesting();
ASSERT_EQ(0U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(0U, spare_manager.GetSpares().size());
const GURL kUrl1("http://foo.com");
base::HistogramTester histograms;
SetContents(CreateTestWebContents());
NavigateAndCommit(kUrl1);
EXPECT_TRUE(main_test_rfh()->GetProcess());
ExpectSpareProcessMaybeTakeActionBucket(
histograms, SpareProcessMaybeTakeAction::kNoSparePresent);
if (RenderProcessHostImpl::IsSpareProcessKeptAtAllTimes()) {
EXPECT_EQ(2U, rph_factory_.GetProcesses()->size());
EXPECT_EQ(1U, spare_manager.GetSpares().size());
} else {
EXPECT_EQ(1U, rph_factory_.GetProcesses()->size());
EXPECT_EQ(0U, spare_manager.GetSpares().size());
}
}
TEST_F(SpareRenderProcessHostUnitTest,
SpareShouldNotLaunchInParallelWithOtherProcess) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
std::unique_ptr<BrowserContext> alternate_context(new TestBrowserContext());
spare_manager.WarmupSpare(alternate_context.get());
ASSERT_EQ(1U, rph_factory_.GetProcesses()->size());
ASSERT_EQ(1U, spare_manager.GetSpares().size());
RenderProcessHost* old_spare = spare_manager.GetSpares()[0];
EXPECT_EQ(alternate_context.get(), old_spare->GetBrowserContext());
EXPECT_EQ(old_spare, rph_factory_.GetProcesses()->at(0).get());
// When we try to get a process for foo.com, we won't be able to use the spare
// (because it is associated with the alternate, mismatched BrowserContext)
// and therefore we will have to spawn a new process. This test verifies that
// we don't at the same time try to warm-up a new spare (leading to
// unnecessary resource contention when 2 processes try to launch at the same
// time).
scoped_refptr<SiteInstanceImpl> site_instance =
SiteInstanceImpl::CreateForTesting(browser_context(),
GURL("http://foo.com"));
RenderProcessHost* site_instance_process =
site_instance->GetOrCreateProcess();
// The SiteInstance shouldn't get the old spare, because of BrowserContext
// mismatch. The SiteInstance will get a new process instead.
EXPECT_NE(old_spare, site_instance_process);
EXPECT_FALSE(site_instance_process->IsReady());
// There should be no new spare at this point to avoid launching 2 processes
// at the same time. Note that the spare might still be created later during
// a navigation (e.g. after cross-site redirects or when committing).
if (!spare_manager.GetSpares().empty()) {
EXPECT_EQ(old_spare, spare_manager.GetSpares()[0]);
}
}
// This unit test looks at the simplified equivalent of what
// CtrlClickShouldEndUpInSameProcessTest.BlankTarget test would have
// encountered. The test verifies that the spare RPH is not launched if 1) we
// need to create another renderer process anyway (e.g. because the spare is
// missing when MaybeTakeSpareRenderProcessHost is called) and 2) creating the
// other renderer process will put as at the process limit. Launching the spare
// in this scenario would put us over the process limit and is therefore
// undesirable.
TEST_F(SpareRenderProcessHostUnitTest, JustBelowProcessLimit) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
// Override a global. TearDown() will call SetMaxRendererProcessCount() again
// to clean up.
RenderProcessHost::SetMaxRendererProcessCount(1);
// No spare or any other renderer process at the start of the test.
EXPECT_EQ(0U, rph_factory_.GetProcesses()->size());
EXPECT_TRUE(spare_manager.GetSpares().empty());
// Navigation can't take a spare (none present) and needs to launch a new
// renderer process.
const GURL kUrl1("http://foo.com");
SetContents(CreateTestWebContents());
NavigateAndCommit(kUrl1);
// We should still be below the process limit.
EXPECT_EQ(1U, rph_factory_.GetProcesses()->size());
// There should be no spare - having one would put us over the process limit.
EXPECT_TRUE(spare_manager.GetSpares().empty());
}
// This unit test verifies that a mismatched spare RenderProcessHost is dropped
// before considering process reuse due to the process limit.
TEST_F(SpareRenderProcessHostUnitTest, AtProcessLimit) {
auto& spare_manager = SpareRenderProcessHostManagerImpl::Get();
// Override a global. TearDown() will call SetMaxRendererProcessCount() again
// to clean up.
RenderProcessHost::SetMaxRendererProcessCount(2);
// Create and navigate the 1st WebContents.
const GURL kUrl1("http://foo.com");
std::unique_ptr<WebContents> contents1(CreateTestWebContents());
static_cast<TestWebContents*>(contents1.get())->NavigateAndCommit(kUrl1);
if (!spare_manager.GetSpares().empty()) {
EXPECT_NE(spare_manager.GetSpares()[0],
contents1->GetPrimaryMainFrame()->GetProcess());
}
// Warm up a mismatched spare.
std::unique_ptr<BrowserContext> alternate_context(new TestBrowserContext());
spare_manager.WarmupSpare(alternate_context.get());
base::RunLoop().RunUntilIdle();
PruneDeadRenderProcessHosts();
EXPECT_EQ(2U, rph_factory_.GetProcesses()->size());
// Create and navigate the 2nd WebContents.
const GURL kUrl2("http://bar.com");
std::unique_ptr<WebContents> contents2(CreateTestWebContents());
static_cast<TestWebContents*>(contents2.get())->NavigateAndCommit(kUrl2);
base::RunLoop().RunUntilIdle();
PruneDeadRenderProcessHosts();
// Creating a 2nd WebContents shouldn't share a renderer process with the 1st
// one - instead the spare should be dropped to stay under the process limit.
EXPECT_EQ(2U, rph_factory_.GetProcesses()->size());
EXPECT_TRUE(spare_manager.GetSpares().empty());
EXPECT_NE(contents1->GetPrimaryMainFrame()->GetProcess(),
contents2->GetPrimaryMainFrame()->GetProcess());
}
} // namespace content
|