OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 #include "content/browser/frame_host/navigation_handle_impl.h" | 6 #include "content/browser/frame_host/navigation_handle_impl.h" |
7 #include "content/browser/web_contents/web_contents_impl.h" | 7 #include "content/browser/web_contents/web_contents_impl.h" |
8 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
9 #include "content/public/browser/web_contents_observer.h" | 9 #include "content/public/browser/web_contents_observer.h" |
10 #include "content/public/common/request_context_type.h" | 10 #include "content/public/common/request_context_type.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 handle_(nullptr), | 33 handle_(nullptr), |
34 has_committed_(false), | 34 has_committed_(false), |
35 is_error_(false), | 35 is_error_(false), |
36 is_main_frame_(false), | 36 is_main_frame_(false), |
37 is_parent_main_frame_(false), | 37 is_parent_main_frame_(false), |
38 is_renderer_initiated_(true), | 38 is_renderer_initiated_(true), |
39 is_same_page_(false), | 39 is_same_page_(false), |
40 was_redirected_(false), | 40 was_redirected_(false), |
41 frame_tree_node_id_(-1), | 41 frame_tree_node_id_(-1), |
42 page_transition_(ui::PAGE_TRANSITION_LINK), | 42 page_transition_(ui::PAGE_TRANSITION_LINK), |
43 expected_start_url_(expected_start_url) {} | 43 expected_start_url_(expected_start_url), |
44 net_error_code_(net::OK) {} | |
44 | 45 |
45 void DidStartNavigation(NavigationHandle* navigation_handle) override { | 46 void DidStartNavigation(NavigationHandle* navigation_handle) override { |
46 if (handle_ || navigation_handle->GetURL() != expected_start_url_) | 47 if (handle_ || navigation_handle->GetURL() != expected_start_url_) |
47 return; | 48 return; |
48 | 49 |
49 handle_ = navigation_handle; | 50 handle_ = navigation_handle; |
50 has_committed_ = false; | 51 has_committed_ = false; |
51 is_error_ = false; | 52 is_error_ = false; |
52 page_transition_ = ui::PAGE_TRANSITION_LINK; | 53 page_transition_ = ui::PAGE_TRANSITION_LINK; |
53 last_committed_url_ = GURL(); | 54 last_committed_url_ = GURL(); |
(...skipping 10 matching lines...) Expand all Loading... | |
64 if (navigation_handle != handle_) | 65 if (navigation_handle != handle_) |
65 return; | 66 return; |
66 | 67 |
67 DCHECK_EQ(is_main_frame_, navigation_handle->IsInMainFrame()); | 68 DCHECK_EQ(is_main_frame_, navigation_handle->IsInMainFrame()); |
68 DCHECK_EQ(is_parent_main_frame_, navigation_handle->IsParentMainFrame()); | 69 DCHECK_EQ(is_parent_main_frame_, navigation_handle->IsParentMainFrame()); |
69 DCHECK_EQ(is_same_page_, navigation_handle->IsSamePage()); | 70 DCHECK_EQ(is_same_page_, navigation_handle->IsSamePage()); |
70 DCHECK_EQ(is_renderer_initiated_, navigation_handle->IsRendererInitiated()); | 71 DCHECK_EQ(is_renderer_initiated_, navigation_handle->IsRendererInitiated()); |
71 DCHECK_EQ(frame_tree_node_id_, navigation_handle->GetFrameTreeNodeId()); | 72 DCHECK_EQ(frame_tree_node_id_, navigation_handle->GetFrameTreeNodeId()); |
72 | 73 |
73 was_redirected_ = navigation_handle->WasServerRedirect(); | 74 was_redirected_ = navigation_handle->WasServerRedirect(); |
75 net_error_code_ = navigation_handle->GetNetErrorCode(); | |
74 | 76 |
75 if (navigation_handle->HasCommitted()) { | 77 if (navigation_handle->HasCommitted()) { |
76 has_committed_ = true; | 78 has_committed_ = true; |
77 if (!navigation_handle->IsErrorPage()) { | 79 if (!navigation_handle->IsErrorPage()) { |
78 page_transition_ = navigation_handle->GetPageTransition(); | 80 page_transition_ = navigation_handle->GetPageTransition(); |
79 last_committed_url_ = navigation_handle->GetURL(); | 81 last_committed_url_ = navigation_handle->GetURL(); |
80 } else { | 82 } else { |
81 is_error_ = true; | 83 is_error_ = true; |
82 } | 84 } |
83 } else { | 85 } else { |
(...skipping 10 matching lines...) Expand all Loading... | |
94 bool is_parent_main_frame() { return is_parent_main_frame_; } | 96 bool is_parent_main_frame() { return is_parent_main_frame_; } |
95 bool is_renderer_initiated() { return is_renderer_initiated_; } | 97 bool is_renderer_initiated() { return is_renderer_initiated_; } |
96 bool is_same_page() { return is_same_page_; } | 98 bool is_same_page() { return is_same_page_; } |
97 bool was_redirected() { return was_redirected_; } | 99 bool was_redirected() { return was_redirected_; } |
98 int frame_tree_node_id() { return frame_tree_node_id_; } | 100 int frame_tree_node_id() { return frame_tree_node_id_; } |
99 | 101 |
100 const GURL& last_committed_url() { return last_committed_url_; } | 102 const GURL& last_committed_url() { return last_committed_url_; } |
101 | 103 |
102 ui::PageTransition page_transition() { return page_transition_; } | 104 ui::PageTransition page_transition() { return page_transition_; } |
103 | 105 |
106 net::Error net_error_code() { return net_error_code_; } | |
107 | |
104 private: | 108 private: |
105 // A reference to the NavigationHandle so this class will track only | 109 // A reference to the NavigationHandle so this class will track only |
106 // one navigation at a time. It is set at DidStartNavigation and cleared | 110 // one navigation at a time. It is set at DidStartNavigation and cleared |
107 // at DidFinishNavigation before the NavigationHandle is destroyed. | 111 // at DidFinishNavigation before the NavigationHandle is destroyed. |
108 NavigationHandle* handle_; | 112 NavigationHandle* handle_; |
109 bool has_committed_; | 113 bool has_committed_; |
110 bool is_error_; | 114 bool is_error_; |
111 bool is_main_frame_; | 115 bool is_main_frame_; |
112 bool is_parent_main_frame_; | 116 bool is_parent_main_frame_; |
113 bool is_renderer_initiated_; | 117 bool is_renderer_initiated_; |
114 bool is_same_page_; | 118 bool is_same_page_; |
115 bool was_redirected_; | 119 bool was_redirected_; |
116 int frame_tree_node_id_; | 120 int frame_tree_node_id_; |
117 ui::PageTransition page_transition_; | 121 ui::PageTransition page_transition_; |
118 GURL expected_start_url_; | 122 GURL expected_start_url_; |
119 GURL last_committed_url_; | 123 GURL last_committed_url_; |
124 net::Error net_error_code_; | |
120 }; | 125 }; |
121 | 126 |
122 // A test NavigationThrottle that will return pre-determined checks and run | 127 // A test NavigationThrottle that will return pre-determined checks and run |
123 // callbacks when the various NavigationThrottle methods are called. It is | 128 // callbacks when the various NavigationThrottle methods are called. It is |
124 // not instantiated directly but through a TestNavigationThrottleInstaller. | 129 // not instantiated directly but through a TestNavigationThrottleInstaller. |
125 class TestNavigationThrottle : public NavigationThrottle { | 130 class TestNavigationThrottle : public NavigationThrottle { |
126 public: | 131 public: |
127 TestNavigationThrottle( | 132 TestNavigationThrottle( |
128 NavigationHandle* handle, | 133 NavigationHandle* handle, |
129 NavigationThrottle::ThrottleCheckResult will_start_result, | 134 NavigationThrottle::ThrottleCheckResult will_start_result, |
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
840 EXPECT_EQ(post_url, url_recorder.urls().back()); | 845 EXPECT_EQ(post_url, url_recorder.urls().back()); |
841 EXPECT_EQ(2ul, url_recorder.urls().size()); | 846 EXPECT_EQ(2ul, url_recorder.urls().size()); |
842 EXPECT_EQ(REQUEST_CONTEXT_TYPE_FORM, | 847 EXPECT_EQ(REQUEST_CONTEXT_TYPE_FORM, |
843 installer.navigation_throttle()->request_context_type()); | 848 installer.navigation_throttle()->request_context_type()); |
844 | 849 |
845 // Finishes the last navigation. | 850 // Finishes the last navigation. |
846 post_manager.WaitForNavigationFinished(); | 851 post_manager.WaitForNavigationFinished(); |
847 EXPECT_FALSE(installer.navigation_throttle()); | 852 EXPECT_FALSE(installer.navigation_throttle()); |
848 } | 853 } |
849 | 854 |
855 // Checks that the error code is properly set on the NavigationHandle when a | |
856 // NavigationThrottle cancels. | |
857 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, | |
858 ErrorCodeOnThrottleCancelNavigation) { | |
859 const GURL kUrl = embedded_test_server()->GetURL("/title1.html"); | |
860 const GURL kRedirectingUrl = | |
861 embedded_test_server()->GetURL("/server-redirect?" + kUrl.spec()); | |
862 | |
863 { | |
864 // Set up a NavigationThrottle that will cancel the navigation in | |
865 // WillStartRequest. | |
866 TestNavigationThrottleInstaller installer( | |
867 shell()->web_contents(), NavigationThrottle::CANCEL_AND_IGNORE, | |
868 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); | |
869 NavigationHandleObserver observer(shell()->web_contents(), kUrl); | |
870 | |
871 // Try to navigate to the url. The navigation should be canceled and the | |
872 // NavigationHandle should have the right error code. | |
873 EXPECT_FALSE(NavigateToURL(shell(), kUrl)); | |
874 EXPECT_EQ(net::ERR_ABORTED, observer.net_error_code()); | |
875 } | |
876 | |
877 { | |
878 // Set up a NavigationThrottle that will cancel the navigation in | |
879 // WillRedirectRequest. | |
880 TestNavigationThrottleInstaller installer( | |
881 shell()->web_contents(), NavigationThrottle::PROCEED, | |
882 NavigationThrottle::CANCEL_AND_IGNORE, NavigationThrottle::PROCEED); | |
883 NavigationHandleObserver observer(shell()->web_contents(), kRedirectingUrl); | |
884 | |
885 // Try to navigate to the url. The navigation should be canceled and the | |
886 // NavigationHandle should have the right error code. | |
887 EXPECT_FALSE(NavigateToURL(shell(), kRedirectingUrl)); | |
888 EXPECT_EQ(net::ERR_ABORTED, observer.net_error_code()); | |
889 } | |
890 | |
891 { | |
892 // Set up a NavigationThrottle that will cancel the navigation in | |
893 // WillProcessResponse. | |
894 TestNavigationThrottleInstaller installer( | |
895 shell()->web_contents(), NavigationThrottle::PROCEED, | |
896 NavigationThrottle::PROCEED, NavigationThrottle::CANCEL_AND_IGNORE); | |
897 NavigationHandleObserver observer(shell()->web_contents(), kUrl); | |
898 | |
899 // Try to navigate to the url. The navigation should be canceled and the | |
900 // NavigationHandle should have the right error code. | |
901 EXPECT_FALSE(NavigateToURL(shell(), kUrl)); | |
902 EXPECT_EQ(net::ERR_ABORTED, observer.net_error_code()); | |
903 } | |
904 | |
905 { | |
906 // Set up a NavigationThrottle that will block the navigation in | |
907 // WillStartRequest. | |
908 TestNavigationThrottleInstaller installer( | |
909 shell()->web_contents(), NavigationThrottle::BLOCK_REQUEST, | |
nasko
2017/02/07 21:25:25
I would've expected the cases will include BLOCK_R
clamy
2017/02/08 16:34:16
We can only use NavigationThrottle::BLOCK_REQUEST
| |
910 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); | |
911 NavigationHandleObserver observer(shell()->web_contents(), kUrl); | |
912 | |
913 // Try to navigate to the url. The navigation should be canceled and the | |
914 // NavigationHandle should have the right error code. | |
915 EXPECT_FALSE(NavigateToURL(shell(), kUrl)); | |
916 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, observer.net_error_code()); | |
917 } | |
918 } | |
919 | |
850 // Specialized test that verifies the NavigationHandle gets the HTTPS upgraded | 920 // Specialized test that verifies the NavigationHandle gets the HTTPS upgraded |
851 // URL from the very beginning of the navigation. | 921 // URL from the very beginning of the navigation. |
852 class NavigationHandleImplHttpsUpgradeBrowserTest | 922 class NavigationHandleImplHttpsUpgradeBrowserTest |
853 : public NavigationHandleImplBrowserTest { | 923 : public NavigationHandleImplBrowserTest { |
854 public: | 924 public: |
855 void CheckHttpsUpgradedIframeNavigation(const GURL& start_url, | 925 void CheckHttpsUpgradedIframeNavigation(const GURL& start_url, |
856 const GURL& iframe_secure_url) { | 926 const GURL& iframe_secure_url) { |
857 ASSERT_TRUE(start_url.SchemeIs(url::kHttpScheme)); | 927 ASSERT_TRUE(start_url.SchemeIs(url::kHttpScheme)); |
858 ASSERT_TRUE(iframe_secure_url.SchemeIs(url::kHttpsScheme)); | 928 ASSERT_TRUE(iframe_secure_url.SchemeIs(url::kHttpsScheme)); |
859 | 929 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
904 IN_PROC_BROWSER_TEST_F(NavigationHandleImplHttpsUpgradeBrowserTest, | 974 IN_PROC_BROWSER_TEST_F(NavigationHandleImplHttpsUpgradeBrowserTest, |
905 StartUrlIsHttpsUpgradedCrossSite) { | 975 StartUrlIsHttpsUpgradedCrossSite) { |
906 GURL start_url( | 976 GURL start_url( |
907 embedded_test_server()->GetURL("/https_upgrade_cross_site.html")); | 977 embedded_test_server()->GetURL("/https_upgrade_cross_site.html")); |
908 GURL cross_site_iframe_secure_url("https://other.com/title1.html"); | 978 GURL cross_site_iframe_secure_url("https://other.com/title1.html"); |
909 | 979 |
910 CheckHttpsUpgradedIframeNavigation(start_url, cross_site_iframe_secure_url); | 980 CheckHttpsUpgradedIframeNavigation(start_url, cross_site_iframe_secure_url); |
911 } | 981 } |
912 | 982 |
913 } // namespace content | 983 } // namespace content |
OLD | NEW |