2004-09-02 20:34:11 +04:00
|
|
|
/* ***** 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"
|
2004-12-18 01:25:12 +03:00
|
|
|
#include "nsString.h"
|
2005-01-14 03:09:49 +03:00
|
|
|
#include "pldhash.h"
|
2005-02-25 00:53:46 +03:00
|
|
|
#include "nsJavaXPTCStub.h"
|
2005-02-23 23:19:55 +03:00
|
|
|
#include "nsAutoLock.h"
|
2004-09-02 20:34:11 +04:00
|
|
|
|
2005-02-23 23:19:55 +03:00
|
|
|
//#define DEBUG_JAVAXPCOM
|
2005-02-25 00:53:46 +03:00
|
|
|
//#define DEBUG_JAVAXPCOM_REFCNT
|
|
|
|
|
2005-02-23 23:19:55 +03:00
|
|
|
#ifdef DEBUG_JAVAXPCOM
|
2004-11-23 20:02:21 +03:00
|
|
|
#define LOG(x) printf x
|
2004-09-02 20:34:11 +04:00
|
|
|
#else
|
2004-11-23 20:02:21 +03:00
|
|
|
#define LOG(x) /* nothing */
|
2004-09-02 20:34:11 +04:00
|
|
|
#endif
|
|
|
|
|
2005-01-19 01:53:50 +03:00
|
|
|
#if defined(XP_WIN) || defined(XP_MAC)
|
|
|
|
#define JX_EXPORT JNIEXPORT
|
|
|
|
#else
|
|
|
|
#define JX_EXPORT JNIEXPORT NS_EXPORT
|
|
|
|
#endif
|
|
|
|
|
2004-09-02 20:34:11 +04:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* Java JNI globals
|
|
|
|
*********************/
|
2005-02-25 00:53:46 +03:00
|
|
|
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jclass booleanClass;
|
|
|
|
extern jclass charClass;
|
|
|
|
extern jclass byteClass;
|
|
|
|
extern jclass shortClass;
|
|
|
|
extern jclass intClass;
|
|
|
|
extern jclass longClass;
|
|
|
|
extern jclass floatClass;
|
|
|
|
extern jclass doubleClass;
|
2004-12-17 21:01:19 +03:00
|
|
|
extern jclass stringClass;
|
|
|
|
extern jclass nsISupportsClass;
|
|
|
|
extern jclass xpcomExceptionClass;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jclass xpcomJavaProxyClass;
|
2004-09-02 20:34:11 +04:00
|
|
|
|
|
|
|
extern jmethodID hashCodeMID;
|
|
|
|
extern jmethodID booleanValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID booleanInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID charValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID charInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID byteValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID byteInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID shortValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID shortInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID intValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID intInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID longValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID longInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID floatValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID floatInitMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
extern jmethodID doubleValueMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID doubleInitMID;
|
|
|
|
extern jmethodID createProxyMID;
|
|
|
|
extern jmethodID isXPCOMJavaProxyMID;
|
|
|
|
extern jmethodID getNativeXPCOMInstMID;
|
2004-09-02 20:34:11 +04:00
|
|
|
|
2005-02-23 23:19:55 +03:00
|
|
|
#ifdef DEBUG_JAVAXPCOM
|
2004-12-17 21:01:19 +03:00
|
|
|
extern jmethodID getNameMID;
|
2005-02-23 23:19:55 +03:00
|
|
|
extern jmethodID proxyToStringMID;
|
2004-12-17 21:01:19 +03:00
|
|
|
#endif
|
2004-09-02 20:34:11 +04:00
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
class NativeToJavaProxyMap;
|
|
|
|
extern NativeToJavaProxyMap* gNativeToJavaProxyMap;
|
|
|
|
class JavaToXPTCStubMap;
|
|
|
|
extern JavaToXPTCStubMap* gJavaToXPTCStubMap;
|
2005-01-14 03:09:49 +03:00
|
|
|
|
2005-02-25 02:17:36 +03:00
|
|
|
// 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.
|
2005-03-15 00:18:53 +03:00
|
|
|
extern PRLock* gJavaXPCOMLock;
|
|
|
|
|
|
|
|
extern PRBool gJavaXPCOMInitialized;
|
2005-02-25 00:53:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize global structures used by Javaconnect.
|
|
|
|
* @param env Java environment pointer
|
|
|
|
* @return PR_TRUE if Javaconnect is initialized; PR_FALSE if an error occurred
|
|
|
|
*/
|
2004-09-02 20:34:11 +04:00
|
|
|
PRBool InitializeJavaGlobals(JNIEnv *env);
|
2005-02-25 00:53:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees global structures that were allocated by InitializeJavaGlobals().
|
|
|
|
* @param env Java environment pointer
|
|
|
|
*/
|
2004-09-02 20:34:11 +04:00
|
|
|
void FreeJavaGlobals(JNIEnv* env);
|
|
|
|
|
|
|
|
|
|
|
|
/*************************
|
|
|
|
* JavaXPCOMInstance
|
|
|
|
*************************/
|
2005-01-12 20:59:01 +03:00
|
|
|
|
2004-09-02 20:34:11 +04:00
|
|
|
class JavaXPCOMInstance
|
|
|
|
{
|
|
|
|
public:
|
2004-10-27 21:10:03 +04:00
|
|
|
JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo);
|
|
|
|
~JavaXPCOMInstance();
|
2004-09-02 20:34:11 +04:00
|
|
|
|
2004-12-23 20:09:54 +03:00
|
|
|
nsISupports* GetInstance() { return mInstance; }
|
|
|
|
nsIInterfaceInfo* InterfaceInfo() { return mIInfo; }
|
2004-09-02 20:34:11 +04:00
|
|
|
|
|
|
|
private:
|
2005-02-23 23:19:55 +03:00
|
|
|
nsISupports* mInstance;
|
|
|
|
nsIInterfaceInfo* mIInfo;
|
2004-09-02 20:34:11 +04:00
|
|
|
};
|
|
|
|
|
2004-09-16 01:06:30 +04:00
|
|
|
|
2004-09-02 20:34:11 +04:00
|
|
|
/**************************************
|
2005-02-25 00:53:46 +03:00
|
|
|
* Java<->XPCOM object mappings
|
2004-09-02 20:34:11 +04:00
|
|
|
**************************************/
|
2005-02-25 00:53:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps native XPCOM objects to their associated Java proxy object.
|
|
|
|
*/
|
|
|
|
class NativeToJavaProxyMap
|
2005-01-14 03:09:49 +03:00
|
|
|
{
|
2005-02-25 02:17:36 +03:00
|
|
|
friend PLDHashOperator DestroyJavaProxyMappingEnum(PLDHashTable* aTable,
|
|
|
|
PLDHashEntryHdr* aHeader,
|
|
|
|
PRUint32 aNumber,
|
|
|
|
void* aData);
|
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2005-01-14 03:09:49 +03:00
|
|
|
public:
|
2005-02-25 00:53:46 +03:00
|
|
|
NativeToJavaProxyMap()
|
|
|
|
: mHashTable(nsnull)
|
2005-01-14 03:09:49 +03:00
|
|
|
{ }
|
|
|
|
|
2005-02-25 02:17:36 +03:00
|
|
|
~NativeToJavaProxyMap()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mHashTable == nsnull,
|
|
|
|
"MUST call Destroy() before deleting object");
|
|
|
|
}
|
2005-01-14 03:09:49 +03:00
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
nsresult Init();
|
2005-01-14 03:09:49 +03:00
|
|
|
|
2005-02-25 02:17:36 +03:00
|
|
|
nsresult Destroy(JNIEnv* env);
|
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
nsresult Add(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID,
|
|
|
|
jobject aProxy);
|
2005-01-14 03:09:49 +03:00
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
nsresult Find(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID,
|
|
|
|
jobject* aResult);
|
2005-01-14 03:09:49 +03:00
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
nsresult Remove(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PLDHashTable* mHashTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps Java objects to their associated nsJavaXPTCStub.
|
|
|
|
*/
|
|
|
|
class JavaToXPTCStubMap
|
|
|
|
{
|
2005-02-25 02:17:36 +03:00
|
|
|
friend PLDHashOperator DestroyXPTCMappingEnum(PLDHashTable* aTable,
|
|
|
|
PLDHashEntryHdr* aHeader,
|
|
|
|
PRUint32 aNumber, void* aData);
|
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
protected:
|
|
|
|
struct Entry : public PLDHashEntryHdr
|
|
|
|
{
|
2005-02-25 02:17:36 +03:00
|
|
|
jint key;
|
|
|
|
nsJavaXPTCStub* xptcstub;
|
2005-02-25 00:53:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
JavaToXPTCStubMap()
|
|
|
|
: mHashTable(nsnull)
|
|
|
|
{ }
|
|
|
|
|
2005-02-25 02:17:36 +03:00
|
|
|
~JavaToXPTCStubMap()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mHashTable == nsnull,
|
|
|
|
"MUST call Destroy() before deleting object");
|
|
|
|
}
|
2005-02-25 00:53:46 +03:00
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2005-02-25 02:17:36 +03:00
|
|
|
nsresult Destroy();
|
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
nsresult Add(JNIEnv* env, jobject aJavaObject, nsJavaXPTCStub* aProxy);
|
|
|
|
|
|
|
|
nsresult Find(JNIEnv* env, jobject aJavaObject, const nsIID& aIID,
|
|
|
|
nsJavaXPTCStub** aResult);
|
|
|
|
|
|
|
|
nsresult Remove(JNIEnv* env, jobject aJavaObject);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PLDHashTable* mHashTable;
|
2005-01-14 03:09:49 +03:00
|
|
|
};
|
|
|
|
|
2004-09-02 20:34:11 +04:00
|
|
|
|
2005-02-25 00:53:46 +03:00
|
|
|
/*******************************
|
|
|
|
* 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
|
|
|
|
* @param aIsNewProxy on success, holds PR_TRUE if aResult points to newlyXPTCStub;
|
|
|
|
* created Java proxy; PR_FALSE otherwise
|
|
|
|
*
|
|
|
|
* @return NS_OK if succeeded; all other return values are error codes.
|
|
|
|
*/
|
|
|
|
nsresult GetNewOrUsedJavaObject(JNIEnv* env, nsISupports* aXPCOMObject,
|
|
|
|
const nsIID& aIID, jobject* aResult,
|
|
|
|
PRBool* aIsNewProxy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param aIsXPTCStub on success, holds PR_TRUE if aResult points to XPTCStub;
|
|
|
|
* PR_FALSE if aResult points to native 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,
|
|
|
|
PRBool* aIsXPTCStub);
|
2004-09-02 20:34:11 +04:00
|
|
|
|
|
|
|
nsresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
|
|
|
|
const nsXPTMethodInfo *methodInfo,
|
|
|
|
const nsXPTParamInfo ¶mInfo,
|
|
|
|
PRUint16 methodIndex,
|
|
|
|
nsXPTCMiniVariant *dispatchParams,
|
|
|
|
PRBool isFullVariantArray,
|
|
|
|
nsID &result);
|
|
|
|
|
2004-12-23 01:03:50 +03:00
|
|
|
|
2004-12-18 01:25:12 +03:00
|
|
|
/*******************************
|
|
|
|
* JNI helper functions
|
|
|
|
*******************************/
|
2004-12-23 01:03:50 +03:00
|
|
|
|
|
|
|
/**
|
2005-01-12 20:59:01 +03:00
|
|
|
* 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.
|
2004-12-23 01:03:50 +03:00
|
|
|
*
|
|
|
|
* @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 <code>nsnull</code> for the default
|
|
|
|
* behaviour.
|
2005-01-12 20:59:01 +03:00
|
|
|
*
|
|
|
|
* @throws OutOfMemoryError if aErrorCode == NS_ERROR_OUT_OF_MEMORY
|
|
|
|
* XPCOMException for all other error codes
|
2004-12-23 01:03:50 +03:00
|
|
|
*/
|
2005-01-12 20:59:01 +03:00
|
|
|
void ThrowException(JNIEnv* env, const nsresult aErrorCode,
|
|
|
|
const char* aMessage);
|
2004-12-23 01:03:50 +03:00
|
|
|
|
2004-12-23 01:36:57 +03:00
|
|
|
/**
|
|
|
|
* Helper functions for converting from java.lang.String to
|
2005-02-25 02:17:36 +03:00
|
|
|
* nsAString/nsACstring. Caller must delete nsAString/nsACString.
|
2004-12-23 01:36:57 +03:00
|
|
|
*
|
|
|
|
* @param env Java environment pointer
|
|
|
|
* @param aString Java string to convert
|
|
|
|
*
|
2005-01-12 20:59:01 +03:00
|
|
|
* @return nsAString/nsACString with same content as given Java string; or
|
|
|
|
* <code>nsnull</code> if out of memory
|
2004-12-23 01:36:57 +03:00
|
|
|
*/
|
2004-12-18 01:25:12 +03:00
|
|
|
nsAString* jstring_to_nsAString(JNIEnv* env, jstring aString);
|
|
|
|
nsACString* jstring_to_nsACString(JNIEnv* env, jstring aString);
|
|
|
|
|
2004-12-23 01:36:57 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2004-12-18 01:25:12 +03:00
|
|
|
nsresult File_to_nsILocalFile(JNIEnv* env, jobject aFile,
|
|
|
|
nsILocalFile** aLocalFile);
|
|
|
|
|
2004-09-02 20:34:11 +04:00
|
|
|
#endif // _nsJavaXPCOMBindingUtils_h_
|