Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Protected Attributes | Friends
NativeToJavaProxyMap Class Reference

Maps native XPCOM objects to their associated Java proxy object. More...

#include <nsJavaXPCOMBindingUtils.h>

Collaboration diagram for NativeToJavaProxyMap:
Collaboration graph
[legend]

List of all members.

Classes

struct  Entry
struct  ProxyList

Public Member Functions

 NativeToJavaProxyMap ()
 ~NativeToJavaProxyMap ()
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 Attributes

PLDHashTablemHashTable

Friends

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

Detailed Description

Maps native XPCOM objects to their associated Java proxy object.

Definition at line 165 of file nsJavaXPCOMBindingUtils.h.


Constructor & Destructor Documentation

Definition at line 193 of file nsJavaXPCOMBindingUtils.h.

Definition at line 197 of file nsJavaXPCOMBindingUtils.h.

  {
    NS_ASSERTION(mHashTable == nsnull,
                 "MUST call Destroy() before deleting object");
  }

Member Function Documentation

nsresult NativeToJavaProxyMap::Add ( JNIEnv env,
nsISupports *  aXPCOMObject,
const nsIID aIID,
jobject  aProxy 
)

Definition at line 533 of file nsJavaXPCOMBindingUtils.cpp.

{
  nsAutoLock lock(gJavaXPCOMLock);

  Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable,
                                                         aXPCOMObject,
                                                         PL_DHASH_ADD));
  if (!e)
    return NS_ERROR_FAILURE;

  jobject ref = nsnull;
  jobject weakRefObj = env->NewObject(weakReferenceClass,
                                      weakReferenceConstructorMID, aProxy);
  if (weakRefObj)
    ref = env->NewGlobalRef(weakRefObj);
  if (!ref)
    return NS_ERROR_OUT_OF_MEMORY;

  // Add Java proxy weak reference ref to start of list
  ProxyList* item = new ProxyList(ref, aIID, e->list);
  e->key = aXPCOMObject;
  e->list = item;

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

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 520 of file nsJavaXPCOMBindingUtils.cpp.

{
  // This is only called from FreeGlobals(), which already holds the lock.
  //  nsAutoLock lock(gJavaXPCOMLock);

  PL_DHashTableEnumerate(mHashTable, DestroyJavaProxyMappingEnum, env);
  PL_DHashTableDestroy(mHashTable);
  mHashTable = nsnull;

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult NativeToJavaProxyMap::Find ( JNIEnv env,
nsISupports *  aNativeObject,
const nsIID aIID,
jobject aResult 
)

Definition at line 568 of file nsJavaXPCOMBindingUtils.cpp.

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

  nsAutoLock lock(gJavaXPCOMLock);

  *aResult = nsnull;
  Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable,
                                                         aNativeObject,
                                                         PL_DHASH_LOOKUP));

  if (PL_DHASH_ENTRY_IS_FREE(e))
    return NS_OK;

  ProxyList* item = e->list;
  while (item != nsnull && *aResult == nsnull) {
    if (item->iid.Equals(aIID)) {
      jobject referentObj = env->CallObjectMethod(item->javaObject,
                                                  getReferentMID);
      if (!env->IsSameObject(referentObj, NULL)) {
        *aResult = referentObj;
#ifdef DEBUG_JAVAXPCOM
        char* iid_str = aIID.ToString();
        LOG(("< NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n",
             (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID,
                                                 *aResult),
             (PRUint32) aNativeObject, iid_str));
        PR_Free(iid_str);
#endif
      }
    }
    item = item->next;
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 471 of file nsJavaXPCOMBindingUtils.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult NativeToJavaProxyMap::Remove ( JNIEnv env,
nsISupports *  aNativeObject,
const nsIID aIID 
)

Definition at line 609 of file nsJavaXPCOMBindingUtils.cpp.

{
  // This is only called from finalizeProxy(), which already holds the lock.
  //  nsAutoLock lock(gJavaXPCOMLock);

  Entry* e = NS_STATIC_CAST(Entry*, PL_DHashTableOperate(mHashTable,
                                                         aNativeObject,
                                                         PL_DHASH_LOOKUP));

  if (PL_DHASH_ENTRY_IS_FREE(e)) {
    NS_WARNING("XPCOM object not found in hash table");
    return NS_ERROR_FAILURE;
  }

  ProxyList* item = e->list;
  ProxyList* last = e->list;
  while (item != nsnull) {
    if (item->iid.Equals(aIID)) {
#ifdef DEBUG_JAVAXPCOM
      char* iid_str = aIID.ToString();
      LOG(("- NativeToJavaProxyMap (Java=%08x | XPCOM=%08x | IID=%s)\n",
           (PRUint32) env->CallStaticIntMethod(systemClass, hashCodeMID,
                                               item->javaObject),
           (PRUint32) aNativeObject, iid_str));
      PR_Free(iid_str);
#endif

      env->CallVoidMethod(item->javaObject, clearReferentMID);
      env->DeleteGlobalRef(item->javaObject);
      if (item == e->list) {
        e->list = item->next;
        if (e->list == nsnull)
          PL_DHashTableOperate(mHashTable, aNativeObject, PL_DHASH_REMOVE);
      } else {
        last->next = item->next;
      }

      delete item;
      return NS_OK;
    }

    last = item;
    item = item->next;
  }

  NS_WARNING("Java proxy matching given IID not found");
  return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

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

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

Member Data Documentation

Definition at line 216 of file nsJavaXPCOMBindingUtils.h.


The documentation for this class was generated from the following files: