| 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/common/request_context_type.h" | 9 #include "content/public/common/request_context_type.h" |
| 10 #include "content/public/test/browser_test_utils.h" | 10 #include "content/public/test/browser_test_utils.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 181 |
| 182 NavigationThrottle::ThrottleCheckResult will_start_result_; | 182 NavigationThrottle::ThrottleCheckResult will_start_result_; |
| 183 NavigationThrottle::ThrottleCheckResult will_redirect_result_; | 183 NavigationThrottle::ThrottleCheckResult will_redirect_result_; |
| 184 NavigationThrottle::ThrottleCheckResult will_process_result_; | 184 NavigationThrottle::ThrottleCheckResult will_process_result_; |
| 185 base::Closure did_call_will_start_; | 185 base::Closure did_call_will_start_; |
| 186 base::Closure did_call_will_redirect_; | 186 base::Closure did_call_will_redirect_; |
| 187 base::Closure did_call_will_process_; | 187 base::Closure did_call_will_process_; |
| 188 RequestContextType request_context_type_; | 188 RequestContextType request_context_type_; |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 // Install a TestNavigationThrottle on all following requests and allows waiting | 191 // Install a TestNavigationThrottle either on all following requests or on |
| 192 // for various NavigationThrottle related events. Waiting works only for the | 192 // requests with an expected starting URL, and allows waiting for various |
| 193 // immediately next navigation. New instances are needed to wait for further | 193 // NavigationThrottle related events. Waiting works only for the immediately |
| 194 // navigations. | 194 // next navigation. New instances are needed to wait for further navigations. |
| 195 class TestNavigationThrottleInstaller : public WebContentsObserver { | 195 class TestNavigationThrottleInstaller : public WebContentsObserver { |
| 196 public: | 196 public: |
| 197 TestNavigationThrottleInstaller( | 197 TestNavigationThrottleInstaller( |
| 198 WebContents* web_contents, | 198 WebContents* web_contents, |
| 199 NavigationThrottle::ThrottleCheckResult will_start_result, | 199 NavigationThrottle::ThrottleCheckResult will_start_result, |
| 200 NavigationThrottle::ThrottleCheckResult will_redirect_result, | 200 NavigationThrottle::ThrottleCheckResult will_redirect_result, |
| 201 NavigationThrottle::ThrottleCheckResult will_process_result) | 201 NavigationThrottle::ThrottleCheckResult will_process_result, |
| 202 GURL expected_start_url = GURL()) |
| 202 : WebContentsObserver(web_contents), | 203 : WebContentsObserver(web_contents), |
| 203 will_start_result_(will_start_result), | 204 will_start_result_(will_start_result), |
| 204 will_redirect_result_(will_redirect_result), | 205 will_redirect_result_(will_redirect_result), |
| 205 will_process_result_(will_process_result), | 206 will_process_result_(will_process_result), |
| 206 will_start_called_(0), | 207 will_start_called_(0), |
| 207 will_redirect_called_(0), | 208 will_redirect_called_(0), |
| 208 will_process_called_(0), | 209 will_process_called_(0), |
| 209 navigation_throttle_(nullptr) {} | 210 navigation_throttle_(nullptr), |
| 211 expected_start_url_(expected_start_url) {} |
| 210 ~TestNavigationThrottleInstaller() override{}; | 212 ~TestNavigationThrottleInstaller() override{}; |
| 211 | 213 |
| 212 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } | 214 TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } |
| 213 | 215 |
| 214 void WaitForThrottleWillStart() { | 216 void WaitForThrottleWillStart() { |
| 215 if (will_start_called_) | 217 if (will_start_called_) |
| 216 return; | 218 return; |
| 217 will_start_loop_runner_ = new MessageLoopRunner(); | 219 will_start_loop_runner_ = new MessageLoopRunner(); |
| 218 will_start_loop_runner_->Run(); | 220 will_start_loop_runner_->Run(); |
| 219 will_start_loop_runner_ = nullptr; | 221 will_start_loop_runner_ = nullptr; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 234 will_process_loop_runner_->Run(); | 236 will_process_loop_runner_->Run(); |
| 235 will_process_loop_runner_ = nullptr; | 237 will_process_loop_runner_ = nullptr; |
| 236 } | 238 } |
| 237 | 239 |
| 238 int will_start_called() { return will_start_called_; } | 240 int will_start_called() { return will_start_called_; } |
| 239 int will_redirect_called() { return will_redirect_called_; } | 241 int will_redirect_called() { return will_redirect_called_; } |
| 240 int will_process_called() { return will_process_called_; } | 242 int will_process_called() { return will_process_called_; } |
| 241 | 243 |
| 242 private: | 244 private: |
| 243 void DidStartNavigation(NavigationHandle* handle) override { | 245 void DidStartNavigation(NavigationHandle* handle) override { |
| 246 if (!expected_start_url_.is_empty() && |
| 247 handle->GetURL() != expected_start_url_) |
| 248 return; |
| 249 |
| 244 std::unique_ptr<NavigationThrottle> throttle(new TestNavigationThrottle( | 250 std::unique_ptr<NavigationThrottle> throttle(new TestNavigationThrottle( |
| 245 handle, will_start_result_, will_redirect_result_, will_process_result_, | 251 handle, will_start_result_, will_redirect_result_, will_process_result_, |
| 246 base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest, | 252 base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest, |
| 247 base::Unretained(this)), | 253 base::Unretained(this)), |
| 248 base::Bind(&TestNavigationThrottleInstaller::DidCallWillRedirectRequest, | 254 base::Bind(&TestNavigationThrottleInstaller::DidCallWillRedirectRequest, |
| 249 base::Unretained(this)), | 255 base::Unretained(this)), |
| 250 base::Bind(&TestNavigationThrottleInstaller::DidCallWillProcessResponse, | 256 base::Bind(&TestNavigationThrottleInstaller::DidCallWillProcessResponse, |
| 251 base::Unretained(this)))); | 257 base::Unretained(this)))); |
| 252 navigation_throttle_ = static_cast<TestNavigationThrottle*>(throttle.get()); | 258 navigation_throttle_ = static_cast<TestNavigationThrottle*>(throttle.get()); |
| 253 handle->RegisterThrottleForTesting(std::move(throttle)); | 259 handle->RegisterThrottleForTesting(std::move(throttle)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 282 NavigationThrottle::ThrottleCheckResult will_start_result_; | 288 NavigationThrottle::ThrottleCheckResult will_start_result_; |
| 283 NavigationThrottle::ThrottleCheckResult will_redirect_result_; | 289 NavigationThrottle::ThrottleCheckResult will_redirect_result_; |
| 284 NavigationThrottle::ThrottleCheckResult will_process_result_; | 290 NavigationThrottle::ThrottleCheckResult will_process_result_; |
| 285 int will_start_called_; | 291 int will_start_called_; |
| 286 int will_redirect_called_; | 292 int will_redirect_called_; |
| 287 int will_process_called_; | 293 int will_process_called_; |
| 288 TestNavigationThrottle* navigation_throttle_; | 294 TestNavigationThrottle* navigation_throttle_; |
| 289 scoped_refptr<MessageLoopRunner> will_start_loop_runner_; | 295 scoped_refptr<MessageLoopRunner> will_start_loop_runner_; |
| 290 scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_; | 296 scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_; |
| 291 scoped_refptr<MessageLoopRunner> will_process_loop_runner_; | 297 scoped_refptr<MessageLoopRunner> will_process_loop_runner_; |
| 298 GURL expected_start_url_; |
| 292 }; | 299 }; |
| 293 | 300 |
| 294 // Records all navigation start URLs from the WebContents. | 301 // Records all navigation start URLs from the WebContents. |
| 295 class NavigationStartUrlRecorder : public WebContentsObserver { | 302 class NavigationStartUrlRecorder : public WebContentsObserver { |
| 296 public: | 303 public: |
| 297 NavigationStartUrlRecorder(WebContents* web_contents) | 304 NavigationStartUrlRecorder(WebContents* web_contents) |
| 298 : WebContentsObserver(web_contents) {} | 305 : WebContentsObserver(web_contents) {} |
| 299 | 306 |
| 300 void DidStartNavigation(NavigationHandle* navigation_handle) override { | 307 void DidStartNavigation(NavigationHandle* navigation_handle) override { |
| 301 urls_.push_back(navigation_handle->GetURL()); | 308 urls_.push_back(navigation_handle->GetURL()); |
| 302 } | 309 } |
| 303 | 310 |
| 304 const std::vector<GURL>& urls() const { return urls_; } | 311 const std::vector<GURL>& urls() const { return urls_; } |
| 305 | 312 |
| 306 private: | 313 private: |
| 307 std::vector<GURL> urls_; | 314 std::vector<GURL> urls_; |
| 308 }; | 315 }; |
| 309 | 316 |
| 317 bool IsChildFrameCollapsed(Shell* shell, const char* element_id) { |
| 318 const char kScript[] = |
| 319 "window.domAutomationController.send(" |
| 320 " document.getElementById(\"%s\").clientWidth" |
| 321 ");"; |
| 322 int client_width = 0; |
| 323 EXPECT_TRUE(ExecuteScriptAndExtractInt( |
| 324 shell, base::StringPrintf(kScript, element_id), &client_width)); |
| 325 return !client_width; |
| 326 } |
| 327 |
| 310 } // namespace | 328 } // namespace |
| 311 | 329 |
| 312 class NavigationHandleImplBrowserTest : public ContentBrowserTest { | 330 class NavigationHandleImplBrowserTest : public ContentBrowserTest { |
| 313 protected: | 331 protected: |
| 314 void SetUpOnMainThread() override { | 332 void SetUpOnMainThread() override { |
| 315 host_resolver()->AddRule("*", "127.0.0.1"); | 333 host_resolver()->AddRule("*", "127.0.0.1"); |
| 316 SetupCrossSiteRedirector(embedded_test_server()); | 334 SetupCrossSiteRedirector(embedded_test_server()); |
| 317 ASSERT_TRUE(embedded_test_server()->Start()); | 335 ASSERT_TRUE(embedded_test_server()->Start()); |
| 318 } | 336 } |
| 319 }; | 337 }; |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 // Wait for the end of the navigation. | 725 // Wait for the end of the navigation. |
| 708 navigation_observer.Wait(); | 726 navigation_observer.Wait(); |
| 709 | 727 |
| 710 EXPECT_TRUE(observer.has_committed()); | 728 EXPECT_TRUE(observer.has_committed()); |
| 711 EXPECT_TRUE(observer.was_redirected()); | 729 EXPECT_TRUE(observer.was_redirected()); |
| 712 EXPECT_FALSE(observer.is_error()); | 730 EXPECT_FALSE(observer.is_error()); |
| 713 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), | 731 EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), |
| 714 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); | 732 GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); |
| 715 } | 733 } |
| 716 | 734 |
| 735 // Ensure that a NavigationThrottle can block the navigation and collapse the |
| 736 // frame owner both on request start as well as after a redirect. Plus, ensure |
| 737 // that the frame is restored on the subsequent non-error-page navigation. |
| 738 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, |
| 739 ThrottleBlockAndCollapseAfterRedirect) { |
| 740 const char kChildFrameId[] = "child0"; |
| 741 GURL main_url(embedded_test_server()->GetURL( |
| 742 "a.com", "/frame_tree/page_with_one_frame.html")); |
| 743 GURL blocked_subframe_url(embedded_test_server()->GetURL( |
| 744 "a.com", "/cross-site/baz.com/title1.html")); |
| 745 GURL allowed_subframe_url(embedded_test_server()->GetURL( |
| 746 "a.com", "/cross-site/baz.com/title2.html")); |
| 747 GURL allowed_subframe_final_url( |
| 748 embedded_test_server()->GetURL("baz.com", "/title2.html")); |
| 749 |
| 750 for (const bool block_after_redirect : {true}) { |
| 751 SCOPED_TRACE(block_after_redirect ? "Blocking on WillRedirectRequest." |
| 752 : "Blocking on WilLStartRequest."); |
| 753 TestNavigationThrottleInstaller subframe_blocking_throttle_installer( |
| 754 shell()->web_contents(), |
| 755 block_after_redirect ? NavigationThrottle::PROCEED |
| 756 : NavigationThrottle::PROCEED, |
| 757 block_after_redirect ? NavigationThrottle::PROCEED |
| 758 : NavigationThrottle::PROCEED, |
| 759 NavigationThrottle::PROCEED, blocked_subframe_url); |
| 760 |
| 761 { |
| 762 SCOPED_TRACE("Initial navigation blocked on main frame load."); |
| 763 NavigationHandleObserver subframe_observer(shell()->web_contents(), |
| 764 blocked_subframe_url); |
| 765 |
| 766 ASSERT_TRUE(NavigateToURL(shell(), main_url)); |
| 767 EXPECT_TRUE(subframe_observer.has_committed()); |
| 768 EXPECT_TRUE(subframe_observer.is_error()); |
| 769 EXPECT_TRUE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
| 770 } |
| 771 |
| 772 { |
| 773 SCOPED_TRACE("Subsequent subframe-only navigation is allowed."); |
| 774 NavigationHandleObserver subframe_observer(shell()->web_contents(), |
| 775 allowed_subframe_url); |
| 776 |
| 777 ASSERT_TRUE(NavigateIframeToURL(shell()->web_contents(), kChildFrameId, |
| 778 allowed_subframe_url)); |
| 779 EXPECT_TRUE(subframe_observer.has_committed()); |
| 780 EXPECT_FALSE(subframe_observer.is_error()); |
| 781 EXPECT_EQ(allowed_subframe_final_url, |
| 782 subframe_observer.last_committed_url()); |
| 783 EXPECT_FALSE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
| 784 } |
| 785 |
| 786 { |
| 787 SCOPED_TRACE("Subsequent, subframe-only navigation is blocked."); |
| 788 NavigationHandleObserver subframe_observer(shell()->web_contents(), |
| 789 blocked_subframe_url); |
| 790 |
| 791 ASSERT_TRUE(NavigateIframeToURL(shell()->web_contents(), kChildFrameId, |
| 792 blocked_subframe_url)); |
| 793 |
| 794 EXPECT_TRUE(subframe_observer.has_committed()); |
| 795 EXPECT_TRUE(subframe_observer.is_error()); |
| 796 EXPECT_TRUE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
| 797 } |
| 798 } |
| 799 } |
| 800 |
| 717 // Checks that the RequestContextType value is properly set. | 801 // Checks that the RequestContextType value is properly set. |
| 718 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, | 802 IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, |
| 719 VerifyRequestContextTypeForFrameTree) { | 803 VerifyRequestContextTypeForFrameTree) { |
| 720 GURL main_url(embedded_test_server()->GetURL( | 804 GURL main_url(embedded_test_server()->GetURL( |
| 721 "a.com", "/cross_site_iframe_factory.html?a(b(c))")); | 805 "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 722 GURL b_url(embedded_test_server()->GetURL( | 806 GURL b_url(embedded_test_server()->GetURL( |
| 723 "b.com", "/cross_site_iframe_factory.html?b(c())")); | 807 "b.com", "/cross_site_iframe_factory.html?b(c())")); |
| 724 GURL c_url(embedded_test_server()->GetURL( | 808 GURL c_url(embedded_test_server()->GetURL( |
| 725 "c.com", "/cross_site_iframe_factory.html?c()")); | 809 "c.com", "/cross_site_iframe_factory.html?c()")); |
| 726 | 810 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 IN_PROC_BROWSER_TEST_F(NavigationHandleImplHttpsUpgradeBrowserTest, | 982 IN_PROC_BROWSER_TEST_F(NavigationHandleImplHttpsUpgradeBrowserTest, |
| 899 StartUrlIsHttpsUpgradedCrossSite) { | 983 StartUrlIsHttpsUpgradedCrossSite) { |
| 900 GURL start_url( | 984 GURL start_url( |
| 901 embedded_test_server()->GetURL("/https_upgrade_cross_site.html")); | 985 embedded_test_server()->GetURL("/https_upgrade_cross_site.html")); |
| 902 GURL cross_site_iframe_secure_url("https://other.com/title1.html"); | 986 GURL cross_site_iframe_secure_url("https://other.com/title1.html"); |
| 903 | 987 |
| 904 CheckHttpsUpgradedIframeNavigation(start_url, cross_site_iframe_secure_url); | 988 CheckHttpsUpgradedIframeNavigation(start_url, cross_site_iframe_secure_url); |
| 905 } | 989 } |
| 906 | 990 |
| 907 } // namespace content | 991 } // namespace content |
| OLD | NEW |