diff --git a/toolkit/components/downloads/DownloadCore.jsm b/toolkit/components/downloads/DownloadCore.jsm index e9372867afa5..b2c3829b6721 100644 --- a/toolkit/components/downloads/DownloadCore.jsm +++ b/toolkit/components/downloads/DownloadCore.jsm @@ -17,7 +17,6 @@ var EXPORTED_SYMBOLS = [ "DownloadSaver", "DownloadCopySaver", "DownloadLegacySaver", - "DownloadPDFSaver", ]; const { Integration } = ChromeUtils.import( @@ -51,12 +50,6 @@ XPCOMUtils.defineLazyServiceGetter( "@mozilla.org/uriloader/external-helper-app-service;1", Ci.nsIExternalHelperAppService ); -XPCOMUtils.defineLazyServiceGetter( - this, - "gPrintSettingsService", - "@mozilla.org/gfx/printsettings-service;1", - Ci.nsIPrintSettingsService -); /* global DownloadIntegration */ Integration.downloads.defineModuleGetter( @@ -1936,9 +1929,6 @@ DownloadSaver.fromSerializable = function(aSerializable) { case "legacy": saver = DownloadLegacySaver.fromSerializable(serializable); break; - case "pdf": - saver = DownloadPDFSaver.fromSerializable(serializable); - break; default: throw new Error("Unrecoginzed download saver type."); } @@ -2846,153 +2836,3 @@ DownloadLegacySaver.prototype = { DownloadLegacySaver.fromSerializable = function() { return new DownloadLegacySaver(); }; - -/** - * This DownloadSaver type creates a PDF file from the current document in a - * given window, specified using the windowRef property of the DownloadSource - * object associated with the download. - * - * In order to prevent the download from saving a different document than the one - * originally loaded in the window, any attempt to restart the download will fail. - * - * Since this DownloadSaver type requires a live document as a source, it cannot - * be persisted across sessions, unless the download already succeeded. - */ -var DownloadPDFSaver = function() {}; - -DownloadPDFSaver.prototype = { - __proto__: DownloadSaver.prototype, - - /** - * An nsIWebBrowserPrint instance for printing this page. - * This is null when saving has not started or has completed, - * or while the operation is being canceled. - */ - _webBrowserPrint: null, - - /** - * Implements "DownloadSaver.execute". - */ - async execute(aSetProgressBytesFn, aSetPropertiesFn) { - if (!this.download.source.windowRef) { - throw new DownloadError({ - message: - "PDF saver must be passed an open window, and cannot be restarted.", - becauseSourceFailed: true, - }); - } - - let win = this.download.source.windowRef.get(); - - // Set windowRef to null to avoid re-trying. - this.download.source.windowRef = null; - - if (!win) { - throw new DownloadError({ - message: "PDF saver can't save a window that has been closed.", - becauseSourceFailed: true, - }); - } - - this.addToHistory(); - - let targetPath = this.download.target.path; - - // An empty target file must exist for the PDF printer to work correctly. - let file = await OS.File.open(targetPath, { truncate: true }); - await file.close(); - - let printSettings = gPrintSettingsService.newPrintSettings; - - printSettings.printToFile = true; - printSettings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF; - printSettings.toFileName = targetPath; - - printSettings.printSilent = true; - printSettings.showPrintProgress = false; - - printSettings.printBGImages = true; - printSettings.printBGColors = true; - printSettings.headerStrCenter = ""; - printSettings.headerStrLeft = ""; - printSettings.headerStrRight = ""; - printSettings.footerStrCenter = ""; - printSettings.footerStrLeft = ""; - printSettings.footerStrRight = ""; - - this._webBrowserPrint = win.getInterface(Ci.nsIWebBrowserPrint); - - try { - await new Promise((resolve, reject) => { - this._webBrowserPrint.print(printSettings, { - onStateChange(webProgress, request, stateFlags, status) { - if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) { - if (!Components.isSuccessCode(status)) { - reject(new DownloadError({ result: status, inferCause: true })); - } else { - resolve(); - } - } - }, - onProgressChange( - webProgress, - request, - curSelfProgress, - maxSelfProgress, - curTotalProgress, - maxTotalProgress - ) { - aSetProgressBytesFn(curTotalProgress, maxTotalProgress, false); - }, - onLocationChange() {}, - onStatusChange() {}, - onSecurityChange() {}, - onContentBlockingEvent() {}, - }); - }); - } finally { - // Remove the print object to avoid leaks - this._webBrowserPrint = null; - } - - let fileInfo = await OS.File.stat(targetPath); - aSetProgressBytesFn(fileInfo.size, fileInfo.size, false); - }, - - /** - * Implements "DownloadSaver.cancel". - */ - cancel: function DCS_cancel() { - if (this._webBrowserPrint) { - this._webBrowserPrint.cancel(); - this._webBrowserPrint = null; - } - }, - - /** - * Implements "DownloadSaver.toSerializable". - */ - toSerializable() { - if (this.download.succeeded) { - return DownloadCopySaver.prototype.toSerializable.call(this); - } - - // This object needs a window to recreate itself. If it didn't succeded - // it will not be possible to restart. Returning null here will - // prevent us from serializing it at all. - return null; - }, -}; - -/** - * Creates a new DownloadPDFSaver object, with its initial state derived from - * its serializable representation. - * - * @param aSerializable - * Serializable representation of a DownloadPDFSaver object. - * - * @return The newly created DownloadPDFSaver object. - */ -DownloadPDFSaver.fromSerializable = function(aSerializable) { - return new DownloadPDFSaver(); -}; diff --git a/toolkit/components/downloads/test/browser/browser.ini b/toolkit/components/downloads/test/browser/browser.ini index 131fc4ec8600..d75fd86f88f0 100644 --- a/toolkit/components/downloads/test/browser/browser.ini +++ b/toolkit/components/downloads/test/browser/browser.ini @@ -3,5 +3,5 @@ support-files = head.js testFile.html -[browser_DownloadPDFSaver.js] +[browser_DownloadCopySaver.js] skip-if = os != "win" diff --git a/toolkit/components/downloads/test/browser/browser_DownloadPDFSaver.js b/toolkit/components/downloads/test/browser/browser_DownloadCopySaver.js similarity index 62% rename from toolkit/components/downloads/test/browser/browser_DownloadPDFSaver.js rename to toolkit/components/downloads/test/browser/browser_DownloadCopySaver.js index e38ed3a92bad..fea7792c156a 100644 --- a/toolkit/components/downloads/test/browser/browser_DownloadPDFSaver.js +++ b/toolkit/components/downloads/test/browser/browser_DownloadCopySaver.js @@ -4,8 +4,7 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ /** - * Tests the PDF download saver, and tests using a window as a - * source for the copy download saver. + * Tests using a window as a source for the copy download saver. */ "use strict"; @@ -64,27 +63,12 @@ async function test_createDownload_common(aPrivate, aType) { await download.start(); await test_download_state_complete(tab, download, aPrivate, false); - if (aType == "pdf") { - let signature = await OS.File.read(download.target.path, { - bytes: 4, - encoding: "us-ascii", - }); - is(signature, "%PDF", "File exists and signature matches"); - } else { - ok(await OS.File.exists(download.target.path), "File exists"); - } + ok(await OS.File.exists(download.target.path), "File exists"); win.gBrowser.removeTab(tab); win.close(); } -add_task(async function test_createDownload_pdf_private() { - await test_createDownload_common(true, "pdf"); -}); -add_task(async function test_createDownload_pdf_not_private() { - await test_createDownload_common(false, "pdf"); -}); - // Even for the copy saver, using a window should produce valid results add_task(async function test_createDownload_copy_private() { await test_createDownload_common(true, "copy"); @@ -92,29 +76,3 @@ add_task(async function test_createDownload_copy_private() { add_task(async function test_createDownload_copy_not_private() { await test_createDownload_common(false, "copy"); }); - -add_task(async function test_cancel_pdf_download() { - let tab = BrowserTestUtils.addTab( - gBrowser, - getRootDirectory(gTestPath) + "testFile.html" - ); - await promiseBrowserLoaded(tab.linkedBrowser); - - let download = await Downloads.createDownload({ - source: tab.linkedBrowser.contentWindow, - target: { path: getTempFile(TEST_TARGET_FILE_NAME_PDF).path }, - saver: "pdf", - }); - - await test_download_windowRef(tab, download); - download.start().catch(() => {}); - - // Immediately cancel the download to test that it is erased correctly. - await download.cancel(); - await test_download_state_complete(tab, download, false, true); - - let exists = await OS.File.exists(download.target.path); - ok(!exists, "Target file does not exist"); - - gBrowser.removeTab(tab); -}); diff --git a/toolkit/components/downloads/test/unit/test_DownloadStore.js b/toolkit/components/downloads/test/unit/test_DownloadStore.js index f889011cf00e..5f3de6429e35 100644 --- a/toolkit/components/downloads/test/unit/test_DownloadStore.js +++ b/toolkit/components/downloads/test/unit/test_DownloadStore.js @@ -62,14 +62,6 @@ add_task(async function test_save_reload() { }) ); - // This PDF download should not be serialized because it never succeeds. - let pdfDownload = await Downloads.createDownload({ - source: { url: httpUrl("empty.txt"), referrerInfo }, - target: getTempFile(TEST_TARGET_FILE_NAME), - saver: "pdf", - }); - listForSave.add(pdfDownload); - // If we used a callback to adjust the channel, the download should // not be serialized because we can't recreate it across sessions. let adjustedDownload = await Downloads.createDownload({ @@ -88,9 +80,8 @@ add_task(async function test_save_reload() { await storeForSave.save(); await storeForLoad.load(); - // Remove the PDF and adjusted downloads because they should not appear here. + // Remove the adjusted download because it should not appear here. listForSave.remove(adjustedDownload); - listForSave.remove(pdfDownload); let itemsForSave = await listForSave.getAll(); let itemsForLoad = await listForLoad.getAll(); diff --git a/toolkit/components/downloads/test/unit/test_Downloads.js b/toolkit/components/downloads/test/unit/test_Downloads.js index 86841b9fae00..b99f82300810 100644 --- a/toolkit/components/downloads/test/unit/test_Downloads.js +++ b/toolkit/components/downloads/test/unit/test_Downloads.js @@ -56,34 +56,6 @@ add_task(async function test_createDownload_public() { Assert.ok(!download.source.isPrivate); }); -/** - * Tests createDownload for a pdf saver throws if only given a url. - */ -add_task(async function test_createDownload_pdf() { - let download = await Downloads.createDownload({ - source: { url: "about:blank" }, - target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, - saver: { type: "pdf" }, - }); - - try { - await download.start(); - do_throw("The download should have failed."); - } catch (ex) { - if (!(ex instanceof Downloads.Error) || !ex.becauseSourceFailed) { - throw ex; - } - } - - Assert.ok(!download.succeeded); - Assert.ok(download.stopped); - Assert.ok(!download.canceled); - Assert.ok(download.error !== null); - Assert.ok(download.error.becauseSourceFailed); - Assert.ok(!download.error.becauseTargetFailed); - Assert.equal(false, await OS.File.exists(download.target.path)); -}); - /** * Tests "fetch" with nsIURI and nsIFile as arguments. */ diff --git a/tools/lint/eslint/modules.json b/tools/lint/eslint/modules.json index d494eac739b0..66e7155fc87a 100644 --- a/tools/lint/eslint/modules.json +++ b/tools/lint/eslint/modules.json @@ -49,7 +49,7 @@ "doctor.js": ["Doctor"], "dom.js": ["ContentEventObserverService", "WebElementEventTarget"], "DOMRequestHelper.jsm": ["DOMRequestIpcHelper"], - "DownloadCore.jsm": ["Download", "DownloadSource", "DownloadTarget", "DownloadError", "DownloadSaver", "DownloadCopySaver", "DownloadLegacySaver", "DownloadPDFSaver"], + "DownloadCore.jsm": ["Download", "DownloadSource", "DownloadTarget", "DownloadError", "DownloadSaver", "DownloadCopySaver", "DownloadLegacySaver"], "DownloadList.jsm": ["DownloadList", "DownloadCombinedList", "DownloadSummary"], "driver.js": ["GeckoDriver"], "element.js": ["ChromeWebElement", "ContentWebElement", "ContentWebFrame", "ContentWebWindow", "element", "WebElement"],