src/java.desktop/share/classes/java/awt/Desktop.java
changeset 51152 edd69f959190
parent 50835 aaf263fe7eba
child 51363 a6fa2016cff1
equal deleted inserted replaced
51151:d6b131d2bc8b 51152:edd69f959190
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    40 import java.io.File;
    40 import java.io.File;
    41 import java.io.FilePermission;
    41 import java.io.FilePermission;
    42 import java.io.IOException;
    42 import java.io.IOException;
    43 import java.net.URI;
    43 import java.net.URI;
    44 import java.net.URISyntaxException;
    44 import java.net.URISyntaxException;
       
    45 import java.net.URL;
       
    46 import java.security.AccessController;
       
    47 import java.security.PrivilegedAction;
    45 import java.util.Objects;
    48 import java.util.Objects;
    46 
    49 
    47 import javax.swing.JMenuBar;
    50 import javax.swing.JMenuBar;
    48 
    51 
    49 import sun.awt.SunToolkit;
    52 import sun.awt.SunToolkit;
   361      *         {@link SecurityManager#checkRead(java.lang.String)} method
   364      *         {@link SecurityManager#checkRead(java.lang.String)} method
   362      *         denies read access to the file
   365      *         denies read access to the file
   363      * @throws NullPointerException if file is null
   366      * @throws NullPointerException if file is null
   364      * @throws IllegalArgumentException if file doesn't exist
   367      * @throws IllegalArgumentException if file doesn't exist
   365      */
   368      */
   366     private static void checkFileValidation(File file){
   369     private static void checkFileValidation(File file) {
   367         if (file == null) throw new NullPointerException("File must not be null");
       
   368 
       
   369         if (!file.exists()) {
   370         if (!file.exists()) {
   370             throw new IllegalArgumentException("The file: "
   371             throw new IllegalArgumentException("The file: "
   371                     + file.getPath() + " doesn't exist.");
   372                     + file.getPath() + " doesn't exist.");
   372         }
   373         }
   373 
       
   374         file.canRead();
       
   375     }
   374     }
   376 
   375 
   377     /**
   376     /**
   378      * Checks if the action type is supported.
   377      * Checks if the action type is supported.
   379      *
   378      *
   423      * permission, or the calling thread is not allowed to create a
   422      * permission, or the calling thread is not allowed to create a
   424      * subprocess
   423      * subprocess
   425      * @see java.awt.AWTPermission
   424      * @see java.awt.AWTPermission
   426      */
   425      */
   427     public void open(File file) throws IOException {
   426     public void open(File file) throws IOException {
       
   427         file = new File(file.getPath());
   428         checkAWTPermission();
   428         checkAWTPermission();
   429         checkExec();
   429         checkExec();
   430         checkActionSupport(Action.OPEN);
   430         checkActionSupport(Action.OPEN);
   431         checkFileValidation(file);
   431         checkFileValidation(file);
   432 
   432 
   454      * permission, or the calling thread is not allowed to create a
   454      * permission, or the calling thread is not allowed to create a
   455      * subprocess
   455      * subprocess
   456      * @see java.awt.AWTPermission
   456      * @see java.awt.AWTPermission
   457      */
   457      */
   458     public void edit(File file) throws IOException {
   458     public void edit(File file) throws IOException {
       
   459         file = new File(file.getPath());
   459         checkAWTPermission();
   460         checkAWTPermission();
   460         checkExec();
   461         checkExec();
   461         checkActionSupport(Action.EDIT);
   462         checkActionSupport(Action.EDIT);
   462         file.canWrite();
   463         file.canWrite();
   463         checkFileValidation(file);
   464         checkFileValidation(file);
   484      * java.lang.SecurityManager#checkPrintJobAccess()} method denies
   485      * java.lang.SecurityManager#checkPrintJobAccess()} method denies
   485      * the permission to print the file, or the calling thread is not
   486      * the permission to print the file, or the calling thread is not
   486      * allowed to create a subprocess
   487      * allowed to create a subprocess
   487      */
   488      */
   488     public void print(File file) throws IOException {
   489     public void print(File file) throws IOException {
       
   490         file = new File(file.getPath());
   489         checkExec();
   491         checkExec();
   490         SecurityManager sm = System.getSecurityManager();
   492         SecurityManager sm = System.getSecurityManager();
   491         if (sm != null) {
   493         if (sm != null) {
   492             sm.checkPrintJobAccess();
   494             sm.checkPrintJobAccess();
   493         }
   495         }
   612             sm.checkPermission(new FilePermission("<<ALL FILES>>",
   614             sm.checkPermission(new FilePermission("<<ALL FILES>>",
   613                     SecurityConstants.FILE_READ_ACTION));
   615                     SecurityConstants.FILE_READ_ACTION));
   614         }
   616         }
   615     }
   617     }
   616 
   618 
   617     private void checkDelete() throws SecurityException {
       
   618         SecurityManager sm = System.getSecurityManager();
       
   619         if (sm != null) {
       
   620             sm.checkPermission(new FilePermission("<<ALL FILES>>",
       
   621                     SecurityConstants.FILE_DELETE_ACTION));
       
   622         }
       
   623     }
       
   624 
       
   625     private void checkQuitPermission() {
   619     private void checkQuitPermission() {
   626         SecurityManager sm = System.getSecurityManager();
   620         SecurityManager sm = System.getSecurityManager();
   627         if (sm != null) {
   621         if (sm != null) {
   628             sm.checkExit(0);
   622             sm.checkExit(0);
   629         }
   623         }
   948      * @throws UnsupportedOperationException if the current platform
   942      * @throws UnsupportedOperationException if the current platform
   949      * does not support the {@link Desktop.Action#APP_HELP_VIEWER} action
   943      * does not support the {@link Desktop.Action#APP_HELP_VIEWER} action
   950      * @since 9
   944      * @since 9
   951      */
   945      */
   952     public void openHelpViewer() {
   946     public void openHelpViewer() {
       
   947         checkAWTPermission();
       
   948         checkExec();
   953         checkEventsProcessingPermission();
   949         checkEventsProcessingPermission();
   954         checkActionSupport(Action.APP_HELP_VIEWER);
   950         checkActionSupport(Action.APP_HELP_VIEWER);
   955         peer.openHelpViewer();
   951         peer.openHelpViewer();
   956     }
   952     }
   957 
   953 
   993      * @throws IllegalArgumentException if the specified file doesn't
   989      * @throws IllegalArgumentException if the specified file doesn't
   994      * exist
   990      * exist
   995      * @since 9
   991      * @since 9
   996      */
   992      */
   997     public void browseFileDirectory(File file) {
   993     public void browseFileDirectory(File file) {
   998         checkRead();
   994         file = new File(file.getPath());
       
   995         checkAWTPermission();
       
   996         checkExec();
   999         checkActionSupport(Action.BROWSE_FILE_DIR);
   997         checkActionSupport(Action.BROWSE_FILE_DIR);
  1000         checkFileValidation(file);
   998         checkFileValidation(file);
       
   999         File parentFile = file.getParentFile();
       
  1000         if (parentFile == null || !parentFile.exists()) {
       
  1001             throw new IllegalArgumentException("Parent folder doesn't exist");
       
  1002         }
  1001         peer.browseFileDirectory(file);
  1003         peer.browseFileDirectory(file);
  1002     }
  1004     }
  1003 
  1005 
  1004     /**
  1006     /**
  1005      * Moves the specified file to the trash.
  1007      * Moves the specified file to the trash.
  1015      * @throws IllegalArgumentException if the specified file doesn't
  1017      * @throws IllegalArgumentException if the specified file doesn't
  1016      * exist
  1018      * exist
  1017      *
  1019      *
  1018      * @since 9
  1020      * @since 9
  1019      */
  1021      */
  1020     public boolean moveToTrash(final File file) {
  1022     public boolean moveToTrash(File file) {
  1021         checkDelete();
  1023         file = new File(file.getPath());
       
  1024         SecurityManager sm = System.getSecurityManager();
       
  1025         if (sm != null) {
       
  1026             sm.checkDelete(file.getPath());
       
  1027         }
  1022         checkActionSupport(Action.MOVE_TO_TRASH);
  1028         checkActionSupport(Action.MOVE_TO_TRASH);
  1023         checkFileValidation(file);
  1029         final File finalFile = file;
       
  1030         AccessController.doPrivileged((PrivilegedAction<?>) () -> {
       
  1031             checkFileValidation(finalFile);
       
  1032             return null;
       
  1033         });
  1024         return peer.moveToTrash(file);
  1034         return peer.moveToTrash(file);
  1025     }
  1035     }
  1026 }
  1036 }