changeset 4580:4fe44cde3384

6205776: Some KeyboardFocusManager methods are unsafe Summary: a security check is added Reviewed-by: bagiras
author ant
date Wed, 26 Oct 2011 14:29:43 +0400
parents 962ddd5ea2d9
children 1d04b88d06f2
files src/share/classes/java/awt/KeyboardFocusManager.java
diffstat 1 files changed, 82 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/KeyboardFocusManager.java	Wed Oct 26 13:35:15 2011 +0400
+++ b/src/share/classes/java/awt/KeyboardFocusManager.java	Wed Oct 26 14:29:43 2011 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, 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
@@ -503,14 +503,8 @@
      */
     protected Component getGlobalFocusOwner() throws SecurityException {
         synchronized (KeyboardFocusManager.class) {
-            if (this == getCurrentKeyboardFocusManager()) {
-                return focusOwner;
-            } else {
-                if (focusLog.isLoggable(PlatformLogger.FINER)) {
-                    focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
-                }
-                throw new SecurityException(notPrivileged);
-            }
+            checkCurrentKFMSecurity();
+            return focusOwner;
         }
     }
 
@@ -528,6 +522,9 @@
      * <code>getFocusOwner()</code>. Use <code>Component.requestFocus()</code>
      * or <code>Component.requestFocusInWindow()</code> to change the focus
      * owner, subject to platform limitations.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's context.
      *
      * @param focusOwner the focus owner
      * @see #getFocusOwner
@@ -535,15 +532,21 @@
      * @see Component#requestFocus()
      * @see Component#requestFocusInWindow()
      * @see Component#isFocusable
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      * @beaninfo
      *       bound: true
      */
