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/command_line.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "base/test/histogram_tester.h" | 8 #include "base/test/histogram_tester.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "content/browser/frame_host/cross_site_transferring_request.h" | 10 #include "content/browser/frame_host/cross_site_transferring_request.h" |
11 #include "content/browser/frame_host/navigation_before_commit_info.h" | |
12 #include "content/browser/frame_host/navigation_controller_impl.h" | 11 #include "content/browser/frame_host/navigation_controller_impl.h" |
13 #include "content/browser/frame_host/navigation_entry_impl.h" | 12 #include "content/browser/frame_host/navigation_entry_impl.h" |
14 #include "content/browser/frame_host/navigation_request.h" | |
15 #include "content/browser/frame_host/navigation_request_info.h" | |
16 #include "content/browser/frame_host/navigator.h" | 13 #include "content/browser/frame_host/navigator.h" |
17 #include "content/browser/frame_host/navigator_impl.h" | |
18 #include "content/browser/frame_host/render_frame_host_manager.h" | 14 #include "content/browser/frame_host/render_frame_host_manager.h" |
19 #include "content/browser/frame_host/render_frame_proxy_host.h" | 15 #include "content/browser/frame_host/render_frame_proxy_host.h" |
20 #include "content/browser/site_instance_impl.h" | 16 #include "content/browser/site_instance_impl.h" |
21 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 17 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
22 #include "content/common/navigation_params.h" | 18 #include "content/common/frame_messages.h" |
23 #include "content/common/view_messages.h" | 19 #include "content/common/view_messages.h" |
24 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
26 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
27 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
28 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
29 #include "content/public/browser/render_widget_host_iterator.h" | 25 #include "content/public/browser/render_widget_host_iterator.h" |
30 #include "content/public/browser/web_contents_delegate.h" | 26 #include "content/public/browser/web_contents_delegate.h" |
31 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
32 #include "content/public/browser/web_ui_controller.h" | 28 #include "content/public/browser/web_ui_controller.h" |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 // BeforeUnload finishes. | 377 // BeforeUnload finishes. |
382 ntp_rfh->SendBeforeUnloadACK(true); | 378 ntp_rfh->SendBeforeUnloadACK(true); |
383 | 379 |
384 dest_rfh->SendNavigate(101, kDestUrl); | 380 dest_rfh->SendNavigate(101, kDestUrl); |
385 ntp_rfh->OnSwappedOut(); | 381 ntp_rfh->OnSwappedOut(); |
386 | 382 |
387 EXPECT_TRUE(ntp_rfh->is_swapped_out()); | 383 EXPECT_TRUE(ntp_rfh->is_swapped_out()); |
388 return ntp_rfh; | 384 return ntp_rfh; |
389 } | 385 } |
390 | 386 |
391 NavigationRequest* GetNavigationRequestForRenderFrameManager( | |
392 RenderFrameHostManager* manager) const { | |
393 return manager->navigation_request_for_testing(); | |
394 } | |
395 | |
396 void EnableBrowserSideNavigation() { | |
397 CommandLine::ForCurrentProcess()->AppendSwitch( | |
398 switches::kEnableBrowserSideNavigation); | |
399 } | |
400 | |
401 void SendRequestNavigation(FrameTreeNode* node, | |
402 const GURL& url) { | |
403 SendRequestNavigationWithParameters( | |
404 node, url, Referrer(), ui::PAGE_TRANSITION_LINK, | |
405 FrameMsg_Navigate_Type::NORMAL); | |
406 } | |
407 | |
408 void SendRequestNavigationWithParameters( | |
409 FrameTreeNode* node, | |
410 const GURL& url, | |
411 const Referrer& referrer, | |
412 ui::PageTransition transition_type, | |
413 FrameMsg_Navigate_Type::Value navigation_type) { | |
414 scoped_ptr<NavigationEntryImpl> entry( | |
415 NavigationEntryImpl::FromNavigationEntry( | |
416 NavigationController::CreateNavigationEntry( | |
417 url, | |
418 referrer, | |
419 transition_type, | |
420 false, | |
421 std::string(), | |
422 controller().GetBrowserContext()))); | |
423 scoped_ptr<NavigationRequest> navigation_request(new NavigationRequest( | |
424 node->frame_tree_node_id(), | |
425 CommonNavigationParams(entry->GetURL(), | |
426 entry->GetReferrer(), | |
427 entry->GetTransitionType(), | |
428 navigation_type, | |
429 !entry->IsViewSourceMode()), | |
430 CommitNavigationParams(entry->GetPageState(), | |
431 entry->GetIsOverridingUserAgent(), | |
432 base::TimeTicks::Now()))); | |
433 RequestNavigationParams request_params( | |
434 entry->GetHasPostData(), | |
435 entry->extra_headers(), | |
436 entry->GetBrowserInitiatedPostData()); | |
437 node->render_manager()->RequestNavigation(navigation_request.Pass(), | |
438 request_params); | |
439 } | |
440 | |
441 private: | 387 private: |
442 RenderFrameHostManagerTestWebUIControllerFactory factory_; | 388 RenderFrameHostManagerTestWebUIControllerFactory factory_; |
443 scoped_ptr<FrameLifetimeConsistencyChecker> lifetime_checker_; | 389 scoped_ptr<FrameLifetimeConsistencyChecker> lifetime_checker_; |
444 }; | 390 }; |
445 | 391 |
446 // Tests that when you navigate from a chrome:// url to another page, and | 392 // Tests that when you navigate from a chrome:// url to another page, and |
447 // then do that same thing in another tab, that the two resulting pages have | 393 // then do that same thing in another tab, that the two resulting pages have |
448 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is | 394 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is |
449 // a regression test for bug 9364. | 395 // a regression test for bug 9364. |
450 TEST_F(RenderFrameHostManagerTest, NewTabPageProcesses) { | 396 TEST_F(RenderFrameHostManagerTest, NewTabPageProcesses) { |
(...skipping 1292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1743 // cause the pending RFH to be swapped out instead of deleted. | 1689 // cause the pending RFH to be swapped out instead of deleted. |
1744 pending_rfh->GetSiteInstance()->increment_active_frame_count(); | 1690 pending_rfh->GetSiteInstance()->increment_active_frame_count(); |
1745 | 1691 |
1746 contents()->GetMainFrame()->OnMessageReceived( | 1692 contents()->GetMainFrame()->OnMessageReceived( |
1747 FrameHostMsg_BeforeUnload_ACK(0, false, now, now)); | 1693 FrameHostMsg_BeforeUnload_ACK(0, false, now, now)); |
1748 EXPECT_FALSE(contents()->cross_navigation_pending()); | 1694 EXPECT_FALSE(contents()->cross_navigation_pending()); |
1749 EXPECT_FALSE(rfh_deleted_observer.deleted()); | 1695 EXPECT_FALSE(rfh_deleted_observer.deleted()); |
1750 } | 1696 } |
1751 } | 1697 } |
1752 | 1698 |
1753 // PlzNavigate: Test that a proper NavigationRequest is created by | |
1754 // BeginNavigation. | |
1755 // Note that all PlzNavigate methods on the browser side require the use of the | |
1756 // flag kEnableBrowserSideNavigation. | |
1757 TEST_F(RenderFrameHostManagerTest, BrowserSideNavigationBeginNavigation) { | |
1758 const GURL kUrl1("http://www.google.com/"); | |
1759 const GURL kUrl2("http://www.chromium.org/"); | |
1760 const GURL kUrl3("http://www.gmail.com/"); | |
1761 const int64 kFirstNavRequestID = 1; | |
1762 | |
1763 // TODO(clamy): we should be enabling browser side navigations here | |
1764 // when CommitNavigation is properly implemented. | |
1765 // Navigate to the first page. | |
1766 contents()->NavigateAndCommit(kUrl1); | |
1767 | |
1768 EnableBrowserSideNavigation(); | |
1769 // Add a subframe. | |
1770 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( | |
1771 contents()->GetFrameTree()->AddFrame( | |
1772 contents()->GetFrameTree()->root(), 14, "Child")); | |
1773 | |
1774 RenderFrameHostManager* subframe_manager = | |
1775 subframe_rfh->frame_tree_node()->render_manager(); | |
1776 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2); | |
1777 // Simulate a BeginNavigation IPC on the subframe. | |
1778 subframe_rfh->SendBeginNavigationWithURL(kUrl2); | |
1779 NavigationRequest* subframe_request = | |
1780 GetNavigationRequestForRenderFrameManager(subframe_manager); | |
1781 ASSERT_TRUE(subframe_request); | |
1782 EXPECT_EQ(kUrl2, subframe_request->common_params().url); | |
1783 // First party for cookies url should be that of the main frame. | |
1784 EXPECT_EQ(kUrl1, subframe_request->info_for_test()->first_party_for_cookies); | |
1785 EXPECT_FALSE(subframe_request->info_for_test()->is_main_frame); | |
1786 EXPECT_TRUE(subframe_request->info_for_test()->parent_is_main_frame); | |
1787 EXPECT_EQ(kFirstNavRequestID, subframe_request->navigation_request_id()); | |
1788 | |
1789 RenderFrameHostManager* main_frame_manager = | |
1790 contents()->GetMainFrame()->frame_tree_node()->render_manager(); | |
1791 SendRequestNavigation(contents()->GetMainFrame()->frame_tree_node(), kUrl3); | |
1792 // Simulate a BeginNavigation IPC on the main frame. | |
1793 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl3); | |
1794 NavigationRequest* main_request = | |
1795 GetNavigationRequestForRenderFrameManager(main_frame_manager); | |
1796 ASSERT_TRUE(main_request); | |
1797 EXPECT_EQ(kUrl3, main_request->common_params().url); | |
1798 EXPECT_EQ(kUrl3, main_request->info_for_test()->first_party_for_cookies); | |
1799 EXPECT_TRUE(main_request->info_for_test()->is_main_frame); | |
1800 EXPECT_FALSE(main_request->info_for_test()->parent_is_main_frame); | |
1801 EXPECT_EQ(kFirstNavRequestID + 1, main_request->navigation_request_id()); | |
1802 } | |
1803 | |
1804 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that | |
1805 // RenderFrameHost is not modified when the navigation commits. | |
1806 TEST_F(RenderFrameHostManagerTest, | |
1807 BrowserSideNavigationRequestNavigationNoLiveRenderer) { | |
1808 const GURL kUrl("http://www.google.com/"); | |
1809 | |
1810 EnableBrowserSideNavigation(); | |
1811 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); | |
1812 RenderFrameHostManager* render_manager = | |
1813 main_test_rfh()->frame_tree_node()->render_manager(); | |
1814 SendRequestNavigation(main_test_rfh()->frame_tree_node(), kUrl); | |
1815 NavigationRequest* main_request = | |
1816 GetNavigationRequestForRenderFrameManager(render_manager); | |
1817 // A NavigationRequest should have been generated. | |
1818 EXPECT_TRUE(main_request != NULL); | |
1819 RenderFrameHostImpl* rfh = main_test_rfh(); | |
1820 | |
1821 // Now commit the same url. | |
1822 NavigationBeforeCommitInfo commit_info; | |
1823 commit_info.navigation_url = kUrl; | |
1824 commit_info.navigation_request_id = main_request->navigation_request_id(); | |
1825 render_manager->CommitNavigation(commit_info); | |
1826 main_request = GetNavigationRequestForRenderFrameManager(render_manager); | |
1827 | |
1828 // The main RFH should not have been changed, and the renderer should have | |
1829 // been initialized. | |
1830 EXPECT_EQ(rfh, main_test_rfh()); | |
1831 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); | |
1832 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); | |
1833 } | |
1834 | |
1835 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross | |
1836 // site navigation. | |
1837 TEST_F(RenderFrameHostManagerTest, | |
1838 BrowserSideNavigationCrossSiteNavigation) { | |
1839 const GURL kUrl1("http://www.chromium.org/"); | |
1840 const GURL kUrl2("http://www.google.com/"); | |
1841 | |
1842 // TODO(clamy): we should be enabling browser side navigations here | |
1843 // when CommitNavigation is properly implemented. | |
1844 // Navigate to the first page. | |
1845 contents()->NavigateAndCommit(kUrl1); | |
1846 RenderFrameHostImpl* rfh = main_test_rfh(); | |
1847 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); | |
1848 RenderFrameHostManager* render_manager = | |
1849 main_test_rfh()->frame_tree_node()->render_manager(); | |
1850 | |
1851 EnableBrowserSideNavigation(); | |
1852 // Navigate to a different site. | |
1853 SendRequestNavigation(main_test_rfh()->frame_tree_node(), kUrl2); | |
1854 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | |
1855 NavigationRequest* main_request = | |
1856 GetNavigationRequestForRenderFrameManager(render_manager); | |
1857 ASSERT_TRUE(main_request); | |
1858 | |
1859 NavigationBeforeCommitInfo commit_info; | |
1860 commit_info.navigation_url = kUrl2; | |
1861 commit_info.navigation_request_id = main_request->navigation_request_id(); | |
1862 render_manager->CommitNavigation(commit_info); | |
1863 EXPECT_NE(main_test_rfh(), rfh); | |
1864 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); | |
1865 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); | |
1866 } | |
1867 | |
1868 // PlzNavigate: Test that a navigation commit is ignored if another request has | |
1869 // been issued in the meantime. | |
1870 // TODO(carlosk): add checks to assert that the cancel call was sent to | |
1871 // ResourceDispatcherHost in the IO thread by extending | |
1872 // ResourceDispatcherHostDelegate (like in cross_site_transfer_browsertest.cc | |
1873 // and plugin_browsertest.cc). | |
1874 TEST_F(RenderFrameHostManagerTest, | |
1875 BrowserSideNavigationIgnoreStaleNavigationCommit) { | |
1876 const GURL kUrl0("http://www.wikipedia.org/"); | |
1877 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0); | |
1878 const GURL kUrl1("http://www.chromium.org/"); | |
1879 const GURL kUrl2("http://www.google.com/"); | |
1880 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2); | |
1881 | |
1882 // Initialization. | |
1883 contents()->NavigateAndCommit(kUrl0); | |
1884 RenderFrameHostManager* render_manager = | |
1885 main_test_rfh()->frame_tree_node()->render_manager(); | |
1886 EnableBrowserSideNavigation(); | |
1887 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); | |
1888 | |
1889 // Request navigation to the 1st URL and gather data. | |
1890 SendRequestNavigation(main_test_rfh()->frame_tree_node(), kUrl1); | |
1891 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); | |
1892 NavigationRequest* request1 = | |
1893 GetNavigationRequestForRenderFrameManager(render_manager); | |
1894 ASSERT_TRUE(request1); | |
1895 int64 request_id1 = request1->navigation_request_id(); | |
1896 | |
1897 // Request navigation to the 2nd URL and gather more data. | |
1898 SendRequestNavigation(main_test_rfh()->frame_tree_node(), kUrl2); | |
1899 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | |
1900 NavigationRequest* request2 = | |
1901 GetNavigationRequestForRenderFrameManager(render_manager); | |
1902 ASSERT_TRUE(request2); | |
1903 int64 request_id2 = request2->navigation_request_id(); | |
1904 EXPECT_NE(request_id1, request_id2); | |
1905 | |
1906 // Confirms that a stale commit is ignored by the RHFM. | |
1907 NavigationBeforeCommitInfo nbc_info; | |
1908 nbc_info.navigation_url = kUrl1; | |
1909 nbc_info.navigation_request_id = request_id1; | |
1910 render_manager->CommitNavigation(nbc_info); | |
1911 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); | |
1912 | |
1913 // Confirms that a valid, request-matching commit is correctly processed. | |
1914 nbc_info.navigation_url = kUrl2; | |
1915 nbc_info.navigation_request_id = request_id2; | |
1916 render_manager->CommitNavigation(nbc_info); | |
1917 EXPECT_EQ(kUrl2_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); | |
1918 } | |
1919 | |
1920 // PlzNavigate: Tests that the navigation histograms are correctly tracked both | |
1921 // when PlzNavigate is enabled and disabled, and also ignores in-tab renderer | |
1922 // initiated navigation for the non-enabled case. | |
1923 // Note: the related histogram, Navigation.TimeToURLJobStart, cannot be tracked | |
1924 // by this test as the IO thread is not running. | |
1925 TEST_F(RenderFrameHostManagerTest, BrowserSideNavigationHistogramTest) { | |
1926 const GURL kUrl0("http://www.google.com/"); | |
1927 const GURL kUrl1("http://www.chromium.org/"); | |
1928 base::HistogramTester histo_tester; | |
1929 | |
1930 // Performs a "normal" non-PlzNavigate navigation | |
1931 contents()->NavigateAndCommit(kUrl0); | |
1932 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 1); | |
1933 | |
1934 // Performs an in-tab renderer initiated navigation | |
1935 int32 new_page_id = 1 + contents()->GetMaxPageIDForSiteInstance( | |
1936 main_test_rfh()->GetSiteInstance()); | |
1937 main_test_rfh()->SendNavigate(new_page_id, kUrl0); | |
1938 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 1); | |
1939 | |
1940 // Performs a PlzNavigate navigation | |
1941 EnableBrowserSideNavigation(); | |
1942 contents()->NavigateAndCommit(kUrl1); | |
1943 histo_tester.ExpectTotalCount("Navigation.TimeToCommit", 2); | |
1944 } | |
1945 | |
1946 // PlzNavigate: Test that a reload navigation is properly signaled to the | |
1947 // renderer when the navigation can commit. | |
1948 TEST_F(RenderFrameHostManagerTest, BrowserSideNavigationReload) { | |
1949 const GURL kUrl("http://www.google.com/"); | |
1950 contents()->NavigateAndCommit(kUrl); | |
1951 | |
1952 EnableBrowserSideNavigation(); | |
1953 RenderFrameHostManager* render_manager = | |
1954 main_test_rfh()->frame_tree_node()->render_manager(); | |
1955 SendRequestNavigationWithParameters( | |
1956 main_test_rfh()->frame_tree_node(), kUrl, Referrer(), | |
1957 ui::PAGE_TRANSITION_LINK, FrameMsg_Navigate_Type::RELOAD); | |
1958 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); | |
1959 // A NavigationRequest should have been generated. | |
1960 NavigationRequest* main_request = | |
1961 GetNavigationRequestForRenderFrameManager(render_manager); | |
1962 ASSERT_TRUE(main_request != NULL); | |
1963 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD, | |
1964 main_request->common_params().navigation_type); | |
1965 int page_id = contents()->GetMaxPageIDForSiteInstance( | |
1966 main_test_rfh()->GetSiteInstance()) + 1; | |
1967 main_test_rfh()->SendNavigate(page_id, kUrl); | |
1968 | |
1969 // Now do a shift+reload. | |
1970 SendRequestNavigationWithParameters( | |
1971 main_test_rfh()->frame_tree_node(), | |
1972 kUrl, | |
1973 Referrer(), | |
1974 ui::PAGE_TRANSITION_LINK, | |
1975 FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE); | |
1976 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); | |
1977 // A NavigationRequest should have been generated. | |
1978 main_request = GetNavigationRequestForRenderFrameManager(render_manager); | |
1979 ASSERT_TRUE(main_request != NULL); | |
1980 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE, | |
1981 main_request->common_params().navigation_type); | |
1982 } | |
1983 | |
1984 } // namespace content | 1699 } // namespace content |
OLD | NEW |