Bug 1660739 - Fix tabs.printPreview API with the new print preview design. r=mstriemer,mixedpuppy

Return a promise from PrintUtils.printPreview which resolves when the
new print preview dialog is closed.

Differential Revision: https://phabricator.services.mozilla.com/D88013
This commit is contained in:
Emilio Cobos Álvarez 2020-08-26 03:10:47 +00:00
parent 04ebe0fe0e
commit 5b82674837
5 changed files with 85 additions and 56 deletions

View File

@ -29,7 +29,7 @@
<command id="cmd_pageSetup" oncommand="PrintUtils.showPageSetup();"/>
<command id="cmd_print" oncommand="PrintUtils.startPrintWindow(window.gBrowser.selectedBrowser.browsingContext);"/>
<command id="cmd_printPreview" oncommand="PrintUtils.printPreview(PrintPreviewListener);"/>
<command id="cmd_printPreview" oncommand="PrintUtils.printPreview(PrintPreviewListener).catch(() => {});"/>
<command id="cmd_file_importFromAnotherBrowser" oncommand="MigrationUtils.showMigrationWizard(window, [MigrationUtils.MIGRATION_ENTRYPOINT_FILE_MENU]);"/>
<command id="cmd_help_importFromAnotherBrowser" oncommand="MigrationUtils.showMigrationWizard(window, [MigrationUtils.MIGRATION_ENTRYPOINT_HELP_MENU]);"/>
<command id="cmd_close" oncommand="BrowserCloseTabOrWindow(event);"/>

View File