-    protected void setGlobalFocusOwner(Component focusOwner) {
+    protected void setGlobalFocusOwner(Component focusOwner)
+        throws SecurityException
+    {
         Component oldFocusOwner = null;
         boolean shouldFire = false;
 
         if (focusOwner == null || focusOwner.isFocusable()) {
             synchronized (KeyboardFocusManager.class) {
+                checkCurrentKFMSecurity();
+
                 oldFocusOwner = getFocusOwner();
 
                 try {
@@ -588,11 +591,21 @@
      * a new Component to receive focus, or a Component is given focus
      * explicitly via a call to <code>requestFocus()</code>. This operation
      * does not change the focused or active Windows.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's context.
      *
      * @see Component#requestFocus()
      * @see java.awt.event.FocusEvent#FOCUS_LOST
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      */
-    public void clearGlobalFocusOwner() {
+    public void clearGlobalFocusOwner()
+        throws SecurityException
+    {
+        synchronized (KeyboardFocusManager.class) {
+            checkCurrentKFMSecurity();
+        }
         if (!GraphicsEnvironment.isHeadless()) {
             // Toolkit must be fully initialized, otherwise
             // _clearGlobalFocusOwner will crash or throw an exception
@@ -672,14 +685,8 @@
         throws SecurityException
     {
         synchronized (KeyboardFocusManager.class) {
-            if (this == getCurrentKeyboardFocusManager()) {
-                return permanentFocusOwner;
-            } else {
-                if (focusLog.isLoggable(PlatformLogger.FINER)) {
-                    focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
-                }
-                throw new SecurityException(notPrivileged);
-            }
+            checkCurrentKFMSecurity();
+            return permanentFocusOwner;
         }
     }
 
@@ -698,6 +705,10 @@
      * <code>Component.requestFocus()</code> or
      * <code>Component.requestFocusInWindow()</code> to change the focus owner,
      * subject to platform limitations.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's
+     * context.
      *
      * @param permanentFocusOwner the permanent focus owner
      * @see #getPermanentFocusOwner
@@ -705,16 +716,21 @@
      * @see Component#requestFocus()
      * @see Component#requestFocusInWindow()
      * @see Component#isFocusable
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      * @beaninfo
      *       bound: true
      */
     protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner)
+        throws SecurityException
     {
         Component oldPermanentFocusOwner = null;
         boolean shouldFire = false;
 
         if (permanentFocusOwner == null || permanentFocusOwner.isFocusable()) {
             synchronized (KeyboardFocusManager.class) {
+                checkCurrentKFMSecurity();
+
                 oldPermanentFocusOwner = getPermanentFocusOwner();
 
                 try {
@@ -780,14 +796,8 @@
      */
     protected Window getGlobalFocusedWindow() throws SecurityException {
         synchronized (KeyboardFocusManager.class) {
-            if (this == getCurrentKeyboardFocusManager()) {
-               return focusedWindow;
-            } else {
-                if (focusLog.isLoggable(PlatformLogger.FINER)) {
-                    focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
-                }
-                throw new SecurityException(notPrivileged);
-            }
+            checkCurrentKFMSecurity();
+            return focusedWindow;
         }
     }
 
@@ -802,6 +812,9 @@
      * <code>Component.requestFocus()</code> or
      * <code>Component.requestFocusInWindow()</code> to change the focused
      * Window, subject to platform limitations.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's context.
      *
      * @param focusedWindow the focused Window
      * @see #getFocusedWindow
@@ -809,15 +822,21 @@
      * @see Component#requestFocus()
      * @see Component#requestFocusInWindow()
      * @see Window#isFocusableWindow
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      * @beaninfo
      *       bound: true
      */
-    protected void setGlobalFocusedWindow(Window focusedWindow) {
+    protected void setGlobalFocusedWindow(Window focusedWindow)
+        throws SecurityException
+    {
         Window oldFocusedWindow = null;
         boolean shouldFire = false;
 
         if (focusedWindow == null || focusedWindow.isFocusableWindow()) {
             synchronized (KeyboardFocusManager.class) {
+                checkCurrentKFMSecurity();
+
                 oldFocusedWindow = getFocusedWindow();
 
                 try {
@@ -884,14 +903,8 @@
      */
     protected Window getGlobalActiveWindow() throws SecurityException {
         synchronized (KeyboardFocusManager.class) {
-            if (this == getCurrentKeyboardFocusManager()) {
-               return activeWindow;
-            } else {
-                if (focusLog.isLoggable(PlatformLogger.FINER)) {
-                    focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
-                }
-                throw new SecurityException(notPrivileged);
-            }
+            checkCurrentKFMSecurity();
+            return activeWindow;
         }
     }
 
@@ -908,18 +921,27 @@
      * <code>Component.requestFocus()</code> or
      * <code>Component.requestFocusInWindow()</code>to change the active
      * Window, subject to platform limitations.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's context.
      *
      * @param activeWindow the active Window
      * @see #getActiveWindow
      * @see #getGlobalActiveWindow
      * @see Component#requestFocus()
      * @see Component#requestFocusInWindow()
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      * @beaninfo
      *       bound: true
      */
-    protected void setGlobalActiveWindow(Window activeWindow) {
+    protected void setGlobalActiveWindow(Window activeWindow)
+        throws SecurityException
+    {
         Window oldActiveWindow;
         synchronized (KeyboardFocusManager.class) {
+            checkCurrentKFMSecurity();
+
             oldActiveWindow = getActiveWindow();
             if (focusLog.isLoggable(PlatformLogger.FINER)) {
                 focusLog.finer("Setting global active window to " + activeWindow + ", old active " + oldActiveWindow);
@@ -1214,14 +1236,8 @@
         throws SecurityException
     {
         synchronized (KeyboardFocusManager.class) {
-            if (this == getCurrentKeyboardFocusManager()) {
-                return currentFocusCycleRoot;
-            } else {
-                if (focusLog.isLoggable(PlatformLogger.FINER)) {
-                    focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
-                }
-                throw new SecurityException(notPrivileged);
-            }
+            checkCurrentKFMSecurity();
+            return currentFocusCycleRoot;
         }
     }
 
@@ -1234,17 +1250,27 @@
      * <p>
      * This method is intended to be used only by KeyboardFocusManagers and
      * focus implementations. It is not for general client use.
+     * <p>
+     * This method will throw a SecurityException if this KeyboardFocusManager
+     * is not the current KeyboardFocusManager for the calling thread's
+     * context.
      *
      * @param newFocusCycleRoot the new focus cycle root
      * @see #getCurrentFocusCycleRoot
      * @see #getGlobalCurrentFocusCycleRoot
+     * @throws SecurityException if this KeyboardFocusManager is not the
+     *         current KeyboardFocusManager for the calling thread's context
      * @beaninfo
      *       bound: true
      */
-    public void setGlobalCurrentFocusCycleRoot(Container newFocusCycleRoot) {
+    public void setGlobalCurrentFocusCycleRoot(Container newFocusCycleRoot)
+        throws SecurityException
+    {
         Container oldFocusCycleRoot;
 
         synchronized (KeyboardFocusManager.class) {
+            checkCurrentKFMSecurity();
+
             oldFocusCycleRoot  = getCurrentFocusCycleRoot();
             currentFocusCycleRoot = newFocusCycleRoot;
         }
@@ -3062,4 +3088,14 @@
                 : null;
         }
     }
+
+    private void checkCurrentKFMSecurity() {
+        if (this != getCurrentKeyboardFocusManager()) {
+            if (focusLog.isLoggable(PlatformLogger.FINER)) {
+                focusLog.finer("This manager is " + this +
+                               ", current is " + getCurrentKeyboardFocusManager());
+            }
+            throw new SecurityException(notPrivileged);
+        }
+    }
 }