 Chromium Code Reviews
 Chromium Code Reviews Issue 2801813005:
  Only show a beforeunload dialog if a frame has had a user gesture since its load.  (Closed)
    
  
    Issue 2801813005:
  Only show a beforeunload dialog if a frame has had a user gesture since its load.  (Closed) 
  | Index: content/browser/frame_host/render_frame_host_impl_browsertest.cc | 
| diff --git a/content/browser/frame_host/render_frame_host_impl_browsertest.cc b/content/browser/frame_host/render_frame_host_impl_browsertest.cc | 
| index 5f467554e62c1979c685e02fcf56874736d739e0..4103d6a46d24acb69109bbef9f283e0daea3db7d 100644 | 
| --- a/content/browser/frame_host/render_frame_host_impl_browsertest.cc | 
| +++ b/content/browser/frame_host/render_frame_host_impl_browsertest.cc | 
| @@ -248,7 +248,11 @@ IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, | 
| "document.body.appendChild(iframe);" | 
| "iframe.contentWindow.onbeforeunload=function(e){return 'x'};"; | 
| EXPECT_TRUE(content::ExecuteScript(wc, script)); | 
| - EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); | 
| + EXPECT_TRUE(WaitForLoadStop(wc)); | 
| + | 
| + // JavaScript onbeforeunload dialogs require a user gesture. | 
| + for (auto* frame : wc->GetAllFrames()) | 
| + frame->ExecuteJavaScriptWithUserGestureForTests(base::string16()); | 
| // Force a process switch by going to a privileged page. The beforeunload | 
| // timer will be started on the top-level frame but will be paused while the | 
| @@ -281,4 +285,37 @@ IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, | 
| wc->SetJavaScriptDialogManagerForTesting(nullptr); | 
| } | 
| +// Tests that a gesture is required in a frame before it can request a | 
| +// beforeunload dialog. | 
| +IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, | 
| + BeforeUnloadDialogRequiresGesture) { | 
| + WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); | 
| + TestJavaScriptDialogManager dialog_manager; | 
| + wc->SetDelegate(&dialog_manager); | 
| + | 
| + EXPECT_TRUE(NavigateToURL( | 
| + shell(), GetTestUrl("render_frame_host", "beforeunload.html"))); | 
| + // Disable the hang monitor, otherwise there will be a race between the | 
| + // beforeunload dialog and the beforeunload hang timer. | 
| + wc->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting(); | 
| 
Nico
2017/04/07 20:05:28
Huh, I guess it makes sense here.
 
Avi (use Gerrit)
2017/05/05 15:30:04
Done.
 | 
| + | 
| + // Reload. There should be no beforeunload dialog because there was no gesture | 
| + // on the page. If there was, this WaitForLoadStop call will hang. | 
| + wc->GetController().Reload(ReloadType::NORMAL, false); | 
| + EXPECT_TRUE(WaitForLoadStop(wc)); | 
| + | 
| + // Give the page a user gesture and try leaving again. This time there | 
| + // should be a dialog. If there is no dialog, the call to Wait will hang. | 
| + wc->GetMainFrame()->ExecuteJavaScriptWithUserGestureForTests( | 
| + base::string16()); | 
| + shell()->LoadURL(GURL("about:blank")); | 
| + dialog_manager.Wait(); | 
| + | 
| + // Answer the dialog. | 
| + dialog_manager.callback().Run(false, base::string16()); | 
| + | 
| + wc->SetDelegate(nullptr); | 
| + wc->SetJavaScriptDialogManagerForTesting(nullptr); | 
| +} | 
| + | 
| } // namespace content |