From 0cd8a95d566a80e50a5fa5415a0e2aa923e027c8 Mon Sep 17 00:00:00 2001 From: chadb Date: Mon, 11 Apr 2022 19:06:45 +0000 Subject: [PATCH] Bug 1651728 Remove reader mode toolbar from saved document. r=niklas,mtigley Differential Revision: https://phabricator.services.mozilla.com/D138562 --- browser/actors/AboutReaderChild.jsm | 10 ++ .../components/downloads/DownloadLegacy.jsm | 31 ++++++- toolkit/components/reader/test/browser.ini | 4 + .../test/browser_readerMode_download.js | 91 +++++++++++++++++++ toolkit/content/contentAreaUtils.js | 11 +++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 toolkit/components/reader/test/browser_readerMode_download.js diff --git a/browser/actors/AboutReaderChild.jsm b/browser/actors/AboutReaderChild.jsm index 562ee4cf06c3..7f247418a400 100644 --- a/browser/actors/AboutReaderChild.jsm +++ b/browser/actors/AboutReaderChild.jsm @@ -77,6 +77,16 @@ class AboutReaderChild extends JSWindowActorChild { ReaderMode.leaveReaderMode(this.docShell, this.contentWindow); break; } + case "Reader:HideToolbar": { + this.toolbar = this.document.getElementById("toolbar"); + this.toolbar.hidden = true; + break; + } + case "Reader:ShowToolbar": { + this.toolbar = this.document.getElementById("toolbar"); + this.toolbar.hidden = false; + break; + } } // Forward the message to the reader if it has been created. diff --git a/toolkit/components/downloads/DownloadLegacy.jsm b/toolkit/components/downloads/DownloadLegacy.jsm index 7471eecb0776..0fbc623e03d7 100644 --- a/toolkit/components/downloads/DownloadLegacy.jsm +++ b/toolkit/components/downloads/DownloadLegacy.jsm @@ -16,6 +16,14 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +XPCOMUtils.defineLazyModuleGetters(this, { + BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm", +}); + ChromeUtils.defineModuleGetter( this, "Downloads", @@ -417,8 +425,27 @@ DownloadLegacyTransfer.prototype = { aDownload.tryToKeepPartialData = true; } - // Start the download before allowing it to be controlled. Ignore errors. - aDownload.start().catch(() => {}); + // Hide toolbar before download if we're in reader mode + let aboutReaderCb = () => {}; + + if (aDownload.source.url.includes("about:reader")) { + let browserWin = BrowserWindowTracker.getTopWindow(); + let actor = browserWin.gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.getActor( + "AboutReader" + ); + actor.sendQuery("Reader:HideToolbar"); + aboutReaderCb = () => { + actor.sendQuery("Reader:ShowToolbar"); + }; + } + + // Start the download before allwoing it to be controlled. Ignore errors. + aDownload + .start() + .then(() => { + aboutReaderCb(); + }) + .catch(() => {}); // Start processing all the other events received through nsITransfer. this._download = aDownload; diff --git a/toolkit/components/reader/test/browser.ini b/toolkit/components/reader/test/browser.ini index 3d67c345ec36..2ae4306f0eea 100644 --- a/toolkit/components/reader/test/browser.ini +++ b/toolkit/components/reader/test/browser.ini @@ -14,6 +14,10 @@ support-files = [browser_readerMode_colorSchemePref.js] support-files = readerModeArticle.html +[browser_readerMode_download.js] +skip-if = os == "linux" && headless +support-files = + readerModeArticleShort.html [browser_readerMode_hidden_nodes.js] skip-if = debug && os == "linux" && os_version == "18.04" #bug 1638027 support-files = diff --git a/toolkit/components/reader/test/browser_readerMode_download.js b/toolkit/components/reader/test/browser_readerMode_download.js new file mode 100644 index 000000000000..9f0d64b7c9fc --- /dev/null +++ b/toolkit/components/reader/test/browser_readerMode_download.js @@ -0,0 +1,91 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// This test verifies that the toolbar is hidden when saving in reader mode + +const TEST_PATH = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "http://example.com" +); + +var MockFilePicker = SpecialPowers.MockFilePicker; +MockFilePicker.init(window); + +function createTemporarySaveDirectory() { + var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + saveDir.append("testsavedir"); + if (!saveDir.exists()) { + saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); + } + return saveDir; +} + +function downloadHadFinished(publicList) { + return new Promise(resolve => { + publicList.addView({ + onDownloadChanged(download) { + if (download.succeeded || download.error) { + publicList.removeView(this); + resolve(download); + } + }, + }); + }); +} + +/** + * Test that the reader modes toolbar is hidden on page download + */ +add_task(async function() { + // Open the page in reader mode + await BrowserTestUtils.withNewTab( + TEST_PATH + "readerModeArticleShort.html", + async function(browser) { + let pageShownPromise = BrowserTestUtils.waitForContentEvent( + browser, + "AboutReaderContentReady" + ); + let readerButton = document.getElementById("reader-mode-button"); + readerButton.click(); + await pageShownPromise; + // Reader mode page open -- Begin downloading the Page + var fileName; + + var destDir = createTemporarySaveDirectory(); + var destFile = destDir.clone(); + MockFilePicker.displayDirectory = destDir; + MockFilePicker.showCallback = function(fp) { + fileName = fp.defaultString; + destFile.append(fileName); + MockFilePicker.setFiles([destFile]); + MockFilePicker.filterIndex = 1; // kSaveAsType_URL + }; + + let fileSavePageAsElement = document.getElementById("menu_savePage"); + fileSavePageAsElement.doCommand(); + + // Wait for the download to complete + let publicList = await Downloads.getList(Downloads.PUBLIC); + + let downloadFinishedPromise = downloadHadFinished(publicList); + + let download = await downloadFinishedPromise; + + // Open the downloaded page + let fileDir = PathUtils.join(download.target.path); + let loadPromise = BrowserTestUtils.browserLoaded( + gBrowser.selectedBrowser + ); + BrowserTestUtils.loadURI(gBrowser.selectedBrowser, fileDir); + await loadPromise; + // Check that the toolbar is hidden + await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { + Assert.ok( + content.document.getElementById("toolbar").hidden, + "The toolbar is hidden" + ); + }); + } + ); +}); diff --git a/toolkit/content/contentAreaUtils.js b/toolkit/content/contentAreaUtils.js index af2ca0e04af2..f02bea72227b 100644 --- a/toolkit/content/contentAreaUtils.js +++ b/toolkit/content/contentAreaUtils.js @@ -326,6 +326,17 @@ function internalSave( promiseTargetFile(fpParams, aSkipPrompt, relatedURI) .then(aDialogAccepted => { if (!aDialogAccepted) { + let browserWin = BrowserWindowTracker.getTopWindow(); + if ( + browserWin.gBrowser.selectedBrowser.currentURI.spec.startsWith( + "about:reader?" + ) + ) { + let actor = browserWin.gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.getActor( + "AboutReader" + ); + actor.sendQuery("Reader:ShowToolbar"); + } return; }