< prev index next >

modules/javafx.web/src/main/java/com/sun/webkit/WebPage.java

Print this page
rev 10053 : 8150982: Crash when calling WebEngine.print on background thread
Reviewed-by:

@@ -52,10 +52,11 @@
 import java.util.Queue;
 import java.util.Set;
 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;
 import netscape.javascript.JSException;
 import org.w3c.dom.Document;

@@ -1759,11 +1760,27 @@
         try {
             if (isDisposed) {
                 log.warning("beginPrinting() called for a disposed web page.");
                 return 0;
             }
-            return twkBeginPrinting(getPage(), width, height);
+            AtomicReference<Integer> 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();
         }
     }
 

@@ -1772,12 +1789,25 @@
         try {
             if (isDisposed) {
                 log.warning("endPrinting() called for a disposed web page.");
                 return;
             }
+            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();
         }
     }
 
     public void print(final WCGraphicsContext gc, final int pageNumber, final float width) {
< prev index next >