8191239: Improve desktop file usage
authorserb
Wed, 31 Jan 2018 18:13:13 -0800
changeset 51152 edd69f959190
parent 51151 d6b131d2bc8b
child 51153 78f16a9f7563
8191239: Improve desktop file usage Reviewed-by: prr, rhalade, aghaisas
src/java.desktop/macosx/classes/com/apple/eio/FileManager.java
src/java.desktop/share/classes/java/awt/Desktop.java
--- a/src/java.desktop/macosx/classes/com/apple/eio/FileManager.java	Thu Jan 18 13:55:26 2018 +0530
+++ b/src/java.desktop/macosx/classes/com/apple/eio/FileManager.java	Wed Jan 31 18:13:13 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -364,11 +364,11 @@
          * @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
          */
         public static boolean moveToTrash(final File file) throws FileNotFoundException {
-                if (file == null || !file.exists()) throw new FileNotFoundException();
+                if (file == null) throw new FileNotFoundException();
                 final String fileName = file.getAbsolutePath();
 
                 final SecurityManager security = System.getSecurityManager();
-                if (security != null) security.checkWrite(fileName);
+                if (security != null) security.checkDelete(fileName);
 
                 return _moveToTrash(fileName);
         }
--- a/src/java.desktop/share/classes/java/awt/Desktop.java	Thu Jan 18 13:55:26 2018 +0530
+++ b/src/java.desktop/share/classes/java/awt/Desktop.java	Wed Jan 31 18:13:13 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,9 @@
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Objects;
 
 import javax.swing.JMenuBar;
@@ -363,15 +366,11 @@
      * @throws NullPointerException if file is null
      * @throws IllegalArgumentException if file doesn't exist
      */
-    private static void checkFileValidation(File file){
-        if (file == null) throw new NullPointerException("File must not be null");
-
+    private static void checkFileValidation(File file) {
         if (!file.exists()) {
             throw new IllegalArgumentException("The file: "
                     + file.getPath() + " doesn't exist.");
         }
-
-        file.canRead();
     }
 
     /**
@@ -425,6 +424,7 @@
      * @see java.awt.AWTPermission
      */
     public void open(File file) throws IOException {
+        file = new File(file.getPath());
         checkAWTPermission();
         checkExec();
         checkActionSupport(Action.OPEN);
@@ -456,6 +456,7 @@
      * @see java.awt.AWTPermission
      */
     public void edit(File file) throws IOException {
+        file = new File(file.getPath());
         checkAWTPermission();
         checkExec();
         checkActionSupport(Action.EDIT);
@@ -486,6 +487,7 @@
      * allowed to create a subprocess
      */
     public void print(File file) throws IOException {
+        file = new File(file.getPath());
         checkExec();
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
@@ -614,14 +616,6 @@
         }
     }
 
-    private void checkDelete() throws SecurityException {
-        SecurityManager sm = System.getSecurityManager();
-        if (sm != null) {
-            sm.checkPermission(new FilePermission("<<ALL FILES>>",
-                    SecurityConstants.FILE_DELETE_ACTION));
-        }
-    }
-
     private void checkQuitPermission() {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
@@ -950,6 +944,8 @@
      * @since 9
      */
     public void openHelpViewer() {
+        checkAWTPermission();
+        checkExec();
         checkEventsProcessingPermission();
         checkActionSupport(Action.APP_HELP_VIEWER);
         peer.openHelpViewer();
@@ -995,9 +991,15 @@
      * @since 9
      */
     public void browseFileDirectory(File file) {
-        checkRead();
+        file = new File(file.getPath());
+        checkAWTPermission();
+        checkExec();
         checkActionSupport(Action.BROWSE_FILE_DIR);
         checkFileValidation(file);
+        File parentFile = file.getParentFile();
+        if (parentFile == null || !parentFile.exists()) {
+            throw new IllegalArgumentException("Parent folder doesn't exist");
+        }
         peer.browseFileDirectory(file);
     }
 
@@ -1017,10 +1019,18 @@
      *
      * @since 9
      */
-    public boolean moveToTrash(final File file) {
-        checkDelete();
+    public boolean moveToTrash(File file) {
+        file = new File(file.getPath());
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            sm.checkDelete(file.getPath());
+        }
         checkActionSupport(Action.MOVE_TO_TRASH);
-        checkFileValidation(file);
+        final File finalFile = file;
+        AccessController.doPrivileged((PrivilegedAction<?>) () -> {
+            checkFileValidation(finalFile);
+            return null;
+        });
         return peer.moveToTrash(file);
     }
 }