Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
nsJavaXPCOMBindingUtils.cpp File Reference
#include "nsJavaXPCOMBindingUtils.h"
#include "nsJavaXPTCStub.h"
#include "nsJavaWrapper.h"
#include "jni.h"
#include "nsIInterfaceInfoManager.h"
#include "nsILocalFile.h"
#include "nsEventQueueUtils.h"
#include "nsProxyRelease.h"

Go to the source code of this file.

Functions

PRBool InitializeJavaGlobals (JNIEnv *env)
 Initialize global structures used by JavaXPCOM.
void FreeJavaGlobals (JNIEnv *env)
 Frees global structures that were allocated by InitializeJavaGlobals().
PLDHashOperator DestroyJavaProxyMappingEnum (PLDHashTable *aTable, PLDHashEntryHdr *aHeader, PRUint32 aNumber, void *aData)
PLDHashOperator DestroyXPTCMappingEnum (PLDHashTable *aTable, PLDHashEntryHdr *aHeader, PRUint32 aNumber, void *aData)
nsresult GetNewOrUsedJavaObject (JNIEnv *env, nsISupports *aXPCOMObject, const nsIID &aIID, jobject aObjectLoader, jobject *aResult)
 Finds the associated Java object for the given XPCOM object and IID.
nsresult GetNewOrUsedXPCOMObject (JNIEnv *env, jobject aJavaObject, const nsIID &aIID, nsISupports **aResult)
 Finds the associated XPCOM object for the given Java object and IID.
nsresult GetIIDForMethodParam (nsIInterfaceInfo *iinfo, const nsXPTMethodInfo *methodInfo, const nsXPTParamInfo &paramInfo, PRUint8 paramType, PRUint16 methodIndex, nsXPTCMiniVariant *dispatchParams, PRBool isFullVariantArray, nsID &result)
JNIEnvGetJNIEnv ()
 Returns a pointer to the appropriate JNIEnv structure.
void ThrowException (JNIEnv *env, const nsresult aErrorCode, const char *aMessage)
 Constructs and throws an exception.
nsAString * jstring_to_nsAString (JNIEnv *env, jstring aString)
 Helper functions for converting from java.lang.String to nsAString/nsACstring.
nsACString * jstring_to_nsACString (JNIEnv *env, jstring aString)
nsresult File_to_nsILocalFile (JNIEnv *env, jobject aFile, nsILocalFile **aLocalFile)
 Helper function for converting from java.io.File to nsILocalFile.

Variables

JavaVMgCachedJVM = nsnull
jclass systemClass = nsnull
jclass booleanClass = nsnull
jclass charClass = nsnull
jclass byteClass = nsnull
jclass shortClass = nsnull
jclass intClass = nsnull
jclass longClass = nsnull
jclass floatClass = nsnull
jclass doubleClass = nsnull
jclass stringClass = nsnull
jclass nsISupportsClass = nsnull
jclass xpcomExceptionClass = nsnull
jclass xpcomJavaProxyClass = nsnull
jclass weakReferenceClass = nsnull
jclass javaXPCOMUtilsClass = nsnull
jmethodID hashCodeMID = nsnull
jmethodID booleanValueMID = nsnull
jmethodID booleanInitMID = nsnull
jmethodID charValueMID = nsnull
jmethodID charInitMID = nsnull
jmethodID byteValueMID = nsnull
jmethodID byteInitMID = nsnull
jmethodID shortValueMID = nsnull
jmethodID shortInitMID = nsnull
jmethodID intValueMID = nsnull
jmethodID intInitMID = nsnull
jmethodID longValueMID = nsnull
jmethodID longInitMID = nsnull
jmethodID floatValueMID = nsnull
jmethodID floatInitMID = nsnull
jmethodID doubleValueMID = nsnull
jmethodID doubleInitMID = nsnull
jmethodID createProxyMID = nsnull
jmethodID isXPCOMJavaProxyMID = nsnull
jmethodID getNativeXPCOMInstMID = nsnull
jmethodID weakReferenceConstructorMID = nsnull
jmethodID getReferentMID = nsnull
jmethodID clearReferentMID = nsnull
jmethodID findClassInLoaderMID = nsnull
NativeToJavaProxyMapgNativeToJavaProxyMap = nsnull
JavaToXPTCStubMapgJavaToXPTCStubMap = nsnull
PRBool gJavaXPCOMInitialized = PR_FALSE
PRLockgJavaXPCOMLock = nsnull
static const char * kJavaKeywords []
nsTHashtable< nsDepCharHashKey > * gJavaKeywords = nsnull
static PLDHashTableOps hash_ops

