OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "content/common/frame_messages.h" |
| 14 #include "content/public/browser/content_browser_client.h" |
| 15 #include "content/public/common/browser_side_navigation_policy.h" |
13 #include "content/public/common/content_constants.h" | 16 #include "content/public/common/content_constants.h" |
14 #include "content/public/test/mock_render_process_host.h" | 17 #include "content/public/test/mock_render_process_host.h" |
15 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| 19 #include "content/test/test_render_frame_host.h" |
16 #include "content/test/test_render_view_host.h" | 20 #include "content/test/test_render_view_host.h" |
| 21 #include "content/test/test_web_contents.h" |
| 22 #include "third_party/WebKit/public/web/WebSandboxFlags.h" |
17 | 23 |
18 namespace content { | 24 namespace content { |
19 | 25 |
20 class RenderProcessHostUnitTest : public RenderViewHostTestHarness {}; | 26 class RenderProcessHostUnitTest : public RenderViewHostImplTestHarness {}; |
21 | 27 |
22 // Tests that guest RenderProcessHosts are not considered suitable hosts when | 28 // Tests that guest RenderProcessHosts are not considered suitable hosts when |
23 // searching for RenderProcessHost. | 29 // searching for RenderProcessHost. |
24 TEST_F(RenderProcessHostUnitTest, GuestsAreNotSuitableHosts) { | 30 TEST_F(RenderProcessHostUnitTest, GuestsAreNotSuitableHosts) { |
25 GURL test_url("http://foo.com"); | 31 GURL test_url("http://foo.com"); |
26 | 32 |
27 MockRenderProcessHost guest_host(browser_context()); | 33 MockRenderProcessHost guest_host(browser_context()); |
28 guest_host.set_is_for_guests_only(true); | 34 guest_host.set_is_for_guests_only(true); |
29 | 35 |
30 EXPECT_FALSE(RenderProcessHostImpl::IsSuitableHost( | 36 EXPECT_FALSE(RenderProcessHostImpl::IsSuitableHost( |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 hosts.push_back(base::MakeUnique<MockRenderProcessHost>(browser_context())); | 83 hosts.push_back(base::MakeUnique<MockRenderProcessHost>(browser_context())); |
78 } | 84 } |
79 | 85 |
80 // Verify that the renderer sharing still won't happen. | 86 // Verify that the renderer sharing still won't happen. |
81 GURL test_url("http://foo.com"); | 87 GURL test_url("http://foo.com"); |
82 EXPECT_FALSE(RenderProcessHostImpl::ShouldTryToUseExistingProcessHost( | 88 EXPECT_FALSE(RenderProcessHostImpl::ShouldTryToUseExistingProcessHost( |
83 browser_context(), test_url)); | 89 browser_context(), test_url)); |
84 } | 90 } |
85 #endif | 91 #endif |
86 | 92 |
| 93 // Tests that RenderProcessHost reuse considers committed sites correctly. |
| 94 TEST_F(RenderProcessHostUnitTest, ReuseCommittedSite) { |
| 95 const GURL kUrl1("http://foo.com"); |
| 96 const GURL kUrl2("http://bar.com"); |
| 97 |
| 98 // At first, trying to get a RenderProcessHost with the |
| 99 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 100 scoped_refptr<SiteInstanceImpl> site_instance = |
| 101 SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 102 site_instance->set_process_reuse_policy( |
| 103 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 104 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 105 |
| 106 // Have the main frame navigate to the first url. Getting a RenderProcessHost |
| 107 // with the REUSE_PENDING_OR_COMMITTED_SITE policy should now return the |
| 108 // process of the main RFH. |
| 109 NavigateAndCommit(kUrl1); |
| 110 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 111 site_instance->set_process_reuse_policy( |
| 112 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 113 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 114 |
| 115 // Navigate away. Getting a RenderProcessHost with the |
| 116 // REUSE_PENDING_OR_COMMITTED_SITE policy should again return a new process. |
| 117 NavigateAndCommit(kUrl2); |
| 118 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 119 site_instance->set_process_reuse_policy( |
| 120 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 121 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 122 |
| 123 // Now add a subframe that navigates to kUrl1. Getting a RenderProcessHost |
| 124 // with the REUSE_PENDING_OR_COMMITTED_SITE policy for kUrl1 should now |
| 125 // return the process of the subframe RFH. |
| 126 std::string unique_name("uniqueName0"); |
| 127 main_test_rfh()->OnCreateChildFrame( |
| 128 process()->GetNextRoutingID(), blink::WebTreeScopeType::kDocument, |
| 129 std::string(), unique_name, blink::WebSandboxFlags::kNone, |
| 130 ParsedFeaturePolicyHeader(), FrameOwnerProperties()); |
| 131 TestRenderFrameHost* subframe = static_cast<TestRenderFrameHost*>( |
| 132 contents()->GetFrameTree()->root()->child_at(0)->current_frame_host()); |
| 133 { |
| 134 FrameHostMsg_DidCommitProvisionalLoad_Params params; |
| 135 params.nav_entry_id = 0; |
| 136 params.frame_unique_name = unique_name; |
| 137 params.did_create_new_entry = false; |
| 138 params.url = kUrl1; |
| 139 params.transition = ui::PAGE_TRANSITION_AUTO_SUBFRAME; |
| 140 params.should_update_history = false; |
| 141 params.gesture = NavigationGestureUser; |
| 142 params.method = "GET"; |
| 143 params.page_state = PageState::CreateFromURL(kUrl1); |
| 144 subframe->SendRendererInitiatedNavigationRequest(kUrl1, false); |
| 145 subframe->PrepareForCommit(); |
| 146 subframe->SendNavigateWithParams(¶ms); |
| 147 } |
| 148 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 149 site_instance->set_process_reuse_policy( |
| 150 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 151 EXPECT_EQ(subframe->GetProcess(), site_instance->GetProcess()); |
| 152 } |
| 153 |
| 154 // Tests that RenderProcessHost will not consider reusing a process that has |
| 155 // committed an error page. |
| 156 TEST_F(RenderProcessHostUnitTest, DoNotReuseError) { |
| 157 const GURL kUrl1("http://foo.com"); |
| 158 const GURL kUrl2("http://bar.com"); |
| 159 |
| 160 // At first, trying to get a RenderProcessHost with the |
| 161 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 162 scoped_refptr<SiteInstanceImpl> site_instance = |
| 163 SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 164 site_instance->set_process_reuse_policy( |
| 165 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 166 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 167 |
| 168 // Have the main frame navigate to the first url. Getting a RenderProcessHost |
| 169 // with the REUSE_PENDING_OR_COMMITTED_SITE policy should now return the |
| 170 // process of the main RFH. |
| 171 NavigateAndCommit(kUrl1); |
| 172 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 173 site_instance->set_process_reuse_policy( |
| 174 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 175 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 176 |
| 177 // Navigate away. Getting a RenderProcessHost with the |
| 178 // REUSE_PENDING_OR_COMMITTED_SITE policy should again return a new process. |
| 179 NavigateAndCommit(kUrl2); |
| 180 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 181 site_instance->set_process_reuse_policy( |
| 182 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 183 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 184 |
| 185 // Navigate back and simulate an error. Getting a RenderProcessHost with the |
| 186 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 187 web_contents()->GetController().GoBack(); |
| 188 TestRenderFrameHost* pending_rfh = contents()->GetPendingMainFrame(); |
| 189 if (!IsBrowserSideNavigationEnabled()) |
| 190 pending_rfh->SimulateNavigationStart(kUrl1); |
| 191 pending_rfh->SimulateNavigationError(kUrl1, net::ERR_TIMED_OUT); |
| 192 pending_rfh->SimulateNavigationErrorPageCommit(); |
| 193 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 194 site_instance->set_process_reuse_policy( |
| 195 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 196 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 197 } |
| 198 |
| 199 // Tests that RenderProcessHost reuse considers navigations correctly. |
| 200 TEST_F(RenderProcessHostUnitTest, ReuseNavigationProcess) { |
| 201 // This is only applicable to PlzNavigate. |
| 202 if (!IsBrowserSideNavigationEnabled()) |
| 203 return; |
| 204 |
| 205 const GURL kUrl1("http://foo.com"); |
| 206 const GURL kUrl2("http://bar.com"); |
| 207 |
| 208 // At first, trying to get a RenderProcessHost with the |
| 209 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 210 scoped_refptr<SiteInstanceImpl> site_instance = |
| 211 SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 212 site_instance->set_process_reuse_policy( |
| 213 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 214 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 215 |
| 216 // Start a navigation. Now Getting RenderProcessHost with the |
| 217 // REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process. |
| 218 main_test_rfh()->SimulateNavigationStart(kUrl1); |
| 219 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl1); |
| 220 site_instance->set_process_reuse_policy( |
| 221 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 222 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 223 |
| 224 // Finish the navigation and start a new cross-site one. Getting |
| 225 // RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy should |
| 226 // return the process of the speculative RenderFrameHost. |
| 227 main_test_rfh()->PrepareForCommit(); |
| 228 main_test_rfh()->SendNavigate(0, true, kUrl1); |
| 229 contents()->GetController().LoadURL(kUrl2, Referrer(), |
| 230 ui::PAGE_TRANSITION_TYPED, std::string()); |
| 231 main_test_rfh()->SendBeforeUnloadACK(true); |
| 232 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl2); |
| 233 site_instance->set_process_reuse_policy( |
| 234 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 235 EXPECT_EQ(contents()->GetPendingMainFrame()->GetProcess(), |
| 236 site_instance->GetProcess()); |
| 237 |
| 238 // Remember the process id and cancel the navigation. Getting |
| 239 // RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy should |
| 240 // no longer return the process of the speculative RenderFrameHost. |
| 241 int speculative_process_host_id = |
| 242 contents()->GetPendingMainFrame()->GetProcess()->GetID(); |
| 243 contents()->GetPendingMainFrame()->SimulateNavigationError(kUrl2, |
| 244 net::ERR_ABORTED); |
| 245 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl2); |
| 246 site_instance->set_process_reuse_policy( |
| 247 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 248 EXPECT_NE(speculative_process_host_id, site_instance->GetProcess()->GetID()); |
| 249 } |
| 250 |
| 251 // Tests that RenderProcessHost reuse considers navigations correctly during |
| 252 // redirects. |
| 253 TEST_F(RenderProcessHostUnitTest, ReuseNavigationProcessRedirects) { |
| 254 // This is only applicable to PlzNavigate. |
| 255 if (!IsBrowserSideNavigationEnabled()) |
| 256 return; |
| 257 |
| 258 const GURL kUrl("http://foo.com"); |
| 259 const GURL kRedirectUrl1("http://foo.com/redirect"); |
| 260 const GURL kRedirectUrl2("http://bar.com"); |
| 261 |
| 262 // At first, trying to get a RenderProcessHost with the |
| 263 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 264 scoped_refptr<SiteInstanceImpl> site_instance = |
| 265 SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 266 site_instance->set_process_reuse_policy( |
| 267 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 268 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 269 |
| 270 // Start a navigation. Now getting RenderProcessHost with the |
| 271 // REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process. |
| 272 main_test_rfh()->SimulateNavigationStart(kUrl); |
| 273 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 274 site_instance->set_process_reuse_policy( |
| 275 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 276 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 277 |
| 278 // Simulate a same-site redirect. Getting RenderProcessHost with the |
| 279 // REUSE_PENDING_OR_COMMITTED_SITE policy should return the current process. |
| 280 main_test_rfh()->SimulateRedirect(kRedirectUrl1); |
| 281 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 282 site_instance->set_process_reuse_policy( |
| 283 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 284 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 285 |
| 286 // Simulate a cross-site redirect. Getting RenderProcessHost with the |
| 287 // REUSE_PENDING_OR_COMMITTED_SITE policy should not return the current |
| 288 // process, whether for the old site or the new site. |
| 289 main_test_rfh()->SimulateRedirect(kRedirectUrl2); |
| 290 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 291 site_instance->set_process_reuse_policy( |
| 292 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 293 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 294 site_instance = |
| 295 SiteInstanceImpl::CreateForURL(browser_context(), kRedirectUrl2); |
| 296 site_instance->set_process_reuse_policy( |
| 297 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 298 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 299 |
| 300 // Once the navigation is ready to commit, Getting RenderProcessHost with the |
| 301 // REUSE_PENDING_OR_COMMITTED_SITE policy should not return the current |
| 302 // process for the final site, but not the initial one. |
| 303 main_test_rfh()->PrepareForCommit(); |
| 304 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 305 site_instance->set_process_reuse_policy( |
| 306 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 307 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 308 site_instance = |
| 309 SiteInstanceImpl::CreateForURL(browser_context(), kRedirectUrl2); |
| 310 site_instance->set_process_reuse_policy( |
| 311 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 312 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 313 } |
| 314 |
| 315 class EffectiveURLContentBrowserClient : public ContentBrowserClient { |
| 316 public: |
| 317 EffectiveURLContentBrowserClient(const GURL& url_to_modify, |
| 318 const GURL& url_to_return) |
| 319 : url_to_modify_(url_to_modify), url_to_return_(url_to_return) {} |
| 320 ~EffectiveURLContentBrowserClient() override {} |
| 321 |
| 322 private: |
| 323 GURL GetEffectiveURL(BrowserContext* browser_context, |
| 324 const GURL& url) override { |
| 325 if (url == url_to_modify_) |
| 326 return url_to_return_; |
| 327 return url; |
| 328 } |
| 329 |
| 330 GURL url_to_modify_; |
| 331 GURL url_to_return_; |
| 332 }; |
| 333 |
| 334 // Tests that RenderProcessHost reuse works correctly even if the site URL of a |
| 335 // URL changes. |
| 336 TEST_F(RenderProcessHostUnitTest, ReuseSiteURLChanges) { |
| 337 const GURL kUrl("http://foo.com"); |
| 338 const GURL kModifiedSiteUrl("custom-scheme://custom"); |
| 339 |
| 340 // At first, trying to get a RenderProcessHost with the |
| 341 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 342 scoped_refptr<SiteInstanceImpl> site_instance = |
| 343 SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 344 site_instance->set_process_reuse_policy( |
| 345 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 346 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 347 |
| 348 // Have the main frame navigate to the first url. Getting a RenderProcessHost |
| 349 // with the REUSE_PENDING_OR_COMMITTED_SITE policy should now return the |
| 350 // process of the main RFH. |
| 351 NavigateAndCommit(kUrl); |
| 352 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 353 site_instance->set_process_reuse_policy( |
| 354 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 355 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 356 |
| 357 // Install the custom ContentBrowserClient. Site URLs are now modified. |
| 358 // Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy |
| 359 // should no longer return the process of the main RFH, as the RFH is |
| 360 // registered with the normal site URL. |
| 361 EffectiveURLContentBrowserClient modified_client(kUrl, kModifiedSiteUrl); |
| 362 ContentBrowserClient* regular_client = |
| 363 SetBrowserClientForTesting(&modified_client); |
| 364 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 365 site_instance->set_process_reuse_policy( |
| 366 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 367 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 368 |
| 369 // Reload. Getting a RenderProcessHost with the |
| 370 // REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the |
| 371 // main RFH, as it is now registered with the modified site URL. |
| 372 contents()->GetController().Reload(ReloadType::NORMAL, false); |
| 373 main_test_rfh()->PrepareForCommit(); |
| 374 main_test_rfh()->SendNavigate(0, true, kUrl); |
| 375 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 376 site_instance->set_process_reuse_policy( |
| 377 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 378 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 379 |
| 380 // Remove the custom ContentBrowserClient. Site URLs are back to normal. |
| 381 // Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy |
| 382 // should no longer return the process of the main RFH, as it is registered |
| 383 // with the modified site URL. |
| 384 SetBrowserClientForTesting(regular_client); |
| 385 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 386 site_instance->set_process_reuse_policy( |
| 387 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 388 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 389 |
| 390 // Reload. Getting a RenderProcessHost with the |
| 391 // REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the |
| 392 // main RFH, as it is now registered with the regular site URL. |
| 393 contents()->GetController().Reload(ReloadType::NORMAL, false); |
| 394 main_test_rfh()->PrepareForCommit(); |
| 395 main_test_rfh()->SendNavigate(0, true, kUrl); |
| 396 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 397 site_instance->set_process_reuse_policy( |
| 398 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 399 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 400 } |
| 401 |
| 402 // Tests that RenderProcessHost reuse works correctly even if the site URL of a |
| 403 // URL we're navigating to changes. |
| 404 TEST_F(RenderProcessHostUnitTest, ReuseExpectedSiteURLChanges) { |
| 405 // This is only applicable to PlzNavigate. |
| 406 if (!IsBrowserSideNavigationEnabled()) |
| 407 return; |
| 408 |
| 409 const GURL kUrl("http://foo.com"); |
| 410 const GURL kModifiedSiteUrl("custom-scheme://custom"); |
| 411 |
| 412 // At first, trying to get a RenderProcessHost with the |
| 413 // REUSE_PENDING_OR_COMMITTED_SITE policy should return a new process. |
| 414 scoped_refptr<SiteInstanceImpl> site_instance = |
| 415 SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 416 site_instance->set_process_reuse_policy( |
| 417 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 418 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 419 |
| 420 // Start a navigation. Getting a RenderProcessHost with the |
| 421 // REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the |
| 422 // main RFH. |
| 423 main_test_rfh()->SimulateNavigationStart(kUrl); |
| 424 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 425 site_instance->set_process_reuse_policy( |
| 426 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 427 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 428 |
| 429 // Install the custom ContentBrowserClient. Site URLs are now modified. |
| 430 // Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy |
| 431 // should no longer return the process of the main RFH, as the RFH is |
| 432 // registered with the normal site URL. |
| 433 EffectiveURLContentBrowserClient modified_client(kUrl, kModifiedSiteUrl); |
| 434 ContentBrowserClient* regular_client = |
| 435 SetBrowserClientForTesting(&modified_client); |
| 436 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 437 site_instance->set_process_reuse_policy( |
| 438 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 439 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 440 |
| 441 // Have the navigation commit. Getting a RenderProcessHost with the |
| 442 // REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the |
| 443 // main RFH, as it was registered with the modified site URL at commit time. |
| 444 main_test_rfh()->PrepareForCommit(); |
| 445 main_test_rfh()->SendNavigate(0, true, kUrl); |
| 446 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 447 site_instance->set_process_reuse_policy( |
| 448 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 449 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 450 |
| 451 // Start a reload. Getting a RenderProcessHost with the |
| 452 // REUSE_PENDING_OR_COMMITTED_SITE policy should return the process of the |
| 453 // main RFH. |
| 454 contents()->GetController().Reload(ReloadType::NORMAL, false); |
| 455 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 456 site_instance->set_process_reuse_policy( |
| 457 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 458 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 459 |
| 460 // Remove the custom ContentBrowserClient. Site URLs are back to normal. |
| 461 // Getting a RenderProcessHost with the REUSE_PENDING_OR_COMMITTED_SITE policy |
| 462 // should no longer return the process of the main RFH, as it is registered |
| 463 // with the modified site URL. |
| 464 SetBrowserClientForTesting(regular_client); |
| 465 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 466 site_instance->set_process_reuse_policy( |
| 467 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 468 EXPECT_NE(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 469 |
| 470 // Finish the reload. Getting a RenderProcessHost with the |
| 471 // REUSE_PENDING_OR_COMMITTED_SITE policy should now return the process of the |
| 472 // main RFH, as it was registered with the regular site URL when it committed. |
| 473 main_test_rfh()->PrepareForCommit(); |
| 474 main_test_rfh()->SendNavigate(0, true, kUrl); |
| 475 site_instance = SiteInstanceImpl::CreateForURL(browser_context(), kUrl); |
| 476 site_instance->set_process_reuse_policy( |
| 477 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE); |
| 478 EXPECT_EQ(main_test_rfh()->GetProcess(), site_instance->GetProcess()); |
| 479 } |
| 480 |
87 } // namespace content | 481 } // namespace content |
OLD | NEW |