modules/graphics/src/main/java/javafx/concurrent/Task.java

Print this page
rev 8410 : RT-39421: Security exception in Service.cancel when running sandboxed applet


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.concurrent;
  27 



  28 import javafx.application.Platform;
  29 import javafx.beans.property.BooleanProperty;
  30 import javafx.beans.property.DoubleProperty;
  31 import javafx.beans.property.ObjectProperty;
  32 import javafx.beans.property.ReadOnlyBooleanProperty;
  33 import javafx.beans.property.ReadOnlyDoubleProperty;
  34 import javafx.beans.property.ReadOnlyObjectProperty;
  35 import javafx.beans.property.ReadOnlyStringProperty;
  36 import javafx.beans.property.SimpleBooleanProperty;
  37 import javafx.beans.property.SimpleDoubleProperty;
  38 import javafx.beans.property.SimpleObjectProperty;
  39 import javafx.beans.property.SimpleStringProperty;
  40 import javafx.beans.property.StringProperty;
  41 import javafx.event.Event;
  42 import javafx.event.EventDispatchChain;
  43 import javafx.event.EventHandler;
  44 import javafx.event.EventTarget;
  45 import javafx.event.EventType;
  46 import java.util.concurrent.Callable;
  47 import java.util.concurrent.FutureTask;


 977     @Override public final double getProgress() { checkThread(); return progress.get(); }
 978     @Override public final ReadOnlyDoubleProperty progressProperty() { checkThread(); return progress; }
 979 
 980     private final BooleanProperty running = new SimpleBooleanProperty(this, "running", false);
 981     private void setRunning(boolean value) { checkThread(); running.set(value); }
 982     @Override public final boolean isRunning() { checkThread(); return running.get(); }
 983     @Override public final ReadOnlyBooleanProperty runningProperty() { checkThread(); return running; }
 984 
 985     private final StringProperty message = new SimpleStringProperty(this, "message", "");
 986     @Override public final String getMessage() { checkThread(); return message.get(); }
 987     @Override public final ReadOnlyStringProperty messageProperty() { checkThread(); return message; }
 988 
 989     private final StringProperty title = new SimpleStringProperty(this, "title", "");
 990     @Override public final String getTitle() { checkThread(); return title.get(); }
 991     @Override public final ReadOnlyStringProperty titleProperty() { checkThread(); return title; }
 992 
 993     @Override public final boolean cancel() {
 994         return cancel(true);
 995     }
 996 






 997     @Override public boolean cancel(boolean mayInterruptIfRunning) {
 998         // Delegate to the super implementation to actually attempt to cancel this thing
 999         boolean flag = super.cancel(mayInterruptIfRunning);





1000         // If cancel succeeded (according to the semantics of the Future cancel method),
1001         // then we need to make sure the State flag is set appropriately
1002         if (flag) {
1003             // If this method was called on the FX application thread, then we can
1004             // just update the state directly and this will make sure that after
1005             // the cancel method was called, the state will be set correctly
1006             // (otherwise it would be indeterminate). However if the cancel method was
1007             // called off the FX app thread, then we must use runLater, and the
1008             // state flag will not be readable immediately after this call. However,
1009             // that would be the case anyway since these properties are not thread-safe.
1010             if (isFxApplicationThread()) {
1011                 setState(State.CANCELLED);
1012             } else {
1013                 runLater(() -> setState(State.CANCELLED));
1014             }
1015         }
1016         // return the flag
1017         return flag;
1018     }
1019 




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.concurrent;
  27 
  28 import java.security.AccessController;
  29 import java.security.Permission;
  30 import java.security.PrivilegedAction;
  31 import javafx.application.Platform;
  32 import javafx.beans.property.BooleanProperty;
  33 import javafx.beans.property.DoubleProperty;
  34 import javafx.beans.property.ObjectProperty;
  35 import javafx.beans.property.ReadOnlyBooleanProperty;
  36 import javafx.beans.property.ReadOnlyDoubleProperty;
  37 import javafx.beans.property.ReadOnlyObjectProperty;
  38 import javafx.beans.property.ReadOnlyStringProperty;
  39 import javafx.beans.property.SimpleBooleanProperty;
  40 import javafx.beans.property.SimpleDoubleProperty;
  41 import javafx.beans.property.SimpleObjectProperty;
  42 import javafx.beans.property.SimpleStringProperty;
  43 import javafx.beans.property.StringProperty;
  44 import javafx.event.Event;
  45 import javafx.event.EventDispatchChain;
  46 import javafx.event.EventHandler;
  47 import javafx.event.EventTarget;
  48 import javafx.event.EventType;
  49 import java.util.concurrent.Callable;
  50 import java.util.concurrent.FutureTask;


 980     @Override public final double getProgress() { checkThread(); return progress.get(); }
 981     @Override public final ReadOnlyDoubleProperty progressProperty() { checkThread(); return progress; }
 982 
 983     private final BooleanProperty running = new SimpleBooleanProperty(this, "running", false);
 984     private void setRunning(boolean value) { checkThread(); running.set(value); }
 985     @Override public final boolean isRunning() { checkThread(); return running.get(); }
 986     @Override public final ReadOnlyBooleanProperty runningProperty() { checkThread(); return running; }
 987 
 988     private final StringProperty message = new SimpleStringProperty(this, "message", "");
 989     @Override public final String getMessage() { checkThread(); return message.get(); }
 990     @Override public final ReadOnlyStringProperty messageProperty() { checkThread(); return message; }
 991 
 992     private final StringProperty title = new SimpleStringProperty(this, "title", "");
 993     @Override public final String getTitle() { checkThread(); return title.get(); }
 994     @Override public final ReadOnlyStringProperty titleProperty() { checkThread(); return title; }
 995 
 996     @Override public final boolean cancel() {
 997         return cancel(true);
 998     }
 999 
1000     // Need to assert the modifyThread permission so an app can cancel
1001     // a task that it created (the default executor for the service runs in
1002     // its own thread group)
1003     // Note that this is needed when running as an applet or a web start app.
1004     private static final Permission modifyThreadPerm = new RuntimePermission("modifyThread");
1005 
1006     @Override public boolean cancel(boolean mayInterruptIfRunning) {
1007         // Delegate to the super implementation to actually attempt to cancel this thing
1008         // Assert the modifyThread permission
1009         boolean flag = AccessController.doPrivileged(
1010             (PrivilegedAction<Boolean>) () -> super.cancel(mayInterruptIfRunning),
1011             null,
1012             modifyThreadPerm);
1013 
1014         // If cancel succeeded (according to the semantics of the Future cancel method),
1015         // then we need to make sure the State flag is set appropriately
1016         if (flag) {
1017             // If this method was called on the FX application thread, then we can
1018             // just update the state directly and this will make sure that after
1019             // the cancel method was called, the state will be set correctly
1020             // (otherwise it would be indeterminate). However if the cancel method was
1021             // called off the FX app thread, then we must use runLater, and the
1022             // state flag will not be readable immediately after this call. However,
1023             // that would be the case anyway since these properties are not thread-safe.
1024             if (isFxApplicationThread()) {
1025                 setState(State.CANCELLED);
1026             } else {
1027                 runLater(() -> setState(State.CANCELLED));
1028             }
1029         }
1030         // return the flag
1031         return flag;
1032     }
1033