From 55b4443d06c01f1e2fae560cce1ca7f2559fe08c Mon Sep 17 00:00:00 2001 From: "jband%netscape.com" Date: Thu, 3 Feb 2000 03:25:53 +0000 Subject: [PATCH] fix XPCContext leak bug 25911 r=beard. fix DOM wrapping case where static called object does not have a static DOM scope, but is run in a DOM context; e.g. calling a JS component from within a window that calls some native service that returns a DOM window - the serive is not a DOm object and has no static DOm scope, but the call is made on a DOM context so a dynamic lookup works. r=mscott git-svn-id: svn://10.0.0.236/trunk@59613 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/js/src/xpconnect/src/xpcconvert.cpp | 32 ++++++++++++++++++- mozilla/js/src/xpconnect/src/xpcjsruntime.cpp | 26 +++++++++++---- mozilla/js/src/xpconnect/src/xpcprivate.h | 1 + 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/mozilla/js/src/xpconnect/src/xpcconvert.cpp b/mozilla/js/src/xpconnect/src/xpcconvert.cpp index c7836dd4053..5b45e2967e8 100644 --- a/mozilla/js/src/xpconnect/src/xpcconvert.cpp +++ b/mozilla/js/src/xpconnect/src/xpcconvert.cpp @@ -135,7 +135,7 @@ GetISupportsFromJSObject(JSContext* cx, JSObject* obj, nsISupports** iface) } /***************************************************************************/ -// This copied from nsJSUtils.cpp in DOMLand +// These are copied from nsJSUtils.cpp in DOMLand static nsresult GetStaticScriptGlobal(JSContext* aContext, @@ -174,6 +174,34 @@ GetStaticScriptContext(JSContext* aContext, return scriptContext ? NS_OK : NS_ERROR_FAILURE; } +static nsresult +GetDynamicScriptContext(JSContext *aContext, + nsIScriptContext** aScriptContext) +{ + // XXX We rely on the rule that if any JSContext in our JSRuntime has a + // private set then that private *must* be a pointer to an nsISupports. + nsISupports *supports = (nsIScriptContext*) JS_GetContextPrivate(aContext); + if (!supports) + return nsnull; + return supports->QueryInterface(NS_GET_IID(nsIScriptContext), + (void**)aScriptContext); +} + +#if 0 +// never called. +static nsresult +GetDynamicScriptGlobal(JSContext* aContext, + nsIScriptGlobalObject** aNativeGlobal) +{ + nsIScriptGlobalObject* nativeGlobal = nsnull; + nsCOMPtr scriptCX; + GetDynamicScriptContext(aContext, getter_AddRefs(scriptCX)); + if (scriptCX) { + *aNativeGlobal = nativeGlobal = scriptCX->GetGlobalObject(); + } + return nativeGlobal ? NS_OK : NS_ERROR_FAILURE; +} +#endif /***************************************************************************/ /***************************************************************************/ @@ -668,6 +696,8 @@ XPCConvert::NativeInterface2JSObject(JSContext* cx, // is a DOM object nsCOMPtr scriptCX; GetStaticScriptContext(cx, scope, getter_AddRefs(scriptCX)); + if(!scriptCX) + GetDynamicScriptContext(cx, getter_AddRefs(scriptCX)); JSObject* aJSObj = nsnull; if(scriptCX && NS_SUCCEEDED(owner->GetScriptObject(scriptCX, (void **)&aJSObj))) diff --git a/mozilla/js/src/xpconnect/src/xpcjsruntime.cpp b/mozilla/js/src/xpconnect/src/xpcjsruntime.cpp index df60a1267bd..8399ab14e0f 100644 --- a/mozilla/js/src/xpconnect/src/xpcjsruntime.cpp +++ b/mozilla/js/src/xpconnect/src/xpcjsruntime.cpp @@ -61,7 +61,7 @@ XPCJSRuntime::~XPCJSRuntime() while(JS_ContextIterator(mJSRuntime, &iter)) count ++; if(count) - printf("deleting XPCJSRuntime with %d total live JSContexts\n", count); + printf("deleting XPCJSRuntime with %d live JSContexts\n", count); } #endif @@ -69,12 +69,7 @@ XPCJSRuntime::~XPCJSRuntime() if(mContextMap) { - SyncXPCContextList(); -#ifdef DEBUG_jband - uint32 count = mContextMap->Count(); - if(count) - printf("deleting XPCJSRuntime with %d live JSContexts known by xpconnect\n", (int)count); -#endif + PurgeXPCContextList(); delete mContextMap; } @@ -239,6 +234,23 @@ XPCJSRuntime::SyncXPCContextList(JSContext* cx /* = nsnull */) return found; } +JS_STATIC_DLL_CALLBACK(intN) +PurgeContextsCB(JSHashEntry *he, intN i, void *arg) +{ + delete (XPCContext*) he->value; + return HT_ENUMERATE_REMOVE; +} + +void +XPCJSRuntime::PurgeXPCContextList() +{ + // hold the map lock through this whole thing + nsAutoLock lock(mMapLock); + + // get rid of all XPCContexts + mContextMap->Enumerate(PurgeContextsCB, nsnull); +} + JSBool XPCJSRuntime::GenerateStringIDs(JSContext* cx) { diff --git a/mozilla/js/src/xpconnect/src/xpcprivate.h b/mozilla/js/src/xpconnect/src/xpcprivate.h index 39f945deeb4..f05e6a522f8 100644 --- a/mozilla/js/src/xpconnect/src/xpcprivate.h +++ b/mozilla/js/src/xpconnect/src/xpcprivate.h @@ -228,6 +228,7 @@ private: JSContext2XPCContextMap* GetContextMap() const {return mContextMap;} JSBool GenerateStringIDs(JSContext* cx); + void PurgeXPCContextList(); private: static const char* mStrings[IDX_TOTAL_COUNT];