1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsProxyRelease.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +/* vim:set ts=4 sw=4 sts=4 et cin: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsProxyRelease.h" 1.10 +#include "nsThreadUtils.h" 1.11 +#include "nsAutoPtr.h" 1.12 + 1.13 +class nsProxyReleaseEvent : public nsRunnable 1.14 +{ 1.15 +public: 1.16 + nsProxyReleaseEvent(nsISupports *doomed) 1.17 + : mDoomed(doomed) { 1.18 + } 1.19 + 1.20 + NS_IMETHOD Run() 1.21 + { 1.22 + mDoomed->Release(); 1.23 + return NS_OK; 1.24 + } 1.25 + 1.26 +private: 1.27 + nsISupports *mDoomed; 1.28 +}; 1.29 + 1.30 +nsresult 1.31 +NS_ProxyRelease(nsIEventTarget *target, nsISupports *doomed, 1.32 + bool alwaysProxy) 1.33 +{ 1.34 + nsresult rv; 1.35 + 1.36 + if (!doomed) { 1.37 + // nothing to do 1.38 + return NS_OK; 1.39 + } 1.40 + 1.41 + if (!target) { 1.42 + NS_RELEASE(doomed); 1.43 + return NS_OK; 1.44 + } 1.45 + 1.46 + if (!alwaysProxy) { 1.47 + bool onCurrentThread = false; 1.48 + rv = target->IsOnCurrentThread(&onCurrentThread); 1.49 + if (NS_SUCCEEDED(rv) && onCurrentThread) { 1.50 + NS_RELEASE(doomed); 1.51 + return NS_OK; 1.52 + } 1.53 + } 1.54 + 1.55 + nsRefPtr<nsIRunnable> ev = new nsProxyReleaseEvent(doomed); 1.56 + if (!ev) { 1.57 + // we do not release doomed here since it may cause a delete on the 1.58 + // wrong thread. better to leak than crash. 1.59 + return NS_ERROR_OUT_OF_MEMORY; 1.60 + } 1.61 + 1.62 + rv = target->Dispatch(ev, NS_DISPATCH_NORMAL); 1.63 + if (NS_FAILED(rv)) { 1.64 + NS_WARNING("failed to post proxy release event"); 1.65 + // again, it is better to leak the doomed object than risk crashing as 1.66 + // a result of deleting it on the wrong thread. 1.67 + } 1.68 + return rv; 1.69 +}