diff --git a/dom/media/MediaManager.cpp b/dom/media/MediaManager.cpp index 542ae84664bf..6bc72a8c4761 100644 --- a/dom/media/MediaManager.cpp +++ b/dom/media/MediaManager.cpp @@ -2532,17 +2532,8 @@ RefPtr MediaManager::GetUserMedia( // Create a window listener if it doesn't already exist. RefPtr windowListener = - GetWindowListener(windowID); - if (windowListener) { - PrincipalHandle existingPrincipalHandle = - windowListener->GetPrincipalHandle(); - MOZ_ASSERT(PrincipalHandleMatches(existingPrincipalHandle, principal)); - } else { - windowListener = new GetUserMediaWindowListener( - windowID, MakePrincipalHandle(principal)); - AddWindowID(windowID, windowListener); - } - + GetOrMakeWindowListener(aWindow); + MOZ_ASSERT(windowListener); auto sourceListener = MakeRefPtr(); windowListener->Register(sourceListener); @@ -3026,21 +3017,9 @@ RefPtr MediaManager::EnumerateDevices( uint64_t windowId = aWindow->WindowID(); Document* doc = aWindow->GetExtantDoc(); MOZ_ASSERT(doc); - - nsIPrincipal* principal = doc->NodePrincipal(); - RefPtr windowListener = - GetWindowListener(windowId); - if (windowListener) { - PrincipalHandle existingPrincipalHandle = - windowListener->GetPrincipalHandle(); - MOZ_ASSERT(PrincipalHandleMatches(existingPrincipalHandle, principal)); - } else { - windowListener = new GetUserMediaWindowListener( - windowId, MakePrincipalHandle(principal)); - AddWindowID(windowId, windowListener); - } - + GetOrMakeWindowListener(aWindow); + MOZ_ASSERT(windowListener); // Create an inactive SourceListener to act as a placeholder, so the // window listener doesn't clean itself up until we're done. auto sourceListener = MakeRefPtr(); @@ -3164,19 +3143,9 @@ RefPtr MediaManager::GetSinkDevice(nsPIDOMWindowInner* aWindow, // We have to add the window id here because enumerate methods // check for that and abort silently if it does not exist. - uint64_t windowId = aWindow->WindowID(); - nsIPrincipal* principal = aWindow->GetExtantDoc()->NodePrincipal(); RefPtr windowListener = - GetWindowListener(windowId); - if (windowListener) { - PrincipalHandle existingPrincipalHandle = - windowListener->GetPrincipalHandle(); - MOZ_ASSERT(PrincipalHandleMatches(existingPrincipalHandle, principal)); - } else { - windowListener = new GetUserMediaWindowListener( - windowId, MakePrincipalHandle(principal)); - AddWindowID(windowId, windowListener); - } + GetOrMakeWindowListener(aWindow); + MOZ_ASSERT(windowListener); // Create an inactive SourceListener to act as a placeholder, so the // window listener doesn't clean itself up until we're done. auto sourceListener = MakeRefPtr(); @@ -3321,6 +3290,28 @@ void MediaManager::OnMicrophoneMute(bool aMute) { } } +RefPtr MediaManager::GetOrMakeWindowListener( + nsPIDOMWindowInner* aWindow) { + Document* doc = aWindow->GetExtantDoc(); + if (!doc) { + // The window has been destroyed. Destroyed windows don't have listeners. + return nullptr; + } + nsIPrincipal* principal = doc->NodePrincipal(); + uint64_t windowId = aWindow->WindowID(); + RefPtr windowListener = + GetWindowListener(windowId); + if (windowListener) { + MOZ_ASSERT(PrincipalHandleMatches(windowListener->GetPrincipalHandle(), + principal)); + } else { + windowListener = new GetUserMediaWindowListener( + windowId, MakePrincipalHandle(principal)); + AddWindowID(windowId, windowListener); + } + return windowListener; +} + void MediaManager::AddWindowID(uint64_t aWindowId, RefPtr aListener) { MOZ_ASSERT(NS_IsMainThread()); diff --git a/dom/media/MediaManager.h b/dom/media/MediaManager.h index e3fda449c87f..c402fc4cc443 100644 --- a/dom/media/MediaManager.h +++ b/dom/media/MediaManager.h @@ -174,6 +174,11 @@ class MediaManager final : public nsIMediaManagerService, public nsIObserver { media::Parent* GetNonE10sParent(); MediaEngine* GetBackend(); + // If the window has not been destroyed, then return the + // GetUserMediaWindowListener for this window. + // If the window has been destroyed, then return null. + RefPtr GetOrMakeWindowListener( + nsPIDOMWindowInner* aWindow); WindowTable* GetActiveWindows() { MOZ_ASSERT(NS_IsMainThread()); return &mActiveWindows;