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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string_view>
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/weak_document_ptr.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/shell/browser/shell.h"
#include "content/shell/common/render_frame_test_helper.mojom.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/chrome_debug_urls.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "url/gurl.h"
namespace content {
namespace {
// The general structure of all tests is to navigate A -> A -> B. A -> A will
// reuse the same `RenderFrameHost` (without RenderDocument) while A -> B will
// swap to a new `RenderFrameHost` (with --site-per-process).
class DocumentTokenBrowserTest : public ContentBrowserTest {
protected:
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
WebContentsImpl* web_contents() {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
blink::DocumentToken GetBrowserSideToken(ToRenderFrameHost adapter) {
return static_cast<RenderFrameHostImpl*>(adapter.render_frame_host())
->GetDocumentToken();
}
// Verifies that the browser-side `DocumentToken` and the renderer-side
// `DocumentToken` have matching values.
[[nodiscard]] ::testing::AssertionResult VerifyMatchingTokens(
ToRenderFrameHost adapter) {
blink::DocumentToken token_from_browser = GetBrowserSideToken(adapter);
mojo::Remote<mojom::RenderFrameTestHelper> remote;
adapter.render_frame_host()->GetRemoteInterfaces()->GetInterface(
remote.BindNewPipeAndPassReceiver());
blink::DocumentToken token_from_renderer;
base::RunLoop run_loop;
remote->GetDocumentToken(
base::BindLambdaForTesting([&](const blink::DocumentToken& token) {
token_from_renderer = token;
run_loop.Quit();
}));
run_loop.Run();
if (token_from_browser == token_from_renderer) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure()
<< "browser token was " << token_from_browser
<< " but renderer token was " << token_from_renderer;
}
// Whether or not `NavigateAndGetNewToken()` should wait for the response and
// validate document token state immediately afterwards. Most tests should
// expect and wait for a response; however, tests that are exercising
// `CommitFailedNavigation()` will probably want to specify `kNo`.
enum class ExpectedResponse {
kYes,
kNo,
};
// Navigate `adapter.render_frame_host()` to `target_url`. Verifies that the
// browser and renderer state are in sync, and that the document token is not
// updated until the navigation actually commits.
//
// Note: this helper makes IPCs to the `RenderFrame`; for the first navigation
// in a WebContents, it is typically more appropriate to use `NavigateToURL()`
// or another similar helper instead.
blink::DocumentToken NavigateAndGetNewToken(
ToRenderFrameHost adapter,
const GURL& target_url,
ExpectedResponse expect_response = ExpectedResponse::kYes) {
SCOPED_TRACE(target_url.spec());
// Capture the FrameTreeNode now; when a navigation commits, the current
// RenderFrameHost may change.
RenderFrameHostImpl* const old_render_frame_host =
static_cast<RenderFrameHostImpl*>(adapter.render_frame_host());
FrameTreeNode* const frame_tree_node =
old_render_frame_host->frame_tree_node();
const int old_process_id =
old_render_frame_host->GetProcess()->GetDeprecatedID();
const blink::LocalFrameToken old_frame_token =
old_render_frame_host->GetFrameToken();
const blink::DocumentToken old_document_token =
GetBrowserSideToken(old_render_frame_host);
const WeakDocumentPtr old_weak_document_ptr =
old_render_frame_host->GetWeakDocumentPtr();
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// Start a new navigation in the main frame. The navigation is still
// ongoing, so `DocumentToken` should not be updated yet.
TestNavigationManager nav_manager(
WebContents::FromRenderFrameHost(old_render_frame_host), target_url);
EXPECT_TRUE(BeginNavigateToURLFromRenderer(adapter, target_url));
EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host));
EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// Just before the request is actually issued, the navigation is still
// ongoing, so `DocumentToken` should not be updated yet.
EXPECT_TRUE(nav_manager.WaitForRequestStart());
EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host));
EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
if (ExpectedResponse::kYes == expect_response) {
// Just before reading the response, the navigation is still ongoing, so
// `DocumentToken` should not be updated yet.
EXPECT_TRUE(nav_manager.WaitForResponse());
EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host));
EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
}
// Once a cross-document navigation completes, the document token should be
// updated though.
EXPECT_TRUE(nav_manager.WaitForNavigationFinished());
// The RenderFrameHost may have changed; use the FrameTreeNode captured
// above instead.
RenderFrameHostImpl* const new_render_frame_host =
frame_tree_node->current_frame_host();
EXPECT_EQ(target_url, new_render_frame_host->GetLastCommittedURL());
EXPECT_TRUE(VerifyMatchingTokens(new_render_frame_host));
const blink::LocalFrameToken new_frame_token =
new_render_frame_host->GetFrameToken();
const blink::DocumentToken new_document_token =
GetBrowserSideToken(new_render_frame_host);
EXPECT_NE(new_document_token, old_document_token);
if (new_frame_token == old_frame_token) {
// If the RenderFrameHost is reused, it should no longer be possible to
// use the old token to look up the RenderFrameHost.
EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
} else if (old_weak_document_ptr.AsRenderFrameHostIfValid()) {
// Otherwise, if the old RenderFrameHost is still around, it should still
// map to the same RenderFrameHost.
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
}
EXPECT_EQ(new_render_frame_host,
RenderFrameHostImpl::FromDocumentToken(
new_render_frame_host->GetProcess()->GetDeprecatedID(),
new_document_token));
return new_document_token;
}
};
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, MainFrameBasic) {
std::vector<blink::DocumentToken> seen_tokens;
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("b.com", "/title1.html")));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, SubFrameBasic) {
std::vector<blink::DocumentToken> seen_tokens;
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
EXPECT_TRUE(VerifyMatchingTokens(ChildFrameAt(web_contents(), 0)));
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
seen_tokens.push_back(GetBrowserSideToken(ChildFrameAt(web_contents(), 0)));
seen_tokens.push_back(NavigateAndGetNewToken(
ChildFrameAt(web_contents(), 0),
embedded_test_server()->GetURL("a.com", "/title1.html")));
// Main document did not navigate so the token should be the same.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
ChildFrameAt(web_contents(), 0),
embedded_test_server()->GetURL("b.com", "/title1.html")));
// Main document did not navigate so the token should be the same.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, NewWindowBasic) {
std::vector<blink::DocumentToken> seen_tokens;
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_EQ(1u, Shell::windows().size());
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
WebContents* new_contents = nullptr;
{
// This block is largely derived from `NavigateAndGetNewToken()`. This test
// cannot easily reuse that helper because:
//
// - it is important to specify an actual target URL other than about:blank
// for `window.open()`. Specifying no target URL and then later navigating
// the window has subtly different behavior (e.g. the
// `NewWindowSyncCommit` test below).
// - the helper expects the `WebContents` to already exist in order to
// install
// `TestNavigationManager`. However, in this test, a new `WebContents` is
// created in the process of running the test.
ExecuteScriptAsync(web_contents(), JsReplace("window.open($1)",
embedded_test_server()->GetURL(
"a.com", "/title1.html")));
ShellAddedObserver wait_for_new_shell;
new_contents = wait_for_new_shell.GetShell()->web_contents();
DCHECK_EQ(2u, Shell::windows().size());
DCHECK_EQ(new_contents, Shell::windows()[1]->web_contents());
DCHECK_NE(new_contents, web_contents());
seen_tokens.push_back(GetBrowserSideToken(new_contents));
TestNavigationManager nav_manager(
new_contents, embedded_test_server()->GetURL("a.com", "/title1.html"));
// Capture the FrameTreeNode now; when a navigation commits, the current
// RenderFrameHost may change.
RenderFrameHostImpl* const old_render_frame_host =
static_cast<RenderFrameHostImpl*>(new_contents->GetPrimaryMainFrame());
FrameTreeNode* const frame_tree_node =
old_render_frame_host->frame_tree_node();
const int old_process_id =
old_render_frame_host->GetProcess()->GetDeprecatedID();
const blink::LocalFrameToken old_frame_token =
old_render_frame_host->GetFrameToken();
const blink::DocumentToken old_document_token =
GetBrowserSideToken(new_contents);
const WeakDocumentPtr old_weak_document_ptr =
old_render_frame_host->GetWeakDocumentPtr();
EXPECT_TRUE(VerifyMatchingTokens(new_contents));
EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// Even after creating a new window, the original `WebContents` should still
// have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
// Just before the request is actually issued, the navigation is still
// ongoing, so `DocumentToken` should not be updated yet.
EXPECT_TRUE(nav_manager.WaitForRequestStart());
EXPECT_TRUE(VerifyMatchingTokens(new_contents));
EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
// Just before reading the response, the navigation is still ongoing, so
// `DocumentToken` should not be updated yet.
EXPECT_TRUE(nav_manager.WaitForResponse());
EXPECT_TRUE(VerifyMatchingTokens(new_contents));
EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents));
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
// Once a cross-document navigation completes, the document token should be
// updated though.
ASSERT_TRUE(nav_manager.WaitForNavigationFinished());
// The RenderFrameHost may have changed; use the FrameTreeNode captured
// above instead.
RenderFrameHostImpl* const new_render_frame_host =
frame_tree_node->current_frame_host();
EXPECT_EQ(embedded_test_server()->GetURL("a.com", "/title1.html"),
new_render_frame_host->GetLastCommittedURL());
EXPECT_TRUE(VerifyMatchingTokens(new_render_frame_host));
const blink::LocalFrameToken new_frame_token =
new_render_frame_host->GetFrameToken();
const blink::DocumentToken new_document_token =
GetBrowserSideToken(new_render_frame_host);
EXPECT_NE(new_document_token, old_document_token);
if (new_frame_token == old_frame_token) {
// If the RenderFrameHost is reused, it should no longer be possible to
// use the old token to look up the RenderFrameHost.
EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
} else if (old_weak_document_ptr.AsRenderFrameHostIfValid()) {
// Otherwise, if the old RenderFrameHost is still around, it should still
// map to the same RenderFrameHost.
EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
}
EXPECT_EQ(new_render_frame_host,
RenderFrameHostImpl::FromDocumentToken(
new_render_frame_host->GetProcess()->GetDeprecatedID(),
new_document_token));
seen_tokens.push_back(new_document_token);
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
}
seen_tokens.push_back(NavigateAndGetNewToken(
new_contents, embedded_test_server()->GetURL("a.com", "/title1.html")));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
new_contents, embedded_test_server()->GetURL("b.com", "/title1.html")));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, SubFrameSyncCommit) {
std::vector<blink::DocumentToken> seen_tokens;
// This is a basic test that the synchronous commit of about:blank reuses the
// same DocumentToken. See https://crbug.com/778318 for more details.
ASSERT_TRUE(NavigateToURL(
web_contents(),
embedded_test_server()->GetURL("a.com", "/page_with_blank_iframe.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
EXPECT_TRUE(VerifyMatchingTokens(ChildFrameAt(web_contents(), 0)));
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
seen_tokens.push_back(GetBrowserSideToken(ChildFrameAt(web_contents(), 0)));
seen_tokens.push_back(NavigateAndGetNewToken(
ChildFrameAt(web_contents(), 0),
embedded_test_server()->GetURL("a.com", "/title1.html")));
// Main document did not navigate so the token should be the same.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
ChildFrameAt(web_contents(), 0),
embedded_test_server()->GetURL("b.com", "/title1.html")));
// Main document did not navigate so the token should be the same.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, NewWindowSyncCommit) {
std::vector<blink::DocumentToken> seen_tokens;
ASSERT_TRUE(NavigateToURL(web_contents(), GURL("about:blank")));
EXPECT_EQ(1u, Shell::windows().size());
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
// This is a basic test that the synchronous commit of about:blank reuses the
// same DocumentToken. See https://crbug.com/778318 for more details.
ASSERT_TRUE(ExecJs(web_contents(), "window.open()"));
ASSERT_EQ(2u, Shell::windows().size());
WebContents* new_contents = Shell::windows()[1]->web_contents();
DCHECK_NE(new_contents, web_contents());
EXPECT_TRUE(VerifyMatchingTokens(new_contents));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
new_contents, embedded_test_server()->GetURL("a.com", "/title1.html")));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
new_contents, embedded_test_server()->GetURL("a.com", "/title1.html")));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
new_contents, embedded_test_server()->GetURL("b.com", "/title1.html")));
// The original `WebContents` should still have the same `DocumentToken`.
EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents()));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, JavascriptURL) {
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
const blink::DocumentToken token = GetBrowserSideToken(web_contents());
// A javascript: navigation that replaces the document should not change the
// DocumentToken. This does not use the normal Navigate*() helpers since it
// does not commit a normal cross-document navigation.
ASSERT_TRUE(ExecJs(web_contents(),
JsReplace("location = $1", "javascript:'Hello world!'")));
EXPECT_EQ("Hello world!", EvalJs(web_contents(), "document.body.innerText"));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
EXPECT_EQ(token, GetBrowserSideToken(web_contents()));
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, FailedNavigation) {
std::vector<blink::DocumentToken> seen_tokens;
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
seen_tokens.push_back(GetBrowserSideToken(web_contents()));
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("a.com", "/close-socket"),
ExpectedResponse::kNo));
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("a.com", "/close-socket"),
ExpectedResponse::kNo));
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("b.com", "/close-socket"),
ExpectedResponse::kNo));
// Test that a regular successful navigation still updates the document token.
seen_tokens.push_back(NavigateAndGetNewToken(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
std::set unique_tokens(seen_tokens.begin(), seen_tokens.end());
EXPECT_EQ(unique_tokens.size(), seen_tokens.size());
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, CrashThenReload) {
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
const int old_process_id =
web_contents()->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID();
const blink::DocumentToken old_document_token =
GetBrowserSideToken(web_contents());
// Cause the renderer to crash.
RenderProcessHostWatcher crash_observer(
web_contents(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
EXPECT_FALSE(NavigateToURL(shell(), GURL(blink::kChromeUICrashURL)));
// Wait for browser to notice the renderer crash.
crash_observer.Wait();
// After a crash, the DocumentToken should still be the same even though the
// renderer process is gone..
EXPECT_EQ(old_document_token, GetBrowserSideToken(web_contents()));
// But when a live RenderFrame is needed again, RenderDocument should force a
// new RenderFrameHost, and thus, a new DocumentToken. The remainder of this
// test does not use `NavigateAndGetNewToken()`, which tries to use a
// renderer-initiated navigation (which is not possible when the renderer is
// not live).
TestNavigationManager nav_manager(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"));
shell()->LoadURL(embedded_test_server()->GetURL("a.com", "/title1.html"));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
const int new_process_id =
web_contents()->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID();
const blink::DocumentToken token_after_navigation_started =
GetBrowserSideToken(web_contents());
EXPECT_NE(token_after_navigation_started, old_document_token);
const WeakDocumentPtr document_weak_ptr =
web_contents()->GetPrimaryMainFrame()->GetWeakDocumentPtr();
EXPECT_EQ(web_contents()->GetPrimaryMainFrame(),
RenderFrameHostImpl::FromDocumentToken(
new_process_id, token_after_navigation_started));
// The old RenderFrameHost should be gone at this point, so a document token
// lookup should fail.
EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken(
old_process_id, old_document_token));
// After the navigation finishes, the RenderFrameHost will still use the same
// DocumentToken, since no new DocumentAssociatedData was created. The latter
// is indirectly tested by checking if the WeakDocumentPtr is still valid
// after the navigation commits.
ASSERT_TRUE(nav_manager.WaitForNavigationFinished());
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
const blink::DocumentToken token_after_navigation_finished =
GetBrowserSideToken(web_contents());
EXPECT_NE(token_after_navigation_finished, old_document_token);
EXPECT_EQ(token_after_navigation_finished, token_after_navigation_started);
EXPECT_NE(document_weak_ptr.AsRenderFrameHostIfValid(), nullptr);
EXPECT_EQ(web_contents()->GetPrimaryMainFrame(),
RenderFrameHostImpl::FromDocumentToken(
new_process_id, token_after_navigation_started));
}
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest,
CrashThenImmediateReinitialize) {
ASSERT_TRUE(NavigateToURL(
web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")));
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
RenderFrameHostImpl* main_frame = web_contents()->GetPrimaryMainFrame();
const blink::LocalFrameToken frame_token = main_frame->GetFrameToken();
const blink::DocumentToken old_document_token =
GetBrowserSideToken(main_frame);
const WeakDocumentPtr document_weak_ptr = main_frame->GetWeakDocumentPtr();
// Cause the renderer to crash.
RenderProcessHostWatcher crash_observer(
web_contents(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
EXPECT_FALSE(NavigateToURL(shell(), GURL(blink::kChromeUICrashURL)));
// Wait for browser to notice the renderer crash.
crash_observer.Wait();
// If the main render frame is re-initialized, it also gets a new
// DocumentAssociatedData. Validate that the new DocumentAssociatedData is
// created before the renderer is re-created; a typical failure in this path
// will manifest as a mismatch between the browser and renderer-side document
// tokens.
main_frame->frame_tree_node()
->render_manager()
->InitializeMainRenderFrameForImmediateUse();
// The RenderFrameHost should be reused.
ASSERT_EQ(frame_token,
web_contents()->GetPrimaryMainFrame()->GetFrameToken());
EXPECT_TRUE(VerifyMatchingTokens(web_contents()));
// The re-created RenderFrame should have a distinct document token.
const blink::DocumentToken new_document_token =
GetBrowserSideToken(web_contents());
EXPECT_NE(new_document_token, old_document_token);
// The previous DocumentWeakPtr should be invalidated since the
// DocumentAssociatedData was re-created.
EXPECT_FALSE(document_weak_ptr.AsRenderFrameHostIfValid());
// Even though the RenderFrameHost did not change, only a lookup using the new
// DocumentToken should succeed.
EXPECT_EQ(web_contents()->GetPrimaryMainFrame(),
RenderFrameHostImpl::FromDocumentToken(web_contents()
->GetPrimaryMainFrame()
->GetProcess()
->GetDeprecatedID(),
new_document_token));
EXPECT_EQ(nullptr,
RenderFrameHostImpl::FromDocumentToken(web_contents()
->GetPrimaryMainFrame()
->GetProcess()
->GetDeprecatedID(),
old_document_token));
}
// TODO(crbug.com/40238502): Add tests for bfcache navigations and
// prerender activations.
IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, MismatchedProcessID) {
RenderFrameHostImpl* main_frame = web_contents()->GetPrimaryMainFrame();
bool called = false;
mojo::ReportBadMessageCallback callback =
base::BindLambdaForTesting([&called](std::string_view reason) {
called = true;
EXPECT_EQ("process ID does not match requested DocumentToken", reason);
});
EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken(
main_frame->GetProcess()->GetDeprecatedID() + 1,
main_frame->GetDocumentToken(), &callback));
EXPECT_TRUE(called);
}
} // namespace
} // namespace content
|