Bug 1261012 - Check whether nsIObserverService is nullptr. r?smaug
During shutdown, mozilla::services::GetObserverServie will return nullptr. So we should check it. Add another nullptr check
MozReview-Commit-ID: 9xBbltRatJF
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -5141,22 +5141,24 @@ nsDocument::DispatchContentLoadedEvents(
mPreloadedPreconnects.Clear();
if (mTiming) {
mTiming->NotifyDOMContentLoadedStart(nsIDocument::GetDocumentURI());
}
// Dispatch observer notification to notify observers document is interactive.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
- nsIPrincipal *principal = GetPrincipal();
- os->NotifyObservers(static_cast<nsIDocument*>(this),
- nsContentUtils::IsSystemPrincipal(principal) ?
- "chrome-document-interactive" :
- "content-document-interactive",
- nullptr);
+ if (os) {
+ nsIPrincipal *principal = GetPrincipal();
+ os->NotifyObservers(static_cast<nsIDocument*>(this),
+ nsContentUtils::IsSystemPrincipal(principal) ?
+ "chrome-document-interactive" :
+ "content-document-interactive",
+ nullptr);
+ }
// Fire a DOM event notifying listeners that this document has been
// loaded (excluding images and other loads initiated by this
// document).
nsContentUtils::DispatchTrustedEvent(this, static_cast<nsIDocument*>(this),
NS_LITERAL_STRING("DOMContentLoaded"),
true, false);
@@ -9276,25 +9278,27 @@ nsDocument::OnPageShow(bool aPersisted,
nsCOMPtr<EventTarget> target = aDispatchStartTarget;
if (!target) {
target = do_QueryInterface(GetWindow());
}
// Dispatch observer notification to notify observers page is shown.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
- nsIPrincipal *principal = GetPrincipal();
- os->NotifyObservers(static_cast<nsIDocument*>(this),
- nsContentUtils::IsSystemPrincipal(principal) ?
- "chrome-page-shown" :
- "content-page-shown",
- nullptr);
- if (!mObservingAppThemeChanged) {
- os->AddObserver(this, "app-theme-changed", /* ownsWeak */ false);
- mObservingAppThemeChanged = true;
+ if (os) {
+ nsIPrincipal *principal = GetPrincipal();
+ os->NotifyObservers(static_cast<nsIDocument*>(this),
+ nsContentUtils::IsSystemPrincipal(principal) ?
+ "chrome-page-shown" :
+ "content-page-shown",
+ nullptr);
+ if (!mObservingAppThemeChanged) {
+ os->AddObserver(this, "app-theme-changed", /* ownsWeak */ false);
+ mObservingAppThemeChanged = true;
+ }
}
DispatchPageTransition(target, NS_LITERAL_STRING("pageshow"), aPersisted);
}
static bool
NotifyPageHide(nsIDocument* aDocument, void* aData)
{
--- a/layout/base/nsDocumentViewer.cpp
+++ b/layout/base/nsDocumentViewer.cpp
@@ -972,22 +972,24 @@ nsDocumentViewer::LoadComplete(nsresult
RefPtr<nsDOMNavigationTiming> timing(d->GetNavigationTiming());
if (timing) {
timing->NotifyLoadEventStart();
}
// Dispatch observer notification to notify observers document load is complete.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
- nsIPrincipal *principal = d->NodePrincipal();
- os->NotifyObservers(d,
- nsContentUtils::IsSystemPrincipal(principal) ?
- "chrome-document-loaded" :
- "content-document-loaded",
- nullptr);
+ if (os) {
+ nsIPrincipal *principal = d->NodePrincipal();
+ os->NotifyObservers(d,
+ nsContentUtils::IsSystemPrincipal(principal) ?
+ "chrome-document-loaded" :
+ "content-document-loaded",
+ nullptr);
+ }
// Notify any devtools about the load.
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
if (timelines && timelines->HasConsumer(docShell)) {
timelines->AddMarkerForDocShell(docShell,
MakeUnique<DocLoadingTimelineMarker>("document::Load"));
}