From acd37837de51b12de6bc9e3dd0deebcfca300243 Mon Sep 17 00:00:00 2001 From: Phil Race Date: Sun, 12 Feb 2017 08:10:34 -0800 Subject: [PATCH] 8174098: Better image fetching Reviewed-by: serb, vadim, skoivu --- .../classes/sun/awt/image/ImageWatched.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/jdk/src/java.desktop/share/classes/sun/awt/image/ImageWatched.java b/jdk/src/java.desktop/share/classes/sun/awt/image/ImageWatched.java index b740aa1870e..07d964659bd 100644 --- a/jdk/src/java.desktop/share/classes/sun/awt/image/ImageWatched.java +++ b/jdk/src/java.desktop/share/classes/sun/awt/image/ImageWatched.java @@ -29,6 +29,10 @@ import java.lang.ref.WeakReference; import java.awt.Image; import java.awt.image.ImageObserver; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.PrivilegedAction; + public abstract class ImageWatched { public static Link endlink = new Link(); @@ -85,16 +89,26 @@ public abstract class ImageWatched { } } + static class AccWeakReference extends WeakReference { + + private final AccessControlContext acc; + + AccWeakReference(T ref) { + super(ref); + acc = AccessController.getContext(); + } + } + /* * Standard Link implementation to manage a Weak Reference * to an ImageObserver. */ public static class WeakLink extends Link { - private WeakReference myref; + private final AccWeakReference myref; private Link next; public WeakLink(ImageObserver obs, Link next) { - myref = new WeakReference(obs); + myref = new AccWeakReference(obs); this.next = next; } @@ -120,6 +134,19 @@ public abstract class ImageWatched { return this; } + private static boolean update(ImageObserver iw, AccessControlContext acc, + Image img, int info, + int x, int y, int w, int h) { + + if (acc != null || System.getSecurityManager() != null) { + return AccessController.doPrivileged( + (PrivilegedAction) () -> { + return iw.imageUpdate(img, info, x, y, w, h); + }, acc); + } + return false; + } + public boolean newInfo(Image img, int info, int x, int y, int w, int h) { @@ -129,7 +156,7 @@ public abstract class ImageWatched { if (myiw == null) { // My referent is null so we must prune in a second pass. ret = true; - } else if (myiw.imageUpdate(img, info, x, y, w, h) == false) { + } else if (update(myiw, myref.acc, img, info, x, y, w, h) == false) { // My referent has lost interest so clear it and ask // for a pruning pass to remove it later. myref.clear();