Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 7 | #include "base/run_loop.h" |
| 8 | #include "base/test/bind.h" |
| 9 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 10 | #include "content/browser/web_contents/web_contents_impl.h" |
| 11 | #include "content/public/browser/weak_document_ptr.h" |
| 12 | #include "content/public/test/browser_test.h" |
| 13 | #include "content/public/test/browser_test_utils.h" |
| 14 | #include "content/public/test/content_browser_test.h" |
| 15 | #include "content/public/test/content_browser_test_utils.h" |
| 16 | #include "content/shell/browser/shell.h" |
| 17 | #include "content/shell/common/render_frame_test_helper.mojom.h" |
| 18 | #include "content/test/content_browser_test_utils_internal.h" |
| 19 | #include "mojo/public/cpp/bindings/remote.h" |
| 20 | #include "net/dns/mock_host_resolver.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 22 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
| 23 | #include "third_party/blink/public/common/tokens/tokens.h" |
| 24 | #include "url/gurl.h" |
| 25 | |
| 26 | namespace content { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // The general structure of all tests is to navigate A -> A -> B. A -> A will |
| 31 | // reuse the same `RenderFrameHost` (without RenderDocument) while A -> B will |
| 32 | // swap to a new `RenderFrameHost` (with --site-per-process). |
| 33 | class DocumentTokenBrowserTest : public ContentBrowserTest { |
| 34 | protected: |
| 35 | void SetUpOnMainThread() override { |
| 36 | ContentBrowserTest::SetUpOnMainThread(); |
| 37 | |
| 38 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 39 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 40 | } |
| 41 | |
| 42 | WebContentsImpl* web_contents() { |
| 43 | return static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 44 | } |
| 45 | |
| 46 | blink::DocumentToken GetBrowserSideToken(ToRenderFrameHost adapter) { |
| 47 | return static_cast<RenderFrameHostImpl*>(adapter.render_frame_host()) |
| 48 | ->GetDocumentToken(); |
| 49 | } |
| 50 | |
| 51 | // Verifies that the browser-side `DocumentToken` and the renderer-side |
| 52 | // `DocumentToken` have matching values. |
| 53 | [[nodiscard]] ::testing::AssertionResult VerifyMatchingTokens( |
| 54 | ToRenderFrameHost adapter) { |
| 55 | blink::DocumentToken token_from_browser = GetBrowserSideToken(adapter); |
| 56 | |
| 57 | mojo::Remote<mojom::RenderFrameTestHelper> remote; |
| 58 | adapter.render_frame_host()->GetRemoteInterfaces()->GetInterface( |
| 59 | remote.BindNewPipeAndPassReceiver()); |
| 60 | blink::DocumentToken token_from_renderer; |
| 61 | base::RunLoop run_loop; |
| 62 | remote->GetDocumentToken( |
| 63 | base::BindLambdaForTesting([&](const blink::DocumentToken& token) { |
| 64 | token_from_renderer = token; |
| 65 | run_loop.Quit(); |
| 66 | })); |
| 67 | run_loop.Run(); |
| 68 | |
| 69 | if (token_from_browser == token_from_renderer) { |
| 70 | return ::testing::AssertionSuccess(); |
| 71 | } |
| 72 | return ::testing::AssertionFailure() |
| 73 | << "browser token was " << token_from_browser |
| 74 | << " but renderer token was " << token_from_renderer; |
| 75 | } |
| 76 | |
| 77 | // Whether or not `NavigateAndGetNewToken()` should wait for the response and |
| 78 | // validate document token state immediately afterwards. Most tests should |
| 79 | // expect and wait for a response; however, tests that are exercising |
| 80 | // `CommitFailedNavigation()` will probably want to specify `kNo`. |
| 81 | enum class ExpectedResponse { |
| 82 | kYes, |
| 83 | kNo, |
| 84 | }; |
| 85 | |
| 86 | // Navigate `adapter.render_frame_host()` to `target_url`. Verifies that the |
| 87 | // browser and renderer state are in sync, and that the document token is not |
| 88 | // updated until the navigation actually commits. |
| 89 | // |
| 90 | // Note: this helper makes IPCs to the `RenderFrame`; for the first navigation |
| 91 | // in a WebContents, it is typically more appropriate to use `NavigateToURL()` |
| 92 | // or another similar helper instead. |
| 93 | blink::DocumentToken NavigateAndGetNewToken( |
| 94 | ToRenderFrameHost adapter, |
| 95 | const GURL& target_url, |
| 96 | ExpectedResponse expect_response = ExpectedResponse::kYes) { |
| 97 | SCOPED_TRACE(target_url.spec()); |
| 98 | // Capture the FrameTreeNode now; when a navigation commits, the current |
| 99 | // RenderFrameHost may change. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 100 | RenderFrameHostImpl* const old_render_frame_host = |
| 101 | static_cast<RenderFrameHostImpl*>(adapter.render_frame_host()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 102 | FrameTreeNode* const frame_tree_node = |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 103 | old_render_frame_host->frame_tree_node(); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 104 | const int old_process_id = |
| 105 | old_render_frame_host->GetProcess()->GetDeprecatedID(); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 106 | const blink::LocalFrameToken old_frame_token = |
| 107 | old_render_frame_host->GetFrameToken(); |
| 108 | const blink::DocumentToken old_document_token = |
| 109 | GetBrowserSideToken(old_render_frame_host); |
| 110 | const WeakDocumentPtr old_weak_document_ptr = |
| 111 | old_render_frame_host->GetWeakDocumentPtr(); |
| 112 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 113 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 114 | |
| 115 | // Start a new navigation in the main frame. The navigation is still |
| 116 | // ongoing, so `DocumentToken` should not be updated yet. |
| 117 | TestNavigationManager nav_manager( |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 118 | WebContents::FromRenderFrameHost(old_render_frame_host), target_url); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 119 | EXPECT_TRUE(BeginNavigateToURLFromRenderer(adapter, target_url)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 120 | EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host)); |
| 121 | EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host)); |
| 122 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 123 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 124 | |
| 125 | // Just before the request is actually issued, the navigation is still |
| 126 | // ongoing, so `DocumentToken` should not be updated yet. |
| 127 | EXPECT_TRUE(nav_manager.WaitForRequestStart()); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 128 | EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host)); |
| 129 | EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host)); |
| 130 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 131 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 132 | |
| 133 | if (ExpectedResponse::kYes == expect_response) { |
| 134 | // Just before reading the response, the navigation is still ongoing, so |
| 135 | // `DocumentToken` should not be updated yet. |
| 136 | EXPECT_TRUE(nav_manager.WaitForResponse()); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 137 | EXPECT_TRUE(VerifyMatchingTokens(old_render_frame_host)); |
| 138 | EXPECT_EQ(old_document_token, GetBrowserSideToken(old_render_frame_host)); |
| 139 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 140 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Once a cross-document navigation completes, the document token should be |
| 144 | // updated though. |
Fergal Daly | 105305ad | 2023-01-16 08:21:52 | [diff] [blame] | 145 | EXPECT_TRUE(nav_manager.WaitForNavigationFinished()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 146 | // The RenderFrameHost may have changed; use the FrameTreeNode captured |
| 147 | // above instead. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 148 | RenderFrameHostImpl* const new_render_frame_host = |
| 149 | frame_tree_node->current_frame_host(); |
| 150 | EXPECT_EQ(target_url, new_render_frame_host->GetLastCommittedURL()); |
| 151 | EXPECT_TRUE(VerifyMatchingTokens(new_render_frame_host)); |
| 152 | const blink::LocalFrameToken new_frame_token = |
| 153 | new_render_frame_host->GetFrameToken(); |
| 154 | const blink::DocumentToken new_document_token = |
| 155 | GetBrowserSideToken(new_render_frame_host); |
| 156 | EXPECT_NE(new_document_token, old_document_token); |
| 157 | if (new_frame_token == old_frame_token) { |
| 158 | // If the RenderFrameHost is reused, it should no longer be possible to |
| 159 | // use the old token to look up the RenderFrameHost. |
| 160 | EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken( |
| 161 | old_process_id, old_document_token)); |
| 162 | } else if (old_weak_document_ptr.AsRenderFrameHostIfValid()) { |
| 163 | // Otherwise, if the old RenderFrameHost is still around, it should still |
| 164 | // map to the same RenderFrameHost. |
| 165 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 166 | old_process_id, old_document_token)); |
| 167 | } |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 168 | EXPECT_EQ(new_render_frame_host, |
| 169 | RenderFrameHostImpl::FromDocumentToken( |
| 170 | new_render_frame_host->GetProcess()->GetDeprecatedID(), |
| 171 | new_document_token)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 172 | return new_document_token; |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 173 | } |
| 174 | }; |
| 175 | |
| 176 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, MainFrameBasic) { |
| 177 | std::vector<blink::DocumentToken> seen_tokens; |
| 178 | |
| 179 | ASSERT_TRUE(NavigateToURL( |
| 180 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 181 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 182 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 183 | |
| 184 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 185 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 186 | |
| 187 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 188 | web_contents(), embedded_test_server()->GetURL("b.com", "/title1.html"))); |
| 189 | |
| 190 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 191 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 192 | } |
| 193 | |
| 194 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, SubFrameBasic) { |
| 195 | std::vector<blink::DocumentToken> seen_tokens; |
| 196 | |
| 197 | ASSERT_TRUE(NavigateToURL( |
| 198 | web_contents(), embedded_test_server()->GetURL( |
| 199 | "a.com", "/cross_site_iframe_factory.html?a(a)"))); |
| 200 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 201 | EXPECT_TRUE(VerifyMatchingTokens(ChildFrameAt(web_contents(), 0))); |
| 202 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 203 | seen_tokens.push_back(GetBrowserSideToken(ChildFrameAt(web_contents(), 0))); |
| 204 | |
| 205 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 206 | ChildFrameAt(web_contents(), 0), |
| 207 | embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 208 | // Main document did not navigate so the token should be the same. |
| 209 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 210 | |
| 211 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 212 | ChildFrameAt(web_contents(), 0), |
| 213 | embedded_test_server()->GetURL("b.com", "/title1.html"))); |
| 214 | // Main document did not navigate so the token should be the same. |
| 215 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 216 | |
| 217 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 218 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 219 | } |
| 220 | |
| 221 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, NewWindowBasic) { |
| 222 | std::vector<blink::DocumentToken> seen_tokens; |
| 223 | |
| 224 | ASSERT_TRUE(NavigateToURL( |
| 225 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 226 | EXPECT_EQ(1u, Shell::windows().size()); |
| 227 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 228 | |
| 229 | WebContents* new_contents = nullptr; |
| 230 | { |
| 231 | // This block is largely derived from `NavigateAndGetNewToken()`. This test |
| 232 | // cannot easily reuse that helper because: |
| 233 | // |
| 234 | // - it is important to specify an actual target URL other than about:blank |
| 235 | // for `window.open()`. Specifying no target URL and then later navigating |
| 236 | // the window has subtly different behavior (e.g. the |
| 237 | // `NewWindowSyncCommit` test below). |
| 238 | // - the helper expects the `WebContents` to already exist in order to |
| 239 | // install |
| 240 | // `TestNavigationManager`. However, in this test, a new `WebContents` is |
| 241 | // created in the process of running the test. |
| 242 | ExecuteScriptAsync(web_contents(), JsReplace("window.open($1)", |
| 243 | embedded_test_server()->GetURL( |
| 244 | "a.com", "/title1.html"))); |
| 245 | ShellAddedObserver wait_for_new_shell; |
| 246 | new_contents = wait_for_new_shell.GetShell()->web_contents(); |
| 247 | DCHECK_EQ(2u, Shell::windows().size()); |
| 248 | DCHECK_EQ(new_contents, Shell::windows()[1]->web_contents()); |
| 249 | DCHECK_NE(new_contents, web_contents()); |
| 250 | seen_tokens.push_back(GetBrowserSideToken(new_contents)); |
| 251 | TestNavigationManager nav_manager( |
| 252 | new_contents, embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 253 | |
| 254 | // Capture the FrameTreeNode now; when a navigation commits, the current |
| 255 | // RenderFrameHost may change. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 256 | RenderFrameHostImpl* const old_render_frame_host = |
| 257 | static_cast<RenderFrameHostImpl*>(new_contents->GetPrimaryMainFrame()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 258 | FrameTreeNode* const frame_tree_node = |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 259 | old_render_frame_host->frame_tree_node(); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 260 | const int old_process_id = |
| 261 | old_render_frame_host->GetProcess()->GetDeprecatedID(); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 262 | const blink::LocalFrameToken old_frame_token = |
| 263 | old_render_frame_host->GetFrameToken(); |
| 264 | const blink::DocumentToken old_document_token = |
| 265 | GetBrowserSideToken(new_contents); |
| 266 | const WeakDocumentPtr old_weak_document_ptr = |
| 267 | old_render_frame_host->GetWeakDocumentPtr(); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 268 | |
| 269 | EXPECT_TRUE(VerifyMatchingTokens(new_contents)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 270 | EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents)); |
| 271 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 272 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 273 | // Even after creating a new window, the original `WebContents` should still |
| 274 | // have the same `DocumentToken`. |
| 275 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 276 | |
| 277 | // Just before the request is actually issued, the navigation is still |
| 278 | // ongoing, so `DocumentToken` should not be updated yet. |
| 279 | EXPECT_TRUE(nav_manager.WaitForRequestStart()); |
| 280 | EXPECT_TRUE(VerifyMatchingTokens(new_contents)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 281 | EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents)); |
| 282 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 283 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 284 | // The original `WebContents` should still have the same `DocumentToken`. |
| 285 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 286 | |
| 287 | // Just before reading the response, the navigation is still ongoing, so |
| 288 | // `DocumentToken` should not be updated yet. |
| 289 | EXPECT_TRUE(nav_manager.WaitForResponse()); |
| 290 | EXPECT_TRUE(VerifyMatchingTokens(new_contents)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 291 | EXPECT_EQ(old_document_token, GetBrowserSideToken(new_contents)); |
| 292 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 293 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 294 | // The original `WebContents` should still have the same `DocumentToken`. |
| 295 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 296 | |
| 297 | // Once a cross-document navigation completes, the document token should be |
| 298 | // updated though. |
Fergal Daly | 105305ad | 2023-01-16 08:21:52 | [diff] [blame] | 299 | ASSERT_TRUE(nav_manager.WaitForNavigationFinished()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 300 | // The RenderFrameHost may have changed; use the FrameTreeNode captured |
| 301 | // above instead. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 302 | RenderFrameHostImpl* const new_render_frame_host = |
| 303 | frame_tree_node->current_frame_host(); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 304 | EXPECT_EQ(embedded_test_server()->GetURL("a.com", "/title1.html"), |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 305 | new_render_frame_host->GetLastCommittedURL()); |
| 306 | EXPECT_TRUE(VerifyMatchingTokens(new_render_frame_host)); |
| 307 | const blink::LocalFrameToken new_frame_token = |
| 308 | new_render_frame_host->GetFrameToken(); |
| 309 | const blink::DocumentToken new_document_token = |
| 310 | GetBrowserSideToken(new_render_frame_host); |
| 311 | EXPECT_NE(new_document_token, old_document_token); |
| 312 | if (new_frame_token == old_frame_token) { |
| 313 | // If the RenderFrameHost is reused, it should no longer be possible to |
| 314 | // use the old token to look up the RenderFrameHost. |
| 315 | EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken( |
| 316 | old_process_id, old_document_token)); |
| 317 | } else if (old_weak_document_ptr.AsRenderFrameHostIfValid()) { |
| 318 | // Otherwise, if the old RenderFrameHost is still around, it should still |
| 319 | // map to the same RenderFrameHost. |
| 320 | EXPECT_EQ(old_render_frame_host, RenderFrameHostImpl::FromDocumentToken( |
| 321 | old_process_id, old_document_token)); |
| 322 | } |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 323 | EXPECT_EQ(new_render_frame_host, |
| 324 | RenderFrameHostImpl::FromDocumentToken( |
| 325 | new_render_frame_host->GetProcess()->GetDeprecatedID(), |
| 326 | new_document_token)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 327 | seen_tokens.push_back(new_document_token); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 328 | // The original `WebContents` should still have the same `DocumentToken`. |
| 329 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 330 | } |
| 331 | |
| 332 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 333 | new_contents, embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 334 | // The original `WebContents` should still have the same `DocumentToken`. |
| 335 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 336 | |
| 337 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 338 | new_contents, embedded_test_server()->GetURL("b.com", "/title1.html"))); |
| 339 | // The original `WebContents` should still have the same `DocumentToken`. |
| 340 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 341 | |
| 342 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 343 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 344 | } |
| 345 | |
| 346 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, SubFrameSyncCommit) { |
| 347 | std::vector<blink::DocumentToken> seen_tokens; |
| 348 | |
| 349 | // This is a basic test that the synchronous commit of about:blank reuses the |
| 350 | // same DocumentToken. See https://crbug.com/778318 for more details. |
| 351 | ASSERT_TRUE(NavigateToURL( |
| 352 | web_contents(), |
| 353 | embedded_test_server()->GetURL("a.com", "/page_with_blank_iframe.html"))); |
| 354 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 355 | EXPECT_TRUE(VerifyMatchingTokens(ChildFrameAt(web_contents(), 0))); |
| 356 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 357 | seen_tokens.push_back(GetBrowserSideToken(ChildFrameAt(web_contents(), 0))); |
| 358 | |
| 359 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 360 | ChildFrameAt(web_contents(), 0), |
| 361 | embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 362 | // Main document did not navigate so the token should be the same. |
| 363 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 364 | |
| 365 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 366 | ChildFrameAt(web_contents(), 0), |
| 367 | embedded_test_server()->GetURL("b.com", "/title1.html"))); |
| 368 | // Main document did not navigate so the token should be the same. |
| 369 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 370 | |
| 371 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 372 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 373 | } |
| 374 | |
| 375 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, NewWindowSyncCommit) { |
| 376 | std::vector<blink::DocumentToken> seen_tokens; |
| 377 | |
| 378 | ASSERT_TRUE(NavigateToURL(web_contents(), GURL("about:blank"))); |
| 379 | EXPECT_EQ(1u, Shell::windows().size()); |
| 380 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 381 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 382 | |
| 383 | // This is a basic test that the synchronous commit of about:blank reuses the |
| 384 | // same DocumentToken. See https://crbug.com/778318 for more details. |
| 385 | ASSERT_TRUE(ExecJs(web_contents(), "window.open()")); |
| 386 | ASSERT_EQ(2u, Shell::windows().size()); |
| 387 | WebContents* new_contents = Shell::windows()[1]->web_contents(); |
| 388 | DCHECK_NE(new_contents, web_contents()); |
| 389 | EXPECT_TRUE(VerifyMatchingTokens(new_contents)); |
| 390 | // The original `WebContents` should still have the same `DocumentToken`. |
| 391 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 392 | |
| 393 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 394 | new_contents, embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 395 | // The original `WebContents` should still have the same `DocumentToken`. |
| 396 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 397 | |
| 398 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 399 | new_contents, embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 400 | // The original `WebContents` should still have the same `DocumentToken`. |
| 401 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 402 | |
| 403 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 404 | new_contents, embedded_test_server()->GetURL("b.com", "/title1.html"))); |
| 405 | // The original `WebContents` should still have the same `DocumentToken`. |
| 406 | EXPECT_EQ(seen_tokens[0], GetBrowserSideToken(web_contents())); |
| 407 | |
| 408 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 409 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 410 | } |
| 411 | |
| 412 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, JavascriptURL) { |
| 413 | ASSERT_TRUE(NavigateToURL( |
| 414 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 415 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 416 | const blink::DocumentToken token = GetBrowserSideToken(web_contents()); |
| 417 | |
| 418 | // A javascript: navigation that replaces the document should not change the |
| 419 | // DocumentToken. This does not use the normal Navigate*() helpers since it |
| 420 | // does not commit a normal cross-document navigation. |
| 421 | ASSERT_TRUE(ExecJs(web_contents(), |
| 422 | JsReplace("location = $1", "javascript:'Hello world!'"))); |
| 423 | EXPECT_EQ("Hello world!", EvalJs(web_contents(), "document.body.innerText")); |
| 424 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 425 | EXPECT_EQ(token, GetBrowserSideToken(web_contents())); |
| 426 | } |
| 427 | |
| 428 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, FailedNavigation) { |
| 429 | std::vector<blink::DocumentToken> seen_tokens; |
| 430 | |
| 431 | ASSERT_TRUE(NavigateToURL( |
| 432 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 433 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 434 | seen_tokens.push_back(GetBrowserSideToken(web_contents())); |
| 435 | |
| 436 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 437 | web_contents(), embedded_test_server()->GetURL("a.com", "/close-socket"), |
| 438 | ExpectedResponse::kNo)); |
| 439 | |
| 440 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 441 | web_contents(), embedded_test_server()->GetURL("a.com", "/close-socket"), |
| 442 | ExpectedResponse::kNo)); |
| 443 | |
| 444 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 445 | web_contents(), embedded_test_server()->GetURL("b.com", "/close-socket"), |
| 446 | ExpectedResponse::kNo)); |
| 447 | |
| 448 | // Test that a regular successful navigation still updates the document token. |
| 449 | seen_tokens.push_back(NavigateAndGetNewToken( |
| 450 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 451 | |
| 452 | std::set unique_tokens(seen_tokens.begin(), seen_tokens.end()); |
| 453 | EXPECT_EQ(unique_tokens.size(), seen_tokens.size()); |
| 454 | } |
| 455 | |
| 456 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, CrashThenReload) { |
| 457 | ASSERT_TRUE(NavigateToURL( |
| 458 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 459 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 460 | const int old_process_id = |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 461 | web_contents()->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID(); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 462 | const blink::DocumentToken old_document_token = |
| 463 | GetBrowserSideToken(web_contents()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 464 | |
| 465 | // Cause the renderer to crash. |
| 466 | RenderProcessHostWatcher crash_observer( |
| 467 | web_contents(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 468 | EXPECT_FALSE(NavigateToURL(shell(), GURL(blink::kChromeUICrashURL))); |
| 469 | // Wait for browser to notice the renderer crash. |
| 470 | crash_observer.Wait(); |
| 471 | |
| 472 | // After a crash, the DocumentToken should still be the same even though the |
| 473 | // renderer process is gone.. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 474 | EXPECT_EQ(old_document_token, GetBrowserSideToken(web_contents())); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 475 | |
| 476 | // But when a live RenderFrame is needed again, RenderDocument should force a |
| 477 | // new RenderFrameHost, and thus, a new DocumentToken. The remainder of this |
| 478 | // test does not use `NavigateAndGetNewToken()`, which tries to use a |
| 479 | // renderer-initiated navigation (which is not possible when the renderer is |
| 480 | // not live). |
| 481 | TestNavigationManager nav_manager( |
| 482 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 483 | shell()->LoadURL(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 484 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 485 | const int new_process_id = |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 486 | web_contents()->GetPrimaryMainFrame()->GetProcess()->GetDeprecatedID(); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 487 | const blink::DocumentToken token_after_navigation_started = |
| 488 | GetBrowserSideToken(web_contents()); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 489 | EXPECT_NE(token_after_navigation_started, old_document_token); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 490 | const WeakDocumentPtr document_weak_ptr = |
| 491 | web_contents()->GetPrimaryMainFrame()->GetWeakDocumentPtr(); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 492 | EXPECT_EQ(web_contents()->GetPrimaryMainFrame(), |
| 493 | RenderFrameHostImpl::FromDocumentToken( |
| 494 | new_process_id, token_after_navigation_started)); |
| 495 | // The old RenderFrameHost should be gone at this point, so a document token |
| 496 | // lookup should fail. |
| 497 | EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken( |
| 498 | old_process_id, old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 499 | |
Daniel Cheng | 273a2c1 | 2022-09-28 02:28:22 | [diff] [blame] | 500 | // After the navigation finishes, the RenderFrameHost will still use the same |
| 501 | // DocumentToken, since no new DocumentAssociatedData was created. The latter |
| 502 | // is indirectly tested by checking if the WeakDocumentPtr is still valid |
| 503 | // after the navigation commits. |
Fergal Daly | 105305ad | 2023-01-16 08:21:52 | [diff] [blame] | 504 | ASSERT_TRUE(nav_manager.WaitForNavigationFinished()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 505 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 506 | const blink::DocumentToken token_after_navigation_finished = |
| 507 | GetBrowserSideToken(web_contents()); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 508 | EXPECT_NE(token_after_navigation_finished, old_document_token); |
Daniel Cheng | 273a2c1 | 2022-09-28 02:28:22 | [diff] [blame] | 509 | EXPECT_EQ(token_after_navigation_finished, token_after_navigation_started); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 510 | EXPECT_NE(document_weak_ptr.AsRenderFrameHostIfValid(), nullptr); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 511 | EXPECT_EQ(web_contents()->GetPrimaryMainFrame(), |
| 512 | RenderFrameHostImpl::FromDocumentToken( |
| 513 | new_process_id, token_after_navigation_started)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, |
| 517 | CrashThenImmediateReinitialize) { |
| 518 | ASSERT_TRUE(NavigateToURL( |
| 519 | web_contents(), embedded_test_server()->GetURL("a.com", "/title1.html"))); |
| 520 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 521 | RenderFrameHostImpl* main_frame = web_contents()->GetPrimaryMainFrame(); |
| 522 | const blink::LocalFrameToken frame_token = main_frame->GetFrameToken(); |
| 523 | const blink::DocumentToken old_document_token = |
| 524 | GetBrowserSideToken(main_frame); |
| 525 | const WeakDocumentPtr document_weak_ptr = main_frame->GetWeakDocumentPtr(); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 526 | |
| 527 | // Cause the renderer to crash. |
| 528 | RenderProcessHostWatcher crash_observer( |
| 529 | web_contents(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 530 | EXPECT_FALSE(NavigateToURL(shell(), GURL(blink::kChromeUICrashURL))); |
| 531 | // Wait for browser to notice the renderer crash. |
| 532 | crash_observer.Wait(); |
| 533 | |
| 534 | // If the main render frame is re-initialized, it also gets a new |
| 535 | // DocumentAssociatedData. Validate that the new DocumentAssociatedData is |
| 536 | // created before the renderer is re-created; a typical failure in this path |
| 537 | // will manifest as a mismatch between the browser and renderer-side document |
| 538 | // tokens. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 539 | main_frame->frame_tree_node() |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 540 | ->render_manager() |
| 541 | ->InitializeMainRenderFrameForImmediateUse(); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 542 | // The RenderFrameHost should be reused. |
| 543 | ASSERT_EQ(frame_token, |
| 544 | web_contents()->GetPrimaryMainFrame()->GetFrameToken()); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 545 | EXPECT_TRUE(VerifyMatchingTokens(web_contents())); |
| 546 | // The re-created RenderFrame should have a distinct document token. |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 547 | const blink::DocumentToken new_document_token = |
| 548 | GetBrowserSideToken(web_contents()); |
| 549 | EXPECT_NE(new_document_token, old_document_token); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 550 | // The previous DocumentWeakPtr should be invalidated since the |
| 551 | // DocumentAssociatedData was re-created. |
| 552 | EXPECT_FALSE(document_weak_ptr.AsRenderFrameHostIfValid()); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 553 | // Even though the RenderFrameHost did not change, only a lookup using the new |
| 554 | // DocumentToken should succeed. |
| 555 | EXPECT_EQ(web_contents()->GetPrimaryMainFrame(), |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 556 | RenderFrameHostImpl::FromDocumentToken(web_contents() |
| 557 | ->GetPrimaryMainFrame() |
| 558 | ->GetProcess() |
| 559 | ->GetDeprecatedID(), |
| 560 | new_document_token)); |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 561 | EXPECT_EQ(nullptr, |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 562 | RenderFrameHostImpl::FromDocumentToken(web_contents() |
| 563 | ->GetPrimaryMainFrame() |
| 564 | ->GetProcess() |
| 565 | ->GetDeprecatedID(), |
| 566 | old_document_token)); |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 567 | } |
| 568 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 569 | // TODO(crbug.com/40238502): Add tests for bfcache navigations and |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 570 | // prerender activations. |
| 571 | |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 572 | IN_PROC_BROWSER_TEST_F(DocumentTokenBrowserTest, MismatchedProcessID) { |
| 573 | RenderFrameHostImpl* main_frame = web_contents()->GetPrimaryMainFrame(); |
| 574 | bool called = false; |
| 575 | mojo::ReportBadMessageCallback callback = |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 576 | base::BindLambdaForTesting([&called](std::string_view reason) { |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 577 | called = true; |
| 578 | EXPECT_EQ("process ID does not match requested DocumentToken", reason); |
| 579 | }); |
| 580 | EXPECT_EQ(nullptr, RenderFrameHostImpl::FromDocumentToken( |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 581 | main_frame->GetProcess()->GetDeprecatedID() + 1, |
Daniel Cheng | 942152d | 2022-10-05 23:22:04 | [diff] [blame] | 582 | main_frame->GetDocumentToken(), &callback)); |
| 583 | EXPECT_TRUE(called); |
| 584 | } |
| 585 | |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 586 | } // namespace |
| 587 | |
| 588 | } // namespace content |