Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Attributes
nsObserverList Class Reference

#include <nsObserverList.h>

Collaboration diagram for nsObserverList:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsObserverList ()
 ~nsObserverList ()
nsresult AddObserver (nsIObserver *anObserver, PRBool ownsWeak)
nsresult RemoveObserver (nsIObserver *anObserver)
nsresult GetObserverList (nsISimpleEnumerator **anEnumerator)

Protected Attributes

PRLockmLock
nsCOMPtr< nsISupportsArraymObserverList

Detailed Description

Definition at line 63 of file nsObserverList.h.


Constructor & Destructor Documentation

Definition at line 48 of file nsObserverList.cpp.

Here is the call graph for this function:

Definition at line 54 of file nsObserverList.cpp.

Here is the call graph for this function:


Member Function Documentation

nsresult nsObserverList::AddObserver ( nsIObserver anObserver,
PRBool  ownsWeak 
)

Definition at line 61 of file nsObserverList.cpp.

{
    nsresult rv;
    PRBool inserted;
    
    NS_ENSURE_ARG(anObserver);

    nsAutoLock lock(mLock);

    if (!mObserverList) {
        rv = NS_NewISupportsArray(getter_AddRefs(mObserverList));
        if (NS_FAILED(rv)) return rv;
    }

#ifdef NS_WEAK_OBSERVERS
    nsCOMPtr<nsISupports> observerRef;
    if (ownsWeak) {
        nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
        NS_ASSERTION(weakRefFactory, "AddObserver: trying weak object that doesnt support nsIWeakReference");
        if ( weakRefFactory )
            observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
    } else {
#if DEBUG_dougt_xxx
        // if you are hitting this assertion, contact dougt@netscape.com.  There may be a ownership problem caused by his checkin to freeze nsIObserver
        nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
        NS_ASSERTION(!weakRefFactory, "Your object supports weak references, but is being added with a strong reference");
#endif
        observerRef = anObserver;
    }
    if (!observerRef)
        return NS_ERROR_FAILURE;

    inserted = mObserverList->AppendElement(observerRef); 
#else
    if (*anObserver)
        inserted = mObserverList->AppendElement(*anObserver);
#endif
    return inserted ? NS_OK : NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Definition at line 146 of file nsObserverList.cpp.

{
    nsAutoLock lock(mLock);

    ObserverListEnumerator * enumerator= new ObserverListEnumerator(mObserverList);
    *anEnumerator = enumerator;
    if (!enumerator)
        return NS_ERROR_OUT_OF_MEMORY;

    NS_ADDREF(enumerator);
    return NS_OK;
}

Here is the call graph for this function:

Definition at line 102 of file nsObserverList.cpp.

{
    PRBool removed = PR_FALSE;
    
    NS_ENSURE_ARG(anObserver);

    // Prevent the observer from being destroyed while we're inside the lock.
    nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver);

#ifdef NS_WEAK_OBSERVERS
    // Get the weak ref before we acquire the lock, because this QI could
    // run any arbitrary JS code
    nsCOMPtr<nsISupportsWeakReference> weakRefFactory = do_QueryInterface(anObserver);
    nsCOMPtr<nsISupports> observerRef;
    if (weakRefFactory) {
        observerRef = getter_AddRefs(NS_STATIC_CAST(nsISupports*, NS_GetWeakReference(weakRefFactory)));
    }

    nsAutoLock lock(mLock);

    if (!mObserverList)
       return NS_ERROR_FAILURE;
    
    if (observerRef) {
        removed = mObserverList->RemoveElement(observerRef);
    }

    if (!removed && anObserver) {
        removed = mObserverList->RemoveElement(anObserver);
    }
#else
    nsAutoLock lock(mLock);

    if (!mObserverList)
       return NS_ERROR_FAILURE;
    
    if (*anObserver)
        removed = mObserverList->RemoveElement(*anObserver);
#endif

    return removed ? NS_OK : NS_ERROR_FAILURE;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 74 of file nsObserverList.h.

Definition at line 75 of file nsObserverList.h.


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