| 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 <set> | 5 #include <set> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "content/public/common/page_state.h" | 28 #include "content/public/common/page_state.h" |
| 29 #include "content/public/common/url_constants.h" | 29 #include "content/public/common/url_constants.h" |
| 30 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
| 31 #include "content/public/test/content_browser_test.h" | 31 #include "content/public/test/content_browser_test.h" |
| 32 #include "content/public/test/content_browser_test_utils.h" | 32 #include "content/public/test/content_browser_test_utils.h" |
| 33 #include "content/public/test/test_navigation_observer.h" | 33 #include "content/public/test/test_navigation_observer.h" |
| 34 #include "content/public/test/test_utils.h" | 34 #include "content/public/test/test_utils.h" |
| 35 #include "content/shell/browser/shell.h" | 35 #include "content/shell/browser/shell.h" |
| 36 #include "net/base/net_util.h" | 36 #include "net/base/net_util.h" |
| 37 #include "net/dns/mock_host_resolver.h" | 37 #include "net/dns/mock_host_resolver.h" |
| 38 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 38 #include "net/test/spawned_test_server/spawned_test_server.h" | 39 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 39 | 40 |
| 40 using base::ASCIIToUTF16; | 41 using base::ASCIIToUTF16; |
| 41 | 42 |
| 42 namespace content { | 43 namespace content { |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 const char kOpenUrlViaClickTargetFunc[] = | 47 const char kOpenUrlViaClickTargetFunc[] = |
| 47 "(function(url) {\n" | 48 "(function(url) {\n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 81 |
| 81 void StartServer() { | 82 void StartServer() { |
| 82 // Support multiple sites on the test server. | 83 // Support multiple sites on the test server. |
| 83 host_resolver()->AddRule("*", "127.0.0.1"); | 84 host_resolver()->AddRule("*", "127.0.0.1"); |
| 84 ASSERT_TRUE(test_server()->Start()); | 85 ASSERT_TRUE(test_server()->Start()); |
| 85 | 86 |
| 86 foo_host_port_ = test_server()->host_port_pair(); | 87 foo_host_port_ = test_server()->host_port_pair(); |
| 87 foo_host_port_.set_host(foo_com_); | 88 foo_host_port_.set_host(foo_com_); |
| 88 } | 89 } |
| 89 | 90 |
| 91 void StartEmbeddedServer() { |
| 92 // Support multiple sites on the embedded test server. |
| 93 host_resolver()->AddRule("*", "127.0.0.1"); |
| 94 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 95 SetupCrossSiteRedirector(embedded_test_server()); |
| 96 } |
| 97 |
| 90 // Returns a URL on foo.com with the given path. | 98 // Returns a URL on foo.com with the given path. |
| 91 GURL GetCrossSiteURL(const std::string& path) { | 99 GURL GetCrossSiteURL(const std::string& path) { |
| 92 GURL cross_site_url(test_server()->GetURL(path)); | 100 GURL cross_site_url(test_server()->GetURL(path)); |
| 93 return cross_site_url.ReplaceComponents(replace_host_); | 101 return cross_site_url.ReplaceComponents(replace_host_); |
| 94 } | 102 } |
| 95 | 103 |
| 96 protected: | 104 protected: |
| 97 std::string foo_com_; | 105 std::string foo_com_; |
| 98 GURL::Replacements replace_host_; | 106 GURL::Replacements replace_host_; |
| 99 net::HostPortPair foo_host_port_; | 107 net::HostPortPair foo_host_port_; |
| (...skipping 1615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1715 shell()->web_contents()->GetController().GoBack(); | 1723 shell()->web_contents()->GetController().GoBack(); |
| 1716 back_nav_load_observer.Wait(); | 1724 back_nav_load_observer.Wait(); |
| 1717 EXPECT_NE(process_id, | 1725 EXPECT_NE(process_id, |
| 1718 shell()->web_contents()->GetRenderProcessHost()->GetID()); | 1726 shell()->web_contents()->GetRenderProcessHost()->GetID()); |
| 1719 | 1727 |
| 1720 // Ensure that the file access still exists in the new process ID. | 1728 // Ensure that the file access still exists in the new process ID. |
| 1721 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( | 1729 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
| 1722 shell()->web_contents()->GetRenderProcessHost()->GetID(), file)); | 1730 shell()->web_contents()->GetRenderProcessHost()->GetID(), file)); |
| 1723 } | 1731 } |
| 1724 | 1732 |
| 1733 // This class implements waiting for RenderFrameHost destruction. It relies on |
| 1734 // the fact that RenderFrameDeleted event is fired when RenderFrameHost is |
| 1735 // destroyed. |
| 1736 // Note: RenderFrameDeleted is also fired when the process associated with the |
| 1737 // RenderFrameHost crashes, so this cannot be used in cases where process dying |
| 1738 // is expected. |
| 1739 class RenderFrameHostDestructionObserver : public WebContentsObserver { |
| 1740 public: |
| 1741 explicit RenderFrameHostDestructionObserver(RenderFrameHost* rfh) |
| 1742 : WebContentsObserver(WebContents::FromRenderFrameHost(rfh)), |
| 1743 message_loop_runner_(new MessageLoopRunner), |
| 1744 deleted_(false), |
| 1745 render_frame_host_(rfh) {} |
| 1746 ~RenderFrameHostDestructionObserver() override {} |
| 1747 |
| 1748 void Wait() { |
| 1749 if (deleted_) |
| 1750 return; |
| 1751 |
| 1752 message_loop_runner_->Run(); |
| 1753 } |
| 1754 |
| 1755 // WebContentsObserver implementation: |
| 1756 void RenderFrameDeleted(RenderFrameHost* rfh) override { |
| 1757 if (rfh == render_frame_host_) { |
| 1758 CHECK(!deleted_); |
| 1759 deleted_ = true; |
| 1760 } |
| 1761 |
| 1762 if (deleted_ && message_loop_runner_->loop_running()) { |
| 1763 base::MessageLoop::current()->PostTask( |
| 1764 FROM_HERE, message_loop_runner_->QuitClosure()); |
| 1765 } |
| 1766 } |
| 1767 |
| 1768 private: |
| 1769 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 1770 bool deleted_; |
| 1771 RenderFrameHost* render_frame_host_; |
| 1772 }; |
| 1773 |
| 1774 // Ensures that no RenderFrameHost/RenderViewHost objects are leaked when |
| 1775 // doing a simple cross-process navigation. |
| 1776 IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, |
| 1777 CleanupOnCrossProcessNavigation) { |
| 1778 StartEmbeddedServer(); |
| 1779 |
| 1780 // Do an initial navigation and capture objects we expect to be cleaned up |
| 1781 // on cross-process navigation. |
| 1782 GURL start_url = embedded_test_server()->GetURL("/title1.html"); |
| 1783 NavigateToURL(shell(), start_url); |
| 1784 |
| 1785 FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 1786 ->GetFrameTree() |
| 1787 ->root(); |
| 1788 int32 orig_site_instance_id = |
| 1789 root->current_frame_host()->GetSiteInstance()->GetId(); |
| 1790 int initial_process_id = |
| 1791 root->current_frame_host()->GetSiteInstance()->GetProcess()->GetID(); |
| 1792 int initial_rfh_id = root->current_frame_host()->GetRoutingID(); |
| 1793 int initial_rvh_id = |
| 1794 root->current_frame_host()->render_view_host()->GetRoutingID(); |
| 1795 |
| 1796 // Navigate cross-process and ensure that cleanup is performed as expected. |
| 1797 GURL cross_site_url = |
| 1798 embedded_test_server()->GetURL("foo.com", "/title2.html"); |
| 1799 RenderFrameHostDestructionObserver rfh_observer(root->current_frame_host()); |
| 1800 NavigateToURL(shell(), cross_site_url); |
| 1801 rfh_observer.Wait(); |
| 1802 |
| 1803 EXPECT_NE(orig_site_instance_id, |
| 1804 root->current_frame_host()->GetSiteInstance()->GetId()); |
| 1805 EXPECT_FALSE(RenderFrameHost::FromID(initial_process_id, initial_rfh_id)); |
| 1806 EXPECT_FALSE(RenderViewHost::FromID(initial_process_id, initial_rvh_id)); |
| 1807 } |
| 1808 |
| 1725 } // namespace content | 1809 } // namespace content |
| OLD | NEW |