Bug 1645852 - do not call `BrowsingContext::Get()` after XPCOM shutdown. r=bryce
authoralwu <alwu@mozilla.com>
Tue, 23 Jun 2020 18:39:57 +0000
changeset 536953 63961738f91ef8c2d00f6e68d6a1c3c06b87c8ad
parent 536952 47a06752bda80871a7ebe98fc6b03755b1e96093
child 536954 6f212eb26c1abd5a9a1de48d97a32548ae010a4f
push id37533
push userdluca@mozilla.com
push dateTue, 23 Jun 2020 21:38:40 +0000
treeherdermozilla-central@d48aa0f0aa0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbryce
bugs1645852
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1645852 - do not call `BrowsingContext::Get()` after XPCOM shutdown. r=bryce If XPCOM has been shutdown, then we should not access the `BrowsingContext::Get()` because `sBrowsingContexts` has also been destroyed. Differential Revision: https://phabricator.services.mozilla.com/D80559
dom/media/mediacontrol/ContentMediaController.cpp
--- a/dom/media/mediacontrol/ContentMediaController.cpp
+++ b/dom/media/mediacontrol/ContentMediaController.cpp
@@ -46,16 +46,27 @@ GetContentMediaControllerFromBrowsingCon
     controller = new ContentMediaController(topLevelBCId);
     sControllers->Put(topLevelBCId, controller);
   } else {
     controller = sControllers->Get(topLevelBCId);
   }
   return controller.forget();
 }
 
+static already_AddRefed<BrowsingContext> GetBrowsingContextForAgent(
+    uint64_t aBrowsingContextId) {
+  // The content media agent would only be created after having `sControllers`.
+  // If the `sControllers` doesn't exist, which means XPCOM has been shutdown
+  // and we're not able to access browsing context as well.
+  if (!sControllers) {
+    return nullptr;
+  }
+  return BrowsingContext::Get(aBrowsingContextId);
+}
+
 /* static */
 ContentMediaControlKeyReceiver* ContentMediaControlKeyReceiver::Get(
     BrowsingContext* aBC) {
   MOZ_ASSERT(NS_IsMainThread());
   RefPtr<ContentMediaController> controller =
       GetContentMediaControllerFromBrowsingContext(aBC);
   return controller
              ? static_cast<ContentMediaControlKeyReceiver*>(controller.get())
@@ -69,17 +80,17 @@ ContentMediaAgent* ContentMediaAgent::Ge
       GetContentMediaControllerFromBrowsingContext(aBC);
   return controller ? static_cast<ContentMediaAgent*>(controller.get())
                     : nullptr;
 }
 
 void ContentMediaAgent::NotifyMediaPlaybackChanged(uint64_t aBrowsingContextId,
                                                    MediaPlaybackState aState) {
   MOZ_ASSERT(NS_IsMainThread());
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media %s in BC %" PRId64, ToMediaPlaybackStateStr(aState),
       bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
@@ -92,17 +103,17 @@ void ContentMediaAgent::NotifyMediaPlayb
       updater->NotifyMediaPlaybackChanged(bc->Id(), aState);
     }
   }
 }
 
 void ContentMediaAgent::NotifyMediaAudibleChanged(uint64_t aBrowsingContextId,
                                                   MediaAudibleState aState) {
   MOZ_ASSERT(NS_IsMainThread());
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media became %s in BC %" PRId64,
       aState == MediaAudibleState::eAudible ? "audible" : "inaudible",
       bc->Id());
   if (XRE_IsContentProcess()) {
@@ -116,17 +127,17 @@ void ContentMediaAgent::NotifyMediaAudib
       updater->NotifyMediaAudibleChanged(bc->Id(), aState);
     }
   }
 }
 
 void ContentMediaAgent::SetIsInPictureInPictureMode(
     uint64_t aBrowsingContextId, bool aIsInPictureInPictureMode) {
   MOZ_ASSERT(NS_IsMainThread());
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media Picture-in-Picture mode '%s' in BC %" PRId64,
       aIsInPictureInPictureMode ? "enabled" : "disabled", bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
@@ -139,17 +150,17 @@ void ContentMediaAgent::SetIsInPictureIn
             bc->Canonical()->GetMediaController()) {
       updater->SetIsInPictureInPictureMode(bc->Id(), aIsInPictureInPictureMode);
     }
   }
 }
 
 void ContentMediaAgent::SetDeclaredPlaybackState(
     uint64_t aBrowsingContextId, MediaSessionPlaybackState aState) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify declared playback state  '%s' in BC %" PRId64,
       ToMediaSessionPlaybackStateStr(aState), bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
