Bug 940426 - part 1 - properly stop observing all the sources in nsXULTemplateBuilder; r=bz

This commit is contained in:
Nathan Froyd 2013-11-19 11:21:00 -05:00
parent 72d3b87910
commit 3441f8f6d6
2 changed files with 37 additions and 13 deletions

View File

@ -171,6 +171,25 @@ nsXULTemplateBuilder::InitGlobals()
return NS_OK;
}
void
nsXULTemplateBuilder::StartObserving(nsIDocument* aDocument)
{
aDocument->AddObserver(this);
mObservedDocument = aDocument;
gObserverService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
gObserverService->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, false);
}
void
nsXULTemplateBuilder::StopObserving()
{
MOZ_ASSERT(mObservedDocument);
mObservedDocument->RemoveObserver(this);
mObservedDocument = nullptr;
gObserverService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
gObserverService->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
}
void
nsXULTemplateBuilder::CleanUp(bool aIsFinal)
{
@ -193,10 +212,7 @@ void
nsXULTemplateBuilder::Uninit(bool aIsFinal)
{
if (mObservedDocument && aIsFinal) {
gObserverService->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
gObserverService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
mObservedDocument->RemoveObserver(this);
mObservedDocument = nullptr;
StopObserving();
}
if (mQueryProcessor)
@ -428,14 +444,7 @@ nsXULTemplateBuilder::Init(nsIContent* aElement)
nsresult rv = LoadDataSources(doc, &shouldDelay);
if (NS_SUCCEEDED(rv)) {
// Add ourselves as a document observer
doc->AddObserver(this);
mObservedDocument = doc;
gObserverService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
false);
gObserverService->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC,
false);
StartObserving(doc);
}
return rv;
@ -1124,7 +1133,8 @@ nsXULTemplateBuilder::ContentRemoved(nsIDocument* aDocument,
nsContentUtils::AddScriptRunner(
NS_NewRunnableMethod(this, &nsXULTemplateBuilder::UninitFalse));
aDocument->RemoveObserver(this);
MOZ_ASSERT(aDocument == mObservedDocument);
StopObserving();
nsCOMPtr<nsIXULDocument> xuldoc = do_QueryInterface(aDocument);
if (xuldoc)

View File

@ -480,6 +480,20 @@ protected:
{
}
/**
* Start observing events from the observer service and the given
* document.
*
* @param aDocument the document to observe
*/
void StartObserving(nsIDocument* aDocument);
/**
* Stop observing events from the observer service and any associated
* document.
*/
void StopObserving();
/**
* Document that we're observing. Weak ref!
*/