Back to index

lightning-sunbird  0.9+nobinonly
nsProxyRelease.cpp
Go to the documentation of this file.
00001 #include "nsProxyRelease.h"
00002 
00003 PR_STATIC_CALLBACK(void*)
00004 HandleProxyReleaseEvent(PLEvent *self)
00005 {              
00006     nsISupports* owner = (nsISupports*) self->owner;
00007     NS_RELEASE(owner);                                                                                              
00008     return nsnull;                                            
00009 }
00010 
00011 PR_STATIC_CALLBACK(void)
00012 DestroyProxyReleaseEvent(PLEvent *self)
00013 {
00014     delete self;
00015 }
00016 
00017 NS_COM nsresult
00018 NS_ProxyRelease(nsIEventTarget *target, nsISupports *doomed, PRBool alwaysProxy)
00019 {
00020     nsresult rv;
00021 
00022     if (!target) {
00023         NS_RELEASE(doomed);
00024         return NS_OK;
00025     }
00026 
00027     if (!alwaysProxy) {
00028         PRBool onCurrentThread = PR_FALSE;
00029         rv = target->IsOnCurrentThread(&onCurrentThread);
00030         if (NS_SUCCEEDED(rv) && onCurrentThread) {
00031             NS_RELEASE(doomed);
00032             return NS_OK;
00033         }
00034     }
00035 
00036     PLEvent *ev = new PLEvent;
00037     if (!ev) {
00038         // we do not release doomed here since it may cause a delete on the
00039         // wrong thread.  better to leak than crash. 
00040         return NS_ERROR_OUT_OF_MEMORY;
00041     }
00042 
00043     PL_InitEvent(ev, doomed,
00044                  HandleProxyReleaseEvent,
00045                  DestroyProxyReleaseEvent);
00046 
00047     rv = target->PostEvent(ev);
00048     if (NS_FAILED(rv)) {
00049         NS_WARNING("failed to post proxy release event");
00050         PL_DestroyEvent(ev);
00051         // again, it is better to leak the doomed object than risk crashing as
00052         // a result of deleting it on the wrong thread.
00053     }
00054     return rv;
00055 }