| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/string16.h" | 7 #include "base/string16.h" |
| 8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/browsing_instance.h" | 9 #include "content/browser/browsing_instance.h" |
| 10 #include "content/browser/child_process_security_policy.h" | 10 #include "content/browser/child_process_security_policy.h" |
| 11 #include "content/browser/mock_content_browser_client.h" | 11 #include "content/browser/mock_content_browser_client.h" |
| 12 #include "content/browser/renderer_host/browser_render_process_host.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/renderer_host/test_render_view_host.h" | 14 #include "content/browser/renderer_host/test_render_view_host.h" |
| 15 #include "content/browser/site_instance.h" | 15 #include "content/browser/site_instance.h" |
| 16 #include "content/browser/tab_contents/navigation_entry.h" | 16 #include "content/browser/tab_contents/navigation_entry.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" | 17 #include "content/browser/tab_contents/tab_contents.h" |
| 18 #include "content/browser/webui/empty_web_ui_factory.h" | 18 #include "content/browser/webui/empty_web_ui_factory.h" |
| 19 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
| 20 #include "content/public/common/content_constants.h" | 20 #include "content/public/common/content_constants.h" |
| 21 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
| 22 #include "content/test/test_browser_context.h" | 22 #include "content/test/test_browser_context.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context, | 61 virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context, |
| 62 const GURL& effective_url) OVERRIDE { | 62 const GURL& effective_url) OVERRIDE { |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 | 65 |
| 66 virtual bool IsURLSameAsAnySiteInstance(const GURL& url) OVERRIDE { | 66 virtual bool IsURLSameAsAnySiteInstance(const GURL& url) OVERRIDE { |
| 67 return url == GURL(kSameAsAnyInstanceURL) || | 67 return url == GURL(kSameAsAnyInstanceURL) || |
| 68 url == GURL(chrome::kAboutCrashURL); | 68 url == GURL(chrome::kAboutCrashURL); |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual bool IsSuitableHost(RenderProcessHost* process_host, | 71 virtual bool IsSuitableHost(content::RenderProcessHost* process_host, |
| 72 const GURL& site_url) OVERRIDE { | 72 const GURL& site_url) OVERRIDE { |
| 73 return (privileged_process_id_ == process_host->id()) == | 73 return (privileged_process_id_ == process_host->GetID()) == |
| 74 site_url.SchemeIs(kPrivilegedScheme); | 74 site_url.SchemeIs(kPrivilegedScheme); |
| 75 } | 75 } |
| 76 | 76 |
| 77 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context, | 77 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context, |
| 78 const GURL& url) OVERRIDE { | 78 const GURL& url) OVERRIDE { |
| 79 return url; | 79 return url; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) { | 82 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) { |
| 83 old_browser_client_ = old_browser_client; | 83 old_browser_client_ = old_browser_client; |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // Make sure max_page_id_ is monotonically increasing. | 288 // Make sure max_page_id_ is monotonically increasing. |
| 289 instance->UpdateMaxPageID(3); | 289 instance->UpdateMaxPageID(3); |
| 290 instance->UpdateMaxPageID(1); | 290 instance->UpdateMaxPageID(1); |
| 291 EXPECT_EQ(3, instance->max_page_id()); | 291 EXPECT_EQ(3, instance->max_page_id()); |
| 292 } | 292 } |
| 293 | 293 |
| 294 // Test to ensure GetProcess returns and creates processes correctly. | 294 // Test to ensure GetProcess returns and creates processes correctly. |
| 295 TEST_F(SiteInstanceTest, GetProcess) { | 295 TEST_F(SiteInstanceTest, GetProcess) { |
| 296 // Ensure that GetProcess returns a process. | 296 // Ensure that GetProcess returns a process. |
| 297 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 297 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 298 scoped_ptr<RenderProcessHost> host1; | 298 scoped_ptr<content::RenderProcessHost> host1; |
| 299 scoped_refptr<SiteInstance> instance( | 299 scoped_refptr<SiteInstance> instance( |
| 300 SiteInstance::CreateSiteInstance(browser_context.get())); | 300 SiteInstance::CreateSiteInstance(browser_context.get())); |
| 301 host1.reset(instance->GetProcess()); | 301 host1.reset(instance->GetProcess()); |
| 302 EXPECT_TRUE(host1.get() != NULL); | 302 EXPECT_TRUE(host1.get() != NULL); |
| 303 | 303 |
| 304 // Ensure that GetProcess creates a new process. | 304 // Ensure that GetProcess creates a new process. |
| 305 scoped_refptr<SiteInstance> instance2( | 305 scoped_refptr<SiteInstance> instance2( |
| 306 SiteInstance::CreateSiteInstance(browser_context.get())); | 306 SiteInstance::CreateSiteInstance(browser_context.get())); |
| 307 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); | 307 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); |
| 308 EXPECT_TRUE(host2.get() != NULL); | 308 EXPECT_TRUE(host2.get() != NULL); |
| 309 EXPECT_NE(host1.get(), host2.get()); | 309 EXPECT_NE(host1.get(), host2.get()); |
| 310 } | 310 } |
| 311 | 311 |
| 312 // Test to ensure SetSite and site() work properly. | 312 // Test to ensure SetSite and site() work properly. |
| 313 TEST_F(SiteInstanceTest, SetSite) { | 313 TEST_F(SiteInstanceTest, SetSite) { |
| 314 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | 314 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); |
| 315 EXPECT_FALSE(instance->has_site()); | 315 EXPECT_FALSE(instance->has_site()); |
| 316 EXPECT_TRUE(instance->site().is_empty()); | 316 EXPECT_TRUE(instance->site().is_empty()); |
| 317 | 317 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 | 507 |
| 508 // Should be able to see that we don't have SiteInstances. | 508 // Should be able to see that we don't have SiteInstances. |
| 509 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 509 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
| 510 GURL("https://www.google.com"))); // not visited before | 510 GURL("https://www.google.com"))); // not visited before |
| 511 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | 511 EXPECT_FALSE(browsing_instance3->HasSiteInstance( |
| 512 GURL("http://www.yahoo.com"))); // different BI, different context | 512 GURL("http://www.yahoo.com"))); // different BI, different context |
| 513 | 513 |
| 514 // browsing_instances will be deleted when their SiteInstances are deleted | 514 // browsing_instances will be deleted when their SiteInstances are deleted |
| 515 } | 515 } |
| 516 | 516 |
| 517 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory, | 517 static SiteInstance* CreateSiteInstance( |
| 518 const GURL& url) { | 518 content::RenderProcessHostFactory* factory, const GURL& url) { |
| 519 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); | 519 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); |
| 520 instance->set_render_process_host_factory(factory); | 520 instance->set_render_process_host_factory(factory); |
| 521 return instance; | 521 return instance; |
| 522 } | 522 } |
| 523 | 523 |
| 524 // Test to ensure that pages that require certain privileges are grouped | 524 // Test to ensure that pages that require certain privileges are grouped |
| 525 // in processes with similar pages. | 525 // in processes with similar pages. |
| 526 TEST_F(SiteInstanceTest, ProcessSharingByType) { | 526 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
| 527 MockRenderProcessHostFactory rph_factory; | 527 MockRenderProcessHostFactory rph_factory; |
| 528 ChildProcessSecurityPolicy* policy = | 528 ChildProcessSecurityPolicy* policy = |
| 529 ChildProcessSecurityPolicy::GetInstance(); | 529 ChildProcessSecurityPolicy::GetInstance(); |
| 530 | 530 |
| 531 // Make a bunch of mock renderers so that we hit the limit. | 531 // Make a bunch of mock renderers so that we hit the limit. |
| 532 std::vector<MockRenderProcessHost*> hosts; | 532 std::vector<MockRenderProcessHost*> hosts; |
| 533 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) | 533 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) |
| 534 hosts.push_back(new MockRenderProcessHost(NULL)); | 534 hosts.push_back(new MockRenderProcessHost(NULL)); |
| 535 | 535 |
| 536 // Create some extension instances and make sure they share a process. | 536 // Create some extension instances and make sure they share a process. |
| 537 scoped_refptr<SiteInstance> extension1_instance( | 537 scoped_refptr<SiteInstance> extension1_instance( |
| 538 CreateSiteInstance(&rph_factory, | 538 CreateSiteInstance(&rph_factory, |
| 539 GURL(kPrivilegedScheme + std::string("://foo/bar")))); | 539 GURL(kPrivilegedScheme + std::string("://foo/bar")))); |
| 540 SetPrivilegedProcessId(extension1_instance->GetProcess()->id()); | 540 SetPrivilegedProcessId(extension1_instance->GetProcess()->GetID()); |
| 541 | 541 |
| 542 scoped_refptr<SiteInstance> extension2_instance( | 542 scoped_refptr<SiteInstance> extension2_instance( |
| 543 CreateSiteInstance(&rph_factory, | 543 CreateSiteInstance(&rph_factory, |
| 544 GURL(kPrivilegedScheme + std::string("://baz/bar")))); | 544 GURL(kPrivilegedScheme + std::string("://baz/bar")))); |
| 545 | 545 |
| 546 scoped_ptr<RenderProcessHost> extension_host( | 546 scoped_ptr<content::RenderProcessHost> extension_host( |
| 547 extension1_instance->GetProcess()); | 547 extension1_instance->GetProcess()); |
| 548 EXPECT_EQ(extension1_instance->GetProcess(), | 548 EXPECT_EQ(extension1_instance->GetProcess(), |
| 549 extension2_instance->GetProcess()); | 549 extension2_instance->GetProcess()); |
| 550 | 550 |
| 551 // Create some WebUI instances and make sure they share a process. | 551 // Create some WebUI instances and make sure they share a process. |
| 552 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, | 552 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, |
| 553 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); | 553 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); |
| 554 policy->GrantWebUIBindings(webui1_instance->GetProcess()->id()); | 554 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); |
| 555 | 555 |
| 556 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory, | 556 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory, |
| 557 GURL(chrome::kChromeUIScheme + std::string("://history")))); | 557 GURL(chrome::kChromeUIScheme + std::string("://history")))); |
| 558 | 558 |
| 559 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); | 559 scoped_ptr<content::RenderProcessHost> dom_host( |
| 560 webui1_instance->GetProcess()); |
| 560 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); | 561 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); |
| 561 | 562 |
| 562 // Make sure none of differing privilege processes are mixed. | 563 // Make sure none of differing privilege processes are mixed. |
| 563 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); | 564 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); |
| 564 | 565 |
| 565 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { | 566 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { |
| 566 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | 567 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); |
| 567 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); | 568 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); |
| 568 } | 569 } |
| 569 | 570 |
| 570 STLDeleteContainerPointers(hosts.begin(), hosts.end()); | 571 STLDeleteContainerPointers(hosts.begin(), hosts.end()); |
| 571 } | 572 } |
| OLD | NEW |