2014-06-30 15:39:45 +00: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 11:12:37 +00: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 01:46:31 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
class nsProxyReleaseEvent : public nsRunnable
|
2003-10-06 01:46:31 +00:00
|
|
|
{
|
2006-05-10 17:30:15 +00:00
|
|
|
public:
|
2014-07-30 00:43:56 +00:00
|
|
|
explicit nsProxyReleaseEvent(nsISupports* aDoomed) : mDoomed(aDoomed) {}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mDoomed->Release();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
private:
|
2014-12-24 02:17:50 +00:00
|
|
|
nsISupports* MOZ_OWNING_REF mDoomed;
|
2006-05-10 17:30:15 +00:00
|
|
|
};
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2006-05-22 14:56:00 +00:00
|
|
|
nsresult
|
2014-06-27 01:35:39 +00:00
|
|
|
NS_ProxyRelease(nsIEventTarget* aTarget, nsISupports* aDoomed,
|
|
|
|
bool aAlwaysProxy)
|
2003-10-06 01:46:31 +00:00
|
|
|
{
|
2014-06-27 01:35:39 +00:00
|
|
|
nsresult rv;
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
if (!aDoomed) {
|
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-08 18:34:49 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
if (!aTarget) {
|
|
|
|
NS_RELEASE(aDoomed);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
if (!aAlwaysProxy) {
|
|
|
|
bool onCurrentThread = false;
|
|
|
|
rv = aTarget->IsOnCurrentThread(&onCurrentThread);
|
|
|
|
if (NS_SUCCEEDED(rv) && onCurrentThread) {
|
|
|
|
NS_RELEASE(aDoomed);
|
|
|
|
return NS_OK;
|
2003-10-06 01:46:31 +00:00
|
|
|
}
|
2014-06-27 01:35:39 +00:00
|
|
|
}
|
2003-10-06 01:46:31 +00:00
|
|
|
|
2015-03-17 16:29:17 +00:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new nsProxyReleaseEvent(aDoomed);
|
2014-06-27 01:35:39 +00: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 01:46:31 +00:00
|
|
|
|
2014-06-27 01:35:39 +00: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 01:46:31 +00:00
|
|
|
}
|