/*
* Copyright (c) 1998, 2013, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*
* Copyright (c) 2014, JogAmp Community. All rights reserved.
*
* JogAmp changes are compatible w/ GPLv2,
* and also available under the the New BSD 2-clause license.
*
* Please visit the JogAmp Community via http://jogamp.org/
* and find appropriate channels (forum, email, irc) to contact us
* if you need additional information or have any
* questions.
*
*/
package jogamp.applet;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import sun.util.logging.PlatformLogger;
import com.jogamp.plugin.ui.NativeWindowDownstream;
/**
* The AppContext is a table referenced by ThreadGroup which stores
* application service instances. (If you are not writing an application
* service, or don't know what one is, please do not use this class.)
* The AppContext allows applet access to what would otherwise be
* potentially dangerous services, such as the ability to peek at
* EventQueues or change the look-and-feel of a Swing application.
*
* Most application services use a singleton object to provide their
* services, either as a default (such as getSystemEventQueue or
* getDefaultToolkit) or as static methods with class data (System).
* The AppContext works with the former method by extending the concept
* of "default" to be ThreadGroup-specific. Application services
* lookup their singleton in the AppContext.
*
* For example, here we have a Foo service, with its pre-AppContext
* code:
*
* public class Foo {
* private static Foo defaultFoo = new Foo();
*
* public static Foo getDefaultFoo() {
* return defaultFoo;
* }
*
* ... Foo service methods
* }
*
* The problem with the above is that the Foo service is global in scope,
* so that applets and other untrusted code can execute methods on the
* single, shared Foo instance. The Foo service therefore either needs
* to block its use by untrusted code using a SecurityManager test, or
* restrict its capabilities so that it doesn't matter if untrusted code
* executes it.
*
* Here's the Foo class written to use the AppContext:
*
* public class Foo {
* public static Foo getDefaultFoo() {
* Foo foo = (Foo)AppContext.getAppContext().get(Foo.class);
* if (foo == null) {
* foo = new Foo();
* getAppContext().put(Foo.class, foo);
* }
* return foo;
* }
*
* ... Foo service methods
* }
*
* Since a separate AppContext can exist for each ThreadGroup, trusted
* and untrusted code have access to different Foo instances. This allows
* untrusted code access to "system-wide" services -- the service remains
* within the AppContext "sandbox". For example, say a malicious applet
* wants to peek all of the key events on the EventQueue to listen for
* passwords; if separate EventQueues are used for each ThreadGroup
* using AppContexts, the only key events that applet will be able to
* listen to are its own. A more reasonable applet request would be to
* change the Swing default look-and-feel; with that default stored in
* an AppContext, the applet's look-and-feel will change without
* disrupting other applets or potentially the browser itself.
*
* Because the AppContext is a facility for safely extending application
* service support to applets, none of its methods may be blocked by a
* a SecurityManager check in a valid Java implementation. Applets may
* therefore safely invoke any of its methods without worry of being
* blocked.
*
* Note: If a SecurityManager is installed which derives from
* sun.awt.AWTSecurityManager, it may override the
* AWTSecurityManager.getAppContext() method to return the proper
* AppContext based on the execution context, in the case where
* the default ThreadGroup-based AppContext indexing would return
* the main "system" AppContext. For example, in an applet situation,
* if a system thread calls into an applet, rather than returning the
* main "system" AppContext (the one corresponding to the system thread),
* an installed AWTSecurityManager may return the applet's AppContext
* based on the execution context.
*
* @author Thomas Ball
* @author Fred Ecks
*/
public final class App3Context {
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.AppContext");
/* Since the contents of an AppContext are unique to each Java
* session, this class should never be serialized. */
/*
* The key to put()/get() the Java EventQueue into/from the AppContext.
*/
public static final Object EVENT_QUEUE_KEY = new StringBuffer("EventQueue");
/*
* The keys to store EventQueue push/pop lock and condition.
*/
public final static Object EVENT_QUEUE_LOCK_KEY = new StringBuilder("EventQueue.Lock");
public final static Object EVENT_QUEUE_COND_KEY = new StringBuilder("EventQueue.Condition");
/* A map of AppContexts, referenced by ThreadGroup.
*/
private static final Map threadGroup2appContext =
Collections.synchronizedMap(new IdentityHashMap());
private final Set registeredWindows = new HashSet();
private static final Map appletWindow2AppContext =
Collections.synchronizedMap(new WeakHashMap());
/**
* Returns a set containing all AppContexts.
*/
public static Set getAppContexts() {
synchronized (threadGroup2appContext) {
return new HashSet(threadGroup2appContext.values());
}
}
/* The main "system" AppContext, used by everything not otherwise
contained in another AppContext. It is implicitly created for
standalone apps only (i.e. not applets)
*/
private static App3Context mainAppContext;
/*
* The hash map associated with this AppContext. A private delegate
* is used instead of subclassing HashMap so as to avoid all of
* HashMap's potentially risky methods, such as clear(), elements(),
* putAll(), etc.
*/
private final Map