Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
nsJavaXPCOMBindingUtils.h File Reference
#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"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  JavaXPCOMInstance
class  NativeToJavaProxyMap
 Maps native XPCOM objects to their associated Java proxy object. More...
struct  NativeToJavaProxyMap::ProxyList
struct  NativeToJavaProxyMap::Entry
class  JavaToXPTCStubMap
 Maps Java objects to their associated nsJavaXPTCStub. More...
struct  JavaToXPTCStubMap::Entry

Defines

#define LOG(x)   /* nothing */

Functions

PRBool InitializeJavaGlobals (JNIEnv *env)
 Initialize global structures used by JavaXPCOM.
void FreeJavaGlobals (JNIEnv *env)
 Frees global structures that were allocated by InitializeJavaGlobals().
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)
jclass FindClassInLoader (JNIEnv *env, jobject aObjectLoader, const char *aClassName)
 Returns the Class object associated with the class or interface with the given string name, using the class loader of the given object.
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

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

Define Documentation

#define LOG (   x)    /* nothing */

Definition at line 57 of file nsJavaXPCOMBindingUtils.h.


Function Documentation

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:

jclass FindClassInLoader ( JNIEnv env,
jobject  aObjectLoader,
const char *  aClassName 
) [inline]

Returns the Class object associated with the class or interface with the given string name, using the class loader of the given object.

Parameters:
envJava environment pointer
aObjectLoaderJava object whose class loader is used to load class
aClassNamefully qualified name of class to load
Returns:
java.lang.Class object of requested Class; NULL if the class wasn't found
See also:
http://java.sun.com/j2se/1.3/docs/guide/jni/jni-12.html#classops

Definition at line 319 of file nsJavaXPCOMBindingUtils.h.

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

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 227 of file nsJavaXPCOMGlue.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.
  jthrowable throwObj = nsnull;
  jclass exceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException");
  if (exceptionClass) {
    jmethodID mid = env->GetMethodID(exceptionClass, "<init>",
                                     "(JLjava/lang/String;)V");
    if (mid) {
      throwObj = (jthrowable) env->NewObject(exceptionClass, mid,
                                             (PRInt64) aErrorCode,
                                             env->NewStringUTF(aMessage));
    }
  }
  NS_ASSERTION(throwObj, "Failed to create XPCOMException object");

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

Here is the call graph for this function:

Here is the caller 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 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.

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.