2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Class that wraps JS objects to appear as XPCOM objects. */
|
|
|
|
|
|
|
|
#include "xpcprivate.h"
|
2010-08-17 10:25:41 -07:00
|
|
|
#include "nsAtomicRefcnt.h"
|
2011-09-14 08:44:27 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2010-10-30 06:01:25 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2010-11-17 12:58:53 -08:00
|
|
|
#include "nsTextFormatter.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// NOTE: much of the fancy footwork is done in xpcstubs.cpp
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXPCWrappedJS)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-03 23:30:26 -07:00
|
|
|
NS_CYCLE_COLLECTION_CLASSNAME(nsXPCWrappedJS)::TraverseImpl
|
|
|
|
(NS_CYCLE_COLLECTION_CLASSNAME(nsXPCWrappedJS) *that, void *p,
|
|
|
|
nsCycleCollectionTraversalCallback &cb)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-31 17:06:53 -08:00
|
|
|
nsISupports *s = static_cast<nsISupports*>(p);
|
|
|
|
NS_ASSERTION(CheckForRightISupports(s),
|
|
|
|
"not the nsISupports pointer we expect");
|
|
|
|
nsXPCWrappedJS *tmp = Downcast(s);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsrefcnt refcnt = tmp->mRefCnt.get();
|
2009-07-08 18:10:29 -07:00
|
|
|
if (cb.WantDebugInfo()) {
|
|
|
|
char name[72];
|
|
|
|
if (tmp->GetClass())
|
|
|
|
JS_snprintf(name, sizeof(name), "nsXPCWrappedJS (%s)",
|
|
|
|
tmp->GetClass()->GetInterfaceName());
|
|
|
|
else
|
|
|
|
JS_snprintf(name, sizeof(name), "nsXPCWrappedJS");
|
2011-06-23 14:10:52 -07:00
|
|
|
cb.DescribeRefCountedNode(refcnt, sizeof(nsXPCWrappedJS), name);
|
2009-07-08 18:10:29 -07:00
|
|
|
} else {
|
2011-06-23 14:10:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsXPCWrappedJS, refcnt)
|
2009-07-08 18:10:29 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsXPCWrappedJS keeps its own refcount artificially at or above 1, see the
|
|
|
|
// comment above nsXPCWrappedJS::AddRef.
|
2012-01-23 15:25:53 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "self");
|
2008-01-31 17:06:53 -08:00
|
|
|
cb.NoteXPCOMChild(s);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-23 15:25:53 -08:00
|
|
|
if (refcnt > 1) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsXPCWrappedJS roots its mJSObj when its refcount is > 1, see
|
|
|
|
// the comment above nsXPCWrappedJS::AddRef.
|
2012-01-23 15:25:53 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mJSObj");
|
2012-05-03 12:28:10 -07:00
|
|
|
cb.NoteJSChild(tmp->GetJSObjectPreserveColor());
|
2012-01-23 15:25:53 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsXPCWrappedJS* root = tmp->GetRootWrapper();
|
2012-01-23 15:25:53 -08:00
|
|
|
if (root == tmp) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The root wrapper keeps the aggregated native object alive.
|
2012-01-23 15:25:53 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "aggregated native");
|
2007-03-22 10:30:00 -07:00
|
|
|
cb.NoteXPCOMChild(tmp->GetAggregatedNativeObject());
|
2012-01-23 15:25:53 -08:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Non-root wrappers keep their root alive.
|
2012-01-23 15:25:53 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "root");
|
2007-07-08 00:08:04 -07:00
|
|
|
cb.NoteXPCOMChild(static_cast<nsIXPConnectWrappedJS*>(root));
|
2012-01-23 15:25:53 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-29 18:05:43 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXPCWrappedJS)
|
2009-01-25 06:34:26 -08:00
|
|
|
tmp->Unlink();
|
2007-11-01 15:51:57 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::AggregatedQueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsAggregatedToNative(), "bad AggregatedQueryInterface call");
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!IsValid())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// Put this here rather that in DelegatedQueryInterface because it needs
|
|
|
|
// to be in QueryInterface before the possible delegation to 'outer', but
|
|
|
|
// we don't want to do this check twice in one call in the normal case:
|
|
|
|
// once in QueryInterface and once in DelegatedQueryInterface.
|
2011-10-14 10:52:48 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIXPConnectWrappedJS))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(this);
|
2007-07-08 00:08:04 -07:00
|
|
|
*aInstancePtr = (void*) static_cast<nsIXPConnectWrappedJS*>(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mClass->DelegatedQueryInterface(this, aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
2011-10-14 10:52:48 -07:00
|
|
|
if (nsnull == aInstancePtr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_PRECONDITION(0, "null pointer");
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2007-05-24 07:10:02 -07:00
|
|
|
if ( aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant)) ) {
|
2012-06-03 23:30:26 -07:00
|
|
|
*aInstancePtr = NS_CYCLE_COLLECTION_PARTICIPANT(nsXPCWrappedJS);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*aInstancePtr =
|
|
|
|
NS_CYCLE_COLLECTION_CLASSNAME(nsXPCWrappedJS)::Upcast(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!IsValid())
|
2008-01-29 18:05:43 -08:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Always check for this first so that our 'outer' can get this interface
|
|
|
|
// from us without recurring into a call to the outer's QI!
|
2011-10-14 10:52:48 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIXPConnectWrappedJS))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(this);
|
2007-07-08 00:08:04 -07:00
|
|
|
*aInstancePtr = (void*) static_cast<nsIXPConnectWrappedJS*>(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports* outer = GetAggregatedNativeObject();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (outer)
|
2007-03-22 10:30:00 -07:00
|
|
|
return outer->QueryInterface(aIID, aInstancePtr);
|
|
|
|
|
|
|
|
// else...
|
|
|
|
|
|
|
|
return mClass->DelegatedQueryInterface(this, aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Refcounting is now similar to that used in the chained (pre-flattening)
|
|
|
|
// wrappednative system.
|
|
|
|
//
|
|
|
|
// We are now holding an extra refcount for nsISupportsWeakReference support.
|
|
|
|
//
|
|
|
|
// Non-root wrappers remove themselves from the chain in their destructors.
|
|
|
|
// We root the JSObject as the refcount transitions from 1->2. And we unroot
|
|
|
|
// the JSObject when the refcount transitions from 2->1.
|
|
|
|
//
|
|
|
|
// When the transition from 2->1 is made and no one holds a weak ref to the
|
|
|
|
// (aggregated) object then we decrement the refcount again to 0 (and
|
|
|
|
// destruct) . However, if a weak ref is held at the 2->1 transition, then we
|
|
|
|
// leave the refcount at 1 to indicate that state. This leaves the JSObject
|
|
|
|
// no longer rooted by us and (as far as we know) subject to possible
|
|
|
|
// collection. Code in XPCJSRuntime watches for JS gc to happen and will do
|
|
|
|
// the final release on wrappers whose JSObjects get finalized. Note that
|
|
|
|
// even after tranistioning to this refcount-of-one state callers might do
|
|
|
|
// an addref and cause us to re-root the JSObject and continue on more normally.
|
|
|
|
|
|
|
|
nsrefcnt
|
|
|
|
nsXPCWrappedJS::AddRef(void)
|
|
|
|
{
|
2010-08-17 10:25:41 -07:00
|
|
|
nsrefcnt cnt = NS_AtomicIncrementRefcnt(mRefCnt);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_ADDREF(this, cnt, "nsXPCWrappedJS", sizeof(*this));
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (2 == cnt && IsValid()) {
|
2007-06-02 08:11:52 -07:00
|
|
|
XPCJSRuntime* rt = mClass->GetRuntime();
|
|
|
|
rt->AddWrappedJSRoot(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsrefcnt
|
|
|
|
nsXPCWrappedJS::Release(void)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
|
|
|
|
2011-09-14 08:44:27 -07:00
|
|
|
if (mMainThreadOnly && !NS_IsMainThread()) {
|
|
|
|
// We'd like to abort here, but this can happen if someone uses a proxy
|
|
|
|
// for the nsXPCWrappedJS.
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
// If we can't get the main thread anymore we just leak, but this really
|
|
|
|
// shouldn't happen.
|
|
|
|
NS_ASSERTION(mainThread,
|
|
|
|
"Can't get main thread, leaking nsXPCWrappedJS!");
|
|
|
|
if (mainThread) {
|
|
|
|
NS_ProxyRelease(mainThread,
|
|
|
|
static_cast<nsIXPConnectWrappedJS*>(this));
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
2008-07-11 14:44:59 -07:00
|
|
|
// need to take the map lock here to prevent GetNewOrUsed from trying
|
|
|
|
// to reuse a wrapper on one thread while it's being destroyed on another
|
2008-10-11 10:35:39 -07:00
|
|
|
XPCJSRuntime* rt = nsXPConnect::GetRuntimeInstance();
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
2008-07-11 14:44:59 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
do_decrement:
|
|
|
|
|
2010-08-17 10:25:41 -07:00
|
|
|
nsrefcnt cnt = NS_AtomicDecrementRefcnt(mRefCnt);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_RELEASE(this, cnt, "nsXPCWrappedJS");
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (0 == cnt) {
|
2010-07-05 02:42:18 -07:00
|
|
|
delete this; // also unlinks us from chain
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2011-10-14 10:52:48 -07:00
|
|
|
if (1 == cnt) {
|
2011-10-14 10:52:48 -07:00
|
|
|
if (IsValid())
|
2010-11-11 14:52:30 -08:00
|
|
|
RemoveFromRootSet(rt->GetMapLock());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If we are not the root wrapper or if we are not being used from a
|
|
|
|
// weak reference, then this extra ref is not needed and we can let
|
|
|
|
// ourself be deleted.
|
|
|
|
// Note: HasWeakReferences() could only return true for the root.
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!HasWeakReferences())
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_decrement;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2007-06-02 08:11:52 -07:00
|
|
|
void
|
|
|
|
nsXPCWrappedJS::TraceJS(JSTracer* trc)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRefCnt >= 2 && IsValid(), "must be strongly referenced");
|
2012-01-13 17:19:19 -08:00
|
|
|
JS_SET_TRACING_DETAILS(trc, GetTraceName, this, 0);
|
2011-02-16 12:47:08 -08:00
|
|
|
JS_CallTracer(trc, GetJSObjectPreserveColor(), JSTRACE_OBJECT);
|
2007-06-02 08:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2012-01-13 17:19:19 -08:00
|
|
|
nsXPCWrappedJS::GetTraceName(JSTracer* trc, char *buf, size_t bufsize)
|
2007-06-02 08:11:52 -07:00
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsXPCWrappedJS* self = static_cast<const nsXPCWrappedJS*>
|
|
|
|
(trc->debugPrintArg);
|
2011-08-04 15:01:43 -07:00
|
|
|
JS_snprintf(buf, bufsize, "nsXPCWrappedJS[%s,0x%p:0x%p].mJSObj",
|
|
|
|
self->GetClass()->GetInterfaceName(), self, self->mXPTCStub);
|
2007-06-02 08:11:52 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::GetWeakReference(nsIWeakReference** aInstancePtr)
|
|
|
|
{
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mRoot != this)
|
2007-03-22 10:30:00 -07:00
|
|
|
return mRoot->GetWeakReference(aInstancePtr);
|
|
|
|
|
|
|
|
return nsSupportsWeakReference::GetWeakReference(aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::GetJSObject(JSObject** aJSObj)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aJSObj, "bad param");
|
2011-02-16 12:47:08 -08:00
|
|
|
NS_PRECONDITION(IsValid(), "bad wrapper");
|
2010-10-30 06:01:25 -07:00
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!(*aJSObj = GetJSObject()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-14 08:44:27 -07:00
|
|
|
static bool
|
|
|
|
CheckMainThreadOnly(nsXPCWrappedJS *aWrapper)
|
|
|
|
{
|
|
|
|
if(aWrapper->IsMainThreadOnly())
|
|
|
|
return NS_IsMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClassInfo> ci;
|
|
|
|
CallQueryInterface(aWrapper, getter_AddRefs(ci));
|
|
|
|
if (ci) {
|
|
|
|
PRUint32 flags;
|
|
|
|
if (NS_SUCCEEDED(ci->GetFlags(&flags)) && !(flags & nsIClassInfo::MAIN_THREAD_ONLY))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!NS_IsMainThread())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aWrapper->SetIsMainThreadOnly();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsXPCWrappedJS::GetNewOrUsed(XPCCallContext& ccx,
|
|
|
|
JSObject* aJSObj,
|
|
|
|
REFNSIID aIID,
|
|
|
|
nsISupports* aOuter,
|
|
|
|
nsXPCWrappedJS** wrapperResult)
|
|
|
|
{
|
|
|
|
JSObject2WrappedJSMap* map;
|
|
|
|
JSObject* rootJSObj;
|
2011-09-05 02:29:06 -07:00
|
|
|
nsXPCWrappedJS* root = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPCWrappedJS* wrapper = nsnull;
|
|
|
|
nsXPCWrappedJSClass* clazz = nsnull;
|
|
|
|
XPCJSRuntime* rt = ccx.GetRuntime();
|
2011-11-26 02:05:59 -08:00
|
|
|
JSBool release_root = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
map = rt->GetWrappedJSMap();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!map) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(map,"bad map");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPCWrappedJSClass::GetNewOrUsed(ccx, aIID, &clazz);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!clazz)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
// from here on we need to return through 'return_wrapper'
|
|
|
|
|
|
|
|
// always find the root JSObject
|
|
|
|
rootJSObj = clazz->GetRootJSObject(ccx, aJSObj);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!rootJSObj)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto return_wrapper;
|
|
|
|
|
2008-07-11 14:44:59 -07:00
|
|
|
// look for the root wrapper, and if found, hold the map lock until
|
|
|
|
// we've added our ref to prevent another thread from destroying it
|
|
|
|
// under us
|
2007-03-22 10:30:00 -07:00
|
|
|
{ // scoped lock
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
root = map->Find(rootJSObj);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (root) {
|
2011-10-14 10:52:48 -07:00
|
|
|
if ((nsnull != (wrapper = root->Find(aIID))) ||
|
2011-10-14 10:52:48 -07:00
|
|
|
(nsnull != (wrapper = root->FindInherited(aIID)))) {
|
2008-07-11 14:44:59 -07:00
|
|
|
NS_ADDREF(wrapper);
|
|
|
|
goto return_wrapper;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2009-07-02 17:00:20 -07:00
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!root) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// build the root wrapper
|
2011-10-14 10:52:48 -07:00
|
|
|
if (rootJSObj == aJSObj) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// the root will do double duty as the interface wrapper
|
|
|
|
wrapper = root = new nsXPCWrappedJS(ccx, aJSObj, clazz, nsnull,
|
|
|
|
aOuter);
|
2011-09-14 08:44:27 -07:00
|
|
|
if (!root)
|
|
|
|
goto return_wrapper;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
{ // scoped lock
|
|
|
|
#if DEBUG_xpc_leaks
|
|
|
|
printf("Created nsXPCWrappedJS %p, JSObject is %p\n",
|
|
|
|
(void*)wrapper, (void*)aJSObj);
|
|
|
|
#endif
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Add(root);
|
|
|
|
}
|
2011-09-14 08:44:27 -07:00
|
|
|
|
|
|
|
if (!CheckMainThreadOnly(root)) {
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Remove(root);
|
|
|
|
|
|
|
|
wrapper = NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
goto return_wrapper;
|
2011-10-14 10:52:48 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
// just a root wrapper
|
|
|
|
nsXPCWrappedJSClass* rootClazz = nsnull;
|
|
|
|
nsXPCWrappedJSClass::GetNewOrUsed(ccx, NS_GET_IID(nsISupports),
|
|
|
|
&rootClazz);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!rootClazz)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto return_wrapper;
|
|
|
|
|
|
|
|
root = new nsXPCWrappedJS(ccx, rootJSObj, rootClazz, nsnull, aOuter);
|
|
|
|
NS_RELEASE(rootClazz);
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!root)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto return_wrapper;
|
2009-07-02 17:00:20 -07:00
|
|
|
|
2011-11-26 02:05:59 -08:00
|
|
|
release_root = true;
|
2009-07-02 17:00:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
{ // scoped lock
|
|
|
|
#if DEBUG_xpc_leaks
|
|
|
|
printf("Created nsXPCWrappedJS %p, JSObject is %p\n",
|
|
|
|
(void*)root, (void*)rootJSObj);
|
|
|
|
#endif
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Add(root);
|
|
|
|
}
|
2011-09-14 08:44:27 -07:00
|
|
|
|
|
|
|
if (!CheckMainThreadOnly(root)) {
|
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Remove(root);
|
|
|
|
|
|
|
|
goto return_wrapper;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point we have a root and may need to build the specific wrapper
|
|
|
|
NS_ASSERTION(root,"bad root");
|
|
|
|
NS_ASSERTION(clazz,"bad clazz");
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!wrapper) {
|
2007-03-22 10:30:00 -07:00
|
|
|
wrapper = new nsXPCWrappedJS(ccx, aJSObj, clazz, root, aOuter);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!wrapper)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto return_wrapper;
|
|
|
|
#if DEBUG_xpc_leaks
|
|
|
|
printf("Created nsXPCWrappedJS %p, JSObject is %p\n",
|
|
|
|
(void*)wrapper, (void*)aJSObj);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper->mNext = root->mNext;
|
|
|
|
root->mNext = wrapper;
|
|
|
|
|
|
|
|
return_wrapper:
|
2011-10-14 10:52:48 -07:00
|
|
|
if (clazz)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_RELEASE(clazz);
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (release_root)
|
2009-07-02 17:00:20 -07:00
|
|
|
NS_RELEASE(root);
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!wrapper)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*wrapperResult = wrapper;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPCWrappedJS::nsXPCWrappedJS(XPCCallContext& ccx,
|
|
|
|
JSObject* aJSObj,
|
|
|
|
nsXPCWrappedJSClass* aClass,
|
|
|
|
nsXPCWrappedJS* root,
|
|
|
|
nsISupports* aOuter)
|
|
|
|
: mJSObj(aJSObj),
|
|
|
|
mClass(aClass),
|
|
|
|
mRoot(root ? root : this),
|
|
|
|
mNext(nsnull),
|
2010-10-30 06:01:25 -07:00
|
|
|
mOuter(root ? nsnull : aOuter),
|
2011-09-14 08:44:27 -07:00
|
|
|
mMainThread(NS_IsMainThread()),
|
|
|
|
mMainThreadOnly(root && root->mMainThreadOnly)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_stats_jband
|
|
|
|
static int count = 0;
|
|
|
|
static const int interval = 10;
|
2011-10-14 10:52:48 -07:00
|
|
|
if (0 == (++count % interval))
|
2007-03-22 10:30:00 -07:00
|
|
|
printf("//////// %d instances of nsXPCWrappedJS created\n", count);
|
|
|
|
#endif
|
|
|
|
|
2011-09-14 08:44:27 -07:00
|
|
|
JS_ASSERT_IF(mMainThreadOnly, mMainThread);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
InitStub(GetClass()->GetIID());
|
|
|
|
|
2008-07-11 14:44:59 -07:00
|
|
|
// intentionally do double addref - see Release().
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
NS_ADDREF(aClass);
|
|
|
|
NS_IF_ADDREF(mOuter);
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mRoot != this)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(mRoot);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPCWrappedJS::~nsXPCWrappedJS()
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(0 == mRefCnt, "refcounting error");
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mRoot == this) {
|
2008-01-29 18:05:43 -08:00
|
|
|
// Remove this root wrapper from the map
|
2008-10-11 10:35:39 -07:00
|
|
|
XPCJSRuntime* rt = nsXPConnect::GetRuntimeInstance();
|
|
|
|
JSObject2WrappedJSMap* map = rt->GetWrappedJSMap();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (map) {
|
2008-10-11 10:35:39 -07:00
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Remove(this);
|
2007-11-02 08:48:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 18:05:43 -08:00
|
|
|
Unlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXPCWrappedJS::Unlink()
|
|
|
|
{
|
2011-10-14 10:52:48 -07:00
|
|
|
if (IsValid()) {
|
2010-11-08 07:02:49 -08:00
|
|
|
XPCJSRuntime* rt = nsXPConnect::GetRuntimeInstance();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (rt) {
|
|
|
|
if (mRoot == this) {
|
2010-11-08 07:02:49 -08:00
|
|
|
// remove this root wrapper from the map
|
|
|
|
JSObject2WrappedJSMap* map = rt->GetWrappedJSMap();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (map) {
|
2010-11-08 07:02:49 -08:00
|
|
|
XPCAutoLock lock(rt->GetMapLock());
|
|
|
|
map->Remove(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mRefCnt > 1)
|
2010-11-08 07:02:49 -08:00
|
|
|
RemoveFromRootSet(rt->GetMapLock());
|
|
|
|
}
|
|
|
|
|
|
|
|
mJSObj = nsnull;
|
|
|
|
}
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mRoot == this) {
|
2009-01-25 06:34:26 -08:00
|
|
|
ClearWeakReferences();
|
2011-10-14 10:52:48 -07:00
|
|
|
} else if (mRoot) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// unlink this wrapper
|
|
|
|
nsXPCWrappedJS* cur = mRoot;
|
2011-10-14 10:52:48 -07:00
|
|
|
while (1) {
|
|
|
|
if (cur->mNext == this) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cur->mNext = mNext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = cur->mNext;
|
|
|
|
NS_ASSERTION(cur, "failed to find wrapper in its own chain");
|
|
|
|
}
|
|
|
|
// let the root go
|
|
|
|
NS_RELEASE(mRoot);
|
|
|
|
}
|
|
|
|
|
2008-01-29 18:05:43 -08:00
|
|
|
NS_IF_RELEASE(mClass);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mOuter) {
|
2008-10-11 10:35:39 -07:00
|
|
|
XPCJSRuntime* rt = nsXPConnect::GetRuntimeInstance();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (rt->GetThreadRunningGC()) {
|
2008-01-29 18:05:43 -08:00
|
|
|
rt->DeferredRelease(mOuter);
|
|
|
|
mOuter = nsnull;
|
2011-10-14 10:52:48 -07:00
|
|
|
} else {
|
2008-01-29 18:05:43 -08:00
|
|
|
NS_RELEASE(mOuter);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPCWrappedJS*
|
|
|
|
nsXPCWrappedJS::Find(REFNSIID aIID)
|
|
|
|
{
|
2011-10-14 10:52:48 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsISupports)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return mRoot;
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
for (nsXPCWrappedJS* cur = mRoot; cur; cur = cur->mNext) {
|
2011-10-14 10:52:48 -07:00
|
|
|
if (aIID.Equals(cur->GetIID()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if asking for an interface that some wrapper in the chain inherits from
|
|
|
|
nsXPCWrappedJS*
|
|
|
|
nsXPCWrappedJS::FindInherited(REFNSIID aIID)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aIID.Equals(NS_GET_IID(nsISupports)), "bad call sequence");
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
for (nsXPCWrappedJS* cur = mRoot; cur; cur = cur->mNext) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found;
|
2011-10-14 10:52:48 -07:00
|
|
|
if (NS_SUCCEEDED(cur->GetClass()->GetInterfaceInfo()->
|
|
|
|
HasAncestor(&aIID, &found)) && found)
|
2007-03-22 10:30:00 -07:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::GetInterfaceInfo(nsIInterfaceInfo** info)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(GetClass(), "wrapper without class");
|
|
|
|
NS_ASSERTION(GetClass()->GetInterfaceInfo(), "wrapper class without interface");
|
|
|
|
|
|
|
|
// Since failing to get this info will crash some platforms(!), we keep
|
|
|
|
// mClass valid at shutdown time.
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!(*info = GetClass()->GetInterfaceInfo()))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
NS_ADDREF(*info);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::CallMethod(PRUint16 methodIndex,
|
|
|
|
const XPTMethodDescriptor* info,
|
|
|
|
nsXPTCMiniVariant* params)
|
|
|
|
{
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!IsValid())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-10-30 08:39:53 -07:00
|
|
|
if (NS_IsMainThread() != mMainThread) {
|
2010-11-17 12:58:53 -08:00
|
|
|
NS_NAMED_LITERAL_STRING(kFmt, "Attempt to use JS function on a different thread calling %s.%s. JS objects may not be shared across threads.");
|
|
|
|
PRUnichar* msg =
|
|
|
|
nsTextFormatter::smprintf(kFmt.get(),
|
|
|
|
GetClass()->GetInterfaceName(),
|
|
|
|
info->name);
|
|
|
|
nsCOMPtr<nsIConsoleService> cs =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (cs)
|
|
|
|
cs->LogStringMessage(msg);
|
|
|
|
NS_Free(msg);
|
2011-10-14 10:52:47 -07:00
|
|
|
|
2010-10-30 06:01:25 -07:00
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
2010-10-30 08:39:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return GetClass()->CallMethod(this, methodIndex, info, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::GetInterfaceIID(nsIID** iid)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(iid, "bad param");
|
|
|
|
|
|
|
|
*iid = (nsIID*) nsMemory::Clone(&(GetIID()), sizeof(nsIID));
|
|
|
|
return *iid ? NS_OK : NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXPCWrappedJS::SystemIsBeingShutDown(JSRuntime* rt)
|
|
|
|
{
|
|
|
|
// XXX It turns out that it is better to leak here then to do any Releases
|
|
|
|
// and have them propagate into all sorts of mischief as the system is being
|
|
|
|
// shutdown. This was learned the hard way :(
|
|
|
|
|
|
|
|
// mJSObj == nsnull is used to indicate that the wrapper is no longer valid
|
|
|
|
// and that calls should fail without trying to use any of the
|
|
|
|
// xpconnect mechanisms. 'IsValid' is implemented by checking this pointer.
|
|
|
|
|
|
|
|
// NOTE: that mClass is retained so that GetInterfaceInfo can continue to
|
|
|
|
// work (and avoid crashing some platforms).
|
|
|
|
mJSObj = nsnull;
|
|
|
|
|
|
|
|
// Notify other wrappers in the chain.
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mNext)
|
2007-03-22 10:30:00 -07:00
|
|
|
mNext->SystemIsBeingShutDown(rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
/* readonly attribute nsISimpleEnumerator enumerator; */
|
2011-10-14 10:52:47 -07:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPCWrappedJS::GetEnumerator(nsISimpleEnumerator * *aEnumerate)
|
|
|
|
{
|
|
|
|
XPCCallContext ccx(NATIVE_CALLER);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!ccx.IsValid())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2011-02-16 12:47:08 -08:00
|
|
|
return nsXPCWrappedJSClass::BuildPropertyEnumerator(ccx, GetJSObject(),
|
|
|
|
aEnumerate);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIVariant getProperty (in AString name); */
|
2011-10-14 10:52:47 -07:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPCWrappedJS::GetProperty(const nsAString & name, nsIVariant **_retval)
|
|
|
|
{
|
|
|
|
XPCCallContext ccx(NATIVE_CALLER);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!ccx.IsValid())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return nsXPCWrappedJSClass::
|
2012-03-08 11:13:34 -08:00
|
|
|
GetNamedPropertyAsVariant(ccx, GetJSObject(), name, _retval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXPCWrappedJS::DebugDump(PRInt16 depth)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
XPC_LOG_ALWAYS(("nsXPCWrappedJS @ %x with mRefCnt = %d", this, mRefCnt.get()));
|
|
|
|
XPC_LOG_INDENT();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isRoot = mRoot == this;
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_ALWAYS(("%s wrapper around JSObject @ %x", \
|
2011-10-14 10:52:48 -07:00
|
|
|
isRoot ? "ROOT":"non-root", mJSObj));
|
2007-03-22 10:30:00 -07:00
|
|
|
char* name;
|
|
|
|
GetClass()->GetInterfaceInfo()->GetName(&name);
|
|
|
|
XPC_LOG_ALWAYS(("interface name is %s", name));
|
2011-10-14 10:52:48 -07:00
|
|
|
if (name)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMemory::Free(name);
|
|
|
|
char * iid = GetClass()->GetIID().ToString();
|
|
|
|
XPC_LOG_ALWAYS(("IID number is %s", iid ? iid : "invalid"));
|
2011-10-14 10:52:48 -07:00
|
|
|
if (iid)
|
2009-07-29 10:54:44 -07:00
|
|
|
NS_Free(iid);
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_ALWAYS(("nsXPCWrappedJSClass @ %x", mClass));
|
|
|
|
|
2011-10-14 10:52:48 -07:00
|
|
|
if (!isRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_OUTDENT();
|
2011-10-14 10:52:48 -07:00
|
|
|
if (mNext) {
|
|
|
|
if (isRoot) {
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_ALWAYS(("Additional wrappers for this object..."));
|
|
|
|
XPC_LOG_INDENT();
|
|
|
|
}
|
|
|
|
mNext->DebugDump(depth);
|
2011-10-14 10:52:48 -07:00
|
|
|
if (isRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_OUTDENT();
|
|
|
|
}
|
2011-10-14 10:52:48 -07:00
|
|
|
if (isRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
XPC_LOG_OUTDENT();
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|