diff --git a/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js b/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js index d6c10619ec6f..a1bfc18a914e 100644 --- a/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js +++ b/toolkit/components/pdfjs/test/browser_pdfjs_saveas.js @@ -170,3 +170,47 @@ add_task(async function test_pdf_saveas_customname() { ); await SpecialPowers.popPrefEnv(); }); + +/** + * Check if the directory where the pdfs are saved is based on the original + * domain (see bug 1768383). + */ +add_task(async function () { + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:blank" }, + async function (browser) { + const downloadLastDir = new DownloadLastDir(null); + const destDirs = []; + for (let i = 1; i <= 2; i++) { + const destDir = createTemporarySaveDirectory(i); + destDirs.push(destDir); + const url = `http://test${i}.example.com/browser/${RELATIVE_DIR}file_pdfjs_test.pdf`; + downloadLastDir.setFile(url, destDir); + await TestUtils.waitForTick(); + } + + const url = `http://test1.example.com/browser/${RELATIVE_DIR}file_pdfjs_hcm.pdf`; + await waitForPdfJS(browser, url); + + const fileSavedPromise = new Promise(resolve => { + MockFilePicker.showCallback = fp => { + MockFilePicker.setFiles([]); + MockFilePicker.showCallback = null; + resolve(fp.displayDirectory.path); + }; + }); + registerCleanupFunction(() => { + for (const destDir of destDirs) { + destDir.remove(true); + } + }); + saveBrowser(browser); + const dirPath = await fileSavedPromise; + is( + dirPath, + destDirs[0].path, + "Proposed directory must be based on the domain" + ); + } + ); +}); diff --git a/toolkit/components/pdfjs/test/head.js b/toolkit/components/pdfjs/test/head.js index 3ad5981671d5..ee554c161c42 100644 --- a/toolkit/components/pdfjs/test/head.js +++ b/toolkit/components/pdfjs/test/head.js @@ -327,9 +327,9 @@ function changeMimeHandler(preferredAction, alwaysAskBeforeHandling) { return oldAction; } -function createTemporarySaveDirectory() { +function createTemporarySaveDirectory(id = "") { var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile); - saveDir.append("testsavedir"); + saveDir.append(`testsavedir${id}`); if (!saveDir.exists()) { saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); } diff --git a/toolkit/content/contentAreaUtils.js b/toolkit/content/contentAreaUtils.js index 084d317a2ae8..d9ee83026e9d 100644 --- a/toolkit/content/contentAreaUtils.js +++ b/toolkit/content/contentAreaUtils.js @@ -326,7 +326,8 @@ function internalSave( }; // Find a URI to use for determining last-downloaded-to directory - let relatedURI = aReferrerInfo?.originalReferrer || sourceURI; + let relatedURI = + aOriginalURL || aReferrerInfo?.originalReferrer || sourceURI; promiseTargetFile(fpParams, aSkipPrompt, relatedURI) .then(aDialogAccepted => {