Function Documentation

PLDHashOperator DestroyJavaProxyMappingEnum ( PLDHashTable aTable,
PLDHashEntryHdr aHeader,
PRUint32  aNumber,
void aData 
)

Definition at line 480 of file nsJavaXPCOMBindingUtils.cpp.

{
  JNIEnv* env = NS_STATIC_CAST(JNIEnv*, aData);
  NativeToJavaProxyMap::Entry* entry =
                          NS_STATIC_CAST(NativeToJavaProxyMap::Entry*, aHeader);

  // first, delete XPCOM instances from the Java proxies
  nsresult rv;
  NativeToJavaProxyMap::ProxyList* item = entry->list;
  while(item != nsnull) {
    void* xpcom_obj;
    jobject javaObject = env->CallObjectMethod(item->javaObject, getReferentMID);
    rv = GetXPCOMInstFromProxy(env, javaObject, &xpcom_obj);
    NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get XPCOM instance from Java proxy");

    if (NS_SUCCEEDED(rv)) {
      JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, xpcom_obj);
#ifdef DEBUG_JAVAXPCOM
      char* iid_str = item->iid.ToString();
      LOG(("- NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n",
           (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID,
                                               javaObject),
           (PRUint32) entry, iid_str));
      PR_Free(iid_str);
#endif
      delete inst;  // releases native XPCOM object
    }

    NativeToJavaProxyMap::ProxyList* next = item->next;
    env->CallVoidMethod(item->javaObject, clearReferentMID);
    env->DeleteGlobalRef(item->javaObject);
    delete item;
    item = next;
  }

  return PL_DHASH_REMOVE;
}

Here is the call graph for this function:

PLDHashOperator DestroyXPTCMappingEnum ( PLDHashTable aTable,
PLDHashEntryHdr aHeader,
PRUint32  aNumber,
void aData 
)

Definition at line 670 of file nsJavaXPCOMBindingUtils.cpp.

{
  JavaToXPTCStubMap::Entry* entry =
                             NS_STATIC_CAST(JavaToXPTCStubMap::Entry*, aHeader);

  // The XPTC stub will be released by the XPCOM side, if it hasn't been
  // already.  We just need to delete the Java global ref held by the XPTC stub,
  // so the Java garbage collector can handle the Java object when necessary.
  entry->xptcstub->DeleteStrongRef();

  return PL_DHASH_REMOVE;
}

Here is the call graph for this function:

nsresult File_to_nsILocalFile ( JNIEnv env,
jobject  aFile,
nsILocalFile **  aLocalFile 
)

Helper function for converting from java.io.File to nsILocalFile.

Parameters:
envJava environment pointer
aFileJava File to convert
aLocalFilereturns the converted nsILocalFile
Returns:
NS_OK for success; other values indicate error in conversion

Definition at line 1091 of file nsJavaXPCOMBindingUtils.cpp.

