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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <atomic>
#include "base/path_service.h"
#include "base/strings/pattern.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/back_forward_cache_disable.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/renderer.mojom.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_paths.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/resource_load_observer.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/base/features.h"
#include "net/base/network_isolation_key.h"
#include "net/base/schemeful_site.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/scoped_mutually_exclusive_feature_list.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
class SplitCacheContentBrowserTest : public ContentBrowserTest {
public:
enum class Context { kMainFrame, kSameOriginFrame, kCrossOriginFrame };
SplitCacheContentBrowserTest() = default;
SplitCacheContentBrowserTest(const SplitCacheContentBrowserTest&) = delete;
SplitCacheContentBrowserTest& operator=(const SplitCacheContentBrowserTest&) =
delete;
void SetUp() override {
RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
ContentBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
// Setup the server to allow serving separate sites, so we can perform
// cross-process navigation.
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->RegisterRequestHandler(
base::BindRepeating(&SplitCacheContentBrowserTest::CachedScriptHandler,
base::Unretained(this)));
ASSERT_TRUE(embedded_test_server()->Start());
}
std::unique_ptr<net::test_server::HttpResponse> CachedScriptHandler(
const net::test_server::HttpRequest& request) {
GURL absolute_url = embedded_test_server()->GetURL(request.relative_url);
// Return a page that redirects to d.com/title1.html.
if (absolute_url.path() == "/redirect_to_d") {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_SEE_OTHER);
http_response->AddCustomHeader(
"Location",
embedded_test_server()->GetURL("d.com", "/title1.html").spec());
return http_response;
}
// Return valid cacheable script.
if (absolute_url.path() == "/script") {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->set_content("console.log(\"Hello World\");");
http_response->set_content_type("application/javascript");
http_response->AddCustomHeader("Cache-Control", "max-age=1000");
return http_response;
}
// A basic cacheable worker that loads 3p.com/script
if (absolute_url.path() == "/worker.js") {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
GURL resource = GenURL("3p.com", "/script");
// Self-terminate the worker just after loading the third party
// script, so that the parent context doesn't need to wait for the
// worker's termination when cleaning up the test. See
// https://crbug.com/1104847 for more details.
std::string content = base::StringPrintf("importScripts('%s');\nclose();",
resource.spec().c_str());
http_response->set_content(content);
http_response->set_content_type("application/javascript");
http_response->AddCustomHeader("Cache-Control", "max-age=100000");
return http_response;
}
// Make the document resource cacheable.
if (absolute_url.path() == "/title1.html") {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->AddCustomHeader("Cache-Control", "max-age=100000");
return http_response;
}
// A cacheable worker that loads a nested worker on an origin provided
// as a query param.
if (absolute_url.path() == "/embedding_worker.js") {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
GURL resource =
GenURL(base::StringPrintf("%s.com", absolute_url.query().c_str()),
"/worker.js");
const char kLoadWorkerScript[] = "let w = new Worker('%s');";
std::string content =
base::StringPrintf(kLoadWorkerScript, resource.spec().c_str());
http_response->set_content(content);
http_response->set_content_type("application/javascript");
http_response->AddCustomHeader("Cache-Control", "max-age=100000");
return http_response;
}
return nullptr;
}
protected:
// Creates and loads subframe, waits for load to stop, and then returns
// subframe from the web contents frame tree.
RenderFrameHost* CreateSubframe(const GURL& sub_frame) {
EXPECT_TRUE(ExecJs(shell(), GetSubframeScript(sub_frame)));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
return static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root()
->child_at(0)
->current_frame_host();
}
// Loads 3p.com/script on page |url|, optionally from |sub_frame| if it's
// valid, and returns whether the script was cached or not.
bool TestResourceLoad(const GURL& url, const GURL& sub_frame) {
return TestResourceLoadHelper(url, sub_frame, GURL());
}
// Loads 3p.com/script on page |url| from |worker| and returns whether
// the script was cached or not.
bool TestResourceLoadFromDedicatedWorker(const GURL& url,
const GURL& worker) {
DCHECK(worker.is_valid());
return TestResourceLoadHelper(url, GURL(), worker);
}
// Loads 3p.com/script on page |url| from |worker| inside |sub_frame|
// and returns whether the script was cached or not.
bool TestResourceLoadFromDedicatedWorkerInIframe(const GURL& url,
const GURL& sub_frame,
const GURL& worker) {
DCHECK(sub_frame.is_valid());
DCHECK(worker.is_valid());
return TestResourceLoadHelper(url, sub_frame, worker);
}
// Loads 3p.com/script on |popup| opened from page |url| and returns whether
// the script was cached or not.
bool TestResourceLoadFromPopup(const GURL& url, const GURL& popup) {
DCHECK(popup.is_valid());
return TestResourceLoadHelper(url, popup, GURL(), true);
}
// Loads 3p.com/script on page |url|. If |new_frame| is valid, it is loaded
// from a new frame with that url; otherwise, it is loaded from the main
// frame. This new frame is a popup if |use_popup|; otherwise, it is a
// subframe. The load is optionally performed by |worker| if it's valid.
bool TestResourceLoadHelper(const GURL& url,
const GURL& new_frame,
const GURL& worker,
bool use_popup = false) {
DCHECK(url.is_valid());
// Allocate a new process to prevent using the in-memory cache.
// 1) Prevent the old page from entering the back-forward cache. Otherwise
// the old process will be kept alive, because it is still being used.
// 2) Navigate to a WebUI URL, which uses a new process.
DisableBFCacheForRFHForTesting(
shell()->web_contents()->GetPrimaryMainFrame());
EXPECT_TRUE(NavigateToURL(shell(), GetWebUIURL("blob-internals")));
// In the case of a redirect, the observed URL will be different from
// what NavigateToURL(...) expects.
if (base::StartsWith(url.path(), "/redirect", base::CompareCase::SENSITIVE))
EXPECT_FALSE(NavigateToURL(shell(), url));
else
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHost* host_to_load_resource =
shell()->web_contents()->GetPrimaryMainFrame();
RenderFrameHostImpl* main_frame =
static_cast<RenderFrameHostImpl*>(host_to_load_resource);
Shell* shell_to_observe = shell();
if (new_frame.is_valid()) {
// If there is supposed to be a subframe or popup, create it.
if (use_popup) {
shell_to_observe = OpenPopup(main_frame, new_frame, "");
host_to_load_resource =
static_cast<WebContentsImpl*>(shell_to_observe->web_contents())
->GetPrimaryMainFrame();
} else {
host_to_load_resource = CreateSubframe(new_frame);
}
}
// `shell_to_observe` may still contain responses depending on process reuse
// policies. Clear the in-memory cache in `shell_to_observe` to make sure
// the following ResourceLoadObserver can observe network requests.
base::RunLoop loop;
shell_to_observe->web_contents()
->GetPrimaryMainFrame()
->GetProcess()
->GetRendererInterface()
->PurgeResourceCache(loop.QuitClosure());
loop.Run();
// Observe network requests.
ResourceLoadObserver observer(shell_to_observe);
GURL resource = GenURL("3p.com", "/script");
// If there is supposed to be a worker to load this resource, create it.
// Otherwise, load the resource directly.
if (worker.is_valid()) {
EXPECT_TRUE(ExecJs(host_to_load_resource, GetWorkerScript(worker)));
} else {
EXPECT_TRUE(
ExecJs(host_to_load_resource, GetLoadResourceScript(resource)));
}
observer.WaitForResourceCompletion(resource);
// Test the network isolation key.
url::Origin top_frame_origin =
main_frame->frame_tree_node()->current_origin();
RenderFrameHostImpl* frame_host =
static_cast<RenderFrameHostImpl*>(host_to_load_resource);
url::Origin frame_origin;
if (new_frame.is_empty()) {
frame_origin = top_frame_origin;
} else {
frame_origin = url::Origin::Create(new_frame);
if (use_popup && !frame_origin.opaque()) {
// The popup is in a new WebContents, so its top_frame_origin is also
// new unless it is blank.
top_frame_origin = frame_origin;
} else {
// Take redirects and initially empty subframes/popups into account.
frame_origin = frame_host->GetLastCommittedOrigin();
}
}
if (!top_frame_origin.opaque() && !frame_origin.opaque()) {
EXPECT_EQ(net::NetworkIsolationKey(net::SchemefulSite(top_frame_origin),
net::SchemefulSite(frame_origin)),
frame_host->GetNetworkIsolationKey());
} else {
EXPECT_TRUE(frame_host->GetNetworkIsolationKey().IsTransient());
}
return (*observer.GetResource(resource))->was_cached;
}
// Navigates to |url| and returns if the navigation resource was fetched from
// the cache or not.
bool NavigationResourceCached(const GURL& url,
const GURL& sub_frame,
bool subframe_navigation_resource_cached) {
return NavigationResourceCached(url, url, sub_frame,
subframe_navigation_resource_cached);
}
// Same as above, but allows explicitly specifying the expected commit URL
// for the navigation to |url|, in case it differs.
bool NavigationResourceCached(const GURL& url,
const GURL& expected_commit_url,
const GURL& sub_frame,
bool subframe_navigation_resource_cached) {
// Do a cross-process navigation to clear the in-memory cache.
// We assume that we don't start this call from "chrome://blob-internals",
// as otherwise it won't be a cross-process navigation. We are relying on
// this navigation to discard the old process.
EXPECT_TRUE(NavigateToURL(shell(), GetWebUIURL("blob-internals")));
// Observe network requests.
ResourceLoadObserver observer(shell());
EXPECT_TRUE(NavigateToURL(shell(), url, expected_commit_url));
RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
observer.WaitForResourceCompletion(url);
if (sub_frame.is_valid()) {
EXPECT_EQ(1U, main_frame->frame_tree_node()->child_count());
NavigateFrameToURL(main_frame->frame_tree_node()->child_at(0), sub_frame);
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
observer.WaitForResourceCompletion(sub_frame);
EXPECT_EQ(subframe_navigation_resource_cached,
(*observer.GetResource(sub_frame))->was_cached);
}
return (*observer.GetResource(url))->was_cached;
}
// Loads a dedicated worker script and checks to see whether or not the
// script was cached.
bool DedicatedWorkerScriptCached(const GURL& url,
const GURL& sub_frame,
const GURL& worker) {
DCHECK(url.is_valid());
DCHECK(worker.is_valid());
// Do a cross-process navigation to clear the in-memory cache.
// We assume that we don't start this call from "chrome://blob-internals",
// as otherwise it won't be a cross-process navigation. We are relying on
// this navigation to discard the old process.
EXPECT_TRUE(NavigateToURL(shell(), GetWebUIURL("blob-internals")));
// Observe network requests.
ResourceLoadObserver observer(shell());
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderFrameHost* host_to_load_resource =
shell()->web_contents()->GetPrimaryMainFrame();
// If there is supposed to be a subframe, create it.
if (sub_frame.is_valid()) {
host_to_load_resource = CreateSubframe(sub_frame);
}
EXPECT_TRUE(ExecJs(host_to_load_resource, GetWorkerScript(worker)));
observer.WaitForResourceCompletion(GenURL("3p.com", "/script"));
observer.WaitForResourceCompletion(worker);
return (*observer.GetResource(worker))->was_cached;
}
bool NavigationRedirectCached(const GURL& url, const GURL& redirect_url) {
// Do a cross-process navigation to clear the in-memory cache.
// We assume that we don't start this call from "chrome://blob-internals",
// as otherwise it won't be a cross-process navigation. We are relying on
// this navigation to discard the old process.
EXPECT_TRUE(NavigateToURL(shell(), GetWebUIURL("blob-internals")));
EXPECT_TRUE(NavigateToURL(shell(), url));
// Observe the redirect.
ResourceLoadObserver observer(shell());
EXPECT_TRUE(ExecJs(shell(), GetRedirectScript(redirect_url)));
WaitForLoadStop(shell()->web_contents());
observer.WaitForResourceCompletion(redirect_url);
return (*observer.GetResource(redirect_url))->was_cached;
}
// Gets script to create subframe.
std::string GetSubframeScript(const GURL& sub_frame) {
const char kLoadIframeScript[] = R"(
let iframe = document.createElement('iframe');
iframe.src = $1;
document.body.appendChild(iframe);
)";
return JsReplace(kLoadIframeScript, sub_frame);
}
// Gets script to create worker.
std::string GetWorkerScript(const GURL& worker) {
const char kLoadWorkerScript[] = "let w = new Worker($1);";
return JsReplace(kLoadWorkerScript, worker);
}
// Gets script to load resource.
std::string GetLoadResourceScript(const GURL& resource) {
const char kLoadResourceScript[] = R"(
let script = document.createElement('script');
script.src = $1;
document.body.appendChild(script);
)";
return JsReplace(kLoadResourceScript, resource);
}
// Gets script to redirect via JavaScript.
std::string GetRedirectScript(const GURL& location) {
const char kRedirectScript[] = R"(
window.location.href = $1;
)";
return JsReplace(kRedirectScript, location);
}
GURL GenURL(const std::string& host, const std::string& path) {
return embedded_test_server()->GetURL(host, path);
}
};
enum class SplitCacheTestCase {
kEnabledTripleKeyed,
kEnabledTriplePlusCrossSiteMainFrameNavBool,
};
const struct {
const SplitCacheTestCase test_case;
base::test::FeatureRef feature;
} kTestCaseToFeatureMapping[] = {
{SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool,
net::features::kSplitCacheByCrossSiteMainFrameNavigationBoolean}};
class SplitCacheContentBrowserTestEnabled
: public SplitCacheContentBrowserTest,
public testing::WithParamInterface<SplitCacheTestCase> {
public:
SplitCacheContentBrowserTestEnabled()
: split_cache_experiment_feature_list_(GetParam(),
kTestCaseToFeatureMapping) {
split_cache_always_enabled_feature_list_.InitAndEnableFeature(
net::features::kSplitCacheByNetworkIsolationKey);
}
private:
net::test::ScopedMutuallyExclusiveFeatureList
split_cache_experiment_feature_list_;
base::test::ScopedFeatureList split_cache_always_enabled_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(
All,
SplitCacheContentBrowserTestEnabled,
testing::ValuesIn(
{SplitCacheTestCase::kEnabledTripleKeyed,
SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool}),
[](const testing::TestParamInfo<SplitCacheTestCase>& info) {
switch (info.param) {
case SplitCacheTestCase::kEnabledTripleKeyed:
return "SplitCacheEnabledTripleKeyed";
case SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool:
return "SplitCacheEnabledTriplePlusCrossSiteMainFrameNavigationBool";
}
});
class SplitCacheContentBrowserTestDisabled
: public SplitCacheContentBrowserTest {
public:
SplitCacheContentBrowserTestDisabled() {
feature_list_.InitAndDisableFeature(
net::features::kSplitCacheByNetworkIsolationKey);
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, MainFrame) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// The second time, it's cached.
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Now load it from a different site, and the resource isn't cached because
// the top frame origin is different.
EXPECT_FALSE(TestResourceLoad(GenURL("b.com", "/title1.html"), GURL()));
// Now load it from 3p.com, which is same-site to the cacheable
// resource. Still not supposed to be cached.
EXPECT_FALSE(TestResourceLoad(GenURL("3p.com", "/title1.html"), GURL()));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, MainFrameRedirect) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load it from a a.com/redirect_to_d which redirects to d.com/title1.html and
// the resource shouldn't be cached because now we're on d.com.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/redirect_to_d"), GURL()));
// Navigate to d.com directly. The main resource should be cached due to the
// earlier navigation.
EXPECT_TRUE(TestResourceLoad(GenURL("d.com", "/title1.html"), GURL()));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, Subframe) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a same-origin iframe on a page where it's already
// cached. It should still be cached.
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"),
GenURL("a.com", "/title1.html")));
// Load the resource from a cross-origin iframe on a page where the
// iframe hasn't been cached previously.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"),
GenURL("e.com", "/title1.html")));
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"),
GenURL("e.com", "/title1.html")));
// Load the resource from a same-origin iframe on a page where it's not
// cached. It should not be cached.
EXPECT_FALSE(TestResourceLoad(GenURL("e.com", "/title1.html"),
GenURL("e.com", "/title1.html")));
EXPECT_TRUE(TestResourceLoad(GenURL("e.com", "/title1.html"),
GenURL("e.com", "/title1.html")));
// Load the resource from a cross-origin iframe where the iframe's origin has
// seen the object before but the top frame hasn't. It should not be cached.
EXPECT_FALSE(TestResourceLoad(GenURL("f.com", "/title1.html"),
GenURL("a.com", "/title1.html")));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, MainFrameDataUrl) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a data url which has an opaque origin. It shouldn't
// be cached.
GURL data_url("data:text/html,<body>Hello World</body>");
EXPECT_FALSE(TestResourceLoad(data_url, GURL()));
// Load the same resource from the same data url, it shouldn't be cached
// because the origin should be unique.
EXPECT_FALSE(TestResourceLoad(data_url, GURL()));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
MainFrameAboutBlank) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a document that points to about:blank.
GURL blank_url(url::kAboutBlankURL);
EXPECT_FALSE(TestResourceLoad(blank_url, GURL()));
// Load the same resource from about:blank url again, it shouldn't be cached
// because the origin is unique. TODO(crbug.com/40092527) will change this
// behavior and about:blank main frame pages will inherit the origin of the
// page that opened it.
EXPECT_FALSE(TestResourceLoad(blank_url, GURL()));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, SubFrameRedirect) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load it from a a.com/redirect_to_d which redirects to d.com/title1.html and
// the resource shouldn't be cached because now we're on d.com.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"),
GenURL("a.com", "/redirect_to_d")));
// Now load it from the d.com iframe directly. It should be cached.
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"),
GenURL("d.com", "/title1.html")));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, SubFrameDataUrl) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a data url which has an opaque origin. It shouldn't
// be cached.
GURL data_url("data:text/html,<body>Hello World</body>");
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), data_url));
// Load the same resource from the same data url. It shouldn't be cached
// because the cache isn't used for transient origins.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), data_url));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
SubframeAboutBlank) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a subframe document that points to about:blank. The
// resource is cached because the resource load is using the main frame's
// URLLoaderFactory and main frame's factory has the NIK set to
// (a.com, a.com) which is already in the cache.
GURL blank_url(url::kAboutBlankURL);
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), blank_url));
// Load the resource from a popup window that points to about:blank. The
// resource is cached because the resource load is using the original main
// frame's URLLoaderFactory and the original main frame's factory has the NIK
// set to (a.com, a.com) which is already in the cache.
EXPECT_TRUE(
TestResourceLoadFromPopup(GenURL("a.com", "/title1.html"), blank_url));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled, Popup) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
ASSERT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Load the resource from a popup window that points to a new origin. The
// resource is not cached because the resource load is using a NIK set to
// (g.com, g.com).
EXPECT_FALSE(TestResourceLoadFromPopup(GenURL("a.com", "/title1.html"),
GenURL("g.com", "/title1.html")));
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.foo.com", "/title1.html"), GURL()));
// The second time, it's cached when accessed with the same eTLD+1.
EXPECT_TRUE(TestResourceLoad(GenURL("b.foo.com", "/title1.html"), GURL()));
// Now load it from a different site, and the resource isn't cached.
EXPECT_FALSE(TestResourceLoad(GenURL("b.com", "/title1.html"), GURL()));
// Now load it from 3p.com, which is same-site to the cacheable
// resource. Still not supposed to be cached.
EXPECT_FALSE(TestResourceLoad(GenURL("3p.com", "/title1.html"), GURL()));
// Test case with iframe. This should be a cache hit since the network
// isolation key is (foo.com, foo.com) as in the first case.
EXPECT_TRUE(TestResourceLoad(GenURL("a.foo.com", "/title1.html"),
GenURL("iframe.foo.com", "/title1.html")));
}
IN_PROC_BROWSER_TEST_F(SplitCacheContentBrowserTestDisabled, NonSplitCache) {
// Load a cacheable resource for the first time, and it's not cached.
EXPECT_FALSE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// The second time, it's cached.
EXPECT_TRUE(TestResourceLoad(GenURL("a.com", "/title1.html"), GURL()));
// Now load it from a different site, and the resource is cached.
EXPECT_TRUE(TestResourceLoad(GenURL("b.com", "/title1.html"), GURL()));
// Load it from a cross-origin iframe, and it's still cached.
EXPECT_TRUE(TestResourceLoad(GenURL("b.com", "/title1.html"),
GenURL("c.com", "/title1.html")));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
NavigationResources) {
// Navigate for the first time, and it's not cached.
EXPECT_FALSE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// The second time, it's cached.
EXPECT_TRUE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// Navigate to a.com/redirect_to_d which redirects to d.com/title1.html.
EXPECT_FALSE(NavigationResourceCached(GenURL("a.com", "/redirect_to_d"),
GenURL("d.com", "/title1.html"), GURL(),
false));
// Navigate to d.com directly. The main resource should be cached due to the
// earlier redirected navigation. Note that the HTTP cache doesn't deem the
// earlier navigation cross-site (for the purposes of calculating the cache
// key) since the navigation is browser-initiated.
EXPECT_TRUE(
NavigationResourceCached(GenURL("d.com", "/title1.html"), GURL(), false));
// Navigate to a subframe with the same top frame origin as in an earlier
// navigation and same url as already navigated to earlier in a main frame
// navigation.
// The 'is-subframe-document-resource' boolean prevents this resource from
// sharing a cache partition with the earlier top-level navigation, even
// in cases where the initiator is same-origin with the page being loaded.
EXPECT_FALSE(NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"),
false));
EXPECT_TRUE(NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"), true));
// Navigate to the same subframe document from a different top frame origin.
// It should be a cache miss.
EXPECT_FALSE(NavigationResourceCached(
GenURL("b.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"), false));
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
SubframeNavigationResources) {
// Navigate for the first time, and it's not cached.
NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"), false);
// The second time it should be a cache hit.
NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"), true);
// Navigate to the same subframe document from a different top frame origin.
// It should be a cache miss.
NavigationResourceCached(
GenURL("b.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/title1.html"), false);
// Navigate the subframe to a.com/redirect_to_d which redirects to
// d.com/title1.html.
NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("a.com", "/redirect_to_d"), false);
// Navigate to d.com directly. The resource should be cached due to the
// earlier redirected navigation.
NavigationResourceCached(
GenURL("a.com", "/navigation_controller/page_with_iframe.html"),
GenURL("d.com", "/title1.html"), true);
}
// Tests that when a subresource URL which is same-site to the fetching frame
// is later used to create a subframe from the same top-level site, it should
// not be a cache hit (crbug.com/1135149).
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
SubframeNavigationResource) {
// main.com iframes 3p.com which fetches a subresource 3p.com/script with
// cache key (main.com, 3p.com, 3p.com/script). Then main.com iframes evil.com
// which iframes 3p.com/script. It should be a cache miss since the first time
// it was fetched as a subresource and second time as a subframe document
// resource.
// Fetch 3p.com/script from a 3p.com iframe, top-level site main.com.
TestResourceLoad(GenURL("main.com", "/title1.html") /* top-level frame */,
GenURL("3p.com", "/title1.html") /* subframe */);
// Create evil.com iframe inside top-level site main.com.
NavigationResourceCached(
GenURL("main.com", "/navigation_controller/page_with_iframe.html"),
GenURL("evil.com", "/title1.html"), false);
RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetPrimaryMainFrame());
EXPECT_EQ(1U, main_frame->frame_tree_node()->child_count());
// Observe network requests.
ResourceLoadObserver observer(shell());
// Now iframe 3p.com/script within evil.com.
GURL subframe_url = GenURL("3p.com", "/script");
EXPECT_TRUE(ExecJs(main_frame->frame_tree_node()->child_at(0),
GetSubframeScript(subframe_url)));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
observer.WaitForResourceCompletion(subframe_url);
EXPECT_EQ(false, (*observer.GetResource(subframe_url))->was_cached);
}
// Tests that a cross-site navigation to a document that was previously loaded
// via top-level navigation doesn't use the cache, since doing so could enable
// cross-site leaks.
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
CrossSiteNavigation) {
// Do a top-level navigation to a document to add it to the cache.
EXPECT_FALSE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// Verify that the document did get added to the cache.
EXPECT_TRUE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// Navigate to a cross-site document that performs a client-side redirect to
// the document visited previously. Ensure that repeating this request results
// in a cache hit, and then try again with a different initiating site.
bool evil_com_initiator_first_navigation_result = NavigationRedirectCached(
GenURL("evil.com", "/title1.html"), GenURL("a.com", "/title1.html"));
EXPECT_TRUE(NavigationRedirectCached(GenURL("evil.com", "/title1.html"),
GenURL("a.com", "/title1.html")));
bool evil2_com_initiator_navigation_result = NavigationRedirectCached(
GenURL("evil2.com", "/title1.html"), GenURL("a.com", "/title1.html"));
switch (GetParam()) {
case SplitCacheTestCase::kEnabledTripleKeyed:
// If we aren't partitioning cross-site navigations then these should come
// from the cache.
EXPECT_TRUE(evil_com_initiator_first_navigation_result);
EXPECT_TRUE(evil2_com_initiator_navigation_result);
break;
case SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool:
// Using the cross-site navigation boolean, the first of these should not
// be in the cache because it's cross-site, but a subsequent cross-site
// navigation from a different initiator should result in a cache hit.
EXPECT_FALSE(evil_com_initiator_first_navigation_result);
EXPECT_TRUE(evil2_com_initiator_navigation_result);
break;
}
}
// Tests that a cross-origin but same-site navigation to a document that was
// previously loaded via top-level navigation uses the cache.
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
CrossOriginSameSiteNavigation) {
// Do a top-level navigation to a document to add it to the cache.
EXPECT_FALSE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// Verify that the document did get added to the cache.
EXPECT_TRUE(
NavigationResourceCached(GenURL("a.com", "/title1.html"), GURL(), false));
// Navigate to a cross-site document that performs a client-side redirect to
// the document visited previously. Ensure that repeating this request results
// in a cache hit, and then try again with a different initiating site that is
// cross-origin but same-site.
bool a_example_com_initiator_first_navigation_result =
NavigationRedirectCached(GenURL("a.example.com", "/title1.html"),
GenURL("a.com", "/title1.html"));
EXPECT_TRUE(NavigationRedirectCached(GenURL("a.example.com", "/title1.html"),
GenURL("a.com", "/title1.html")));
bool b_example_com_initiator_navigation_result = NavigationRedirectCached(
GenURL("b.example.com", "/title1.html"), GenURL("a.com", "/title1.html"));
switch (GetParam()) {
case SplitCacheTestCase::kEnabledTripleKeyed:
// If we aren't partitioning cross-site navigations then these should come
// from the cache.
EXPECT_TRUE(a_example_com_initiator_first_navigation_result);
EXPECT_TRUE(b_example_com_initiator_navigation_result);
break;
case SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool:
// Using the cross-site navigation boolean, the first of these should not
// be in the cache because it's cross-site, but a subsequent cross-site
// navigation from a different initiator should result in a cache hit.
EXPECT_FALSE(a_example_com_initiator_first_navigation_result);
EXPECT_TRUE(b_example_com_initiator_navigation_result);
break;
}
}
// This class invokes ComputeHttpCacheSize on the Network Context and
// waits for the callback to be invoked.
class SplitCacheComputeHttpCacheSize {
public:
SplitCacheComputeHttpCacheSize() = default;
~SplitCacheComputeHttpCacheSize() = default;
int64_t ComputeHttpCacheSize(BrowserContext* context,
base::Time start_time,
base::Time end_time) {
last_computed_cache_size_ = -1;
auto* network_context =
context->GetDefaultStoragePartition()->GetNetworkContext();
network::mojom::NetworkContext::ComputeHttpCacheSizeCallback size_callback =
base::BindOnce(
&SplitCacheComputeHttpCacheSize::ComputeCacheSizeCallback,
base::Unretained(this));
network_context->ComputeHttpCacheSize(start_time, end_time,
std::move(size_callback));
runloop_ = std::make_unique<base::RunLoop>();
runloop_->Run();
return last_computed_cache_size_;
}
void ComputeCacheSizeCallback(bool is_upper_bound, int64_t size) {
last_computed_cache_size_ = size;
runloop_->Quit();
}
SplitCacheComputeHttpCacheSize(const SplitCacheComputeHttpCacheSize&) =
delete;
SplitCacheComputeHttpCacheSize& operator=(
const SplitCacheComputeHttpCacheSize&) = delete;
private:
std::unique_ptr<base::RunLoop> runloop_;
int64_t last_computed_cache_size_ = -1;
};
// Tests that NotifyExternalCacheHit() has the correct value of
// is_subframe_document_resource by checking that the size of the http cache
// resources accessed after the resource is loaded from the blink cache is the
// same as before that.
// TODO(crbug.com/40164302): Test is flaky on Win.
#if BUILDFLAG(IS_WIN)
#define MAYBE_NotifyExternalCacheHitCheckSubframeBit \
DISABLED_NotifyExternalCacheHitCheckSubframeBit
#else
#define MAYBE_NotifyExternalCacheHitCheckSubframeBit \
NotifyExternalCacheHitCheckSubframeBit
#endif
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
MAYBE_NotifyExternalCacheHitCheckSubframeBit) {
ResourceLoadObserver observer(shell());
BrowserContext* context = shell()->web_contents()->GetBrowserContext();
std::unique_ptr<SplitCacheComputeHttpCacheSize> http_cache_size =
std::make_unique<SplitCacheComputeHttpCacheSize>();
// Since no resources are loaded yet, Http cache's size will be 0.
EXPECT_EQ(0, http_cache_size->ComputeHttpCacheSize(context, base::Time(),
base::Time::Max()));
// First fetch will populate the cache.
GURL page_url(
embedded_test_server()->GetURL("/page_with_cached_subresource.html"));
EXPECT_TRUE(NavigateToURL(shell(), page_url));
// Checking the size of resources should be >0 as resources have been loaded.
int64_t size1 = http_cache_size->ComputeHttpCacheSize(context, base::Time(),
base::Time::Max());
EXPECT_GT(size1, 0);
ASSERT_EQ(2U, observer.resource_load_entries().size());
EXPECT_TRUE(observer.memory_cached_loaded_urls().empty());
observer.Reset();
// Make sure time has moved forward from when the last entry was cached.
base::Time start = base::Time::Now();
while (start == base::Time::Now()) {
}
base::Time after_first = base::Time::Now();
// Loading again should serve the request out of the in-memory cache.
EXPECT_TRUE(NavigateToURL(shell(), page_url));
ASSERT_EQ(1U, observer.resource_load_entries().size());
ASSERT_EQ(1U, observer.memory_cached_loaded_urls().size());
// Loading from the in-memory cache also changes the last accessed time of
// those resources in the http cache. So if we check the size of resources
// accessed after the first load till now, it will be the same as before the
// 2nd navigation.
int64_t size2 = http_cache_size->ComputeHttpCacheSize(context, after_first,
base::Time::Max());
EXPECT_EQ(size1, size2);
}
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
DedicatedWorkers) {
// Load 3p.com/script from a.com's worker. The first time it's loaded from the
// network and the second it's cached.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorker(
GenURL("a.com", "/title1.html"), GenURL("a.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("a.com", "/title1.html"), GenURL("a.com", "/worker.js")));
// Load 3p.com/script from a worker with a new top-frame origin. Due to split
// caching it's a cache miss.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorker(
GenURL("b.com", "/title1.html"), GenURL("b.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("b.com", "/title1.html"), GenURL("b.com", "/worker.js")));
// Load 3p.com/script from a nested worker with a new top-frame origin. Due to
// split caching it's a cache miss.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorker(
GenURL("c.com", "/title1.html"),
GenURL("c.com", "/embedding_worker.js?c")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("c.com", "/title1.html"),
GenURL("c.com", "/embedding_worker.js?c")));
// Load 3p.com/script from a worker with a new top-frame origin and nested in
// a cross-origin iframe. Due to split caching it's a cache miss.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("d.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("d.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
// Load 3p.com/script from a worker with a new top-frame origin and nested in
// a cross-origin iframe whose URL has previously been loaded.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("f.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("f.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
}
// https://crbug.com/1218723 started flaking after Field Trial Testing Config
// was enabled for content_browsertests.
IN_PROC_BROWSER_TEST_P(SplitCacheContentBrowserTestEnabled,
DISABLED_DedicatedWorkersScripts) {
// Load a.com's worker. The first time the worker script is loaded from the
// network and the second it's cached.
EXPECT_FALSE(DedicatedWorkerScriptCached(
GenURL("a.com", "/title1.html"), GURL(), GenURL("a.com", "/worker.js")));
EXPECT_TRUE(DedicatedWorkerScriptCached(
GenURL("a.com", "/title1.html"), GURL(), GenURL("a.com", "/worker.js")));
// Load a nested worker with a new top-frame origin. It's a cache miss for
// the embedding worker the first time, as it hasn't been loaded yet, and
// then the second time it's cached.
EXPECT_FALSE(
DedicatedWorkerScriptCached(GenURL("c.com", "/title1.html"), GURL(),
GenURL("c.com", "/embedding_worker.js?c")));
EXPECT_TRUE(
DedicatedWorkerScriptCached(GenURL("c.com", "/title1.html"), GURL(),
GenURL("c.com", "/embedding_worker.js?c")));
// Load a worker with a new top-frame origin and nested in a cross-origin
// iframe. It's a cache miss for the worker script the first time, then
// the second time it's cached.
EXPECT_FALSE(DedicatedWorkerScriptCached(GenURL("d.com", "/title1.html"),
GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
EXPECT_TRUE(DedicatedWorkerScriptCached(GenURL("d.com", "/title1.html"),
GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
// Load a worker with a new top-frame origin and nested in a cross-origin
// iframe whose URL has previously been loaded. Due to split caching it's a
// cache miss for the worker script the first time.
EXPECT_FALSE(DedicatedWorkerScriptCached(GenURL("f.com", "/title1.html"),
GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
EXPECT_TRUE(DedicatedWorkerScriptCached(GenURL("f.com", "/title1.html"),
GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
}
IN_PROC_BROWSER_TEST_F(SplitCacheContentBrowserTestDisabled, DedicatedWorkers) {
// Load 3p.com/script from a.com's worker. The first time it's loaded from the
// network and the second it's cached.
EXPECT_FALSE(TestResourceLoadFromDedicatedWorker(
GenURL("a.com", "/title1.html"), GenURL("a.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("a.com", "/title1.html"), GenURL("a.com", "/worker.js")));
// Load 3p.com/script from b.com's worker. The cache isn't split by top-frame
// origin so the resource is already cached.
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("b.com", "/title1.html"), GenURL("b.com", "/worker.js")));
// Load 3p.com/script from a nested worker with a new top-frame origin. The
// cache isn't split by top-frame origin so the resource is already cached.
EXPECT_TRUE(TestResourceLoadFromDedicatedWorker(
GenURL("c.com", "/title1.html"),
GenURL("c.com", "/embedding_worker.js?c")));
// Load 3p.com/script from a worker with a new top-frame origin and nested in
// a cross-origin iframe. The cache isn't split by top-frame origin so the
// resource is already cached.
EXPECT_TRUE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("d.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
EXPECT_TRUE(TestResourceLoadFromDedicatedWorkerInIframe(
GenURL("f.com", "/title1.html"), GenURL("e.com", "/title1.html"),
GenURL("e.com", "/worker.js")));
}
class SplitCacheByIncludeCredentialsTest : public ContentBrowserTest {
public:
SplitCacheByIncludeCredentialsTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
feature_list_.InitAndEnableFeature(
net::features::kSplitCacheByIncludeCredentials);
}
int cacheable_request_count() const { return cacheable_request_count_; }
net::EmbeddedTestServer* https_server() { return &https_server_; }
GURL CacheableUrl() { return https_server()->GetURL("b.test", "/cacheable"); }
void RequestAnonymous(Shell* shell) {
EXPECT_TRUE(ExecJs(shell, JsReplace(R"(
new Promise(resolve => {
const image = new Image();
image.src = $1;
image.crossOrigin = "anonymous";
image.onload = resolve;
document.body.appendChild(image);
});
)",
CacheableUrl())));
}
void RequestUseCredentials(Shell* shell) {
EXPECT_TRUE(ExecJs(shell, JsReplace(R"(
new Promise(resolve => {
const image = new Image();
image.src = $1;
image.crossOrigin = "use-credentials";
image.onload = resolve;
document.body.appendChild(image);
});
)",
CacheableUrl())));
}
private:
void SetUpOnMainThread() final {
ContentBrowserTest::SetUpOnMainThread();
cacheable_request_count_ = 0;
host_resolver()->AddRule("*", "127.0.0.1");
https_server()->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
https_server()->RegisterRequestHandler(
base::BindRepeating(&SplitCacheByIncludeCredentialsTest::RequestHandler,
base::Unretained(this)));
https_server()->ServeFilesFromSourceDirectory(GetTestDataFilePath());
net::test_server::RegisterDefaultHandlers(https_server());
SetupCrossSiteRedirector(https_server());
CHECK(https_server()->Start());
}
std::unique_ptr<net::test_server::HttpResponse> RequestHandler(
const net::test_server::HttpRequest& request) {
if (request.relative_url == "/cacheable") {
cacheable_request_count_++;
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_content_type("image/svg+xml");
response->set_content("<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>");
response->AddCustomHeader("Cache-Control", "max-age=3600");
response->AddCustomHeader("Cross-Origin-Resource-Policy", "cross-origin");
response->AddCustomHeader("Access-Control-Allow-Credentials", "true");
response->AddCustomHeader(
"Access-Control-Allow-Origin",
https_server()->GetOrigin("a.test").Serialize());
return response;
}
return nullptr;
}
base::test::ScopedFeatureList feature_list_;
net::EmbeddedTestServer https_server_;
// Initialized and read from the UI thread. Written from the |https_server_|.
std::atomic<int> cacheable_request_count_;
};
// Note: Compared to .DifferentProcess and .DifferentProcessVariant, this test
// emits requests from the same renderer process. This is useful for checking
// the behavior of blink's memory cache instead of the HTTP cache.
IN_PROC_BROWSER_TEST_F(SplitCacheByIncludeCredentialsTest, SameProcess) {
GURL page_url(https_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), page_url));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(0, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(2, cacheable_request_count());
}
IN_PROC_BROWSER_TEST_F(SplitCacheByIncludeCredentialsTest, SameProcessVariant) {
GURL page_url(https_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), page_url));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
EXPECT_EQ(0, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(2, cacheable_request_count());
}
// Note: Compared to .SameProcess and .SameProcessVariant, this test emits
// requests from two different renderer process. This is useful for checking the
// behavior of the HTTP cache instead of blink's memory cache.
//
// COOP+COEP are used to get two same-origin documents loaded from different
// renderer process. This avoids interferences from
// SplitCacheByNetworkIsolationKey.
IN_PROC_BROWSER_TEST_F(SplitCacheByIncludeCredentialsTest, DifferentProcess) {
GURL page_1_url(https_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), page_1_url));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
GURL page_2_url =
https_server()->GetURL("a.test",
"/set-header?"
"Cross-Origin-Opener-Policy: same-origin&"
"Cross-Origin-Embedder-Policy: require-corp");
ShellAddedObserver shell_observer;
EXPECT_TRUE(ExecJs(shell(), JsReplace("window.open($1)", page_2_url)));
Shell* new_shell = shell_observer.GetShell();
EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents()));
EXPECT_NE(static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryMainFrame()
->GetProcess(),
static_cast<WebContentsImpl*>(new_shell->web_contents())
->GetPrimaryMainFrame()
->GetProcess());
EXPECT_EQ(0, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestAnonymous(new_shell);
EXPECT_EQ(1, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestUseCredentials(new_shell);
EXPECT_EQ(2, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(2, cacheable_request_count());
}
IN_PROC_BROWSER_TEST_F(SplitCacheByIncludeCredentialsTest,
DifferentProcessVariant) {
GURL page_1_url(https_server()->GetURL("a.test", "/empty.html"));
EXPECT_TRUE(NavigateToURL(shell(), page_1_url));
EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
GURL page_2_url =
https_server()->GetURL("a.test",
"/set-header?"
"Cross-Origin-Opener-Policy: same-origin&"
"Cross-Origin-Embedder-Policy: require-corp");
ShellAddedObserver shell_observer;
EXPECT_TRUE(ExecJs(shell(), JsReplace("window.open($1)", page_2_url)));
Shell* new_shell = shell_observer.GetShell();
EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents()));
EXPECT_NE(static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryMainFrame()
->GetProcess(),
static_cast<WebContentsImpl*>(new_shell->web_contents())
->GetPrimaryMainFrame()
->GetProcess());
EXPECT_EQ(0, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(1, cacheable_request_count());
RequestUseCredentials(new_shell);
EXPECT_EQ(1, cacheable_request_count());
RequestAnonymous(shell());
EXPECT_EQ(2, cacheable_request_count());
RequestAnonymous(new_shell);
EXPECT_EQ(2, cacheable_request_count());
RequestUseCredentials(shell());
EXPECT_EQ(2, cacheable_request_count());
}
} // namespace
} // namespace content
|