3567N/A/* ***** BEGIN LICENSE BLOCK *****
3349N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3349N/A *
3349N/A * The contents of this file are subject to the Mozilla Public License Version
3349N/A * 1.1 (the "License"); you may not use this file except in compliance with
3349N/A * the License. You may obtain a copy of the License at
3349N/A * http://www.mozilla.org/MPL/
3349N/A *
3349N/A * Software distributed under the License is distributed on an "AS IS" basis,
3349N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
3349N/A * for the specific language governing rights and limitations under the
3349N/A * License.
3349N/A *
3349N/A * The Original Code is Java XPCOM Bindings.
3349N/A *
3349N/A * The Initial Developer of the Original Code is
3349N/A * IBM Corporation.
3349N/A * Portions created by the Initial Developer are Copyright (C) 2005
3349N/A * IBM Corporation. All Rights Reserved.
3349N/A *
3349N/A * Contributor(s):
3349N/A * Javier Pedemonte (jhpedemonte@gmail.com)
3349N/A *
3349N/A * Alternatively, the contents of this file may be used under the terms of
3349N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
3349N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
3349N/A * in which case the provisions of the GPL or the LGPL are applicable instead
3349N/A * of those above. If you wish to allow use of your version of this file only
3349N/A * under the terms of either the GPL or the LGPL, and not to allow others to
3349N/A * use your version of this file under the terms of the MPL, indicate your
3349N/A * decision by deleting the provisions above and replace them with the notice
3349N/A * and other provisions required by the GPL or the LGPL. If you do not delete
3349N/A * the provisions above, a recipient may use your version of this file under
3349N/A * the terms of any one of the MPL, the GPL or the LGPL.
3349N/A *
3349N/A * ***** END LICENSE BLOCK ***** */
3349N/A
3349N/A#ifndef _nsJavaXPCOMBindingUtils_h_
3349N/A#define _nsJavaXPCOMBindingUtils_h_
3349N/A
3349N/A#include "jni.h"
3349N/A#include "xptcall.h"
3349N/A#include "nsCOMPtr.h"
3349N/A#include "nsString.h"
3349N/A#include "pldhash.h"
3349N/A#include "nsJavaXPTCStub.h"
3349N/A#include "nsAutoLock.h"
3349N/A#include "nsTHashtable.h"
3349N/A#include "nsHashKeys.h"
3349N/A#include "nsILocalFile.h"
3349N/A
3349N/A//#define DEBUG_JAVAXPCOM
3349N/A//#define DEBUG_JAVAXPCOM_REFCNT
3349N/A
3349N/A#ifdef DEBUG_JAVAXPCOM
3349N/A#define LOG(x) printf x
3349N/A#else
3349N/A#define LOG(x) /* nothing */
3349N/A#endif
3349N/A
3349N/A
3349N/A/*********************
3349N/A * Java JNI globals
3349N/A *********************/
3349N/A
3349N/Aextern jclass systemClass;
3349N/Aextern jclass booleanClass;
3349N/Aextern jclass charClass;
3349N/Aextern jclass byteClass;
3349N/Aextern jclass shortClass;
3349N/Aextern jclass intClass;
3349N/Aextern jclass longClass;
3349N/Aextern jclass floatClass;
3349N/Aextern jclass doubleClass;
3349N/Aextern jclass stringClass;
3349N/Aextern jclass nsISupportsClass;
3349N/Aextern jclass xpcomExceptionClass;
3349N/Aextern jclass xpcomJavaProxyClass;
3349N/Aextern jclass weakReferenceClass;
3349N/Aextern jclass javaXPCOMUtilsClass;
3349N/A
3349N/Aextern jmethodID hashCodeMID;
3349N/Aextern jmethodID booleanValueMID;
3349N/Aextern jmethodID booleanInitMID;
3349N/Aextern jmethodID charValueMID;
3349N/Aextern jmethodID charInitMID;
3349N/Aextern jmethodID byteValueMID;
3349N/Aextern jmethodID byteInitMID;
3349N/Aextern jmethodID shortValueMID;
3349N/Aextern jmethodID shortInitMID;
3349N/Aextern jmethodID intValueMID;
3349N/Aextern jmethodID intInitMID;
3349N/Aextern jmethodID longValueMID;
3349N/Aextern jmethodID longInitMID;
3349N/Aextern jmethodID floatValueMID;
3349N/Aextern jmethodID floatInitMID;
3349N/Aextern jmethodID doubleValueMID;
3349N/Aextern jmethodID doubleInitMID;
3349N/Aextern jmethodID createProxyMID;
3349N/Aextern jmethodID isXPCOMJavaProxyMID;
3349N/Aextern jmethodID getNativeXPCOMInstMID;
3349N/Aextern jmethodID weakReferenceConstructorMID;
3349N/Aextern jmethodID getReferentMID;
3349N/Aextern jmethodID clearReferentMID;
3349N/Aextern jmethodID findClassInLoaderMID;
3349N/A
3349N/A#ifdef DEBUG_JAVAXPCOM
3349N/Aextern jmethodID getNameMID;
3349N/Aextern jmethodID proxyToStringMID;
3349N/A#endif
3349N/A
3349N/Aclass NativeToJavaProxyMap;
3349N/Aextern NativeToJavaProxyMap* gNativeToJavaProxyMap;
3349N/Aclass JavaToXPTCStubMap;
3349N/Aextern JavaToXPTCStubMap* gJavaToXPTCStubMap;
3349N/A
3349N/Aextern nsTHashtable<nsDepCharHashKey>* gJavaKeywords;
3349N/A
3349N/A// The Java garbage collector runs in a separate thread. Since it calls the
3349N/A// finalizeProxy() function in nsJavaWrapper.cpp, we need to make sure that
3349N/A// all the structures touched by finalizeProxy() are multithread aware.
3349N/Aextern PRLock* gJavaXPCOMLock;
3349N/A
3349N/Aextern PRBool gJavaXPCOMInitialized;
3349N/A
3349N/A/**
3349N/A * Initialize global structures used by JavaXPCOM.
3349N/A * @param env Java environment pointer
3349N/A * @return PR_TRUE if JavaXPCOM is initialized; PR_FALSE if an error occurred
3349N/A */
3349N/APRBool InitializeJavaGlobals(JNIEnv *env);
3349N/A
3349N/A/**
3349N/A * Frees global structures that were allocated by InitializeJavaGlobals().
3349N/A * @param env Java environment pointer
3349N/A */
3349N/Avoid FreeJavaGlobals(JNIEnv* env);
3349N/A
3349N/A
3349N/A/*************************
3349N/A * JavaXPCOMInstance
3349N/A *************************/
3349N/A
3349N/Aclass JavaXPCOMInstance
3349N/A{
3349N/Apublic:
3349N/A JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo);
3349N/A ~JavaXPCOMInstance();
3349N/A
3349N/A nsISupports* GetInstance() { return mInstance; }
3349N/A nsIInterfaceInfo* InterfaceInfo() { return mIInfo; }
3349N/A
3349N/Aprivate:
3349N/A nsISupports* mInstance;
3349N/A nsIInterfaceInfo* mIInfo;
3349N/A};
3349N/A
3349N/A
3349N/A/**************************************
3349N/A * Java<->XPCOM object mappings
3349N/A **************************************/
3349N/A
3349N/A/**
3349N/A * Maps native XPCOM objects to their associated Java proxy object.
3349N/A */
3349N/Aclass NativeToJavaProxyMap
3349N/A{
3349N/A friend PLDHashOperator DestroyJavaProxyMappingEnum(PLDHashTable* aTable,
3349N/A PLDHashEntryHdr* aHeader,
3349N/A PRUint32 aNumber,
3349N/A void* aData);
3349N/A
3349N/Aprotected:
3349N/A struct ProxyList
3349N/A {
3349N/A ProxyList(const jobject aRef, const nsIID& aIID, ProxyList* aList)
3349N/A : javaObject(aRef)
3349N/A , iid(aIID)
3349N/A , next(aList)
3349N/A { }
3349N/A
3349N/A const jobject javaObject;
3349N/A const nsIID iid;
3349N/A ProxyList* next;
3349N/A };
3349N/A
3349N/A struct Entry : public PLDHashEntryHdr
3349N/A {
3349N/A nsISupports* key;
3349N/A ProxyList* list;
3349N/A };
3349N/A
3349N/Apublic:
3349N/A NativeToJavaProxyMap()
3349N/A : mHashTable(nsnull)
3349N/A { }
3349N/A
3349N/A ~NativeToJavaProxyMap()
3349N/A {
3349N/A NS_ASSERTION(mHashTable == nsnull,
3349N/A "MUST call Destroy() before deleting object");
3349N/A }
3349N/A
3349N/A nsresult Init();
3349N/A
3349N/A nsresult Destroy(JNIEnv* env);
3349N/A
3349N/A nsresult Add(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID,
3349N/A jobject aProxy);
3349N/A
3349N/A nsresult Find(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID,
3349N/A jobject* aResult);
3349N/A
3349N/A nsresult Remove(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID);
3349N/A
3349N/Aprotected:
3349N/A PLDHashTable* mHashTable;
3349N/A};
3349N/A
3349N/A/**
3349N/A * Maps Java objects to their associated nsJavaXPTCStub.
3349N/A */
3349N/Aclass JavaToXPTCStubMap
3349N/A{
3349N/A friend PLDHashOperator DestroyXPTCMappingEnum(PLDHashTable* aTable,
3349N/A PLDHashEntryHdr* aHeader,
3349N/A PRUint32 aNumber, void* aData);
3349N/A
3349N/Aprotected:
3349N/A struct Entry : public PLDHashEntryHdr
3349N/A {
3349N/A jint key;
3349N/A nsJavaXPTCStub* xptcstub;
3349N/A };
3349N/A
3349N/Apublic:
3349N/A JavaToXPTCStubMap()
3349N/A : mHashTable(nsnull)
3349N/A { }
3349N/A
3349N/A ~JavaToXPTCStubMap()
3349N/A {
3349N/A NS_ASSERTION(mHashTable == nsnull,
3349N/A "MUST call Destroy() before deleting object");
3349N/A }
3349N/A
3349N/A nsresult Init();
3349N/A
3349N/A nsresult Destroy();
3349N/A
3349N/A nsresult Add(jint aJavaObjectHashCode, nsJavaXPTCStub* aProxy);
3349N/A
3349N/A nsresult Find(jint aJavaObjectHashCode, const nsIID& aIID,
3349N/A nsJavaXPTCStub** aResult);
3349N/A
3349N/A nsresult Remove(jint aJavaObjectHashCode);
3349N/A
3349N/Aprotected:
3349N/A PLDHashTable* mHashTable;
3349N/A};
3349N/A
3349N/A
3349N/A/*******************************
3349N/A * Helper functions
3349N/A *******************************/
3349N/A
3349N/A/**
3349N/A * Convert a native nsISupports to a Java object.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aXPCOMObject XPCOM object for which to find/create Java object
3349N/A * @param aIID desired interface IID for Java object
3349N/A * @param aObjectLoader Java object whose class loader we use for finding
3349N/A * classes; can be null
3349N/A * @param aResult on success, holds reference to Java object
3349N/A *
3349N/A * @return NS_OK if succeeded; all other return values are error codes.
3349N/A */
3349N/Ansresult NativeInterfaceToJavaObject(JNIEnv* env, nsISupports* aXPCOMObject,
3349N/A const nsIID& aIID, jobject aObjectLoader,
3349N/A jobject* aResult);
3349N/A
3349N/A/**
3349N/A * Convert a Java object to a native nsISupports object.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aJavaObject Java object for which to find/create XPCOM object
3349N/A * @param aIID desired interface IID for XPCOM object
3349N/A * @param aResult on success, holds AddRef'd reference to XPCOM object
3349N/A *
3349N/A * @return NS_OK if succeeded; all other return values are error codes.
3349N/A */
3349N/Ansresult JavaObjectToNativeInterface(JNIEnv* env, jobject aJavaObject,
3349N/A const nsIID& aIID, void** aResult);
3349N/A
3349N/Ansresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
3349N/A const XPTMethodDescriptor *methodInfo,
3349N/A const nsXPTParamInfo &paramInfo,
3349N/A PRUint8 paramType, PRUint16 methodIndex,
3349N/A nsXPTCMiniVariant *dispatchParams,
3349N/A PRBool isFullVariantArray,
3349N/A nsID &result);
3349N/A
3349N/A/**
3349N/A * Returns the Class object associated with the class or interface with the
3349N/A * given string name, using the class loader of the given object.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aObjectLoader Java object whose class loader is used to load class
3349N/A * @param aClassName fully qualified name of class to load
3349N/A *
3349N/A * @return java.lang.Class object of requested Class; NULL if the class
3349N/A * wasn't found
3349N/A *
3349N/A * @see http://java.sun.com/j2se/1.3/docs/guide/jni/jni-12.html#classops
3349N/A */
3349N/Ainline jclass
3349N/AFindClassInLoader(JNIEnv* env, jobject aObjectLoader, const char* aClassName)
3349N/A{
3349N/A jclass clazz = nsnull;
3349N/A jstring name = env->NewStringUTF(aClassName);
3349N/A if (name)
3349N/A clazz = (jclass) env->CallStaticObjectMethod(javaXPCOMUtilsClass,
3349N/A findClassInLoaderMID, aObjectLoader, name);
3349N/A
3349N/A#ifdef DEBUG
3349N/A if (!clazz)
3349N/A fprintf(stderr, "WARNING: failed to find class [%s]\n", aClassName);
3349N/A#endif
3349N/A return clazz;
3349N/A}
3349N/A
3349N/A
3349N/A/*******************************
3349N/A * JNI helper functions
3349N/A *******************************/
3349N/A
3349N/A/**
3349N/A * Returns a pointer to the appropriate JNIEnv structure. This function is
3349N/A * useful in callbacks or other functions that are not called directly from
3349N/A * Java and therefore do not have the JNIEnv structure passed in.
3349N/A *
3349N/A * @return pointer to JNIEnv structure for current thread
3349N/A */
3349N/AJNIEnv* GetJNIEnv();
3349N/A
3349N/A/**
3349N/A * Constructs and throws an exception. Some error codes (such as
3349N/A * NS_ERROR_OUT_OF_MEMORY) are handled by the appropriate Java exception/error.
3349N/A * Otherwise, an instance of XPCOMException is created with the given error
3349N/A * code and message.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aErrorCode The error code returned by an XPCOM/Gecko function. Pass
3349N/A * zero for the default behaviour.
3349N/A * @param aMessage A string that provides details for throwing this
3349N/A * exception. Pass in <code>nsnull</code> for the default
3349N/A * behaviour.
3349N/A *
3349N/A * @throws OutOfMemoryError if aErrorCode == NS_ERROR_OUT_OF_MEMORY
3349N/A * XPCOMException for all other error codes
3349N/A */
3349N/Avoid ThrowException(JNIEnv* env, const nsresult aErrorCode,
3349N/A const char* aMessage);
3349N/A
3349N/A/**
3349N/A * Helper functions for converting from java.lang.String to
3349N/A * nsAString/nsACstring. Caller must delete nsAString/nsACString.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aString Java string to convert
3349N/A *
3349N/A * @return nsAString/nsACString with same content as given Java string;
3349N/A * a 'void' nsAString/nsACString object if aString is
3349N/A * <code>null</code>; or <code>nsnull</code> if out of memory
3349N/A */
3349N/AnsAString* jstring_to_nsAString(JNIEnv* env, jstring aString);
3349N/AnsACString* jstring_to_nsACString(JNIEnv* env, jstring aString);
3349N/A
3349N/A/**
3349N/A * Helper function for converting from java.io.File to nsILocalFile.
3349N/A *
3349N/A * @param env Java environment pointer
3349N/A * @param aFile Java File to convert
3349N/A * @param aLocalFile returns the converted nsILocalFile
3349N/A *
3349N/A * @return NS_OK for success; other values indicate error in conversion
3349N/A */
3349N/Ansresult File_to_nsILocalFile(JNIEnv* env, jobject aFile,
3349N/A nsILocalFile** aLocalFile);
3349N/A
3349N/A#endif // _nsJavaXPCOMBindingUtils_h_
3349N/A