{
  nsresult rv = NS_ERROR_FAILURE;
  jstring pathName = nsnull;
  jclass clazz = env->FindClass("java/io/File");
  if (clazz) {
    jmethodID pathMID = env->GetMethodID(clazz, "getCanonicalPath",
                                         "()Ljava/lang/String;");
    if (pathMID) {
      pathName = (jstring) env->CallObjectMethod(aFile, pathMID);
      if (pathName != nsnull && !env->ExceptionCheck())
        rv = NS_OK;
    }
  }

  if (NS_SUCCEEDED(rv)) {
    nsAString* path = jstring_to_nsAString(env, pathName);
    if (!path)
      rv = NS_ERROR_OUT_OF_MEMORY;

    if (NS_SUCCEEDED(rv)) {
      rv = NS_NewLocalFile(*path, false, aLocalFile);
      delete path;
    }
  }

  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Frees global structures that were allocated by InitializeJavaGlobals().

Parameters:
envJava environment pointer

Definition at line 352 of file nsJavaXPCOMBindingUtils.cpp.

{
  PRLock* tempLock = nsnull;
  if (gJavaXPCOMLock) {
    PR_Lock(gJavaXPCOMLock);

    // null out global lock so no one else can use it
    tempLock = gJavaXPCOMLock;
    gJavaXPCOMLock = nsnull;
  }

  gJavaXPCOMInitialized = PR_FALSE;

  // Free the mappings first, since that process depends on some of the Java
  // globals that are freed later.
  if (gNativeToJavaProxyMap) {
    gNativeToJavaProxyMap->Destroy(env);
    delete gNativeToJavaProxyMap;
    gNativeToJavaProxyMap = nsnull;
  }
  if (gJavaToXPTCStubMap) {
    gJavaToXPTCStubMap->Destroy();
    delete gJavaToXPTCStubMap;
    gJavaToXPTCStubMap = nsnull;
  }

  // Free remaining Java globals
  if (systemClass) {
    env->DeleteGlobalRef(systemClass);
    systemClass = nsnull;
  }
  if (booleanClass) {
    env->DeleteGlobalRef(booleanClass);
    booleanClass = nsnull;
  }
  if (charClass) {
    env->DeleteGlobalRef(charClass);
    charClass = nsnull;
  }
  if (byteClass) {
    env->DeleteGlobalRef(byteClass);
    byteClass = nsnull;
  }
  if (shortClass) {
    env->DeleteGlobalRef(shortClass);
    shortClass = nsnull;
  }
  if (intClass) {
    env->DeleteGlobalRef(intClass);
    intClass = nsnull;
  }
  if (longClass) {
    env->DeleteGlobalRef(longClass);
    longClass = nsnull;
  }
  if (floatClass) {
    env->DeleteGlobalRef(floatClass);
    floatClass = nsnull;
  }
  if (doubleClass) {
    env->DeleteGlobalRef(doubleClass);
    doubleClass = nsnull;
  }
  if (stringClass) {
    env->DeleteGlobalRef(stringClass);
    stringClass = nsnull;
  }
  if (nsISupportsClass) {
    env->DeleteGlobalRef(nsISupportsClass);
    nsISupportsClass = nsnull;
  }
  if (xpcomExceptionClass) {
    env->DeleteGlobalRef(xpcomExceptionClass);
    xpcomExceptionClass = nsnull;
  }
  if (xpcomJavaProxyClass) {
    env->DeleteGlobalRef(xpcomJavaProxyClass);
    xpcomJavaProxyClass = nsnull;
  }
  if (weakReferenceClass) {
    env->DeleteGlobalRef(weakReferenceClass);
    weakReferenceClass = nsnull;
  }

  if (gJavaKeywords) {
    delete gJavaKeywords;
    gJavaKeywords = nsnull;
  }

  if (tempLock) {
    PR_Unlock(tempLock);
    PR_DestroyLock(tempLock);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetIIDForMethodParam ( nsIInterfaceInfo iinfo,
const nsXPTMethodInfo methodInfo,
const nsXPTParamInfo paramInfo,
PRUint8  paramType,
PRUint16  methodIndex,
nsXPTCMiniVariant dispatchParams,
PRBool  isFullVariantArray,
nsID result 
)

Definition at line 919 of file nsJavaXPCOMBindingUtils.cpp.

{
  nsresult rv;

  switch (paramType)
  {
    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))
        break;

      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) {
        rv = NS_ERROR_UNEXPECTED;
        break;
      }

      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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Returns:
pointer to JNIEnv structure for current thread

Definition at line 974 of file nsJavaXPCOMBindingUtils.cpp.

{
  JNIEnv* env;
  jint rc = gCachedJVM->GetEnv((void**) &env, JNI_VERSION_1_2);
  NS_ASSERTION(rc == JNI_OK && env != nsnull,
               "Current thread not attached to given JVM instance");
  return env;
}

Here is the caller graph for this function:

nsresult GetNewOrUsedJavaObject ( JNIEnv env,
nsISupports *  aXPCOMObject,
const nsIID aIID,
jobject  aObjectLoader,
jobject aResult 
)

Finds the associated Java object for the given XPCOM object and IID.

If no such Java object exists, then it creates one.

Parameters:
envJava environment pointer
aXPCOMObjectXPCOM object for which to find/create Java object
aIIDdesired interface IID for Java object
aObjectLoaderJava object whose class loader we use for finding classes; can be null
aResulton success, holds reference to Java object
Returns:
NS_OK if succeeded; all other return values are error codes.

Definition at line 809 of file nsJavaXPCOMBindingUtils.cpp.

{
  NS_PRECONDITION(aResult != nsnull, "null ptr");
  if (!aResult)
    return NS_ERROR_NULL_POINTER;

  nsresult rv;
  nsJavaXPTCStub* stub = nsnull;
  aXPCOMObject->QueryInterface(NS_GET_IID(nsJavaXPTCStub), (void**) &stub);
  if (stub) {
    // Get Java object directly from nsJavaXPTCStub
    *aResult = stub->GetJavaObject();
    NS_ASSERTION(*aResult != nsnull, "nsJavaXPTCStub w/o matching Java object");
    NS_RELEASE(stub);
    return NS_OK;
  }

  // Get the root nsISupports of the xpcom object
  nsCOMPtr<nsISupports> rootObject = do_QueryInterface(aXPCOMObject, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  // Get associated Java object from hash table
  rv = gNativeToJavaProxyMap->Find(env, rootObject, aIID, aResult);
  NS_ENSURE_SUCCESS(rv, rv);
  if (*aResult)
    return NS_OK;

  // No Java object is associated with the given XPCOM object, so we
  // create a Java proxy.
  return CreateJavaProxy(env, rootObject, aIID, aObjectLoader, aResult);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetNewOrUsedXPCOMObject ( JNIEnv env,
jobject  aJavaObject,
const nsIID aIID,
nsISupports **  aResult 
)

Finds the associated XPCOM object for the given Java object and IID.

If no such XPCOM object exists, then it creates one.

Parameters:
envJava environment pointer
aJavaObjectJava object for which to find/create XPCOM object
aIIDdesired interface IID for XPCOM object
aResulton success, holds AddRef'd reference to XPCOM object
Returns:
NS_OK if succeeded; all other return values are error codes.

Definition at line 844 of file nsJavaXPCOMBindingUtils.cpp.

{
  NS_PRECONDITION(aResult != nsnull, "null ptr");
  if (!aResult)
    return NS_ERROR_NULL_POINTER;

  nsresult rv;
  *aResult = nsnull;

  // Check if the given Java object is actually one of our Java proxies.  If so,
  // then we query the associated XPCOM object directly from the proxy.
  // If Java object is not a proxy, then we try to find associated XPCOM object
  // in the mapping table.
  jboolean isProxy = env->CallStaticBooleanMethod(xpcomJavaProxyClass,
                                                  isXPCOMJavaProxyMID,
                                                  aJavaObject);
  if (env->ExceptionCheck())
    return NS_ERROR_FAILURE;

  if (isProxy) {
    void* inst;
    rv = GetXPCOMInstFromProxy(env, aJavaObject, &inst);
    NS_ENSURE_SUCCESS(rv, rv);

    nsISupports* rootObject =
              NS_STATIC_CAST(JavaXPCOMInstance*, inst)->GetInstance();
    rv = rootObject->QueryInterface(aIID, (void**) aResult);
    NS_ENSURE_SUCCESS(rv, rv);

    return NS_OK;
  }

  nsJavaXPTCStub* stub;
  jint hash = env->CallStaticIntMethod(systemClass, hashCodeMID, aJavaObject);
  rv = gJavaToXPTCStubMap->Find(hash, aIID, &stub);
  NS_ENSURE_SUCCESS(rv, rv);
  if (stub) {
    // stub is already AddRef'd and QI'd
    *aResult = NS_STATIC_CAST(nsISupports*,
                              NS_STATIC_CAST(nsXPTCStubBase*, stub));
    return NS_OK;
  }

  // If there is no corresponding XPCOM object, then that means that the
  // parameter is a 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;
  rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
  NS_ENSURE_SUCCESS(rv, rv);

  // Create XPCOM stub
  stub = new nsJavaXPTCStub(aJavaObject, iinfo);
  if (!stub) {
    return NS_ERROR_OUT_OF_MEMORY;
  }
  rv = gJavaToXPTCStubMap->Add(hash, stub);
  if (NS_FAILED(rv)) {
    delete stub;
    return rv;
  }

  NS_ADDREF(stub);
  *aResult = NS_STATIC_CAST(nsISupports*,
                            NS_STATIC_CAST(nsXPTCStubBase*, stub));

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Initialize global structures used by JavaXPCOM.

Parameters:
envJava environment pointer
Returns:
PR_TRUE if JavaXPCOM is initialized; PR_FALSE if an error occurred

Definition at line 138 of file nsJavaXPCOMBindingUtils.cpp.

{
  if (gJavaXPCOMInitialized)
    return PR_TRUE;

  // Save pointer to JavaVM, which is valid across threads.
  jint rc = env->GetJavaVM(&gCachedJVM);
  if (rc != 0) {
    NS_WARNING("Failed to get JavaVM");
    goto init_error;
  }

  jclass clazz;
  if (!(clazz = env->FindClass("java/lang/System")) ||
      !(systemClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(hashCodeMID = env->GetStaticMethodID(clazz, "identityHashCode",
                                             "(Ljava/lang/Object;)I")))
  {
    NS_WARNING("Problem creating java.lang.System globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Boolean")) ||
      !(booleanClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(booleanValueMID = env->GetMethodID(clazz, "booleanValue", "()Z")) ||
      !(booleanInitMID = env->GetMethodID(clazz, "<init>", "(Z)V")))
  {
    NS_WARNING("Problem creating java.lang.Boolean globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Character")) ||
      !(charClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(charValueMID = env->GetMethodID(clazz, "charValue", "()C")) ||
      !(charInitMID = env->GetMethodID(clazz, "<init>", "(C)V")))
  {
    NS_WARNING("Problem creating java.lang.Character globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Byte")) ||
      !(byteClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(byteValueMID = env->GetMethodID(clazz, "byteValue", "()B")) ||
      !(byteInitMID = env->GetMethodID(clazz, "<init>", "(B)V")))
  {
    NS_WARNING("Problem creating java.lang.Byte globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Short")) ||
      !(shortClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(shortValueMID = env->GetMethodID(clazz, "shortValue", "()S")) ||
      !(shortInitMID = env->GetMethodID(clazz, "<init>", "(S)V")))
  {
    NS_WARNING("Problem creating java.lang.Short globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Integer")) ||
      !(intClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(intValueMID = env->GetMethodID(clazz, "intValue", "()I")) ||
      !(intInitMID = env->GetMethodID(clazz, "<init>", "(I)V")))
  {
    NS_WARNING("Problem creating java.lang.Integer globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Long")) ||
      !(longClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(longValueMID = env->GetMethodID(clazz, "longValue", "()J")) ||
      !(longInitMID = env->GetMethodID(clazz, "<init>", "(J)V")))
  {
    NS_WARNING("Problem creating java.lang.Long globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Float")) ||
      !(floatClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(floatValueMID = env->GetMethodID(clazz, "floatValue", "()F")) ||
      !(floatInitMID = env->GetMethodID(clazz, "<init>", "(F)V")))
  {
    NS_WARNING("Problem creating java.lang.Float globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/Double")) ||
      !(doubleClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(doubleValueMID = env->GetMethodID(clazz, "doubleValue", "()D")) ||
      !(doubleInitMID = env->GetMethodID(clazz, "<init>", "(D)V")))
  {
    NS_WARNING("Problem creating java.lang.Double globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/String")) ||
      !(stringClass = (jclass) env->NewGlobalRef(clazz)))
  {
    NS_WARNING("Problem creating java.lang.String globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("org/mozilla/interfaces/nsISupports")) ||
      !(nsISupportsClass = (jclass) env->NewGlobalRef(clazz)))
  {
    NS_WARNING("Problem creating org.mozilla.interfaces.nsISupports globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("org/mozilla/xpcom/XPCOMException")) ||
      !(xpcomExceptionClass = (jclass) env->NewGlobalRef(clazz)))
  {
    NS_WARNING("Problem creating org.mozilla.xpcom.XPCOMException globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMJavaProxy")) ||
      !(xpcomJavaProxyClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(createProxyMID = env->GetStaticMethodID(clazz, "createProxy",
                                   "(Ljava/lang/Class;J)Ljava/lang/Object;")) ||
      !(isXPCOMJavaProxyMID = env->GetStaticMethodID(clazz, "isXPCOMJavaProxy",
                                                    "(Ljava/lang/Object;)Z")) ||
      !(getNativeXPCOMInstMID = env->GetStaticMethodID(xpcomJavaProxyClass,
                                                       "getNativeXPCOMInstance",
                                                       "(Ljava/lang/Object;)J")))
  {
    NS_WARNING("Problem creating org.mozilla.xpcom.internal.XPCOMJavaProxy globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("java/lang/ref/WeakReference")) ||
      !(weakReferenceClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(weakReferenceConstructorMID = env->GetMethodID(weakReferenceClass, 
                                           "<init>","(Ljava/lang/Object;)V")) ||
      !(getReferentMID = env->GetMethodID(weakReferenceClass,
                                          "get", "()Ljava/lang/Object;")) ||
      !(clearReferentMID = env->GetMethodID(weakReferenceClass, 
                                            "clear", "()V")))
  {
    NS_WARNING("Problem creating java.lang.ref.WeakReference globals");
    goto init_error;
  }

  if (!(clazz = env->FindClass("org/mozilla/xpcom/internal/JavaXPCOMMethods")) ||
      !(javaXPCOMUtilsClass = (jclass) env->NewGlobalRef(clazz)) ||
      !(findClassInLoaderMID = env->GetStaticMethodID(clazz,
                    "findClassInLoader",
                    "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Class;")))
  {
    NS_WARNING("Problem creating org.mozilla.xpcom.internal.JavaXPCOMMethods globals");
    goto init_error;
  }

#ifdef DEBUG_JAVAXPCOM
  if (!(clazz = env->FindClass("java/lang/Class")) ||
      !(getNameMID = env->GetMethodID(clazz, "getName","()Ljava/lang/String;")))
  {
    NS_WARNING("Problem creating java.lang.Class globals");
    goto init_error;
  }

  if (!(proxyToStringMID = env->GetStaticMethodID(xpcomJavaProxyClass,
                                                  "proxyToString",
                                     "(Ljava/lang/Object;)Ljava/lang/String;")))
  {
    NS_WARNING("Problem creating proxyToString global");
    goto init_error;
  }
#endif

  gNativeToJavaProxyMap = new NativeToJavaProxyMap();
  if (!gNativeToJavaProxyMap || NS_FAILED(gNativeToJavaProxyMap->Init())) {
    NS_WARNING("Problem creating NativeToJavaProxyMap");
    goto init_error;
  }
  gJavaToXPTCStubMap = new JavaToXPTCStubMap();
  if (!gJavaToXPTCStubMap || NS_FAILED(gJavaToXPTCStubMap->Init())) {
    NS_WARNING("Problem creating JavaToXPTCStubMap");
    goto init_error;
  }

  {
    nsresult rv = NS_OK;
    PRUint32 size = NS_ARRAY_LENGTH(kJavaKeywords);
    gJavaKeywords = new nsTHashtable<nsDepCharHashKey>();
    if (!gJavaKeywords || NS_FAILED(gJavaKeywords->Init(size))) {
      NS_WARNING("Failed to init JavaKeywords HashSet");
      goto init_error;
    }
    for (PRUint32 i = 0; i < size && NS_SUCCEEDED(rv); i++) {
      if (!gJavaKeywords->PutEntry(kJavaKeywords[i])) {
        rv = NS_ERROR_OUT_OF_MEMORY;
      }
    }
    if (NS_FAILED(rv)) {
      NS_WARNING("Failed to populate JavaKeywords hash");
      goto init_error;
    }
  }

  gJavaXPCOMLock = PR_NewLock();
  gJavaXPCOMInitialized = PR_TRUE;
  return PR_TRUE;

init_error:
  // If we encounter an error during initialization, then free any globals that
  // were allocated, and return false.
  FreeJavaGlobals(env);
  return PR_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsACString* jstring_to_nsACString ( JNIEnv env,
jstring  aString 
)

Definition at line 1069 of file nsJavaXPCOMBindingUtils.cpp.

{
  const char* buf = nsnull;
  if (aString) {
    buf = env->GetStringUTFChars(aString, nsnull);
    if (!buf)
      return nsnull;  // exception already thrown
  }

  nsCString* str = new nsCString(buf);

  if (aString) {
    env->ReleaseStringUTFChars(aString, buf);
  } else {
    str->SetIsVoid(PR_TRUE);
  }

  // returns string, or nsnull if 'new' failed
  return str;
}

Here is the caller graph for this function:

nsAString* jstring_to_nsAString ( JNIEnv env,
jstring  aString 
)

Helper functions for converting from java.lang.String to nsAString/nsACstring.

Caller must delete nsAString/nsACString.

Parameters:
envJava environment pointer
aStringJava string to convert
Returns:
nsAString/nsACString with same content as given Java string; a 'void' nsAString/nsACString object if aString is null; or nsnull if out of memory

Definition at line 1047 of file nsJavaXPCOMBindingUtils.cpp.

{
  const PRUnichar* buf = nsnull;
  if (aString) {
    buf = env->GetStringChars(aString, nsnull);
    if (!buf)
      return nsnull;  // exception already thrown
  }

  nsString* str = new nsString(buf);

  if (aString) {
    env->ReleaseStringChars(aString, buf);
  } else {
    str->SetIsVoid(PR_TRUE);
  }

  // returns string, or nsnull if 'new' failed
  return str;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ThrowException ( JNIEnv env,
const nsresult  aErrorCode,
const char *  aMessage 
)

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.

Parameters:
envJava environment pointer
aErrorCodeThe error code returned by an XPCOM/Gecko function. Pass zero for the default behaviour.
aMessageA string that provides details for throwing this exception. Pass in nsnull for the default behaviour.
Exceptions:
OutOfMemoryErrorif aErrorCode == NS_ERROR_OUT_OF_MEMORY XPCOMException for all other error codes

Definition at line 984 of file nsJavaXPCOMBindingUtils.cpp.

{
  // Only throw this exception if one hasn't already been thrown, so we don't
  // mask a previous exception/error.
  if (env->ExceptionCheck())
    return;

  // If the error code we get is for an Out Of Memory error, try to throw an
  // OutOfMemoryError.  The JVM may have enough memory to create this error.
  if (aErrorCode == NS_ERROR_OUT_OF_MEMORY) {
    jclass clazz = env->FindClass("java/lang/OutOfMemoryError");
    if (clazz) {
      env->ThrowNew(clazz, aMessage);
    }
    env->DeleteLocalRef(clazz);
    return;
  }

  // If the error was not handled above, then create an XPCOMException with the
  // given error code and message.

  // Create parameters and method signature. Max of 2 params.  The error code
  // comes before the message string.
  PRInt64 errorCode = aErrorCode ? aErrorCode : NS_ERROR_FAILURE;
  nsCAutoString methodSig("(J");
  jstring message = nsnull;
  if (aMessage) {
    message = env->NewStringUTF(aMessage);
    if (!message) {
      return;
    }
    methodSig.AppendLiteral("Ljava/lang/String;");
  }
  methodSig.AppendLiteral(")V");

  // In some instances (such as in shutdownXPCOM() and termEmbedding()), we
  // will need to throw an exception when JavaXPCOM has already been
  // terminated.  In such a case, 'xpcomExceptionClass' will be null.  So we
  // reset it temporarily in order to throw the appropriate exception.
  if (xpcomExceptionClass == nsnull) {
    xpcomExceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException");
    if (!xpcomExceptionClass) {
      return;
    }
  }

  // create exception object
  jthrowable throwObj = nsnull;
  jmethodID mid = env->GetMethodID(xpcomExceptionClass, "<init>",
                                   methodSig.get());
  if (mid) {
    throwObj = (jthrowable) env->NewObject(xpcomExceptionClass, mid, errorCode,
                                           message);
  }
  NS_ASSERTION(throwObj, "Failed to create XPCOMException object");

  // throw exception
  if (throwObj) {
    env->Throw(throwObj);
  }
}

Here is the call graph for this function:


Variable Documentation

Definition at line 54 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 71 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 70 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 56 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 75 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 74 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 55 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 73 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 72 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 91 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 86 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 61 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 85 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 84 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 92 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 60 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 83 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 82 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 51 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 88 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 90 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 131 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 100 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 102 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 103 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 99 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 69 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 58 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 79 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 78 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 87 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 67 of file nsJavaXPCOMBindingUtils.cpp.

const char* kJavaKeywords[] [static]
Initial value:
 {
  "abstract", "default"  , "if"        , "private"     , "throw"       ,
  "boolean" , "do"       , "implements", "protected"   , "throws"      ,
  "break"   , "double"   , "import",     "public"      , "transient"   ,
  "byte"    , "else"     , "instanceof", "return"      , "try"         ,
  "case"    , "extends"  , "int"       , "short"       , "void"        ,
  "catch"   , "final"    , "interface" , "static"      , "volatile"    ,
  "char"    , "finally"  , "long"      , "super"       , "while"       ,
  "class"   , "float"    , "native"    , "switch"      ,
  "const"   , "for"      , "new"       , "synchronized",
  "continue", "goto"     , "package"   , "this"        ,
    
  "strictfp",
    
  "assert"  ,
    
  "enum"    ,
    
  "true"    , "false"    , "null"      ,
    
  "clone"   , "equals"   , "finalize"  , "getClass"    , "hashCode"    ,
  "notify"  , "notifyAll",  "wait"
}

Definition at line 105 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 59 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 81 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 80 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 63 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 57 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 77 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 76 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 62 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 53 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 66 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 89 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 64 of file nsJavaXPCOMBindingUtils.cpp.

Definition at line 65 of file nsJavaXPCOMBindingUtils.cpp.