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 "content/browser/frame_host/navigation_handle_impl.h" | 5 #include "content/browser/frame_host/navigation_handle_impl.h" |
6 #include "content/browser/web_contents/web_contents_impl.h" | 6 #include "content/browser/web_contents/web_contents_impl.h" |
7 #include "content/public/browser/web_contents.h" | 7 #include "content/public/browser/web_contents.h" |
8 #include "content/public/browser/web_contents_observer.h" | 8 #include "content/public/browser/web_contents_observer.h" |
9 #include "content/public/test/browser_test_utils.h" | 9 #include "content/public/test/browser_test_utils.h" |
10 #include "content/public/test/content_browser_test.h" | 10 #include "content/public/test/content_browser_test.h" |
11 #include "content/public/test/content_browser_test_utils.h" | 11 #include "content/public/test/content_browser_test_utils.h" |
12 #include "content/public/test/test_navigation_observer.h" | 12 #include "content/public/test/test_navigation_observer.h" |
13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
14 #include "content/shell/browser/shell.h" | 14 #include "content/shell/browser/shell.h" |
15 #include "content/test/content_browser_test_utils_internal.h" | 15 #include "content/test/content_browser_test_utils_internal.h" |
16 #include "net/dns/mock_host_resolver.h" | 16 #include "net/dns/mock_host_resolver.h" |
17 #include "ui/base/page_transition_types.h" | 17 #include "ui/base/page_transition_types.h" |
18 #include "url/url_constants.h" | 18 #include "url/url_constants.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Gathers data from the NavigationHandle assigned to navigations that start | |
25 // with the expected URL. Overlapping navigations are ignored; create new | |
26 // instances as needed. | |
24 class NavigationHandleObserver : public WebContentsObserver { | 27 class NavigationHandleObserver : public WebContentsObserver { |
25 public: | 28 public: |
26 NavigationHandleObserver(WebContents* web_contents, const GURL& expected_url) | 29 NavigationHandleObserver(WebContents* web_contents, |
30 const GURL& expected_start_url) | |
27 : WebContentsObserver(web_contents), | 31 : WebContentsObserver(web_contents), |
28 handle_(nullptr), | 32 handle_(nullptr), |
29 has_committed_(false), | 33 has_committed_(false), |
30 is_error_(false), | 34 is_error_(false), |
31 is_main_frame_(false), | 35 is_main_frame_(false), |
32 is_parent_main_frame_(false), | 36 is_parent_main_frame_(false), |
33 is_renderer_initiated_(true), | 37 is_renderer_initiated_(true), |
34 is_synchronous_(false), | 38 is_synchronous_(false), |
35 is_srcdoc_(false), | 39 is_srcdoc_(false), |
36 was_redirected_(false), | 40 was_redirected_(false), |
37 frame_tree_node_id_(-1), | 41 frame_tree_node_id_(-1), |
38 page_transition_(ui::PAGE_TRANSITION_LINK), | 42 page_transition_(ui::PAGE_TRANSITION_LINK), |
39 expected_url_(expected_url) {} | 43 expected_start_url_(expected_start_url) {} |
40 | 44 |
41 void DidStartNavigation(NavigationHandle* navigation_handle) override { | 45 void DidStartNavigation(NavigationHandle* navigation_handle) override { |
42 if (handle_ || navigation_handle->GetURL() != expected_url_) | 46 if (handle_ || navigation_handle->GetURL() != expected_start_url_) { |
47 last_unmatched_start_url_ = navigation_handle->GetURL(); | |
43 return; | 48 return; |
49 } | |
44 | 50 |
45 handle_ = navigation_handle; | 51 handle_ = navigation_handle; |
46 has_committed_ = false; | 52 has_committed_ = false; |
47 is_error_ = false; | 53 is_error_ = false; |
48 page_transition_ = ui::PAGE_TRANSITION_LINK; | 54 page_transition_ = ui::PAGE_TRANSITION_LINK; |
49 last_committed_url_ = GURL(); | 55 last_committed_url_ = GURL(); |
50 | 56 |
51 is_main_frame_ = navigation_handle->IsInMainFrame(); | 57 is_main_frame_ = navigation_handle->IsInMainFrame(); |
52 is_parent_main_frame_ = navigation_handle->IsParentMainFrame(); | 58 is_parent_main_frame_ = navigation_handle->IsParentMainFrame(); |
53 is_renderer_initiated_ = navigation_handle->IsRendererInitiated(); | 59 is_renderer_initiated_ = navigation_handle->IsRendererInitiated(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 bool has_committed() { return has_committed_; } | 95 bool has_committed() { return has_committed_; } |
90 bool is_error() { return is_error_; } | 96 bool is_error() { return is_error_; } |
91 bool is_main_frame() { return is_main_frame_; } | 97 bool is_main_frame() { return is_main_frame_; } |
92 bool is_parent_main_frame() { return is_parent_main_frame_; } | 98 bool is_parent_main_frame() { return is_parent_main_frame_; } |
93 bool is_renderer_initiated() { return is_renderer_initiated_; } | 99 bool is_renderer_initiated() { return is_renderer_initiated_; } |
94 bool is_synchronous() { return is_synchronous_; } | 100 bool is_synchronous() { return is_synchronous_; } |
95 bool is_srcdoc() { return is_srcdoc_; } | 101 bool is_srcdoc() { return is_srcdoc_; } |
96 bool was_redirected() { return was_redirected_; } | 102 bool was_redirected() { return was_redirected_; } |
97 int frame_tree_node_id() { return frame_tree_node_id_; } | 103 int frame_tree_node_id() { return frame_tree_node_id_; } |
98 | 104 |
105 const GURL& last_unmatched_start_url() { return last_unmatched_start_url_; } | |
99 const GURL& last_committed_url() { return last_committed_url_; } | 106 const GURL& last_committed_url() { return last_committed_url_; } |
100 | 107 |
101 ui::PageTransition page_transition() { return page_transition_; } | 108 ui::PageTransition page_transition() { return page_transition_; } |
102 | 109 |
103 private: | 110 private: |
104 // A reference to the NavigationHandle so this class will track only | 111 // A reference to the NavigationHandle so this class will track only |
105 // one navigation at a time. It is set at DidStartNavigation and cleared | 112 // one navigation at a time. It is set at DidStartNavigation and cleared |
106 // at DidFinishNavigation before the NavigationHandle is destroyed. | 113 // at DidFinishNavigation before the NavigationHandle is destroyed. |
107 NavigationHandle* handle_; | 114 NavigationHandle* handle_; |
108 bool has_committed_; | 115 bool has_committed_; |
109 bool is_error_; | 116 bool is_error_; |
110 bool is_main_frame_; | 117 bool is_main_frame_; |
111 bool is_parent_main_frame_; | 118 bool is_parent_main_frame_; |
112 bool is_renderer_initiated_; | 119 bool is_renderer_initiated_; |
113 bool is_synchronous_; | 120 bool is_synchronous_; |
114 bool is_srcdoc_; | 121 bool is_srcdoc_; |
115 bool was_redirected_; | 122 bool was_redirected_; |
116 int frame_tree_node_id_; | 123 int frame_tree_node_id_; |
117 ui::PageTransition page_transition_; | 124 ui::PageTransition page_transition_; |
118 GURL expected_url_; | 125 GURL expected_start_url_; |
126 GURL last_unmatched_start_url_; | |
119 GURL last_committed_url_; | 127 GURL last_committed_url_; |
120 }; | 128 }; |
121 | 129 |
122 // A test NavigationThrottle that will return pre-determined checks and run | 130 // A test NavigationThrottle that will return pre-determined checks and run |
123 // callbacks when the various NavigationThrottle methods are called. | 131 // callbacks when the various NavigationThrottle methods are called. This is |
132 // generally not instantiated directly but through a | |
133 // TestNavigationThrottleInstaller. | |
124 class TestNavigationThrottle : public NavigationThrottle { | 134 class TestNavigationThrottle : public NavigationThrottle { |
125 public: | 135 public: |
126 TestNavigationThrottle( | 136 TestNavigationThrottle( |
127 NavigationHandle* handle, | 137 NavigationHandle* handle, |
128 NavigationThrottle::ThrottleCheckResult will_start_result, | 138 NavigationThrottle::ThrottleCheckResult will_start_result, |
129 NavigationThrottle::ThrottleCheckResult will_redirect_result, | 139 NavigationThrottle::ThrottleCheckResult will_redirect_result, |
130 NavigationThrottle::ThrottleCheckResult will_process_result, | 140 NavigationThrottle::ThrottleCheckResult will_process_result, |
131 base::Closure did_call_will_start, | 141 base::Closure did_call_will_start, |
132 base::Closure did_call_will_redirect, | 142 base::Closure did_call_will_redirect, |
133 base::Closure did_call_will_process) | 143 base::Closure did_call_will_process) |
(...skipping 29 matching lines...) Expand all Loading... | |
163 | 173 |
164 NavigationThrottle::ThrottleCheckResult will_start_result_; | 174 NavigationThrottle::ThrottleCheckResult will_start_result_; |
165 NavigationThrottle::ThrottleCheckResult will_redirect_result_; | 175 NavigationThrottle::ThrottleCheckResult will_redirect_result_; |
166 NavigationThrottle::ThrottleCheckResult will_process_result_; | 176 NavigationThrottle::ThrottleCheckResult will_process_result_; |
167 base::Closure did_call_will_start_; | 177 base::Closure did_call_will_start_; |
168 base::Closure did_call_will_redirect_; | 178 base::Closure did_call_will_redirect_; |
169 base::Closure did_call_will_process_; | 179 base::Closure did_call_will_process_; |
170 }; | 180 }; |
171 | 181 |
172 // Install a TestNavigationThrottle on all requests and allows waiting for | 182 // Install a TestNavigationThrottle on all requests and allows waiting for |
173 // various NavigationThrottle related events. | 183 // various NavigationThrottle related events. Waiting is only allows for the |
184 // immediately next navigation; create new instances as needed. | |
174 class TestNavigationThrottleInstaller : public WebContentsObserver { | 185 class TestNavigationThrottleInstaller : public WebContentsObserver { |
175 public: | 186 public: |
176 TestNavigationThrottleInstaller( | 187 TestNavigationThrottleInstaller( |
177 WebContents* web_contents, | 188 WebContents* web_contents, |
178 NavigationThrottle::ThrottleCheckResult will_start_result, | 189 NavigationThrottle::ThrottleCheckResult will_start_result, |
179 NavigationThrottle::ThrottleCheckResult will_redirect_result, | 190 NavigationThrottle::ThrottleCheckResult will_redirect_result, |
180 NavigationThrottle::ThrottleCheckResult will_process_result) | 191 NavigationThrottle::ThrottleCheckResult will_process_result) |
181 : WebContentsObserver(web_contents), | 192 : WebContentsObserver(web_contents), |
182 will_start_result_(will_start_result), | 193 will_start_result_(will_start_result), |
183 will_redirect_result_(will_redirect_result), | 194 will_redirect_result_(will_redirect_result), |
184 will_process_result_(will_process_result), | 195 will_process_result_(will_process_result), |
185 will_start_called_(0), | 196 will_start_called_(0), |
186 will_redirect_called_(0), | 197 will_redirect_called_(0), |
187 will_process_called_(0), | 198 will_process_called_(0), |
188 navigation_throttle_(nullptr) {} | 199 navigation_throttle_(nullptr) {} |
189 ~TestNavigationThrottleInstaller() override{}; | 200 ~TestNavigationThrottleInstaller() override{}; |
190 | 201 |
191 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } | 202 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } |
192 | 203 |
193 void WaitForThrottleWillStart() { | 204 void WaitForThrottleWillStart() { |
194 if (will_start_called_) | 205 CHECK(!will_start_called_); |
195 return; | |
196 will_start_loop_runner_ = new MessageLoopRunner(); | 206 will_start_loop_runner_ = new MessageLoopRunner(); |
197 will_start_loop_runner_->Run(); | 207 will_start_loop_runner_->Run(); |
198 will_start_loop_runner_ = nullptr; | 208 will_start_loop_runner_ = nullptr; |
199 } | 209 } |
200 | 210 |
201 void WaitForThrottleWillRedirect() { | 211 void WaitForThrottleWillRedirect() { |
202 if (will_redirect_called_) | 212 CHECK(!will_redirect_called_); |
203 return; | |
204 will_redirect_loop_runner_ = new MessageLoopRunner(); | 213 will_redirect_loop_runner_ = new MessageLoopRunner(); |
205 will_redirect_loop_runner_->Run(); | 214 will_redirect_loop_runner_->Run(); |
206 will_redirect_loop_runner_ = nullptr; | 215 will_redirect_loop_runner_ = nullptr; |
207 } | 216 } |
208 | 217 |
209 void WaitForThrottleWillProcess() { | 218 void WaitForThrottleWillProcess() { |
210 if (will_process_called_) | 219 CHECK(!will_process_called_); |
211 return; | |
212 will_process_loop_runner_ = new MessageLoopRunner(); | 220 will_process_loop_runner_ = new MessageLoopRunner(); |
213 will_process_loop_runner_->Run(); | 221 will_process_loop_runner_->Run(); |
214 will_process_loop_runner_ = nullptr; | 222 will_process_loop_runner_ = nullptr; |
215 } | 223 } |
216 | 224 |
217 int will_start_called() { return will_start_called_; } | 225 int will_start_called() { return will_start_called_; } |
218 int will_redirect_called() { return will_redirect_called_; } | 226 int will_redirect_called() { return will_redirect_called_; } |
219 int will_process_called() { return will_process_called_; } | 227 int will_process_called() { return will_process_called_; } |
220 | 228 |
221 private: | 229 private: |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
625 // Wait for the end of the navigation. | 633 // Wait for the end of the navigation. |
626 navigation_observer.Wait(); | 634 navigation_observer.Wait(); |
627 | 635 |
628 EXPECT_TRUE(observer.has_committed()); | 636 EXPECT_TRUE(observer.has_committed()); |
629 EXPECT_TRUE(observer.was_redirected()); | 637 EXPECT_TRUE(observer.was_redirected()); |
630 EXPECT_FALSE(observer.is_error()); | 638 EXPECT_FALSE(observer.is_error()); |
631 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), | 639 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), |
632 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); | 640 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); |
633 } | 641 } |
634 | 642 |
643 // Ensure that the URL received with the provisional load start notification is | |
644 // already upgraded to HTTPS. | |
645 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, | |
646 ProvisionalLoadStartUrlIsHttpsUpgraded) { | |
647 GURL start_url(embedded_test_server()->GetURL( | |
648 "/navigation_handle_https_upgrade_test.html")); | |
649 ASSERT_TRUE(start_url.SchemeIs(url::kHttpScheme)); | |
650 | |
651 // Builds the expected upgraded URL. | |
652 GURL::Replacements replace_scheme; | |
653 replace_scheme.SetSchemeStr("https"); | |
654 replace_scheme.SetPortStr(""); | |
655 GURL iframe_secure_url = embedded_test_server() | |
656 ->GetURL("other.com", "/title1.html") | |
657 .ReplaceComponents(replace_scheme); | |
658 ASSERT_TRUE(iframe_secure_url.SchemeIs(url::kHttpsScheme)); | |
659 ASSERT_FALSE(iframe_secure_url.has_port()); | |
Mike West
2016/07/04 08:33:25
I'm missing something; how does this work? Don't y
carlosk
2016/07/04 09:22:59
I'm testing only if the URL received by the Naviga
Mike West
2016/07/04 09:31:43
The upgrade should not remove the port. That is, `
carlosk
2016/07/05 09:57:23
I updated the test so that it will check the same
| |
660 | |
661 NavigationHandleObserver observer_main(shell()->web_contents(), start_url); | |
662 NavigationHandleObserver observer_iframe(shell()->web_contents(), | |
663 iframe_secure_url); | |
664 TestNavigationThrottleInstaller installer_main( | |
665 shell()->web_contents(), NavigationThrottle::PROCEED, | |
666 NavigationThrottle::PROCEED, NavigationThrottle::DEFER); | |
667 | |
668 // Start loading the main frame and stop at WillProcess. | |
669 shell()->LoadURL(start_url); | |
670 installer_main.WaitForThrottleWillProcess(); | |
671 EXPECT_EQ(1, installer_main.will_start_called()); | |
672 EXPECT_EQ(0, installer_main.will_redirect_called()); | |
673 EXPECT_EQ(1, installer_main.will_process_called()); | |
674 | |
675 // Create a new throttle installer and start the iframe navigation until | |
676 // WillStart. | |
677 TestNavigationThrottleInstaller installer_iframe( | |
678 shell()->web_contents(), NavigationThrottle::DEFER, | |
679 NavigationThrottle::PROCEED, NavigationThrottle::PROCEED); | |
680 installer_main.navigation_throttle()->Resume(); | |
681 installer_iframe.WaitForThrottleWillStart(); | |
682 EXPECT_EQ(1, installer_iframe.will_start_called()); | |
683 EXPECT_EQ(0, installer_iframe.will_redirect_called()); | |
684 EXPECT_EQ(0, installer_iframe.will_process_called()); | |
685 | |
686 // Confirm the navigation of the main frame succeeded. | |
687 EXPECT_NE(-1, observer_main.frame_tree_node_id()); | |
688 EXPECT_EQ(start_url, observer_main.last_committed_url()); | |
689 EXPECT_TRUE(observer_main.has_committed()); | |
690 EXPECT_TRUE(observer_main.is_main_frame()); | |
691 EXPECT_FALSE(observer_main.is_renderer_initiated()); | |
692 | |
693 // Check that the handle observer did receive the start call with the correct | |
694 // URL. | |
695 ASSERT_NE(-1, observer_iframe.frame_tree_node_id()) | |
696 << "The start URL \"" << observer_iframe.last_unmatched_start_url() | |
697 << "\" didn't match the expected \"" << iframe_secure_url << "\""; | |
698 EXPECT_FALSE(observer_iframe.has_committed()); | |
699 EXPECT_FALSE(observer_iframe.is_main_frame()); | |
700 EXPECT_TRUE(observer_iframe.is_parent_main_frame()); | |
701 EXPECT_TRUE(observer_iframe.is_renderer_initiated()); | |
702 } | |
703 | |
635 } // namespace content | 704 } // namespace content |
OLD | NEW |