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/browser_side_navigation_policy.h" |
10 #include "content/public/common/request_context_type.h" | 11 #include "content/public/common/request_context_type.h" |
11 #include "content/public/test/browser_test_utils.h" | 12 #include "content/public/test/browser_test_utils.h" |
12 #include "content/public/test/content_browser_test.h" | 13 #include "content/public/test/content_browser_test.h" |
13 #include "content/public/test/content_browser_test_utils.h" | 14 #include "content/public/test/content_browser_test_utils.h" |
14 #include "content/public/test/test_navigation_observer.h" | 15 #include "content/public/test/test_navigation_observer.h" |
15 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
16 #include "content/shell/browser/shell.h" | 17 #include "content/shell/browser/shell.h" |
17 #include "content/test/content_browser_test_utils_internal.h" | 18 #include "content/test/content_browser_test_utils_internal.h" |
18 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
19 #include "ui/base/page_transition_types.h" | 20 #include "ui/base/page_transition_types.h" |
(...skipping 11 matching lines...) Expand all Loading... |
31 const GURL& expected_start_url) | 32 const GURL& expected_start_url) |
32 : WebContentsObserver(web_contents), | 33 : WebContentsObserver(web_contents), |
33 handle_(nullptr), | 34 handle_(nullptr), |
34 has_committed_(false), | 35 has_committed_(false), |
35 is_error_(false), | 36 is_error_(false), |
36 is_main_frame_(false), | 37 is_main_frame_(false), |
37 is_parent_main_frame_(false), | 38 is_parent_main_frame_(false), |
38 is_renderer_initiated_(true), | 39 is_renderer_initiated_(true), |
39 is_same_page_(false), | 40 is_same_page_(false), |
40 was_redirected_(false), | 41 was_redirected_(false), |
| 42 has_finished_(false), |
41 frame_tree_node_id_(-1), | 43 frame_tree_node_id_(-1), |
42 page_transition_(ui::PAGE_TRANSITION_LINK), | 44 page_transition_(ui::PAGE_TRANSITION_LINK), |
43 expected_start_url_(expected_start_url), | 45 expected_start_url_(expected_start_url), |
44 net_error_code_(net::OK) {} | 46 net_error_code_(net::OK) {} |
45 | 47 |
46 void DidStartNavigation(NavigationHandle* navigation_handle) override { | 48 void DidStartNavigation(NavigationHandle* navigation_handle) override { |
47 if (handle_ || navigation_handle->GetURL() != expected_start_url_) | 49 if (handle_ || has_finished_ || |
| 50 navigation_handle->GetURL() != expected_start_url_) |
48 return; | 51 return; |
49 | 52 |
50 handle_ = navigation_handle; | 53 handle_ = navigation_handle; |
51 has_committed_ = false; | 54 has_committed_ = false; |
52 is_error_ = false; | 55 is_error_ = false; |
53 page_transition_ = ui::PAGE_TRANSITION_LINK; | 56 page_transition_ = ui::PAGE_TRANSITION_LINK; |
54 last_committed_url_ = GURL(); | 57 last_committed_url_ = GURL(); |
55 | 58 |
56 is_main_frame_ = navigation_handle->IsInMainFrame(); | 59 is_main_frame_ = navigation_handle->IsInMainFrame(); |
57 is_parent_main_frame_ = navigation_handle->IsParentMainFrame(); | 60 is_parent_main_frame_ = navigation_handle->IsParentMainFrame(); |
(...skipping 23 matching lines...) Expand all Loading... |
81 last_committed_url_ = navigation_handle->GetURL(); | 84 last_committed_url_ = navigation_handle->GetURL(); |
82 } else { | 85 } else { |
83 is_error_ = true; | 86 is_error_ = true; |
84 } | 87 } |
85 } else { | 88 } else { |
86 has_committed_ = false; | 89 has_committed_ = false; |
87 is_error_ = true; | 90 is_error_ = true; |
88 } | 91 } |
89 | 92 |
90 handle_ = nullptr; | 93 handle_ = nullptr; |
| 94 has_finished_ = true; |
91 } | 95 } |
92 | 96 |
93 bool has_committed() { return has_committed_; } | 97 bool has_committed() { return has_committed_; } |
94 bool is_error() { return is_error_; } | 98 bool is_error() { return is_error_; } |
95 bool is_main_frame() { return is_main_frame_; } | 99 bool is_main_frame() { return is_main_frame_; } |
96 bool is_parent_main_frame() { return is_parent_main_frame_; } | 100 bool is_parent_main_frame() { return is_parent_main_frame_; } |
97 bool is_renderer_initiated() { return is_renderer_initiated_; } | 101 bool is_renderer_initiated() { return is_renderer_initiated_; } |
98 bool is_same_page() { return is_same_page_; } | 102 bool is_same_page() { return is_same_page_; } |
99 bool was_redirected() { return was_redirected_; } | 103 bool was_redirected() { return was_redirected_; } |
100 int frame_tree_node_id() { return frame_tree_node_id_; } | 104 int frame_tree_node_id() { return frame_tree_node_id_; } |
101 | 105 |
102 const GURL& last_committed_url() { return last_committed_url_; } | 106 const GURL& last_committed_url() { return last_committed_url_; } |
103 | 107 |
104 ui::PageTransition page_transition() { return page_transition_; } | 108 ui::PageTransition page_transition() { return page_transition_; } |
105 | 109 |
106 net::Error net_error_code() { return net_error_code_; } | 110 net::Error net_error_code() { return net_error_code_; } |
107 | 111 |
108 private: | 112 private: |
109 // A reference to the NavigationHandle so this class will track only | 113 // A reference to the NavigationHandle so this class will track only one |
110 // one navigation at a time. It is set at DidStartNavigation and cleared | 114 // navigation. It is set at DidStartNavigation and cleared at |
111 // at DidFinishNavigation before the NavigationHandle is destroyed. | 115 // DidFinishNavigation before the NavigationHandle is destroyed. |
112 NavigationHandle* handle_; | 116 NavigationHandle* handle_; |
113 bool has_committed_; | 117 bool has_committed_; |
114 bool is_error_; | 118 bool is_error_; |
115 bool is_main_frame_; | 119 bool is_main_frame_; |
116 bool is_parent_main_frame_; | 120 bool is_parent_main_frame_; |
117 bool is_renderer_initiated_; | 121 bool is_renderer_initiated_; |
118 bool is_same_page_; | 122 bool is_same_page_; |
119 bool was_redirected_; | 123 bool was_redirected_; |
| 124 bool has_finished_; |
120 int frame_tree_node_id_; | 125 int frame_tree_node_id_; |
121 ui::PageTransition page_transition_; | 126 ui::PageTransition page_transition_; |
122 GURL expected_start_url_; | 127 GURL expected_start_url_; |
123 GURL last_committed_url_; | 128 GURL last_committed_url_; |
124 net::Error net_error_code_; | 129 net::Error net_error_code_; |
125 }; | 130 }; |
126 | 131 |
127 // A test NavigationThrottle that will return pre-determined checks and run | 132 // A test NavigationThrottle that will return pre-determined checks and run |
128 // callbacks when the various NavigationThrottle methods are called. It is | 133 // callbacks when the various NavigationThrottle methods are called. It is |
129 // not instantiated directly but through a TestNavigationThrottleInstaller. | 134 // not instantiated directly but through a TestNavigationThrottleInstaller. |
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 TestNavigationThrottleInstaller installer( | 921 TestNavigationThrottleInstaller installer( |
917 shell()->web_contents(), NavigationThrottle::BLOCK_REQUEST, | 922 shell()->web_contents(), NavigationThrottle::BLOCK_REQUEST, |
918 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); | 923 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); |
919 NavigationHandleObserver observer(shell()->web_contents(), kUrl); | 924 NavigationHandleObserver observer(shell()->web_contents(), kUrl); |
920 | 925 |
921 // Try to navigate to the url. The navigation should be canceled and the | 926 // Try to navigate to the url. The navigation should be canceled and the |
922 // NavigationHandle should have the right error code. | 927 // NavigationHandle should have the right error code. |
923 EXPECT_FALSE(NavigateToURL(shell(), kUrl)); | 928 EXPECT_FALSE(NavigateToURL(shell(), kUrl)); |
924 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, observer.net_error_code()); | 929 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, observer.net_error_code()); |
925 } | 930 } |
| 931 |
| 932 { |
| 933 // Set up a NavigationThrottle that will block the navigation in |
| 934 // WillRedirectRequest. |
| 935 TestNavigationThrottleInstaller installer( |
| 936 shell()->web_contents(), NavigationThrottle::PROCEED, |
| 937 NavigationThrottle::BLOCK_REQUEST, NavigationThrottle::PROCEED); |
| 938 NavigationHandleObserver observer(shell()->web_contents(), kRedirectingUrl); |
| 939 |
| 940 // Try to navigate to the url. The navigation should be canceled and the |
| 941 // NavigationHandle should have the right error code. |
| 942 EXPECT_FALSE(NavigateToURL(shell(), kRedirectingUrl)); |
| 943 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, observer.net_error_code()); |
| 944 } |
926 } | 945 } |
927 | 946 |
928 // Specialized test that verifies the NavigationHandle gets the HTTPS upgraded | 947 // Specialized test that verifies the NavigationHandle gets the HTTPS upgraded |
929 // URL from the very beginning of the navigation. | 948 // URL from the very beginning of the navigation. |
930 class NavigationHandleImplHttpsUpgradeBrowserTest | 949 class NavigationHandleImplHttpsUpgradeBrowserTest |
931 : public NavigationHandleImplBrowserTest { | 950 : public NavigationHandleImplBrowserTest { |
932 public: | 951 public: |
933 void CheckHttpsUpgradedIframeNavigation(const GURL& start_url, | 952 void CheckHttpsUpgradedIframeNavigation(const GURL& start_url, |
934 const GURL& iframe_secure_url) { | 953 const GURL& iframe_secure_url) { |
935 ASSERT_TRUE(start_url.SchemeIs(url::kHttpScheme)); | 954 ASSERT_TRUE(start_url.SchemeIs(url::kHttpScheme)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 shell()->web_contents(), NavigationThrottle::PROCEED, | 1069 shell()->web_contents(), NavigationThrottle::PROCEED, |
1051 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); | 1070 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); |
1052 NavigationHandleObserver observer(shell()->web_contents(), url); | 1071 NavigationHandleObserver observer(shell()->web_contents(), url); |
1053 EXPECT_TRUE(NavigateToURL(shell(), url)); | 1072 EXPECT_TRUE(NavigateToURL(shell(), url)); |
1054 EXPECT_EQ(1, installer.will_start_called()); | 1073 EXPECT_EQ(1, installer.will_start_called()); |
1055 EXPECT_EQ(1, installer.will_process_called()); | 1074 EXPECT_EQ(1, installer.will_process_called()); |
1056 EXPECT_FALSE(observer.is_same_page()); | 1075 EXPECT_FALSE(observer.is_same_page()); |
1057 } | 1076 } |
1058 } | 1077 } |
1059 | 1078 |
| 1079 // Record and list the navigations that are started and finished. |
| 1080 class NavigationLogger : public WebContentsObserver { |
| 1081 public: |
| 1082 NavigationLogger(WebContents* web_contents) |
| 1083 : WebContentsObserver(web_contents) {} |
| 1084 |
| 1085 void DidStartNavigation(NavigationHandle* navigation_handle) override { |
| 1086 started_navigation_urls_.push_back(navigation_handle->GetURL()); |
| 1087 } |
| 1088 |
| 1089 void DidFinishNavigation(NavigationHandle* navigation_handle) override { |
| 1090 finished_navigation_urls_.push_back(navigation_handle->GetURL()); |
| 1091 } |
| 1092 |
| 1093 const std::vector<GURL>& started_navigation_urls() const { |
| 1094 return started_navigation_urls_; |
| 1095 } |
| 1096 const std::vector<GURL>& finished_navigation_urls() const { |
| 1097 return finished_navigation_urls_; |
| 1098 } |
| 1099 |
| 1100 private: |
| 1101 std::vector<GURL> started_navigation_urls_; |
| 1102 std::vector<GURL> finished_navigation_urls_; |
| 1103 }; |
| 1104 |
| 1105 // There was a bug without PlzNavigate that happened when a navigation was |
| 1106 // blocked after a redirect. Blink didn't know about the redirect and tried |
| 1107 // to commit an error page to the post-redirect URL. The result was that the |
| 1108 // NavigationHandle was not found on the browser-side and a new NavigationHandle |
| 1109 // created for committing the error page. This test makes sure that only one |
| 1110 // NavigationHandle is used for committing the error page. |
| 1111 // See crbug.com/695421 |
| 1112 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, BlockedOnRedirect) { |
| 1113 const GURL kUrl = embedded_test_server()->GetURL("/title1.html"); |
| 1114 const GURL kRedirectingUrl = |
| 1115 embedded_test_server()->GetURL("/server-redirect?" + kUrl.spec()); |
| 1116 |
| 1117 // Set up a NavigationThrottle that will block the navigation in |
| 1118 // WillRedirectRequest. |
| 1119 TestNavigationThrottleInstaller installer( |
| 1120 shell()->web_contents(), NavigationThrottle::PROCEED, |
| 1121 NavigationThrottle::BLOCK_REQUEST, NavigationThrottle::PROCEED); |
| 1122 NavigationHandleObserver observer(shell()->web_contents(), kRedirectingUrl); |
| 1123 NavigationLogger logger(shell()->web_contents()); |
| 1124 |
| 1125 // Try to navigate to the url. The navigation should be canceled and the |
| 1126 // NavigationHandle should have the right error code. |
| 1127 EXPECT_FALSE(NavigateToURL(shell(), kRedirectingUrl)); |
| 1128 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, observer.net_error_code()); |
| 1129 |
| 1130 // Only one navigation is expected to happen. |
| 1131 std::vector<GURL> started_navigation = {kRedirectingUrl}; |
| 1132 EXPECT_EQ(started_navigation, logger.started_navigation_urls()); |
| 1133 |
| 1134 std::vector<GURL> finished_navigation; |
| 1135 if (IsBrowserSideNavigationEnabled()) |
| 1136 finished_navigation = {kUrl}; |
| 1137 else |
| 1138 finished_navigation = {kRedirectingUrl}; |
| 1139 EXPECT_EQ(finished_navigation, logger.finished_navigation_urls()); |
| 1140 } |
| 1141 |
1060 } // namespace content | 1142 } // namespace content |
OLD | NEW |