Initial drop. Not part of default build.

This commit is contained in:
pedemont%us.ibm.com 2004-09-02 16:34:11 +00:00
parent e757042645
commit d23fc1338b
9 changed files with 3771 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/* ***** 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;
public final class GeckoEmbed {
// XPCOM Utility functions
public static native void NS_InitEmbedding(String aMozBinDirectory, int aAppFileLocProvider);
public static native void NS_TermEmbedding();
public static native nsILocalFile NS_NewLocalFile(String aPath, boolean followLinks);
public static native nsIComponentManager NS_GetComponentManager();
public static native nsIServiceManager NS_GetServiceManager();
public static native nsISimpleEnumerator NS_NewSingletonEnumerator(nsISupports aSingleton);
// Calling functions used by Java stub classes
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);
public static native int nsWriteSegmentFun(int ptr, Object aInStream, int aClosure, byte[] aFromSegment, int aToOffset, int aCount);
}

View File

@ -0,0 +1,169 @@
# ***** 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
MODULE = javaembed
LIBRARY_NAME = javaembed
EXPORT_LIBRARY = 1
EMBED_JAR = mozembed.jar
EMBED_JAR_SRC = $(patsubst %.jar,%-src.jar,$(EMBED_JAR))
REQUIRES = xpcom \
string \
embed_base \
nspr \
gfx \
$(NULL)
CPPSRCS = \
nsJavaInterfaces.cpp \
nsJavaWrapper.cpp \
nsJavaXPTCStub.cpp \
nsJavaXPCOMBindingUtils.cpp \
$(NULL)
JAVA_LOC = /opt/IBMJava2-141
LOCAL_INCLUDES = \
-I$(JAVA_LOC)/include \
$(NULL)
EXTRA_DSO_LIBS = \
embed_base_s \
xpcomglue_s \
xpcom \
$(NULL)
EXTRA_DSO_LDOPTS = \
$(LIBS_DIR) \
$(EXTRA_DSO_LIBS) \
$(NULL)
IDL_FILES := $(filter-out nsrootidl.idl,$(wildcard $(DIST)/idl/*))
JAVA_IFACES = $(addprefix $(XPIDL_GEN_DIR)/org/mozilla/xpcom/,$($(notdir $(IDL_FILES)):.idl=.java))
JAVA_STUBS = $(patsubst %.idl,$(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/%_Stub.java,$(notdir $(IDL_FILES)))
_JAVA_IFACE_CLASSES = $(patsubst %.idl,org/mozilla/xpcom/%.class,$(notdir $(IDL_FILES)))
_JAVA_STUB_CLASSES = $(patsubst %.idl,org/mozilla/xpcom/stubs/%_Stub.class,$(notdir $(IDL_FILES)))
_IGNORE_IFACES = domstubs
IGNORE_IFACES = $(patsubst %,org/mozilla/xpcom/%.class,$(_IGNORE_IFACES))
JAVA_IFACE_CLASSES = $(filter-out $(IGNORE_IFACES),$(_JAVA_IFACE_CLASSES))
_IGNORE_STUBS = domstubs gfxIFormats
IGNORE_STUBS = $(patsubst %,org/mozilla/xpcom/stubs/%_Stub.class,$(_IGNORE_STUBS))
JAVA_STUB_CLASSES = $(filter-out $(IGNORE_STUBS),$(_JAVA_STUB_CLASSES))
GARBAGE = $(EMBED_JAR) $(EMBED_JAR_SRC)
GARBAGE_DIRS += $(XPIDL_GEN_DIR) org
include $(topsrcdir)/config/rules.mk
$(XPIDL_GEN_DIR)/.done:
@if test ! -d $(XPIDL_GEN_DIR); then echo Creating $(XPIDL_GEN_DIR)/.done; rm -rf $(XPIDL_GEN_DIR); mkdir $(XPIDL_GEN_DIR); fi
@touch $@
$(XPIDL_GEN_DIR)/org/mozilla/xpcom/.done: $(XPIDL_GEN_DIR)/.done
@if test ! -d $(XPIDL_GEN_DIR)/org/mozilla/xpcom; then echo Creating $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.done; rm -rf $(XPIDL_GEN_DIR)/org; mkdir $(XPIDL_GEN_DIR)/org; mkdir $(XPIDL_GEN_DIR)/org/mozilla; mkdir $(XPIDL_GEN_DIR)/org/mozilla/xpcom; fi
@touch $@
$(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.done: $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.done
@if test ! -d $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs; then echo Creating $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.done; rm -rf $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs; mkdir $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs; fi
@touch $@
org/mozilla/xpcom/.done:
@if test ! -d org/mozilla/xpcom; then echo Creating org/mozilla/xpcom/.done; rm -rf org; mkdir org; mkdir org/mozilla; mkdir org/mozilla/xpcom; fi
@touch $@
org/mozilla/xpcom/stubs/.done: org/mozilla/xpcom/.done
@if test ! -d org/mozilla/xpcom/stubs; then echo Creating org/mozilla/xpcom/stubs/.done; rm -rf org/mozilla/xpcom/stubs; mkdir org/mozilla/xpcom/stubs; fi
@touch $@
#$(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/%.java: $(XPIDL_COMPILE) $(XPIDL_GEN_DIR)/.done
# $(ELOG) $(XPIDL_COMPILE) -m javastub -w -I$(srcdir) -I$(IDL_DIR) -o $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/$* $(_VPATH_SRCS)
$(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.java_done: $(XPIDL_COMPILE) $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.done
@echo Making Java stub files
@for idl in $(IDL_FILES); do \
$(XPIDL_COMPILE) -m javastub -w -I$(srcdir) -I$(IDL_DIR) -o $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/`basename $$idl`_Stub $$idl; \
done
@touch $@
#$(XPIDL_GEN_DIR)/org/mozilla/xpcom/%.java: $(XPIDL_COMPILE) $(XPIDL_GEN_DIR)/.done
# $(ELOG) $(XPIDL_COMPILE) -m java -w -I$(srcdir) -I$(IDL_DIR) -o $(XPIDL_GEN_DIR)/org/mozilla/xpcom/$* $(_VPATH_SRCS)
$(XPIDL_GEN_DIR)/org/mozilla/xpcom/.java_done: $(XPIDL_COMPILE) $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.done
@echo Making Java interface files
@for idl in $(IDL_FILES); do \
$(XPIDL_COMPILE) -m java -w -I$(srcdir) -I$(IDL_DIR) -o $(XPIDL_GEN_DIR)/org/mozilla/xpcom/`basename $$idl` $$idl; \
done
@$(INSTALL) -m 644 $(srcdir)/GeckoEmbed.java $(XPIDL_GEN_DIR)/org/mozilla/xpcom
@touch $@
#org/mozilla/xpcom/stubs/%.class: $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.java_done org/mozilla/xpcom/stubs/.done
# @javac -classpath org/mozilla/xpcom:org/mozilla/xpcom/stubs -sourcepath $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs -d org/mozilla/xpcom/stubs $(XPIDL_GEN_DIR)/$(patsubst %.class,%.java,$@)
#org/mozilla/xpcom/%.class: $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.java_done org/mozilla/xpcom/.done
# @javac -classpath org/mozilla/xpcom -sourcepath $(XPIDL_GEN_DIR)/org/mozilla/xpcom -d org/mozilla/xpcom $(XPIDL_GEN_DIR)/$(patsubst %.class,%.java,$@)
org/mozilla/xpcom/.class_done: $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.java_done
@echo Compiling Java interface classes
@javac -classpath . -d . $(filter-out $(addprefix $(XPIDL_GEN_DIR)/,$(IGNORE_STUBS:.class=.java)),$(wildcard $(XPIDL_GEN_DIR)/org/mozilla/xpcom/*.java))
@touch $@
org/mozilla/xpcom/stubs/.class_done: org/mozilla/xpcom/.class_done $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.java_done
@echo Compiling Java stub classes
@javac -classpath . -d . $(filter-out $(addprefix $(XPIDL_GEN_DIR)/,$(IGNORE_STUBS:.class=.java)),$(wildcard $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/*.java))
@touch $@
#$(EMBED_JAR): $(JAVA_IFACE_CLASSES) $(JAVA_STUB_CLASSES)
$(EMBED_JAR): org/mozilla/xpcom/.class_done org/mozilla/xpcom/stubs/.class_done
$(ZIP) $(EMBED_JAR) -qq -r org
$(EMBED_JAR_SRC): $(XPIDL_GEN_DIR)/org/mozilla/xpcom/.java_done $(XPIDL_GEN_DIR)/org/mozilla/xpcom/stubs/.java_done
@cd $(XPIDL_GEN_DIR); $(ZIP) ../$(EMBED_JAR_SRC) -qq -r org
jar:: $(EMBED_JAR) $(EMBED_JAR_SRC)
$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin

View File

@ -0,0 +1,522 @@
/* ***** 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 ***** */
#include "nsJavaWrapper.h"
#include "nsJavaXPCOMBindingUtils.h"
#include "nsJavaXPTCStub.h"
#include "nsEmbedAPI.h"
#include "nsString.h"
#include "nsISimpleEnumerator.h"
#include "nsIInterfaceInfoManager.h"
#include "nsIInputStream.h"
#include "nsEnumeratorUtils.h"
#define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_GeckoEmbed_##func
PRBool gEmbeddingInitialized = PR_FALSE;
extern "C" JNIEXPORT void JNICALL
XPCOM_NATIVE(NS_1InitEmbedding) (JNIEnv* env, jclass, jstring aMozBinDirectory,
jint aAppFileLocProvider)
{
if (!InitializeJavaGlobals(env)) {
FreeJavaGlobals(env);
ThrowXPCOMException(env, 0);
return;
}
nsresult rv;
nsCOMPtr<nsILocalFile> directory;
if (aMozBinDirectory)
{
jboolean isCopy;
const PRUnichar* buf = env->GetStringChars(aMozBinDirectory, &isCopy);
nsAutoString path(buf);
rv = NS_NewLocalFile(path, PR_TRUE, getter_AddRefs(directory));
if (NS_FAILED(rv)) {
ThrowXPCOMException(env, rv);
return;
}
}
/* XXX How do we handle AppFileLocProvider, if we can't create any of the
* Java<->XPCOM mappings before NS_InitEmbedding has been called?
*/
nsIDirectoryServiceProvider* provider = nsnull;
/* if (aAppFileLocProvider)
{
JavaXPCOMInstance* inst = (JavaXPCOMInstance*) aMozBinDirectory;
provider = (nsIDirectoryServiceProvider*) inst->GetInstance();
} */
rv = NS_InitEmbedding(directory, provider);
if (NS_FAILED(rv))
ThrowXPCOMException(env, rv);
gEmbeddingInitialized = PR_TRUE;
}
extern "C" JNIEXPORT void JNICALL
XPCOM_NATIVE(NS_1TermEmbedding) (JNIEnv *env, jclass)
{
FreeJavaGlobals(env);
nsresult rv = NS_TermEmbedding();
if (NS_FAILED(rv))
ThrowXPCOMException(env, rv);
}
/* XXX This can be called before XPCOM is init'd. So we need to find a way
* to create an appropriate Java class for this, such that if it is passed
* through the JNI code (or if we make NS_InitEmbedding take it as a param),
* then we can deal with it accordingly, since it won't yet have an
* InterfaceInfo attached to it. Perhaps we can set its InterfaceInfo to
* NULL and just create it lazily.
*/
extern "C" JNIEXPORT jobject JNICALL
XPCOM_NATIVE(NS_1NewLocalFile) (JNIEnv *env, jclass, jstring aPath,
jboolean aFollowLinks)
{
jobject java_stub = nsnull;
// XXX For now, return if we haven't called NS_InitEmbedding yet
if (!gEmbeddingInitialized) {
ThrowXPCOMException(env, 0);
return nsnull;
}
// Create a Mozilla string from the jstring
jboolean isCopy;
const PRUnichar* buf = nsnull;
if (aPath) {
buf = env->GetStringChars(aPath, &isCopy);
}
nsAutoString path_str(buf);
if (isCopy) {
env->ReleaseStringChars(aPath, buf);
}
// Make call to given function
nsCOMPtr<nsILocalFile> file;
nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, getter_AddRefs(file));
if (NS_SUCCEEDED(rv)) {
// wrap xpcom instance
JavaXPCOMInstance* inst;
inst = CreateJavaXPCOMInstance(file, &NS_GET_IID(nsILocalFile));
if (inst) {
// create java stub
java_stub = CreateJavaWrapper(env, "nsILocalFile");
if (java_stub) {
// Associate XPCOM object w/ Java stub
AddJavaXPCOMBinding(env, java_stub, inst);
}
}
}
if (java_stub == nsnull)
ThrowXPCOMException(env, 0);
return java_stub;
}
extern "C" JNIEXPORT jobject JNICALL
XPCOM_NATIVE(NS_1NewSingletonEnumerator) (JNIEnv *env, jclass, jobject aSingleton)
{
void* inst = GetMatchingXPCOMObject(env, aSingleton);
if (inst == nsnull) {
// If there is not corresponding XPCOM object, then that means that the
// parameter is 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<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
nsCOMPtr<nsIInterfaceInfo> iinfo;
iim->GetInfoForIID(&NS_GET_IID(nsISupports), getter_AddRefs(iinfo));
// Create XPCOM stub
nsJavaXPTCStub* xpcomStub = new nsJavaXPTCStub(env, aSingleton, iinfo);
NS_ADDREF(xpcomStub);
inst = SetAsXPTCStub(xpcomStub);
AddJavaXPCOMBinding(env, aSingleton, inst);
}
nsISupports* singleton;
if (IsXPTCStub(inst))
singleton = (nsISupports*) GetXPTCStubAddr(inst);
else {
JavaXPCOMInstance* xpcomInst = (JavaXPCOMInstance*) inst;
singleton = xpcomInst->GetInstance();
}
// Call XPCOM method
jobject java_stub = nsnull;
nsISimpleEnumerator *enumerator = nsnull;
nsresult rv = NS_NewSingletonEnumerator(&enumerator, singleton);
if (NS_SUCCEEDED(rv)) {
// wrap xpcom instance
JavaXPCOMInstance* inst;
inst = CreateJavaXPCOMInstance(enumerator, &NS_GET_IID(nsISimpleEnumerator));
if (inst) {
// create java stub
java_stub = CreateJavaWrapper(env, "nsISimpleEnumerator");
if (java_stub) {
// Associate XPCOM object w/ Java stub
AddJavaXPCOMBinding(env, java_stub, inst);
}
}
}
if (java_stub == nsnull)
ThrowXPCOMException(env, 0);
return java_stub;
}
extern "C" JNIEXPORT jobject JNICALL
XPCOM_NATIVE(NS_1GetComponentManager) (JNIEnv *env, jclass)
{
jobject java_stub = nsnull;
// Call XPCOM method
nsCOMPtr<nsIComponentManager> cm;
nsresult rv = NS_GetComponentManager(getter_AddRefs(cm));
if (NS_SUCCEEDED(rv)) {
// wrap xpcom instance
JavaXPCOMInstance* inst;
inst = CreateJavaXPCOMInstance(cm, &NS_GET_IID(nsIComponentManager));
if (inst) {
// create java stub
java_stub = CreateJavaWrapper(env, "nsIComponentManager");
if (java_stub) {
// Associate XPCOM object w/ Java stub
AddJavaXPCOMBinding(env, java_stub, inst);
}
}
}
if (java_stub == nsnull)
ThrowXPCOMException(env, 0);
return java_stub;
}
extern "C" JNIEXPORT jobject JNICALL
XPCOM_NATIVE(NS_1GetServiceManager) (JNIEnv *env, jclass)
{
jobject java_stub = nsnull;
// Call XPCOM method
nsCOMPtr<nsIServiceManager> sm;
nsresult rv = NS_GetServiceManager(getter_AddRefs(sm));
if (NS_SUCCEEDED(rv)) {
// wrap xpcom instance
JavaXPCOMInstance* inst;
inst = CreateJavaXPCOMInstance(sm, &NS_GET_IID(nsIServiceManager));
if (inst) {
// create java stub
java_stub = CreateJavaWrapper(env, "nsIServiceManager");
if (java_stub) {
// Associate XPCOM object w/ Java stub
AddJavaXPCOMBinding(env, java_stub, inst);
}
}
}
if (java_stub == nsnull)
ThrowXPCOMException(env, 0);
return java_stub;
}
// JNI wrapper for calling an nsWriteSegmentFun function
extern "C" JNIEXPORT jint JNICALL
XPCOM_NATIVE(nsWriteSegmentFun) (JNIEnv *env, jclass that, jint aWriterFunc,
jobject aInStream, jint aClosure,
jbyteArray aFromSegment, jint aToOffset,
jint aCount)
{
nsresult rc;
void* inst = GetMatchingXPCOMObject(env, aInStream);
if (inst == nsnull) {
// If there is not corresponding XPCOM object, then that means that the
// parameter is 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<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
nsCOMPtr<nsIInterfaceInfo> iinfo;
iim->GetInfoForIID(&NS_GET_IID(nsIInputStream), getter_AddRefs(iinfo));
// Create XPCOM stub
nsJavaXPTCStub* xpcomStub = new nsJavaXPTCStub(env, aInStream, iinfo);
NS_ADDREF(xpcomStub);
inst = SetAsXPTCStub(xpcomStub);
AddJavaXPCOMBinding(env, aInStream, inst);
}
nsIInputStream* instream;
if (IsXPTCStub(inst))
instream = (nsIInputStream*) GetXPTCStubAddr(inst);
else {
JavaXPCOMInstance* xpcomInst = (JavaXPCOMInstance*) inst;
instream = (nsIInputStream*) xpcomInst->GetInstance();
}
jbyte* fromSegment = nsnull;
jboolean isCopy = JNI_FALSE;
if (aFromSegment) {
fromSegment = env->GetByteArrayElements(aFromSegment, &isCopy);
}
PRUint32 write_count;
nsWriteSegmentFun writer = (nsWriteSegmentFun) aWriterFunc;
rc = writer(instream, (void*) aClosure, (const char*) fromSegment, aToOffset,
aCount, &write_count);
if (NS_FAILED(rc)) {
ThrowXPCOMException(env, rc);
return 0;
}
if (isCopy) {
env->ReleaseByteArrayElements(aFromSegment, fromSegment, 0);
}
return write_count;
}
extern "C" JNIEXPORT void JNICALL
XPCOM_NATIVE(CallXPCOMMethodVoid) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
}
extern "C" JNIEXPORT jboolean JNICALL
XPCOM_NATIVE(CallXPCOMMethodBool) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.z;
}
extern "C" JNIEXPORT jbooleanArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodBoolA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jbooleanArray) rc.l;
}
extern "C" JNIEXPORT jbyte JNICALL
XPCOM_NATIVE(CallXPCOMMethodByte) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.b;
}
extern "C" JNIEXPORT jbyteArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodByteA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jbyteArray) rc.l;
}
extern "C" JNIEXPORT jchar JNICALL
XPCOM_NATIVE(CallXPCOMMethodChar) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.c;
}
extern "C" JNIEXPORT jcharArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodCharA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jcharArray) rc.l;
}
extern "C" JNIEXPORT jshort JNICALL
XPCOM_NATIVE(CallXPCOMMethodShort) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.s;
}
extern "C" JNIEXPORT jshortArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodShortA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jshortArray) rc.l;
}
extern "C" JNIEXPORT jint JNICALL
XPCOM_NATIVE(CallXPCOMMethodInt) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.i;
}
extern "C" JNIEXPORT jintArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodIntA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jintArray) rc.l;
}
extern "C" JNIEXPORT jlong JNICALL
XPCOM_NATIVE(CallXPCOMMethodLong) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.j;
}
extern "C" JNIEXPORT jlongArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodLongA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jlongArray) rc.l;
}
extern "C" JNIEXPORT jfloat JNICALL
XPCOM_NATIVE(CallXPCOMMethodFloat) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.f;
}
extern "C" JNIEXPORT jfloatArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodFloatA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jfloatArray) rc.l;
}
extern "C" JNIEXPORT jdouble JNICALL
XPCOM_NATIVE(CallXPCOMMethodDouble) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.d;
}
extern "C" JNIEXPORT jdoubleArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodDoubleA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jdoubleArray) rc.l;
}
extern "C" JNIEXPORT jobject JNICALL
XPCOM_NATIVE(CallXPCOMMethodObj) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return rc.l;
}
extern "C" JNIEXPORT jobjectArray JNICALL
XPCOM_NATIVE(CallXPCOMMethodObjA) (JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams)
{
jvalue rc;
CallXPCOMMethod(env, that, aJavaObject, aMethodIndex, aParams, rc);
return (jobjectArray) rc.l;
}
extern "C" JNIEXPORT void JNICALL
XPCOM_NATIVE(FinalizeStub) (JNIEnv *env, jclass that, jobject aJavaObject)
{
#ifdef DEBUG
jboolean isCopy;
jclass clazz = env->GetObjectClass(aJavaObject);
jstring name = (jstring) env->CallObjectMethod(clazz, getNameMID);
const char* javaObjectName = env->GetStringUTFChars(name, &isCopy);
fprintf(stderr, "*** Finalize(java_obj=%s)\n", javaObjectName);
if (isCopy)
env->ReleaseStringUTFChars(name, javaObjectName);
#endif
nsISupports* xpcomObj = RemoveXPCOMBinding(env, aJavaObject);
NS_RELEASE(xpcomObj);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,50 @@
/* ***** 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"
void CallXPCOMMethod(JNIEnv *env, jclass that, jobject aJavaObject,
jint aMethodIndex, jobjectArray aParams, jvalue &aResult);
// Creates a Java stub for an XPCOM object of type aClassName
jobject CreateJavaWrapper(JNIEnv* env, const char* aClassName);
#endif // _nsJavaWrapper_h_

View File

@ -0,0 +1,521 @@
/* ***** 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 ***** */
#include "nsJavaXPCOMBindingUtils.h"
#include "nsJavaXPTCStub.h"
#include "jni.h"
#include "nsIInterfaceInfoManager.h"
#include "pldhash.h"
/* Java JNI globals */
jclass classClass = nsnull;
jmethodID getNameMID = nsnull;
jclass objectClass = nsnull;
jmethodID hashCodeMID = nsnull;
jclass booleanClass = nsnull;
jclass booleanArrayClass = nsnull;
jmethodID booleanInitMID = nsnull;
jmethodID booleanValueMID = nsnull;
jclass charClass = nsnull;
jclass charArrayClass = nsnull;
jmethodID charInitMID = nsnull;
jmethodID charValueMID = nsnull;
jclass byteClass = nsnull;
jclass byteArrayClass = nsnull;
jmethodID byteInitMID = nsnull;
jmethodID byteValueMID = nsnull;
jclass shortClass = nsnull;
jclass shortArrayClass = nsnull;
jmethodID shortInitMID = nsnull;
jmethodID shortValueMID = nsnull;
jclass intClass = nsnull;
jclass intArrayClass = nsnull;
jmethodID intInitMID = nsnull;
jmethodID intValueMID = nsnull;
jclass longClass = nsnull;
jclass longArrayClass = nsnull;
jmethodID longInitMID = nsnull;
jmethodID longValueMID = nsnull;
jclass floatClass = nsnull;
jclass floatArrayClass = nsnull;
jmethodID floatInitMID = nsnull;
jmethodID floatValueMID = nsnull;
jclass doubleClass = nsnull;
jclass doubleArrayClass = nsnull;
jmethodID doubleInitMID = nsnull;
jmethodID doubleValueMID = nsnull;
jclass stringClass = nsnull;
jclass stringArrayClass = nsnull;
jclass nsISupportsClass = nsnull;
jclass exceptionClass = nsnull;
/**************************************
* Java<->XPCOM binding stores
**************************************/
class JavaXPCOMBindingEntry : public PLDHashEntryHdr
{
public:
jobject mJavaObject;
void* mXPCOMInstance;
};
static PLDHashTable *gJAVAtoXPCOMBindings = nsnull;
static PLDHashTable *gXPCOMtoJAVABindings = nsnull;
PR_STATIC_CALLBACK(PRBool)
InitJAVAtoXPCOMBindingEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
const void *key)
{
JavaXPCOMBindingEntry *e =
NS_CONST_CAST(JavaXPCOMBindingEntry *,
NS_STATIC_CAST(const JavaXPCOMBindingEntry *, entry));
e->mJavaObject = NS_CONST_CAST(jobject,
NS_STATIC_CAST(const __jobject*, key));
e->mXPCOMInstance = nsnull;
return PR_TRUE;
}
PR_STATIC_CALLBACK(PRBool)
InitXPCOMtoJAVABindingEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
const void *key)
{
JavaXPCOMBindingEntry *e =
NS_CONST_CAST(JavaXPCOMBindingEntry *,
NS_STATIC_CAST(const JavaXPCOMBindingEntry *, entry));
e->mXPCOMInstance = NS_CONST_CAST(void*, key);
e->mJavaObject = nsnull;
return PR_TRUE;
}
void
AddJavaXPCOMBinding(JNIEnv* env, jobject aJavaObject, void* aXPCOMObject)
{
jint hash = env->CallIntMethod(aJavaObject, hashCodeMID);
JavaXPCOMBindingEntry *entry =
NS_STATIC_CAST(JavaXPCOMBindingEntry*,
PL_DHashTableOperate(gJAVAtoXPCOMBindings,
NS_INT32_TO_PTR(hash),
PL_DHASH_ADD));
entry->mXPCOMInstance = aXPCOMObject;
entry =
NS_STATIC_CAST(JavaXPCOMBindingEntry*,
PL_DHashTableOperate(gXPCOMtoJAVABindings, aXPCOMObject,
PL_DHASH_ADD));
entry->mJavaObject = aJavaObject;
// LOG("+ Adding Java<->XPCOM binding (Java=0x%08x | XPCOM=0x%08x)\n",
// hash, (int) aXPCOMObject);
}
nsISupports*
RemoveXPCOMBinding(JNIEnv* env, jobject aJavaObject)
{
void* xpcomObj = GetMatchingXPCOMObject(env, aJavaObject);
// Remove both instances from stores
jint hash = env->CallIntMethod(aJavaObject, hashCodeMID);
PL_DHashTableOperate(gJAVAtoXPCOMBindings, NS_INT32_TO_PTR(hash),
PL_DHASH_REMOVE);
PL_DHashTableOperate(gXPCOMtoJAVABindings, xpcomObj, PL_DHASH_REMOVE);
if (IsXPTCStub(xpcomObj)) {
return (nsISupports*) GetXPTCStubAddr(xpcomObj);
} else {
JavaXPCOMInstance* xpcomInst = (JavaXPCOMInstance*) xpcomObj;
nsISupports* inst = xpcomInst->GetInstance();
// XXX Getting some odd thread issues when calling this. Addreffing for
// now to work around the errors.
NS_ADDREF(inst);
delete xpcomInst;
return inst;
}
}
void*
GetMatchingXPCOMObject(JNIEnv* env, jobject aJavaObject)
{
jint hash = env->CallIntMethod(aJavaObject, hashCodeMID);
JavaXPCOMBindingEntry *entry =
NS_STATIC_CAST(JavaXPCOMBindingEntry*,
PL_DHashTableOperate(gJAVAtoXPCOMBindings,
NS_INT32_TO_PTR(hash),
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
// LOG("< Get Java<->XPCOM binding (Java=0x%08x | XPCOM=0x%08x)\n",
// (int) aJavaObject, (int) entry->mXPCOMInstance);
return entry->mXPCOMInstance;
}
return nsnull;
}
jobject
GetMatchingJavaObject(void* aXPCOMObject)
{
JavaXPCOMBindingEntry *entry =
NS_STATIC_CAST(JavaXPCOMBindingEntry*,
PL_DHashTableOperate(gXPCOMtoJAVABindings, aXPCOMObject,
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
// LOG("< Get Java<->XPCOM binding (Java=0x%08x | XPCOM=0x%08x)\n",
// (int) entry->mJavaObject, (int) aXPCOMObject);
return entry->mJavaObject;
}
return nsnull;
}
/******************************
* InitializeJavaGlobals
******************************/
PRBool
InitializeJavaGlobals(JNIEnv *env)
{
jclass clazz;
if (!(clazz = env->FindClass("java/lang/Class")) ||
!(classClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Object")) ||
!(objectClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Boolean")) ||
!(booleanClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[Z")) ||
!(booleanArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Character")) ||
!(charClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[C")) ||
!(charArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Byte")) ||
!(byteClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[B")) ||
!(byteArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Short")) ||
!(shortClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[[S")) ||
!(shortArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Integer")) ||
!(intClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[I")) ||
!(intArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Long")) ||
!(longClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[J")))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Float")) ||
!(floatClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[F")) ||
!(floatArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Double")) ||
!(doubleClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[D")) ||
!(doubleArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/String")) ||
!(stringClass = (jclass) env->NewGlobalRef(clazz)) ||
!(clazz = env->FindClass("[Ljava/lang/String;")) ||
!(stringArrayClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("org/mozilla/xpcom/nsISupports")) ||
!(nsISupportsClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(clazz = env->FindClass("java/lang/Exception")) ||
!(exceptionClass = (jclass) env->NewGlobalRef(clazz)))
{
return PR_FALSE;
}
if (!(hashCodeMID = env->GetMethodID(objectClass, "hashCode","()I"))) {
return PR_FALSE;
}
if (!(booleanInitMID = env->GetMethodID(booleanClass,"<init>","(Z)V"))) {
return PR_FALSE;
}
if (!(booleanValueMID = env->GetMethodID(booleanClass,"booleanValue","()Z"))) {
return PR_FALSE;
}
if (!(charInitMID = env->GetMethodID(charClass,"<init>","(C)V"))) {
return PR_FALSE;
}
if (!(charValueMID = env->GetMethodID(charClass,"charValue","()C"))) {
return PR_FALSE;
}
if (!(byteInitMID = env->GetMethodID(byteClass,"<init>","(B)V"))) {
return PR_FALSE;
}
if (!(byteValueMID = env->GetMethodID(byteClass,"byteValue","()B"))) {
return PR_FALSE;
}
if (!(shortInitMID = env->GetMethodID(shortClass,"<init>","(S)V"))) {
return PR_FALSE;
}
if (!(shortValueMID = env->GetMethodID(shortClass,"shortValue","()S"))) {
return PR_FALSE;
}
if (!(intInitMID = env->GetMethodID(intClass,"<init>","(I)V"))) {
return PR_FALSE;
}
if (!(intValueMID = env->GetMethodID(intClass,"intValue","()I"))) {
return PR_FALSE;
}
if (!(longInitMID = env->GetMethodID(longClass,"<init>","(J)V"))) {
return PR_FALSE;
}
if (!(longValueMID = env->GetMethodID(longClass,"longValue","()J"))) {
return PR_FALSE;
}
if (!(floatInitMID = env->GetMethodID(floatClass,"<init>","(F)V"))) {
return PR_FALSE;
}
if (!(floatValueMID = env->GetMethodID(floatClass,"floatValue","()F"))) {
return PR_FALSE;
}
if (!(doubleInitMID = env->GetMethodID(doubleClass,"<init>","(D)V"))) {
return PR_FALSE;
}
if (!(doubleValueMID = env->GetMethodID(doubleClass,"doubleValue","()D"))) {
return PR_FALSE;
}
if (!(getNameMID = env->GetMethodID(classClass, "getName","()Ljava/lang/String;"))) {
return PR_FALSE;
}
static PLDHashTableOps java_to_xpcom_hash_ops =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
PL_DHashGetKeyStub,
PL_DHashVoidPtrKeyStub,
PL_DHashMatchEntryStub,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
InitJAVAtoXPCOMBindingEntry
};
gJAVAtoXPCOMBindings = PL_NewDHashTable(&java_to_xpcom_hash_ops, nsnull,
sizeof(JavaXPCOMBindingEntry), 16);
if (!gJAVAtoXPCOMBindings) {
return PR_FALSE;
}
static PLDHashTableOps xpcom_to_java_hash_ops =
{
PL_DHashAllocTable,
PL_DHashFreeTable,
PL_DHashGetKeyStub,
PL_DHashVoidPtrKeyStub,
PL_DHashMatchEntryStub,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
InitXPCOMtoJAVABindingEntry
};
gXPCOMtoJAVABindings = PL_NewDHashTable(&xpcom_to_java_hash_ops, nsnull,
sizeof(JavaXPCOMBindingEntry), 16);
if (!gXPCOMtoJAVABindings) {
return PR_FALSE;
}
return PR_TRUE;
}
/*************************
* FreeJavaGlobals
*************************/
void
FreeJavaGlobals(JNIEnv* env)
{
// XXX Need to write
}
/**********************************************************
* JavaXPCOMInstance
*********************************************************/
JavaXPCOMInstance*
CreateJavaXPCOMInstance(nsISupports* aXPCOMObject, const nsIID* aIID)
{
JavaXPCOMInstance* inst = nsnull;
// Get interface info for class
nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
NS_ASSERTION(iim != nsnull, "Failed to get InterfaceInfoManager");
if (iim) {
nsCOMPtr<nsIInterfaceInfo> info;
iim->GetInfoForIID(aIID, getter_AddRefs(info));
// Wrap XPCOM object
inst = new JavaXPCOMInstance(aXPCOMObject, info);
NS_ADDREF(aXPCOMObject);
}
return inst;
}
void
ThrowXPCOMException(JNIEnv* env, int aFailureCode)
{
// only throw this exception if one hasn't already been thrown
jthrowable throwObj = env->ExceptionOccurred();
if (throwObj != nsnull) {
char exp_msg[32];
sprintf(exp_msg, "\nInternal Gecko Error: %x", aFailureCode);
env->ThrowNew(exceptionClass, exp_msg);
}
}
nsresult
GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
const nsXPTMethodInfo *methodInfo,
const nsXPTParamInfo &paramInfo,
PRUint16 methodIndex,
nsXPTCMiniVariant *dispatchParams,
PRBool isFullVariantArray,
nsID &result)
{
nsresult rv;
switch (paramInfo.GetType().TagPart())
{
case nsXPTType::T_INTERFACE:
rv = iinfo->GetIIDForParamNoAlloc(methodIndex, &paramInfo, &result);
break;
case nsXPTType::T_INTERFACE_IS:
{
PRUint8 argnum;
rv = iinfo->GetInterfaceIsArgNumberForParam(methodIndex, &paramInfo, &argnum);
if (NS_FAILED(rv))
return rv;
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)
return NS_ERROR_UNEXPECTED;
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;
}

View File

@ -0,0 +1,153 @@
/* ***** 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 _nsJavaXPCOMBindingUtils_h_
#define _nsJavaXPCOMBindingUtils_h_
#include "jni.h"
#include "xptcall.h"
#include "nsCOMPtr.h"
#ifdef DEBUG
#define LOG(...) printf(__VA_ARGS__)
#else
#define LOG(format, ...)
#endif
/*********************
* Java JNI globals
*********************/
extern jclass classClass;
extern jmethodID getNameMID;
extern jclass objectClass;
extern jmethodID hashCodeMID;
extern jclass booleanClass;
extern jclass booleanArrayClass;
extern jmethodID booleanInitMID;
extern jmethodID booleanValueMID;
extern jclass charClass;
extern jclass charArrayClass;
extern jmethodID charInitMID;
extern jmethodID charValueMID;
extern jclass byteClass;
extern jclass byteArrayClass;
extern jmethodID byteInitMID;
extern jmethodID byteValueMID;
extern jclass shortClass;
extern jclass shortArrayClass;
extern jmethodID shortInitMID;
extern jmethodID shortValueMID;
extern jclass intClass;
extern jclass intArrayClass;
extern jmethodID intInitMID;
extern jmethodID intValueMID;
extern jclass longClass;
extern jclass longArrayClass;
extern jmethodID longInitMID;
extern jmethodID longValueMID;
extern jclass floatClass;
extern jclass floatArrayClass;
extern jmethodID floatInitMID;
extern jmethodID floatValueMID;
extern jclass doubleClass;
extern jclass doubleArrayClass;
extern jmethodID doubleInitMID;
extern jmethodID doubleValueMID;
extern jclass stringClass;
extern jclass stringArrayClass;
extern jclass nsISupportsClass;
extern jclass exceptionClass;
PRBool InitializeJavaGlobals(JNIEnv *env);
void FreeJavaGlobals(JNIEnv* env);
/*************************
* JavaXPCOMInstance
*************************/
class JavaXPCOMInstance
{
public:
JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo)
: mInstance(aInstance),
mIInfo(aIInfo)
{}
nsISupports* GetInstance() { return mInstance; }
nsIInterfaceInfo* InterfaceInfo() { return mIInfo; }
private:
nsCOMPtr<nsISupports> mInstance;
nsCOMPtr<nsIInterfaceInfo> mIInfo;
};
JavaXPCOMInstance* CreateJavaXPCOMInstance(nsISupports* aXPCOMObject,
const nsIID* aIID);
/**************************************
* Java<->XPCOM binding stores
**************************************/
void AddJavaXPCOMBinding(JNIEnv* env, jobject aJavaStub, void* aXPCOMObject);
nsISupports* RemoveXPCOMBinding(JNIEnv* env, jobject aJavaObject);
void* GetMatchingXPCOMObject(JNIEnv* env, jobject aJavaObject);
jobject GetMatchingJavaObject(void* aXPCOMObject);
void ThrowXPCOMException(JNIEnv* env, int aFailureCode);
nsresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
const nsXPTMethodInfo *methodInfo,
const nsXPTParamInfo &paramInfo,
PRUint16 methodIndex,
nsXPTCMiniVariant *dispatchParams,
PRBool isFullVariantArray,
nsID &result);
#endif // _nsJavaXPCOMBindingUtils_h_

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,110 @@
/* ***** 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 _nsJavaXPTCStub_h_
#define _nsJavaXPTCStub_h_
#include "xptcall.h"
#include "jni.h"
#include "nsVoidArray.h"
#include "nsIInterfaceInfo.h"
#include "nsCOMPtr.h"
class nsJavaXPTCStub : public nsXPTCStubBase
{
public:
NS_DECL_ISUPPORTS
nsJavaXPTCStub(JNIEnv* aJavaEnv, 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);
private:
// NS_HIDDEN ~JavaStub();
// returns a weak reference to a child supporting the specified interface
// NS_HIDDEN_(JavaStub *) FindStubSupportingIID(const nsID &aIID);
nsJavaXPTCStub * FindStubSupportingIID(const nsID &aIID);
// returns true if this stub supports the specified interface
// NS_HIDDEN_(PRBool) SupportsIID(const nsID &aIID);
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,
nsACString &aRetvalSig);
nsresult FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
const nsXPTMethodInfo* aMethodInfo,
PRUint16 aMethodIndex,
nsXPTCMiniVariant* aDispatchParams,
nsXPTCMiniVariant &aVariant,
jvalue &aJValue);
nsresult SetXPCOMRetval();
JNIEnv* mJavaEnv;
jobject mJavaObject;
nsCOMPtr<nsIInterfaceInfo> mIInfo;
nsVoidArray mChildren; // weak references (cleared by the children)
nsJavaXPTCStub *mMaster; // strong reference
};
inline void* SetAsXPTCStub(nsJavaXPTCStub* ptr)
{ return (void*) (((unsigned long) ptr) | 0x1); }
inline PRBool IsXPTCStub(void* ptr)
{ return ((unsigned long) ptr) & 0x1; }
inline nsJavaXPTCStub* GetXPTCStubAddr(void* ptr)
{ return (nsJavaXPTCStub*) (((unsigned long) ptr) & ~0x1); }
#endif // _nsJavaXPTCStub_h_