| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
| 8 #include "base/string16.h" | 8 #include "base/string16.h" |
| 9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/browser/browsing_instance.h" | 10 #include "content/browser/browsing_instance.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 old_browser_client_ = SetBrowserClientForTesting(&browser_client_); | 96 old_browser_client_ = SetBrowserClientForTesting(&browser_client_); |
| 97 url_util::AddStandardScheme(kPrivilegedScheme); | 97 url_util::AddStandardScheme(kPrivilegedScheme); |
| 98 url_util::AddStandardScheme(chrome::kChromeUIScheme); | 98 url_util::AddStandardScheme(chrome::kChromeUIScheme); |
| 99 } | 99 } |
| 100 | 100 |
| 101 virtual void TearDown() { | 101 virtual void TearDown() { |
| 102 // Ensure that no RenderProcessHosts are left over after the tests. | 102 // Ensure that no RenderProcessHosts are left over after the tests. |
| 103 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd()); | 103 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd()); |
| 104 | 104 |
| 105 SetBrowserClientForTesting(old_browser_client_); | 105 SetBrowserClientForTesting(old_browser_client_); |
| 106 SiteInstanceImpl::set_render_process_host_factory(NULL); |
| 106 | 107 |
| 107 // http://crbug.com/143565 found SiteInstanceTest leaking an | 108 // http://crbug.com/143565 found SiteInstanceTest leaking an |
| 108 // AppCacheDatabase. This happens because some part of the test indirectly | 109 // AppCacheDatabase. This happens because some part of the test indirectly |
| 109 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts | 110 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts |
| 110 // a task to the IO thread to create the AppCacheDatabase. Since the | 111 // a task to the IO thread to create the AppCacheDatabase. Since the |
| 111 // message loop is not running, the AppCacheDatabase ends up getting | 112 // message loop is not running, the AppCacheDatabase ends up getting |
| 112 // created when DrainMessageLoops() gets called at the end of a test case. | 113 // created when DrainMessageLoops() gets called at the end of a test case. |
| 113 // Immediately after, the test case ends and the AppCacheDatabase gets | 114 // Immediately after, the test case ends and the AppCacheDatabase gets |
| 114 // scheduled for deletion. Here, call DrainMessageLoops() again so the | 115 // scheduled for deletion. Here, call DrainMessageLoops() again so the |
| 115 // AppCacheDatabase actually gets deleted. | 116 // AppCacheDatabase actually gets deleted. |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 GURL("https://www.google.com"))); // not visited before | 537 GURL("https://www.google.com"))); // not visited before |
| 537 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | 538 EXPECT_FALSE(browsing_instance3->HasSiteInstance( |
| 538 GURL("http://www.yahoo.com"))); // different BI, different context | 539 GURL("http://www.yahoo.com"))); // different BI, different context |
| 539 | 540 |
| 540 // browsing_instances will be deleted when their SiteInstances are deleted. | 541 // browsing_instances will be deleted when their SiteInstances are deleted. |
| 541 // The processes will be unregistered when the RPH scoped_ptrs go away. | 542 // The processes will be unregistered when the RPH scoped_ptrs go away. |
| 542 | 543 |
| 543 DrainMessageLoops(); | 544 DrainMessageLoops(); |
| 544 } | 545 } |
| 545 | 546 |
| 546 static SiteInstanceImpl* CreateSiteInstance( | 547 static SiteInstanceImpl* CreateSiteInstance(BrowserContext* browser_context, |
| 547 BrowserContext* browser_context, | 548 const GURL& url) { |
| 548 RenderProcessHostFactory* factory, | 549 return static_cast<SiteInstanceImpl*>( |
| 549 const GURL& url) { | 550 SiteInstance::CreateForURL(browser_context, url)); |
| 550 SiteInstanceImpl* instance = | |
| 551 reinterpret_cast<SiteInstanceImpl*>( | |
| 552 SiteInstance::CreateForURL(browser_context, url)); | |
| 553 instance->set_render_process_host_factory(factory); | |
| 554 return instance; | |
| 555 } | 551 } |
| 556 | 552 |
| 557 // Test to ensure that pages that require certain privileges are grouped | 553 // Test to ensure that pages that require certain privileges are grouped |
| 558 // in processes with similar pages. | 554 // in processes with similar pages. |
| 559 TEST_F(SiteInstanceTest, ProcessSharingByType) { | 555 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
| 560 MockRenderProcessHostFactory rph_factory; | 556 MockRenderProcessHostFactory rph_factory; |
| 557 SiteInstanceImpl::set_render_process_host_factory(&rph_factory); |
| 561 ChildProcessSecurityPolicyImpl* policy = | 558 ChildProcessSecurityPolicyImpl* policy = |
| 562 ChildProcessSecurityPolicyImpl::GetInstance(); | 559 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 563 | 560 |
| 564 // Make a bunch of mock renderers so that we hit the limit. | 561 // Make a bunch of mock renderers so that we hit the limit. |
| 565 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 562 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 566 ScopedVector<MockRenderProcessHost> hosts; | 563 ScopedVector<MockRenderProcessHost> hosts; |
| 567 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) | 564 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) |
| 568 hosts.push_back(new MockRenderProcessHost(browser_context.get())); | 565 hosts.push_back(new MockRenderProcessHost(browser_context.get())); |
| 569 | 566 |
| 570 // Create some extension instances and make sure they share a process. | 567 // Create some extension instances and make sure they share a process. |
| 571 scoped_refptr<SiteInstanceImpl> extension1_instance( | 568 scoped_refptr<SiteInstanceImpl> extension1_instance( |
| 572 CreateSiteInstance(browser_context.get(), &rph_factory, | 569 CreateSiteInstance(browser_context.get(), |
| 573 GURL(kPrivilegedScheme + std::string("://foo/bar")))); | 570 GURL(kPrivilegedScheme + std::string("://foo/bar")))); |
| 574 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); | 571 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); |
| 575 | 572 |
| 576 scoped_refptr<SiteInstanceImpl> extension2_instance( | 573 scoped_refptr<SiteInstanceImpl> extension2_instance( |
| 577 CreateSiteInstance(browser_context.get(), &rph_factory, | 574 CreateSiteInstance(browser_context.get(), |
| 578 GURL(kPrivilegedScheme + std::string("://baz/bar")))); | 575 GURL(kPrivilegedScheme + std::string("://baz/bar")))); |
| 579 | 576 |
| 580 scoped_ptr<RenderProcessHost> extension_host( | 577 scoped_ptr<RenderProcessHost> extension_host( |
| 581 extension1_instance->GetProcess()); | 578 extension1_instance->GetProcess()); |
| 582 EXPECT_EQ(extension1_instance->GetProcess(), | 579 EXPECT_EQ(extension1_instance->GetProcess(), |
| 583 extension2_instance->GetProcess()); | 580 extension2_instance->GetProcess()); |
| 584 | 581 |
| 585 // Create some WebUI instances and make sure they share a process. | 582 // Create some WebUI instances and make sure they share a process. |
| 586 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( | 583 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( |
| 587 browser_context.get(), &rph_factory, | 584 browser_context.get(), |
| 588 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); | 585 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); |
| 589 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); | 586 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); |
| 590 | 587 |
| 591 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( | 588 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( |
| 592 browser_context.get(), &rph_factory, | 589 browser_context.get(), |
| 593 GURL(chrome::kChromeUIScheme + std::string("://history")))); | 590 GURL(chrome::kChromeUIScheme + std::string("://history")))); |
| 594 | 591 |
| 595 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); | 592 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); |
| 596 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); | 593 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); |
| 597 | 594 |
| 598 // Make sure none of differing privilege processes are mixed. | 595 // Make sure none of differing privilege processes are mixed. |
| 599 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); | 596 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); |
| 600 | 597 |
| 601 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) { | 598 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) { |
| 602 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | 599 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 751 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
| 755 host.reset(instance->GetProcess()); | 752 host.reset(instance->GetProcess()); |
| 756 | 753 |
| 757 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( | 754 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( |
| 758 browser_context.get(), GURL())); | 755 browser_context.get(), GURL())); |
| 759 | 756 |
| 760 DrainMessageLoops(); | 757 DrainMessageLoops(); |
| 761 } | 758 } |
| 762 | 759 |
| 763 } // namespace content | 760 } // namespace content |
| OLD | NEW |