Bug 1685184, print selected frame when selecting Print Frame from context menu r=marionette-reviewers,mstriemer

Differential Revision: https://phabricator.services.mozilla.com/D100842
This commit is contained in:
Emma Malysz 2021-01-07 19:34:56 +00:00
Родитель 18e3c81cf5
Коммит c04c6333bc
9 изменённых файлов: 108 добавлений и 34 удалений

Просмотреть файл

@ -6131,7 +6131,7 @@ nsBrowserAccess.prototype = {
let browser = PrintUtils.startPrintWindow(
"window_print",
aOpenWindowInfo.parent,
aOpenWindowInfo
{ openWindowInfo: aOpenWindowInfo }
);
if (browser) {
browsingContext = browser.browsingContext;
@ -6215,7 +6215,7 @@ nsBrowserAccess.prototype = {
return PrintUtils.startPrintWindow(
"window_print",
aParams.openWindowInfo.parent,
aParams.openWindowInfo
{ openWindowInfo: aParams.openWindowInfo }
);
}

Просмотреть файл

@ -1937,7 +1937,8 @@ class nsContextMenu {
printFrame() {
PrintUtils.startPrintWindow(
"context_print_frame",
this.actor.browsingContext
this.actor.browsingContext,
{ printFrameOnly: true }
);
}
@ -1945,8 +1946,7 @@ class nsContextMenu {
PrintUtils.startPrintWindow(
"context_print_selection",
this.actor.browsingContext,
/* aOpenWindowInfo = */ null,
/* aPrintSelectionOnly = */ true
{ printSelectionOnly: true }
);
}

Просмотреть файл

@ -170,7 +170,7 @@ class BrowserDOMWindow {
return PrintUtils.startPrintWindow(
"window_print",
params.openWindowInfo.parent,
params.openWindowInfo
{ openWindowInfo: params.openWindowInfo }
);
}

Просмотреть файл

@ -26,7 +26,7 @@ BrowserDOMWindow.prototype = {
return PrintUtils.startPrintWindow(
"window_print",
aOpenWindowInfo.parent,
aOpenWindowInfo
{ openWindowInfo: aOpenWindowInfo }
);
}
return null;

Просмотреть файл

@ -171,6 +171,7 @@ var PrintEventHandler = {
this.printSelectionOnly = args.getProperty("printSelectionOnly");
this.hasSelection =
args.getProperty("hasSelection") && this.selectionPreviewBrowser;
this.printFrameOnly = args.getProperty("printFrameOnly");
// Get the temporary browser that will previously have been created for the
// platform code to generate the static clone printing doc into if this
// print is for a window.print() call. In that case we steal the browser's
@ -193,8 +194,10 @@ var PrintEventHandler = {
this.originalSourceCurrentURI =
sourceBrowsingContext.currentWindowContext.documentURI.spec;
this.sourceWindowId =
sourceBrowsingContext.top.embedderElement.browsingContext.currentWindowGlobal.outerWindowId;
this.sourceWindowId = this.printFrameOnly
? sourceBrowsingContext.currentWindowGlobal.outerWindowId
: sourceBrowsingContext.top.embedderElement.browsingContext
.currentWindowGlobal.outerWindowId;
this.selectionWindowId =
sourceBrowsingContext.currentWindowGlobal.outerWindowId;

Просмотреть файл

@ -217,6 +217,8 @@ var PrintUtils = {
* @param aPrintSelectionOnly
* Whether to print only the active selection of the given browsing
* context.
* @param aPrintFrameOnly
* Whether to print the selected frame only
* @return promise resolving when the dialog is open, rejected if the preview
* fails.
*/
@ -224,7 +226,8 @@ var PrintUtils = {
aBrowsingContext,
aExistingPreviewBrowser,
aPrintInitiationTime,
aPrintSelectionOnly
aPrintSelectionOnly,
aPrintFrameOnly
) {
let hasSelection = aPrintSelectionOnly;
if (!aPrintSelectionOnly) {
@ -256,6 +259,7 @@ var PrintUtils = {
previewBrowser: aExistingPreviewBrowser,
printSelectionOnly: !!aPrintSelectionOnly,
hasSelection,
printFrameOnly: !!aPrintFrameOnly,
});
let dialogBox = gBrowser.getTabDialogBox(sourceBrowser);
return dialogBox.open(
@ -275,31 +279,34 @@ var PrintUtils = {
* The BrowsingContext of the window to print.
* Note that the browsing context could belong to a subframe of the
* tab that called window.print, or similar shenanigans.
* @param aOpenWindowInfo
* Non-null if this call comes from window.print(). This is the
* nsIOpenWindowInfo object that has to be passed down to
* createBrowser in order for the child process to clone into it.
* @param aOptions
* {openWindowInfo} Non-null if this call comes from window.print().
* This is the nsIOpenWindowInfo object that has to
* be passed down to createBrowser in order for the
* child process to clone into it.
* {printSelectionOnly} Whether to print only the active selection of
* the given browsing context.
* {printFrameOnly} Whether to print the selected frame.
*/
startPrintWindow(
aTrigger,
aBrowsingContext,
aOpenWindowInfo,
aPrintSelectionOnly
) {
startPrintWindow(aTrigger, aBrowsingContext, aOptions) {
const printInitiationTime = Date.now();
let openWindowInfo, printSelectionOnly, printFrameOnly;
if (aOptions) {
({ openWindowInfo, printSelectionOnly, printFrameOnly } = aOptions);
}
// When we have a non-null aOpenWindowInfo, we only want to record
// When we have a non-null openWindowInfo, we only want to record
// telemetry if we're triggered by window.print() itself, otherwise it's an
// internal print (like the one we do when we actually print from the
// preview dialog, etc.), and that'd cause us to overcount.
if (!aOpenWindowInfo || aOpenWindowInfo.isForWindowDotPrint) {
if (!openWindowInfo || openWindowInfo.isForWindowDotPrint) {
Services.telemetry.keyedScalarAdd("printing.trigger", aTrigger, 1);
}
let browser = null;
if (aOpenWindowInfo) {
if (openWindowInfo) {
browser = document.createXULElement("browser");
browser.openWindowInfo = aOpenWindowInfo;
browser.openWindowInfo = openWindowInfo;
browser.setAttribute("type", "content");
let remoteType = aBrowsingContext.currentRemoteType;
if (remoteType) {
@ -324,14 +331,15 @@ var PrintUtils = {
if (
PRINT_TAB_MODAL &&
!PRINT_ALWAYS_SILENT &&
(!aOpenWindowInfo || aOpenWindowInfo.isForWindowDotPrint)
(!openWindowInfo || openWindowInfo.isForWindowDotPrint)
) {
let browsingContext = aBrowsingContext;
let focusedBc = Services.focus.focusedContentBrowsingContext;
if (
focusedBc &&
focusedBc.top.embedderElement == browsingContext.top.embedderElement &&
(!aOpenWindowInfo || !aOpenWindowInfo.isForWindowDotPrint)
(!openWindowInfo || !openWindowInfo.isForWindowDotPrint) &&
!printFrameOnly
) {
browsingContext = focusedBc;
}
@ -339,7 +347,8 @@ var PrintUtils = {
browsingContext,
browser,
printInitiationTime,
aPrintSelectionOnly
printSelectionOnly,
printFrameOnly
).catch(() => {});
return browser;
}
@ -351,7 +360,7 @@ var PrintUtils = {
}
let settings = this.getPrintSettings();
settings.printSelectionOnly = aPrintSelectionOnly;
settings.printSelectionOnly = printSelectionOnly;
this.printWindow(aBrowsingContext, settings);
return null;
},

Просмотреть файл

@ -24,6 +24,7 @@ support-files =
[browser_print_paper_sizes.js]
[browser_pdf_printer_settings.js]
[browser_print_bcg_id_overflow.js]
[browser_print_context_menu.js]
[browser_print_duplex.js]
skip-if = (verify && (os == 'mac')) # bug 1675609
[browser_print_margins.js]

Просмотреть файл

@ -0,0 +1,64 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const frameSource = `<a href="about:mozilla">Inner frame</a>`;
const source = `<html><h1>Top level text</h1><iframe srcdoc='${frameSource}' id="f"></iframe></html>`;
add_task(async function() {
let url = `data:text/html,${source}`;
await BrowserTestUtils.withNewTab({ gBrowser, url }, async function(browser) {
let contentAreaContextMenuPopup = document.getElementById(
"contentAreaContextMenu"
);
let popupShownPromise = BrowserTestUtils.waitForEvent(
contentAreaContextMenuPopup,
"popupshown"
);
await BrowserTestUtils.synthesizeMouseAtCenter(
"#f",
{ type: "contextmenu", button: 2 },
gBrowser.selectedBrowser
);
await popupShownPromise;
let frameContextMenu = document.getElementById("frame");
popupShownPromise = BrowserTestUtils.waitForEvent(
frameContextMenu,
"popupshown"
);
EventUtils.synthesizeMouseAtCenter(frameContextMenu, {});
await popupShownPromise;
let popupHiddenPromise = BrowserTestUtils.waitForEvent(
frameContextMenu,
"popuphidden"
);
let item = document.getElementById("context-printframe");
EventUtils.synthesizeMouseAtCenter(item, {});
await popupHiddenPromise;
await BrowserTestUtils.waitForCondition(
() => !!document.querySelector(".printPreviewBrowser")
);
let previewBrowser = document.querySelector(
".printPreviewBrowser[previewtype='primary']"
);
let helper = new PrintHelper(browser);
let textContent = await TestUtils.waitForCondition(() =>
SpecialPowers.spawn(previewBrowser, [], function() {
return content.document.body.textContent;
})
);
is(textContent, "Inner frame", "Correct content loaded");
is(
helper.win.PrintEventHandler.printFrameOnly,
true,
"Print frame only is true"
);
PrintHelper.resetPrintPrefs();
});
});

Просмотреть файл

@ -44,12 +44,9 @@ add_task(async function print_selection() {
let helper = new PrintHelper(browser);
// If you change this, change nsContextMenu.printSelection() too.
PrintUtils.startPrintWindow(
"tests",
frameBC,
null,
/* aSelectionOnly = */ true
);
PrintUtils.startPrintWindow("tests", frameBC, {
printSelectionOnly: true,
});
await BrowserTestUtils.waitForCondition(
() => !!document.querySelector(".printPreviewBrowser")