Bumping manifests a=b2g-bump
[gecko.git] / xpcom / glue / nsProxyRelease.cpp
blob091751dce82dd3d184319000ea8d61995802db14
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsProxyRelease.h"
8 #include "nsThreadUtils.h"
9 #include "nsAutoPtr.h"
11 class nsProxyReleaseEvent : public nsRunnable
13 public:
14 explicit nsProxyReleaseEvent(nsISupports* aDoomed) : mDoomed(aDoomed) {}
16 NS_IMETHOD Run()
18 mDoomed->Release();
19 return NS_OK;
22 private:
23 nsISupports* MOZ_OWNING_REF mDoomed;
26 nsresult
27 NS_ProxyRelease(nsIEventTarget* aTarget, nsISupports* aDoomed,
28 bool aAlwaysProxy)
30 nsresult rv;
32 if (!aDoomed) {
33 // nothing to do
34 return NS_OK;
37 if (!aTarget) {
38 NS_RELEASE(aDoomed);
39 return NS_OK;
42 if (!aAlwaysProxy) {
43 bool onCurrentThread = false;
44 rv = aTarget->IsOnCurrentThread(&onCurrentThread);
45 if (NS_SUCCEEDED(rv) && onCurrentThread) {
46 NS_RELEASE(aDoomed);
47 return NS_OK;
51 nsRefPtr<nsIRunnable> ev = new nsProxyReleaseEvent(aDoomed);
52 if (!ev) {
53 // we do not release aDoomed here since it may cause a delete on the
54 // wrong thread. better to leak than crash.
55 return NS_ERROR_OUT_OF_MEMORY;
58 rv = aTarget->Dispatch(ev, NS_DISPATCH_NORMAL);
59 if (NS_FAILED(rv)) {
60 NS_WARNING("failed to post proxy release event");
61 // again, it is better to leak the aDoomed object than risk crashing as
62 // a result of deleting it on the wrong thread.
64 return rv;