| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/site_instance_impl.h" | 5 #include "content/browser/site_instance_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 // Test to ensure no memory leaks for SiteInstance objects. | 150 // Test to ensure no memory leaks for SiteInstance objects. |
| 151 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { | 151 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { |
| 152 // The existence of this object will cause WebContentsImpl to create our | 152 // The existence of this object will cause WebContentsImpl to create our |
| 153 // test one instead of the real one. | 153 // test one instead of the real one. |
| 154 RenderViewHostTestEnabler rvh_test_enabler; | 154 RenderViewHostTestEnabler rvh_test_enabler; |
| 155 const GURL url("test:foo"); | 155 const GURL url("test:foo"); |
| 156 | 156 |
| 157 // Ensure that instances are deleted when their NavigationEntries are gone. | 157 // Ensure that instances are deleted when their NavigationEntries are gone. |
| 158 scoped_refptr<SiteInstanceImpl> instance = SiteInstanceImpl::Create(nullptr); | 158 constexpr int child_process_param_id = 0; |
| 159 scoped_refptr<SiteInstanceImpl> instance = |
| 160 SiteInstanceImpl::Create(nullptr, child_process_param_id); |
| 159 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 161 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 160 | 162 |
| 161 NavigationEntryImpl* e1 = new NavigationEntryImpl( | 163 NavigationEntryImpl* e1 = new NavigationEntryImpl( |
| 162 instance, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, | 164 instance, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, |
| 163 false); | 165 false); |
| 164 | 166 |
| 165 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. | 167 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. |
| 166 e1->set_site_instance(instance); | 168 e1->set_site_instance(instance); |
| 167 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 169 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 168 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 170 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 207 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 206 // contents is now deleted, along with instance and browsing_instance | 208 // contents is now deleted, along with instance and browsing_instance |
| 207 } | 209 } |
| 208 | 210 |
| 209 // Test that NavigationEntries with SiteInstances can be cloned, but that their | 211 // Test that NavigationEntries with SiteInstances can be cloned, but that their |
| 210 // SiteInstances can be changed afterwards. Also tests that the ref counts are | 212 // SiteInstances can be changed afterwards. Also tests that the ref counts are |
| 211 // updated properly after the change. | 213 // updated properly after the change. |
| 212 TEST_F(SiteInstanceTest, CloneNavigationEntry) { | 214 TEST_F(SiteInstanceTest, CloneNavigationEntry) { |
| 213 const GURL url("test:foo"); | 215 const GURL url("test:foo"); |
| 214 | 216 |
| 217 constexpr int child_process_param_id = 0; |
| 215 std::unique_ptr<NavigationEntryImpl> e1 = | 218 std::unique_ptr<NavigationEntryImpl> e1 = |
| 216 base::WrapUnique(new NavigationEntryImpl( | 219 base::WrapUnique(new NavigationEntryImpl( |
| 217 SiteInstanceImpl::Create(nullptr), url, Referrer(), | 220 SiteInstanceImpl::Create(nullptr, child_process_param_id), url, |
| 218 base::string16(), ui::PAGE_TRANSITION_LINK, false)); | 221 Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, false)); |
| 219 | 222 |
| 220 // Clone the entry. | 223 // Clone the entry. |
| 221 std::unique_ptr<NavigationEntryImpl> e2 = e1->Clone(); | 224 std::unique_ptr<NavigationEntryImpl> e2 = e1->Clone(); |
| 222 | 225 |
| 223 // Should be able to change the SiteInstance of the cloned entry. | 226 // Should be able to change the SiteInstance of the cloned entry. |
| 224 e2->set_site_instance(SiteInstanceImpl::Create(nullptr)); | 227 e2->set_site_instance( |
| 228 SiteInstanceImpl::Create(nullptr, child_process_param_id)); |
| 225 | 229 |
| 226 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 230 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 227 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 231 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 228 | 232 |
| 229 // The first SiteInstance and BrowsingInstance should go away after resetting | 233 // The first SiteInstance and BrowsingInstance should go away after resetting |
| 230 // e1, since e2 should no longer be referencing it. | 234 // e1, since e2 should no longer be referencing it. |
| 231 e1.reset(); | 235 e1.reset(); |
| 232 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 236 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 233 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 237 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 234 | 238 |
| 235 // The second SiteInstance should go away after resetting e2. | 239 // The second SiteInstance should go away after resetting e2. |
| 236 e2.reset(); | 240 e2.reset(); |
| 237 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 241 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 238 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 242 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 239 | 243 |
| 240 DrainMessageLoop(); | 244 DrainMessageLoop(); |
| 241 } | 245 } |
| 242 | 246 |
| 243 // Test to ensure GetProcess returns and creates processes correctly. | 247 // Test to ensure GetProcess returns and creates processes correctly. |
| 244 TEST_F(SiteInstanceTest, GetProcess) { | 248 TEST_F(SiteInstanceTest, GetProcess) { |
| 245 // Ensure that GetProcess returns a process. | 249 // Ensure that GetProcess returns a process. |
| 246 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 250 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 247 std::unique_ptr<RenderProcessHost> host1; | 251 std::unique_ptr<RenderProcessHost> host1; |
| 252 constexpr int child_process_param_id = 0; |
| 248 scoped_refptr<SiteInstanceImpl> instance( | 253 scoped_refptr<SiteInstanceImpl> instance( |
| 249 SiteInstanceImpl::Create(browser_context.get())); | 254 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 250 host1.reset(instance->GetProcess()); | 255 host1.reset(instance->GetProcess()); |
| 251 EXPECT_TRUE(host1.get() != nullptr); | 256 EXPECT_TRUE(host1.get() != nullptr); |
| 252 | 257 |
| 253 // Ensure that GetProcess creates a new process. | 258 // Ensure that GetProcess creates a new process. |
| 254 scoped_refptr<SiteInstanceImpl> instance2( | 259 scoped_refptr<SiteInstanceImpl> instance2( |
| 255 SiteInstanceImpl::Create(browser_context.get())); | 260 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 256 std::unique_ptr<RenderProcessHost> host2(instance2->GetProcess()); | 261 std::unique_ptr<RenderProcessHost> host2(instance2->GetProcess()); |
| 257 EXPECT_TRUE(host2.get() != nullptr); | 262 EXPECT_TRUE(host2.get() != nullptr); |
| 258 EXPECT_NE(host1.get(), host2.get()); | 263 EXPECT_NE(host1.get(), host2.get()); |
| 259 | 264 |
| 260 DrainMessageLoop(); | 265 DrainMessageLoop(); |
| 261 } | 266 } |
| 262 | 267 |
| 263 // Test to ensure SetSite and site() work properly. | 268 // Test to ensure SetSite and site() work properly. |
| 264 TEST_F(SiteInstanceTest, SetSite) { | 269 TEST_F(SiteInstanceTest, SetSite) { |
| 265 scoped_refptr<SiteInstanceImpl> instance(SiteInstanceImpl::Create(nullptr)); | 270 constexpr int child_process_param_id = 0; |
| 271 scoped_refptr<SiteInstanceImpl> instance( |
| 272 SiteInstanceImpl::Create(nullptr, child_process_param_id)); |
| 266 EXPECT_FALSE(instance->HasSite()); | 273 EXPECT_FALSE(instance->HasSite()); |
| 267 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 274 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
| 268 | 275 |
| 269 instance->SetSite(GURL("http://www.google.com/index.html")); | 276 instance->SetSite(GURL("http://www.google.com/index.html")); |
| 270 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL()); | 277 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL()); |
| 271 | 278 |
| 272 EXPECT_TRUE(instance->HasSite()); | 279 EXPECT_TRUE(instance->HasSite()); |
| 273 | 280 |
| 274 DrainMessageLoop(); | 281 DrainMessageLoop(); |
| 275 } | 282 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 | 424 |
| 418 DrainMessageLoop(); | 425 DrainMessageLoop(); |
| 419 } | 426 } |
| 420 | 427 |
| 421 // Test to ensure that there is only one SiteInstance per site in a given | 428 // Test to ensure that there is only one SiteInstance per site in a given |
| 422 // BrowsingInstance, when process-per-site is not in use. | 429 // BrowsingInstance, when process-per-site is not in use. |
| 423 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | 430 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { |
| 424 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( | 431 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 425 switches::kProcessPerSite)); | 432 switches::kProcessPerSite)); |
| 426 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 433 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 434 constexpr int child_process_param_id = 0; |
| 427 BrowsingInstance* browsing_instance = | 435 BrowsingInstance* browsing_instance = |
| 428 new BrowsingInstance(browser_context.get()); | 436 new BrowsingInstance(browser_context.get(), child_process_param_id); |
| 429 | 437 |
| 430 const GURL url_a1("http://www.google.com/1.html"); | 438 const GURL url_a1("http://www.google.com/1.html"); |
| 431 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 439 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
| 432 browsing_instance->GetSiteInstanceForURL(url_a1)); | 440 browsing_instance->GetSiteInstanceForURL(url_a1)); |
| 433 EXPECT_TRUE(site_instance_a1.get() != nullptr); | 441 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
| 434 | 442 |
| 435 // A separate site should create a separate SiteInstance. | 443 // A separate site should create a separate SiteInstance. |
| 436 const GURL url_b1("http://www.yahoo.com/"); | 444 const GURL url_b1("http://www.yahoo.com/"); |
| 437 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 445 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
| 438 | 446 |
| 439 browsing_instance->GetSiteInstanceForURL(url_b1)); | 447 browsing_instance->GetSiteInstanceForURL(url_b1)); |
| 440 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 448 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
| 441 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); | 449 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); |
| 442 | 450 |
| 443 // Getting the new SiteInstance from the BrowsingInstance and from another | 451 // Getting the new SiteInstance from the BrowsingInstance and from another |
| 444 // SiteInstance in the BrowsingInstance should give the same result. | 452 // SiteInstance in the BrowsingInstance should give the same result. |
| 445 EXPECT_EQ(site_instance_b1.get(), | 453 EXPECT_EQ(site_instance_b1.get(), |
| 446 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 454 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
| 447 | 455 |
| 448 // A second visit to the original site should return the same SiteInstance. | 456 // A second visit to the original site should return the same SiteInstance. |
| 449 const GURL url_a2("http://www.google.com/2.html"); | 457 const GURL url_a2("http://www.google.com/2.html"); |
| 450 EXPECT_EQ(site_instance_a1.get(), | 458 EXPECT_EQ(site_instance_a1.get(), |
| 451 browsing_instance->GetSiteInstanceForURL(url_a2)); | 459 browsing_instance->GetSiteInstanceForURL(url_a2)); |
| 452 EXPECT_EQ(site_instance_a1.get(), | 460 EXPECT_EQ(site_instance_a1.get(), |
| 453 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 461 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
| 454 | 462 |
| 455 // A visit to the original site in a new BrowsingInstance (same or different | 463 // A visit to the original site in a new BrowsingInstance (same or different |
| 456 // browser context) should return a different SiteInstance. | 464 // browser context) should return a different SiteInstance. |
| 457 BrowsingInstance* browsing_instance2 = | 465 BrowsingInstance* browsing_instance2 = |
| 458 new BrowsingInstance(browser_context.get()); | 466 new BrowsingInstance(browser_context.get(), child_process_param_id); |
| 459 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 467 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
| 460 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( | 468 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( |
| 461 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 469 browsing_instance2->GetSiteInstanceForURL(url_a2)); |
| 462 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | 470 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); |
| 463 EXPECT_FALSE( | 471 EXPECT_FALSE( |
| 464 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get())); | 472 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get())); |
| 465 | 473 |
| 466 // The two SiteInstances for http://google.com should not use the same process | 474 // The two SiteInstances for http://google.com should not use the same process |
| 467 // if process-per-site is not enabled. | 475 // if process-per-site is not enabled. |
| 468 std::unique_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); | 476 std::unique_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 489 | 497 |
| 490 DrainMessageLoop(); | 498 DrainMessageLoop(); |
| 491 } | 499 } |
| 492 | 500 |
| 493 // Test to ensure that there is only one RenderProcessHost per site for an | 501 // Test to ensure that there is only one RenderProcessHost per site for an |
| 494 // entire BrowserContext, if process-per-site is in use. | 502 // entire BrowserContext, if process-per-site is in use. |
| 495 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { | 503 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { |
| 496 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 504 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 497 switches::kProcessPerSite); | 505 switches::kProcessPerSite); |
| 498 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 506 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 507 constexpr int child_process_param_id = 0; |
| 499 scoped_refptr<BrowsingInstance> browsing_instance = | 508 scoped_refptr<BrowsingInstance> browsing_instance = |
| 500 new BrowsingInstance(browser_context.get()); | 509 new BrowsingInstance(browser_context.get(), child_process_param_id); |
| 501 | 510 |
| 502 const GURL url_a1("http://www.google.com/1.html"); | 511 const GURL url_a1("http://www.google.com/1.html"); |
| 503 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 512 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
| 504 browsing_instance->GetSiteInstanceForURL(url_a1)); | 513 browsing_instance->GetSiteInstanceForURL(url_a1)); |
| 505 EXPECT_TRUE(site_instance_a1.get() != nullptr); | 514 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
| 506 std::unique_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); | 515 std::unique_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); |
| 507 | 516 |
| 508 // A separate site should create a separate SiteInstance. | 517 // A separate site should create a separate SiteInstance. |
| 509 const GURL url_b1("http://www.yahoo.com/"); | 518 const GURL url_b1("http://www.yahoo.com/"); |
| 510 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 519 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
| 511 browsing_instance->GetSiteInstanceForURL(url_b1)); | 520 browsing_instance->GetSiteInstanceForURL(url_b1)); |
| 512 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 521 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
| 513 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); | 522 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); |
| 514 | 523 |
| 515 // Getting the new SiteInstance from the BrowsingInstance and from another | 524 // Getting the new SiteInstance from the BrowsingInstance and from another |
| 516 // SiteInstance in the BrowsingInstance should give the same result. | 525 // SiteInstance in the BrowsingInstance should give the same result. |
| 517 EXPECT_EQ(site_instance_b1.get(), | 526 EXPECT_EQ(site_instance_b1.get(), |
| 518 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 527 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
| 519 | 528 |
| 520 // A second visit to the original site should return the same SiteInstance. | 529 // A second visit to the original site should return the same SiteInstance. |
| 521 const GURL url_a2("http://www.google.com/2.html"); | 530 const GURL url_a2("http://www.google.com/2.html"); |
| 522 EXPECT_EQ(site_instance_a1.get(), | 531 EXPECT_EQ(site_instance_a1.get(), |
| 523 browsing_instance->GetSiteInstanceForURL(url_a2)); | 532 browsing_instance->GetSiteInstanceForURL(url_a2)); |
| 524 EXPECT_EQ(site_instance_a1.get(), | 533 EXPECT_EQ(site_instance_a1.get(), |
| 525 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 534 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
| 526 | 535 |
| 527 // A visit to the original site in a new BrowsingInstance (same browser | 536 // A visit to the original site in a new BrowsingInstance (same browser |
| 528 // context) should return a different SiteInstance with the same process. | 537 // context) should return a different SiteInstance with the same process. |
| 529 BrowsingInstance* browsing_instance2 = | 538 BrowsingInstance* browsing_instance2 = |
| 530 new BrowsingInstance(browser_context.get()); | 539 new BrowsingInstance(browser_context.get(), child_process_param_id); |
| 531 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( | 540 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( |
| 532 browsing_instance2->GetSiteInstanceForURL(url_a1)); | 541 browsing_instance2->GetSiteInstanceForURL(url_a1)); |
| 533 EXPECT_TRUE(site_instance_a1.get() != nullptr); | 542 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
| 534 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); | 543 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); |
| 535 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); | 544 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); |
| 536 | 545 |
| 537 // A visit to the original site in a new BrowsingInstance (different browser | 546 // A visit to the original site in a new BrowsingInstance (different browser |
| 538 // context) should return a different SiteInstance with a different process. | 547 // context) should return a different SiteInstance with a different process. |
| 539 std::unique_ptr<TestBrowserContext> browser_context2( | 548 std::unique_ptr<TestBrowserContext> browser_context2( |
| 540 new TestBrowserContext()); | 549 new TestBrowserContext()); |
| 541 BrowsingInstance* browsing_instance3 = | 550 BrowsingInstance* browsing_instance3 = |
| 542 new BrowsingInstance(browser_context2.get()); | 551 new BrowsingInstance(browser_context2.get(), child_process_param_id); |
| 543 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( | 552 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( |
| 544 browsing_instance3->GetSiteInstanceForURL(url_a2)); | 553 browsing_instance3->GetSiteInstanceForURL(url_a2)); |
| 545 EXPECT_TRUE(site_instance_a2_3.get() != nullptr); | 554 EXPECT_TRUE(site_instance_a2_3.get() != nullptr); |
| 546 std::unique_ptr<RenderProcessHost> process_a2_3( | 555 std::unique_ptr<RenderProcessHost> process_a2_3( |
| 547 site_instance_a2_3->GetProcess()); | 556 site_instance_a2_3->GetProcess()); |
| 548 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | 557 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); |
| 549 EXPECT_NE(process_a1.get(), process_a2_3.get()); | 558 EXPECT_NE(process_a1.get(), process_a2_3.get()); |
| 550 | 559 |
| 551 // Should be able to see that we do have SiteInstances. | 560 // Should be able to see that we do have SiteInstances. |
| 552 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 561 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 566 | 575 |
| 567 // browsing_instances will be deleted when their SiteInstances are deleted. | 576 // browsing_instances will be deleted when their SiteInstances are deleted. |
| 568 // The processes will be unregistered when the RPH scoped_ptrs go away. | 577 // The processes will be unregistered when the RPH scoped_ptrs go away. |
| 569 | 578 |
| 570 DrainMessageLoop(); | 579 DrainMessageLoop(); |
| 571 } | 580 } |
| 572 | 581 |
| 573 static scoped_refptr<SiteInstanceImpl> CreateSiteInstance( | 582 static scoped_refptr<SiteInstanceImpl> CreateSiteInstance( |
| 574 BrowserContext* browser_context, | 583 BrowserContext* browser_context, |
| 575 const GURL& url) { | 584 const GURL& url) { |
| 576 return SiteInstanceImpl::CreateForURL(browser_context, url); | 585 constexpr int child_process_param_id = 0; |
| 586 return SiteInstanceImpl::CreateForURL(browser_context, url, |
| 587 child_process_param_id); |
| 577 } | 588 } |
| 578 | 589 |
| 579 // Test to ensure that pages that require certain privileges are grouped | 590 // Test to ensure that pages that require certain privileges are grouped |
| 580 // in processes with similar pages. | 591 // in processes with similar pages. |
| 581 TEST_F(SiteInstanceTest, ProcessSharingByType) { | 592 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
| 582 // This test shouldn't run with --site-per-process mode, which prohibits | 593 // This test shouldn't run with --site-per-process mode, which prohibits |
| 583 // the renderer process reuse this test explicitly exercises. | 594 // the renderer process reuse this test explicitly exercises. |
| 584 if (AreAllSitesIsolatedForTesting()) | 595 if (AreAllSitesIsolatedForTesting()) |
| 585 return; | 596 return; |
| 586 | 597 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 | 649 |
| 639 // Disable the process limit override. | 650 // Disable the process limit override. |
| 640 RenderProcessHost::SetMaxRendererProcessCount(0u); | 651 RenderProcessHost::SetMaxRendererProcessCount(0u); |
| 641 } | 652 } |
| 642 | 653 |
| 643 // Test to ensure that HasWrongProcessForURL behaves properly for different | 654 // Test to ensure that HasWrongProcessForURL behaves properly for different |
| 644 // types of URLs. | 655 // types of URLs. |
| 645 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { | 656 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { |
| 646 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 657 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 647 std::unique_ptr<RenderProcessHost> host; | 658 std::unique_ptr<RenderProcessHost> host; |
| 659 constexpr int child_process_param_id = 0; |
| 648 scoped_refptr<SiteInstanceImpl> instance( | 660 scoped_refptr<SiteInstanceImpl> instance( |
| 649 SiteInstanceImpl::Create(browser_context.get())); | 661 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 650 | 662 |
| 651 EXPECT_FALSE(instance->HasSite()); | 663 EXPECT_FALSE(instance->HasSite()); |
| 652 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 664 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
| 653 | 665 |
| 654 instance->SetSite(GURL("http://evernote.com/")); | 666 instance->SetSite(GURL("http://evernote.com/")); |
| 655 EXPECT_TRUE(instance->HasSite()); | 667 EXPECT_TRUE(instance->HasSite()); |
| 656 | 668 |
| 657 // Check prior to "assigning" a process to the instance, which is expected | 669 // Check prior to "assigning" a process to the instance, which is expected |
| 658 // to return false due to not being attached to any process yet. | 670 // to return false due to not being attached to any process yet. |
| 659 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 671 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
| 660 | 672 |
| 661 // The call to GetProcess actually creates a new real process, which works | 673 // The call to GetProcess actually creates a new real process, which works |
| 662 // fine, but might be a cause for problems in different contexts. | 674 // fine, but might be a cause for problems in different contexts. |
| 663 host.reset(instance->GetProcess()); | 675 host.reset(instance->GetProcess()); |
| 664 EXPECT_TRUE(host.get() != nullptr); | 676 EXPECT_TRUE(host.get() != nullptr); |
| 665 EXPECT_TRUE(instance->HasProcess()); | 677 EXPECT_TRUE(instance->HasProcess()); |
| 666 | 678 |
| 667 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 679 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
| 668 EXPECT_FALSE(instance->HasWrongProcessForURL( | 680 EXPECT_FALSE(instance->HasWrongProcessForURL( |
| 669 GURL("javascript:alert(document.location.href);"))); | 681 GURL("javascript:alert(document.location.href);"))); |
| 670 | 682 |
| 671 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); | 683 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); |
| 672 | 684 |
| 673 // Test that WebUI SiteInstances reject normal web URLs. | 685 // Test that WebUI SiteInstances reject normal web URLs. |
| 674 const GURL webui_url("chrome://gpu"); | 686 const GURL webui_url("chrome://gpu"); |
| 675 scoped_refptr<SiteInstanceImpl> webui_instance( | 687 scoped_refptr<SiteInstanceImpl> webui_instance( |
| 676 SiteInstanceImpl::Create(browser_context.get())); | 688 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 677 webui_instance->SetSite(webui_url); | 689 webui_instance->SetSite(webui_url); |
| 678 std::unique_ptr<RenderProcessHost> webui_host(webui_instance->GetProcess()); | 690 std::unique_ptr<RenderProcessHost> webui_host(webui_instance->GetProcess()); |
| 679 | 691 |
| 680 // Simulate granting WebUI bindings for the process. | 692 // Simulate granting WebUI bindings for the process. |
| 681 ChildProcessSecurityPolicyImpl::GetInstance()->GrantWebUIBindings( | 693 ChildProcessSecurityPolicyImpl::GetInstance()->GrantWebUIBindings( |
| 682 webui_host->GetID()); | 694 webui_host->GetID()); |
| 683 | 695 |
| 684 EXPECT_TRUE(webui_instance->HasProcess()); | 696 EXPECT_TRUE(webui_instance->HasProcess()); |
| 685 EXPECT_FALSE(webui_instance->HasWrongProcessForURL(webui_url)); | 697 EXPECT_FALSE(webui_instance->HasWrongProcessForURL(webui_url)); |
| 686 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://google.com"))); | 698 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://google.com"))); |
| 687 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://gpu"))); | 699 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://gpu"))); |
| 688 | 700 |
| 689 // WebUI uses process-per-site, so another instance will use the same process | 701 // WebUI uses process-per-site, so another instance will use the same process |
| 690 // even if we haven't called GetProcess yet. Make sure HasWrongProcessForURL | 702 // even if we haven't called GetProcess yet. Make sure HasWrongProcessForURL |
| 691 // doesn't crash (http://crbug.com/137070). | 703 // doesn't crash (http://crbug.com/137070). |
| 692 scoped_refptr<SiteInstanceImpl> webui_instance2( | 704 scoped_refptr<SiteInstanceImpl> webui_instance2( |
| 693 SiteInstanceImpl::Create(browser_context.get())); | 705 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 694 webui_instance2->SetSite(webui_url); | 706 webui_instance2->SetSite(webui_url); |
| 695 EXPECT_FALSE(webui_instance2->HasWrongProcessForURL(webui_url)); | 707 EXPECT_FALSE(webui_instance2->HasWrongProcessForURL(webui_url)); |
| 696 EXPECT_TRUE( | 708 EXPECT_TRUE( |
| 697 webui_instance2->HasWrongProcessForURL(GURL("http://google.com"))); | 709 webui_instance2->HasWrongProcessForURL(GURL("http://google.com"))); |
| 698 | 710 |
| 699 DrainMessageLoop(); | 711 DrainMessageLoop(); |
| 700 } | 712 } |
| 701 | 713 |
| 702 // Test to ensure that HasWrongProcessForURL behaves properly even when | 714 // Test to ensure that HasWrongProcessForURL behaves properly even when |
| 703 // --site-per-process is used (http://crbug.com/160671). | 715 // --site-per-process is used (http://crbug.com/160671). |
| 704 TEST_F(SiteInstanceTest, HasWrongProcessForURLInSitePerProcess) { | 716 TEST_F(SiteInstanceTest, HasWrongProcessForURLInSitePerProcess) { |
| 705 IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess()); | 717 IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess()); |
| 706 | 718 |
| 707 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 719 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 708 std::unique_ptr<RenderProcessHost> host; | 720 std::unique_ptr<RenderProcessHost> host; |
| 721 constexpr int child_process_param_id = 0; |
| 709 scoped_refptr<SiteInstanceImpl> instance( | 722 scoped_refptr<SiteInstanceImpl> instance( |
| 710 SiteInstanceImpl::Create(browser_context.get())); | 723 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 711 | 724 |
| 712 instance->SetSite(GURL("http://evernote.com/")); | 725 instance->SetSite(GURL("http://evernote.com/")); |
| 713 EXPECT_TRUE(instance->HasSite()); | 726 EXPECT_TRUE(instance->HasSite()); |
| 714 | 727 |
| 715 // Check prior to "assigning" a process to the instance, which is expected | 728 // Check prior to "assigning" a process to the instance, which is expected |
| 716 // to return false due to not being attached to any process yet. | 729 // to return false due to not being attached to any process yet. |
| 717 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 730 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
| 718 | 731 |
| 719 // The call to GetProcess actually creates a new real process, which works | 732 // The call to GetProcess actually creates a new real process, which works |
| 720 // fine, but might be a cause for problems in different contexts. | 733 // fine, but might be a cause for problems in different contexts. |
| 721 host.reset(instance->GetProcess()); | 734 host.reset(instance->GetProcess()); |
| 722 EXPECT_TRUE(host.get() != nullptr); | 735 EXPECT_TRUE(host.get() != nullptr); |
| 723 EXPECT_TRUE(instance->HasProcess()); | 736 EXPECT_TRUE(instance->HasProcess()); |
| 724 | 737 |
| 725 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 738 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
| 726 EXPECT_FALSE(instance->HasWrongProcessForURL( | 739 EXPECT_FALSE(instance->HasWrongProcessForURL( |
| 727 GURL("javascript:alert(document.location.href);"))); | 740 GURL("javascript:alert(document.location.href);"))); |
| 728 | 741 |
| 729 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); | 742 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); |
| 730 | 743 |
| 731 DrainMessageLoop(); | 744 DrainMessageLoop(); |
| 732 } | 745 } |
| 733 | 746 |
| 734 // Test that we do not reuse a process in process-per-site mode if it has the | 747 // Test that we do not reuse a process in process-per-site mode if it has the |
| 735 // wrong bindings for its URL. http://crbug.com/174059. | 748 // wrong bindings for its URL. http://crbug.com/174059. |
| 736 TEST_F(SiteInstanceTest, ProcessPerSiteWithWrongBindings) { | 749 TEST_F(SiteInstanceTest, ProcessPerSiteWithWrongBindings) { |
| 737 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 750 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 738 std::unique_ptr<RenderProcessHost> host; | 751 std::unique_ptr<RenderProcessHost> host; |
| 739 std::unique_ptr<RenderProcessHost> host2; | 752 std::unique_ptr<RenderProcessHost> host2; |
| 753 constexpr int child_process_param_id = 0; |
| 740 scoped_refptr<SiteInstanceImpl> instance( | 754 scoped_refptr<SiteInstanceImpl> instance( |
| 741 SiteInstanceImpl::Create(browser_context.get())); | 755 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 742 | 756 |
| 743 EXPECT_FALSE(instance->HasSite()); | 757 EXPECT_FALSE(instance->HasSite()); |
| 744 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 758 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
| 745 | 759 |
| 746 // Simulate navigating to a WebUI URL in a process that does not have WebUI | 760 // Simulate navigating to a WebUI URL in a process that does not have WebUI |
| 747 // bindings. This already requires bypassing security checks. | 761 // bindings. This already requires bypassing security checks. |
| 748 const GURL webui_url("chrome://gpu"); | 762 const GURL webui_url("chrome://gpu"); |
| 749 instance->SetSite(webui_url); | 763 instance->SetSite(webui_url); |
| 750 EXPECT_TRUE(instance->HasSite()); | 764 EXPECT_TRUE(instance->HasSite()); |
| 751 | 765 |
| 752 // The call to GetProcess actually creates a new real process. | 766 // The call to GetProcess actually creates a new real process. |
| 753 host.reset(instance->GetProcess()); | 767 host.reset(instance->GetProcess()); |
| 754 EXPECT_TRUE(host.get() != nullptr); | 768 EXPECT_TRUE(host.get() != nullptr); |
| 755 EXPECT_TRUE(instance->HasProcess()); | 769 EXPECT_TRUE(instance->HasProcess()); |
| 756 | 770 |
| 757 // Without bindings, this should look like the wrong process. | 771 // Without bindings, this should look like the wrong process. |
| 758 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url)); | 772 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url)); |
| 759 | 773 |
| 760 // WebUI uses process-per-site, so another instance would normally use the | 774 // WebUI uses process-per-site, so another instance would normally use the |
| 761 // same process. Make sure it doesn't use the same process if the bindings | 775 // same process. Make sure it doesn't use the same process if the bindings |
| 762 // are missing. | 776 // are missing. |
| 763 scoped_refptr<SiteInstanceImpl> instance2( | 777 scoped_refptr<SiteInstanceImpl> instance2( |
| 764 SiteInstanceImpl::Create(browser_context.get())); | 778 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 765 instance2->SetSite(webui_url); | 779 instance2->SetSite(webui_url); |
| 766 host2.reset(instance2->GetProcess()); | 780 host2.reset(instance2->GetProcess()); |
| 767 EXPECT_TRUE(host2.get() != nullptr); | 781 EXPECT_TRUE(host2.get() != nullptr); |
| 768 EXPECT_TRUE(instance2->HasProcess()); | 782 EXPECT_TRUE(instance2->HasProcess()); |
| 769 EXPECT_NE(host.get(), host2.get()); | 783 EXPECT_NE(host.get(), host2.get()); |
| 770 | 784 |
| 771 DrainMessageLoop(); | 785 DrainMessageLoop(); |
| 772 } | 786 } |
| 773 | 787 |
| 774 // Test that we do not register processes with empty sites for process-per-site | 788 // Test that we do not register processes with empty sites for process-per-site |
| 775 // mode. | 789 // mode. |
| 776 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) { | 790 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) { |
| 777 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 791 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 778 switches::kProcessPerSite); | 792 switches::kProcessPerSite); |
| 779 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 793 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 780 std::unique_ptr<RenderProcessHost> host; | 794 std::unique_ptr<RenderProcessHost> host; |
| 795 constexpr int child_process_param_id = 0; |
| 781 scoped_refptr<SiteInstanceImpl> instance( | 796 scoped_refptr<SiteInstanceImpl> instance( |
| 782 SiteInstanceImpl::Create(browser_context.get())); | 797 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id)); |
| 783 | 798 |
| 784 instance->SetSite(GURL()); | 799 instance->SetSite(GURL()); |
| 785 EXPECT_TRUE(instance->HasSite()); | 800 EXPECT_TRUE(instance->HasSite()); |
| 786 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 801 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
| 787 host.reset(instance->GetProcess()); | 802 host.reset(instance->GetProcess()); |
| 788 | 803 |
| 789 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( | 804 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( |
| 790 browser_context.get(), GURL())); | 805 browser_context.get(), GURL())); |
| 791 | 806 |
| 792 DrainMessageLoop(); | 807 DrainMessageLoop(); |
| 793 } | 808 } |
| 794 | 809 |
| 795 TEST_F(SiteInstanceTest, DefaultSubframeSiteInstance) { | 810 TEST_F(SiteInstanceTest, DefaultSubframeSiteInstance) { |
| 796 if (AreAllSitesIsolatedForTesting()) | 811 if (AreAllSitesIsolatedForTesting()) |
| 797 return; // --top-document-isolation is not possible. | 812 return; // --top-document-isolation is not possible. |
| 798 | 813 |
| 799 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 814 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 800 switches::kTopDocumentIsolation); | 815 switches::kTopDocumentIsolation); |
| 801 | 816 |
| 802 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 817 std::unique_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 818 constexpr int child_process_param_id = 0; |
| 803 scoped_refptr<SiteInstanceImpl> main_instance = | 819 scoped_refptr<SiteInstanceImpl> main_instance = |
| 804 SiteInstanceImpl::Create(browser_context.get()); | 820 SiteInstanceImpl::Create(browser_context.get(), child_process_param_id); |
| 805 scoped_refptr<SiteInstanceImpl> subframe_instance = | 821 scoped_refptr<SiteInstanceImpl> subframe_instance = |
| 806 main_instance->GetDefaultSubframeSiteInstance(); | 822 main_instance->GetDefaultSubframeSiteInstance(); |
| 807 int subframe_instance_id = subframe_instance->GetId(); | 823 int subframe_instance_id = subframe_instance->GetId(); |
| 808 | 824 |
| 809 EXPECT_NE(main_instance, subframe_instance); | 825 EXPECT_NE(main_instance, subframe_instance); |
| 810 EXPECT_EQ(subframe_instance, main_instance->GetDefaultSubframeSiteInstance()); | 826 EXPECT_EQ(subframe_instance, main_instance->GetDefaultSubframeSiteInstance()); |
| 811 EXPECT_FALSE(main_instance->IsDefaultSubframeSiteInstance()); | 827 EXPECT_FALSE(main_instance->IsDefaultSubframeSiteInstance()); |
| 812 EXPECT_TRUE(subframe_instance->IsDefaultSubframeSiteInstance()); | 828 EXPECT_TRUE(subframe_instance->IsDefaultSubframeSiteInstance()); |
| 813 | 829 |
| 814 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 830 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 835 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 851 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 836 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 852 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 837 | 853 |
| 838 // Free the subframe instance, which should free the browsing instance. | 854 // Free the subframe instance, which should free the browsing instance. |
| 839 subframe_instance = nullptr; | 855 subframe_instance = nullptr; |
| 840 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); | 856 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 841 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); | 857 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 842 } | 858 } |
| 843 | 859 |
| 844 } // namespace content | 860 } // namespace content |
| OLD | NEW |