This is an automated email from the git hooks/post-receive script.
richard pushed a commit to branch geckoview-102.3.0esr-12.0-1 in repository tor-browser.
commit 35ce7a62c998c33c42211ddb673c2d1d3b918265 Author: Gijs Kruitbosch gijskruitbosch@gmail.com AuthorDate: Mon Jul 18 09:53:31 2022 +0000
Bug 1768671 - force append '.pdf' for pdf blob downloads displayed inline if it's missing, r=marco a=RyanVM
Differential Revision: https://phabricator.services.mozilla.com/D151890 --- .../pdfjs/content/PdfStreamConverter.jsm | 7 +++ toolkit/components/pdfjs/test/browser.ini | 3 + .../pdfjs/test/browser_pdfjs_nonpdf_filename.js | 73 ++++++++++++++++++++++ 3 files changed, 83 insertions(+)
diff --git a/toolkit/components/pdfjs/content/PdfStreamConverter.jsm b/toolkit/components/pdfjs/content/PdfStreamConverter.jsm index e096de107c3ad..46ad896b51bd0 100644 --- a/toolkit/components/pdfjs/content/PdfStreamConverter.jsm +++ b/toolkit/components/pdfjs/content/PdfStreamConverter.jsm @@ -1118,6 +1118,13 @@ PdfStreamConverter.prototype = { contentDispositionFilename = aRequest.contentDispositionFilename; } catch (e) {}
+ if ( + contentDispositionFilename && + !/.pdf$/i.test(contentDispositionFilename) + ) { + contentDispositionFilename += ".pdf"; + } + // Change the content type so we don't get stuck in a loop. aRequest.setProperty("contentType", aRequest.contentType); aRequest.contentType = "text/html"; diff --git a/toolkit/components/pdfjs/test/browser.ini b/toolkit/components/pdfjs/test/browser.ini index 1a3036f2ec591..9a06d2b9e1dd0 100644 --- a/toolkit/components/pdfjs/test/browser.ini +++ b/toolkit/components/pdfjs/test/browser.ini @@ -20,6 +20,9 @@ support-files = file_pdfjs_js.pdf [browser_pdfjs_main.js] [browser_pdfjs_navigation.js] +[browser_pdfjs_nonpdf_filename.js] +support-files = + file_pdf_download_link.html [browser_pdfjs_not_default.js] support-files = file_pdfjs_object_stream.pdf diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js b/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js new file mode 100644 index 0000000000000..ef7de3dab6e90 --- /dev/null +++ b/toolkit/components/pdfjs/test/browser_pdfjs_nonpdf_filename.js @@ -0,0 +1,73 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const TESTROOT = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content/", + "https://example.com/" +); + +// The page we use to open the PDF. +const LINK_PAGE_URL = TESTROOT + "file_pdf_download_link.html"; + +add_task(async function test_filename_nonpdf_extension() { + var MockFilePicker = SpecialPowers.MockFilePicker; + MockFilePicker.init(window); + let filepickerNamePromise = new Promise(resolve => { + MockFilePicker.showCallback = function(fp) { + resolve(fp.defaultString); + return MockFilePicker.returnCancel; + }; + }); + registerCleanupFunction(() => MockFilePicker.cleanup()); + + await SpecialPowers.pushPrefEnv({ + set: [["browser.download.open_pdf_attachments_inline", true]], + }); + + await BrowserTestUtils.withNewTab( + { gBrowser, url: LINK_PAGE_URL }, + async function(browser) { + await SpecialPowers.spawn(browser, [], async () => { + let link = content.document.getElementById("custom_filename"); + let response = await content.fetch(link.href, { + method: "GET", + headers: { + "Content-Type": "application/pdf", + }, + }); + let blob = await response.blob(); + const url = content.URL.createObjectURL(blob); + link.href = url; + link.download = "Fido-2022-04-28"; + link.rel = "noopener"; + }); + + let pdfLoaded = BrowserTestUtils.waitForNewTab( + gBrowser, + url => url.startsWith("blob:"), + true + ); + await BrowserTestUtils.synthesizeMouseAtCenter( + "#custom_filename", + {}, + browser + ); + let newTab = await pdfLoaded; + + info("Clicking on the download button..."); + await SpecialPowers.spawn(newTab.linkedBrowser, [], () => { + content.document.getElementById("download").click(); + }); + info("Waiting for a filename to be picked from the file picker"); + let defaultName = await filepickerNamePromise; + is( + defaultName, + "Fido-2022-04-28.pdf", + "Should have gotten the provided filename with pdf suffixed." + ); + BrowserTestUtils.removeTab(newTab); + } + ); +});