diff --git a/mozilla/extensions/java/xpcom/XPCOMException.java b/mozilla/extensions/java/xpcom/XPCOMException.java deleted file mode 100644 index 44636d540a0..00000000000 --- a/mozilla/extensions/java/xpcom/XPCOMException.java +++ /dev/null @@ -1,95 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - - -/** - * This exception is thrown whenever an internal XPCOM/Gecko error occurs. - * You can query the error ID returned by XPCOM by checking - * errorcode field. - */ -public class XPCOMException extends RuntimeException { - - /** - * The XPCOM error value. - */ - public long errorcode; - - private static final long serialVersionUID = 198521829884000593L; - - /** - * Constructs a new XPCOMException instance, with a default error - * (NS_ERROR_FAILURE) and message. - */ - public XPCOMException() { - this(0x80004005L, "Unspecified internal XPCOM error"); - } - - /** - * Constructs a new XPCOMException instance with the given message, passing - * NS_ERROR_FAILURE as the error code. - * - * @param message detailed message of exception - */ - public XPCOMException(String message) { - this(0x80004005L, message); - } - - /** - * Constructs a new XPCOMException instance with the given code, passing - * a default message. - * - * @param code internal XPCOM error ID - */ - public XPCOMException(long code) { - this(code, "Internal XPCOM error"); - } - - /** - * Constructs a new XPCOMException instance with an error code and message. - * - * @param code internal XPCOM error ID - * @param message detailed message of exception - */ - public XPCOMException(long code, String message) { - super(message + " (0x" + Long.toHexString(code) + ")"); - this.errorcode = code; - } - -} - diff --git a/mozilla/extensions/java/xpcom/XPCOMJavaProxy.java b/mozilla/extensions/java/xpcom/XPCOMJavaProxy.java deleted file mode 100644 index 924e8b4b821..00000000000 --- a/mozilla/extensions/java/xpcom/XPCOMJavaProxy.java +++ /dev/null @@ -1,255 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom.internal; - -import java.lang.reflect.*; -import org.mozilla.xpcom.*; - - -/** - * This class is used to pass XPCOM objects to Java functions. A - * java.lang.reflect.Proxy instance is created using the expected - * interface, and all calls to the proxy are forwarded to the XPCOM object. - */ -public class XPCOMJavaProxy implements InvocationHandler { - - /** - * Pointer to the XPCOM object for which we are a proxy. - */ - protected long nativeXPCOMPtr; - - /** - * Default constructor. - * - * @param aXPCOMInstance address of XPCOM object as a long - */ - public XPCOMJavaProxy(long aXPCOMInstance) { - nativeXPCOMPtr = aXPCOMInstance; - } - - /** - * Returns the XPCOM object that the given proxy references. - * - * @param aProxy Proxy created by createProxy - * - * @return address of XPCOM object as a long - */ - protected static long getNativeXPCOMInstance(Object aProxy) { - XPCOMJavaProxy proxy = (XPCOMJavaProxy) Proxy.getInvocationHandler(aProxy); - return proxy.nativeXPCOMPtr; - } - - /** - * Creates a Proxy for the given XPCOM object. - * - * @param aInterface interface from which to create Proxy - * @param aXPCOMInstance address of XPCOM object as a long - * - * @return Proxy of given XPCOM object - */ - protected static Object createProxy(Class aInterface, long aXPCOMInstance) { - // XXX We should really get the class loader from |aInterface|. However, - // that class loader doesn't know about |XPCOMJavaProxyBase|. So for - // now, we get the class loader that loaded |XPCOMJavaProxy|. When - // we get rid of the "XPCOMJavaProxyBase.java" class, we can revert - // to the old method below. -// return Proxy.newProxyInstance(aInterface.getClassLoader(), - return Proxy.newProxyInstance(XPCOMJavaProxy.class.getClassLoader(), - new Class[] { aInterface, XPCOMJavaProxyBase.class }, - new XPCOMJavaProxy(aXPCOMInstance)); - } - - /** - * All calls to the Java proxy are forwarded to this method. This method - * takes care of a few of the Object method calls; all other - * calls are forwarded to the XPCOM object. - * - * @param aProxy Proxy created by createProxy - * @param aMethod object that describes the called method - * @param aParams array of the arguments passed to the method - * - * @return return value as defined by given aMethod - */ - public Object invoke(Object aProxy, Method aMethod, Object[] aParams) - throws Throwable { - String methodName = aMethod.getName(); - - // Handle the three java.lang.Object methods that are passed to us. - if (aMethod.getDeclaringClass() == Object.class) { - if (methodName.equals("hashCode")) { - return proxyHashCode(aProxy); - } - if (methodName.equals("equals")) { - return proxyEquals(aProxy, aParams[0]); - } - if (methodName.equals("toString")) { - return proxyToString(aProxy); - } - System.err.println("WARNING: Unhandled Object method [" + - methodName + "]"); - return null; - } - - // Handle the 'finalize' method called during garbage collection - if (aMethod.getDeclaringClass() == XPCOMJavaProxyBase.class) { - if (methodName.equals("finalize")) { - finalizeProxy(aProxy); - } else { - System.err.println("WARNING: Unhandled XPCOMJavaProxyBase method [" + - methodName + "]"); - } - return null; - } - - // If not already handled, pass method calls to XPCOM object. - return callXPCOMMethod(aProxy, methodName, aParams); - } - - /** - * Handles method calls of java.lang.Object.hashCode - * - * @param aProxy Proxy created by createProxy - * - * @return Integer object representing hash code of given object - * - * @see Object#hashCode() - */ - protected static Integer proxyHashCode(Object aProxy) { - return new Integer(System.identityHashCode(aProxy)); - } - - /** - * Handles method calls of java.lang.Object.equals - * - * @param aProxy Proxy created by createProxy - * @param aOther another object - * - * @return true if the given objects are the same; - * false otherwise - * - * @see Object#equals(Object) - */ - protected static Boolean proxyEquals(Object aProxy, Object aOther) { - // See if the two are the same Java object - if (aProxy == aOther) { - return Boolean.TRUE; - } else { - // If not, then see if they represent the same XPCOM object. But first, - // we need to check if |aOther| is an XPCOMJavaProxy. - if (isXPCOMJavaProxy(aOther) && isSameXPCOMObject(aProxy, aOther)) { - return Boolean.TRUE; - } - } - return Boolean.FALSE; - } - - /** - * Indicates whether the given object is an XPCOMJavaProxy. - * - * @param aObject object to check - * - * @return true if the given object is an XPCOMJavaProxy; - * false otherwise - */ - protected static boolean isXPCOMJavaProxy(Object aObject) { - if (aObject != null && Proxy.isProxyClass(aObject.getClass())) { - InvocationHandler h = Proxy.getInvocationHandler(aObject); - if (h instanceof XPCOMJavaProxy) { - return true; - } - } - return false; - } - - /** - * Checks if the two given XPCOMJavaProxy objects are proxies for - * the same XPCOM object. - * - * @param aProxy1 XPCOMJavaProxy created by createProxy - * @param aProxy2 XPCOMJavaProxy created by createProxy - * - * @return true if both proxies represent the same XPCOM object; - * false otherwise - */ - protected static native boolean isSameXPCOMObject(Object aProxy1, - Object aProxy2); - - /** - * Handles method calls of java.lang.Object.toString - * - * @param aProxy Proxy created by createProxy - * - * @return String representation of given object - * - * @see Object#toString() - */ - protected static String proxyToString(Object aProxy) { - return aProxy.getClass().getInterfaces()[0].getName() + '@' + - Integer.toHexString(aProxy.hashCode()); - } - - /** - * Called when the proxy is garbage collected by the JVM. Allows us to clean - * up any references to the XPCOM object. - * - * @param aProxy reference to Proxy that is being garbage collected - */ - protected void finalizeProxy(Object aProxy) throws Throwable { - finalizeProxyNative(aProxy); - super.finalize(); - } - - protected static native void finalizeProxyNative(Object aProxy); - - /** - * Calls the XPCOM object referenced by the proxy with the given method. - * - * @param aProxy Proxy created by createProxy - * @param aMethodName name of method that we want to call - * @param aParams array of params passed to method - * - * @return return value as defined by given method - * - * @exception XPCOMException if XPCOM method failed. Values of XPCOMException - * are defined by the method called. - */ - protected static native Object callXPCOMMethod(Object aProxy, - String aMethodName, Object[] aParams); - -} - diff --git a/mozilla/extensions/java/xpcom/XPCOMJavaProxyBase.java b/mozilla/extensions/java/xpcom/XPCOMJavaProxyBase.java deleted file mode 100644 index 595a7a2c0fe..00000000000 --- a/mozilla/extensions/java/xpcom/XPCOMJavaProxyBase.java +++ /dev/null @@ -1,53 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom.internal; - - -/** - * This interface forms the foundation of any XPCOMJavaProxy that is created. - * It allows us to handle any JVM calls to finalize when the Proxy - * is garbage collected. - */ -public interface XPCOMJavaProxyBase { - - /** - * @see java.lang.Object#finalize() - */ - void finalize() throws Throwable; - -} diff --git a/mozilla/extensions/java/xpcom/XPCOMPrivate.java b/mozilla/extensions/java/xpcom/XPCOMPrivate.java deleted file mode 100644 index 6e4f52d90ff..00000000000 --- a/mozilla/extensions/java/xpcom/XPCOMPrivate.java +++ /dev/null @@ -1,91 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - - -/** - * This class contains methods that are used by the JavaXPCOM proxy classes. - * They are for internal JavaXPCOM use only and should not be used by - * developers. - */ -public final class XPCOMPrivate { - - public static native - void CallXPCOMMethodVoid(Object thisObj, int fnNumber, Object[] params); - public static native - boolean CallXPCOMMethodBool(Object thisObj, int fnNumber, Object[] params); - public static native - boolean[] CallXPCOMMethodBoolA(Object thisObj, int fnNumber, Object[] params); - public static native - byte CallXPCOMMethodByte(Object thisObj, int fnNumber, Object[] params); - public static native - byte[] CallXPCOMMethodByteA(Object thisObj, int fnNumber, Object[] params); - public static native - char CallXPCOMMethodChar(Object thisObj, int fnNumber, Object[] params); - public static native - char[] CallXPCOMMethodCharA(Object thisObj, int fnNumber, Object[] params); - public static native - short CallXPCOMMethodShort(Object thisObj, int fnNumber, Object[] params); - public static native - short[] CallXPCOMMethodShortA(Object thisObj, int fnNumber, Object[] params); - public static native - int CallXPCOMMethodInt(Object thisObj, int fnNumber, Object[] params); - public static native - int[] CallXPCOMMethodIntA(Object thisObj, int fnNumber, Object[] params); - public static native - long CallXPCOMMethodLong(Object thisObj, int fnNumber, Object[] params); - public static native - long[] CallXPCOMMethodLongA(Object thisObj, int fnNumber, Object[] params); - public static native - float CallXPCOMMethodFloat(Object thisObj, int fnNumber, Object[] params); - public static native - float[] CallXPCOMMethodFloatA(Object thisObj, int fnNumber, Object[] params); - public static native - double CallXPCOMMethodDouble(Object thisObj, int fnNumber, Object[] params); - public static native - double[] CallXPCOMMethodDoubleA(Object thisObj, int fnNumber, Object[] params); - public static native - Object CallXPCOMMethodObj(Object thisObj, int fnNumber, Object[] params); - public static native - Object[] CallXPCOMMethodObjA(Object thisObj, int fnNumber, Object[] params); - - public static native - void FinalizeStub(Object thisObj); - -} - diff --git a/mozilla/extensions/java/xpcom/build/Makefile.in b/mozilla/extensions/java/xpcom/build/Makefile.in deleted file mode 100644 index 9d0f0657338..00000000000 --- a/mozilla/extensions/java/xpcom/build/Makefile.in +++ /dev/null @@ -1,109 +0,0 @@ -# ***** BEGIN LICENSE BLOCK ***** -# Version: MPL 1.1/GPL 2.0/LGPL 2.1 -# -# The contents of this file are subject to the Mozilla Public License Version -# 1.1 (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# http://www.mozilla.org/MPL/ -# -# Software distributed under the License is distributed on an "AS IS" basis, -# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License -# for the specific language governing rights and limitations under the -# License. -# -# The Original Code is Java XPCOM Bindings. -# -# The Initial Developer of the Original Code is -# IBM Corporation. -# Portions created by the Initial Developer are Copyright (C) 2004 -# IBM Corporation. All Rights Reserved. -# -# Contributor(s): -# Javier Pedemonte (jhpedemonte@gmail.com) -# -# Alternatively, the contents of this file may be used under the terms of -# either the GNU General Public License Version 2 or later (the "GPL"), or -# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), -# in which case the provisions of the GPL or the LGPL are applicable instead -# of those above. If you wish to allow use of your version of this file only -# under the terms of either the GPL or the LGPL, and not to allow others to -# use your version of this file under the terms of the MPL, indicate your -# decision by deleting the provisions above and replace them with the notice -# and other provisions required by the GPL or the LGPL. If you do not delete -# the provisions above, a recipient may use your version of this file under -# the terms of any one of the MPL, the GPL or the LGPL. -# -# ***** END LICENSE BLOCK ***** - -DEPTH = ../../../.. -topsrcdir = @top_srcdir@ -srcdir = @srcdir@ -VPATH = @srcdir@ - -include $(DEPTH)/config/autoconf.mk -include $(topsrcdir)/config/config.mk - -JARFILE = mozjava.jar -JARFILE_SRC = $(patsubst %.jar,%-src.jar,$(JARFILE)) - -GARBAGE += $(JARFILE) $(JARFILE_SRC) -GARBAGE_DIRS += _javagen org - -JAVA_SRCS = \ - XPCOM.java \ - XPCOMJavaProxy.java \ - XPCOMJavaProxyBase.java \ - XPCOMException.java \ - GeckoEmbed.java \ - AppFileLocProvider.java \ - $(NULL) - -GEN_JAVA_SRCS = \ - XPCOMError.java \ - $(NULL) - -include $(topsrcdir)/config/rules.mk - -ifdef MOZ_DEBUG -JAVAC_FLAGS = -g -endif - -ifeq ($(OS_ARCH),WINNT) -RUN = -OUTPUT_DIR = $(shell cygpath -w $(CURDIR)/_javagen/org/mozilla/xpcom) -else -RUN = $(DIST)/bin/run-mozilla.sh -OUTPUT_DIR = $(CURDIR)/_javagen/org/mozilla/xpcom -endif - -_javagen/org/mozilla/xpcom/.iface_done: - @if test ! -d _javagen/org/mozilla/xpcom; then \ - touch .done; \ - $(INSTALL) -m 644 .done _javagen/org/mozilla/xpcom; \ - fi - @echo Copying Java source files - @$(INSTALL) -m 644 $(addprefix $(srcdir)/../,$(JAVA_SRCS)) \ - _javagen/org/mozilla/xpcom - @$(INSTALL) -m 644 $(addprefix ../,$(GEN_JAVA_SRCS)) \ - _javagen/org/mozilla/xpcom - @echo Generating Java interface files - $(RUN) $(DIST)/bin/GenerateJavaInterfaces$(BIN_SUFFIX) -d $(OUTPUT_DIR) - @touch $@ - -# Use find and xargs for passing list of Java files to JAVAC. This avoids the -# "argument list too long" error on Windows when using *.java -org/mozilla/xpcom/.class_done: _javagen/org/mozilla/xpcom/.iface_done - @echo Compiling Java interface classes - find _javagen -name *.java | xargs $(JAVAC) $(JAVAC_FLAGS) -classpath . -d . \ - -sourcepath _javagen - @touch $@ - -$(JARFILE): org/mozilla/xpcom/.class_done Makefile - $(JAR) cf $@ org - -$(JARFILE_SRC): $(JARFILE) - $(JAR) cf $@ -C _javagen org - -libs:: $(JARFILE) $(JARFILE_SRC) - $(INSTALL) $(IFLAGS1) $^ $(DIST)/bin - diff --git a/mozilla/extensions/java/xpcom/gen-nsError.pl b/mozilla/extensions/java/xpcom/gen-nsError.pl deleted file mode 100644 index 2a98955c481..00000000000 --- a/mozilla/extensions/java/xpcom/gen-nsError.pl +++ /dev/null @@ -1,163 +0,0 @@ -#! /usr/bin/env perl -# ***** BEGIN LICENSE BLOCK ***** -# Version: MPL 1.1/GPL 2.0/LGPL 2.1 -# -# The contents of this file are subject to the Mozilla Public License Version -# 1.1 (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# http://www.mozilla.org/MPL/ -# -# Software distributed under the License is distributed on an "AS IS" basis, -# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License -# for the specific language governing rights and limitations under the -# License. -# -# The Original Code is Java XPCOM Bindings. -# -# The Initial Developer of the Original Code is -# Michal Ceresna. -# Portions created by the Initial Developer are Copyright (C) 2005 -# the Initial Developer. All Rights Reserved. -# -# Contributor(s): -# Michal Ceresna (ceresna@dbai.tuwien.ac.at) -# Javier Pedemonte (jhpedemonte@gmail.com) -# -# Alternatively, the contents of this file may be used under the terms of -# either the GNU General Public License Version 2 or later (the "GPL"), or -# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), -# in which case the provisions of the GPL or the LGPL are applicable instead -# of those above. If you wish to allow use of your version of this file only -# under the terms of either the GPL or the LGPL, and not to allow others to -# use your version of this file under the terms of the MPL, indicate your -# decision by deleting the provisions above and replace them with the notice -# and other provisions required by the GPL or the LGPL. If you do not delete -# the provisions above, a recipient may use your version of this file under -# the terms of any one of the MPL, the GPL or the LGPL. -# -# ***** END LICENSE BLOCK ***** - -# Generates IXPCOMError.java from xpcom/base/nsError.h -# -# usage: perl gen-nsErrors.pl < /xpcom/base/nsError.h > IXPCOMError.java - - -print "/* ***** BEGIN LICENSE BLOCK *****\n"; -print " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"; -print " *\n"; -print " * The contents of this file are subject to the Mozilla Public License Version\n"; -print " * 1.1 (the \"License\"); you may not use this file except in compliance with\n"; -print " * the License. You may obtain a copy of the License at\n"; -print " * http://www.mozilla.org/MPL/\n"; -print " *\n"; -print " * Software distributed under the License is distributed on an \"AS IS\" basis,\n"; -print " * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n"; -print " * for the specific language governing rights and limitations under the\n"; -print " * License.\n"; -print " *\n"; -print " * The Original Code is mozilla.org code.\n"; -print " *\n"; -print " * The Initial Developer of the Original Code is\n"; -print " * Netscape Communications Corporation.\n"; -print " * Portions created by the Initial Developer are Copyright (C) 1998\n"; -print " * the Initial Developer. All Rights Reserved.\n"; -print " *\n"; -print " * Contributor(s):\n"; -print " *\n"; -print " * Alternatively, the contents of this file may be used under the terms of\n"; -print " * either of the GNU General Public License Version 2 or later (the \"GPL\"),\n"; -print " * or the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"),\n"; -print " * in which case the provisions of the GPL or the LGPL are applicable instead\n"; -print " * of those above. If you wish to allow use of your version of this file only\n"; -print " * under the terms of either the GPL or the LGPL, and not to allow others to\n"; -print " * use your version of this file under the terms of the MPL, indicate your\n"; -print " * decision by deleting the provisions above and replace them with the notice\n"; -print " * and other provisions required by the GPL or the LGPL. If you do not delete\n"; -print " * the provisions above, a recipient may use your version of this file under\n"; -print " * the terms of any one of the MPL, the GPL or the LGPL.\n"; -print " *\n"; -print " * ***** END LICENSE BLOCK ***** */\n"; - -print "\n"; -print "package org.mozilla.xpcom;\n"; -print "\n\n"; - -print "/**\n"; -print " * Mozilla XPCOM error codes.\n"; -print " *

\n"; -print " * THIS FILE GENERATED FROM mozilla/xpcom/base/nsError.h.\n"; -print " * PLEASE SEE THAT FILE FOR FULL DOCUMENTATION.\n"; -print " */\n"; -print "public interface IXPCOMError {\n"; - -while () { - $line = $_; - - if ($prevline) { - $_ = $prevline.$_; - } - if (/(.*)\\$/) { - #splitted line - $prevline = $1; - next; - } - $prevline = ""; - - if (/^\s*#define\s+(NS_[A-Z_]+)\s+(0x)?([0-9a-fA-F]+)\s*$/) { - #define NS_ERROR_MODULE_XPCOM 1 - #define NS_ERROR_MODULE_BASE_OFFSET 0x45 - print " long $1 = $2$3L;\n"; - } - elsif (/^\s*#define\s+(NS_[A-Z_]+)\s+\((NS_[A-Z_]+)\s+\+\s+(0x)?([0-9a-fA-F]+)\s*\)\s*/) { - #define NS_ERROR_NOT_INITIALIZED (NS_ERROR_BASE + 1) - #define NS_ERROR_FACTORY_EXISTS (NS_ERROR_BASE + 0x100) - print " long $1 = $2 + $3$4L;\n"; - } - elsif (/^\s*#define\s+(NS_[A-Z_]+)\s+(NS_[A-Z_]+)\s\s*/) { - #define NS_ERROR_NO_INTERFACE NS_NOINTERFACE - print " long $1 = $2;\n"; - } - elsif (/^\s*#define\s+(NS_[A-Z_]+)\s+\(\(nsresult\)\s*(0x)?([0-9a-fA-F]+)L?\)\s*$/) { - #define NS_ERROR_BASE ((nsresult) 0xC1F30000) - #define NS_ERROR_ABORT ((nsresult) 0x80004004L) - print " long $1 = $2$3L;\n"; - } - elsif (/^\s*#define\s+(NS_[A-Z_]+)\s+NS_ERROR_GENERATE_FAILURE\s*\(\s*(NS_[A-Z_]+)\s*,\s*([0-9]+)\s*\)\s*$/) { - #define NS_BASE_STREAM_CLOSED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_BASE, 2) - $module = $2; - $code = $3; - print " long $1 = ((NS_ERROR_SEVERITY_ERROR<<31) |\n"; - print " (($module+NS_ERROR_MODULE_BASE_OFFSET)<<16) | $code);\n"; - } - elsif (/^\s*#define\s+(NS_[A-Z_]+)\s+NS_ERROR_GENERATE_SUCCESS\s*\(\s*(NS_[A-Z_]+)\s*,\s*([0-9]+)\s*\)\s*$/) { - #define NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_XPCOM, 1) - $module = $2; - $code = $3; - print " long $1 = ((NS_ERROR_SEVERITY_SUCCESS<<31) |\n"; - print " (($module+NS_ERROR_MODULE_BASE_OFFSET)<<16) | $code);\n"; - } - elsif (/^\s*\/\*(.*)\*\/\s*$/ && !/^\s*\/\*@[\{\}]\*\/\s*$/ && - !/^\s*\/\*\ -\*- Mode:/) { - #single line comment, but not - #/*@{*/, /*@}*/, - #/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ - print " /*$1*/\n"; - } - elsif (/^\s*$/) { - #empty line, but write only the first - #line from a sequence of empty lines - if (!$wasEmpty) { - print "\n"; - } - $wasEmpty = 1; - next; - } - else { - next; - } - - $wasEmpty = 0; -} - -print "}\n"; - diff --git a/mozilla/extensions/java/xpcom/interfaces/GREVersionRange.java b/mozilla/extensions/java/xpcom/interfaces/GREVersionRange.java deleted file mode 100644 index ede1a52322d..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/GREVersionRange.java +++ /dev/null @@ -1,80 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - - -public class GREVersionRange { - - private String lower; - private boolean lowerInclusive; - private String upper; - private boolean upperInclusive; - - public GREVersionRange(String aLower, boolean aLowerInclusive, - String aUpper, boolean aUpperInclusive) { - lower = aLower; - lowerInclusive = aLowerInclusive; - upper = aUpper; - upperInclusive = aUpperInclusive; - } - - public boolean check(String aVersion) { - VersionComparator comparator = new VersionComparator(); - int c = comparator.compare(aVersion, lower); - if (c < 0) { - return false; - } - - if (c == 0 && !lowerInclusive) { - return false; - } - - c = comparator.compare(aVersion, upper); - if (c > 0) { - return false; - } - - if (c == 0 && !upperInclusive) { - return false; - } - - return true; - } - -} - diff --git a/mozilla/extensions/java/xpcom/interfaces/IAppFileLocProvider.java b/mozilla/extensions/java/xpcom/interfaces/IAppFileLocProvider.java deleted file mode 100644 index 2b5232f2289..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/IAppFileLocProvider.java +++ /dev/null @@ -1,92 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.io.*; - - -/** - * Used by XPCOM's Directory Service to get file locations. - *

- * This interface is similar to nsIDirectoryServiceProvider and - * nsIDirectoryServiceProvider2, except that its methods use - * java.io.File instead of nsIFile. - *

- * - * @see Mozilla#initEmbedding - * @see Mozilla#initXPCOM - * @see - * nsIDirectoryServiceProvider - * @see - * Directory Service property names - */ -public interface IAppFileLocProvider { - - /** - * Directory Service calls this when it gets the first request for - * a property or on every request if the property is not persistent. - * - * @param prop the symbolic name of the file - * @param persistent an array of length one used to supply the output value: - * - * - * @return the file represented by the property - */ - File getFile(String prop, boolean[] persistent); - - /** - * Directory Service calls this when it gets a request for - * a property and the requested type is nsISimpleEnumerator. - * - * @param prop the symbolic name of the file list - * - * @return an array of file locations - */ - File[] getFiles(String prop); - -} - diff --git a/mozilla/extensions/java/xpcom/interfaces/IGRE.java b/mozilla/extensions/java/xpcom/interfaces/IGRE.java deleted file mode 100644 index 35ecd6e0118..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/IGRE.java +++ /dev/null @@ -1,127 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.io.*; - - -public interface IGRE { - - /** - * Initializes libXUL for embedding purposes. - *

- * NOTE: This function must be called from the "main" thread. - *

- * NOTE: At the present time, this function may only be called once in - * a given process. Use termEmbedding to clean up and free - * resources allocated by initEmbedding. - * - * @param aLibXULDirectory The directory in which the libXUL shared library - * was found. - * @param aAppDirectory The directory in which the application components - * and resources can be found. This will map to - * the "resource:app" directory service key. - * @param aAppDirProvider A directory provider for the application. This - * provider will be aggregated by a libXUL provider - * which will provide the base required GRE keys. - * - * @throws XPCOMException if a failure occurred during initialization - */ - void initEmbedding(File aLibXULDirectory, File aAppDirectory, - IAppFileLocProvider aAppDirProvider) throws XPCOMException; - - /** - * Terminates libXUL embedding. - *

- * NOTE: Release any references to XPCOM objects that you may be holding - * before calling this function. - * - * @throws XPCOMException if a failure occurred during initialization - */ - void termEmbedding() throws XPCOMException; - - /** - * Lock a profile directory using platform-specific semantics. - * - * @param aDirectory The profile directory to lock. - * @param aLockObject An opaque lock object. The directory will remain locked - * as long as the XPCOM reference is held. - */ - nsISupports lockProfileDirectory(File aDirectory) - throws XPCOMException; - - /** - * Fire notifications to inform the toolkit about a new profile. This - * method should be called after initEmbedding if the - * embedder wishes to run with a profile. - *

- * Normally the embedder should call lockProfileDirectory - * to lock the directory before calling this method. - *

- * NOTE: There are two possibilities for selecting a profile: - *

- */ - void notifyProfile(); - -} - - diff --git a/mozilla/extensions/java/xpcom/interfaces/INIParser.java b/mozilla/extensions/java/xpcom/interfaces/INIParser.java deleted file mode 100644 index f28aaaca326..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/INIParser.java +++ /dev/null @@ -1,235 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.io.*; -import java.nio.charset.Charset; -import java.util.*; - - -/** - * A simple parser for INI files. - */ -public class INIParser { - - private HashMap mSections; - - /** - * Creates a new INIParser instance from the INI file at the - * given path. aCharset specifies the character encoding of - * the file. - * - * @param aFilename path of INI file to parse - * @param aCharset character encoding of file - * @throws FileNotFoundException if aFilename does not exist. - * @throws IOException if there is a problem reading the given file. - */ - public INIParser(String aFilename, Charset aCharset) - throws FileNotFoundException, IOException { - initFromFile(new File(aFilename), aCharset); - } - - /** - * Creates a new INIParser instance from the INI file at the - * given path, which is assumed to be in the UTF-8 charset. - * - * @param aFilename path of INI file to parse - * @throws FileNotFoundException if aFilename does not exist. - * @throws IOException if there is a problem reading the given file. - */ - public INIParser(String aFilename) throws FileNotFoundException, IOException { - initFromFile(new File(aFilename), Charset.forName("UTF-8")); - } - - /** - * Creates a new INIParser instance from the given file. - * aCharset specifies the character encoding of the file. - * - * @param aFile INI file to parse - * @param aCharset character encoding of file - * @throws FileNotFoundException if aFile does not exist. - * @throws IOException if there is a problem reading the given file. - */ - public INIParser(File aFile, Charset aCharset) - throws FileNotFoundException, IOException { - initFromFile(aFile, aCharset); - } - - /** - * Creates a new INIParser instance from the given file, - * which is assumed to be in the UTF-8 charset. - * - * @param aFile INI file to parse - * @throws FileNotFoundException if aFile does not exist. - * @throws IOException if there is a problem reading the given file. - */ - public INIParser(File aFile) throws FileNotFoundException, IOException { - initFromFile(aFile, Charset.forName("UTF-8")); - } - - /** - * Parses given INI file. - * - * @param aFile INI file to parse - * @param aCharset character encoding of file - * @throws FileNotFoundException if aFile does not exist. - * @throws IOException if there is a problem reading the given file. - */ - private void initFromFile(File aFile, Charset aCharset) - throws FileNotFoundException, IOException { - FileInputStream fileStream = new FileInputStream(aFile); - InputStreamReader inStream = new InputStreamReader(fileStream, aCharset); - BufferedReader reader = new BufferedReader(inStream); - - mSections = new HashMap(); - String currSection = null; - - String line; - while ((line = reader.readLine()) != null) { - // skip empty lines and comment lines - String trimmedLine = line.trim(); - if (trimmedLine.length() == 0 || trimmedLine.startsWith("#") - || trimmedLine.startsWith(";")) { - continue; - } - - // Look for section headers (i.e. "[Section]"). - if (line.startsWith("[")) { - /* - * We are looking for a well-formed "[Section]". If this header is - * malformed (i.e. "[Section" or "[Section]Moretext"), just skip it - * and go on to next well-formed section header. - */ - if (!trimmedLine.endsWith("]") || - trimmedLine.indexOf("]") != (trimmedLine.length() - 1)) { - currSection = null; - continue; - } - - // remove enclosing brackets - currSection = trimmedLine.substring(1, trimmedLine.length() - 1); - continue; - } - - // If we haven't found a valid section header, continue to next line - if (currSection == null) { - continue; - } - - StringTokenizer tok = new StringTokenizer(line, "="); - if (tok.countTokens() != 2) { // looking for value pairs - continue; - } - - Properties props = (Properties) mSections.get(currSection); - if (props == null) { - props = new Properties(); - mSections.put(currSection, props); - } - props.setProperty(tok.nextToken(), tok.nextToken()); - } - - reader.close(); - } - - /** - * Returns an iterator over the section names available in the INI file. - * - * @return an iterator over the section names - */ - public Iterator getSections() { - return mSections.keySet().iterator(); - } - - /** - * Returns an iterator over the keys available within a section. - * - * @param aSection section name whose keys are to be returned - * @return an iterator over section keys, or null if no - * such section exists - */ - public Iterator getKeys(String aSection) { - /* - * Simple wrapper class to convert Enumeration to Iterator - */ - class PropertiesIterator implements Iterator { - private Enumeration e; - - public PropertiesIterator(Enumeration aEnum) { - e = aEnum; - } - - public boolean hasNext() { - return e.hasMoreElements(); - } - - public Object next() { - return e.nextElement(); - } - - public void remove() { - return; - } - } - - Properties props = (Properties) mSections.get(aSection); - if (props == null) { - return null; - } - - return new PropertiesIterator(props.propertyNames()); - } - - /** - * Gets the string value for a particular section and key. - * - * @param aSection a section name - * @param aKey the key whose value is to be returned. - * @return string value of particular section and key - */ - public String getString(String aSection, String aKey) { - Properties props = (Properties) mSections.get(aSection); - if (props == null) { - return null; - } - - return props.getProperty(aKey); - } - -} - diff --git a/mozilla/extensions/java/xpcom/interfaces/IXPCOM.java b/mozilla/extensions/java/xpcom/interfaces/IXPCOM.java deleted file mode 100644 index c455436364a..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/IXPCOM.java +++ /dev/null @@ -1,131 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.io.*; - - -public interface IXPCOM { - - /** - * Initializes XPCOM. You must call this method before proceeding - * to use XPCOM. - * - * @param aMozBinDirectory The directory containing the component - * registry and runtime libraries; - * or use null to use the working - * directory. - * - * @param aAppFileLocProvider The object to be used by Gecko that specifies - * to Gecko where to find profiles, the component - * registry preferences and so on; or use - * null for the default behaviour. - * - * @return the service manager - * - * @exception XPCOMException - */ - nsIServiceManager initXPCOM(File aMozBinDirectory, - IAppFileLocProvider aAppFileLocProvider) throws XPCOMException; - - /** - * Shutdown XPCOM. You must call this method after you are finished - * using xpcom. - * - * @param aServMgr The service manager which was returned by initXPCOM. - * This will release servMgr. - * - * @exception XPCOMException if a failure occurred during termination - */ - void shutdownXPCOM(nsIServiceManager aServMgr) throws XPCOMException; - - /** - * Public Method to access to the service manager. - * - * @return the service manager - * - * @exception XPCOMException - */ - nsIServiceManager getServiceManager() throws XPCOMException; - - /** - * Public Method to access to the component manager. - * - * @return the component manager - * - * @exception XPCOMException - */ - nsIComponentManager getComponentManager() throws XPCOMException; - - /** - * Public Method to access to the component registration manager. - * - * @return the component registration manager - * - * @exception XPCOMException - */ - nsIComponentRegistrar getComponentRegistrar() throws XPCOMException; - - /** - * Public Method to create an instance of a nsILocalFile. - * - * @param aPath A string which specifies a full file path to a - * location. Relative paths will be treated as an - * error (NS_ERROR_FILE_UNRECOGNIZED_PATH). - * @param aFollowLinks This attribute will determine if the nsLocalFile will - * auto resolve symbolic links. By default, this value - * will be false on all non unix systems. On unix, this - * attribute is effectively a noop. - * - * @return an instance of an nsILocalFile that points to given path - * - * @exception XPCOMException - */ - nsILocalFile newLocalFile(String aPath, boolean aFollowLinks) - throws XPCOMException; - -} - diff --git a/mozilla/extensions/java/xpcom/interfaces/Mozilla.java b/mozilla/extensions/java/xpcom/interfaces/Mozilla.java deleted file mode 100644 index aa35939c336..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/Mozilla.java +++ /dev/null @@ -1,903 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.io.*; -import java.lang.reflect.*; -import java.net.*; -import java.nio.charset.Charset; -import java.util.*; - - -/** - * @see http://www.mozilla.org/projects/embedding/GRE.html - */ -public class Mozilla implements IGRE, IXPCOM, IXPCOMError { - - private static Mozilla mozillaInstance = new Mozilla(); - - private static final String JAVAXPCOM_JAR = "javaxpcom.jar"; - - private IGRE gre = null; - - private IXPCOM xpcom = null; - - /** - * @return - */ - public static Mozilla getInstance() { - return mozillaInstance; - } - - /** - * - */ - private Mozilla() { - } - - /** - * Locates the path of a GRE with the specified properties. This method - * will only return GREs that support Java embedding (looks for the - * presence of "javaxpcom.jar"). - *

- * Currently this uses a "first-fit" algorithm, it does not select - * the newest available GRE. - * - * @param aVersions An array of version ranges: if any version range - * matches, the GRE is considered acceptable. - * @param aProperties A list of GRE property/value pairs which must - * all be satisfied. This parameter is ignored on - * Macintosh, because of the manner in which the - * XUL frameworks are installed. - * - * @return A file object of the appropriate path. If - * the "local" GRE is specified (via the USE_LOCAL_GRE - * environment variable, for example), returns - * null. - * - * @throws FileNotFoundException if an appropriate GRE could not be found - */ - public static File getGREPathWithProperties(GREVersionRange[] aVersions, - Properties aProperties) throws FileNotFoundException { - File grePath = null; - - // if GRE_HOME is in the environment, use that GRE - String env = System.getProperty("GRE_HOME"); - if (env != null) { - try { - grePath = new File(env).getCanonicalFile(); - } catch (IOException e) { - throw new FileNotFoundException("cannot access GRE_HOME"); - } - if (!grePath.exists()) { - throw new FileNotFoundException("GRE_HOME doesn't exist"); - } - return grePath; - } - - // the Gecko bits that sit next to the application or in the PATH - env = System.getProperty("USE_LOCAL_GRE"); - if (env != null) { - return null; - } - - // Search for GRE in platform specific locations. We want a GRE that - // supports Java, so we look for the "javaxpcom" property by default. - if (aProperties == null) { - aProperties = new Properties(); - } - aProperties.setProperty("javaxpcom", "1"); - - String osName = System.getProperty("os.name").toLowerCase(); - if (osName.startsWith("mac os x")) { - grePath = getGREPathMacOSX(aVersions); - } else if (osName.startsWith("windows")) { - grePath = getGREPathWindows(aVersions, aProperties); - } else { - // assume everything else is Unix/Linux - grePath = getGREPathUnix(aVersions, aProperties); - } - - if (grePath == null) { - throw new FileNotFoundException("GRE not found"); - } - - return grePath; - } - - /** - * @param aVersions - * @return - */ - private static File getGREPathMacOSX(GREVersionRange[] aVersions) { - /* - * Check the application bundle first, for - * /Contents/Frameworks/XUL.framework/libxpcom.dylib. - */ - File grePath = findGREBundleFramework(); - if (grePath != null) { - return grePath; - } - - // Check ~/Library/Frameworks/XUL.framework/Versions//libxpcom.dylib - String home = System.getProperty("user.home"); - if (home != null) { - grePath = findGREFramework(home, aVersions); - if (grePath != null) { - return grePath; - } - } - - // Check /Library/Frameworks/XUL.framework/Versions//libxpcom.dylib - return findGREFramework("", aVersions); - } - - /** - * @return - */ - private static File findGREBundleFramework() { - /* - * Use reflection to get Apple's NSBundle class, which can be used - * to get the bundle's "Frameworks" directory. - */ - try { - URL[] urls = new URL[1]; - urls[0] = new File("/System/Library/Java/").toURL(); - ClassLoader loader = new URLClassLoader(urls); - Class bundleClass = Class.forName("com.apple.cocoa.foundation.NSBundle", - true, loader); - - // Get the bundle for this app. If this is not executing from - // a bundle, this will return null. - Method mainBundleMethod = bundleClass.getMethod("mainBundle", null); - Object bundle = mainBundleMethod.invoke(null, null); - - if (bundle != null) { - // Get the path to the bundle's "Frameworks" directory - Method fwPathMethod = bundleClass.getMethod("privateFrameworksPath", - null); - String path = (String) fwPathMethod.invoke(bundle, null); - - // look for libxpcom.dylib - if (path.length() != 0) { - File xulDir = new File(path, "XUL.framework"); - if (xulDir.isDirectory()) { - File xpcomLib = new File(xulDir, "libxpcom.dylib"); - if (xpcomLib.canRead()) { - File grePath = xpcomLib.getCanonicalFile().getParentFile(); - - // Since GRE Properties aren't supported on Mac OS X, we check - // for the existence of the "javaxpcom.jar" file in the GRE. - File jar = new File(grePath, JAVAXPCOM_JAR); - if (jar.canRead()) { - // found GRE - return grePath; - } - } - } - } - } - } catch (Exception e) { } - - return null; - } - - /** - * @param aRootPath - * @param aVersions - * @return - */ - private static File findGREFramework(String aRootPath, - GREVersionRange[] aVersions) { - File frameworkDir = new File(aRootPath + - "/Library/Frameworks/XUL.framework/Versions"); - if (!frameworkDir.exists()) - return null; - - File[] files = frameworkDir.listFiles(); - for (int i = 0; i < files.length; i++) { - if (checkVersion(files[i].getName(), aVersions)) { - File xpcomLib = new File(files[i], "libxpcom.dylib"); - - // Since GRE Properties aren't supported on Mac OS X, we check - // for the existence of the "javaxpcom.jar" file in the GRE. - File jar = new File(files[i], JAVAXPCOM_JAR); - if (xpcomLib.canRead() && jar.canRead()) { - return files[i]; - } - } - } - - return null; - } - - /** - * @param aVersions - * @param aProperties - * @return - */ - private static File getGREPathWindows(GREVersionRange[] aVersions, - Properties aProperties) { - /* - * Note the usage of the "Software\\mozilla.org\\GRE" subkey - this allows - * us to have multiple versions of GREs on the same machine by having - * subkeys such as 1.0, 1.1, 2.0 etc. under it. - * - * Please see http://www.mozilla.org/projects/embedding/GRE.html for - * more info. - */ - - final String greKey = "Software\\mozilla.org\\GRE"; - - // See if there is a GRE registered for the current user. - // If not, look for one on the system. - String key = "HKEY_CURRENT_USER" + "\\" + greKey; - File grePath = getGREPathFromRegKey(key, aVersions, aProperties); - if (grePath == null) { - key = "HKEY_LOCAL_MACHINE" + "\\" + greKey; - grePath = getGREPathFromRegKey(key, aVersions, aProperties); - } - - return grePath; - } - - /** - * @param aRegKey - * @param aVersions - * @param aProperties - * @return - */ - private static File getGREPathFromRegKey(String aRegKey, - GREVersionRange[] aVersions, Properties aProperties) { - // create a temp file for the registry export - File tempFile; - try { - tempFile = File.createTempFile("jx_registry", null); - } catch (IOException e) { - // failed to create temp file. ABORT - return null; - } - - Process proc; - try { - proc = Runtime.getRuntime().exec("regedit /e " + "\"" + tempFile.getPath() - + "\" \"" + aRegKey + "\""); - proc.waitFor(); - } catch (Exception e) { - // Failed to run regedit.exe. Length of temp file is zero, and that's - // handled next. - } - - // If there is a key by that name in the registry, then the file length - // will not be zero. - File grePath = null; - if (tempFile.length() != 0) { - grePath = getGREPathFromRegistryFile(tempFile.getPath(), - aRegKey, aVersions, aProperties); - } - - tempFile.delete(); - return grePath; - } - - /** - * @param aFileName - * @param aCharset - * @param aKeyName - * @param aVersions - * @param aProperties - * @return - */ - private static File getGREPathFromRegistryFile(String aFileName, - String aKeyName, GREVersionRange[] aVersions, - Properties aProperties) { - INIParser parser; - try { - parser = new INIParser(aFileName, Charset.forName("UTF-16")); - } catch (Exception e) { - // Problem reading from file. Bail out. - return null; - } - - Iterator sectionsIter = parser.getSections(); - while (sectionsIter.hasNext()) { - // get 'section' name, which will be a registry key name - String section = (String) sectionsIter.next(); - - // Skip over GRE key ("\Software\mozilla.org\GRE") - int gre_len = aKeyName.length(); - if (section.length() <= gre_len) { - continue; - } - - // Get the GRE subkey; that is, everything after - // "\Software\mozilla.org\GRE\" - String subkeyName = section.substring(gre_len + 1); - - // We are only interested in _immediate_ subkeys. We want - // "\Software\mozilla.org\GRE\" but not - // "\Software\mozilla.org\GRE\\". - if (subkeyName.indexOf('\\') != -1) { - continue; - } - - // See if this registry key has a "Version" value, and if so, compare - // it to our desired versions. - String version = parser.getString(section, "\"Version\""); - if (version == null) { - continue; - } - // remove quotes around string - version = version.substring(1, version.length() - 1); - if (!checkVersion(version, aVersions)) { - continue; - } - - // All properties must match, keeping in mind that the propery/value - // pairs returned by regedit.exe have quotes around them. - if (aProperties != null) { - boolean ok = true; - Enumeration e = aProperties.propertyNames(); - while (ok && e.hasMoreElements()) { - String prop = (String) e.nextElement(); - String greValue = parser.getString(section, "\"" + prop + "\""); - if (greValue == null) { - // No such property is set for this GRE. Go on to next GRE. - ok = false; - } else { - // See if the value of the property for the GRE matches - // the given value. - String value = aProperties.getProperty(prop); - if (!greValue.equals("\"" + value + "\"")) { - ok = false; - } - } - } - if (!ok) { - continue; - } - } - - String pathStr = parser.getString(section, "\"GreHome\""); - if (pathStr != null) { - // remove quotes around string - pathStr = pathStr.substring(1, pathStr.length() - 1); - File grePath = new File(pathStr); - if (grePath.exists()) { - File xpcomLib = new File(grePath, "xpcom.dll"); - if (xpcomLib.canRead()) { - // found a good GRE - return grePath; - } - } - } - } - - return null; - } - - /** - * @param aVersions - * @param aProperties - * @return - */ - private static File getGREPathUnix(GREVersionRange[] aVersions, - Properties aProperties) { - File grePath = null; - - String env = System.getProperty("MOZ_GRE_CONF"); - if (env != null) { - grePath = getPathFromConfigFile(env, aVersions, aProperties); - if (grePath != null) { - return grePath; - } - } - - final String greUserConfFile = ".gre.config"; - final String greUserConfDir = ".gre.d"; - final String greConfPath = "/etc/gre.conf"; - final String greConfDir = "/etc/gre.d"; - - env = System.getProperty("user.home"); - if (env != null) { - // Look in ~/.gre.config - grePath = getPathFromConfigFile(env + File.separator + greUserConfFile, - aVersions, aProperties); - if (grePath != null) { - return grePath; - } - - // Look in ~/.gre.d/*.conf - grePath = getPathFromConfigDir(env + File.separator + greUserConfDir, - aVersions, aProperties); - if (grePath != null) { - return grePath; - } - } - - // Look for a global /etc/gre.conf file - grePath = getPathFromConfigFile(greConfPath, aVersions, aProperties); - if (grePath != null) { - return grePath; - } - - // Look for a group of config files in /etc/gre.d/ - grePath = getPathFromConfigDir(greConfDir, aVersions, aProperties); - return grePath; - } - - /** - * @param aFileName - * @param aVersions - * @param aProperties - * @return - */ - private static File getPathFromConfigFile(String aFileName, - GREVersionRange[] aVersions, Properties aProperties) { - INIParser parser; - try { - parser = new INIParser(aFileName); - } catch (Exception e) { - // Problem reading from file. Bail out. - return null; - } - - Iterator sectionsIter = parser.getSections(); - while (sectionsIter.hasNext()) { - // get 'section' name, which will be a version string - String section = (String) sectionsIter.next(); - - // if this isn't one of the versions we are looking for, move - // on to next section - if (!checkVersion(section, aVersions)) { - continue; - } - - // all properties must match - if (aProperties != null) { - boolean ok = true; - Enumeration e = aProperties.propertyNames(); - while (ok && e.hasMoreElements()) { - String prop = (String) e.nextElement(); - String greValue = parser.getString(section, prop); - if (greValue == null) { - // No such property is set for this GRE. Go on to next GRE. - ok = false; - } else { - // See if the value of the property for the GRE matches - // the given value. - if (!greValue.equals(aProperties.getProperty(prop))) { - ok = false; - } - } - } - if (!ok) { - continue; - } - } - - String pathStr = parser.getString(section, "GRE_PATH"); - if (pathStr != null) { - File grePath = new File(pathStr); - if (grePath.exists()) { - File xpcomLib = new File(grePath, "libxpcom.so"); - if (xpcomLib.canRead()) { - // found a good GRE - return grePath; - } - } - } - } - - return null; - } - - /** - * @param aDirName - * @param aVersions - * @param aProperties - * @return - */ - private static File getPathFromConfigDir(String aDirName, - GREVersionRange[] aVersions, Properties aProperties) { - /* - * Open the directory provided and try to read any files in that - * directory that end with .conf. We look for an entry that might - * point to the GRE that we're interested in. - */ - - File dir = new File(aDirName); - if (!dir.isDirectory()) { - return null; - } - - File grePath = null; - File[] files = dir.listFiles(); - for (int i = 0; i < files.length && grePath == null; i++) { - // only look for files that end in '.conf' - if (!files[i].getName().endsWith(".conf")) { - continue; - } - - grePath = getPathFromConfigFile(files[i].getPath(), aVersions, - aProperties); - } - - return grePath; - } - - /** - * @param aVersionToCheck - * @param aVersions - * @return - */ - private static boolean checkVersion(String aVersionToCheck, - GREVersionRange[] aVersions) { - for (int i = 0; i < aVersions.length; i++) { - if (aVersions[i].check(aVersionToCheck)) { - return true; - } - } - return false; - } - - /** - * Initializes libXUL for embedding purposes. - *

- * NOTE: This function must be called from the "main" thread. - *

- * NOTE: At the present time, this function may only be called once in - * a given process. Use termEmbedding to clean up and free - * resources allocated by initEmbedding. - * - * @param aLibXULDirectory The directory in which the libXUL shared library - * was found. - * @param aAppDirectory The directory in which the application components - * and resources can be found. This will map to - * the "resource:app" directory service key. - * @param aAppDirProvider A directory provider for the application. This - * provider will be aggregated by a libXUL provider - * which will provide the base required GRE keys. - * - * @throws IllegalArgumentException if aLibXULDirectory is not - * a valid path - * @throws XPCOMException if a failure occurred during initialization - */ - public void initEmbedding(File aLibXULDirectory, File aAppDirectory, - IAppFileLocProvider aAppDirProvider) throws XPCOMException { - loadJavaXPCOM(aLibXULDirectory, true); - gre.initEmbedding(aLibXULDirectory, aAppDirectory, aAppDirProvider); - } - - /** - * @param aLibXULDirectory - * @param aLoadGREImpl - * @throws XPCOMException - */ - private void loadJavaXPCOM(File aLibXULDirectory, boolean aLoadGREImpl) - throws XPCOMException { - File jar = new File(aLibXULDirectory, JAVAXPCOM_JAR); - if (!jar.exists()) { - throw new XPCOMException(NS_ERROR_FILE_INVALID_PATH); - } - - URL[] urls = new URL[1]; - try { - urls[0] = jar.toURL(); - } catch (MalformedURLException e) { - throw new XPCOMException(NS_ERROR_FILE_INVALID_PATH); - } - ClassLoader loader = new URLClassLoader(urls, - this.getClass().getClassLoader()); - - try { - if (aLoadGREImpl) { - Class greClass = Class.forName("org.mozilla.xpcom.internal.GREImpl", - true, loader); - gre = (IGRE) greClass.newInstance(); - } - Class xpcomClass = Class.forName("org.mozilla.xpcom.internal.XPCOMImpl", - true, loader); - xpcom = (IXPCOM) xpcomClass.newInstance(); - } catch (Exception e) { - throw new XPCOMException(NS_ERROR_FAILURE, - "failure creating org.mozilla.xpcom.internal.*"); - } - } - - /** - * Terminates libXUL embedding. - *

- * NOTE: Release any references to XPCOM objects that you may be holding - * before calling this function. - */ - public void termEmbedding() throws XPCOMException { - try { - gre.termEmbedding(); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized GRE object"); - } finally { - gre = null; - xpcom = null; - } - } - - /** - * Initializes XPCOM. You must call this method before proceeding - * to use XPCOM. - * - * @param aMozBinDirectory The directory containing the component - * registry and runtime libraries; - * or use null to use the working - * directory. - * - * @param aAppFileLocProvider The object to be used by Gecko that specifies - * to Gecko where to find profiles, the component - * registry preferences and so on; or use - * null for the default behaviour. - * - * @return the service manager - * - * @exception XPCOMException

- */ - public nsIServiceManager initXPCOM(File aMozBinDirectory, - IAppFileLocProvider aAppFileLocProvider) throws XPCOMException { - loadJavaXPCOM(aMozBinDirectory, false); - return xpcom.initXPCOM(aMozBinDirectory, aAppFileLocProvider); - } - - /** - * Shutdown XPCOM. You must call this method after you are finished - * using xpcom. - * - * @param aServMgr The service manager which was returned by initXPCOM. - * This will release servMgr. - * - * @exception XPCOMException if a failure occurred during termination - */ - public void shutdownXPCOM(nsIServiceManager aServMgr) throws XPCOMException { - try { - xpcom.shutdownXPCOM(aServMgr); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized XPCOM object"); - } finally { - xpcom = null; - } - } - - /** - * Public Method to access to the service manager. - * - * @return the service manager - * - * @exception XPCOMException - */ - public nsIServiceManager getServiceManager() throws XPCOMException { - try { - return xpcom.getServiceManager(); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized XPCOM object"); - } - } - - /** - * Public Method to access to the component manager. - * - * @return the component manager - * - * @exception XPCOMException - */ - public nsIComponentManager getComponentManager() throws XPCOMException { - try { - return xpcom.getComponentManager(); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized XPCOM object"); - } - } - - /** - * Public Method to access to the component registration manager. - * - * @return the component registration manager - * - * @exception XPCOMException - */ - public nsIComponentRegistrar getComponentRegistrar() throws XPCOMException { - try { - return xpcom.getComponentRegistrar(); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized XPCOM object"); - } - } - - /** - * Public Method to create an instance of a nsILocalFile. - * - * @param aPath A string which specifies a full file path to a - * location. Relative paths will be treated as an - * error (NS_ERROR_FILE_UNRECOGNIZED_PATH). - * @param aFollowLinks This attribute will determine if the nsLocalFile will - * auto resolve symbolic links. By default, this value - * will be false on all non unix systems. On unix, this - * attribute is effectively a noop. - * - * @return an instance of an nsILocalFile that points to given path - * - * @exception XPCOMException - */ - public nsILocalFile newLocalFile(String aPath, boolean aFollowLinks) - throws XPCOMException { - try { - return xpcom.newLocalFile(aPath, aFollowLinks); - } catch (NullPointerException e) { - throw new XPCOMException(Mozilla.NS_ERROR_NULL_POINTER, - "Attempt to use unitialized XPCOM object"); - } - } - - /** - * If you create a class that implements nsISupports, you will need to provide - * an implementation of the queryInterface method. This helper - * function provides a simple implementation. Therefore, if your class does - * not need to do anything special with queryInterface, your - * implementation would look like: - *
-   *      public nsISupports queryInterface(String aIID) {
-   *        return XPCOM.queryInterface(this, aIID);
-   *      }
-   * 
- * - * @param aObject object to query - * @param aIID requested interface IID - * - * @return aObject if the given object supports that - * interface; - * null otherwise. - */ - public static nsISupports queryInterface(nsISupports aObject, String aIID) { - ArrayList classes = new ArrayList(); - classes.add(aObject.getClass()); - - while (!classes.isEmpty()) { - Class clazz = (Class) classes.remove(0); - - // Skip over any class/interface in the "java.*" and "javax.*" domains. - String className = clazz.getName(); - if (className.startsWith("java.") || className.startsWith("javax.")) { - continue; - } - - // If given IID matches that of the current interface, then we - // know that aObject implements the interface specified by the given IID. - if (clazz.isInterface() && className.startsWith("org.mozilla")) { - String iid = Mozilla.getInterfaceIID(clazz); - if (iid != null && aIID.equals(iid)) { - return aObject; - } - } - - // clazz didn't match, so add the interfaces it implements - Class[] interfaces = clazz.getInterfaces(); - for (int i = 0; i < interfaces.length; i++ ) { - classes.add(interfaces[i]); - } - - // Also add its superclass - Class superclass = clazz.getSuperclass(); - if (superclass != null) { - classes.add(superclass); - } - } - - return null; - } - - /** - * Gets the interface IID for a particular Java interface. This is similar - * to NS_GET_IID in the C++ Mozilla files. - * - * @param aInterface interface which has defined an IID - * - * @return IID for given interface - */ - public static String getInterfaceIID(Class aInterface) { - // Get short class name (i.e. "bar", not "org.blah.foo.bar") - StringBuffer iidName = new StringBuffer(); - String fullClassName = aInterface.getName(); - int index = fullClassName.lastIndexOf("."); - String className = index > 0 ? fullClassName.substring(index + 1) - : fullClassName; - - // Create iid field name - if (className.startsWith("ns")) { - iidName.append("NS_"); - iidName.append(className.substring(2).toUpperCase()); - } else { - iidName.append(className.toUpperCase()); - } - iidName.append("_IID"); - - String iid; - try { - Field iidField = aInterface.getDeclaredField(iidName.toString()); - iid = (String) iidField.get(null); - } catch (NoSuchFieldException e) { - // Class may implement non-Mozilla interfaces, which would not have an - // IID method. In that case, just null. - iid = null; - } catch (IllegalAccessException e) { - // Not allowed to access that field for some reason. Write out an - // error message, but don't fail. - System.err.println("ERROR: Could not get field " + iidName.toString()); - iid = null; - } - - return iid; - } - - /** - * @see IGRE#lockProfileDirectory(File, nsISupports) - */ - public nsISupports lockProfileDirectory(File aDirectory) - throws XPCOMException - { - return gre.lockProfileDirectory(aDirectory); - } - - /** - * @see IGRE#notifyProfile() - */ - public void notifyProfile() { - gre.notifyProfile(); - } - -} - diff --git a/mozilla/extensions/java/xpcom/interfaces/VersionComparator.java b/mozilla/extensions/java/xpcom/interfaces/VersionComparator.java deleted file mode 100644 index c4438f49c96..00000000000 --- a/mozilla/extensions/java/xpcom/interfaces/VersionComparator.java +++ /dev/null @@ -1,269 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom; - -import java.util.*; - - -/** - * Version strings are dot-separated sequences of version-parts. - *

- * A version-part consists of up to four parts, all of which are optional: - *
- * <number-a><string-b><number-c> - * <string-d (everything else)> - *

- * A version-part may also consist of a single asterisk "*" which indicates - * "infinity". - *

- * Numbers are base-10, and are zero if left out. - * Strings are compared bytewise. - *

- * For additional backwards compatibility, if "string-b" is "+" then - * "number-a" is incremented by 1 and "string-b" becomes "pre". - *

- * 1.0pre1
- * < 1.0pre2  
- *   < 1.0 == 1.0.0 == 1.0.0.0
- *     < 1.1pre == 1.1pre0 == 1.0+
- *       < 1.1pre1a
- *         < 1.1pre1
- *           < 1.1pre10a
- *             < 1.1pre10
- * 
- * Although not required by this interface, it is recommended that - * numbers remain within the limits of a signed char, i.e. -127 to 128. - */ -public class VersionComparator implements nsIVersionComparator { - - public nsISupports queryInterface(String aIID) { - return Mozilla.queryInterface(this, aIID); - } - - /** - * Compare two version strings - * @param A a version string - * @param B a version string - * @return a value less than 0 if A < B; - * the value 0 if A == B; - * or a value greater than 0 if A > B - */ - public int compare(String A, String B) { - int result; - String a = A, b = B; - - do { - VersionPart va = new VersionPart(); - VersionPart vb = new VersionPart(); - a = parseVersionPart(a, va); - b = parseVersionPart(b, vb); - - result = compareVersionPart(va, vb); - if (result != 0) { - break; - } - } while (a != null || b != null); - - return result; - } - - private class VersionPart { - int numA = 0; - String strB; - int numC = 0; - String extraD; - } - - private static String parseVersionPart(String aVersion, VersionPart result) { - if (aVersion == null || aVersion.length() == 0) { - return aVersion; - } - - StringTokenizer tok = new StringTokenizer(aVersion.trim(), "."); - String part = tok.nextToken(); - - if (part.equals("*")) { - result.numA = Integer.MAX_VALUE; - result.strB = ""; - } else { - VersionPartTokenizer vertok = new VersionPartTokenizer(part); - try { - result.numA = Integer.parseInt(vertok.nextToken()); - } catch (NumberFormatException e) { - // parsing error; default to zero like 'strtol' C function - result.numA = 0; - } - - if (vertok.hasMoreElements()) { - String str = vertok.nextToken(); - - // if part is of type "+" - if (str.charAt(0) == '+') { - result.numA++; - result.strB = "pre"; - } else { - // else if part is of type "..." - result.strB = str; - - if (vertok.hasMoreTokens()) { - try { - result.numC = Integer.parseInt(vertok.nextToken()); - } catch (NumberFormatException e) { - // parsing error; default to zero like 'strtol' C function - result.numC = 0; - } - if (vertok.hasMoreTokens()) { - result.extraD = vertok.getRemainder(); - } - } - } - } - } - - if (tok.hasMoreTokens()) { - // return everything after "." - return aVersion.substring(part.length() + 1); - } - return null; - } - - private int compareVersionPart(VersionPart va, VersionPart vb) { - int res = compareInt(va.numA, vb.numA); - if (res != 0) { - return res; - } - - res = compareString(va.strB, vb.strB); - if (res != 0) { - return res; - } - - res = compareInt(va.numC, vb.numC); - if (res != 0) { - return res; - } - - return compareString(va.extraD, vb.extraD); - } - - private int compareInt(int n1, int n2) { - return n1 - n2; - } - - private int compareString(String str1, String str2) { - // any string is *before* no string - if (str1 == null) { - return (str2 != null) ? 1 : 0; - } - - if (str2 == null) { - return -1; - } - - return str1.compareTo(str2); - } - -} - -/** - * Specialized tokenizer for Mozilla version strings. A token can - * consist of one of the four sections of a version string: - * <number-a><string-b><number-c> - * <string-d (everything else)>. - */ -class VersionPartTokenizer implements Enumeration { - - String part; - - public VersionPartTokenizer(String aPart) { - part = aPart; - } - - public boolean hasMoreElements() { - return part.length() != 0; - } - - public boolean hasMoreTokens() { - return part.length() != 0; - } - - public Object nextElement() { - if (part.matches("[\\+\\-]?[0-9].*")) { - // if string starts with a number... - int index = 0; - if (part.charAt(0) == '+' || part.charAt(0) == '-') { - index = 1; - } - - while (index < part.length() && Character.isDigit(part.charAt(index))) { - index++; - } - - String numPart = part.substring(0, index); - part = part.substring(index); - return numPart; - } else { - // ... or if this is the non-numeric part of version string - int index = 0; - while (index < part.length() && !Character.isDigit(part.charAt(index))) { - index++; - } - - String alphaPart = part.substring(0, index); - part = part.substring(index); - return alphaPart; - } - } - - public String nextToken() { - return (String) nextElement(); - } - - /** - * Returns what remains of the original string, without tokenization. This - * method is useful for getting the <string-d (everything else)> - * section of a version string. - * - * @return remaining version string - */ - public String getRemainder() { - return part; - } - -} - diff --git a/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.cpp b/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.cpp deleted file mode 100644 index ff4bd56fe81..00000000000 --- a/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.cpp +++ /dev/null @@ -1,253 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "nsAppFileLocProviderProxy.h" -#include "nsJavaXPCOMBindingUtils.h" -#include "nsILocalFile.h" -#include "nsISimpleEnumerator.h" - - -nsAppFileLocProviderProxy::nsAppFileLocProviderProxy(jobject aJavaObject) -{ - mJavaLocProvider = GetJNIEnv()->NewGlobalRef(aJavaObject); -} - -nsAppFileLocProviderProxy::~nsAppFileLocProviderProxy() -{ - GetJNIEnv()->DeleteGlobalRef(mJavaLocProvider); -} - -NS_IMPL_ISUPPORTS2(nsAppFileLocProviderProxy, - nsIDirectoryServiceProvider, - nsIDirectoryServiceProvider2) - - -// nsIDirectoryServiceProvider - -NS_IMETHODIMP -nsAppFileLocProviderProxy::GetFile(const char* aProp, PRBool* aIsPersistant, - nsIFile** aResult) -{ - // Setup params for calling Java function - JNIEnv* env = GetJNIEnv(); - jstring prop = env->NewStringUTF(aProp); - if (!prop) - return NS_ERROR_OUT_OF_MEMORY; - jbooleanArray persistant = env->NewBooleanArray(1); - if (!persistant) - return NS_ERROR_OUT_OF_MEMORY; - - // Create method ID - jmethodID mid = nsnull; - jclass clazz = env->GetObjectClass(mJavaLocProvider); - if (clazz) { - mid = env->GetMethodID(clazz, "getFile", - "(Ljava/lang/String;[Z)Ljava/io/File;"); - } - if (!mid) - return NS_ERROR_FAILURE; - - // Call Java function - jobject javaFile = nsnull; - javaFile = env->CallObjectMethod(mJavaLocProvider, mid, prop, persistant); - if (javaFile == nsnull || env->ExceptionCheck()) - return NS_ERROR_FAILURE; - - // Set boolean output value - env->GetBooleanArrayRegion(persistant, 0, 1, (jboolean*) aIsPersistant); - - // Set nsIFile result value - nsCOMPtr localFile; - nsresult rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile)); - if (NS_SUCCEEDED(rv)) { - return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult); - } - - return rv; -} - - -// nsIDirectoryServiceProvider2 - -class DirectoryEnumerator : public nsISimpleEnumerator -{ -public: - NS_DECL_ISUPPORTS - - DirectoryEnumerator(jobjectArray aJavaFileArray) - : mIndex(0) - { - JNIEnv* env = GetJNIEnv(); - mJavaFileArray = NS_STATIC_CAST(jobjectArray, - env->NewGlobalRef(aJavaFileArray)); - mArraySize = env->GetArrayLength(aJavaFileArray); - } - - ~DirectoryEnumerator() - { - GetJNIEnv()->DeleteGlobalRef(mJavaFileArray); - } - - NS_IMETHOD HasMoreElements(PRBool* aResult) - { - if (!mJavaFileArray) { - *aResult = PR_FALSE; - } else { - *aResult = (mIndex < mArraySize); - } - return NS_OK; - } - - NS_IMETHOD GetNext(nsISupports** aResult) - { - nsresult rv = NS_ERROR_FAILURE; - - JNIEnv* env = GetJNIEnv(); - jobject javaFile = env->GetObjectArrayElement(mJavaFileArray, mIndex++); - if (javaFile) { - nsCOMPtr localFile; - rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile)); - env->DeleteLocalRef(javaFile); - - if (NS_SUCCEEDED(rv)) { - return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult); - } - } - - env->ExceptionClear(); - return NS_ERROR_FAILURE; - } - -private: - jobjectArray mJavaFileArray; - PRUint32 mArraySize; - PRUint32 mIndex; -}; - -NS_IMPL_ISUPPORTS1(DirectoryEnumerator, nsISimpleEnumerator) - -NS_IMETHODIMP -nsAppFileLocProviderProxy::GetFiles(const char* aProp, - nsISimpleEnumerator** aResult) -{ - nsresult rv = NS_OK; - - // Setup params for calling Java function - JNIEnv* env = GetJNIEnv(); - jstring prop = env->NewStringUTF(aProp); - if (!prop) - rv = NS_ERROR_OUT_OF_MEMORY; - - // Create method ID - jmethodID mid = nsnull; - if (NS_SUCCEEDED(rv)) { - jclass clazz = env->GetObjectClass(mJavaLocProvider); - if (clazz) { - mid = env->GetMethodID(clazz, "getFiles", - "(Ljava/lang/String;)[Ljava/io/File;"); - env->DeleteLocalRef(clazz); - } - if (!mid) - rv = NS_ERROR_FAILURE; - } - - // Call Java function - jobject javaFileArray = nsnull; - if (NS_SUCCEEDED(rv)) { - javaFileArray = env->CallObjectMethod(mJavaLocProvider, mid, prop); - - // Handle any exception thrown by Java method. - jthrowable exp = env->ExceptionOccurred(); - if (exp) { -#ifdef DEBUG - env->ExceptionDescribe(); -#endif - - // If the exception is an instance of XPCOMException, then get the - // nsresult from the exception instance. Else, default to - // NS_ERROR_FAILURE. - if (env->IsInstanceOf(exp, xpcomExceptionClass)) { - jfieldID fid; - fid = env->GetFieldID(xpcomExceptionClass, "errorcode", "J"); - if (fid) { - rv = env->GetLongField(exp, fid); - } else { - rv = NS_ERROR_FAILURE; - } - NS_ASSERTION(fid, "Couldn't get 'errorcode' field of XPCOMException"); - } else { - rv = NS_ERROR_FAILURE; - } - } else { - // No exception thrown. Check the result. - if (javaFileArray == nsnull) { - rv = NS_ERROR_FAILURE; - } - } - } - - if (NS_SUCCEEDED(rv)) { - // Parse return value - *aResult = new DirectoryEnumerator(NS_STATIC_CAST(jobjectArray, - javaFileArray)); - NS_ADDREF(*aResult); - return NS_OK; - } - - // Handle error conditions - *aResult = nsnull; - env->ExceptionClear(); - return rv; -} - - -//////////////////////////////////////////////////////////////////////////////// - -nsresult -NS_NewAppFileLocProviderProxy(jobject aJavaLocProvider, - nsIDirectoryServiceProvider** aResult) -{ - nsAppFileLocProviderProxy* provider = - new nsAppFileLocProviderProxy(aJavaLocProvider); - if (provider == nsnull) - return NS_ERROR_OUT_OF_MEMORY; - NS_ADDREF(provider); - - *aResult = provider; - return NS_OK; -} - diff --git a/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.h b/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.h deleted file mode 100644 index d3b90880d6c..00000000000 --- a/mozilla/extensions/java/xpcom/nsAppFileLocProviderProxy.h +++ /dev/null @@ -1,65 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef _nsAppFileLocProviderProxy_h_ -#define _nsAppFileLocProviderProxy_h_ - -#include "nsIDirectoryService.h" -#include "jni.h" - - -class nsAppFileLocProviderProxy : public nsIDirectoryServiceProvider2 -{ -public: - nsAppFileLocProviderProxy(jobject aJavaLocProvider); - ~nsAppFileLocProviderProxy(); - - NS_DECL_ISUPPORTS - NS_DECL_NSIDIRECTORYSERVICEPROVIDER - NS_DECL_NSIDIRECTORYSERVICEPROVIDER2 - -private: - jobject mJavaLocProvider; -}; - -extern "C" nsresult -NS_NewAppFileLocProviderProxy(jobject aJavaLocProvider, - nsIDirectoryServiceProvider** aResult); - - -#endif //_nsAppFileLocProviderProxy_h_ - diff --git a/mozilla/extensions/java/xpcom/nsJavaInterfaces.cpp b/mozilla/extensions/java/xpcom/nsJavaInterfaces.cpp deleted file mode 100644 index e34eb7fa938..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaInterfaces.cpp +++ /dev/null @@ -1,313 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "nsJavaInterfaces.h" -#include "nsJavaWrapper.h" -#include "nsJavaXPCOMBindingUtils.h" -#include "nsJavaXPTCStub.h" -#include "nsIComponentRegistrar.h" -#include "nsString.h" -#include "nsISimpleEnumerator.h" -#include "nsIInterfaceInfoManager.h" -#include "nsIInputStream.h" -#include "nsEnumeratorUtils.h" -#include "nsAppFileLocProviderProxy.h" -#include "nsXULAppAPI.h" -#include "nsILocalFile.h" - - -nsresult -InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory, - jobject aAppDirectory, jobject aAppDirProvider) -{ - nsresult rv; - if (!InitializeJavaGlobals(env)) - return NS_ERROR_FAILURE; - - // create an nsILocalFile from given java.io.File - nsCOMPtr libXULDir; - if (aLibXULDirectory) { - rv = File_to_nsILocalFile(env, aLibXULDirectory, getter_AddRefs(libXULDir)); - NS_ENSURE_SUCCESS(rv, rv); - } - nsCOMPtr appDir; - if (aAppDirectory) { - rv = File_to_nsILocalFile(env, aAppDirectory, getter_AddRefs(appDir)); - NS_ENSURE_SUCCESS(rv, rv); - } - - // create nsAppFileLocProviderProxy from given Java object - nsCOMPtr provider; - if (aAppDirProvider) { - rv = NS_NewAppFileLocProviderProxy(aAppDirProvider, - getter_AddRefs(provider)); - NS_ENSURE_SUCCESS(rv, rv); - } - - // init libXUL - return XRE_InitEmbedding(libXULDir, appDir, provider, nsnull, 0); -} - -extern "C" NS_EXPORT void -GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory, - jobject aAppDirectory, jobject aAppDirProvider) -{ - nsresult rv = InitEmbedding_Impl(env, aLibXULDirectory, aAppDirectory, - aAppDirProvider); - - if (NS_FAILED(rv)) { - ThrowException(env, rv, "Failure in initEmbedding"); - FreeJavaGlobals(env); - } -} - -extern "C" NS_EXPORT void -GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject) -{ - // Free globals before calling XRE_TermEmbedding(), since we need some - // XPCOM services. - FreeJavaGlobals(env); - - XRE_TermEmbedding(); -} - -nsresult -InitXPCOM_Impl(JNIEnv* env, jobject aMozBinDirectory, - jobject aAppFileLocProvider, jobject* aResult) -{ - nsresult rv; - if (!InitializeJavaGlobals(env)) - return NS_ERROR_FAILURE; - - // create an nsILocalFile from given java.io.File - nsCOMPtr directory; - if (aMozBinDirectory) { - rv = File_to_nsILocalFile(env, aMozBinDirectory, getter_AddRefs(directory)); - NS_ENSURE_SUCCESS(rv, rv); - } - - // create nsAppFileLocProviderProxy from given Java object - nsAppFileLocProviderProxy* provider = nsnull; - if (aAppFileLocProvider) { - provider = new nsAppFileLocProviderProxy(aAppFileLocProvider); - if (!provider) - return NS_ERROR_OUT_OF_MEMORY; - } - - // init XPCOM - nsCOMPtr servMan; - rv = NS_InitXPCOM2(getter_AddRefs(servMan), directory, provider); - if (provider) { - delete provider; - } - NS_ENSURE_SUCCESS(rv, rv); - - // create Java proxy for service manager returned by NS_InitXPCOM2 - return GetNewOrUsedJavaObject(env, servMan, NS_GET_IID(nsIServiceManager), - aResult); -} - -extern "C" NS_EXPORT jobject -XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory, - jobject aAppFileLocProvider) -{ - jobject servMan; - nsresult rv = InitXPCOM_Impl(env, aMozBinDirectory, aAppFileLocProvider, - &servMan); - if (NS_SUCCEEDED(rv)) - return servMan; - - ThrowException(env, rv, "Failure in initXPCOM"); - FreeJavaGlobals(env); - return nsnull; -} - -extern "C" NS_EXPORT void -XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr) -{ - nsresult rv; - nsCOMPtr servMgr; - if (aServMgr) { - // Get native XPCOM instance - rv = GetNewOrUsedXPCOMObject(env, aServMgr, NS_GET_IID(nsIServiceManager), - getter_AddRefs(servMgr)); - NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get XPCOM obj for ServiceMgr."); - - // Even if we failed to get the matching xpcom object, we don't abort this - // function. Just call NS_ShutdownXPCOM with a null service manager. - } - - // Free globals before calling NS_ShutdownXPCOM(), since we need some - // XPCOM services. - FreeJavaGlobals(env); - - rv = NS_ShutdownXPCOM(servMgr); - if (NS_FAILED(rv)) - ThrowException(env, rv, "NS_ShutdownXPCOM failed"); -} - -extern "C" NS_EXPORT jobject -XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath, - jboolean aFollowLinks) -{ - // Create a Mozilla string from the jstring - const PRUnichar* buf = nsnull; - if (aPath) { - buf = env->GetStringChars(aPath, nsnull); - if (!buf) - return nsnull; // exception already thrown - } - - nsAutoString path_str(buf); - env->ReleaseStringChars(aPath, buf); - - // Make call to given function - nsCOMPtr file; - nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, getter_AddRefs(file)); - - if (NS_SUCCEEDED(rv)) { - jobject javaProxy; - rv = GetNewOrUsedJavaObject(env, file, NS_GET_IID(nsILocalFile), - &javaProxy); - if (NS_SUCCEEDED(rv)) - return javaProxy; - } - - ThrowException(env, rv, "Failure in newLocalFile"); - return nsnull; -} - -extern "C" NS_EXPORT jobject -XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject) -{ - // Call XPCOM method - nsCOMPtr cm; - nsresult rv = NS_GetComponentManager(getter_AddRefs(cm)); - - if (NS_SUCCEEDED(rv)) { - jobject javaProxy; - rv = GetNewOrUsedJavaObject(env, cm, NS_GET_IID(nsIComponentManager), - &javaProxy); - if (NS_SUCCEEDED(rv)) - return javaProxy; - } - - ThrowException(env, rv, "Failure in getComponentManager"); - return nsnull; -} - -extern "C" NS_EXPORT jobject -XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject) -{ - // Call XPCOM method - nsCOMPtr cr; - nsresult rv = NS_GetComponentRegistrar(getter_AddRefs(cr)); - - if (NS_SUCCEEDED(rv)) { - jobject javaProxy; - rv = GetNewOrUsedJavaObject(env, cr, NS_GET_IID(nsIComponentRegistrar), - &javaProxy); - if (NS_SUCCEEDED(rv)) - return javaProxy; - } - - ThrowException(env, rv, "Failure in getComponentRegistrar"); - return nsnull; -} - -extern "C" NS_EXPORT jobject -XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject) -{ - // Call XPCOM method - nsCOMPtr sm; - nsresult rv = NS_GetServiceManager(getter_AddRefs(sm)); - - if (NS_SUCCEEDED(rv)) { - jobject javaProxy; - rv = GetNewOrUsedJavaObject(env, sm, NS_GET_IID(nsIServiceManager), - &javaProxy); - if (NS_SUCCEEDED(rv)) - return javaProxy; - } - - ThrowException(env, rv, "Failure in getServiceManager"); - return nsnull; -} - -nsresult -LockProfileDirectory_Impl(JNIEnv* env, jobject aDirectory, - jobject* aJavaLock) -{ - - nsresult rv; - - nsCOMPtr profDir; - if (!aDirectory) return NS_ERROR_NULL_POINTER; - - rv = File_to_nsILocalFile(env, aDirectory, getter_AddRefs(profDir)); - NS_ENSURE_SUCCESS(rv, rv); - - nsISupports* lockObj; - rv = XRE_LockProfileDirectory(profDir, &lockObj); - NS_ENSURE_SUCCESS(rv, rv); - - rv = GetNewOrUsedJavaObject(env, lockObj, NS_GET_IID(nsISupports), - aJavaLock); - NS_IF_RELEASE(lockObj); - return rv; -} - -extern "C" NS_EXPORT jobject -GRE_NATIVE(lockProfileDirectory) (JNIEnv* env, jobject, jobject aDirectory) -{ - - jobject profLock; - nsresult rv = LockProfileDirectory_Impl(env, aDirectory, &profLock); - if (NS_SUCCEEDED(rv)) { - return profLock; - } - - ThrowException(env, rv, "Failure in lockProfileDirectory"); - return nsnull; -} - -extern "C" NS_EXPORT void -GRE_NATIVE(notifyProfile) (JNIEnv *env, jobject) -{ - XRE_NotifyProfile(); -} - diff --git a/mozilla/extensions/java/xpcom/nsJavaWrapper.cpp b/mozilla/extensions/java/xpcom/nsJavaWrapper.cpp deleted file mode 100644 index 8305dc9e045..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaWrapper.cpp +++ /dev/null @@ -1,1782 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "nsJavaInterfaces.h" -#include "nsJavaWrapper.h" -#include "nsJavaXPTCStub.h" -#include "nsJavaXPCOMBindingUtils.h" -#include "jni.h" -#include "xptcall.h" -#include "nsIInterfaceInfoManager.h" -#include "nsString.h" -#include "nsCRT.h" -#include "prmem.h" -#include "nsServiceManagerUtils.h" - -static nsID nullID = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; - - -nsresult -CreateJavaArray(JNIEnv* env, PRUint8 aType, PRUint32 aSize, const nsID& aIID, - jobject* aResult) -{ - jobject array = nsnull; - switch (aType) - { - case nsXPTType::T_I8: - array = env->NewByteArray(aSize); - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U8: - array = env->NewShortArray(aSize); - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U16: - array = env->NewIntArray(aSize); - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U32: - array = env->NewLongArray(aSize); - break; - - case nsXPTType::T_FLOAT: - array = env->NewFloatArray(aSize); - break; - - // XXX how do we handle unsigned 64-bit values? - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - array = env->NewDoubleArray(aSize); - break; - - case nsXPTType::T_BOOL: - array = env->NewBooleanArray(aSize); - break; - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - array = env->NewCharArray(aSize); - break; - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - case nsXPTType::T_IID: - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - array = env->NewObjectArray(aSize, stringClass, nsnull); - break; - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - { - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID)); - NS_ASSERTION(iim, "Failed to get InterfaceInfoManager"); - if (!iim) - return NS_ERROR_FAILURE; - - // Get interface info for given IID - nsCOMPtr info; - nsresult rv = iim->GetInfoForIID(&aIID, getter_AddRefs(info)); - if (NS_FAILED(rv)) - return rv; - - // Get interface name - const char* iface_name; - rv = info->GetNameShared(&iface_name); - if (NS_FAILED(rv)) - return rv; - - // Create proper Java interface name - nsCAutoString class_name("org/mozilla/xpcom/"); - class_name.AppendASCII(iface_name); - jclass ifaceClass = env->FindClass(class_name.get()); - if (!ifaceClass) - return NS_ERROR_FAILURE; - - array = env->NewObjectArray(aSize, ifaceClass, nsnull); - break; - } - - case nsXPTType::T_VOID: - array = env->NewLongArray(aSize); - break; - - default: - NS_WARNING("unknown type"); - return NS_ERROR_FAILURE; - } - - if (!array) - return NS_ERROR_OUT_OF_MEMORY; - - *aResult = array; - return NS_OK; -} - -nsresult -GetNativeArrayElement(PRUint8 aType, void* aArray, PRUint32 aIndex, - nsXPTCVariant* aResult) -{ - switch (aType) - { - case nsXPTType::T_I8: - case nsXPTType::T_U8: - aResult->val.u8 = NS_STATIC_CAST(PRUint8*, aArray)[aIndex]; - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U16: - aResult->val.u16 = NS_STATIC_CAST(PRUint16*, aArray)[aIndex]; - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U32: - aResult->val.u32 = NS_STATIC_CAST(PRUint32*, aArray)[aIndex]; - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U64: - aResult->val.u64 = NS_STATIC_CAST(PRUint64*, aArray)[aIndex]; - break; - - case nsXPTType::T_FLOAT: - aResult->val.f = NS_STATIC_CAST(float*, aArray)[aIndex]; - break; - - case nsXPTType::T_DOUBLE: - aResult->val.d = NS_STATIC_CAST(double*, aArray)[aIndex]; - break; - - case nsXPTType::T_BOOL: - aResult->val.b = NS_STATIC_CAST(PRBool*, aArray)[aIndex]; - break; - - case nsXPTType::T_CHAR: - aResult->val.c = NS_STATIC_CAST(char*, aArray)[aIndex]; - break; - - case nsXPTType::T_WCHAR: - aResult->val.wc = NS_STATIC_CAST(PRUnichar*, aArray)[aIndex]; - break; - - case nsXPTType::T_CHAR_STR: - aResult->val.p = NS_STATIC_CAST(char**, aArray)[aIndex]; - break; - - case nsXPTType::T_WCHAR_STR: - aResult->val.p = NS_STATIC_CAST(PRUnichar**, aArray)[aIndex]; - break; - - case nsXPTType::T_IID: - aResult->val.p = NS_STATIC_CAST(nsID**, aArray)[aIndex]; - break; - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - aResult->val.p = NS_STATIC_CAST(nsISupports**, aArray)[aIndex]; - break; - - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - aResult->val.p = NS_STATIC_CAST(nsString**, aArray)[aIndex]; - break; - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - aResult->val.p = NS_STATIC_CAST(nsCString**, aArray)[aIndex]; - break; - - case nsXPTType::T_VOID: - aResult->val.p = NS_STATIC_CAST(void**, aArray)[aIndex]; - break; - - default: - NS_WARNING("unknown type"); - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - -nsresult -CreateNativeArray(PRUint8 aType, PRUint32 aSize, void** aResult) -{ - void* array = nsnull; - switch (aType) - { - case nsXPTType::T_I8: - case nsXPTType::T_U8: - array = PR_Malloc(aSize * sizeof(PRUint8)); - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U16: - array = PR_Malloc(aSize * sizeof(PRUint16)); - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U32: - array = PR_Malloc(aSize * sizeof(PRUint32)); - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U64: - array = PR_Malloc(aSize * sizeof(PRUint64)); - break; - - case nsXPTType::T_FLOAT: - array = PR_Malloc(aSize * sizeof(float)); - break; - - case nsXPTType::T_DOUBLE: - array = PR_Malloc(aSize * sizeof(double)); - break; - - case nsXPTType::T_BOOL: - array = PR_Malloc(aSize * sizeof(PRBool)); - break; - - case nsXPTType::T_CHAR: - array = PR_Malloc(aSize * sizeof(char)); - break; - - case nsXPTType::T_WCHAR: - array = PR_Malloc(aSize * sizeof(PRUnichar)); - break; - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - case nsXPTType::T_IID: - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - array = PR_Malloc(aSize * sizeof(void*)); - break; - - case nsXPTType::T_VOID: - array = PR_Malloc(aSize * sizeof(void*)); - break; - - default: - NS_WARNING("unknown type"); - return NS_ERROR_FAILURE; - } - - if (!array) - return NS_ERROR_OUT_OF_MEMORY; - - *aResult = array; - return NS_OK; -} - -/** - * Handle 'in' and 'inout' params. - */ -nsresult -SetupParams(JNIEnv *env, const jobject aParam, PRUint8 aType, PRBool aIsOut, - const nsID& aIID, PRUint8 aArrayType, PRUint32 aArraySize, - PRBool aIsArrayElement, PRUint32 aIndex, nsXPTCVariant &aVariant) -{ - nsresult rv = NS_OK; - - switch (aType) - { - case nsXPTType::T_I8: - { - LOG(("byte\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.i8 = env->CallByteMethod(aParam, byteValueMID); - } else { // 'inout' & 'array' - jbyte value; - if (aParam) { - env->GetByteArrayRegion((jbyteArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.i8 = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(PRInt8*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_I16: - case nsXPTType::T_U8: // C++ unsigned octet <=> Java short - { - LOG(("short\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.i16 = env->CallShortMethod(aParam, shortValueMID); - } else { // 'inout' & 'array' - jshort value; - if (aParam) { - env->GetShortArrayRegion((jshortArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.i16 = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - if (aType == nsXPTType::T_I16) - NS_STATIC_CAST(PRInt16*, aVariant.val.p)[aIndex] = value; - else - NS_STATIC_CAST(PRUint8*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_I32: - case nsXPTType::T_U16: // C++ unsigned short <=> Java int - { - LOG(("int\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.i32 = env->CallIntMethod(aParam, intValueMID); - } else { // 'inout' & 'array' - jint value; - if (aParam) { - env->GetIntArrayRegion((jintArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.i32 = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - if (aType == nsXPTType::T_I32) - NS_STATIC_CAST(PRInt32*, aVariant.val.p)[aIndex] = value; - else - NS_STATIC_CAST(PRUint16*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_I64: - case nsXPTType::T_U32: // C++ unsigned int <=> Java long - { - LOG(("long\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.i64 = env->CallLongMethod(aParam, longValueMID); - } else { // 'inout' & 'array' - jlong value; - if (aParam) { - env->GetLongArrayRegion((jlongArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.i64 = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - if (aType == nsXPTType::T_I64) - NS_STATIC_CAST(PRInt64*, aVariant.val.p)[aIndex] = value; - else - NS_STATIC_CAST(PRUint32*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_FLOAT: - { - LOG(("float\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.f = env->CallFloatMethod(aParam, floatValueMID); - } else { // 'inout' & 'array' - jfloat value; - if (aParam) { - env->GetFloatArrayRegion((jfloatArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.f = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(float*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - // XXX how do we handle unsigned 64-bit value? - case nsXPTType::T_U64: // C++ unsigned long <=> Java double - case nsXPTType::T_DOUBLE: - { - LOG(("double\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - jdouble value = env->CallDoubleMethod(aParam, doubleValueMID); - if (aType == nsXPTType::T_DOUBLE) - aVariant.val.d = value; - else - aVariant.val.u64 = NS_STATIC_CAST(PRUint64, value); - } else { // 'inout' & 'array' - jdouble value; - if (aParam) { - env->GetDoubleArrayRegion((jdoubleArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - if (aType == nsXPTType::T_DOUBLE) - aVariant.val.d = value; - else - aVariant.val.u64 = NS_STATIC_CAST(PRUint64, value); - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - if (aType == nsXPTType::T_DOUBLE) - NS_STATIC_CAST(double*, aVariant.val.p)[aIndex] = value; - else - NS_STATIC_CAST(PRUint64*, aVariant.val.p)[aIndex] = - NS_STATIC_CAST(PRUint64, value); - } - } - break; - } - - case nsXPTType::T_BOOL: - { - LOG(("boolean\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.b = env->CallBooleanMethod(aParam, booleanValueMID); - } else { // 'inout' & 'array' - jboolean value; - if (aParam) { - env->GetBooleanArrayRegion((jbooleanArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.b = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(PRBool*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_CHAR: - { - LOG(("char\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.c = env->CallCharMethod(aParam, charValueMID); - } else { // 'inout' & 'array' - jchar value; - if (aParam) { - env->GetCharArrayRegion((jcharArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.c = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(char*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_WCHAR: - { - LOG(("char\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.wc = env->CallCharMethod(aParam, charValueMID); - } else { // 'inout' & 'array' - jchar value; - if (aParam) { - env->GetCharArrayRegion((jcharArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.wc = value; - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(PRUnichar*, aVariant.val.p)[aIndex] = value; - } - } - break; - } - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - { - LOG(("String\n")); - jstring data = nsnull; - if (!aIsOut && !aIsArrayElement) { // 'in' - data = (jstring) aParam; - } else if (aParam) { // 'inout' & 'array' - data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam, - aIndex); - } - - void* buf = nsnull; - if (data) { - jsize uniLength = env->GetStringLength(data); - if (uniLength > 0) { - if (aType == nsXPTType::T_CHAR_STR) { - jsize utf8Length = env->GetStringUTFLength(data); - buf = nsMemory::Alloc((utf8Length + 1) * sizeof(char)); - if (!buf) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - char* char_str = NS_STATIC_CAST(char*, buf); - env->GetStringUTFRegion(data, 0, uniLength, char_str); - char_str[utf8Length] = '\0'; - - } else { // if T_WCHAR_STR - buf = nsMemory::Alloc((uniLength + 1) * sizeof(jchar)); - if (!buf) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - jchar* jchar_str = NS_STATIC_CAST(jchar*, buf); - env->GetStringRegion(data, 0, uniLength, jchar_str); - jchar_str[uniLength] = '\0'; - } - } else { - // create empty string - buf = nsMemory::Alloc(2); - if (!buf) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - ((jchar*)buf)[0] = '\0'; - } - } - - if (!aIsArrayElement) { // 'in' & 'inout' - aVariant.val.p = buf; - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val; - aVariant.SetPtrIsData(); - } - } else { // 'array' - if (aType == nsXPTType::T_CHAR_STR) { - char* str = NS_STATIC_CAST(char*, buf); - NS_STATIC_CAST(char**, aVariant.val.p)[aIndex] = str; - } else { - PRUnichar* str = NS_STATIC_CAST(PRUnichar*, buf); - NS_STATIC_CAST(PRUnichar**, aVariant.val.p)[aIndex] = str; - } - } - break; - } - - case nsXPTType::T_IID: - { - LOG(("String(IID)\n")); - jstring data = nsnull; - if (!aIsOut && !aIsArrayElement) { // 'in' - data = (jstring) aParam; - } else if (aParam) { // 'inout' & 'array' - data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam, - aIndex); - } - - nsID* iid = new nsID; - if (!iid) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - if (data) { - // extract IID string from Java string - const char* str = env->GetStringUTFChars(data, nsnull); - if (!str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - // parse string into IID object - iid->Parse(str); - env->ReleaseStringUTFChars(data, str); - } else { - *iid = nullID; - } - - if (!aIsArrayElement) { // 'in' & 'inout' - aVariant.val.p = iid; - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val; - aVariant.SetPtrIsData(); - } - } else { // 'array' - NS_STATIC_CAST(nsID**, aVariant.val.p)[aIndex] = iid; - } - break; - } - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - { - LOG(("nsISupports\n")); - jobject java_obj = nsnull; - if (!aIsOut && !aIsArrayElement) { // 'in' - java_obj = (jobject) aParam; - } else if (aParam) { // 'inout' & 'array' - java_obj = (jobject) env->GetObjectArrayElement((jobjectArray) aParam, - aIndex); - } - - nsISupports* xpcom_obj; - if (java_obj) { - // If the requested interface is nsIWeakReference, then we look for or - // create a stub for the nsISupports interface. Then we create a weak - // reference from that stub. - PRBool isWeakRef; - nsID iid; - if (aIID.Equals(NS_GET_IID(nsIWeakReference))) { - isWeakRef = PR_TRUE; - iid = NS_GET_IID(nsISupports); - } else { - isWeakRef = PR_FALSE; - iid = aIID; - } - - rv = GetNewOrUsedXPCOMObject(env, java_obj, iid, &xpcom_obj); - if (NS_FAILED(rv)) - break; - - // If the function expects a weak reference, then we need to - // create it here. - if (isWeakRef) { - nsCOMPtr supportsweak = - do_QueryInterface(xpcom_obj); - if (supportsweak) { - nsWeakPtr weakref; - supportsweak->GetWeakReference(getter_AddRefs(weakref)); - NS_RELEASE(xpcom_obj); - xpcom_obj = weakref; - NS_ADDREF(xpcom_obj); - } else { - xpcom_obj = nsnull; - } - } - } else { - xpcom_obj = nsnull; - } - - if (!aIsArrayElement) { // 'in' & 'inout' - aVariant.val.p = xpcom_obj; - aVariant.SetValIsInterface(); - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val; - aVariant.SetPtrIsData(); - } - } else { // 'array' - NS_STATIC_CAST(nsISupports**, aVariant.val.p)[aIndex] = xpcom_obj; - } - break; - } - - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - { - LOG(("String\n")); - jstring data = nsnull; - if (!aIsOut && !aIsArrayElement) { // 'in' - data = (jstring) aParam; - } else if (aParam) { // 'inout' & 'array' - data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam, - aIndex); - } - - nsAString* str; - if (data) { - str = jstring_to_nsAString(env, data); - if (!str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - str = nsnull; - } - - if (!aIsArrayElement) { // 'in' & 'inout' - aVariant.val.p = str; - aVariant.SetValIsDOMString(); - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val; - aVariant.SetPtrIsData(); - } - } else { // 'array' - NS_STATIC_CAST(nsAString**, aVariant.val.p)[aIndex] = str; - } - break; - } - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - { - LOG(("StringUTF\n")); - jstring data = nsnull; - if (!aIsOut && !aIsArrayElement) { // 'in' - data = (jstring) aParam; - } else if (aParam) { // 'inout' & 'array' - data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam, - aIndex); - } - - nsACString* str; - if (data) { - str = jstring_to_nsACString(env, data); - if (!str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - str = nsnull; - } - - if (!aIsArrayElement) { // 'in' & 'inout' - aVariant.val.p = str; - if (aType == nsXPTType::T_CSTRING) { - aVariant.SetValIsCString(); - } else { - aVariant.SetValIsUTF8String(); - } - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val; - aVariant.SetPtrIsData(); - } - } else { // 'array' - NS_STATIC_CAST(nsACString**, aVariant.val.p)[aIndex] = str; - } - break; - } - - // handle "void *" as an "long" in Java - case nsXPTType::T_VOID: - { - LOG(("long (void*)\n")); - if (!aIsOut && !aIsArrayElement) { // 'in' - aVariant.val.p = - NS_REINTERPRET_CAST(void*, env->CallLongMethod(aParam, longValueMID)); - } else { // 'inout' & 'array' - jlong value; - if (aParam) { - env->GetLongArrayRegion((jlongArray) aParam, aIndex, 1, &value); - } - - if (aIsOut) { // 'inout' - if (aParam) { - aVariant.val.p = NS_REINTERPRET_CAST(void*, value); - aVariant.ptr = &aVariant.val; - } else { - aVariant.ptr = nsnull; - } - aVariant.SetPtrIsData(); - } else { // 'array' - NS_STATIC_CAST(void**, aVariant.val.p)[aIndex] = - NS_REINTERPRET_CAST(void*, value); - } - } - break; - } - - case nsXPTType::T_ARRAY: - { - jobject sourceArray = nsnull; - if (!aIsOut) { // 'in' - sourceArray = aParam; - } else if (aParam) { // 'inout' - jobjectArray array = NS_STATIC_CAST(jobjectArray, aParam); - sourceArray = env->GetObjectArrayElement(array, 0); - } - - if (sourceArray) { - rv = CreateNativeArray(aArrayType, aArraySize, &aVariant.val.p); - - for (PRUint32 i = 0; i < aArraySize && NS_SUCCEEDED(rv); i++) { - rv = SetupParams(env, sourceArray, aArrayType, PR_FALSE, aIID, 0, 0, - PR_TRUE, i, aVariant); - } - } - - if (aIsOut) { // 'inout' - aVariant.ptr = &aVariant.val.p; - aVariant.SetPtrIsData(); - } - break; - } - - default: - NS_WARNING("unexpected parameter type"); - return NS_ERROR_UNEXPECTED; - } - - return rv; -} - -/** - * Does any cleanup from objects created in SetupParams, as well as converting - * any out params to Java. - * - * NOTE: If aInvokeResult is an error condition, then we just do cleanup in - * this function. - */ -nsresult -FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType, - nsXPTCVariant &aVariant, const nsID& aIID, - PRBool aIsArrayElement, PRUint8 aArrayType, PRUint32 aArraySize, - PRUint32 aIndex, nsresult aInvokeResult, jobject* aParam) -{ - nsresult rv = NS_OK; - - switch (aType) - { - case nsXPTType::T_I8: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jbyte value = aVariant.val.i8; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(byteClass, byteInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetByteArrayRegion((jbyteArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_I16: - case nsXPTType::T_U8: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jshort value = (aType == nsXPTType::T_I16) ? aVariant.val.i16 : - aVariant.val.u8; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(shortClass, shortInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && aParam) { - env->SetShortArrayRegion((jshortArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_I32: - case nsXPTType::T_U16: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jint value = (aType == nsXPTType::T_I32) ? aVariant.val.i32 : - aVariant.val.u16; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(intClass, intInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetIntArrayRegion((jintArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_I64: - case nsXPTType::T_U32: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jlong value = (aType == nsXPTType::T_I64) ? aVariant.val.i64 : - aVariant.val.u32; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(longClass, longInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetLongArrayRegion((jlongArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_FLOAT: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jfloat value = aVariant.val.f; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(floatClass, floatInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetFloatArrayRegion((jfloatArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - // XXX how do we handle unsigned 64-bit values? - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jdouble value = (aType == nsXPTType::T_DOUBLE) ? aVariant.val.d : - aVariant.val.u64; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(doubleClass, doubleInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetDoubleArrayRegion((jdoubleArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_BOOL: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jboolean value = aVariant.val.b; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(booleanClass, booleanInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetBooleanArrayRegion((jbooleanArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jchar value; - if (aType == nsXPTType::T_CHAR) - value = aVariant.val.c; - else - value = aVariant.val.wc; - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(charClass, charInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetCharArrayRegion((jcharArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - { - if ((aParamInfo.IsOut() || aIsArrayElement) && - NS_SUCCEEDED(aInvokeResult)) - { - // create new string from data - jstring str = nsnull; - if (aVariant.val.p) { - if (aType == nsXPTType::T_CHAR_STR) { - str = env->NewStringUTF((const char*) aVariant.val.p); - } else { - PRUint32 length = nsCRT::strlen((const PRUnichar*) aVariant.val.p); - str = env->NewString((const jchar*) aVariant.val.p, length); - } - if (!str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } - - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = str; - } else if (*aParam) { - // put new string into output array - env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, str); - } - } - - // cleanup - if (aVariant.val.p) - nsMemory::Free(aVariant.val.p); - break; - } - - case nsXPTType::T_IID: - { - nsID* iid = NS_STATIC_CAST(nsID*, aVariant.val.p); - - if ((aParamInfo.IsOut() || aIsArrayElement) && - NS_SUCCEEDED(aInvokeResult)) - { - // Create the string from nsID - jstring str = nsnull; - if (iid) { - char* iid_str = iid->ToString(); - if (iid_str) { - str = env->NewStringUTF(iid_str); - } - if (!iid_str || !str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - PR_Free(iid_str); - } - - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = str; - } else if (*aParam) { - // put new string into output array - env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, str); - } - } - - // Ordinarily, we would delete 'iid' here. But we cannot do that until - // we've handled all of the params. See comment in CallXPCOMMethod. - // We can safely delete array elements, though. - if (aIsArrayElement) - delete iid; - - break; - } - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - { - nsISupports* xpcom_obj = NS_STATIC_CAST(nsISupports*, aVariant.val.p); - - if ((aParamInfo.IsOut() || aIsArrayElement) && - NS_SUCCEEDED(aInvokeResult)) - { - jobject java_obj = nsnull; - if (xpcom_obj) { - // Get matching Java object for given xpcom object - rv = GetNewOrUsedJavaObject(env, xpcom_obj, aIID, &java_obj); - if (NS_FAILED(rv)) - break; - } - - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = java_obj; - } else if (*aParam) { - // put new Java object into output array - env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, java_obj); - } - } - - // cleanup - NS_IF_RELEASE(xpcom_obj); - break; - } - - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - { - nsString* str = NS_STATIC_CAST(nsString*, aVariant.val.p); - - if ((aParamInfo.IsOut() || aParamInfo.IsDipper() || aIsArrayElement) && - NS_SUCCEEDED(aInvokeResult)) - { - // Create Java string from returned nsString - jstring jstr = nsnull; - if (str) { - jstr = env->NewString((const jchar*) str->get(), str->Length()); - if (!jstr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } - - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = jstr; - } else if (*aParam) { - // put new Java string into output array - env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, jstr); - } - } - - // cleanup - if (str) { - delete str; - } - break; - } - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - { - nsCString* str = NS_STATIC_CAST(nsCString*, aVariant.val.p); - - if ((aParamInfo.IsOut() || aParamInfo.IsDipper() || aIsArrayElement) && - NS_SUCCEEDED(aInvokeResult)) - { - // Create Java string from returned nsString - jstring jstr = nsnull; - if (str) { - jstr = env->NewStringUTF((const char*) str->get()); - if (!jstr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } - - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = jstr; - } else if (*aParam) { - // put new Java string into output array - env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, jstr); - } - } - - // cleanup - if (str) { - delete str; - } - break; - } - - case nsXPTType::T_VOID: - { - if (NS_SUCCEEDED(aInvokeResult)) { - jlong value = NS_REINTERPRET_CAST(jlong, aVariant.val.p); - if (aParamInfo.IsRetval() && !aIsArrayElement) { - *aParam = env->NewObject(longClass, longInitMID, value); - } else if ((aParamInfo.IsOut() || aIsArrayElement) && *aParam) { - env->SetLongArrayRegion((jlongArray) *aParam, aIndex, 1, &value); - } - } - break; - } - - case nsXPTType::T_ARRAY: - { - if (aParamInfo.IsOut() && NS_SUCCEEDED(aInvokeResult)) { - // Create Java array from returned native array - jobject jarray = nsnull; - if (aVariant.val.p) { - rv = CreateJavaArray(env, aArrayType, aArraySize, aIID, &jarray); - if (NS_FAILED(rv)) - break; - - nsXPTCVariant var; - for (PRUint32 i = 0; i < aArraySize && NS_SUCCEEDED(rv); i++) { - rv = GetNativeArrayElement(aArrayType, aVariant.val.p, i, &var); - if (NS_SUCCEEDED(rv)) { - rv = FinalizeParams(env, aParamInfo, aArrayType, var, aIID, - PR_TRUE, 0, 0, i, aInvokeResult, &jarray); - } - } - } - - if (aParamInfo.IsRetval()) { - *aParam = jarray; - } else if (*aParam) { - // put new Java array into output array - env->SetObjectArrayElement((jobjectArray) *aParam, 0, jarray); - } - } - - // cleanup - // If this is not an out param or if the invokeResult is a failure case, - // then the array elements have not been cleaned up. Do so now. - if (!aParamInfo.IsOut() || NS_FAILED(aInvokeResult)) { - nsXPTCVariant var; - for (PRUint32 i = 0; i < aArraySize; i++) { - rv = GetNativeArrayElement(aArrayType, aVariant.val.p, i, &var); - if (NS_SUCCEEDED(rv)) { - FinalizeParams(env, aParamInfo, aArrayType, var, aIID, PR_TRUE, - 0, 0, i, NS_ERROR_FAILURE, nsnull); - } - } - } - PR_Free(aVariant.val.p); - break; - } - - default: - NS_WARNING("unexpected parameter type"); - return NS_ERROR_UNEXPECTED; - } - - // Check for Java exception, but don't overwrite pre-existing error code. - if (NS_SUCCEEDED(rv) && env->ExceptionCheck()) - rv = NS_ERROR_FAILURE; - - return rv; -} - -nsresult -QueryAttributeInfo(nsIInterfaceInfo* aIInfo, const char* aMethodName, - PRBool aCapitalizedAttr, PRUint16* aMethodIndex, - const nsXPTMethodInfo** aMethodInfo) - -{ - nsresult rv = NS_ERROR_FAILURE; - - // An 'attribute' will start with either "get" or "set". But first, - // we check the length, in order to skip over method names that match exactly - // "get" or "set". - if (strlen(aMethodName) > 3) { - if (strncmp("get", aMethodName, 3) == 0) { - char* getterName = strdup(aMethodName + 3); - if (!aCapitalizedAttr) { - getterName[0] = tolower(getterName[0]); - } - rv = aIInfo->GetMethodInfoForName(getterName, aMethodIndex, aMethodInfo); - free(getterName); - } else if (strncmp("set", aMethodName, 3) == 0) { - char* setterName = strdup(aMethodName + 3); - if (!aCapitalizedAttr) { - setterName[0] = tolower(setterName[0]); - } - rv = aIInfo->GetMethodInfoForName(setterName, aMethodIndex, aMethodInfo); - if (NS_SUCCEEDED(rv)) { - // If this succeeded, GetMethodInfoForName will have returned the - // method info for the 'getter'. We want the 'setter', so increase - // method index by one ('setter' immediately follows the 'getter'), - // and get its method info. - (*aMethodIndex)++; - rv = aIInfo->GetMethodInfo(*aMethodIndex, aMethodInfo); - if (NS_SUCCEEDED(rv)) { - // Double check that this methodInfo matches the given method. - if (!(*aMethodInfo)->IsSetter() || - strcmp(setterName, (*aMethodInfo)->name) != 0) { - rv = NS_ERROR_FAILURE; - } - } - } - free(setterName); - } - } - - return rv; -} - -/** - * Given an interface info struct and a method name, returns the method info - * and index, if that method exists. - * - * Most method names are lower case. Unfortunately, the method names of some - * interfaces (such as nsIAppShell) start with a capital letter. This function - * will try all of the permutations. - */ -nsresult -QueryMethodInfo(nsIInterfaceInfo* aIInfo, const char* aMethodName, - PRUint16* aMethodIndex, const nsXPTMethodInfo** aMethodInfo) -{ - // Skip over any leading underscores, since these are methods that conflicted - // with existing Java keywords - const char* methodName = aMethodName; - if (methodName[0] == '_') { - methodName++; - } - - // The common case is that the method name is lower case, so we check - // that first. - nsresult rv; - rv = aIInfo->GetMethodInfoForName(methodName, aMethodIndex, aMethodInfo); - if (NS_SUCCEEDED(rv)) - return rv; - - // If there is no method called , then maybe it is an - // 'attribute'. - rv = QueryAttributeInfo(aIInfo, methodName, PR_FALSE, aMethodIndex, - aMethodInfo); - if (NS_SUCCEEDED(rv)) - return rv; - - // If we get here, then maybe the method name is capitalized. - char* name = strdup(methodName); - name[0] = toupper(name[0]); - rv = aIInfo->GetMethodInfoForName(name, aMethodIndex, aMethodInfo); - free(name); - if (NS_SUCCEEDED(rv)) - return rv; - - // If there is no method called , then maybe it is an - // 'attribute'. - rv = QueryAttributeInfo(aIInfo, methodName, PR_TRUE, aMethodIndex, - aMethodInfo); - - return rv; -} - -/** - * org.mozilla.xpcom.XPCOMJavaProxy.internal.callXPCOMMethod - */ -extern "C" NS_EXPORT jobject -JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy, - jstring aMethodName, jobjectArray aParams) -{ - nsresult rv; - - // Get native XPCOM instance - void* xpcom_obj; - rv = GetXPCOMInstFromProxy(env, aJavaProxy, &xpcom_obj); - if (NS_FAILED(rv)) { - ThrowException(env, 0, "Failed to get matching XPCOM object"); - return nsnull; - } - JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj); - - // Get method info - PRUint16 methodIndex; - const nsXPTMethodInfo* methodInfo; - nsIInterfaceInfo* iinfo = inst->InterfaceInfo(); - const char* methodName = env->GetStringUTFChars(aMethodName, nsnull); - rv = QueryMethodInfo(iinfo, methodName, &methodIndex, &methodInfo); - env->ReleaseStringUTFChars(aMethodName, methodName); - - if (NS_FAILED(rv)) { - ThrowException(env, rv, "GetMethodInfoForName failed"); - return nsnull; - } - -#ifdef DEBUG_JAVAXPCOM - const char* ifaceName; - iinfo->GetNameShared(&ifaceName); - LOG(("===> (XPCOM) %s::%s()\n", ifaceName, methodInfo->GetName())); -#endif - - // Convert the Java params - PRUint8 paramCount = methodInfo->GetParamCount(); - nsXPTCVariant* params = nsnull; - if (paramCount) - { - params = new nsXPTCVariant[paramCount]; - if (!params) { - ThrowException(env, NS_ERROR_OUT_OF_MEMORY, "Can't create params array"); - return nsnull; - } - memset(params, 0, paramCount * sizeof(nsXPTCVariant)); - - for (PRUint8 i = 0; i < paramCount && NS_SUCCEEDED(rv); i++) - { - LOG(("\t Param %d: ", i)); - const nsXPTParamInfo ¶mInfo = methodInfo->GetParam(i); - params[i].type = paramInfo.GetType(); - - if (paramInfo.IsIn() && !paramInfo.IsDipper()) { - PRUint8 type = params[i].type.TagPart(); - - // is paramater an array? - PRUint8 arrayType = 0; - PRUint32 arraySize = 0; - if (type == nsXPTType::T_ARRAY) { - // get array type - nsXPTType xpttype; - rv = iinfo->GetTypeForParam(methodIndex, ¶mInfo, 1, &xpttype); - if (NS_FAILED(rv)) - break; - arrayType = xpttype.TagPart(); - - // get size of array - PRUint8 argnum; - rv = iinfo->GetSizeIsArgNumberForParam(methodIndex, ¶mInfo, 0, - &argnum); - if (NS_FAILED(rv)) - break; - arraySize = params[argnum].val.u32; - } - - // get IID for interface params - nsID iid; - if (type == nsXPTType::T_INTERFACE || - type == nsXPTType::T_INTERFACE_IS || - type == nsXPTType::T_ARRAY && - (arrayType == nsXPTType::T_INTERFACE || - arrayType == nsXPTType::T_INTERFACE_IS)) - { - PRUint8 paramType = type == nsXPTType::T_ARRAY ? arrayType : type; - rv = GetIIDForMethodParam(iinfo, methodInfo, paramInfo, paramType, - methodIndex, params, PR_TRUE, iid); - } - - if (NS_SUCCEEDED(rv)) { - rv = SetupParams(env, env->GetObjectArrayElement(aParams, i), type, - paramInfo.IsOut(), iid, arrayType, arraySize, - PR_FALSE, 0, params[i]); - } - } else if (paramInfo.IsDipper()) { - LOG(("dipper\n")); - switch (params[i].type.TagPart()) - { - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - { - params[i].val.p = new nsString(); - if (params[i].val.p == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - params[i].SetValIsDOMString(); - break; - } - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - { - params[i].val.p = new nsCString(); - if (params[i].val.p == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - params[i].SetValIsCString(); - break; - } - - default: - LOG(("unhandled dipper type\n")); - rv = NS_ERROR_UNEXPECTED; - } - } else { - LOG(("out/retval\n")); - params[i].ptr = &(params[i].val); - params[i].SetPtrIsData(); - } - } - if (NS_FAILED(rv)) { - ThrowException(env, rv, "SetupParams failed"); - return nsnull; - } - } - - // Call the XPCOM method - const nsIID* iid; - iinfo->GetIIDShared(&iid); - nsISupports* realObject; - rv = inst->GetInstance()->QueryInterface(*iid, (void**) &realObject); - if (NS_FAILED(rv)) { - ThrowException(env, rv, "Failed to get real XPCOM object"); - return nsnull; - } - nsresult invokeResult = XPTC_InvokeByIndex(realObject, methodIndex, - paramCount, params); - NS_RELEASE(realObject); - - // Clean up params - jobject result = nsnull; - for (PRUint8 i = 0; i < paramCount && NS_SUCCEEDED(rv); i++) - { - const nsXPTParamInfo ¶mInfo = methodInfo->GetParam(i); - PRUint8 type = paramInfo.GetType().TagPart(); - - // is paramater an array? - PRUint8 arrayType = 0; - PRUint32 arraySize = 0; - if (type == nsXPTType::T_ARRAY) { - // get array type - nsXPTType array_xpttype; - rv = iinfo->GetTypeForParam(methodIndex, ¶mInfo, 1, &array_xpttype); - if (NS_FAILED(rv)) - break; - arrayType = array_xpttype.TagPart(); - - // get size of array - PRUint8 argnum; - rv = iinfo->GetSizeIsArgNumberForParam(methodIndex, ¶mInfo, 0, - &argnum); - if (NS_FAILED(rv)) - break; - arraySize = params[argnum].val.u32; - } - - // get IID for interface params - nsID iid; - if (type == nsXPTType::T_INTERFACE || type == nsXPTType::T_INTERFACE_IS || - type == nsXPTType::T_ARRAY && (arrayType == nsXPTType::T_INTERFACE || - arrayType == nsXPTType::T_INTERFACE_IS)) - { - PRUint8 paramType = type == nsXPTType::T_ARRAY ? arrayType : type; - rv = GetIIDForMethodParam(iinfo, methodInfo, paramInfo, paramType, - methodIndex, params, PR_TRUE, iid); - if (NS_FAILED(rv)) - break; - } - - jobject* javaElement; - if (!paramInfo.IsRetval()) { - jobject element = env->GetObjectArrayElement(aParams, i); - javaElement = &element; - } else { - javaElement = &result; - } - rv = FinalizeParams(env, paramInfo, type, params[i], iid, PR_FALSE, - arrayType, arraySize, 0, invokeResult, javaElement); - } - if (NS_FAILED(rv)) { - ThrowException(env, rv, "FinalizeParams failed"); - return nsnull; - } - - // Normally, we would delete any created nsID object in the above loop. - // However, GetIIDForMethodParam may need some of the nsID params when it's - // looking for the IID of an INTERFACE_IS. Therefore, we can't delete it - // until we've gone through the 'Finalize' loop once and created the result. - for (PRUint8 j = 0; j < paramCount && NS_SUCCEEDED(rv); j++) - { - const nsXPTParamInfo ¶mInfo = methodInfo->GetParam(j); - const nsXPTType &type = paramInfo.GetType(); - if (type.TagPart() == nsXPTType::T_IID) { - nsID* iid = (nsID*) params[j].val.p; - delete iid; - } - } - - if (params) { - delete params; - } - - // If the XPCOM method invocation failed, we don't immediately throw an - // exception and return so that we can clean up any parameters. - if (NS_FAILED(invokeResult)) { - nsCAutoString message("The function \""); - message.AppendASCII(methodInfo->GetName()); - message.AppendLiteral("\" returned an error condition"); - ThrowException(env, invokeResult, message.get()); - } - - LOG(("<=== (XPCOM) %s::%s()\n", ifaceName, methodInfo->GetName())); - return result; -} - -nsresult -CreateJavaProxy(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID, - jobject* aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_NULL_POINTER; - - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID)); - NS_ASSERTION(iim, "Failed to get InterfaceInfoManager"); - if (!iim) - return NS_ERROR_FAILURE; - - // Get interface info for class - nsCOMPtr info; - nsresult rv = iim->GetInfoForIID(&aIID, getter_AddRefs(info)); - if (NS_FAILED(rv)) - return rv; - - // Wrap XPCOM object (addrefs aXPCOMObject) - JavaXPCOMInstance* inst = new JavaXPCOMInstance(aXPCOMObject, info); - if (!inst) - return NS_ERROR_OUT_OF_MEMORY; - - // Get interface name - const char* iface_name; - rv = info->GetNameShared(&iface_name); - - if (NS_SUCCEEDED(rv)) { - jobject java_obj = nsnull; - - // Create proper Java interface name - nsCAutoString class_name("org/mozilla/xpcom/"); - class_name.AppendASCII(iface_name); - jclass ifaceClass = env->FindClass(class_name.get()); - - if (ifaceClass) { - java_obj = env->CallStaticObjectMethod(xpcomJavaProxyClass, - createProxyMID, ifaceClass, - NS_REINTERPRET_CAST(jlong, inst)); - if (env->ExceptionCheck()) - java_obj = nsnull; - } - - if (java_obj) { -#ifdef DEBUG_JAVAXPCOM - char* iid_str = aIID.ToString(); - LOG(("+ CreateJavaProxy (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - java_obj), - (PRUint32) aXPCOMObject, iid_str)); - PR_Free(iid_str); -#endif - - // Associate XPCOM object with Java proxy - rv = gNativeToJavaProxyMap->Add(env, aXPCOMObject, aIID, java_obj); - if (NS_SUCCEEDED(rv)) { - *aResult = java_obj; - return NS_OK; - } - } else { - rv = NS_ERROR_FAILURE; - } - } - - // If there was an error, clean up. - delete inst; - return rv; -} - -nsresult -GetXPCOMInstFromProxy(JNIEnv* env, jobject aJavaObject, void** aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_NULL_POINTER; - - jlong xpcom_obj = env->CallStaticLongMethod(xpcomJavaProxyClass, - getNativeXPCOMInstMID, aJavaObject); - - if (!xpcom_obj || env->ExceptionCheck()) { - return NS_ERROR_FAILURE; - } - - *aResult = NS_REINTERPRET_CAST(void*, xpcom_obj); -#ifdef DEBUG_JAVAXPCOM - JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, *aResult); - nsIID* iid; - inst->InterfaceInfo()->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - LOG(("< GetXPCOMInstFromProxy (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - aJavaObject), - (PRUint32) inst->GetInstance(), iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif - return NS_OK; -} - -/** - * org.mozilla.xpcom.internal.XPCOMJavaProxy.finalizeProxy - */ -extern "C" NS_EXPORT void -JAVAPROXY_NATIVE(finalizeProxy) (JNIEnv *env, jclass that, jobject aJavaProxy) -{ -#ifdef DEBUG_JAVAXPCOM - PRUint32 xpcom_addr = 0; -#endif - - // Due to Java's garbage collection, this finalize statement may get called - // after FreeJavaGlobals(). So check to make sure that everything is still - // initialized. - if (gJavaXPCOMLock) { - nsAutoLock lock(gJavaXPCOMLock); - - // If may be possible for the lock to be acquired here when FreeGlobals is - // in the middle of running. If so, then this thread will sleep until - // FreeGlobals releases its lock. At that point, we resume this thread - // here, but JavaXPCOM may no longer be initialized. So we need to check - // that everything is legit after acquiring the lock. - if (gJavaXPCOMInitialized) { - // Get native XPCOM instance - void* xpcom_obj; - nsresult rv = GetXPCOMInstFromProxy(env, aJavaProxy, &xpcom_obj); - if (NS_SUCCEEDED(rv)) { - JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj); -#ifdef DEBUG_JAVAXPCOM - xpcom_addr = NS_REINTERPRET_CAST(PRUint32, inst->GetInstance()); -#endif - nsIID* iid; - rv = inst->InterfaceInfo()->GetInterfaceIID(&iid); - if (NS_SUCCEEDED(rv)) { - rv = gNativeToJavaProxyMap->Remove(env, inst->GetInstance(), *iid); - nsMemory::Free(iid); - } - NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to RemoveJavaProxy"); - // Release gJavaXPCOMLock before deleting inst (see bug 340022) - lock.unlock(); - delete inst; - } - } - } - -#ifdef DEBUG_JAVAXPCOM - LOG(("- Finalize (Java=%08x | XPCOM=%08x)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - aJavaProxy), - xpcom_addr)); -#endif -} - -/** - * org.mozilla.xpcom.XPCOMJavaProxy.isSameXPCOMObject - */ -extern "C" NS_EXPORT jboolean -JAVAPROXY_NATIVE(isSameXPCOMObject) (JNIEnv *env, jclass that, - jobject aProxy1, jobject aProxy2) -{ - void* xpcom_obj1; - nsresult rv = GetXPCOMInstFromProxy(env, aProxy1, &xpcom_obj1); - if (NS_SUCCEEDED(rv)) { - void* xpcom_obj2; - rv = GetXPCOMInstFromProxy(env, aProxy2, &xpcom_obj2); - if (NS_SUCCEEDED(rv)) { - JavaXPCOMInstance* inst1 = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj1); - JavaXPCOMInstance* inst2 = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj2); - if (inst1->GetInstance() == inst2->GetInstance()) { - return JNI_TRUE; - } - } - } - return JNI_FALSE; -} - diff --git a/mozilla/extensions/java/xpcom/nsJavaWrapper.h b/mozilla/extensions/java/xpcom/nsJavaWrapper.h deleted file mode 100644 index 74dc63fc575..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaWrapper.h +++ /dev/null @@ -1,72 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2004 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef _nsJavaWrapper_h_ -#define _nsJavaWrapper_h_ - -#include "jni.h" -#include "nsISupports.h" - - -/** - * Creates a Java proxy around an XPCOM C++ object. - * - * @param env pointer to Java context - * @param aXPCOMObject XPCOM object to create proxy for - * @param aIID IID for XPCOM object - * @param aResult on exit, holds reference to Java proxy - * - * @return NS_OK if Java proxy was successfully created; - * any other value denotes an error condition. - */ -nsresult CreateJavaProxy(JNIEnv* env, nsISupports* aXPCOMObject, - const nsIID& aIID, jobject* aResult); - -/** - * Returns the XPCOM object for which the given Java proxy was created. - * - * @param env pointer to Java context - * @param aJavaObject a Java proxy created by CreateJavaProxy() - * @param aResult on exit, holds pointer to XPCOM instance - * - * @return NS_OK if the XPCOM object was successfully retrieved; - * any other value denotes an error condition. - */ -nsresult GetXPCOMInstFromProxy(JNIEnv* env, jobject aJavaObject, - void** aResult); - -#endif // _nsJavaWrapper_h_ diff --git a/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp b/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp deleted file mode 100644 index f8aa4a42482..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp +++ /dev/null @@ -1,1099 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - - -#include "nsJavaXPCOMBindingUtils.h" -#include "nsJavaXPTCStub.h" -#include "nsJavaWrapper.h" -#include "jni.h" -#include "nsIInterfaceInfoManager.h" -#include "nsILocalFile.h" -#include "nsThreadUtils.h" -#include "nsProxyRelease.h" - - -/* Java JNI globals */ - -JavaVM* gCachedJVM = nsnull; - -jclass systemClass = nsnull; -jclass booleanClass = nsnull; -jclass charClass = nsnull; -jclass byteClass = nsnull; -jclass shortClass = nsnull; -jclass intClass = nsnull; -jclass longClass = nsnull; -jclass floatClass = nsnull; -jclass doubleClass = nsnull; -jclass stringClass = nsnull; -jclass nsISupportsClass = nsnull; -jclass xpcomExceptionClass = nsnull; -jclass xpcomJavaProxyClass = nsnull; -jclass weakReferenceClass = nsnull; - -jmethodID hashCodeMID = nsnull; -jmethodID booleanValueMID = nsnull; -jmethodID booleanInitMID = nsnull; -jmethodID charValueMID = nsnull; -jmethodID charInitMID = nsnull; -jmethodID byteValueMID = nsnull; -jmethodID byteInitMID = nsnull; -jmethodID shortValueMID = nsnull; -jmethodID shortInitMID = nsnull; -jmethodID intValueMID = nsnull; -jmethodID intInitMID = nsnull; -jmethodID longValueMID = nsnull; -jmethodID longInitMID = nsnull; -jmethodID floatValueMID = nsnull; -jmethodID floatInitMID = nsnull; -jmethodID doubleValueMID = nsnull; -jmethodID doubleInitMID = nsnull; -jmethodID createProxyMID = nsnull; -jmethodID isXPCOMJavaProxyMID = nsnull; -jmethodID getNativeXPCOMInstMID = nsnull; -jmethodID weakReferenceConstructorMID = nsnull; -jmethodID getReferentMID = nsnull; -jmethodID clearReferentMID = nsnull; - -#ifdef DEBUG_JAVAXPCOM -jmethodID getNameMID = nsnull; -jmethodID proxyToStringMID = nsnull; -#endif - -NativeToJavaProxyMap* gNativeToJavaProxyMap = nsnull; -JavaToXPTCStubMap* gJavaToXPTCStubMap = nsnull; - -PRBool gJavaXPCOMInitialized = PR_FALSE; -PRLock* gJavaXPCOMLock = nsnull; - -static const char* kJavaKeywords[] = { - "abstract", "default" , "if" , "private" , "throw" , - "boolean" , "do" , "implements", "protected" , "throws" , - "break" , "double" , "import", "public" , "transient" , - "byte" , "else" , "instanceof", "return" , "try" , - "case" , "extends" , "int" , "short" , "void" , - "catch" , "final" , "interface" , "static" , "volatile" , - "char" , "finally" , "long" , "super" , "while" , - "class" , "float" , "native" , "switch" , - "const" , "for" , "new" , "synchronized", - "continue", "goto" , "package" , "this" , - /* added in Java 1.2 */ - "strictfp", - /* added in Java 1.4 */ - "assert" , - /* added in Java 5.0 */ - "enum" , - /* Java constants */ - "true" , "false" , "null" , - /* java.lang.Object methods * - * - don't worry about "toString", since it does the same thing * - * as Object's "toString" */ - "clone" , "equals" , "finalize" , "getClass" , "hashCode" , - "notify" , "notifyAll", /*"toString" ,*/ "wait" -}; - -nsTHashtable* gJavaKeywords = nsnull; - - -/****************************** - * InitializeJavaGlobals - ******************************/ -PRBool -InitializeJavaGlobals(JNIEnv *env) -{ - if (gJavaXPCOMInitialized) - return PR_TRUE; - - // Save pointer to JavaVM, which is valid across threads. - jint rc = env->GetJavaVM(&gCachedJVM); - if (rc != 0) { - NS_WARNING("Failed to get JavaVM"); - goto init_error; - } - - jclass clazz; - if (!(clazz = env->FindClass("java/lang/System")) || - !(systemClass = (jclass) env->NewGlobalRef(clazz)) || - !(hashCodeMID = env->GetStaticMethodID(clazz, "identityHashCode", - "(Ljava/lang/Object;)I"))) - { - NS_WARNING("Problem creating java.lang.System globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Boolean")) || - !(booleanClass = (jclass) env->NewGlobalRef(clazz)) || - !(booleanValueMID = env->GetMethodID(clazz, "booleanValue", "()Z")) || - !(booleanInitMID = env->GetMethodID(clazz, "", "(Z)V"))) - { - NS_WARNING("Problem creating java.lang.Boolean globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Character")) || - !(charClass = (jclass) env->NewGlobalRef(clazz)) || - !(charValueMID = env->GetMethodID(clazz, "charValue", "()C")) || - !(charInitMID = env->GetMethodID(clazz, "", "(C)V"))) - { - NS_WARNING("Problem creating java.lang.Character globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Byte")) || - !(byteClass = (jclass) env->NewGlobalRef(clazz)) || - !(byteValueMID = env->GetMethodID(clazz, "byteValue", "()B")) || - !(byteInitMID = env->GetMethodID(clazz, "", "(B)V"))) - { - NS_WARNING("Problem creating java.lang.Byte globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Short")) || - !(shortClass = (jclass) env->NewGlobalRef(clazz)) || - !(shortValueMID = env->GetMethodID(clazz, "shortValue", "()S")) || - !(shortInitMID = env->GetMethodID(clazz, "", "(S)V"))) - { - NS_WARNING("Problem creating java.lang.Short globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Integer")) || - !(intClass = (jclass) env->NewGlobalRef(clazz)) || - !(intValueMID = env->GetMethodID(clazz, "intValue", "()I")) || - !(intInitMID = env->GetMethodID(clazz, "", "(I)V"))) - { - NS_WARNING("Problem creating java.lang.Integer globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Long")) || - !(longClass = (jclass) env->NewGlobalRef(clazz)) || - !(longValueMID = env->GetMethodID(clazz, "longValue", "()J")) || - !(longInitMID = env->GetMethodID(clazz, "", "(J)V"))) - { - NS_WARNING("Problem creating java.lang.Long globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Float")) || - !(floatClass = (jclass) env->NewGlobalRef(clazz)) || - !(floatValueMID = env->GetMethodID(clazz, "floatValue", "()F")) || - !(floatInitMID = env->GetMethodID(clazz, "", "(F)V"))) - { - NS_WARNING("Problem creating java.lang.Float globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/Double")) || - !(doubleClass = (jclass) env->NewGlobalRef(clazz)) || - !(doubleValueMID = env->GetMethodID(clazz, "doubleValue", "()D")) || - !(doubleInitMID = env->GetMethodID(clazz, "", "(D)V"))) - { - NS_WARNING("Problem creating java.lang.Double globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/String")) || - !(stringClass = (jclass) env->NewGlobalRef(clazz))) - { - NS_WARNING("Problem creating java.lang.String globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("org/mozilla/xpcom/nsISupports")) || - !(nsISupportsClass = (jclass) env->NewGlobalRef(clazz))) - { - NS_WARNING("Problem creating org.mozilla.xpcom.nsISupports globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("org/mozilla/xpcom/XPCOMException")) || - !(xpcomExceptionClass = (jclass) env->NewGlobalRef(clazz))) - { - NS_WARNING("Problem creating org.mozilla.xpcom.XPCOMException globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMJavaProxy")) || - !(xpcomJavaProxyClass = (jclass) env->NewGlobalRef(clazz)) || - !(createProxyMID = env->GetStaticMethodID(clazz, "createProxy", - "(Ljava/lang/Class;J)Ljava/lang/Object;")) || - !(isXPCOMJavaProxyMID = env->GetStaticMethodID(clazz, "isXPCOMJavaProxy", - "(Ljava/lang/Object;)Z")) || - !(getNativeXPCOMInstMID = env->GetStaticMethodID(xpcomJavaProxyClass, - "getNativeXPCOMInstance", - "(Ljava/lang/Object;)J"))) - { - NS_WARNING("Problem creating org.mozilla.xpcom.internal.XPCOMJavaProxy globals"); - goto init_error; - } - - if (!(clazz = env->FindClass("java/lang/ref/WeakReference")) || - !(weakReferenceClass = (jclass) env->NewGlobalRef(clazz)) || - !(weakReferenceConstructorMID = env->GetMethodID(weakReferenceClass, - "","(Ljava/lang/Object;)V")) || - !(getReferentMID = env->GetMethodID(weakReferenceClass, - "get", "()Ljava/lang/Object;")) || - !(clearReferentMID = env->GetMethodID(weakReferenceClass, - "clear", "()V"))) - { - NS_WARNING("Problem creating java.lang.ref.WeakReference globals"); - goto init_error; - } - -#ifdef DEBUG_JAVAXPCOM - if (!(clazz = env->FindClass("java/lang/Class")) || - !(getNameMID = env->GetMethodID(clazz, "getName","()Ljava/lang/String;"))) - { - NS_WARNING("Problem creating java.lang.Class globals"); - goto init_error; - } - - if (!(proxyToStringMID = env->GetStaticMethodID(xpcomJavaProxyClass, - "proxyToString", - "(Ljava/lang/Object;)Ljava/lang/String;"))) - { - NS_WARNING("Problem creating proxyToString global"); - goto init_error; - } -#endif - - gNativeToJavaProxyMap = new NativeToJavaProxyMap(); - if (!gNativeToJavaProxyMap || NS_FAILED(gNativeToJavaProxyMap->Init())) { - NS_WARNING("Problem creating NativeToJavaProxyMap"); - goto init_error; - } - gJavaToXPTCStubMap = new JavaToXPTCStubMap(); - if (!gJavaToXPTCStubMap || NS_FAILED(gJavaToXPTCStubMap->Init())) { - NS_WARNING("Problem creating JavaToXPTCStubMap"); - goto init_error; - } - - { - nsresult rv = NS_OK; - PRUint32 size = NS_ARRAY_LENGTH(kJavaKeywords); - gJavaKeywords = new nsTHashtable(); - if (!gJavaKeywords || NS_FAILED(gJavaKeywords->Init(size))) { - NS_WARNING("Failed to init JavaKeywords HashSet"); - goto init_error; - } - for (PRUint32 i = 0; i < size && NS_SUCCEEDED(rv); i++) { - if (!gJavaKeywords->PutEntry(kJavaKeywords[i])) { - rv = NS_ERROR_OUT_OF_MEMORY; - } - } - if (NS_FAILED(rv)) { - NS_WARNING("Failed to populate JavaKeywords hash"); - goto init_error; - } - } - - gJavaXPCOMLock = PR_NewLock(); - gJavaXPCOMInitialized = PR_TRUE; - return PR_TRUE; - -init_error: - // If we encounter an error during initialization, then free any globals that - // were allocated, and return false. - FreeJavaGlobals(env); - return PR_FALSE; -} - -/************************* - * FreeJavaGlobals - *************************/ -void -FreeJavaGlobals(JNIEnv* env) -{ - PRLock* tempLock = nsnull; - if (gJavaXPCOMLock) { - PR_Lock(gJavaXPCOMLock); - - // null out global lock so no one else can use it - tempLock = gJavaXPCOMLock; - gJavaXPCOMLock = nsnull; - } - - gJavaXPCOMInitialized = PR_FALSE; - - // Free the mappings first, since that process depends on some of the Java - // globals that are freed later. - if (gNativeToJavaProxyMap) { - gNativeToJavaProxyMap->Destroy(env); - delete gNativeToJavaProxyMap; - gNativeToJavaProxyMap = nsnull; - } - if (gJavaToXPTCStubMap) { - gJavaToXPTCStubMap->Destroy(); - delete gJavaToXPTCStubMap; - gJavaToXPTCStubMap = nsnull; - } - - // Free remaining Java globals - if (systemClass) { - env->DeleteGlobalRef(systemClass); - systemClass = nsnull; - } - if (booleanClass) { - env->DeleteGlobalRef(booleanClass); - booleanClass = nsnull; - } - if (charClass) { - env->DeleteGlobalRef(charClass); - charClass = nsnull; - } - if (byteClass) { - env->DeleteGlobalRef(byteClass); - byteClass = nsnull; - } - if (shortClass) { - env->DeleteGlobalRef(shortClass); - shortClass = nsnull; - } - if (intClass) { - env->DeleteGlobalRef(intClass); - intClass = nsnull; - } - if (longClass) { - env->DeleteGlobalRef(longClass); - longClass = nsnull; - } - if (floatClass) { - env->DeleteGlobalRef(floatClass); - floatClass = nsnull; - } - if (doubleClass) { - env->DeleteGlobalRef(doubleClass); - doubleClass = nsnull; - } - if (stringClass) { - env->DeleteGlobalRef(stringClass); - stringClass = nsnull; - } - if (nsISupportsClass) { - env->DeleteGlobalRef(nsISupportsClass); - nsISupportsClass = nsnull; - } - if (xpcomExceptionClass) { - env->DeleteGlobalRef(xpcomExceptionClass); - xpcomExceptionClass = nsnull; - } - if (xpcomJavaProxyClass) { - env->DeleteGlobalRef(xpcomJavaProxyClass); - xpcomJavaProxyClass = nsnull; - } - if (weakReferenceClass) { - env->DeleteGlobalRef(weakReferenceClass); - weakReferenceClass = nsnull; - } - - if (gJavaKeywords) { - delete gJavaKeywords; - gJavaKeywords = nsnull; - } - - if (tempLock) { - PR_Unlock(tempLock); - PR_DestroyLock(tempLock); - } -} - - -/************************************** - * Java<->XPCOM object mappings - **************************************/ - -static PLDHashTableOps hash_ops = -{ - PL_DHashAllocTable, - PL_DHashFreeTable, - PL_DHashGetKeyStub, - PL_DHashVoidPtrKeyStub, - PL_DHashMatchEntryStub, - PL_DHashMoveEntryStub, - PL_DHashClearEntryStub, - PL_DHashFinalizeStub -}; - -// NativeToJavaProxyMap: The common case is that each XPCOM object will have -// one Java proxy. But there are instances where there will be multiple Java -// proxies for a given XPCOM object, each representing a different interface. -// So we optimize the common case by using a hash table. Then, if there are -// multiple Java proxies, we cycle through the linked list, comparing IIDs. - -nsresult -NativeToJavaProxyMap::Init() -{ - mHashTable = PL_NewDHashTable(&hash_ops, nsnull, sizeof(Entry), 16); - if (!mHashTable) - return NS_ERROR_OUT_OF_MEMORY; - return NS_OK; -} - -PLDHashOperator -DestroyJavaProxyMappingEnum(PLDHashTable* aTable, PLDHashEntryHdr* aHeader, - PRUint32 aNumber, void* aData) -{ - JNIEnv* env = NS_STATIC_CAST(JNIEnv*, aData); - NativeToJavaProxyMap::Entry* entry = - NS_STATIC_CAST(NativeToJavaProxyMap::Entry*, aHeader); - - // first, delete XPCOM instances from the Java proxies - nsresult rv; - NativeToJavaProxyMap::ProxyList* item = entry->list; - while(item != nsnull) { - void* xpcom_obj; - jobject javaObject = env->CallObjectMethod(item->javaObject, getReferentMID); - rv = GetXPCOMInstFromProxy(env, javaObject, &xpcom_obj); - NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get XPCOM instance from Java proxy"); - - if (NS_SUCCEEDED(rv)) { - JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj); -#ifdef DEBUG_JAVAXPCOM - char* iid_str = item->iid.ToString(); - LOG(("- NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - javaObject), - (PRUint32) entry, iid_str)); - PR_Free(iid_str); -#endif - delete inst; // releases native XPCOM object - } - - NativeToJavaProxyMap::ProxyList* next = item->next; - env->CallVoidMethod(item->javaObject, clearReferentMID); - env->DeleteGlobalRef(item->javaObject); - delete item; - item = next; - } - - return PL_DHASH_REMOVE; -} - -nsresult -NativeToJavaProxyMap::Destroy(JNIEnv* env) -{ - // This is only called from FreeGlobals(), which already holds the lock. - // nsAutoLock lock(gJavaXPCOMLock); - - PL_DHashTableEnumerate(mHashTable, DestroyJavaProxyMappingEnum, env); - PL_DHashTableDestroy(mHashTable); - mHashTable = nsnull; - - return NS_OK; -} - -nsresult -NativeToJavaProxyMap::Add(JNIEnv* env, nsISupports* aXPCOMObject, - const nsIID& aIID, jobject aProxy) -{ - nsAutoLock lock(gJavaXPCOMLock); - - Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable, - aXPCOMObject, - PL_DHASH_ADD)); - if (!e) - return NS_ERROR_FAILURE; - - jobject ref = nsnull; - jobject weakRefObj = env->NewObject(weakReferenceClass, - weakReferenceConstructorMID, aProxy); - if (weakRefObj) - ref = env->NewGlobalRef(weakRefObj); - if (!ref) - return NS_ERROR_OUT_OF_MEMORY; - - // Add Java proxy weak reference ref to start of list - ProxyList* item = new ProxyList(ref, aIID, e->list); - e->key = aXPCOMObject; - e->list = item; - -#ifdef DEBUG_JAVAXPCOM - char* iid_str = aIID.ToString(); - LOG(("+ NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, aProxy), - (PRUint32) aXPCOMObject, iid_str)); - PR_Free(iid_str); -#endif - return NS_OK; -} - -nsresult -NativeToJavaProxyMap::Find(JNIEnv* env, nsISupports* aNativeObject, - const nsIID& aIID, jobject* aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_FAILURE; - - nsAutoLock lock(gJavaXPCOMLock); - - *aResult = nsnull; - Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable, - aNativeObject, - PL_DHASH_LOOKUP)); - - if (PL_DHASH_ENTRY_IS_FREE(e)) - return NS_OK; - - ProxyList* item = e->list; - while (item != nsnull && *aResult == nsnull) { - if (item->iid.Equals(aIID)) { - jobject referentObj = env->CallObjectMethod(item->javaObject, - getReferentMID); - if (!env->IsSameObject(referentObj, NULL)) { - *aResult = referentObj; -#ifdef DEBUG_JAVAXPCOM - char* iid_str = aIID.ToString(); - LOG(("< NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - *aResult), - (PRUint32) aNativeObject, iid_str)); - PR_Free(iid_str); -#endif - } - } - item = item->next; - } - - return NS_OK; -} - -nsresult -NativeToJavaProxyMap::Remove(JNIEnv* env, nsISupports* aNativeObject, - const nsIID& aIID) -{ - // This is only called from finalizeProxy(), which already holds the lock. - // nsAutoLock lock(gJavaXPCOMLock); - - Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable, - aNativeObject, - PL_DHASH_LOOKUP)); - - if (PL_DHASH_ENTRY_IS_FREE(e)) { - NS_WARNING("XPCOM object not found in hash table"); - return NS_ERROR_FAILURE; - } - - ProxyList* item = e->list; - ProxyList* last = e->list; - while (item != nsnull) { - if (item->iid.Equals(aIID)) { -#ifdef DEBUG_JAVAXPCOM - char* iid_str = aIID.ToString(); - LOG(("- NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID, - item->javaObject), - (PRUint32) aNativeObject, iid_str)); - PR_Free(iid_str); -#endif - - env->CallVoidMethod(item->javaObject, clearReferentMID); - env->DeleteGlobalRef(item->javaObject); - if (item == e->list) { - e->list = item->next; - if (e->list == nsnull) - PL_DHashTableOperate(mHashTable, aNativeObject, PL_DHASH_REMOVE); - } else { - last->next = item->next; - } - - delete item; - return NS_OK; - } - - last = item; - item = item->next; - } - - NS_WARNING("Java proxy matching given IID not found"); - return NS_ERROR_FAILURE; -} - -nsresult -JavaToXPTCStubMap::Init() -{ - mHashTable = PL_NewDHashTable(&hash_ops, nsnull, sizeof(Entry), 16); - if (!mHashTable) - return NS_ERROR_OUT_OF_MEMORY; - return NS_OK; -} - - -PLDHashOperator -DestroyXPTCMappingEnum(PLDHashTable* aTable, PLDHashEntryHdr* aHeader, - PRUint32 aNumber, void* aData) -{ - JavaToXPTCStubMap::Entry* entry = - NS_STATIC_CAST(JavaToXPTCStubMap::Entry*, aHeader); - - // The XPTC stub will be released by the XPCOM side, if it hasn't been - // already. We just need to delete the Java global ref held by the XPTC stub, - // so the Java garbage collector can handle the Java object when necessary. - entry->xptcstub->DeleteStrongRef(); - - return PL_DHASH_REMOVE; -} - -nsresult -JavaToXPTCStubMap::Destroy() -{ - // This is only called from FreeGlobals(), which already holds the lock. - // nsAutoLock lock(gJavaXPCOMLock); - - PL_DHashTableEnumerate(mHashTable, DestroyXPTCMappingEnum, nsnull); - PL_DHashTableDestroy(mHashTable); - mHashTable = nsnull; - - return NS_OK; -} - -nsresult -JavaToXPTCStubMap::Add(jint aJavaObjectHashCode, nsJavaXPTCStub* aProxy) -{ - nsAutoLock lock(gJavaXPCOMLock); - - Entry* e = NS_STATIC_CAST(Entry*, - PL_DHashTableOperate(mHashTable, - NS_INT32_TO_PTR(aJavaObjectHashCode), - PL_DHASH_ADD)); - if (!e) - return NS_ERROR_FAILURE; - - NS_ASSERTION(e->key == nsnull, - "XPTCStub for given Java object already exists in hash table"); - e->key = aJavaObjectHashCode; - e->xptcstub = aProxy; - -#ifdef DEBUG_JAVAXPCOM - nsIInterfaceInfo* iface_info; - aProxy->GetInterfaceInfo(&iface_info); - nsIID* iid; - iface_info->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - LOG(("+ JavaToXPTCStubMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) hash, (PRUint32) aProxy, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); - NS_RELEASE(iface_info); -#endif - return NS_OK; -} - -nsresult -JavaToXPTCStubMap::Find(jint aJavaObjectHashCode, const nsIID& aIID, - nsJavaXPTCStub** aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_FAILURE; - - nsAutoLock lock(gJavaXPCOMLock); - - *aResult = nsnull; - Entry* e = NS_STATIC_CAST(Entry*, - PL_DHashTableOperate(mHashTable, - NS_INT32_TO_PTR(aJavaObjectHashCode), - PL_DHASH_LOOKUP)); - - if (PL_DHASH_ENTRY_IS_FREE(e)) - return NS_OK; - - nsresult rv = e->xptcstub->QueryInterface(aIID, (void**) aResult); - -#ifdef DEBUG_JAVAXPCOM - if (NS_SUCCEEDED(rv)) { - char* iid_str = aIID.ToString(); - LOG(("< JavaToXPTCStubMap (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) hash, (PRUint32) *aResult, iid_str)); - PR_Free(iid_str); - } -#endif - - // NS_NOINTERFACE is not an error condition - if (rv == NS_NOINTERFACE) - rv = NS_OK; - return rv; -} - -nsresult -JavaToXPTCStubMap::Remove(jint aJavaObjectHashCode) -{ - PL_DHashTableOperate(mHashTable, NS_INT32_TO_PTR(aJavaObjectHashCode), - PL_DHASH_REMOVE); - -#ifdef DEBUG_JAVAXPCOM - LOG(("- JavaToXPTCStubMap (Java=%08x)\n", (PRUint32) aJavaObjectHashCode)); -#endif - - return NS_OK; -} - - -/********************************************************** - * JavaXPCOMInstance - *********************************************************/ -JavaXPCOMInstance::JavaXPCOMInstance(nsISupports* aInstance, - nsIInterfaceInfo* aIInfo) - : mInstance(aInstance) - , mIInfo(aIInfo) -{ - NS_ADDREF(mInstance); - NS_ADDREF(mIInfo); -} - -JavaXPCOMInstance::~JavaXPCOMInstance() -{ - nsresult rv = NS_OK; - - // Need to release these objects on the main thread. - nsCOMPtr thread = do_GetMainThread(); - if (thread) { - rv = NS_ProxyRelease(thread, mInstance); - rv |= NS_ProxyRelease(thread, mIInfo); - } - NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to release using NS_ProxyRelease"); -} - - -/******************************* - * Helper functions - *******************************/ - -nsresult -GetNewOrUsedJavaObject(JNIEnv* env, nsISupports* aXPCOMObject, - const nsIID& aIID, jobject* aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_NULL_POINTER; - - nsresult rv; - nsJavaXPTCStub* stub = nsnull; - aXPCOMObject->QueryInterface(NS_GET_IID(nsJavaXPTCStub), (void**) &stub); - if (stub) { - // Get Java object directly from nsJavaXPTCStub - *aResult = stub->GetJavaObject(); - NS_ASSERTION(*aResult != nsnull, "nsJavaXPTCStub w/o matching Java object"); - NS_RELEASE(stub); - return NS_OK; - } - - // Get the root nsISupports of the xpcom object - nsCOMPtr rootObject = do_QueryInterface(aXPCOMObject, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - // Get associated Java object from hash table - rv = gNativeToJavaProxyMap->Find(env, rootObject, aIID, aResult); - NS_ENSURE_SUCCESS(rv, rv); - if (*aResult) - return NS_OK; - - // No Java object is associated with the given XPCOM object, so we - // create a Java proxy. - return CreateJavaProxy(env, rootObject, aIID, aResult); -} - -nsresult -GetNewOrUsedXPCOMObject(JNIEnv* env, jobject aJavaObject, const nsIID& aIID, - nsISupports** aResult) -{ - NS_PRECONDITION(aResult != nsnull, "null ptr"); - if (!aResult) - return NS_ERROR_NULL_POINTER; - - nsresult rv; - *aResult = nsnull; - - // Check if the given Java object is actually one of our Java proxies. If so, - // then we query the associated XPCOM object directly from the proxy. - // If Java object is not a proxy, then we try to find associated XPCOM object - // in the mapping table. - jboolean isProxy = env->CallStaticBooleanMethod(xpcomJavaProxyClass, - isXPCOMJavaProxyMID, - aJavaObject); - if (env->ExceptionCheck()) - return NS_ERROR_FAILURE; - - if (isProxy) { - void* inst; - rv = GetXPCOMInstFromProxy(env, aJavaObject, &inst); - NS_ENSURE_SUCCESS(rv, rv); - - nsISupports* rootObject = - NS_STATIC_CAST(JavaXPCOMInstance*, inst)->GetInstance(); - rv = rootObject->QueryInterface(aIID, (void**) aResult); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; - } - - nsJavaXPTCStub* stub; - jint hash = env->CallStaticIntMethod(systemClass, hashCodeMID, aJavaObject); - rv = gJavaToXPTCStubMap->Find(hash, aIID, &stub); - NS_ENSURE_SUCCESS(rv, rv); - if (stub) { - // stub is already AddRef'd and QI'd - *aResult = NS_STATIC_CAST(nsISupports*, - NS_STATIC_CAST(nsXPTCStubBase*, stub)); - return NS_OK; - } - - // If there is no corresponding XPCOM object, then that means that the - // parameter is a non-generated class (that is, it is not one of our - // Java stubs that represent an exising XPCOM object). So we need to - // create an XPCOM stub, that can route any method calls to the class. - - // Get interface info for class - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr iinfo; - rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo)); - NS_ENSURE_SUCCESS(rv, rv); - - // Create XPCOM stub - stub = new nsJavaXPTCStub(aJavaObject, iinfo); - if (!stub) { - return NS_ERROR_OUT_OF_MEMORY; - } - rv = gJavaToXPTCStubMap->Add(hash, stub); - if (NS_FAILED(rv)) { - delete stub; - return rv; - } - - NS_ADDREF(stub); - *aResult = NS_STATIC_CAST(nsISupports*, - NS_STATIC_CAST(nsXPTCStubBase*, stub)); - - return NS_OK; -} - -nsresult -GetIIDForMethodParam(nsIInterfaceInfo *iinfo, const nsXPTMethodInfo *methodInfo, - const nsXPTParamInfo ¶mInfo, PRUint8 paramType, - PRUint16 methodIndex, nsXPTCMiniVariant *dispatchParams, - PRBool isFullVariantArray, nsID &result) -{ - nsresult rv; - - switch (paramType) - { - case nsXPTType::T_INTERFACE: - rv = iinfo->GetIIDForParamNoAlloc(methodIndex, ¶mInfo, &result); - break; - - case nsXPTType::T_INTERFACE_IS: - { - PRUint8 argnum; - rv = iinfo->GetInterfaceIsArgNumberForParam(methodIndex, ¶mInfo, - &argnum); - if (NS_FAILED(rv)) - break; - - const nsXPTParamInfo& arg_param = methodInfo->GetParam(argnum); - const nsXPTType& arg_type = arg_param.GetType(); - - // The xpidl compiler ensures this. We reaffirm it for safety. - if (!arg_type.IsPointer() || arg_type.TagPart() != nsXPTType::T_IID) { - rv = NS_ERROR_UNEXPECTED; - break; - } - - nsID *p = nsnull; - if (isFullVariantArray) { - p = (nsID *) ((nsXPTCVariant*) dispatchParams)[argnum].val.p; - } else { - p = (nsID *) dispatchParams[argnum].val.p; - } - if (!p) - return NS_ERROR_UNEXPECTED; - - result = *p; - break; - } - - default: - rv = NS_ERROR_UNEXPECTED; - } - return rv; -} - - -/******************************* - * JNI helper functions - *******************************/ - -JNIEnv* -GetJNIEnv() -{ - JNIEnv* env; - jint rc = gCachedJVM->GetEnv((void**) &env, JNI_VERSION_1_2); - NS_ASSERTION(rc == JNI_OK && env != nsnull, - "Current thread not attached to given JVM instance"); - return env; -} - -void -ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) -{ - // Only throw this exception if one hasn't already been thrown, so we don't - // mask a previous exception/error. - if (env->ExceptionCheck()) - return; - - // If the error code we get is for an Out Of Memory error, try to throw an - // OutOfMemoryError. The JVM may have enough memory to create this error. - if (aErrorCode == NS_ERROR_OUT_OF_MEMORY) { - jclass clazz = env->FindClass("java/lang/OutOfMemoryError"); - if (clazz) { - env->ThrowNew(clazz, aMessage); - } - env->DeleteLocalRef(clazz); - return; - } - - // If the error was not handled above, then create an XPCOMException with the - // given error code and message. - - // Create parameters and method signature. Max of 2 params. The error code - // comes before the message string. - PRInt64 errorCode = aErrorCode ? aErrorCode : NS_ERROR_FAILURE; - nsCAutoString methodSig("(J"); - jstring message = nsnull; - if (aMessage) { - message = env->NewStringUTF(aMessage); - if (!message) { - return; - } - methodSig.AppendLiteral("Ljava/lang/String;"); - } - methodSig.AppendLiteral(")V"); - - // In some instances (such as in shutdownXPCOM() and termEmbedding()), we - // will need to throw an exception when JavaXPCOM has already been - // terminated. In such a case, 'xpcomExceptionClass' will be null. So we - // reset it temporarily in order to throw the appropriate exception. - if (xpcomExceptionClass == nsnull) { - xpcomExceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException"); - if (!xpcomExceptionClass) { - return; - } - } - - // create exception object - jthrowable throwObj = nsnull; - jmethodID mid = env->GetMethodID(xpcomExceptionClass, "", - methodSig.get()); - if (mid) { - throwObj = (jthrowable) env->NewObject(xpcomExceptionClass, mid, errorCode, - message); - } - NS_ASSERTION(throwObj, "Failed to create XPCOMException object"); - - // throw exception - if (throwObj) { - env->Throw(throwObj); - } -} - -nsAString* -jstring_to_nsAString(JNIEnv* env, jstring aString) -{ - const PRUnichar* buf = nsnull; - if (aString) { - buf = env->GetStringChars(aString, nsnull); - if (!buf) - return nsnull; // exception already thrown - } - - nsString* str = new nsString(buf); - env->ReleaseStringChars(aString, buf); - - // returns string, or nsnull if 'new' failed - return str; -} - -nsACString* -jstring_to_nsACString(JNIEnv* env, jstring aString) -{ - const char* buf = nsnull; - if (aString) { - buf = env->GetStringUTFChars(aString, nsnull); - if (!buf) - return nsnull; // exception already thrown - } - - nsCString* str = new nsCString(buf); - env->ReleaseStringUTFChars(aString, buf); - - // returns string, or nsnull if 'new' failed - return str; -} - -nsresult -File_to_nsILocalFile(JNIEnv* env, jobject aFile, nsILocalFile** aLocalFile) -{ - nsresult rv = NS_ERROR_FAILURE; - jstring pathName = nsnull; - jclass clazz = env->FindClass("java/io/File"); - if (clazz) { - jmethodID pathMID = env->GetMethodID(clazz, "getCanonicalPath", - "()Ljava/lang/String;"); - if (pathMID) { - pathName = (jstring) env->CallObjectMethod(aFile, pathMID); - if (pathName != nsnull && !env->ExceptionCheck()) - rv = NS_OK; - } - } - - if (NS_SUCCEEDED(rv)) { - nsAString* path = jstring_to_nsAString(env, pathName); - if (!path) - rv = NS_ERROR_OUT_OF_MEMORY; - - if (NS_SUCCEEDED(rv)) { - rv = NS_NewLocalFile(*path, false, aLocalFile); - delete path; - } - } - - return rv; -} - diff --git a/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h b/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h deleted file mode 100644 index e5f3e699ecb..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPCOMBindingUtils.h +++ /dev/null @@ -1,358 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef _nsJavaXPCOMBindingUtils_h_ -#define _nsJavaXPCOMBindingUtils_h_ - -#include "jni.h" -#include "xptcall.h" -#include "nsCOMPtr.h" -#include "nsString.h" -#include "pldhash.h" -#include "nsJavaXPTCStub.h" -#include "nsAutoLock.h" -#include "nsTHashtable.h" -#include "nsHashKeys.h" - -//#define DEBUG_JAVAXPCOM -//#define DEBUG_JAVAXPCOM_REFCNT - -#ifdef DEBUG_JAVAXPCOM -#define LOG(x) printf x -#else -#define LOG(x) /* nothing */ -#endif - - -/********************* - * Java JNI globals - *********************/ - -extern jclass systemClass; -extern jclass booleanClass; -extern jclass charClass; -extern jclass byteClass; -extern jclass shortClass; -extern jclass intClass; -extern jclass longClass; -extern jclass floatClass; -extern jclass doubleClass; -extern jclass stringClass; -extern jclass nsISupportsClass; -extern jclass xpcomExceptionClass; -extern jclass xpcomJavaProxyClass; -extern jclass weakReferenceClass; - -extern jmethodID hashCodeMID; -extern jmethodID booleanValueMID; -extern jmethodID booleanInitMID; -extern jmethodID charValueMID; -extern jmethodID charInitMID; -extern jmethodID byteValueMID; -extern jmethodID byteInitMID; -extern jmethodID shortValueMID; -extern jmethodID shortInitMID; -extern jmethodID intValueMID; -extern jmethodID intInitMID; -extern jmethodID longValueMID; -extern jmethodID longInitMID; -extern jmethodID floatValueMID; -extern jmethodID floatInitMID; -extern jmethodID doubleValueMID; -extern jmethodID doubleInitMID; -extern jmethodID createProxyMID; -extern jmethodID isXPCOMJavaProxyMID; -extern jmethodID getNativeXPCOMInstMID; -extern jmethodID weakReferenceConstructorMID; -extern jmethodID getReferentMID; -extern jmethodID clearReferentMID; - -#ifdef DEBUG_JAVAXPCOM -extern jmethodID getNameMID; -extern jmethodID proxyToStringMID; -#endif - -class NativeToJavaProxyMap; -extern NativeToJavaProxyMap* gNativeToJavaProxyMap; -class JavaToXPTCStubMap; -extern JavaToXPTCStubMap* gJavaToXPTCStubMap; - -extern nsTHashtable* gJavaKeywords; - -// The Java garbage collector runs in a separate thread. Since it calls the -// finalizeProxy() function in nsJavaWrapper.cpp, we need to make sure that -// all the structures touched by finalizeProxy() are multithread aware. -extern PRLock* gJavaXPCOMLock; - -extern PRBool gJavaXPCOMInitialized; - -/** - * Initialize global structures used by JavaXPCOM. - * @param env Java environment pointer - * @return PR_TRUE if JavaXPCOM is initialized; PR_FALSE if an error occurred - */ -PRBool InitializeJavaGlobals(JNIEnv *env); - -/** - * Frees global structures that were allocated by InitializeJavaGlobals(). - * @param env Java environment pointer - */ -void FreeJavaGlobals(JNIEnv* env); - - -/************************* - * JavaXPCOMInstance - *************************/ - -class JavaXPCOMInstance -{ -public: - JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo); - ~JavaXPCOMInstance(); - - nsISupports* GetInstance() { return mInstance; } - nsIInterfaceInfo* InterfaceInfo() { return mIInfo; } - -private: - nsISupports* mInstance; - nsIInterfaceInfo* mIInfo; -}; - - -/************************************** - * Java<->XPCOM object mappings - **************************************/ - -/** - * Maps native XPCOM objects to their associated Java proxy object. - */ -class NativeToJavaProxyMap -{ - friend PLDHashOperator DestroyJavaProxyMappingEnum(PLDHashTable* aTable, - PLDHashEntryHdr* aHeader, - PRUint32 aNumber, - void* aData); - -protected: - struct ProxyList - { - ProxyList(const jobject aRef, const nsIID& aIID, ProxyList* aList) - : javaObject(aRef) - , iid(aIID) - , next(aList) - { } - - const jobject javaObject; - const nsIID iid; - ProxyList* next; - }; - - struct Entry : public PLDHashEntryHdr - { - nsISupports* key; - ProxyList* list; - }; - -public: - NativeToJavaProxyMap() - : mHashTable(nsnull) - { } - - ~NativeToJavaProxyMap() - { - NS_ASSERTION(mHashTable == nsnull, - "MUST call Destroy() before deleting object"); - } - - nsresult Init(); - - nsresult Destroy(JNIEnv* env); - - nsresult Add(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID, - jobject aProxy); - - nsresult Find(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID, - jobject* aResult); - - nsresult Remove(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID); - -protected: - PLDHashTable* mHashTable; -}; - -/** - * Maps Java objects to their associated nsJavaXPTCStub. - */ -class JavaToXPTCStubMap -{ - friend PLDHashOperator DestroyXPTCMappingEnum(PLDHashTable* aTable, - PLDHashEntryHdr* aHeader, - PRUint32 aNumber, void* aData); - -protected: - struct Entry : public PLDHashEntryHdr - { - jint key; - nsJavaXPTCStub* xptcstub; - }; - -public: - JavaToXPTCStubMap() - : mHashTable(nsnull) - { } - - ~JavaToXPTCStubMap() - { - NS_ASSERTION(mHashTable == nsnull, - "MUST call Destroy() before deleting object"); - } - - nsresult Init(); - - nsresult Destroy(); - - nsresult Add(jint aJavaObjectHashCode, nsJavaXPTCStub* aProxy); - - nsresult Find(jint aJavaObjectHashCode, const nsIID& aIID, - nsJavaXPTCStub** aResult); - - nsresult Remove(jint aJavaObjectHashCode); - -protected: - PLDHashTable* mHashTable; -}; - - -/******************************* - * Helper functions - *******************************/ - -/** - * Finds the associated Java object for the given XPCOM object and IID. If no - * such Java object exists, then it creates one. - * - * @param env Java environment pointer - * @param aXPCOMObject XPCOM object for which to find/create Java object - * @param aIID desired interface IID for Java object - * @param aResult on success, holds reference to Java object - * - * @return NS_OK if succeeded; all other return values are error codes. - */ -nsresult GetNewOrUsedJavaObject(JNIEnv* env, nsISupports* aXPCOMObject, - const nsIID& aIID, jobject* aResult); - -/** - * Finds the associated XPCOM object for the given Java object and IID. If no - * such XPCOM object exists, then it creates one. - * - * @param env Java environment pointer - * @param aJavaObject Java object for which to find/create XPCOM object - * @param aIID desired interface IID for XPCOM object - * @param aResult on success, holds AddRef'd reference to XPCOM object - * - * @return NS_OK if succeeded; all other return values are error codes. - */ -nsresult GetNewOrUsedXPCOMObject(JNIEnv* env, jobject aJavaObject, - const nsIID& aIID, nsISupports** aResult); - -nsresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo, - const nsXPTMethodInfo *methodInfo, - const nsXPTParamInfo ¶mInfo, - PRUint8 paramType, PRUint16 methodIndex, - nsXPTCMiniVariant *dispatchParams, - PRBool isFullVariantArray, - nsID &result); - - -/******************************* - * JNI helper functions - *******************************/ - -/** - * Returns a pointer to the appropriate JNIEnv structure. This function is - * useful in callbacks or other functions that are not called directly from - * Java and therefore do not have the JNIEnv structure passed in. - * - * @return pointer to JNIEnv structure for current thread - */ -JNIEnv* GetJNIEnv(); - -/** - * Constructs and throws an exception. Some error codes (such as - * NS_ERROR_OUT_OF_MEMORY) are handled by the appropriate Java exception/error. - * Otherwise, an instance of XPCOMException is created with the given error - * code and message. - * - * @param env Java environment pointer - * @param aErrorCode The error code returned by an XPCOM/Gecko function. Pass - * zero for the default behaviour. - * @param aMessage A string that provides details for throwing this - * exception. Pass in nsnull for the default - * behaviour. - * - * @throws OutOfMemoryError if aErrorCode == NS_ERROR_OUT_OF_MEMORY - * XPCOMException for all other error codes - */ -void ThrowException(JNIEnv* env, const nsresult aErrorCode, - const char* aMessage); - -/** - * Helper functions for converting from java.lang.String to - * nsAString/nsACstring. Caller must delete nsAString/nsACString. - * - * @param env Java environment pointer - * @param aString Java string to convert - * - * @return nsAString/nsACString with same content as given Java string; or - * nsnull if out of memory - */ -nsAString* jstring_to_nsAString(JNIEnv* env, jstring aString); -nsACString* jstring_to_nsACString(JNIEnv* env, jstring aString); - -/** - * Helper function for converting from java.io.File to nsILocalFile. - * - * @param env Java environment pointer - * @param aFile Java File to convert - * @param aLocalFile returns the converted nsILocalFile - * - * @return NS_OK for success; other values indicate error in conversion - */ -nsresult File_to_nsILocalFile(JNIEnv* env, jobject aFile, - nsILocalFile** aLocalFile); - -#endif // _nsJavaXPCOMBindingUtils_h_ diff --git a/mozilla/extensions/java/xpcom/nsJavaXPTCStub.cpp b/mozilla/extensions/java/xpcom/nsJavaXPTCStub.cpp deleted file mode 100644 index 097b29708e5..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPTCStub.cpp +++ /dev/null @@ -1,1707 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "nsJavaXPTCStub.h" -#include "nsJavaWrapper.h" -#include "nsJavaXPCOMBindingUtils.h" -#include "prmem.h" -#include "nsIInterfaceInfoManager.h" -#include "nsString.h" -#include "nsString.h" -#include "nsCRT.h" -#include "nsServiceManagerUtils.h" - - -nsJavaXPTCStub::nsJavaXPTCStub(jobject aJavaObject, nsIInterfaceInfo *aIInfo) - : mJavaStrongRef(nsnull) - , mIInfo(aIInfo) - , mMaster(nsnull) - , mWeakRefCnt(0) -{ - JNIEnv* env = GetJNIEnv(); - jobject weakref = env->NewObject(weakReferenceClass, - weakReferenceConstructorMID, aJavaObject); - mJavaWeakRef = env->NewGlobalRef(weakref); - mJavaRefHashCode = env->CallStaticIntMethod(systemClass, hashCodeMID, - aJavaObject); - -#ifdef DEBUG_JAVAXPCOM - nsIID* iid; - mIInfo->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - LOG(("+ nsJavaXPTCStub (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) mJavaRefHashCode, (PRUint32) this, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif -} - -nsJavaXPTCStub::~nsJavaXPTCStub() -{ -} - -NS_IMETHODIMP_(nsrefcnt) -nsJavaXPTCStub::AddRefInternal() -{ - // If this is the first AddRef call, we create a strong global ref to the - // Java object to keep it from being garbage collected. - if (mRefCnt == 0) { - JNIEnv* env = GetJNIEnv(); - jobject referent = env->CallObjectMethod(mJavaWeakRef, getReferentMID); - if (!env->IsSameObject(referent, NULL)) { - mJavaStrongRef = env->NewGlobalRef(referent); - } - NS_ASSERTION(mJavaStrongRef != nsnull, "Failed to acquire strong ref"); - } - - // if this is the master interface - NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt"); - NS_ASSERT_OWNINGTHREAD(nsJavaXPTCStub); - ++mRefCnt; - NS_LOG_ADDREF(this, mRefCnt, "nsJavaXPTCStub", sizeof(*this)); - return mRefCnt; -} - -NS_IMETHODIMP_(nsrefcnt) -nsJavaXPTCStub::AddRef() -{ -#ifdef DEBUG_JAVAXPCOM_REFCNT - nsIID* iid; - mIInfo->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - int refcnt = PRInt32(mMaster ? mMaster->mRefCnt : mRefCnt) + 1; - LOG(("= nsJavaXPTCStub::AddRef (XPCOM=%08x | refcnt = %d | IID=%s)\n", - (int) this, refcnt, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif - - nsJavaXPTCStub* master = mMaster ? mMaster : this; - return master->AddRefInternal(); -} - -NS_IMETHODIMP_(nsrefcnt) -nsJavaXPTCStub::ReleaseInternal() -{ - NS_PRECONDITION(0 != mRefCnt, "dup release"); - NS_ASSERT_OWNINGTHREAD(nsJavaXPTCStub); - --mRefCnt; - NS_LOG_RELEASE(this, mRefCnt, "nsJavaXPTCStub"); - if (mRefCnt == 0) { - // delete strong ref; allows Java object to be garbage collected - DeleteStrongRef(); - - // If we have a weak ref, we don't delete this object. - if (mWeakRefCnt == 0) { - mRefCnt = 1; /* stabilize */ - Destroy(); - delete this; - } - return 0; - } - return mRefCnt; -} - -NS_IMETHODIMP_(nsrefcnt) -nsJavaXPTCStub::Release() -{ -#ifdef DEBUG_JAVAXPCOM_REFCNT - nsIID* iid; - mIInfo->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - int refcnt = PRInt32(mMaster ? mMaster->mRefCnt : mRefCnt) - 1; - LOG(("= nsJavaXPTCStub::Release (XPCOM=%08x | refcnt = %d | IID=%s)\n", - (int) this, refcnt, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif - - nsJavaXPTCStub* master = mMaster ? mMaster : this; - return master->ReleaseInternal(); -} - -void -nsJavaXPTCStub::Destroy() -{ - JNIEnv* env = GetJNIEnv(); - -#ifdef DEBUG_JAVAXPCOM - nsIID* iid; - mIInfo->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - LOG(("- nsJavaXPTCStub (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) mJavaRefHashCode, (PRUint32) this, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif - - if (!mMaster) { - // delete each child stub - for (PRInt32 i = 0; i < mChildren.Count(); i++) { - delete (nsJavaXPTCStub*) mChildren[i]; - } - - // Since we are destroying this stub, also remove the mapping. - // It is possible for mJavaStrongRef to be NULL here. That is why we - // store the hash code value earlier. - if (gJavaXPCOMInitialized) { - gJavaToXPTCStubMap->Remove(mJavaRefHashCode); - } - } - - env->CallVoidMethod(mJavaWeakRef, clearReferentMID); - env->DeleteGlobalRef(mJavaWeakRef); -} - -void -nsJavaXPTCStub::ReleaseWeakRef() -{ - // if this is a child - if (mMaster) - mMaster->ReleaseWeakRef(); - - --mWeakRefCnt; - - // If there are no more associated weak refs, and no one else holds a strong - // ref to this object, then delete it. - if (mWeakRefCnt == 0 && mRefCnt == 0) { - NS_ASSERT_OWNINGTHREAD(nsJavaXPTCStub); - mRefCnt = 1; /* stabilize */ - Destroy(); - delete this; - } -} - -void -nsJavaXPTCStub::DeleteStrongRef() -{ - if (mJavaStrongRef == nsnull) - return; - - GetJNIEnv()->DeleteGlobalRef(mJavaStrongRef); - mJavaStrongRef = nsnull; -} - -NS_IMETHODIMP -nsJavaXPTCStub::QueryInterface(const nsID &aIID, void **aInstancePtr) -{ - nsresult rv; - - LOG(("JavaStub::QueryInterface()\n")); - *aInstancePtr = nsnull; - nsJavaXPTCStub *master = mMaster ? mMaster : this; - - // This helps us differentiate between the help classes. - if (aIID.Equals(NS_GET_IID(nsJavaXPTCStub))) - { - *aInstancePtr = master; - NS_ADDREF(this); - return NS_OK; - } - - // always return the master stub for nsISupports - if (aIID.Equals(NS_GET_IID(nsISupports))) - { - *aInstancePtr = NS_STATIC_CAST(nsISupports*, - NS_STATIC_CAST(nsXPTCStubBase*, master)); - NS_ADDREF(master); - return NS_OK; - } - - // All Java objects support weak references - if (aIID.Equals(NS_GET_IID(nsISupportsWeakReference))) - { - *aInstancePtr = NS_STATIC_CAST(nsISupportsWeakReference*, master); - NS_ADDREF(master); - return NS_OK; - } - - // does any existing stub support the requested IID? - nsJavaXPTCStub *stub = master->FindStubSupportingIID(aIID); - if (stub) - { - *aInstancePtr = stub; - NS_ADDREF(stub); - return NS_OK; - } - - JNIEnv* env = GetJNIEnv(); - - // Query Java object - LOG(("\tCalling Java object queryInterface\n")); - jobject javaObject = env->CallObjectMethod(mJavaWeakRef, getReferentMID); - - jmethodID qiMID = 0; - jclass clazz = env->GetObjectClass(javaObject); - if (clazz) { - char* sig = "(Ljava/lang/String;)Lorg/mozilla/xpcom/nsISupports;"; - qiMID = env->GetMethodID(clazz, "queryInterface", sig); - NS_ASSERTION(qiMID, "Failed to get queryInterface method ID"); - } - - if (qiMID == 0) { - env->ExceptionClear(); - return NS_NOINTERFACE; - } - - // construct IID string - jstring iid_jstr = nsnull; - char* iid_str = aIID.ToString(); - if (iid_str) { - iid_jstr = env->NewStringUTF(iid_str); - } - if (!iid_str || !iid_jstr) { - env->ExceptionClear(); - return NS_ERROR_OUT_OF_MEMORY; - } - PR_Free(iid_str); - - // call queryInterface method - jobject obj = env->CallObjectMethod(javaObject, qiMID, iid_jstr); - if (env->ExceptionCheck()) { - env->ExceptionClear(); - return NS_ERROR_FAILURE; - } - if (!obj) - return NS_NOINTERFACE; - - // Get interface info for new java object - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr iinfo; - rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo)); - if (NS_FAILED(rv)) - return rv; - - stub = new nsJavaXPTCStub(obj, iinfo); - if (!stub) - return NS_ERROR_OUT_OF_MEMORY; - - // add stub to the master's list of children, so we can preserve - // symmetry in future QI calls. the master will delete each child - // when it is destroyed. the refcount of each child is bound to - // the refcount of the master. this is done to deal with code - // like this: - // - // nsCOMPtr bar = ...; - // nsIFoo *foo; - // { - // nsCOMPtr temp = do_QueryInterface(bar); - // foo = temp; - // } - // foo->DoStuff(); - // - // while this code is not valid XPCOM (since it is using |foo| - // after having called Release on it), such code is unfortunately - // very common in the mozilla codebase. the assumption this code - // is making is that so long as |bar| is alive, it should be valid - // to access |foo| even if the code doesn't own a strong reference - // to |foo|! clearly wrong, but we need to support it anyways. - - stub->mMaster = master; - master->mChildren.AppendElement(stub); - - *aInstancePtr = stub; - NS_ADDREF(stub); - return NS_OK; -} - -PRBool -nsJavaXPTCStub::SupportsIID(const nsID &iid) -{ - PRBool match; - nsCOMPtr iter = mIInfo; - do - { - if (NS_SUCCEEDED(iter->IsIID(&iid, &match)) && match) - return PR_TRUE; - - nsCOMPtr parent; - iter->GetParent(getter_AddRefs(parent)); - iter = parent; - } - while (iter != nsnull); - - return PR_FALSE; -} - -nsJavaXPTCStub * -nsJavaXPTCStub::FindStubSupportingIID(const nsID &iid) -{ - NS_ASSERTION(mMaster == nsnull, "this is not a master stub"); - - if (SupportsIID(iid)) - return this; - - for (PRInt32 i = 0; i < mChildren.Count(); i++) - { - nsJavaXPTCStub *child = (nsJavaXPTCStub *) mChildren[i]; - if (child->SupportsIID(iid)) - return child; - } - return nsnull; -} - -NS_IMETHODIMP -nsJavaXPTCStub::GetInterfaceInfo(nsIInterfaceInfo **aInfo) -{ - NS_ADDREF(*aInfo = mIInfo); - return NS_OK; -} - -NS_IMETHODIMP -nsJavaXPTCStub::CallMethod(PRUint16 aMethodIndex, - const nsXPTMethodInfo *aMethodInfo, - nsXPTCMiniVariant *aParams) -{ -#ifdef DEBUG_JAVAXPCOM - const char* ifaceName; - mIInfo->GetNameShared(&ifaceName); - LOG(("---> (Java) %s::%s()\n", ifaceName, aMethodInfo->GetName())); -#endif - - nsresult rv = NS_OK; - JNIEnv* env = GetJNIEnv(); - jobject javaObject = env->CallObjectMethod(mJavaWeakRef, getReferentMID); - - nsCAutoString methodSig("("); - - // Create jvalue array to hold Java params - PRUint8 paramCount = aMethodInfo->GetParamCount(); - jvalue* java_params = nsnull; - const nsXPTParamInfo* retvalInfo = nsnull; - if (paramCount) { - java_params = new jvalue[paramCount]; - if (!java_params) - return NS_ERROR_OUT_OF_MEMORY; - - for (PRUint8 i = 0; i < paramCount && NS_SUCCEEDED(rv); i++) - { - const nsXPTParamInfo ¶mInfo = aMethodInfo->GetParam(i); - NS_ASSERTION(!paramInfo.IsDipper(), "Dipper!"); - if (!paramInfo.IsRetval()) { - rv = SetupJavaParams(paramInfo, aMethodInfo, aMethodIndex, aParams, - aParams[i], java_params[i], methodSig); - } else { - retvalInfo = ¶mInfo; - } - } - NS_ASSERTION(NS_SUCCEEDED(rv), "SetupJavaParams failed"); - } - - // Finish method signature - if (NS_SUCCEEDED(rv)) { - methodSig.Append(')'); - if (retvalInfo) { - nsCAutoString retvalSig; - rv = GetRetvalSig(retvalInfo, aMethodInfo, aMethodIndex, aParams, - retvalSig); - methodSig.Append(retvalSig); - } else { - methodSig.Append('V'); - } - NS_ASSERTION(NS_SUCCEEDED(rv), "GetRetvalSig failed"); - } - - // Get Java method to call - jmethodID mid = nsnull; - if (NS_SUCCEEDED(rv)) { - nsCAutoString methodName; - if (aMethodInfo->IsGetter() || aMethodInfo->IsSetter()) { - if (aMethodInfo->IsGetter()) - methodName.AppendLiteral("get"); - else - methodName.AppendLiteral("set"); - methodName.AppendASCII(aMethodInfo->GetName()); - methodName.SetCharAt(toupper(methodName[3]), 3); - } else { - methodName.AppendASCII(aMethodInfo->GetName()); - methodName.SetCharAt(tolower(methodName[0]), 0); - } - // If it's a Java keyword, then prepend an underscore - if (gJavaKeywords->GetEntry(methodName.get())) { - methodName.Insert('_', 0); - } - - jclass clazz = env->GetObjectClass(javaObject); - if (clazz) - mid = env->GetMethodID(clazz, methodName.get(), methodSig.get()); - NS_ASSERTION(mid, "Failed to get requested method for Java object"); - if (!mid) - rv = NS_ERROR_FAILURE; - } - - // Call method - jvalue retval; - if (NS_SUCCEEDED(rv)) { - if (!retvalInfo) { - env->CallVoidMethodA(javaObject, mid, java_params); - } else { - switch (retvalInfo->GetType().TagPart()) - { - case nsXPTType::T_I8: - retval.b = env->CallByteMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U8: - retval.s = env->CallShortMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U16: - retval.i = env->CallIntMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U32: - retval.j = env->CallLongMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_FLOAT: - retval.f = env->CallFloatMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - retval.d = env->CallDoubleMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_BOOL: - retval.z = env->CallBooleanMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - retval.c = env->CallCharMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - case nsXPTType::T_IID: - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - retval.l = env->CallObjectMethodA(javaObject, mid, java_params); - break; - - case nsXPTType::T_VOID: - retval.j = env->CallLongMethodA(javaObject, mid, java_params); - break; - - default: - NS_WARNING("Unhandled retval type"); - break; - } - } - - // Check for exception from called Java function - jthrowable exp = env->ExceptionOccurred(); - if (exp) { -#ifdef DEBUG - env->ExceptionDescribe(); -#endif - - // If the exception is an instance of XPCOMException, then get the - // nsresult from the exception instance. Else, default to - // NS_ERROR_FAILURE. - if (env->IsInstanceOf(exp, xpcomExceptionClass)) { - jfieldID fid; - fid = env->GetFieldID(xpcomExceptionClass, "errorcode", "J"); - if (fid) { - rv = env->GetLongField(exp, fid); - } else { - rv = NS_ERROR_FAILURE; - } - NS_ASSERTION(fid, "Couldn't get 'errorcode' field of XPCOMException"); - } else { - rv = NS_ERROR_FAILURE; - } - } - } - - // Handle any 'inout', 'out' and 'retval' params - if (NS_SUCCEEDED(rv)) { - for (PRUint8 i = 0; i < paramCount; i++) - { - const nsXPTParamInfo ¶mInfo = aMethodInfo->GetParam(i); - if (paramInfo.IsIn() && !paramInfo.IsOut()) // 'in' - continue; - - // If param is null, then caller is not expecting an output value. - if (aParams[i].val.p == nsnull) - continue; - - if (!paramInfo.IsRetval()) { - rv = FinalizeJavaParams(paramInfo, aMethodInfo, aMethodIndex, aParams, - aParams[i], java_params[i]); - } else { - rv = FinalizeJavaParams(paramInfo, aMethodInfo, aMethodIndex, aParams, - aParams[i], retval); - } - } - NS_ASSERTION(NS_SUCCEEDED(rv), "FinalizeJavaParams/SetXPCOMRetval failed"); - } - - if (java_params) - delete [] java_params; - - LOG(("<--- (Java) %s::%s()\n", ifaceName, aMethodInfo->GetName())); - env->ExceptionClear(); - return rv; -} - -/** - * Handle 'in', 'inout', and 'out' params - */ -nsresult -nsJavaXPTCStub::SetupJavaParams(const nsXPTParamInfo &aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsXPTCMiniVariant &aVariant, jvalue &aJValue, - nsACString &aMethodSig) -{ - nsresult rv = NS_OK; - JNIEnv* env = GetJNIEnv(); - const nsXPTType &type = aParamInfo.GetType(); - - PRUint8 tag = type.TagPart(); - switch (tag) - { - case nsXPTType::T_I8: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.b = aVariant.val.i8; - aMethodSig.Append('B'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jbyteArray array = env->NewByteArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetByteArrayRegion(array, 0, 1, (jbyte*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[B"); - } - } - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U8: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.s = (tag == nsXPTType::T_I16) ? aVariant.val.i16 : - aVariant.val.u8; - aMethodSig.Append('S'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jshortArray array = env->NewShortArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetShortArrayRegion(array, 0, 1, (jshort*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[S"); - } - } - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U16: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.i = (tag == nsXPTType::T_I32) ? aVariant.val.i32 : - aVariant.val.u16; - aMethodSig.Append('I'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jintArray array = env->NewIntArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetIntArrayRegion(array, 0, 1, (jint*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[I"); - } - } - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U32: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.j = (tag == nsXPTType::T_I64) ? aVariant.val.i64 : - aVariant.val.u32; - aMethodSig.Append('J'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jlongArray array = env->NewLongArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetLongArrayRegion(array, 0, 1, (jlong*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[J"); - } - } - break; - - case nsXPTType::T_FLOAT: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.f = aVariant.val.f; - aMethodSig.Append('F'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jfloatArray array = env->NewFloatArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetFloatArrayRegion(array, 0, 1, (jfloat*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[F"); - } - } - break; - - // XXX how do we handle unsigned 64-bit values? - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.d = (tag == nsXPTType::T_DOUBLE) ? aVariant.val.d : - aVariant.val.u64; - aMethodSig.Append('D'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jdoubleArray array = env->NewDoubleArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetDoubleArrayRegion(array, 0, 1, (jdouble*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[D"); - } - } - break; - - case nsXPTType::T_BOOL: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.z = aVariant.val.b; - aMethodSig.Append('Z'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jbooleanArray array = env->NewBooleanArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetBooleanArrayRegion(array, 0, 1, (jboolean*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[Z"); - } - } - break; - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - { - if (!aParamInfo.IsOut()) { // 'in' - if (tag == nsXPTType::T_CHAR) - aJValue.c = aVariant.val.c; - else - aJValue.c = aVariant.val.wc; - aMethodSig.Append('C'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jcharArray array = env->NewCharArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetCharArrayRegion(array, 0, 1, (jchar*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[C"); - } - } - break; - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - { - void* ptr = nsnull; - if (!aParamInfo.IsOut()) { // 'in' - ptr = aVariant.val.p; - } else if (aVariant.val.p) { // 'inout' & 'out' - void** variant = NS_STATIC_CAST(void**, aVariant.val.p); - ptr = *variant; - } - - jobject str; - if (ptr) { - if (tag == nsXPTType::T_CHAR_STR) { - str = env->NewStringUTF((const char*) ptr); - } else { - const PRUnichar* buf = (const PRUnichar*) ptr; - str = env->NewString(buf, nsCRT::strlen(buf)); - } - if (!str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - str = nsnull; - } - - if (!aParamInfo.IsOut()) { // 'in' - aJValue.l = str; - aMethodSig.AppendLiteral("Ljava/lang/String;"); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - aJValue.l = env->NewObjectArray(1, stringClass, str); - if (aJValue.l == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[Ljava/lang/String;"); - } - } - break; - - case nsXPTType::T_IID: - { - nsID* iid = nsnull; - if (!aParamInfo.IsOut()) { // 'in' - iid = NS_STATIC_CAST(nsID*, aVariant.val.p); - } else if (aVariant.val.p) { // 'inout' & 'out' - nsID** variant = NS_STATIC_CAST(nsID**, aVariant.val.p); - iid = *variant; - } - - jobject str = nsnull; - if (iid) { - char* iid_str = iid->ToString(); - if (iid_str) { - str = env->NewStringUTF(iid_str); - } - if (!iid_str || !str) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - PR_Free(iid_str); - } - - if (!aParamInfo.IsOut()) { // 'in' - aJValue.l = str; - aMethodSig.AppendLiteral("Ljava/lang/String;"); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - aJValue.l = env->NewObjectArray(1, stringClass, str); - if (aJValue.l == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[Ljava/lang/String;"); - } - } - break; - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - { - nsISupports* xpcom_obj = nsnull; - if (!aParamInfo.IsOut()) { // 'in' - xpcom_obj = NS_STATIC_CAST(nsISupports*, aVariant.val.p); - } else if (aVariant.val.p) { // 'inout' & 'out' - nsISupports** variant = NS_STATIC_CAST(nsISupports**, aVariant.val.p); - xpcom_obj = *variant; - } - - nsID iid; - rv = GetIIDForMethodParam(mIInfo, aMethodInfo, aParamInfo, - aParamInfo.GetType().TagPart(), aMethodIndex, - aDispatchParams, PR_FALSE, iid); - if (NS_FAILED(rv)) - break; - - // get name of interface - char* iface_name = nsnull; - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv)); - if (NS_FAILED(rv)) - break; - - rv = iim->GetNameForIID(&iid, &iface_name); - if (NS_FAILED(rv) || !iface_name) - break; - - jobject java_stub = nsnull; - if (xpcom_obj) { - // Get matching Java object for given xpcom object - rv = GetNewOrUsedJavaObject(env, xpcom_obj, iid, &java_stub); - if (NS_FAILED(rv)) - break; - } - - if (!aParamInfo.IsOut()) { // 'in' - aJValue.l = java_stub; - } else { // 'inout' & 'out' - if (aVariant.val.p) { - aJValue.l = env->NewObjectArray(1, nsISupportsClass, java_stub); - if (aJValue.l == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - aJValue.l = nsnull; - } - aMethodSig.Append('['); - } - - if (tag != nsXPTType::T_INTERFACE_IS) { - aMethodSig.AppendLiteral("Lorg/mozilla/xpcom/"); - aMethodSig.AppendASCII(iface_name); - aMethodSig.Append(';'); - } else { - aMethodSig.AppendLiteral("Lorg/mozilla/xpcom/nsISupports;"); - } - - nsMemory::Free(iface_name); - } - break; - - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - { - nsString* str = nsnull; - if (!aParamInfo.IsOut()) { // 'in' - str = NS_STATIC_CAST(nsString*, aVariant.val.p); - } else if (aVariant.val.p) { // 'inout' & 'out' - nsString** variant = NS_STATIC_CAST(nsString**, aVariant.val.p); - str = *variant; - } - - jstring jstr; - if (str) { - jstr = env->NewString(str->get(), str->Length()); - if (!jstr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - jstr = nsnull; - } - - if (!aParamInfo.IsOut()) { // 'in' - aJValue.l = jstr; - aMethodSig.AppendLiteral("Ljava/lang/String;"); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - aJValue.l = env->NewObjectArray(1, stringClass, jstr); - if (aJValue.l == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[Ljava/lang/String;"); - } - } - break; - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - { - nsCString* str = nsnull; - if (!aParamInfo.IsOut()) { // 'in' - str = NS_STATIC_CAST(nsCString*, aVariant.val.p); - } else if (aVariant.val.p) { // 'inout' & 'out' - nsCString** variant = NS_STATIC_CAST(nsCString**, aVariant.val.p); - str = *variant; - } - - jstring jstr; - if (str) { - jstr = env->NewStringUTF(str->get()); - if (!jstr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - jstr = nsnull; - } - - if (!aParamInfo.IsOut()) { // 'in' - aJValue.l = jstr; - aMethodSig.AppendLiteral("Ljava/lang/String;"); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - aJValue.l = env->NewObjectArray(1, stringClass, jstr); - if (aJValue.l == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[Ljava/lang/String;"); - } - } - break; - - // Pass the 'void*' address as a long - case nsXPTType::T_VOID: - { - if (!aParamInfo.IsOut()) { // 'in' - aJValue.j = NS_REINTERPRET_CAST(jlong, aVariant.val.p); - aMethodSig.Append('J'); - } else { // 'inout' & 'out' - if (aVariant.val.p) { - jlongArray array = env->NewLongArray(1); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - env->SetLongArrayRegion(array, 0, 1, (jlong*) aVariant.val.p); - aJValue.l = array; - } else { - aJValue.l = nsnull; - } - aMethodSig.AppendLiteral("[J"); - } - } - break; - - case nsXPTType::T_ARRAY: - NS_WARNING("array types are not yet supported"); - return NS_ERROR_NOT_IMPLEMENTED; - break; - - case nsXPTType::T_PSTRING_SIZE_IS: - case nsXPTType::T_PWSTRING_SIZE_IS: - default: - NS_WARNING("unexpected parameter type"); - return NS_ERROR_UNEXPECTED; - } - - return rv; -} - -nsresult -nsJavaXPTCStub::GetRetvalSig(const nsXPTParamInfo* aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsACString &aRetvalSig) -{ - PRUint8 type = aParamInfo->GetType().TagPart(); - switch (type) - { - case nsXPTType::T_I8: - aRetvalSig.Append('B'); - break; - - case nsXPTType::T_I16: - case nsXPTType::T_U8: - aRetvalSig.Append('S'); - break; - - case nsXPTType::T_I32: - case nsXPTType::T_U16: - aRetvalSig.Append('I'); - break; - - case nsXPTType::T_I64: - case nsXPTType::T_U32: - aRetvalSig.Append('J'); - break; - - case nsXPTType::T_FLOAT: - aRetvalSig.Append('F'); - break; - - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - aRetvalSig.Append('D'); - break; - - case nsXPTType::T_BOOL: - aRetvalSig.Append('Z'); - break; - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - aRetvalSig.Append('C'); - break; - - case nsXPTType::T_CHAR_STR: - case nsXPTType::T_WCHAR_STR: - case nsXPTType::T_IID: - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - aRetvalSig.AppendLiteral("Ljava/lang/String;"); - break; - - case nsXPTType::T_INTERFACE: - { - nsID iid; - nsresult rv = GetIIDForMethodParam(mIInfo, aMethodInfo, *aParamInfo, type, - aMethodIndex, aDispatchParams, - PR_FALSE, iid); - if (NS_FAILED(rv)) - break; - - // get name of interface - char* iface_name = nsnull; - nsCOMPtr - iim(do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv)); - if (NS_FAILED(rv)) - break; - - rv = iim->GetNameForIID(&iid, &iface_name); - if (NS_FAILED(rv) || !iface_name) - break; - - aRetvalSig.AppendLiteral("Lorg/mozilla/xpcom/"); - aRetvalSig.AppendASCII(iface_name); - aRetvalSig.Append(';'); - nsMemory::Free(iface_name); - break; - } - - case nsXPTType::T_INTERFACE_IS: - aRetvalSig.AppendLiteral("Lorg/mozilla/xpcom/nsISupports;"); - break; - - case nsXPTType::T_VOID: - aRetvalSig.Append('J'); - break; - - case nsXPTType::T_ARRAY: - NS_WARNING("array types are not yet supported"); - return NS_ERROR_NOT_IMPLEMENTED; - break; - - case nsXPTType::T_PSTRING_SIZE_IS: - case nsXPTType::T_PWSTRING_SIZE_IS: - default: - NS_WARNING("unexpected parameter type"); - return NS_ERROR_UNEXPECTED; - } - - return NS_OK; -} - -/** - * Handle 'inout', 'out', and 'retval' params - */ -nsresult -nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsXPTCMiniVariant &aVariant, jvalue &aJValue) -{ - nsresult rv = NS_OK; - JNIEnv* env = GetJNIEnv(); - const nsXPTType &type = aParamInfo.GetType(); - - PRUint8 tag = type.TagPart(); - switch (tag) - { - case nsXPTType::T_I8: - { - jbyte value; - if (aParamInfo.IsRetval()) { // 'retval' - value = aJValue.b; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetByteArrayRegion((jbyteArray) aJValue.l, 0, 1, &value); - } - if (aVariant.val.p) - *((PRInt8 *) aVariant.val.p) = value; - } - break; - - case nsXPTType::T_U8: - case nsXPTType::T_I16: - { - jshort value = 0; - if (aParamInfo.IsRetval()) { // 'retval' - value = aJValue.s; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetShortArrayRegion((jshortArray) aJValue.l, 0, 1, &value); - } - - if (aVariant.val.p) { - if (tag == nsXPTType::T_U8) - *((PRUint8 *) aVariant.val.p) = value; - else - *((PRInt16 *) aVariant.val.p) = value; - } - } - break; - - case nsXPTType::T_U16: - case nsXPTType::T_I32: - { - jint value = 0; - if (aParamInfo.IsRetval()) { // 'retval' - value = aJValue.i; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetIntArrayRegion((jintArray) aJValue.l, 0, 1, &value); - } - - if (aVariant.val.p) { - if (tag == nsXPTType::T_U16) - *((PRUint16 *) aVariant.val.p) = value; - else - *((PRInt32 *) aVariant.val.p) = value; - } - } - break; - - case nsXPTType::T_U32: - case nsXPTType::T_I64: - { - jlong value = 0; - if (aParamInfo.IsRetval()) { // 'retval' - value = aJValue.j; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetLongArrayRegion((jlongArray) aJValue.l, 0, 1, &value); - } - - if (aVariant.val.p) { - if (tag == nsXPTType::T_U32) - *((PRUint32 *) aVariant.val.p) = value; - else - *((PRInt64 *) aVariant.val.p) = value; - } - } - break; - - case nsXPTType::T_FLOAT: - { - if (aParamInfo.IsRetval()) { // 'retval' - *((float *) aVariant.val.p) = aJValue.f; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetFloatArrayRegion((jfloatArray) aJValue.l, 0, 1, - (jfloat*) aVariant.val.p); - } - } - break; - - // XXX how do we handle 64-bit values? - case nsXPTType::T_U64: - case nsXPTType::T_DOUBLE: - { - jdouble value = 0; - if (aParamInfo.IsRetval()) { // 'retval' - value = aJValue.d; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetDoubleArrayRegion((jdoubleArray) aJValue.l, 0, 1, &value); - } - - if (aVariant.val.p) { - if (tag == nsXPTType::T_DOUBLE) - *((double *) aVariant.val.p) = value; - else - *((PRUint64 *) aVariant.val.p) = NS_STATIC_CAST(PRUint64, value); - } - } - break; - - case nsXPTType::T_BOOL: - { - if (aParamInfo.IsRetval()) { // 'retval' - *((PRBool *) aVariant.val.p) = aJValue.z; - } else if (aJValue.l) { // 'inout' & 'out' - env->GetBooleanArrayRegion((jbooleanArray) aJValue.l, 0, 1, - (jboolean*) aVariant.val.p); - } - } - break; - - case nsXPTType::T_CHAR: - case nsXPTType::T_WCHAR: - { - if (aParamInfo.IsRetval()) { // 'retval' - if (type.TagPart() == nsXPTType::T_CHAR) - *((char *) aVariant.val.p) = aJValue.c; - else - *((PRUnichar *) aVariant.val.p) = aJValue.c; - } else if (aJValue.l) { // 'inout' & 'out' - jchar* array = env->GetCharArrayElements((jcharArray) aJValue.l, - nsnull); - if (!array) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - if (type.TagPart() == nsXPTType::T_CHAR) - *((char *) aVariant.val.p) = array[0]; - else - *((PRUnichar *) aVariant.val.p) = array[0]; - - env->ReleaseCharArrayElements((jcharArray) aJValue.l, array, JNI_ABORT); - } - } - break; - - case nsXPTType::T_CHAR_STR: - { - jstring str = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - str = (jstring) aJValue.l; - } else { // 'inout' & 'out' - str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - char** variant = NS_STATIC_CAST(char**, aVariant.val.p); - if (str) { - // Get string buffer - const char* char_ptr = env->GetStringUTFChars(str, nsnull); - if (!char_ptr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - // If new string is different from one passed in, free old string - // and replace with new string. - if (aParamInfo.IsRetval() || - *variant == nsnull || strcmp(*variant, char_ptr) != 0) - { - if (!aParamInfo.IsRetval() && *variant) - PR_Free(*variant); - - *variant = strdup(char_ptr); - if (*variant == nsnull) { - rv = NS_ERROR_OUT_OF_MEMORY; - // don't 'break'; fall through to release chars - } - } - - // Release string buffer - env->ReleaseStringUTFChars(str, char_ptr); - } else { - // If we were passed in a string, delete it now, and set to null. - // (Only for 'inout' & 'out' params) - if (*variant && !aParamInfo.IsRetval()) { - PR_Free(*variant); - } - *variant = nsnull; - } - } - break; - - case nsXPTType::T_WCHAR_STR: - { - jstring str = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - str = (jstring) aJValue.l; - } else { // 'inout' & 'out' - str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - PRUnichar** variant = NS_STATIC_CAST(PRUnichar**, aVariant.val.p); - if (str) { - // Get string buffer - const jchar* wchar_ptr = env->GetStringChars(str, nsnull); - if (!wchar_ptr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - // If new string is different from one passed in, free old string - // and replace with new string. We - if (aParamInfo.IsRetval() || - *variant == nsnull || nsCRT::strcmp(*variant, wchar_ptr) != 0) - { - if (!aParamInfo.IsRetval() && *variant) - PR_Free(*variant); - - PRUint32 length = nsCRT::strlen(wchar_ptr); - *variant = (PRUnichar*) PR_Malloc((length + 1) * sizeof(PRUnichar)); - if (*variant) { - memcpy(*variant, wchar_ptr, length * sizeof(PRUnichar)); - (*variant)[length] = 0; - } else { - rv = NS_ERROR_OUT_OF_MEMORY; - // don't 'break'; fall through to release chars - } - } - - // Release string buffer - env->ReleaseStringChars(str, wchar_ptr); - } else { - // If we were passed in a string, delete it now, and set to null. - // (Only for 'inout' & 'out' params) - if (*variant && !aParamInfo.IsRetval()) { - PR_Free(*variant); - } - *variant = nsnull; - } - } - break; - - case nsXPTType::T_IID: - { - jstring str = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - str = (jstring) aJValue.l; - } else { // 'inout' & 'out' - str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - nsID** variant = NS_STATIC_CAST(nsID**, aVariant.val.p); - if (str) { - // Get string buffer - const char* char_ptr = env->GetStringUTFChars(str, nsnull); - if (!char_ptr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - if (!aParamInfo.IsRetval() && *variant) { - // If we were given an nsID, set it to the new string - nsID* oldIID = *variant; - oldIID->Parse(char_ptr); - } else { - // If the argument that was passed in was null, then we need to - // create a new nsID. - nsID* newIID = new nsID; - if (newIID) { - newIID->Parse(char_ptr); - *variant = newIID; - } else { - rv = NS_ERROR_OUT_OF_MEMORY; - // don't 'break'; fall through to release chars - } - } - - // Release string buffer - env->ReleaseStringUTFChars(str, char_ptr); - } else { - // If we were passed in an nsID, delete it now, and set to null. - // (Free only 'inout' & 'out' params) - if (*variant && !aParamInfo.IsRetval()) { - delete *variant; - } - *variant = nsnull; - } - } - break; - - case nsXPTType::T_INTERFACE: - case nsXPTType::T_INTERFACE_IS: - { - jobject java_obj = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - java_obj = aJValue.l; - } else if (aJValue.l) { // 'inout' & 'out' - java_obj = env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - nsISupports* xpcom_obj = nsnull; - if (java_obj) { - // Get IID for this param - nsID iid; - rv = GetIIDForMethodParam(mIInfo, aMethodInfo, aParamInfo, - aParamInfo.GetType().TagPart(), aMethodIndex, - aDispatchParams, PR_FALSE, iid); - if (NS_FAILED(rv)) - break; - - // If the requested interface is nsIWeakReference, then we look for or - // create a stub for the nsISupports interface. Then we create a weak - // reference from that stub. - PRBool isWeakRef; - if (iid.Equals(NS_GET_IID(nsIWeakReference))) { - isWeakRef = PR_TRUE; - iid = NS_GET_IID(nsISupports); - } else { - isWeakRef = PR_FALSE; - } - - rv = GetNewOrUsedXPCOMObject(env, java_obj, iid, &xpcom_obj); - if (NS_FAILED(rv)) - break; - - // If the function expects a weak reference, then we need to - // create it here. - if (isWeakRef) { - nsCOMPtr supportsweak = - do_QueryInterface(xpcom_obj); - if (supportsweak) { - nsWeakPtr weakref; - supportsweak->GetWeakReference(getter_AddRefs(weakref)); - NS_RELEASE(xpcom_obj); - xpcom_obj = weakref; - NS_ADDREF(xpcom_obj); - } else { - xpcom_obj = nsnull; - } - } - } - - // For 'inout' params, if the resulting xpcom value is different than the - // one passed in, then we must release the incoming xpcom value. - nsISupports** variant = NS_STATIC_CAST(nsISupports**, aVariant.val.p); - if (aParamInfo.IsIn() && *variant) { - nsCOMPtr in = do_QueryInterface(*variant); - nsCOMPtr out = do_QueryInterface(xpcom_obj); - if (in != out) { - NS_RELEASE(*variant); - } - } - - *variant = xpcom_obj; - } - break; - - case nsXPTType::T_ASTRING: - case nsXPTType::T_DOMSTRING: - { - jstring str = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - str = (jstring) aJValue.l; - } else { // 'inout' & 'out' - str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - nsString** variant = NS_STATIC_CAST(nsString**, aVariant.val.p); - if (str) { - // Get string buffer - const jchar* wchar_ptr = env->GetStringChars(str, nsnull); - if (!wchar_ptr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - if (!aParamInfo.IsRetval() && *variant) { - // If we were given an nsString, set it to the new string - nsString* string = *variant; - string->Assign(wchar_ptr); - } else { - // If the argument that was passed in was null, then we need to - // create a new string. - nsString* embedStr = new nsString(wchar_ptr); - if (embedStr) { - *variant = embedStr; - } else { - rv = NS_ERROR_OUT_OF_MEMORY; - // don't 'break'; fall through to release chars - } - } - - // release String buffer - env->ReleaseStringChars(str, wchar_ptr); - } else { - // If we were passed in a string, delete it now, and set to null. - // (Free only 'inout' & 'out' params) - if (*variant && !aParamInfo.IsRetval()) { - delete *variant; - } - *variant = nsnull; - } - } - break; - - case nsXPTType::T_UTF8STRING: - case nsXPTType::T_CSTRING: - { - jstring str = nsnull; - if (aParamInfo.IsRetval()) { // 'retval' - str = (jstring) aJValue.l; - } else { // 'inout' & 'out' - str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0); - } - - nsCString** variant = NS_STATIC_CAST(nsCString**, aVariant.val.p); - if (str) { - // Get string buffer - const char* char_ptr = env->GetStringUTFChars(str, nsnull); - if (!char_ptr) { - rv = NS_ERROR_OUT_OF_MEMORY; - break; - } - - if (!aParamInfo.IsRetval() && *variant) { - // If we were given an nsString, set it to the new string - nsCString* string = *variant; - string->Assign(char_ptr); - } else { - // If the argument that was passed in was null, then we need to - // create a new nsID. - nsCString* embedStr = new nsCString(char_ptr); - if (embedStr) { - *variant = embedStr; - } else { - rv = NS_ERROR_OUT_OF_MEMORY; - // don't 'break'; fall through to release chars - } - } - - // release String buffer - env->ReleaseStringUTFChars(str, char_ptr); - } else { - // If we were passed in a string, delete it now, and set to null. - // (Free only 'inout' & 'out' params) - if (*variant && !aParamInfo.IsRetval()) { - delete *variant; - } - *variant = nsnull; - } - } - break; - - case nsXPTType::T_VOID: - { - if (aParamInfo.IsRetval()) { // 'retval' - aVariant.val.p = NS_REINTERPRET_CAST(void*, aJValue.j); - } else if (aJValue.l) { // 'inout' & 'out' - env->GetLongArrayRegion((jlongArray) aJValue.l, 0, 1, - (jlong*) aVariant.val.p); - } - } - break; - - default: - NS_WARNING("unexpected parameter type"); - return NS_ERROR_UNEXPECTED; - } - - return rv; -} - -NS_IMETHODIMP -nsJavaXPTCStub::GetWeakReference(nsIWeakReference** aInstancePtr) -{ - if (mMaster) - return mMaster->GetWeakReference(aInstancePtr); - - LOG(("==> nsJavaXPTCStub::GetWeakReference()\n")); - - if (!aInstancePtr) - return NS_ERROR_NULL_POINTER; - - jobject javaObject = GetJNIEnv()->CallObjectMethod(mJavaWeakRef, - getReferentMID); - nsJavaXPTCStubWeakRef* weakref; - weakref = new nsJavaXPTCStubWeakRef(javaObject, this); - if (!weakref) - return NS_ERROR_OUT_OF_MEMORY; - - *aInstancePtr = weakref; - NS_ADDREF(*aInstancePtr); - ++mWeakRefCnt; - - return NS_OK; -} - -jobject -nsJavaXPTCStub::GetJavaObject() -{ - JNIEnv* env = GetJNIEnv(); - jobject javaObject = env->CallObjectMethod(mJavaWeakRef, getReferentMID); - -#ifdef DEBUG_JAVAXPCOM - nsIID* iid; - mIInfo->GetInterfaceIID(&iid); - char* iid_str = iid->ToString(); - LOG(("< nsJavaXPTCStub (Java=%08x | XPCOM=%08x | IID=%s)\n", - (PRUint32) mJavaRefHashCode, (PRUint32) this, iid_str)); - PR_Free(iid_str); - nsMemory::Free(iid); -#endif - - return javaObject; -} diff --git a/mozilla/extensions/java/xpcom/nsJavaXPTCStub.h b/mozilla/extensions/java/xpcom/nsJavaXPTCStub.h deleted file mode 100644 index 711d0298ff8..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPTCStub.h +++ /dev/null @@ -1,135 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef _nsJavaXPTCStub_h_ -#define _nsJavaXPTCStub_h_ - -#include "xptcall.h" -#include "jni.h" -#include "nsVoidArray.h" -#include "nsIInterfaceInfo.h" -#include "nsCOMPtr.h" -#include "nsWeakReference.h" -#include "nsJavaXPTCStubWeakRef.h" - - -#define NS_JAVAXPTCSTUB_IID \ -{0x88dd8130, 0xebe6, 0x4431, {0x9d, 0xa7, 0xe6, 0xb7, 0x54, 0x74, 0xfb, 0x21}} - -class nsJavaXPTCStub : public nsXPTCStubBase, - public nsSupportsWeakReference -{ - friend class nsJavaXPTCStubWeakRef; - -public: - NS_DECL_ISUPPORTS - NS_DECL_NSISUPPORTSWEAKREFERENCE - NS_DECLARE_STATIC_IID_ACCESSOR(NS_JAVAXPTCSTUB_IID) - - nsJavaXPTCStub(jobject aJavaObject, nsIInterfaceInfo *aIInfo); - - virtual ~nsJavaXPTCStub(); - - // return a refcounted pointer to the InterfaceInfo for this object - // NOTE: on some platforms this MUST not fail or we crash! - NS_IMETHOD GetInterfaceInfo(nsIInterfaceInfo **aInfo); - - // call this method and return result - NS_IMETHOD CallMethod(PRUint16 aMethodIndex, - const nsXPTMethodInfo *aInfo, - nsXPTCMiniVariant *aParams); - - // getter for mJavaObject - jobject GetJavaObject(); - - // Deletes the strong global ref for the Java object, so it can be garbage - // collected if necessary. See DestroyXPTCMappingEnum(). - void DeleteStrongRef(); - -private: - NS_IMETHOD_(nsrefcnt) AddRefInternal(); - NS_IMETHOD_(nsrefcnt) ReleaseInternal(); - - // Deletes this object and its members. Called by ReleaseInternal() and - // ReleaseWeakRef(). - void Destroy(); - - // When a nsJavaXPTCStubWeakRef associated with this object is released, it - // calls this function to let this object know that there is one less weak - // ref. If there are no more weakrefs referencing this object, and no one - // holds a strong ref, then this function takes care of deleting the object. - void ReleaseWeakRef(); - - // returns a weak reference to a child supporting the specified interface - nsJavaXPTCStub * FindStubSupportingIID(const nsID &aIID); - - // returns true if this stub supports the specified interface - PRBool SupportsIID(const nsID &aIID); - - nsresult SetupJavaParams(const nsXPTParamInfo &aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsXPTCMiniVariant &aVariant, - jvalue &aJValue, nsACString &aMethodSig); - nsresult GetRetvalSig(const nsXPTParamInfo* aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsACString &aRetvalSig); - nsresult FinalizeJavaParams(const nsXPTParamInfo &aParamInfo, - const nsXPTMethodInfo* aMethodInfo, - PRUint16 aMethodIndex, - nsXPTCMiniVariant* aDispatchParams, - nsXPTCMiniVariant &aVariant, - jvalue &aJValue); - nsresult SetXPCOMRetval(); - - jobject mJavaWeakRef; - jobject mJavaStrongRef; - jint mJavaRefHashCode; - nsCOMPtr mIInfo; - - nsVoidArray mChildren; // weak references (cleared by the children) - nsJavaXPTCStub *mMaster; // strong reference - - nsAutoRefCnt mWeakRefCnt; // count for number of associated weak refs -}; - -NS_DEFINE_STATIC_IID_ACCESSOR(nsJavaXPTCStub, NS_JAVAXPTCSTUB_IID) - -#endif // _nsJavaXPTCStub_h_ diff --git a/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.cpp b/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.cpp deleted file mode 100644 index cb98fc9bd78..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#include "jni.h" -#include "nsJavaXPTCStubWeakRef.h" -#include "nsJavaXPTCStub.h" -#include "nsJavaXPCOMBindingUtils.h" -#include "nsIInterfaceInfoManager.h" - - -/** - * How we handle XPCOM weak references to a Java object: - * - * If XPCOM requires or asks for a weak reference of a Java object, we first - * find (or create) an nsJavaXPTCStub for that Java object. That way, there is - * always an nsJavaXPTCStub for any nsJavaXPTCStubWeakRef. However, the - * XPTCStub may not always be 'valid'; that is, its refcount may be zero if - * is not currently referenced by any XPCOM class. - * When an XPCOM method queries the referent from the weak reference, the - * weak ref checks first whether the Java object is still valid. If so, we can - * immediately return an addref'd nsJavaXPTCStub. The XPTCStub takes care of - * finding an XPTCStub for the required IID. - */ - -nsJavaXPTCStubWeakRef::nsJavaXPTCStubWeakRef(jobject aJavaObject, - nsJavaXPTCStub* aXPTCStub) - : mXPTCStub(aXPTCStub) -{ - JNIEnv* env = GetJNIEnv(); - jobject weakref = env->NewObject(weakReferenceClass, - weakReferenceConstructorMID, aJavaObject); - mWeakRef = env->NewGlobalRef(weakref); -} - -nsJavaXPTCStubWeakRef::~nsJavaXPTCStubWeakRef() -{ - JNIEnv* env = GetJNIEnv(); - env->CallVoidMethod(mWeakRef, clearReferentMID); - env->DeleteGlobalRef(mWeakRef); - mXPTCStub->ReleaseWeakRef(); -} - -NS_IMPL_ADDREF(nsJavaXPTCStubWeakRef) -NS_IMPL_RELEASE(nsJavaXPTCStubWeakRef) - -NS_IMPL_QUERY_INTERFACE1(nsJavaXPTCStubWeakRef, nsIWeakReference) - -NS_IMETHODIMP -nsJavaXPTCStubWeakRef::QueryReferent(const nsIID& aIID, void** aInstancePtr) -{ - LOG(("nsJavaXPTCStubWeakRef::QueryReferent()\n")); - - // Is weak ref still valid? - // We create a strong local ref to make sure Java object isn't garbage - // collected during this call. - JNIEnv* env = GetJNIEnv(); - jobject javaObject = env->CallObjectMethod(mWeakRef, getReferentMID); - if (env->IsSameObject(javaObject, NULL)) - return NS_ERROR_NULL_POINTER; - - // Java object has not been garbage collected, so return QI from XPTCStub. - return mXPTCStub->QueryInterface(aIID, aInstancePtr); -} - diff --git a/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.h b/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.h deleted file mode 100644 index b538eecb4c2..00000000000 --- a/mozilla/extensions/java/xpcom/nsJavaXPTCStubWeakRef.h +++ /dev/null @@ -1,63 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is - * IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2005 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef _nsJavaXPTCStubWeakRef_h_ -#define _nsJavaXPTCStubWeakRef_h_ - -#include "jni.h" -#include "nsIWeakReference.h" - - -class nsJavaXPTCStub; - -/** - * This class represents an XPCOM weak reference to a Java object. - */ -class nsJavaXPTCStubWeakRef : public nsIWeakReference -{ -public: - nsJavaXPTCStubWeakRef(jobject aJavaObject, nsJavaXPTCStub* aXPTCStub); - virtual ~nsJavaXPTCStubWeakRef(); - NS_DECL_ISUPPORTS - NS_DECL_NSIWEAKREFERENCE - -protected: - jobject mWeakRef; - nsJavaXPTCStub* mXPTCStub; -}; - -#endif // _nsJavaXPTCStubWeakRef_h_ diff --git a/mozilla/extensions/java/xpcom/src/GREImpl.java b/mozilla/extensions/java/xpcom/src/GREImpl.java deleted file mode 100644 index 5955287234d..00000000000 --- a/mozilla/extensions/java/xpcom/src/GREImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2006 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom.internal; - -import java.io.*; -import org.mozilla.xpcom.*; - - -public class GREImpl extends JavaXPCOMMethods implements IGRE { - - public void initEmbedding(File aLibXULDirectory, File aAppDirectory, - IAppFileLocProvider aAppDirProvider) { - registerJavaXPCOMMethods(aLibXULDirectory); - initEmbeddingNative(aLibXULDirectory, aAppDirectory, aAppDirProvider); - } - - public native void initEmbeddingNative(File aLibXULDirectory, - File aAppDirectory, IAppFileLocProvider aAppDirProvider); - - public native void termEmbedding(); - - public native nsISupports lockProfileDirectory(File aDirectory); - - public native void notifyProfile(); - -} - diff --git a/mozilla/extensions/java/xpcom/src/JavaXPCOMMethods.java b/mozilla/extensions/java/xpcom/src/JavaXPCOMMethods.java deleted file mode 100644 index 27d525c10f0..00000000000 --- a/mozilla/extensions/java/xpcom/src/JavaXPCOMMethods.java +++ /dev/null @@ -1,58 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2006 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom.internal; - -import java.io.File; - - -public class JavaXPCOMMethods { - - public static void registerJavaXPCOMMethods(File aLibXULDirectory) { - // load JNI library - String path = ""; - if (aLibXULDirectory != null) { - path = aLibXULDirectory + File.separator; - } - System.load(path + System.mapLibraryName("javaxpcomglue")); - - registerJavaXPCOMMethodsNative(aLibXULDirectory); - } - - public static native void - registerJavaXPCOMMethodsNative(File aLibXULDirectory); -} - diff --git a/mozilla/extensions/java/xpcom/src/XPCOMImpl.java b/mozilla/extensions/java/xpcom/src/XPCOMImpl.java deleted file mode 100644 index 633d53f5e95..00000000000 --- a/mozilla/extensions/java/xpcom/src/XPCOMImpl.java +++ /dev/null @@ -1,65 +0,0 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Java XPCOM Bindings. - * - * The Initial Developer of the Original Code is IBM Corporation. - * Portions created by the Initial Developer are Copyright (C) 2006 - * IBM Corporation. All Rights Reserved. - * - * Contributor(s): - * Javier Pedemonte (jhpedemonte@gmail.com) - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -package org.mozilla.xpcom.internal; - -import java.io.*; -import org.mozilla.xpcom.*; - - -public class XPCOMImpl extends JavaXPCOMMethods implements IXPCOM { - - public nsIServiceManager initXPCOM(File aMozBinDirectory, - IAppFileLocProvider aAppFileLocProvider) { - registerJavaXPCOMMethods(aMozBinDirectory); - return initXPCOMNative(aMozBinDirectory, aAppFileLocProvider); - } - - public native nsIServiceManager initXPCOMNative(File aMozBinDirectory, - IAppFileLocProvider aAppFileLocProvider); - - public native void shutdownXPCOM(nsIServiceManager aServMgr); - - public native nsIComponentManager getComponentManager(); - - public native nsIComponentRegistrar getComponentRegistrar(); - - public native nsIServiceManager getServiceManager(); - - public native nsILocalFile newLocalFile(String aPath, boolean aFollowLinks); - -} -