Bug 413447 - "nsXBLDocumentInfo can keep closed global window alive". r=jst, sr=sicking, a=blocking1.9+.

This commit is contained in:
bent.mozilla@gmail.com 2008-02-12 09:48:26 -08:00
parent b3ef4eabc6
commit e1b07adf64
6 changed files with 119 additions and 58 deletions

View File

@ -368,12 +368,6 @@ nsXBLPrototypeBinding::UnlinkJSObjects()
{ {
if (mImplementation) if (mImplementation)
mImplementation->UnlinkJSObjects(); mImplementation->UnlinkJSObjects();
nsXBLPrototypeHandler* curr = mPrototypeHandler;
while (curr) {
curr->UnlinkJSObjects();
curr = curr->GetNextHandler();
}
} }
void void
@ -381,12 +375,6 @@ nsXBLPrototypeBinding::Trace(TraceCallback aCallback, void *aClosure) const
{ {
if (mImplementation) if (mImplementation)
mImplementation->Trace(aCallback, aClosure); mImplementation->Trace(aCallback, aClosure);
nsXBLPrototypeHandler* curr = mPrototypeHandler;
while (curr) {
curr->Trace(aCallback, aClosure);
curr = curr->GetNextHandler();
}
} }
void void

View File

@ -118,7 +118,6 @@ nsXBLPrototypeHandler::nsXBLPrototypeHandler(const PRUnichar* aEvent,
PRUint32 aLineNumber) PRUint32 aLineNumber)
: mHandlerText(nsnull), : mHandlerText(nsnull),
mLineNumber(aLineNumber), mLineNumber(aLineNumber),
mCachedHandler(nsnull),
mNextHandler(nsnull), mNextHandler(nsnull),
mPrototypeBinding(aBinding) mPrototypeBinding(aBinding)
{ {
@ -135,7 +134,6 @@ nsXBLPrototypeHandler::nsXBLPrototypeHandler(const PRUnichar* aEvent,
nsXBLPrototypeHandler::nsXBLPrototypeHandler(nsIContent* aHandlerElement) nsXBLPrototypeHandler::nsXBLPrototypeHandler(nsIContent* aHandlerElement)
: mHandlerElement(nsnull), : mHandlerElement(nsnull),
mLineNumber(0), mLineNumber(0),
mCachedHandler(nsnull),
mNextHandler(nsnull), mNextHandler(nsnull),
mPrototypeBinding(nsnull) mPrototypeBinding(nsnull)
{ {
@ -161,19 +159,6 @@ nsXBLPrototypeHandler::~nsXBLPrototypeHandler()
delete mNextHandler; delete mNextHandler;
} }
void
nsXBLPrototypeHandler::Trace(TraceCallback aCallback, void *aClosure) const
{
if (mCachedHandler)
aCallback(nsIProgrammingLanguage::JAVASCRIPT, mCachedHandler, aClosure);
}
void
nsXBLPrototypeHandler::UnlinkJSObjects()
{
ForgetCachedHandler();
}
already_AddRefed<nsIContent> already_AddRefed<nsIContent>
nsXBLPrototypeHandler::GetHandlerElement() nsXBLPrototypeHandler::GetHandlerElement()
{ {
@ -196,11 +181,9 @@ nsXBLPrototypeHandler::AppendHandlerText(const nsAString& aText)
mHandlerText = ToNewUnicode(nsDependentString(temp) + aText); mHandlerText = ToNewUnicode(nsDependentString(temp) + aText);
nsMemory::Free(temp); nsMemory::Free(temp);
} }
else else {
mHandlerText = ToNewUnicode(aText); mHandlerText = ToNewUnicode(aText);
}
// Remove our cached handler
ForgetCachedHandler();
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -369,15 +352,12 @@ nsXBLPrototypeHandler::EnsureEventHandler(nsIScriptGlobalObject* aGlobal,
nsScriptObjectHolder &aHandler) nsScriptObjectHolder &aHandler)
{ {
// Check to see if we've already compiled this // Check to see if we've already compiled this
if (mCachedHandler) { nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(aGlobal);
nsCOMPtr<nsIScriptGlobalObject> cachedGlobal = if (pWindow) {
do_QueryReferent(mGlobalForCachedHandler); void* cachedHandler = pWindow->GetCachedXBLPrototypeHandler(this);
if (cachedGlobal == aGlobal) { if (cachedHandler) {
aHandler.set(mCachedHandler); aHandler.set(cachedHandler);
if (!aHandler) return aHandler ? NS_OK : NS_ERROR_FAILURE;
return NS_ERROR_FAILURE;
return NS_OK;
} }
} }
@ -398,8 +378,9 @@ nsXBLPrototypeHandler::EnsureEventHandler(nsIScriptGlobalObject* aGlobal,
mLineNumber, aHandler); mLineNumber, aHandler);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
mCachedHandler = aHandler; if (pWindow) {
mGlobalForCachedHandler = do_GetWeakReference(aGlobal); pWindow->CacheXBLPrototypeHandler(this, aHandler);
}
return NS_OK; return NS_OK;
} }
@ -871,7 +852,6 @@ nsXBLPrototypeHandler::ConstructPrototype(nsIContent* aKeyElement,
mMisc = 0; mMisc = 0;
mKeyMask = 0; mKeyMask = 0;
mPhase = NS_PHASE_BUBBLING; mPhase = NS_PHASE_BUBBLING;
ForgetCachedHandler();
if (aAction) if (aAction)
mHandlerText = ToNewUnicode(nsDependentString(aAction)); mHandlerText = ToNewUnicode(nsDependentString(aAction));

View File

@ -153,9 +153,6 @@ public:
return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0; return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0;
} }
void Trace(TraceCallback aCallback, void *aClosure) const;
void UnlinkJSObjects();
public: public:
static PRUint32 gRefCnt; static PRUint32 gRefCnt;
@ -180,11 +177,6 @@ protected:
nsresult EnsureEventHandler(nsIScriptGlobalObject* aGlobal, nsresult EnsureEventHandler(nsIScriptGlobalObject* aGlobal,
nsIScriptContext *aBoundContext, nsIAtom *aName, nsIScriptContext *aBoundContext, nsIAtom *aName,
nsScriptObjectHolder &aHandler); nsScriptObjectHolder &aHandler);
void ForgetCachedHandler()
{
mCachedHandler = nsnull;
mGlobalForCachedHandler = nsnull;
}
static PRInt32 KeyToMask(PRInt32 key); static PRInt32 KeyToMask(PRInt32 key);
static PRInt32 kAccelKey; static PRInt32 kAccelKey;
@ -230,10 +222,6 @@ protected:
PRInt32 mDetail; // For key events, contains a charcode or keycode. For PRInt32 mDetail; // For key events, contains a charcode or keycode. For
// mouse events, stores the button info. // mouse events, stores the button info.
// cache a handler to avoid compiling each time
void *mCachedHandler;
nsWeakPtr mGlobalForCachedHandler;
// Prototype handlers are chained. We own the next handler in the chain. // Prototype handlers are chained. We own the next handler in the chain.
nsXBLPrototypeHandler* mNextHandler; nsXBLPrototypeHandler* mNextHandler;
nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress" nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress"

View File

@ -70,6 +70,8 @@ class nsIDocument;
class nsIScriptTimeoutHandler; class nsIScriptTimeoutHandler;
class nsPresContext; class nsPresContext;
struct nsTimeout; struct nsTimeout;
class nsScriptObjectHolder;
class nsXBLPrototypeHandler;
#define NS_PIDOMWINDOW_IID \ #define NS_PIDOMWINDOW_IID \
{ 0x909852b5, 0xb9e6, 0x4d94, \ { 0x909852b5, 0xb9e6, 0x4d94, \
@ -379,6 +381,10 @@ public:
*/ */
virtual void InitJavaProperties() = 0; virtual void InitJavaProperties() = 0;
virtual void* GetCachedXBLPrototypeHandler(nsXBLPrototypeHandler* aKey) = 0;
virtual void CacheXBLPrototypeHandler(nsXBLPrototypeHandler* aKey,
nsScriptObjectHolder& aHandler) = 0;
protected: protected:
// The nsPIDOMWindow constructor. The aOuterWindow argument should // The nsPIDOMWindow constructor. The aOuterWindow argument should
// be null if and only if the created window itself is an outer // be null if and only if the created window itself is an outer

View File

@ -580,7 +580,6 @@ nsTimeout::~nsTimeout()
MOZ_COUNT_DTOR(nsTimeout); MOZ_COUNT_DTOR(nsTimeout);
} }
//***************************************************************************** //*****************************************************************************
//*** nsGlobalWindow: Object Management //*** nsGlobalWindow: Object Management
@ -753,6 +752,23 @@ nsGlobalWindow::ShutDown()
NS_IF_RELEASE(sComputedDOMStyleFactory); NS_IF_RELEASE(sComputedDOMStyleFactory);
} }
// static
void
nsGlobalWindow::CleanupCachedXBLHandlers(nsGlobalWindow* aWindow)
{
if (aWindow->mCachedXBLPrototypeHandlers.IsInitialized() &&
aWindow->mCachedXBLPrototypeHandlers.Count() > 0) {
aWindow->mCachedXBLPrototypeHandlers.Clear();
nsCOMPtr<nsISupports> supports;
aWindow->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
getter_AddRefs(supports));
NS_ASSERTION(supports, "Failed to QI to nsCycleCollectionISupports?!");
nsContentUtils::DropJSObjects(supports);
}
}
void void
nsGlobalWindow::CleanUp() nsGlobalWindow::CleanUp()
{ {
@ -798,6 +814,8 @@ nsGlobalWindow::CleanUp()
mArguments = nsnull; mArguments = nsnull;
mArgumentsLast = nsnull; mArgumentsLast = nsnull;
CleanupCachedXBLHandlers(this);
#ifdef DEBUG #ifdef DEBUG
nsCycleCollector_DEBUG_shouldBeFreed(static_cast<nsIScriptGlobalObject*>(this)); nsCycleCollector_DEBUG_shouldBeFreed(static_cast<nsIScriptGlobalObject*>(this));
#endif #endif
@ -882,6 +900,8 @@ nsGlobalWindow::FreeInnerObjects(PRBool aClearScope)
} }
#endif #endif
CleanupCachedXBLHandlers(this);
#ifdef DEBUG #ifdef DEBUG
nsCycleCollector_DEBUG_shouldBeFreed(static_cast<nsIScriptGlobalObject*>(this)); nsCycleCollector_DEBUG_shouldBeFreed(static_cast<nsIScriptGlobalObject*>(this));
#endif #endif
@ -1012,6 +1032,30 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGlobalWindow)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
struct TraceData
{
TraceCallback& callback;
void* closure;
};
PR_STATIC_CALLBACK(PLDHashOperator)
TraceXBLHandlers(const void* aKey, void* aData, void* aClosure)
{
TraceData* data = static_cast<TraceData*>(aClosure);
data->callback(nsIProgrammingLanguage::JAVASCRIPT, aData, data->closure);
return PL_DHASH_NEXT;
}
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsGlobalWindow)
if (tmp->mCachedXBLPrototypeHandlers.IsInitialized()) {
TraceData data = { aCallback, aClosure };
tmp->mCachedXBLPrototypeHandlers.EnumerateRead(TraceXBLHandlers, &data);
}
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTION_ROOT_BEGIN(nsGlobalWindow)
nsGlobalWindow::CleanupCachedXBLHandlers(tmp);
NS_IMPL_CYCLE_COLLECTION_ROOT_END
//***************************************************************************** //*****************************************************************************
// nsGlobalWindow::nsIScriptGlobalObject // nsGlobalWindow::nsIScriptGlobalObject
@ -5560,6 +5604,49 @@ nsGlobalWindow::InitJavaProperties()
#endif #endif
} }
void*
nsGlobalWindow::GetCachedXBLPrototypeHandler(nsXBLPrototypeHandler* aKey)
{
void* handler = nsnull;
if (mCachedXBLPrototypeHandlers.IsInitialized()) {
mCachedXBLPrototypeHandlers.Get(aKey, &handler);
}
return handler;
}
void
nsGlobalWindow::CacheXBLPrototypeHandler(nsXBLPrototypeHandler* aKey,
nsScriptObjectHolder& aHandler)
{
if (!mCachedXBLPrototypeHandlers.IsInitialized() &&
!mCachedXBLPrototypeHandlers.Init()) {
NS_ERROR("Failed to initiailize hashtable!");
return;
}
if (!mCachedXBLPrototypeHandlers.Count()) {
// Can't use macros to get the participant because nsGlobalChromeWindow also
// runs through this code. Use QueryInterface to get the correct objects.
nsXPCOMCycleCollectionParticipant* participant;
CallQueryInterface(this, &participant);
NS_ASSERTION(participant,
"Failed to QI to nsXPCOMCycleCollectionParticipant!");
nsCOMPtr<nsISupports> thisSupports;
QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
getter_AddRefs(thisSupports));
NS_ASSERTION(thisSupports, "Failed to QI to nsCycleCollectionISupports!");
nsresult rv = nsContentUtils::HoldJSObjects(thisSupports, participant);
if (NS_FAILED(rv)) {
NS_ERROR("nsContentUtils::HoldJSObjects failed!");
return;
}
}
mCachedXBLPrototypeHandlers.Put(aKey, aHandler);
}
NS_IMETHODIMP NS_IMETHODIMP
nsGlobalWindow::GetFrameElement(nsIDOMElement** aFrameElement) nsGlobalWindow::GetFrameElement(nsIDOMElement** aFrameElement)
{ {

View File

@ -51,6 +51,7 @@
#include "nsHashtable.h" #include "nsHashtable.h"
#include "nsDataHashtable.h" #include "nsDataHashtable.h"
#include "nsCycleCollectionParticipant.h" #include "nsCycleCollectionParticipant.h"
#include "nsDOMScriptObjectHolder.h"
// Interfaces Needed // Interfaces Needed
#include "nsDOMWindowList.h" #include "nsDOMWindowList.h"
@ -410,6 +411,7 @@ public:
const PRUnichar* aData); const PRUnichar* aData);
static void ShutDown(); static void ShutDown();
static void CleanupCachedXBLHandlers(nsGlobalWindow* aWindow);
static PRBool IsCallerChrome(); static PRBool IsCallerChrome();
static void CloseBlockScriptTerminationFunc(nsISupports *aRef); static void CloseBlockScriptTerminationFunc(nsISupports *aRef);
@ -418,11 +420,19 @@ public:
friend class WindowStateHolder; friend class WindowStateHolder;
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsGlobalWindow, NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsGlobalWindow,
nsIScriptGlobalObject) nsIScriptGlobalObject)
void InitJavaProperties(); void InitJavaProperties();
virtual NS_HIDDEN_(void*)
GetCachedXBLPrototypeHandler(nsXBLPrototypeHandler* aKey);
virtual NS_HIDDEN_(void)
CacheXBLPrototypeHandler(nsXBLPrototypeHandler* aKey,
nsScriptObjectHolder& aHandler);
protected: protected:
// Object Management // Object Management
virtual ~nsGlobalWindow(); virtual ~nsGlobalWindow();
@ -722,6 +732,8 @@ protected:
nsCOMPtr<nsIDOMOfflineResourceList> mApplicationCache; nsCOMPtr<nsIDOMOfflineResourceList> mApplicationCache;
nsDataHashtable<nsVoidPtrHashKey, void*> mCachedXBLPrototypeHandlers;
friend class nsDOMScriptableHelper; friend class nsDOMScriptableHelper;
friend class nsDOMWindowUtils; friend class nsDOMWindowUtils;
static nsIFactory *sComputedDOMStyleFactory; static nsIFactory *sComputedDOMStyleFactory;