Bug 1265771 P1 Only store active documents in the global observer list. r=bz a=ritu

This commit is contained in:
Ben Kelly 2016-05-04 14:18:40 -07:00
parent ac2453b514
commit 01037cb33e

View File

@ -1665,11 +1665,6 @@ nsDocument::~nsDocument()
mImageTracker.Clear();
mPlugins.Clear();
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
os->RemoveObserver(this, "service-worker-get-client");
}
}
NS_INTERFACE_TABLE_HEAD(nsDocument)
@ -2071,11 +2066,6 @@ nsDocument::Init()
mozilla::HoldJSObjects(this);
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
os->AddObserver(this, "service-worker-get-client", /* ownsWeak */ true);
}
return NS_OK;
}
@ -4659,6 +4649,28 @@ nsDocument::SetScriptGlobalObject(nsIScriptGlobalObject *aScriptGlobalObject)
}
swm->MaybeStopControlling(this);
}
// Remove ourself from the list of clients. We only register
// content principal documents in this list.
if (!nsContentUtils::IsSystemPrincipal(GetPrincipal()) &&
!GetPrincipal()->GetIsNullPrincipal()) {
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
os->RemoveObserver(this, "service-worker-get-client");
}
}
} else if (!mScriptGlobalObject && aScriptGlobalObject &&
mDocumentContainer && GetChannel() &&
!nsContentUtils::IsSystemPrincipal(GetPrincipal()) &&
!GetPrincipal()->GetIsNullPrincipal()) {
// This document is being activated. Register it in the list of
// clients. We only do this for content principal documents
// since we can never observe system or null principals.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
os->AddObserver(this, "service-worker-get-client", /* ownsWeak */ false);
}
}
mScriptGlobalObject = aScriptGlobalObject;
@ -12423,11 +12435,18 @@ nsDocument::Observe(nsISupports *aSubject,
OnAppThemeChanged();
}
} else if (strcmp("service-worker-get-client", aTopic) == 0) {
nsAutoString clientId;
GetOrCreateId(clientId);
// No need to generate the ID if it doesn't exist here. The ID being
// requested must already be generated in order to passed in as
// aSubject.
nsString clientId = GetId();
if (!clientId.IsEmpty() && clientId.Equals(aData)) {
nsCOMPtr<nsISupportsInterfacePointer> ifptr = do_QueryInterface(aSubject);
if (ifptr) {
#ifdef DEBUG
nsCOMPtr<nsISupports> value;
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(ifptr->GetData(getter_AddRefs(value))));
MOZ_ASSERT(!value);
#endif
ifptr->SetData(static_cast<nsIDocument*>(this));
ifptr->SetDataIID(&NS_GET_IID(nsIDocument));
}