1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/* ***** 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 jclass javaXPCOMUtilsClass;
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;
extern jmethodID findClassInLoaderMID;
#ifdef DEBUG_JAVAXPCOM
extern jmethodID getNameMID;
extern jmethodID proxyToStringMID;
#endif
class NativeToJavaProxyMap;
extern NativeToJavaProxyMap* gNativeToJavaProxyMap;
class JavaToXPTCStubMap;
extern JavaToXPTCStubMap* gJavaToXPTCStubMap;
extern nsTHashtable<nsDepCharHashKey>* 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 aObjectLoader Java object whose class loader we use for finding
* classes; can be null
* @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 aObjectLoader,
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);
/**
* Returns the Class object associated with the class or interface with the
* given string name, using the class loader of the given object.
*
* @param env Java environment pointer
* @param aObjectLoader Java object whose class loader is used to load class
* @param aClassName fully qualified name of class to load
*
* @return java.lang.Class object of requested Class; NULL if the class
* wasn't found
*
* @see http://java.sun.com/j2se/1.3/docs/guide/jni/jni-12.html#classops
*/
inline jclass
FindClassInLoader(JNIEnv* env, jobject aObjectLoader, const char* aClassName)
{
jclass clazz = nsnull;
jstring name = env->NewStringUTF(aClassName);
if (name)
clazz = (jclass) env->CallStaticObjectMethod(javaXPCOMUtilsClass,
findClassInLoaderMID, aObjectLoader, name);
#ifdef DEBUG
if (!clazz)
fprintf(stderr, "WARNING: failed to find class [%s]\n", aClassName);
#endif
return clazz;
}
/*******************************
* 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 <code>nsnull</code> 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;
* a 'void' nsAString/nsACString object if aString is
* <code>null</code>; or <code>nsnull</code> 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_
|