Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsJavaWrapper.h File Reference
#include "jni.h"
#include "nsISupports.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

nsresult CreateJavaProxy (JNIEnv *env, nsISupports *aXPCOMObject, const nsIID &aIID, jobject aObjectLoader, jobject *aResult)
 Creates a Java proxy around an XPCOM C++ object.
nsresult GetXPCOMInstFromProxy (JNIEnv *env, jobject aJavaObject, void **aResult)
 Returns the XPCOM object for which the given Java proxy was created.

Function Documentation

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

Creates a Java proxy around an XPCOM C++ object.

Parameters:
envpointer to Java context
aXPCOMObjectXPCOM object to create proxy for
aIIDIID for XPCOM object
aObjectLoaderJava object whose class loader we use for finding classes; can be null
aResulton exit, holds reference to Java proxy
Returns:
NS_OK if Java proxy was successfully created; any other value denotes an error condition.

Definition at line 1734 of file nsJavaWrapper.cpp.

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

  nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
  NS_ASSERTION(iim != nsnull, "Failed to get InterfaceInfoManager");
  if (!iim)
    return NS_ERROR_FAILURE;

  // Get interface info for class
  nsCOMPtr<nsIInterfaceInfo> info;
  nsresult rv = iim->GetInfoForIID(&aIID, getter_AddRefs(info));
  if (NS_FAILED(rv))
    return rv;

  // Wrap XPCOM object (addrefs aXPCOMObject)
  JavaXPCOMInstance* inst = new JavaXPCOMInstance(aXPCOMObject, info);
  if (!inst)
    return NS_ERROR_OUT_OF_MEMORY;

  // Get interface name
  const char* iface_name;
  rv = info->GetNameShared(&iface_name);

  if (NS_SUCCEEDED(rv)) {
    jobject java_obj = nsnull;

    // Create proper Java interface name
    nsCAutoString class_name("org.mozilla.interfaces.");
    class_name.AppendASCII(iface_name);
    jclass ifaceClass = FindClassInLoader(env, aObjectLoader, class_name.get());

    if (ifaceClass) {
      java_obj = env->CallStaticObjectMethod(xpcomJavaProxyClass,
                                             createProxyMID, ifaceClass,
                                             NS_REINTERPRET_CAST(jlong, inst));
      if (env->ExceptionCheck())
        java_obj = nsnull;
    }

    if (java_obj) {
#ifdef DEBUG_JAVAXPCOM
      char* iid_str = aIID.ToString();
      LOG(("+ CreateJavaProxy (Java=%08x | XPCOM=%08x | IID=%s)\n",
           (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID,
                                               java_obj),
           (PRUint32) aXPCOMObject, iid_str));
      PR_Free(iid_str);
#endif

      // Associate XPCOM object with Java proxy
      rv = gNativeToJavaProxyMap->Add(env, aXPCOMObject, aIID, java_obj);
      if (NS_SUCCEEDED(rv)) {
        *aResult = java_obj;
        return NS_OK;
      }
    } else {
      rv = NS_ERROR_FAILURE;
    }
  }

  // If there was an error, clean up.
  delete inst;
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult GetXPCOMInstFromProxy ( JNIEnv env,
jobject  aJavaObject,
void **  aResult 
)

Returns the XPCOM object for which the given Java proxy was created.

Parameters:
envpointer to Java context
aJavaObjecta Java proxy created by CreateJavaProxy()
aResulton exit, holds pointer to XPCOM instance
Returns:
NS_OK if the XPCOM object was successfully retrieved; any other value denotes an error condition.

Definition at line 1804 of file nsJavaWrapper.cpp.

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

  jlong xpcom_obj = env->CallStaticLongMethod(xpcomJavaProxyClass,
                                            getNativeXPCOMInstMID, aJavaObject);

  if (!xpcom_obj || env->ExceptionCheck()) {
    return NS_ERROR_FAILURE;
  }

  *aResult = NS_REINTERPRET_CAST(void*, xpcom_obj);
#ifdef DEBUG_JAVAXPCOM
  JavaXPCOMInstance* inst = NS_STATIC_CAST(JavaXPCOMInstance*, *aResult);
  nsIID* iid;
  inst->InterfaceInfo()->GetInterfaceIID(&iid);
  char* iid_str = iid->ToString();
  LOG(("< GetXPCOMInstFromProxy (Java=%08x | XPCOM=%08x | IID=%s)\n",
       (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID,
                                           aJavaObject),
       (PRUint32) inst->GetInstance(), iid_str));
  PR_Free(iid_str);
  nsMemory::Free(iid);
#endif
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function: