$FreeBSD$ --- ../../deploy/src/plugin/src/share/classes/sun/plugin/com/DispatchImpl.java 22 Oct 2003 23:04:20 -0000 1.1 +++ ../../deploy/src/plugin/src/share/classes/sun/plugin/com/DispatchImpl.java 3 Dec 2004 03:56:58 -0000 1.2 @@ -1,7 +1,7 @@ /* - * @(#)DispatchImpl.java 1.6 03/01/23 + * @(#)DispatchImpl.java 1.16 04/06/20 * - * Copyright 2003 Sun Microsystems, Inc. All rights reserved. + * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ @@ -9,8 +9,32 @@ import sun.plugin.util.Trace; import sun.plugin.javascript.ocx.JSObject; -import java.applet.Applet; +import sun.plugin.liveconnect.JavaScriptProtectionDomain; import sun.plugin.viewer.context.IExplorerAppletContext; +import sun.plugin.security.PluginClassLoader; +import java.applet.Applet; +import java.net.URL; +import java.net.MalformedURLException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.net.SocketPermission; +import java.io.FilePermission; +import java.io.File; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.CodeSource; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.security.AllPermission; +import java.security.Permissions; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.security.AccessControlException; +import java.security.PrivilegedActionException; +import sun.net.www.ParseUtil; +import sun.security.util.SecurityConstants; /** * DispatchImpl encapsulates a Java Object and provides Dispatch interface @@ -22,14 +46,19 @@ { JavaClass targetClass = null; Object targetObj = null; + int handle = 0; + int wndHandle = 0; + AccessControlContext context = null; + boolean isBridge = false; /* * Constructor * @param obj the object to be wrapped */ - public DispatchImpl(Object obj) + public DispatchImpl(Object obj, int id) { targetObj = obj; + handle = id; } /** @@ -40,19 +69,87 @@ * @param params Arguments. * @return Java object. */ - public Object invoke(int flag, int index, Object []params) + public Object invoke(final int flag, final int index, final Object []params) + throws Exception + { + try { + //No security constraints in case of ActiveX bridge application + if(isBridge) + return invokeImpl(flag, index, params); + + if(context == null) { + context = createContext(); + } + + // Invoke the method within the applet sand box security restricitions + return AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws Exception{ + return invokeImpl(flag, index, params); + } + }, context + ); + }catch(Exception exc) { + Throwable cause = exc.getCause(); + if(cause == null) { + cause = exc; + } + + Trace.liveConnectPrintException(cause); + throw new Exception(cause.toString()); + } + } + + public AccessControlContext createContext() { + try { + ProtectionDomain[] domains = new ProtectionDomain[1]; + //Obtain the java code origin + ProtectionDomain pd = (ProtectionDomain)AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return targetObj.getClass().getProtectionDomain(); + } + }); + + CodeSource cs = null; + URL url = null; + if(pd != null) + cs = pd.getCodeSource(); + if(cs != null) + url = cs.getLocation(); + + domains[0] = getJSProtectionDomain(url, targetObj.getClass()); + return new AccessControlContext(domains); + }catch(Exception exc) { + Trace.liveConnectPrintException(exc); + } + + return null; + } + + /** + * Invoke a method according to the method index. + * + * @param flag Invoke flag + * @param index Method index + * @param params Arguments. + * @return Java object. + */ + public Object invokeImpl(int flag, int index, Object []params) throws Exception { Object retObj = null; Dispatcher disp = null; try { - convertParams(params); + if(params != null) + convertParams(params); disp = targetClass.getDispatcher(flag, index, params); - return disp.invoke(targetObj, params); - } - catch (Throwable e) - { - //e.printStackTrace(); + if(disp != null) { + retObj = disp.invoke(targetObj, params); + if(retObj != null) + retObj = Utils.convertReturn(disp.getReturnType(), retObj, handle); + } + return retObj; + } catch (Throwable e) { Throwable cause = e.getCause(); if(cause == null) { cause = e; @@ -82,11 +179,39 @@ return targetClass; } + public int getReturnType(int id){ + return targetClass.getReturnType(id); + } + + public int getIdForName(final String name) throws Exception{ + try { + //No security constraints in case of ActiveX bridge application + if(isBridge) + return getIdForNameImpl(name); + + if(context == null) { + context = createContext(); + } + + // Invoke the method within the applet sand box security restricitions + Integer retVal = (Integer)AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws Exception{ + return new Integer(getIdForNameImpl(name)); + } + }, context + ); + return retVal.intValue(); + }catch(PrivilegedActionException pe) { + } + + return -1; + } /* * */ - public int getIdForName(String name) throws Exception{ + public int getIdForNameImpl(String name) throws Exception{ int id = -1; if(targetClass == null && targetObj != null) { @@ -103,27 +228,122 @@ } /* - * + * Unwraps the wrapped java object arguments */ private void convertParams(Object []params) { for(int i=0;i