| 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/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_impl.h" | 10 #include "content/browser/child_process_security_policy_impl.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/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.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_impl.h" | 15 #include "content/browser/site_instance_impl.h" |
| 16 #include "content/browser/web_contents/navigation_entry_impl.h" | 16 #include "content/browser/web_contents/navigation_entry_impl.h" |
| 17 #include "content/browser/web_contents/web_contents_impl.h" | 17 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/public/browser/web_ui_controller_factory.h" | 18 #include "content/public/browser/web_ui_controller_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/public/test/mock_render_process_host.h" | 22 #include "content/public/test/mock_render_process_host.h" |
| 23 #include "content/test/test_browser_context.h" | 23 #include "content/public/test/test_browser_context.h" |
| 24 #include "content/test/test_browser_thread.h" | 24 #include "content/test/test_browser_thread.h" |
| 25 #include "content/test/test_content_client.h" | 25 #include "content/test/test_content_client.h" |
| 26 #include "googleurl/src/url_util.h" | 26 #include "googleurl/src/url_util.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using content::BrowserContext; | 29 using content::BrowserContext; |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using content::BrowserThreadImpl; | 31 using content::BrowserThreadImpl; |
| 32 using content::MockRenderProcessHost; | 32 using content::MockRenderProcessHost; |
| 33 using content::MockRenderProcessHostFactory; | 33 using content::MockRenderProcessHostFactory; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 delete e1; | 247 delete e1; |
| 248 EXPECT_EQ(0, site_delete_counter); | 248 EXPECT_EQ(0, site_delete_counter); |
| 249 EXPECT_EQ(0, browsing_delete_counter); | 249 EXPECT_EQ(0, browsing_delete_counter); |
| 250 delete e2; | 250 delete e2; |
| 251 EXPECT_EQ(1, site_delete_counter); | 251 EXPECT_EQ(1, site_delete_counter); |
| 252 // instance is now deleted | 252 // instance is now deleted |
| 253 EXPECT_EQ(1, browsing_delete_counter); | 253 EXPECT_EQ(1, browsing_delete_counter); |
| 254 // browsing_instance is now deleted | 254 // browsing_instance is now deleted |
| 255 | 255 |
| 256 // Ensure that instances are deleted when their RenderViewHosts are gone. | 256 // Ensure that instances are deleted when their RenderViewHosts are gone. |
| 257 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 257 scoped_ptr<content::TestBrowserContext> browser_context( |
| 258 new content::TestBrowserContext()); |
| 258 instance = | 259 instance = |
| 259 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), | 260 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), |
| 260 &site_delete_counter, | 261 &site_delete_counter, |
| 261 &browsing_delete_counter); | 262 &browsing_delete_counter); |
| 262 { | 263 { |
| 263 WebContentsImpl web_contents(browser_context.get(), instance, | 264 WebContentsImpl web_contents(browser_context.get(), instance, |
| 264 MSG_ROUTING_NONE, NULL, NULL, NULL); | 265 MSG_ROUTING_NONE, NULL, NULL, NULL); |
| 265 EXPECT_EQ(1, site_delete_counter); | 266 EXPECT_EQ(1, site_delete_counter); |
| 266 EXPECT_EQ(1, browsing_delete_counter); | 267 EXPECT_EQ(1, browsing_delete_counter); |
| 267 } | 268 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 EXPECT_EQ(1, site_delete_counter1); | 312 EXPECT_EQ(1, site_delete_counter1); |
| 312 EXPECT_EQ(1, site_delete_counter2); | 313 EXPECT_EQ(1, site_delete_counter2); |
| 313 | 314 |
| 314 // Both BrowsingInstances are also now deleted | 315 // Both BrowsingInstances are also now deleted |
| 315 EXPECT_EQ(2, browsing_delete_counter); | 316 EXPECT_EQ(2, browsing_delete_counter); |
| 316 } | 317 } |
| 317 | 318 |
| 318 // Test to ensure GetProcess returns and creates processes correctly. | 319 // Test to ensure GetProcess returns and creates processes correctly. |
| 319 TEST_F(SiteInstanceTest, GetProcess) { | 320 TEST_F(SiteInstanceTest, GetProcess) { |
| 320 // Ensure that GetProcess returns a process. | 321 // Ensure that GetProcess returns a process. |
| 321 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 322 scoped_ptr<content::TestBrowserContext> browser_context( |
| 323 new content::TestBrowserContext()); |
| 322 scoped_ptr<content::RenderProcessHost> host1; | 324 scoped_ptr<content::RenderProcessHost> host1; |
| 323 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( | 325 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( |
| 324 SiteInstance::Create(browser_context.get()))); | 326 SiteInstance::Create(browser_context.get()))); |
| 325 host1.reset(instance->GetProcess()); | 327 host1.reset(instance->GetProcess()); |
| 326 EXPECT_TRUE(host1.get() != NULL); | 328 EXPECT_TRUE(host1.get() != NULL); |
| 327 | 329 |
| 328 // Ensure that GetProcess creates a new process. | 330 // Ensure that GetProcess creates a new process. |
| 329 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>( | 331 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>( |
| 330 SiteInstance::Create(browser_context.get()))); | 332 SiteInstance::Create(browser_context.get()))); |
| 331 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); | 333 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 // it won't be deleted by its children. Thus, we'll keep a ref count to it | 507 // it won't be deleted by its children. Thus, we'll keep a ref count to it |
| 506 // to make sure it gets deleted. | 508 // to make sure it gets deleted. |
| 507 scoped_refptr<TestBrowsingInstance> browsing_instance2( | 509 scoped_refptr<TestBrowsingInstance> browsing_instance2( |
| 508 new TestBrowsingInstance(NULL, &delete_counter)); | 510 new TestBrowsingInstance(NULL, &delete_counter)); |
| 509 browsing_instance2->set_use_process_per_site(true); | 511 browsing_instance2->set_use_process_per_site(true); |
| 510 EXPECT_EQ(site_instance_a1.get(), | 512 EXPECT_EQ(site_instance_a1.get(), |
| 511 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 513 browsing_instance2->GetSiteInstanceForURL(url_a2)); |
| 512 | 514 |
| 513 // A visit to the original site in a new BrowsingInstance (different browser | 515 // A visit to the original site in a new BrowsingInstance (different browser |
| 514 // context) should return a different SiteInstance. | 516 // context) should return a different SiteInstance. |
| 515 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 517 scoped_ptr<content::TestBrowserContext> browser_context( |
| 518 new content::TestBrowserContext()); |
| 516 TestBrowsingInstance* browsing_instance3 = | 519 TestBrowsingInstance* browsing_instance3 = |
| 517 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 520 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
| 518 browsing_instance3->set_use_process_per_site(true); | 521 browsing_instance3->set_use_process_per_site(true); |
| 519 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 522 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
| 520 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( | 523 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( |
| 521 static_cast<SiteInstanceImpl*>( | 524 static_cast<SiteInstanceImpl*>( |
| 522 browsing_instance3->GetSiteInstanceForURL(url_a2))); | 525 browsing_instance3->GetSiteInstanceForURL(url_a2))); |
| 523 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | 526 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); |
| 524 | 527 |
| 525 // Should be able to see that we do have SiteInstances. | 528 // Should be able to see that we do have SiteInstances. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | 601 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); |
| 599 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); | 602 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); |
| 600 } | 603 } |
| 601 | 604 |
| 602 STLDeleteContainerPointers(hosts.begin(), hosts.end()); | 605 STLDeleteContainerPointers(hosts.begin(), hosts.end()); |
| 603 } | 606 } |
| 604 | 607 |
| 605 // Test to ensure that HasWrongProcessForURL behaves properly for different | 608 // Test to ensure that HasWrongProcessForURL behaves properly for different |
| 606 // types of URLs. | 609 // types of URLs. |
| 607 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { | 610 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { |
| 608 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 611 scoped_ptr<content::TestBrowserContext> browser_context( |
| 612 new content::TestBrowserContext()); |
| 609 scoped_ptr<content::RenderProcessHost> host; | 613 scoped_ptr<content::RenderProcessHost> host; |
| 610 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( | 614 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( |
| 611 SiteInstance::Create(browser_context.get()))); | 615 SiteInstance::Create(browser_context.get()))); |
| 612 | 616 |
| 613 EXPECT_FALSE(instance->HasSite()); | 617 EXPECT_FALSE(instance->HasSite()); |
| 614 EXPECT_TRUE(instance->GetSite().is_empty()); | 618 EXPECT_TRUE(instance->GetSite().is_empty()); |
| 615 | 619 |
| 616 instance->SetSite(GURL("http://evernote.com/")); | 620 instance->SetSite(GURL("http://evernote.com/")); |
| 617 EXPECT_TRUE(instance->HasSite()); | 621 EXPECT_TRUE(instance->HasSite()); |
| 618 | 622 |
| 619 // Check prior to "assigning" a process to the instance, which is expected | 623 // Check prior to "assigning" a process to the instance, which is expected |
| 620 // to return false due to not being attached to any process yet. | 624 // to return false due to not being attached to any process yet. |
| 621 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 625 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
| 622 | 626 |
| 623 // The call to GetProcess actually creates a new real process, which works | 627 // The call to GetProcess actually creates a new real process, which works |
| 624 // fine, but might be a cause for problems in different contexts. | 628 // fine, but might be a cause for problems in different contexts. |
| 625 host.reset(instance->GetProcess()); | 629 host.reset(instance->GetProcess()); |
| 626 EXPECT_TRUE(host.get() != NULL); | 630 EXPECT_TRUE(host.get() != NULL); |
| 627 EXPECT_TRUE(instance->HasProcess()); | 631 EXPECT_TRUE(instance->HasProcess()); |
| 628 | 632 |
| 629 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 633 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
| 630 EXPECT_FALSE(instance->HasWrongProcessForURL( | 634 EXPECT_FALSE(instance->HasWrongProcessForURL( |
| 631 GURL("javascript:alert(document.location.href);"))); | 635 GURL("javascript:alert(document.location.href);"))); |
| 632 | 636 |
| 633 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); | 637 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); |
| 634 } | 638 } |
| OLD | NEW |