@@ -160,17 +171,17 @@ void ContentMediaAgent::SetDeclaredPlayb
   // This would only happen when we disable e10s.
   if (RefPtr<IMediaInfoUpdater> updater =
           bc->Canonical()->GetMediaController()) {
     updater->SetDeclaredPlaybackState(bc->Id(), aState);
   }
 }
 
 void ContentMediaAgent::NotifySessionCreated(uint64_t aBrowsingContextId) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media session being created in BC %" PRId64, bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
     Unused << contentChild->SendNotifyMediaSessionUpdated(bc, true);
@@ -179,17 +190,17 @@ void ContentMediaAgent::NotifySessionCre
   // This would only happen when we disable e10s.
   if (RefPtr<IMediaInfoUpdater> updater =
           bc->Canonical()->GetMediaController()) {
     updater->NotifySessionCreated(bc->Id());
   }
 }
 
 void ContentMediaAgent::NotifySessionDestroyed(uint64_t aBrowsingContextId) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media session being destroyed in BC %" PRId64, bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
     Unused << contentChild->SendNotifyMediaSessionUpdated(bc, false);
@@ -199,17 +210,17 @@ void ContentMediaAgent::NotifySessionDes
   if (RefPtr<IMediaInfoUpdater> updater =
           bc->Canonical()->GetMediaController()) {
     updater->NotifySessionDestroyed(bc->Id());
   }
 }
 
 void ContentMediaAgent::UpdateMetadata(
     uint64_t aBrowsingContextId, const Maybe<MediaMetadataBase>& aMetadata) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify media session metadata change in BC %" PRId64, bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
     Unused << contentChild->SendNotifyUpdateMediaMetadata(bc, aMetadata);
@@ -219,17 +230,17 @@ void ContentMediaAgent::UpdateMetadata(
   if (RefPtr<IMediaInfoUpdater> updater =
           bc->Canonical()->GetMediaController()) {
     updater->UpdateMetadata(bc->Id(), aMetadata);
   }
 }
 
 void ContentMediaAgent::EnableAction(uint64_t aBrowsingContextId,
                                      MediaSessionAction aAction) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify to enable action '%s' in BC %" PRId64,
       ToMediaSessionActionStr(aAction), bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();
@@ -241,17 +252,17 @@ void ContentMediaAgent::EnableAction(uin
   if (RefPtr<IMediaInfoUpdater> updater =
           bc->Canonical()->GetMediaController()) {
     updater->EnableAction(bc->Id(), aAction);
   }
 }
 
 void ContentMediaAgent::DisableAction(uint64_t aBrowsingContextId,
                                       MediaSessionAction aAction) {
-  RefPtr<BrowsingContext> bc = BrowsingContext::Get(aBrowsingContextId);
+  RefPtr<BrowsingContext> bc = GetBrowsingContextForAgent(aBrowsingContextId);
   if (!bc || bc->IsDiscarded()) {
     return;
   }
 
   LOG("Notify to disable action '%s' in BC %" PRId64,
       ToMediaSessionActionStr(aAction), bc->Id());
   if (XRE_IsContentProcess()) {
     ContentChild* contentChild = ContentChild::GetSingleton();