Don't dispatch beforeunload events for downloads

BUG=812293
TBR=jochen@chromium.org

(cherry picked from commit cd05cee6edd1301f63d44306ff120820b6a4cfde)

Change-Id: I1375ecbdef13ea43ac350cdae6e550267166fadc
Reviewed-on: https://chromium-review.googlesource.com/934204
Reviewed-by: Mike West <mkwst@chromium.org>
Commit-Queue: Jochen Eisinger <jochen@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#539097}
Reviewed-on: https://chromium-review.googlesource.com/941446
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#623}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/content/renderer/render_frame_impl.cc b/content/renderer/render_frame_impl.cc
index 03f713d..956085e 100644
--- a/content/renderer/render_frame_impl.cc
+++ b/content/renderer/render_frame_impl.cc
@@ -5985,7 +5985,10 @@
       info.url_request.CheckForBrowserSideNavigation() &&
       // No need to dispatch beforeunload if the frame has not committed a
       // navigation and contains an empty initial document.
-      (has_accessed_initial_document_ || !current_history_item_.IsNull());
+      (has_accessed_initial_document_ || !current_history_item_.IsNull()) &&
+      // Don't dispatch beforeunload if the navigation might end up as a
+      // download.
+      !info.url_request.GetSuggestedFilename().has_value();
 
   if (should_dispatch_before_unload) {
     // Execute the BeforeUnload event. If asked not to proceed or the frame is
diff --git a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/embedded-content/the-area-element/area-download-click-expected.txt b/third_party/WebKit/LayoutTests/external/wpt/html/semantics/embedded-content/the-area-element/area-download-click-expected.txt
deleted file mode 100644
index 03a0063..0000000
--- a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/embedded-content/the-area-element/area-download-click-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-FAIL Clicking on an <area> element with a download attribute must not throw an exception assert_unreached: Navigated instead of downloading Reached unreachable code
-Harness: the test ran to completion.
-
diff --git a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-404-expected.txt b/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-404-expected.txt
index fd1a48ae..d3c5fc6 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-404-expected.txt
+++ b/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-404-expected.txt
@@ -1,4 +1,4 @@
 This is a testharness.js-based test.
-FAIL Do not navigate to 404 for anchor with download assert_unreached: Navigated instead of downloading Reached unreachable code
+FAIL Do not navigate to 404 for anchor with download Blocked a frame with origin "http://web-platform.test:8001" from accessing a cross-origin frame.
 Harness: the test ran to completion.
 
diff --git a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-expected.txt b/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-expected.txt
deleted file mode 100644
index f372e80..0000000
--- a/third_party/WebKit/LayoutTests/external/wpt/html/semantics/text-level-semantics/the-a-element/a-download-click-expected.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-This is a testharness.js-based test.
-FAIL Clicking on an <a> element with a download attribute must not throw an exception assert_unreached: Navigated instead of downloading Reached unreachable code
-Harness: the test ran to completion.
-