changeset 742:a486f1493133

Handle resizing plugin message more robustly by not blocking worker thread
author Adam Domurad <adomurad@redhat.com>
date Mon, 03 Jun 2013 12:25:50 -0400
parents d6f6c5524acc
children 07f37cd6d7bc
files ChangeLog plugin/icedteanp/java/sun/applet/PluginAppletViewer.java
diffstat 2 files changed, 52 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jun 03 10:51:47 2013 -0400
+++ b/ChangeLog	Mon Jun 03 12:25:50 2013 -0400
@@ -1,3 +1,8 @@
+2013-06-03  Adam Domurad  <adomurad@redhat.com>
+
+	* plugin/icedteanp/java/sun/applet/PluginAppletViewer.java:
+	Handle resizing more robustly by not blocking worker thread 
+
 2013-06-03  Adam Domurad  <adomurad@redhat.com>
 
 	* netx/net/sourceforge/jnlp/util/StreamUtils.java
--- a/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Mon Jun 03 10:51:47 2013 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Mon Jun 03 12:25:50 2013 -0400
@@ -677,53 +677,62 @@
         PluginDebug.debug("Applet panel ", panel, " initialized");
     }
 
+    /* Resizes an applet panel, waiting for the applet to initialze. 
+     * Should be done asynchronously to avoid the chance of deadlock. */
+    private void resizeAppletPanel(final int width, final int height) {
+        // Wait for panel to come alive
+        waitForAppletInit(panel);
+
+        SwingUtilities.invokeLater(new Runnable() {
+            public void run() {
+                panel.updateSizeInAtts(height, width);
+
+                setSize(width, height);
+
+                // There is a rather odd drawing bug whereby resizing
+                // the panel makes no difference on initial call
+                // because the panel thinks that it is already the
+                // right size. Validation has no effect there either.
+                // So we work around by setting size to 1, validating,
+                // and then setting to the right size and validating
+                // again. This is not very efficient, and there is
+                // probably a better way -- but resizing happens
+                // quite infrequently, so for now this is how we do it
+
+                panel.setSize(1, 1);
+                panel.validate();
+
+                panel.setSize(width, height);
+                panel.validate();
+
+                panel.applet.resize(width, height);
+                panel.applet.validate();
+            }
+        });
+    }
+
     public void handleMessage(int reference, String message) {
         if (message.startsWith("width")) {
 
-            // Wait for panel to come alive
-            waitForAppletInit(panel);
-
             // 0 => width, 1=> width_value, 2 => height, 3=> height_value
             String[] dimMsg = message.split(" ");
 
+            final int width = Integer.parseInt(dimMsg[1]);
             final int height = Integer.parseInt(dimMsg[3]);
-            final int width = Integer.parseInt(dimMsg[1]);
-
-            panel.updateSizeInAtts(height, width);
-
-            try {
-                SwingUtilities.invokeAndWait(new Runnable() {
-                    public void run() {
-
-                        setSize(width, height);
 
-                        // There is a rather odd drawing bug whereby resizing
-                        // the panel makes no difference on initial call
-                        // because the panel thinks that it is already the
-                        // right size. Validation has no effect there either.
-                        // So we work around by setting size to 1, validating,
-                        // and then setting to the right size and validating
-                        // again. This is not very efficient, and there is
-                        // probably a better way -- but resizing happens
-                        // quite infrequently, so for now this is how we do it
-
-                        panel.setSize(1, 1);
-                        panel.validate();
+            /* Resize the applet asynchronously, to avoid the chance of 
+             * deadlock while waiting for the applet to initialize. 
+             * 
+             * In general, worker threads should spawn new threads for any blocking operations. */
+            Thread resizeAppletThread = new Thread("resizeAppletThread") {
+                @Override
+                public void run() {
+                    resizeAppletPanel(width, height);
+                }
+            };
 
-                        panel.setSize(width, height);
-                        panel.validate();
-
-                        panel.applet.resize(width, height);
-                        panel.applet.validate();
-                    }
-                });
-            } catch (InterruptedException e) {
-                // do nothing
-                e.printStackTrace();
-            } catch (InvocationTargetException e) {
-                // do nothing
-                e.printStackTrace();
-            }
+            /* Let it eventually complete */
+            resizeAppletThread.start();
 
         } else if (message.startsWith("GetJavaObject")) {