@ -8927,6 +8927,10 @@ class TabDialogBox {
if (event.type !== "TabClose") {
return;
}
this.abortAllDialogs();
}
abortAllDialogs() {
this._dialogManager.abortAll();
}
@ -8941,7 +8945,7 @@ class TabDialogBox {
) {
return;
}
this._dialogManager.abortAll();
this.abortAllDialogs();
}
get tab() {

View File

@ -1259,29 +1259,14 @@ this.tabs = class extends ExtensionAPI {
PrintUtils.startPrintWindow(activeTab.linkedBrowser.browsingContext);
},
printPreview() {
async printPreview() {
let activeTab = getTabOrActive(null);
let { PrintUtils, PrintPreviewListener } = activeTab.ownerGlobal;
return new Promise((resolve, reject) => {
let ppBrowser = PrintUtils.shouldSimplify
? PrintPreviewListener.getSimplifiedPrintPreviewBrowser()
: PrintPreviewListener.getPrintPreviewBrowser();
let mm = ppBrowser.messageManager;
let onEntered = message => {
mm.removeMessageListener("Printing:Preview:Entered", onEntered);
if (message.data.failed) {
reject({ message: "Print preview failed" });
}
resolve();
};
mm.addMessageListener("Printing:Preview:Entered", onEntered);
PrintUtils.printPreview(PrintPreviewListener);
});
try {
await PrintUtils.printPreview(PrintPreviewListener);
} catch (ex) {
return Promise.reject({ message: "Print preview failed" });
}
},
saveAsPDF(pageSettings) {

View File

@ -2,10 +2,7 @@
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
add_task(async function testPrintPreview() {
await SpecialPowers.pushPrefEnv({
set: [["print.tab_modal.enabled", false]],
});
async function testPrintPreview() {
await BrowserTestUtils.openNewForegroundTab(gBrowser, "http://example.net/");
let extension = ExtensionTestUtils.loadExtension({
@ -20,31 +17,58 @@ add_task(async function testPrintPreview() {
},
});
await extension.startup();
await extension.awaitFinish("tabs.printPreview");
await extension.unload();
let ppTab = PrintUtils.shouldSimplify
? PrintPreviewListener._simplifiedPrintPreviewTab
: PrintPreviewListener._printPreviewTab;
let ppToolbar = document.getElementById("print-preview-toolbar");
is(window.gInPrintPreviewMode, true, "window in print preview mode");
isnot(ppTab, null, "print preview tab created");
isnot(ppTab.linkedBrowser, null, "print preview browser created");
isnot(ppToolbar, null, "print preview toolbar created");
is(ppTab, gBrowser.selectedTab, "print preview tab selected");
is(
ppTab.linkedBrowser.currentURI.spec,
"about:printpreview",
"print preview browser url correct"
document.querySelector(".printPreviewBrowser"),
null,
"There shouldn't be any print preview browser"
);
PrintUtils.exitPrintPreview();
await BrowserTestUtils.waitForCondition(() => !window.gInPrintPreviewMode);
await extension.startup();
if (!Services.prefs.getBoolPref("print.tab_modal.enabled")) {
await extension.awaitFinish("tabs.printPreview");
let ppTab = PrintUtils.shouldSimplify
? PrintPreviewListener._simplifiedPrintPreviewTab
: PrintPreviewListener._printPreviewTab;
let ppToolbar = document.getElementById("print-preview-toolbar");
is(window.gInPrintPreviewMode, true, "window in print preview mode");
isnot(ppTab, null, "print preview tab created");
isnot(ppTab.linkedBrowser, null, "print preview browser created");
isnot(ppToolbar, null, "print preview toolbar created");
is(ppTab, gBrowser.selectedTab, "print preview tab selected");
is(
ppTab.linkedBrowser.currentURI.spec,
"about:printpreview",
"print preview browser url correct"
);
PrintUtils.exitPrintPreview();
await BrowserTestUtils.waitForCondition(() => !window.gInPrintPreviewMode);
} else {
// Ensure we're showing the preview...
await BrowserTestUtils.waitForCondition(
() => !!document.querySelector(".printPreviewBrowser")
);
gBrowser.getTabDialogBox(gBrowser.selectedBrowser).abortAllDialogs();
await extension.awaitFinish("tabs.printPreview");
}
await extension.unload();
BrowserTestUtils.removeTab(gBrowser.tabs[1]);
}
add_task(async function() {
for (let prefValue of [false, true]) {
info("Testing with tab modal enabled: " + prefValue);
await SpecialPowers.pushPrefEnv({
set: [["print.tab_modal.enabled", prefValue]],
});
await testPrintPreview();
}
});

View File

@ -146,8 +146,10 @@ var PrintUtils = {
* The BrowsingContext of the window to print.
* @param aExistingPreviewBrowser
* An existing browser created for printing from window.print().
* @return promise resolving when the dialog is open, rejected if the preview
* fails.
*/
_openTabModalPrint(aBrowsingContext, aExistingPreviewBrowser) {
async _openTabModalPrint(aBrowsingContext, aExistingPreviewBrowser) {
let sourceBrowser = aBrowsingContext.top.embedderElement;
let previewBrowser = this.getPreviewBrowser(sourceBrowser);
if (previewBrowser) {
@ -159,7 +161,7 @@ var PrintUtils = {
if (aExistingPreviewBrowser) {
aExistingPreviewBrowser.remove();
}
return;
return Promise.reject();
}
// Create a preview browser.
@ -167,7 +169,7 @@ var PrintUtils = {
previewBrowser: aExistingPreviewBrowser,
});
let dialogBox = gBrowser.getTabDialogBox(sourceBrowser);
dialogBox.open(
return dialogBox.open(
`chrome://global/content/print.html?browsingContextId=${aBrowsingContext.id}`,
"resizable=no",
args,
@ -216,7 +218,7 @@ var PrintUtils = {
!PRINT_ALWAYS_SILENT &&
(!aOpenWindowInfo || aOpenWindowInfo.isForPrintPreview)
) {
this._openTabModalPrint(aBrowsingContext, browser);
this._openTabModalPrint(aBrowsingContext, browser).catch(() => {});
return browser;
}
@ -322,8 +324,7 @@ var PrintUtils = {
*/
printPreview(aListenerObj) {
if (PRINT_TAB_MODAL) {
this._openTabModalPrint(gBrowser.selectedBrowser.browsingContext);
return;
return this._openTabModalPrint(gBrowser.selectedBrowser.browsingContext);
}
// If we already have a toolbar someone is calling printPreview() to get us
@ -369,6 +370,11 @@ var PrintUtils = {
let PPROMPTSVC = Cc[
"@mozilla.org/embedcomp/printingprompt-service;1"
].getService(Ci.nsIPrintingPromptService);
let promise = new Promise((resolve, reject) => {
this._onEntered.push({ resolve, reject });
});
// just in case we are already printing,
// an error code could be returned if the Progress Dialog is already displayed
try {
@ -394,6 +400,7 @@ var PrintUtils = {
} catch (e) {
this._enterPrintPreview();
}
return promise;
},
// "private" methods and members. Don't use them.
@ -608,6 +615,8 @@ var PrintUtils = {
this._shouldSimplify = shouldSimplify;
},
_onEntered: [],
/**
* Currently, we create a new print preview browser to host the simplified
* cloned-document when Simplify Page option is used on preview. To accomplish
@ -706,7 +715,14 @@ var PrintUtils = {
let onEntered = message => {
mm.removeMessageListener("Printing:Preview:Entered", onEntered);
for (let { resolve, reject } of this._onEntered) {
if (message.data.failed) {
reject();
} else {
resolve();
}
}
this._onEntered = [];
if (message.data.failed) {
// Something went wrong while putting the document into print preview
// mode. Bail out.