diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:49:04 +0000 |
commit | 16f504a9dca3fe3b70568f67b7d41241ae485288 (patch) | |
tree | c60f36ada0496ba928b7161059ba5ab1ab224f9d /src/libs/xpcom18a4/xpcom/proxy/public | |
parent | Initial commit. (diff) | |
download | virtualbox-16f504a9dca3fe3b70568f67b7d41241ae485288.tar.xz virtualbox-16f504a9dca3fe3b70568f67b7d41241ae485288.zip |
Adding upstream version 7.0.6-dfsg.upstream/7.0.6-dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libs/xpcom18a4/xpcom/proxy/public')
7 files changed, 663 insertions, 0 deletions
diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/.cvsignore b/src/libs/xpcom18a4/xpcom/proxy/public/.cvsignore new file mode 100644 index 00000000..f3c7a7c5 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/.cvsignore @@ -0,0 +1 @@ +Makefile diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/Makefile.in b/src/libs/xpcom18a4/xpcom/proxy/public/Makefile.in new file mode 100644 index 00000000..d6950b9a --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/Makefile.in @@ -0,0 +1,63 @@ +# +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the terms of +# either of the GNU General Public License Version 2 or later (the "GPL"), +# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = xpcom +ifeq ($(OS_ARCH),WINNT) +XPIDL_MODULE = proxyObject +else +XPIDL_MODULE = proxyObjInst +endif + +EXPORTS = \ + nsProxyEvent.h \ + nsProxyRelease.h \ + nsProxiedService.h \ + $(NULL) + +XPIDLSRCS = nsIProxyCreateInstance.idl \ + nsIProxyObjectManager.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyCreateInstance.idl b/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyCreateInstance.idl new file mode 100644 index 00000000..cd576f7b --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyCreateInstance.idl @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + +[scriptable, uuid(948c2080-0398-11d3-915e-0000863011c4)] +interface nsIProxyCreateInstance : nsISupports +{ + [noscript] void CreateInstanceByIID(in nsIIDRef cid, + in nsISupports aOuter, + in nsIIDRef iid, + out voidPtr result); + + [noscript] void CreateInstanceByContractID(in string aContractID, + in nsISupports aOuter, + in nsIIDRef iid, + out voidPtr result); +}; + diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyObjectManager.idl b/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyObjectManager.idl new file mode 100644 index 00000000..5b2cfd4d --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/nsIProxyObjectManager.idl @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Doug Turner <dougt@netscape.com> (Original Author) + * Dan Mosedale <dmose@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +#include "nsISupports.idl" +interface nsIEventQueue; + +[scriptable, uuid(eea90d43-b059-11d2-915e-c12b696c9333)] +interface nsIProxyObjectManager : nsISupports +{ + void getProxyForObject(in nsIEventQueue destQueue, + in nsIIDRef iid, + in nsISupports object, + in PRInt32 proxyType, + [iid_is(iid),retval] out nsQIResult result); + + void getProxy(in nsIEventQueue destQueue, + in nsIIDRef cid, + in nsISupports aOuter, + in nsIIDRef iid, + in PRInt32 proxyType, + [iid_is(iid),retval] out nsQIResult result); + +}; + + +%{C++ +#include "nsProxyEvent.h" + +#define NS_XPCOMPROXY_CONTRACTID "@mozilla.org/xpcomproxy;1" +#define NS_XPCOMPROXY_CLASSNAME "XPCom Proxy" + +#define NS_PROXYEVENT_MANAGER_CID \ +{ 0xeea90d41, \ + 0xb059, \ + 0x11d2, \ + {0x91, 0x5e, 0xc1, 0x2b, 0x69, 0x6c, 0x93, 0x33}\ +} + +/** + * Helper function for code that already has a link-time dependency on + * libxpcom and needs to get proxies in a bunch of different places. + * This way, the caller isn't forced to get the proxy object manager + * themselves every single time, thus making the calling code more + * readable. + */ +extern NS_COM nsresult +NS_GetProxyForObject(nsIEventQueue *destQueue, + REFNSIID aIID, + nsISupports* aObj, + PRInt32 proxyType, + void** aProxyObject); +%} diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/nsProxiedService.h b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxiedService.h new file mode 100644 index 00000000..3a39ade1 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxiedService.h @@ -0,0 +1,164 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pierre Phaneuf <pp@ludusdesign.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef __nsProxiedServiceManager_h_ +#define __nsProxiedServiceManager_h_ + +#include "nsIServiceManager.h" +#include "nsIProxyObjectManager.h" + +//////////////////////////////////////////////////////////////////////////////// +// NS_WITH_PROXIED_SERVICE: macro to make using services that need to be proxied +// before using them easier. +// Now you can replace this: +// { +// nsresult rv; +// nsCOMPtr<nsIMyService> pIMyService = +// do_GetService(kMyServiceCID, &rv); +// if(NS_FAILED(rv)) +// return; +// nsCOMPtr<nsIProxyObjectManager> pIProxyObjectManager = +// do_GetService(kProxyObjectManagerCID, &rv); +// if(NS_FAILED(rv)) +// return; +// nsIMyService pIProxiedObject = NULL; +// rv = pIProxyObjectManager->GetProxyForObject(pIProxyQueue, +// NS_GET_IID(nsIMyService), +// pIMyService, PROXY_SYNC, +// (void**)&pIProxiedObject); +// pIProxiedObject->DoIt(...); // Executed on same thread as pIProxyQueue +// ... +// pIProxiedObject->Release(); // Must be done as not managed for you. +// } +// with this: +// { +// nsresult rv; +// NS_WITH_PROXIED_SERVICE(nsIMyService, pIMyService, kMyServiceCID, +// pIProxyQueue, &rv); +// if(NS_FAILED(rv)) +// return; +// pIMyService->DoIt(...); // Executed on the same thread as pIProxyQueue +// } +// and the automatic destructor will take care of releasing the service and +// the proxied object for you. +// +// Note that this macro requires you to link with the xpcom DLL to pick up the +// static member functions from nsServiceManager. + +#define NS_WITH_PROXIED_SERVICE(T, var, cid, Q, rvAddr) \ + nsProxiedService _serv##var(cid, NS_GET_IID(T), Q, PR_FALSE, rvAddr); \ + T* var = (T*)(nsISupports*)_serv##var; + +#define NS_WITH_ALWAYS_PROXIED_SERVICE(T, var, cid, Q, rvAddr) \ + nsProxiedService _serv##var(cid, NS_GET_IID(T), Q, PR_TRUE, rvAddr); \ + T* var = (T*)(nsISupports*)_serv##var; + +//////////////////////////////////////////////////////////////////////////////// +// nsProxiedService +//////////////////////////////////////////////////////////////////////////////// + +class nsProxiedService +{ + public: + + nsProxiedService(const nsCID &aClass, const nsIID &aIID, + nsIEventQueue* pIProxyQueue, PRBool always, nsresult*rv) + { + *rv = nsServiceManager::GetService(aClass, + aIID, + getter_AddRefs(mService)); + if (NS_FAILED(*rv)) return; + InitProxy(aIID, pIProxyQueue, always, rv); + } + + nsProxiedService(const char* aContractID, const nsIID &aIID, + nsIEventQueue* pIProxyQueue, PRBool always, nsresult*rv) + { + *rv = nsServiceManager::GetService(aContractID, + aIID, + getter_AddRefs(mService)); + if (NS_FAILED(*rv)) return; + InitProxy(aIID, pIProxyQueue, always, rv); + } + + void InitProxy(const nsIID &aIID, nsIEventQueue* pIProxyQueue, + PRBool always, nsresult*rv) + { + static NS_DEFINE_CID(kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID); + + nsCOMPtr<nsIProxyObjectManager> pIProxyObjectManager = + do_GetService(kProxyObjectManagerCID, rv); + if (NS_FAILED(*rv)) return; + + PRInt32 proxyType = PROXY_SYNC; + if (always) proxyType |= PROXY_ALWAYS; + *rv = pIProxyObjectManager->GetProxyForObject(pIProxyQueue, + aIID, + mService, + proxyType, + getter_AddRefs(mProxiedService)); + } + + ~nsProxiedService() + { + } + + nsISupports* operator->() const + { + NS_PRECONDITION(mProxiedService != 0, "Your code should test the error result from the constructor."); + return mProxiedService; + } + + PRBool operator==(const nsISupports* other) + { + return ((mProxiedService == other) || (mService == other)); + } + + operator nsISupports*() const + { + return mProxiedService; + } + + protected: + nsCOMPtr<nsISupports> mProxiedService; + nsCOMPtr<nsISupports> mService; + + }; + + +#endif //__nsProxiedServiceManager_h_ diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyEvent.h b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyEvent.h new file mode 100644 index 00000000..c718bbdb --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyEvent.h @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef __nsProxyEvent_h_ +#define __nsProxyEvent_h_ + +#include "nsCOMPtr.h" +#include "nsAutoPtr.h" +#include "nscore.h" +#include "nsISupports.h" +#include "nsIFactory.h" + +#include "nsIEventQueueService.h" +#include "nsIEventQueue.h" +#include "plevent.h" +#include "prtypes.h" +#include "xptcall.h" +#include "xptinfo.h" + +class nsProxyObjectCallInfo; + +#define PROXY_SYNC 0x0001 // acts just like a function call. +#define PROXY_ASYNC 0x0002 // fire and forget. This will return immediately and you will lose all return information. +#define PROXY_ALWAYS 0x0004 // ignore check to see if the eventQ is on the same thread as the caller, and alway return a proxied object. + +//#define AUTOPROXIFICATION + +// WARNING about PROXY_ASYNC: +// +// If the calling thread goes away, any function which accesses the calling stack +// will blow up. +// +// example: +// +// myFoo->bar(&x) +// +// ... thread goes away ... +// +// bar(PRInt32 *x) +// { +// *x = 0; <----- You will blow up here. +// +// +// So what gets saved? +// +// You can safely pass base types by value. You can also pass interface pointers. +// I will make sure that the interface pointers are addrefed while they are being +// proxied. You can also pass string and wstring. These I will copy and free. +// +// I do **NOT** copy arrays or strings with size. If you are using these either +// change your interface, or contact me about this feature request. + + + + +class nsProxyObject +{ +public: + nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, nsISupports *realObject); + nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID); + + void AddRef(); + void Release(); + + ~nsProxyObject(); + + nsresult Post( PRUint32 methodIndex, + nsXPTMethodInfo * info, + nsXPTCMiniVariant * params, + nsIInterfaceInfo * interfaceInfo); + + nsresult PostAndWait(nsProxyObjectCallInfo *proxyInfo); + nsISupports* GetRealObject() const { return mRealObject; } + nsIEventQueue* GetQueue() const { return mDestQueue; } + PRInt32 GetProxyType() const { return mProxyType; } + + friend class nsProxyEventObject; +private: + + nsAutoRefCnt mRefCnt; + + PRInt32 mProxyType; + + nsCOMPtr<nsIEventQueue> mDestQueue; /* destination queue */ + + nsCOMPtr<nsISupports> mRealObject; /* the non-proxy object that this event is referring to. + This is a strong ref. */ + nsCOMPtr<nsIEventQueueService> mEventQService; + + nsresult convertMiniVariantToVariant(nsXPTMethodInfo * methodInfo, + nsXPTCMiniVariant * params, + nsXPTCVariant **fullParam, + uint8 *paramCount); + +}; + + +class nsProxyObjectCallInfo +{ +public: + + nsProxyObjectCallInfo(nsProxyObject* owner, + nsXPTMethodInfo *methodInfo, + PRUint32 methodIndex, + nsXPTCVariant* parameterList, + PRUint32 parameterCount, + PLEvent *event); + + ~nsProxyObjectCallInfo(); + + PRUint32 GetMethodIndex() const { return mMethodIndex; } + nsXPTCVariant* GetParameterList() const { return mParameterList; } + PRUint32 GetParameterCount() const { return mParameterCount; } + PLEvent* GetPLEvent() const { return mEvent; } + nsresult GetResult() const { return mResult; } + nsProxyObject* GetProxyObject() const { return mOwner; } + + PRBool GetCompleted(); + void SetCompleted(); + void PostCompleted(); + + void SetResult(nsresult rv) {mResult = rv; } + + nsIEventQueue* GetCallersQueue(); + void SetCallersQueue(nsIEventQueue* queue); + +private: + + nsresult mResult; /* this is the return result of the called function */ + nsXPTMethodInfo *mMethodInfo; + PRUint32 mMethodIndex; /* which method to be called? */ + nsXPTCVariant *mParameterList; /* marshalled in parameter buffer */ + PRUint32 mParameterCount; /* number of params */ + PLEvent *mEvent; /* the current plevent */ + PRInt32 mCompleted; /* is true when the method has been called. */ + + nsCOMPtr<nsIEventQueue> mCallersEventQ; /* this is the eventQ that we must post a message back to + when we are done invoking the method (only PROXY_SYNC). + */ + + nsRefPtr<nsProxyObject> mOwner; /* this is the strong referenced nsProxyObject */ + + void RefCountInInterfacePointers(PRBool addRef); + void CopyStrings(PRBool copy); + +}; + + +#endif diff --git a/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyRelease.h b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyRelease.h new file mode 100644 index 00000000..413a3d0e --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/proxy/public/nsProxyRelease.h @@ -0,0 +1,112 @@ +/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsProxyRelease_h__ +#define nsProxyRelease_h__ + +#include "nsIEventQueueService.h" +#include "pratom.h" +#include "prmem.h" + +/** + * Ensures that the delete of a nsISupports object occurs on the target thread. + * + * @param target + * the target thread where the doomed object should be released. + * @param doomed + * the doomed object; the object to be released on the target thread. + * @param alwaysProxy + * normally, if NS_ProxyRelease is called on the target thread, then the + * doomed object will released directly. however, if this parameter is + * true, then a PLEvent will always be posted to the target thread and + * the release will happen when that PLEvent is handled. + */ +NS_COM nsresult NS_ProxyRelease + (nsIEventTarget *target, nsISupports *doomed, PRBool alwaysProxy=PR_FALSE); + + +#define NS_IMPL_PROXY_RELEASE(_class) \ +NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \ +{ \ + NS_PRECONDITION(0 != mRefCnt, "dup release"); \ + nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mRefCnt); \ + NS_LOG_RELEASE(this, count, #_class); \ + \ + if (count == 0) \ + { \ + mRefCnt = 1; /* stabilize */ \ + PRBool callDirectly = PR_TRUE; \ + static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); \ + nsCOMPtr<nsIEventQueueService> eventQService \ + = do_GetService(kEventQueueServiceCID); \ + NS_ASSERTION(eventQService, "event queue service is unavailable"); \ + \ + nsCOMPtr<nsIEventQueue> eventQ; \ + if (eventQService) { \ + eventQService->GetThreadEventQueue(NS_UI_THREAD, getter_AddRefs(eventQ)); \ + if (eventQ) \ + eventQ->IsOnCurrentThread(&callDirectly); \ + } \ + \ + if (callDirectly) \ + { \ + NS_RELEASE(this); \ + return 0; \ + } \ + PLEvent *event = new PLEvent; \ + if (event == nsnull) \ + { \ + NS_ASSERTION(0, "Could not create a plevent. Deleting on wrong thread!"); \ + NS_DELETEXPCOM(this); \ + return 0; \ + } \ + \ + PL_InitEvent(event, \ + NS_STATIC_CAST(nsISupports*, this), \ + ReleaseDestructorEventHandler, \ + ReleaseDestructorDestroyHandler); \ + \ + eventQ->PostEvent(event); \ + return 0; \ + } \ + return count; \ +} \ + + + + +#endif |