# HG changeset patch # User kcr # Date 1475086886 25200 # Node ID c16228e37dd3d14f3d65612aa02607fa50372fd3 # Parent e3061a3b2ddd7385a6689269dfeeceacf4526e6b 8150982: Crash when calling WebEngine.print on background thread Reviewed-by: diff --git a/modules/javafx.web/src/main/java/com/sun/webkit/WebPage.java b/modules/javafx.web/src/main/java/com/sun/webkit/WebPage.java --- a/modules/javafx.web/src/main/java/com/sun/webkit/WebPage.java +++ b/modules/javafx.web/src/main/java/com/sun/webkit/WebPage.java @@ -54,6 +54,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; +import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; @@ -1761,7 +1762,23 @@ log.warning("beginPrinting() called for a disposed web page."); return 0; } - return twkBeginPrinting(getPage(), width, height); + AtomicReference retVal = new AtomicReference<>(0); + final CountDownLatch l = new CountDownLatch(1); + Invoker.getInvoker().invokeOnEventThread(() -> { + try { + int nPages = twkBeginPrinting(getPage(), width, height); + retVal.set(nPages); + } finally { + l.countDown(); + } + }); + + try { + l.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return retVal.get(); } finally { unlockPage(); } @@ -1774,7 +1791,20 @@ log.warning("endPrinting() called for a disposed web page."); return; } - twkEndPrinting(getPage()); + final CountDownLatch l = new CountDownLatch(1); + Invoker.getInvoker().invokeOnEventThread(() -> { + try { + twkEndPrinting(getPage()); + } finally { + l.countDown(); + } + }); + + try { + l.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } finally { unlockPage(); } diff --git a/modules/javafx.web/src/main/java/javafx/scene/web/WebEngine.java b/modules/javafx.web/src/main/java/javafx/scene/web/WebEngine.java --- a/modules/javafx.web/src/main/java/javafx/scene/web/WebEngine.java +++ b/modules/javafx.web/src/main/java/javafx/scene/web/WebEngine.java @@ -1599,6 +1599,16 @@ } } + private static final boolean printStatusOK(PrinterJob job) { + switch (job.getJobStatus()) { + case NOT_STARTED: + case PRINTING: + return true; + default: + return false; + } + } + /** * Prints the current Web page using the given printer job. *

This method does not modify the state of the job, nor does it call @@ -1608,14 +1618,20 @@ * @since JavaFX 8.0 */ public void print(PrinterJob job) { + if (!printStatusOK(job)) { + return; + } + PageLayout pl = job.getJobSettings().getPageLayout(); float width = (float) pl.getPrintableWidth(); float height = (float) pl.getPrintableHeight(); int pageCount = page.beginPrinting(width, height); for (int i = 0; i < pageCount; i++) { - Node printable = new Printable(page, i, width); - job.printPage(printable); + if (printStatusOK(job)) { + Node printable = new Printable(page, i, width); + job.printPage(printable); + } } page.endPrinting(); }