2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
class nsProxyReleaseEvent : public nsRunnable
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit nsProxyReleaseEvent(nsISupports* aDoomed) : mDoomed(aDoomed) {}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mDoomed->Release();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
private:
|
2014-12-24 05:17:50 +03:00
|
|
|
nsISupports* MOZ_OWNING_REF mDoomed;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2006-05-22 18:56:00 +04:00
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ProxyRelease(nsIEventTarget* aTarget, nsISupports* aDoomed,
|
|
|
|
bool aAlwaysProxy)
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult rv;
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aDoomed) {
|
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-08 22:34:49 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aTarget) {
|
|
|
|
NS_RELEASE(aDoomed);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aAlwaysProxy) {
|
|
|
|
bool onCurrentThread = false;
|
|
|
|
rv = aTarget->IsOnCurrentThread(&onCurrentThread);
|
|
|
|
if (NS_SUCCEEDED(rv) && onCurrentThread) {
|
|
|
|
NS_RELEASE(aDoomed);
|
|
|
|
return NS_OK;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new nsProxyReleaseEvent(aDoomed);
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!ev) {
|
|
|
|
// we do not release aDoomed here since it may cause a delete on the
|
|
|
|
// wrong thread. better to leak than crash.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
rv = aTarget->Dispatch(ev, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to post proxy release event");
|
|
|
|
// again, it is better to leak the aDoomed object than risk crashing as
|
|
|
|
// a result of deleting it on the wrong thread.
|
|
|
|
}
|
|
|
|
return rv;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|