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 "base/command_line.h" |
5 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/time/time.h" |
7 #include "content/browser/frame_host/cross_site_transferring_request.h" | 9 #include "content/browser/frame_host/cross_site_transferring_request.h" |
| 10 #include "content/browser/frame_host/navigation_before_commit_info.h" |
8 #include "content/browser/frame_host/navigation_controller_impl.h" | 11 #include "content/browser/frame_host/navigation_controller_impl.h" |
9 #include "content/browser/frame_host/navigation_entry_impl.h" | 12 #include "content/browser/frame_host/navigation_entry_impl.h" |
10 #include "content/browser/frame_host/navigation_request.h" | 13 #include "content/browser/frame_host/navigation_request.h" |
11 #include "content/browser/frame_host/navigator.h" | 14 #include "content/browser/frame_host/navigator.h" |
| 15 #include "content/browser/frame_host/navigator_impl.h" |
12 #include "content/browser/frame_host/render_frame_host_manager.h" | 16 #include "content/browser/frame_host/render_frame_host_manager.h" |
13 #include "content/browser/site_instance_impl.h" | 17 #include "content/browser/site_instance_impl.h" |
14 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 18 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
15 #include "content/common/view_messages.h" | 19 #include "content/common/view_messages.h" |
16 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
17 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
19 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
20 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
21 #include "content/public/browser/render_widget_host_iterator.h" | 25 #include "content/public/browser/render_widget_host_iterator.h" |
22 #include "content/public/browser/web_contents_delegate.h" | 26 #include "content/public/browser/web_contents_delegate.h" |
23 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
24 #include "content/public/browser/web_ui_controller.h" | 28 #include "content/public/browser/web_ui_controller.h" |
25 #include "content/public/common/bindings_policy.h" | 29 #include "content/public/common/bindings_policy.h" |
| 30 #include "content/public/common/content_switches.h" |
26 #include "content/public/common/javascript_message_type.h" | 31 #include "content/public/common/javascript_message_type.h" |
27 #include "content/public/common/page_transition_types.h" | 32 #include "content/public/common/page_transition_types.h" |
28 #include "content/public/common/url_constants.h" | 33 #include "content/public/common/url_constants.h" |
29 #include "content/public/common/url_utils.h" | 34 #include "content/public/common/url_utils.h" |
30 #include "content/public/test/mock_render_process_host.h" | 35 #include "content/public/test/mock_render_process_host.h" |
31 #include "content/public/test/test_notification_tracker.h" | 36 #include "content/public/test/test_notification_tracker.h" |
32 #include "content/test/test_content_browser_client.h" | 37 #include "content/test/test_content_browser_client.h" |
33 #include "content/test/test_content_client.h" | 38 #include "content/test/test_content_client.h" |
34 #include "content/test/test_render_view_host.h" | 39 #include "content/test/test_render_view_host.h" |
35 #include "content/test/test_web_contents.h" | 40 #include "content/test/test_web_contents.h" |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 // BeforeUnload finishes. | 361 // BeforeUnload finishes. |
357 ntp_rvh->SendBeforeUnloadACK(true); | 362 ntp_rvh->SendBeforeUnloadACK(true); |
358 | 363 |
359 dest_rvh->SendNavigate(101, kDestUrl); | 364 dest_rvh->SendNavigate(101, kDestUrl); |
360 ntp_rvh->OnSwappedOut(false); | 365 ntp_rvh->OnSwappedOut(false); |
361 | 366 |
362 EXPECT_TRUE(ntp_rvh->IsSwappedOut()); | 367 EXPECT_TRUE(ntp_rvh->IsSwappedOut()); |
363 return ntp_rvh; | 368 return ntp_rvh; |
364 } | 369 } |
365 | 370 |
366 NavigationRequest* NavigationRequestForRenderFrameManager( | 371 NavigationRequest* GetNavigationRequestForRenderFrameManager( |
367 RenderFrameHostManager* manager) const { | 372 RenderFrameHostManager* manager) const { |
368 return manager->navigation_request_for_testing(); | 373 return manager->navigation_request_for_testing(); |
369 } | 374 } |
370 | 375 |
| 376 RenderFrameHostImpl* GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 377 RenderFrameHostManager* manager) { |
| 378 return manager->speculative_render_frame_host_.get(); |
| 379 } |
| 380 |
| 381 void EnableBrowserSideNavigation() { |
| 382 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 383 switches::kEnableBrowserSideNavigation); |
| 384 } |
371 private: | 385 private: |
372 RenderFrameHostManagerTestWebUIControllerFactory factory_; | 386 RenderFrameHostManagerTestWebUIControllerFactory factory_; |
373 scoped_ptr<FrameLifetimeConsistencyChecker> lifetime_checker_; | 387 scoped_ptr<FrameLifetimeConsistencyChecker> lifetime_checker_; |
374 }; | 388 }; |
375 | 389 |
376 // Tests that when you navigate from a chrome:// url to another page, and | 390 // Tests that when you navigate from a chrome:// url to another page, and |
377 // then do that same thing in another tab, that the two resulting pages have | 391 // then do that same thing in another tab, that the two resulting pages have |
378 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is | 392 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is |
379 // a regression test for bug 9364. | 393 // a regression test for bug 9364. |
380 TEST_F(RenderFrameHostManagerTest, NewTabPageProcesses) { | 394 TEST_F(RenderFrameHostManagerTest, NewTabPageProcesses) { |
(...skipping 1462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1843 static_cast<SiteInstanceImpl*>( | 1857 static_cast<SiteInstanceImpl*>( |
1844 pending_rfh->GetSiteInstance())->increment_active_view_count(); | 1858 pending_rfh->GetSiteInstance())->increment_active_view_count(); |
1845 | 1859 |
1846 main_test_rfh()->OnMessageReceived( | 1860 main_test_rfh()->OnMessageReceived( |
1847 FrameHostMsg_BeforeUnload_ACK(0, false, now, now)); | 1861 FrameHostMsg_BeforeUnload_ACK(0, false, now, now)); |
1848 EXPECT_FALSE(contents()->cross_navigation_pending()); | 1862 EXPECT_FALSE(contents()->cross_navigation_pending()); |
1849 EXPECT_FALSE(rvh_deleted_observer.deleted()); | 1863 EXPECT_FALSE(rvh_deleted_observer.deleted()); |
1850 } | 1864 } |
1851 } | 1865 } |
1852 | 1866 |
1853 // Browser-side navigation: Test that a proper NavigationRequest is created by | 1867 // PlzNavigate: Test that a proper NavigationRequest is created by |
1854 // BeginNavigation. | 1868 // BeginNavigation. |
1855 TEST_F(RenderFrameHostManagerTest, BrowserSideNavigationBeginNavigation) { | 1869 TEST_F(RenderFrameHostManagerTest, BrowserSideNavigationBeginNavigation) { |
1856 const GURL kUrl1("http://www.google.com/"); | 1870 const GURL kUrl1("http://www.google.com/"); |
1857 const GURL kUrl2("http://www.chromium.org/"); | 1871 const GURL kUrl2("http://www.chromium.org/"); |
1858 const GURL kUrl3("http://www.gmail.com/"); | 1872 const GURL kUrl3("http://www.gmail.com/"); |
1859 | 1873 |
| 1874 // TODO(clamy): we should be enabling browser side navigations here |
| 1875 // when CommitNavigation is properly implemented. |
1860 // Navigate to the first page. | 1876 // Navigate to the first page. |
1861 contents()->NavigateAndCommit(kUrl1); | 1877 contents()->NavigateAndCommit(kUrl1); |
1862 TestRenderViewHost* rvh1 = test_rvh(); | 1878 TestRenderViewHost* rvh1 = test_rvh(); |
1863 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state()); | 1879 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state()); |
1864 | 1880 |
| 1881 EnableBrowserSideNavigation(); |
1865 // Add a subframe. | 1882 // Add a subframe. |
1866 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( | 1883 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( |
1867 contents()->GetFrameTree()->AddFrame( | 1884 contents()->GetFrameTree()->AddFrame( |
1868 contents()->GetFrameTree()->root(), 14, "Child")); | 1885 contents()->GetFrameTree()->root(), 14, "Child")); |
1869 | 1886 |
1870 // Simulate a BeginNavigation IPC on the subframe. | 1887 // Simulate a BeginNavigation IPC on the subframe. |
| 1888 contents()->GetController().LoadURL( |
| 1889 kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
1871 subframe_rfh->SendBeginNavigationWithURL(kUrl2); | 1890 subframe_rfh->SendBeginNavigationWithURL(kUrl2); |
1872 NavigationRequest* subframe_request = | 1891 NavigationRequest* subframe_request = |
1873 NavigationRequestForRenderFrameManager( | 1892 GetNavigationRequestForRenderFrameManager( |
1874 subframe_rfh->frame_tree_node()->render_manager()); | 1893 subframe_rfh->frame_tree_node()->render_manager()); |
1875 ASSERT_TRUE(subframe_request); | 1894 ASSERT_TRUE(subframe_request); |
1876 EXPECT_EQ(kUrl2, subframe_request->info_for_testing().navigation_params.url); | 1895 EXPECT_EQ(kUrl2, subframe_request->info_for_testing().navigation_params.url); |
1877 // First party for cookies url should be that of the main frame. | 1896 // First party for cookies url should be that of the main frame. |
1878 EXPECT_EQ( | 1897 EXPECT_EQ( |
1879 kUrl1, subframe_request->info_for_testing().first_party_for_cookies); | 1898 kUrl1, subframe_request->info_for_testing().first_party_for_cookies); |
1880 EXPECT_FALSE(subframe_request->info_for_testing().is_main_frame); | 1899 EXPECT_FALSE(subframe_request->info_for_testing().is_main_frame); |
1881 EXPECT_TRUE(subframe_request->info_for_testing().parent_is_main_frame); | 1900 EXPECT_TRUE(subframe_request->info_for_testing().parent_is_main_frame); |
1882 | 1901 |
1883 // Simulate a BeginNavigation IPC on the main frame. | 1902 // Simulate a BeginNavigation IPC on the main frame. |
| 1903 contents()->GetController().LoadURL( |
| 1904 kUrl3, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
1884 main_test_rfh()->SendBeginNavigationWithURL(kUrl3); | 1905 main_test_rfh()->SendBeginNavigationWithURL(kUrl3); |
1885 NavigationRequest* main_request = | 1906 NavigationRequest* main_request = |
1886 NavigationRequestForRenderFrameManager( | 1907 GetNavigationRequestForRenderFrameManager( |
1887 main_test_rfh()->frame_tree_node()->render_manager()); | 1908 main_test_rfh()->frame_tree_node()->render_manager()); |
1888 ASSERT_TRUE(main_request); | 1909 ASSERT_TRUE(main_request); |
1889 EXPECT_EQ(kUrl3, main_request->info_for_testing().navigation_params.url); | 1910 EXPECT_EQ(kUrl3, main_request->info_for_testing().navigation_params.url); |
1890 EXPECT_EQ(kUrl3, main_request->info_for_testing().first_party_for_cookies); | 1911 EXPECT_EQ(kUrl3, main_request->info_for_testing().first_party_for_cookies); |
1891 EXPECT_TRUE(main_request->info_for_testing().is_main_frame); | 1912 EXPECT_TRUE(main_request->info_for_testing().is_main_frame); |
1892 EXPECT_FALSE(main_request->info_for_testing().parent_is_main_frame); | 1913 EXPECT_FALSE(main_request->info_for_testing().parent_is_main_frame); |
1893 } | 1914 } |
1894 | 1915 |
| 1916 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that |
| 1917 // the RenderFrameHost is initialized to handle the request when it commits. |
| 1918 // Also ensures that the RenderFrameHost is not modified when the navigation |
| 1919 // commits. |
| 1920 TEST_F(RenderFrameHostManagerTest, |
| 1921 BrowserSideNavigationRequestNavigationNoLiveRenderer) { |
| 1922 const GURL kUrl("http://www.google.com/"); |
| 1923 |
| 1924 EnableBrowserSideNavigation(); |
| 1925 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); |
| 1926 contents()->GetController().LoadURL( |
| 1927 kUrl, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
| 1928 NavigationRequest* main_request = |
| 1929 GetNavigationRequestForRenderFrameManager( |
| 1930 main_test_rfh()->frame_tree_node()->render_manager()); |
| 1931 // A NavigationRequest should have been generated. |
| 1932 EXPECT_TRUE(main_request != NULL); |
| 1933 // The RFH should have been initialized to handle the navigation when it |
| 1934 // commits. |
| 1935 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); |
| 1936 RenderFrameHostImpl* rfh = main_test_rfh(); |
| 1937 |
| 1938 // Now commit the same url. |
| 1939 NavigationBeforeCommitInfo commit_info; |
| 1940 commit_info.navigation_url = kUrl; |
| 1941 main_test_rfh()->frame_tree_node()->render_manager()->CommitNavigation( |
| 1942 commit_info); |
| 1943 main_request = GetNavigationRequestForRenderFrameManager( |
| 1944 main_test_rfh()->frame_tree_node()->render_manager()); |
| 1945 |
| 1946 // The main rfh should not have been changed and we should not have a |
| 1947 // navigation request anymore. |
| 1948 EXPECT_EQ(rfh, main_test_rfh()); |
| 1949 EXPECT_TRUE(main_request == NULL); |
| 1950 } |
| 1951 |
| 1952 // PlzNavigate: Test that a speculative renderer is spawned when navigating to a |
| 1953 // different site instance than the one of the main render frame host. Test that |
| 1954 // it is used if the navigation commits with a url pertaining to the same site |
| 1955 // instance. |
| 1956 TEST_F(RenderFrameHostManagerTest, |
| 1957 BrowserSideNavigationSpeculativeRendererSameURL) { |
| 1958 const GURL kUrl1("http://www.chromium.org/"); |
| 1959 const GURL kUrl2("http://www.google.com/"); |
| 1960 const GURL kUrl3("http://docs.google.com/"); |
| 1961 |
| 1962 // TODO(clamy): we should be enabling browser side navigations here |
| 1963 // when CommitNavigation is properly implemented. |
| 1964 // Navigate to the first page. |
| 1965 contents()->NavigateAndCommit(kUrl1); |
| 1966 TestRenderViewHost* rvh1 = test_rvh(); |
| 1967 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state()); |
| 1968 |
| 1969 EnableBrowserSideNavigation(); |
| 1970 // Navigate to a different site. |
| 1971 contents()->GetController().LoadURL( |
| 1972 kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
| 1973 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
| 1974 NavigationRequest* main_request = |
| 1975 GetNavigationRequestForRenderFrameManager( |
| 1976 main_test_rfh()->frame_tree_node()->render_manager()); |
| 1977 ASSERT_TRUE(main_request); |
| 1978 RenderFrameHostImpl* speculative_rfh = |
| 1979 GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 1980 main_test_rfh()->frame_tree_node()->render_manager()); |
| 1981 EXPECT_TRUE(speculative_rfh != NULL); |
| 1982 EXPECT_NE(main_test_rfh(), speculative_rfh); |
| 1983 |
| 1984 // Commit a url of the same site instance. |
| 1985 NavigationBeforeCommitInfo commit_info; |
| 1986 commit_info.navigation_url = kUrl3; |
| 1987 main_test_rfh()->frame_tree_node()->render_manager()->CommitNavigation( |
| 1988 commit_info); |
| 1989 main_request = GetNavigationRequestForRenderFrameManager( |
| 1990 main_test_rfh()->frame_tree_node()->render_manager()); |
| 1991 EXPECT_EQ(main_test_rfh(), speculative_rfh); |
| 1992 EXPECT_TRUE(main_request == NULL); |
| 1993 } |
| 1994 |
| 1995 // PlzNavigate: Test that a speculative renderer is spawned when navigating to a |
| 1996 // different site instance than the one of the main render frame host. Test that |
| 1997 // it is discarded if the navigation commits with a different url. |
| 1998 TEST_F(RenderFrameHostManagerTest, |
| 1999 BrowserSideNavigationSpeculativeRendererDifferentURL) { |
| 2000 const GURL kUrl1("http://www.google.com/"); |
| 2001 const GURL kUrl2("http://www.chromium.org/"); |
| 2002 const GURL kUrl3("http://www.gmail.com/"); |
| 2003 |
| 2004 // TODO(clamy): we should be enabling browser side navigations here |
| 2005 // eventually. |
| 2006 // Navigate to the first page. |
| 2007 contents()->NavigateAndCommit(kUrl1); |
| 2008 TestRenderViewHost* rvh1 = test_rvh(); |
| 2009 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state()); |
| 2010 |
| 2011 EnableBrowserSideNavigation(); |
| 2012 // Navigate to a different site. |
| 2013 contents()->GetController().LoadURL( |
| 2014 kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
| 2015 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
| 2016 NavigationRequest* main_request = |
| 2017 GetNavigationRequestForRenderFrameManager( |
| 2018 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2019 ASSERT_TRUE(main_request); |
| 2020 RenderFrameHostImpl* speculative_rfh = |
| 2021 GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 2022 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2023 RenderFrameHostImpl* current_rfh = main_test_rfh(); |
| 2024 EXPECT_TRUE(speculative_rfh != NULL); |
| 2025 EXPECT_NE(main_test_rfh(), speculative_rfh); |
| 2026 |
| 2027 // Commit a different url. |
| 2028 NavigationBeforeCommitInfo commit_info; |
| 2029 commit_info.navigation_url = kUrl3; |
| 2030 main_test_rfh()->frame_tree_node()->render_manager()->CommitNavigation( |
| 2031 commit_info); |
| 2032 main_request = GetNavigationRequestForRenderFrameManager( |
| 2033 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2034 speculative_rfh = GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 2035 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2036 EXPECT_NE(main_test_rfh(), speculative_rfh); |
| 2037 EXPECT_NE(main_test_rfh(), current_rfh); |
| 2038 EXPECT_TRUE(main_request == NULL); |
| 2039 EXPECT_TRUE(speculative_rfh == NULL); |
| 2040 } |
| 2041 |
| 2042 // PlzNavigate: Test that a speculative renderer is spawned when navigating to a |
| 2043 // different site instance than the one of the main render frame host. Test that |
| 2044 // it is discarded and the old render frame host is used if the navigation |
| 2045 // commits with the url from the previous render frame host. |
| 2046 TEST_F(RenderFrameHostManagerTest, |
| 2047 BrowserSideNavigationSpeculativeRendererOldURL) { |
| 2048 const GURL kUrl1("http://www.google.com/"); |
| 2049 const GURL kUrl2("http://www.chromium.org/"); |
| 2050 |
| 2051 // TODO(clamy): we should be enabling browser side navigations here |
| 2052 // eventually. |
| 2053 // Navigate to the first page. |
| 2054 contents()->NavigateAndCommit(kUrl1); |
| 2055 TestRenderViewHost* rvh1 = test_rvh(); |
| 2056 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state()); |
| 2057 |
| 2058 EnableBrowserSideNavigation(); |
| 2059 // Navigate to a different site. |
| 2060 contents()->GetController().LoadURL( |
| 2061 kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string()); |
| 2062 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
| 2063 NavigationRequest* main_request = |
| 2064 GetNavigationRequestForRenderFrameManager( |
| 2065 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2066 ASSERT_TRUE(main_request); |
| 2067 RenderFrameHostImpl* speculative_rfh = |
| 2068 GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 2069 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2070 RenderFrameHostImpl* current_rfh = main_test_rfh(); |
| 2071 EXPECT_TRUE(speculative_rfh != NULL); |
| 2072 EXPECT_NE(main_test_rfh(), speculative_rfh); |
| 2073 |
| 2074 // Commit a different url. |
| 2075 NavigationBeforeCommitInfo commit_info; |
| 2076 commit_info.navigation_url = kUrl1; |
| 2077 main_test_rfh()->frame_tree_node()->render_manager()->CommitNavigation( |
| 2078 commit_info); |
| 2079 main_request = GetNavigationRequestForRenderFrameManager( |
| 2080 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2081 speculative_rfh = GetSpeculativeRenderFrameHostForRenderFrameManager( |
| 2082 main_test_rfh()->frame_tree_node()->render_manager()); |
| 2083 EXPECT_NE(main_test_rfh(), speculative_rfh); |
| 2084 EXPECT_EQ(main_test_rfh(), current_rfh); |
| 2085 EXPECT_TRUE(main_request == NULL); |
| 2086 EXPECT_TRUE(speculative_rfh == NULL); |
| 2087 } |
| 2088 |
1895 } // namespace content | 2089 } // namespace content |
OLD | NEW |