Back to index

lightning-sunbird  0.9+nobinonly
nsJavaXPCOMBindingUtils.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Java XPCOM Bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Javier Pedemonte (jhpedemonte@gmail.com)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _nsJavaXPCOMBindingUtils_h_
00039 #define _nsJavaXPCOMBindingUtils_h_
00040 
00041 #include "jni.h"
00042 #include "xptcall.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsString.h"
00045 #include "pldhash.h"
00046 #include "nsJavaXPTCStub.h"
00047 #include "nsAutoLock.h"
00048 #include "nsTHashtable.h"
00049 #include "nsHashKeys.h"
00050 
00051 //#define DEBUG_JAVAXPCOM
00052 //#define DEBUG_JAVAXPCOM_REFCNT
00053 
00054 #ifdef DEBUG_JAVAXPCOM
00055 #define LOG(x)  printf x
00056 #else
00057 #define LOG(x)  /* nothing */
00058 #endif
00059 
00060 
00061 /*********************
00062  * Java JNI globals
00063  *********************/
00064 
00065 extern jclass systemClass;
00066 extern jclass booleanClass;
00067 extern jclass charClass;
00068 extern jclass byteClass;
00069 extern jclass shortClass;
00070 extern jclass intClass;
00071 extern jclass longClass;
00072 extern jclass floatClass;
00073 extern jclass doubleClass;
00074 extern jclass stringClass;
00075 extern jclass nsISupportsClass;
00076 extern jclass xpcomExceptionClass;
00077 extern jclass xpcomJavaProxyClass;
00078 extern jclass weakReferenceClass;
00079 extern jclass javaXPCOMUtilsClass;
00080 
00081 extern jmethodID hashCodeMID;
00082 extern jmethodID booleanValueMID;
00083 extern jmethodID booleanInitMID;
00084 extern jmethodID charValueMID;
00085 extern jmethodID charInitMID;
00086 extern jmethodID byteValueMID;
00087 extern jmethodID byteInitMID;
00088 extern jmethodID shortValueMID;
00089 extern jmethodID shortInitMID;
00090 extern jmethodID intValueMID;
00091 extern jmethodID intInitMID;
00092 extern jmethodID longValueMID;
00093 extern jmethodID longInitMID;
00094 extern jmethodID floatValueMID;
00095 extern jmethodID floatInitMID;
00096 extern jmethodID doubleValueMID;
00097 extern jmethodID doubleInitMID;
00098 extern jmethodID createProxyMID;
00099 extern jmethodID isXPCOMJavaProxyMID;
00100 extern jmethodID getNativeXPCOMInstMID;
00101 extern jmethodID weakReferenceConstructorMID;
00102 extern jmethodID getReferentMID;
00103 extern jmethodID clearReferentMID;
00104 extern jmethodID findClassInLoaderMID;
00105 
00106 #ifdef DEBUG_JAVAXPCOM
00107 extern jmethodID getNameMID;
00108 extern jmethodID proxyToStringMID;
00109 #endif
00110 
00111 class NativeToJavaProxyMap;
00112 extern NativeToJavaProxyMap* gNativeToJavaProxyMap;
00113 class JavaToXPTCStubMap;
00114 extern JavaToXPTCStubMap* gJavaToXPTCStubMap;
00115 
00116 extern nsTHashtable<nsDepCharHashKey>* gJavaKeywords;
00117 
00118 // The Java garbage collector runs in a separate thread.  Since it calls the
00119 // finalizeProxy() function in nsJavaWrapper.cpp, we need to make sure that
00120 // all the structures touched by finalizeProxy() are multithread aware.
00121 extern PRLock* gJavaXPCOMLock;
00122 
00123 extern PRBool gJavaXPCOMInitialized;
00124 
00130 PRBool InitializeJavaGlobals(JNIEnv *env);
00131 
00136 void FreeJavaGlobals(JNIEnv* env);
00137 
00138 
00139 /*************************
00140  *  JavaXPCOMInstance
00141  *************************/
00142 
00143 class JavaXPCOMInstance
00144 {
00145 public:
00146   JavaXPCOMInstance(nsISupports* aInstance, nsIInterfaceInfo* aIInfo);
00147   ~JavaXPCOMInstance();
00148 
00149   nsISupports* GetInstance()  { return mInstance; }
00150   nsIInterfaceInfo* InterfaceInfo() { return mIInfo; }
00151 
00152 private:
00153   nsISupports*        mInstance;
00154   nsIInterfaceInfo*   mIInfo;
00155 };
00156 
00157 
00158 /**************************************
00159  *  Java<->XPCOM object mappings
00160  **************************************/
00161 
00165 class NativeToJavaProxyMap
00166 {
00167   friend PLDHashOperator DestroyJavaProxyMappingEnum(PLDHashTable* aTable,
00168                                                      PLDHashEntryHdr* aHeader,
00169                                                      PRUint32 aNumber,
00170                                                      void* aData);
00171 
00172 protected:
00173   struct ProxyList
00174   {
00175     ProxyList(const jobject aRef, const nsIID& aIID, ProxyList* aList)
00176       : javaObject(aRef)
00177       , iid(aIID)
00178       , next(aList)
00179     { }
00180 
00181     const jobject   javaObject;
00182     const nsIID     iid;
00183     ProxyList*      next;
00184   };
00185 
00186   struct Entry : public PLDHashEntryHdr
00187   {
00188     nsISupports*  key;
00189     ProxyList*    list;
00190   };
00191 
00192 public:
00193   NativeToJavaProxyMap()
00194     : mHashTable(nsnull)
00195   { }
00196 
00197   ~NativeToJavaProxyMap()
00198   {
00199     NS_ASSERTION(mHashTable == nsnull,
00200                  "MUST call Destroy() before deleting object");
00201   }
00202 
00203   nsresult Init();
00204 
00205   nsresult Destroy(JNIEnv* env);
00206 
00207   nsresult Add(JNIEnv* env, nsISupports* aXPCOMObject, const nsIID& aIID,
00208                jobject aProxy);
00209 
00210   nsresult Find(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID,
00211                 jobject* aResult);
00212 
00213   nsresult Remove(JNIEnv* env, nsISupports* aNativeObject, const nsIID& aIID);
00214 
00215 protected:
00216   PLDHashTable* mHashTable;
00217 };
00218 
00222 class JavaToXPTCStubMap
00223 {
00224   friend PLDHashOperator DestroyXPTCMappingEnum(PLDHashTable* aTable,
00225                                                 PLDHashEntryHdr* aHeader,
00226                                                 PRUint32 aNumber, void* aData);
00227 
00228 protected:
00229   struct Entry : public PLDHashEntryHdr
00230   {
00231     jint              key;
00232     nsJavaXPTCStub*   xptcstub;
00233   };
00234 
00235 public:
00236   JavaToXPTCStubMap()
00237     : mHashTable(nsnull)
00238   { }
00239 
00240   ~JavaToXPTCStubMap()
00241   {
00242     NS_ASSERTION(mHashTable == nsnull,
00243                  "MUST call Destroy() before deleting object");
00244   }
00245 
00246   nsresult Init();
00247 
00248   nsresult Destroy();
00249 
00250   nsresult Add(jint aJavaObjectHashCode, nsJavaXPTCStub* aProxy);
00251 
00252   nsresult Find(jint aJavaObjectHashCode, const nsIID& aIID,
00253                 nsJavaXPTCStub** aResult);
00254 
00255   nsresult Remove(jint aJavaObjectHashCode);
00256 
00257 protected:
00258   PLDHashTable* mHashTable;
00259 };
00260 
00261 
00262 /*******************************
00263  *  Helper functions
00264  *******************************/
00265 
00279 nsresult GetNewOrUsedJavaObject(JNIEnv* env, nsISupports* aXPCOMObject,
00280                                 const nsIID& aIID, jobject aObjectLoader,
00281                                 jobject* aResult);
00282 
00294 nsresult GetNewOrUsedXPCOMObject(JNIEnv* env, jobject aJavaObject,
00295                                  const nsIID& aIID, nsISupports** aResult);
00296 
00297 nsresult GetIIDForMethodParam(nsIInterfaceInfo *iinfo,
00298                               const nsXPTMethodInfo *methodInfo,
00299                               const nsXPTParamInfo &paramInfo,
00300                               PRUint8 paramType, PRUint16 methodIndex,
00301                               nsXPTCMiniVariant *dispatchParams,
00302                               PRBool isFullVariantArray,
00303                               nsID &result);
00304 
00318 inline jclass
00319 FindClassInLoader(JNIEnv* env, jobject aObjectLoader, const char* aClassName)
00320 {
00321   jclass clazz = nsnull;
00322   jstring name = env->NewStringUTF(aClassName);
00323   if (name)
00324     clazz = (jclass) env->CallStaticObjectMethod(javaXPCOMUtilsClass,
00325                                   findClassInLoaderMID, aObjectLoader, name);
00326 
00327 #ifdef DEBUG
00328   if (!clazz)
00329     fprintf(stderr, "WARNING: failed to find class [%s]\n", aClassName);
00330 #endif
00331   return clazz;
00332 }
00333 
00334 
00335 /*******************************
00336  *  JNI helper functions
00337  *******************************/
00338 
00346 JNIEnv* GetJNIEnv();
00347 
00364 void ThrowException(JNIEnv* env, const nsresult aErrorCode,
00365                     const char* aMessage);
00366 
00378 nsAString* jstring_to_nsAString(JNIEnv* env, jstring aString);
00379 nsACString* jstring_to_nsACString(JNIEnv* env, jstring aString);
00380 
00390 nsresult File_to_nsILocalFile(JNIEnv* env, jobject aFile,
00391                               nsILocalFile** aLocalFile);
00392 
00393 #endif // _nsJavaXPCOMBindingUtils_h_