Index: content/browser/frame_host/navigation_handle_impl_browsertest.cc |
diff --git a/content/browser/frame_host/navigation_handle_impl_browsertest.cc b/content/browser/frame_host/navigation_handle_impl_browsertest.cc |
index ba1c812e470e081ec81e0cf068c26b7b2637463b..87619bdde567213077f3b70b9e5adb503f162f32 100644 |
--- a/content/browser/frame_host/navigation_handle_impl_browsertest.cc |
+++ b/content/browser/frame_host/navigation_handle_impl_browsertest.cc |
@@ -141,6 +141,9 @@ class TestNavigationThrottle : public NavigationThrottle { |
~TestNavigationThrottle() override {} |
void Resume() { navigation_handle()->Resume(); } |
+ void Cancel(NavigationThrottle::ThrottleCheckResult result) { |
+ navigation_handle()->CancelDeferredNavigation(result); |
+ } |
RequestContextType request_context_type() { return request_context_type_; } |
@@ -188,17 +191,18 @@ class TestNavigationThrottle : public NavigationThrottle { |
RequestContextType request_context_type_; |
}; |
-// Install a TestNavigationThrottle on all following requests and allows waiting |
-// for various NavigationThrottle related events. Waiting works only for the |
-// immediately next navigation. New instances are needed to wait for further |
-// navigations. |
+// Installs a TestNavigationThrottle either on all following requests or on |
+// requests with an expected starting URL, and allows waiting for various |
+// NavigationThrottle related events. Waiting works only for the immediately |
+// next navigation. New instances are needed to wait for further navigations. |
class TestNavigationThrottleInstaller : public WebContentsObserver { |
public: |
TestNavigationThrottleInstaller( |
WebContents* web_contents, |
NavigationThrottle::ThrottleCheckResult will_start_result, |
NavigationThrottle::ThrottleCheckResult will_redirect_result, |
- NavigationThrottle::ThrottleCheckResult will_process_result) |
+ NavigationThrottle::ThrottleCheckResult will_process_result, |
+ GURL expected_start_url = GURL()) |
: WebContentsObserver(web_contents), |
will_start_result_(will_start_result), |
will_redirect_result_(will_redirect_result), |
@@ -206,7 +210,8 @@ class TestNavigationThrottleInstaller : public WebContentsObserver { |
will_start_called_(0), |
will_redirect_called_(0), |
will_process_called_(0), |
- navigation_throttle_(nullptr) {} |
+ navigation_throttle_(nullptr), |
+ expected_start_url_(expected_start_url) {} |
~TestNavigationThrottleInstaller() override{}; |
TestNavigationThrottle* navigation_throttle() { return navigation_throttle_; } |
@@ -235,12 +240,43 @@ class TestNavigationThrottleInstaller : public WebContentsObserver { |
will_process_loop_runner_ = nullptr; |
} |
+ void Continue(NavigationThrottle::ThrottleCheckResult result) { |
+ ASSERT_NE(NavigationThrottle::DEFER, result); |
+ if (result == NavigationThrottle::PROCEED) |
+ navigation_throttle()->Resume(); |
+ else |
+ navigation_throttle()->Cancel(result); |
+ } |
+ |
int will_start_called() { return will_start_called_; } |
int will_redirect_called() { return will_redirect_called_; } |
int will_process_called() { return will_process_called_; } |
+ protected: |
+ virtual void DidCallWillStartRequest() { |
+ will_start_called_++; |
+ if (will_start_loop_runner_) |
+ will_start_loop_runner_->Quit(); |
+ } |
+ |
+ virtual void DidCallWillRedirectRequest() { |
+ will_redirect_called_++; |
+ if (will_redirect_loop_runner_) |
+ will_redirect_loop_runner_->Quit(); |
+ } |
+ |
+ virtual void DidCallWillProcessResponse() { |
+ will_process_called_++; |
+ if (will_process_loop_runner_) |
+ will_process_loop_runner_->Quit(); |
+ } |
+ |
private: |
void DidStartNavigation(NavigationHandle* handle) override { |
+ if (!expected_start_url_.is_empty() && |
+ handle->GetURL() != expected_start_url_) |
+ return; |
+ |
std::unique_ptr<NavigationThrottle> throttle(new TestNavigationThrottle( |
handle, will_start_result_, will_redirect_result_, will_process_result_, |
base::Bind(&TestNavigationThrottleInstaller::DidCallWillStartRequest, |
@@ -261,24 +297,6 @@ class TestNavigationThrottleInstaller : public WebContentsObserver { |
navigation_throttle_ = nullptr; |
} |
- void DidCallWillStartRequest() { |
- will_start_called_++; |
- if (will_start_loop_runner_) |
- will_start_loop_runner_->Quit(); |
- } |
- |
- void DidCallWillRedirectRequest() { |
- will_redirect_called_++; |
- if (will_redirect_loop_runner_) |
- will_redirect_loop_runner_->Quit(); |
- } |
- |
- void DidCallWillProcessResponse() { |
- will_process_called_++; |
- if (will_process_loop_runner_) |
- will_process_loop_runner_->Quit(); |
- } |
- |
NavigationThrottle::ThrottleCheckResult will_start_result_; |
NavigationThrottle::ThrottleCheckResult will_redirect_result_; |
NavigationThrottle::ThrottleCheckResult will_process_result_; |
@@ -289,6 +307,50 @@ class TestNavigationThrottleInstaller : public WebContentsObserver { |
scoped_refptr<MessageLoopRunner> will_start_loop_runner_; |
scoped_refptr<MessageLoopRunner> will_redirect_loop_runner_; |
scoped_refptr<MessageLoopRunner> will_process_loop_runner_; |
+ GURL expected_start_url_; |
+}; |
+ |
+// Same as above, but installs NavigationThrottles that do not directly return |
+// the pre-programmed check results, but first DEFER the navigation at each |
+// stage and then resume/cancel asynchronously. |
+class TestDeferringNavigationThrottleInstaller |
+ : public TestNavigationThrottleInstaller { |
+ public: |
+ TestDeferringNavigationThrottleInstaller( |
+ WebContents* web_contents, |
+ NavigationThrottle::ThrottleCheckResult will_start_result, |
+ NavigationThrottle::ThrottleCheckResult will_redirect_result, |
+ NavigationThrottle::ThrottleCheckResult will_process_result, |
+ GURL expected_start_url = GURL()) |
+ : TestNavigationThrottleInstaller(web_contents, |
+ NavigationThrottle::DEFER, |
+ NavigationThrottle::DEFER, |
+ NavigationThrottle::DEFER, |
+ expected_start_url), |
+ will_start_deferred_result_(will_start_result), |
+ will_redirect_deferred_result_(will_redirect_result), |
+ will_process_deferred_result_(will_process_result) {} |
+ |
+ protected: |
+ void DidCallWillStartRequest() override { |
+ TestNavigationThrottleInstaller::DidCallWillStartRequest(); |
+ Continue(will_start_deferred_result_); |
+ } |
+ |
+ void DidCallWillRedirectRequest() override { |
+ TestNavigationThrottleInstaller::DidCallWillStartRequest(); |
+ Continue(will_redirect_deferred_result_); |
+ } |
+ |
+ void DidCallWillProcessResponse() override { |
+ TestNavigationThrottleInstaller::DidCallWillStartRequest(); |
+ Continue(will_process_deferred_result_); |
+ } |
+ |
+ private: |
+ NavigationThrottle::ThrottleCheckResult will_start_deferred_result_; |
+ NavigationThrottle::ThrottleCheckResult will_redirect_deferred_result_; |
+ NavigationThrottle::ThrottleCheckResult will_process_deferred_result_; |
}; |
// Records all navigation start URLs from the WebContents. |
@@ -307,6 +369,17 @@ class NavigationStartUrlRecorder : public WebContentsObserver { |
std::vector<GURL> urls_; |
}; |
+bool IsChildFrameCollapsed(Shell* shell, const char* element_id) { |
+ const char kScript[] = |
+ "window.domAutomationController.send(" |
+ " document.getElementById(\"%s\").clientWidth" |
+ ");"; |
+ int client_width = 0; |
+ EXPECT_TRUE(ExecuteScriptAndExtractInt( |
+ shell, base::StringPrintf(kScript, element_id), &client_width)); |
+ return !client_width; |
+} |
+ |
} // namespace |
class NavigationHandleImplBrowserTest : public ContentBrowserTest { |
@@ -714,6 +787,89 @@ IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, ThrottleDefer) { |
GURL(embedded_test_server()->GetURL("bar.com", "/title2.html"))); |
} |
+// Ensure that a NavigationThrottle can block the navigation and collapse the |
+// frame owner both on request start as well as after a redirect. Plus, ensure |
+// that the frame is restored on the subsequent non-error-page navigation. |
+IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, |
+ ThrottleBlockAndCollapse) { |
+ const char kChildFrameId[] = "child0"; |
+ GURL main_url(embedded_test_server()->GetURL( |
+ "a.com", "/frame_tree/page_with_one_frame.html")); |
+ GURL blocked_subframe_url(embedded_test_server()->GetURL( |
+ "a.com", "/cross-site/baz.com/title1.html")); |
+ GURL allowed_subframe_url(embedded_test_server()->GetURL( |
+ "a.com", "/cross-site/baz.com/title2.html")); |
+ GURL allowed_subframe_final_url( |
+ embedded_test_server()->GetURL("baz.com", "/title2.html")); |
+ |
+ // Exercise both synchronous and deferred throttle check results, and both on |
+ // WillStartRequest and on WillRedirectRequest. |
+ for (const bool deferred_block : {false, true}) { |
+ for (const bool block_on_redirect : {false, true}) { |
+ SCOPED_TRACE(deferred_block ? "Direct block" : "Deferred block"); |
+ SCOPED_TRACE(block_on_redirect ? "Block on WillStartRequest" |
+ : "Block on WillRedirectRequest"); |
+ |
+ NavigationThrottle::ThrottleCheckResult will_start_result = |
+ block_on_redirect ? NavigationThrottle::PROCEED |
+ : NavigationThrottle::BLOCK_REQUEST_AND_COLLAPSE; |
+ NavigationThrottle::ThrottleCheckResult will_redirect_result = |
+ block_on_redirect ? NavigationThrottle::BLOCK_REQUEST_AND_COLLAPSE |
+ : NavigationThrottle::PROCEED; |
+ |
+ std::unique_ptr<TestNavigationThrottleInstaller> |
+ subframe_throttle_installer; |
+ if (deferred_block) { |
+ subframe_throttle_installer.reset( |
+ new TestDeferringNavigationThrottleInstaller( |
+ shell()->web_contents(), will_start_result, |
+ will_redirect_result, NavigationThrottle::PROCEED, |
+ blocked_subframe_url)); |
+ } else { |
+ subframe_throttle_installer.reset(new TestNavigationThrottleInstaller( |
+ shell()->web_contents(), will_start_result, will_redirect_result, |
+ NavigationThrottle::PROCEED, blocked_subframe_url)); |
+ } |
+ |
+ { |
+ SCOPED_TRACE("Initial navigation blocked on main frame load."); |
+ NavigationHandleObserver subframe_observer(shell()->web_contents(), |
+ blocked_subframe_url); |
+ |
+ ASSERT_TRUE(NavigateToURL(shell(), main_url)); |
+ EXPECT_TRUE(subframe_observer.is_error()); |
+ EXPECT_TRUE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
+ } |
+ |
+ { |
+ SCOPED_TRACE("Subsequent subframe navigation is allowed."); |
+ NavigationHandleObserver subframe_observer(shell()->web_contents(), |
+ allowed_subframe_url); |
+ |
+ ASSERT_TRUE(NavigateIframeToURL(shell()->web_contents(), kChildFrameId, |
+ allowed_subframe_url)); |
+ EXPECT_TRUE(subframe_observer.has_committed()); |
+ EXPECT_FALSE(subframe_observer.is_error()); |
+ EXPECT_EQ(allowed_subframe_final_url, |
+ subframe_observer.last_committed_url()); |
+ EXPECT_FALSE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
+ } |
+ |
+ { |
+ SCOPED_TRACE("Subsequent subframe navigation is blocked."); |
+ NavigationHandleObserver subframe_observer(shell()->web_contents(), |
+ blocked_subframe_url); |
+ |
+ ASSERT_TRUE(NavigateIframeToURL(shell()->web_contents(), kChildFrameId, |
+ blocked_subframe_url)); |
+ |
+ EXPECT_TRUE(subframe_observer.is_error()); |
+ EXPECT_TRUE(IsChildFrameCollapsed(shell(), kChildFrameId)); |
+ } |
+ } |
+ } |
+} |
+ |
// Checks that the RequestContextType value is properly set. |
IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, |
VerifyRequestContextTypeForFrameTree) { |
@@ -738,7 +894,8 @@ IN_PROC_BROWSER_TEST_F(NavigationHandleImplBrowserTest, |
EXPECT_TRUE(main_manager.WaitForRequestStart()); |
// The throttle should not be null. |
EXPECT_NE(previous_throttle, installer.navigation_throttle()); |
- // Checks the only URL recorded so far is the one expected for the main frame. |
+ // Checks the only URL recorded so far is the one expected for the main |
+ // frame. |
nasko
2017/01/20 23:34:36
Why this change in comment? git cl format?
|
EXPECT_EQ(main_url, url_recorder.urls().back()); |
EXPECT_EQ(1ul, url_recorder.urls().size()); |
// Checks the main frame RequestContextType. |