Skip to content

Commit

Permalink
print: Make the firefox printing code able to fail and be re-invoked.
Browse files Browse the repository at this point in the history
This fixes a set of issues described in Mozilla bug 1662426[1].

In particular, once the print callback fails once (because the printing
operation has been canceled in Gecko / replaced by a newer one, for example) it
can't be re-invoked.

This patch fixes it by properly cancelling the render task if it throws, or if
the print callback is called again while ongoing.

[1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1662426
  • Loading branch information
emilio committed Sep 2, 2020
1 parent ed9ea8f commit 13cd49d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion web/firefox_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function composePage(
canvasWrapper.appendChild(canvas);
printContainer.appendChild(canvasWrapper);

let currentRenderTask = null;
canvas.mozPrintCallback = function (obj) {
// Printing/rendering the page.
const ctx = obj.context;
Expand All @@ -50,9 +51,14 @@ function composePage(
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();

let thisRenderTask = null;
pdfDocument
.getPage(pageNumber)
.then(function (pdfPage) {
if (currentRenderTask) {
currentRenderTask.cancel();
currentRenderTask = null;
}
const renderContext = {
canvasContext: ctx,
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
Expand All @@ -61,15 +67,25 @@ function composePage(
annotationStorage: pdfDocument.annotationStorage,
optionalContentConfigPromise,
};
return pdfPage.render(renderContext).promise;
currentRenderTask = thisRenderTask = pdfPage.render(renderContext);
return thisRenderTask.promise;
})
.then(
function () {
// Tell the printEngine that rendering this canvas/page has finished.
if (currentRenderTask == thisRenderTask) {
currentRenderTask = null;
}
obj.done();
},
function (error) {
console.error(error);

if (currentRenderTask == thisRenderTask) {
currentRenderTask.cancel();
currentRenderTask = null;
}

// Tell the printEngine that rendering this canvas/page has failed.
// This will make the print process stop.
if ("abort" in obj) {
Expand Down

0 comments on commit 13cd49d

Please sign in to comment.