From 2a3cdfc4f665d3d73cdf34c2166fef509c069ef7 Mon Sep 17 00:00:00 2001 From: Andreas Farre Date: Fri, 1 Sep 2017 10:32:33 +0200 Subject: [PATCH] Bug 1393359 - Register active user media on top level window. r=smaug,jib --- dom/base/TimeoutManager.cpp | 3 +-- dom/base/nsGlobalWindow.cpp | 27 ++++++++++++++++++++++++++- dom/base/nsPIDOMWindow.h | 9 +++++++++ dom/media/MediaManager.cpp | 8 ++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/dom/base/TimeoutManager.cpp b/dom/base/TimeoutManager.cpp index 27990346a5d3..4bb253ed6c82 100644 --- a/dom/base/TimeoutManager.cpp +++ b/dom/base/TimeoutManager.cpp @@ -1258,8 +1258,7 @@ TimeoutManager::BudgetThrottlingEnabled(bool aIsBackground) const } // Check if we have active GetUserMedia - if (MediaManager::Exists() && - MediaManager::Get()->IsWindowStillActive(mWindow.WindowID())) { + if (mWindow.AsInner()->HasActiveUserMedia()) { return false; } diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 2966038ba9cf..ad78cba8dde0 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -1039,7 +1039,8 @@ nsPIDOMWindow::nsPIDOMWindow(nsPIDOMWindowOuter *aOuterWindow) mLargeAllocStatus(LargeAllocStatus::NONE), mHasTriedToCacheTopInnerWindow(false), mNumOfIndexedDBDatabases(0), - mNumOfOpenWebSockets(0) + mNumOfOpenWebSockets(0), + mNumOfActiveUserMedia(0) { if (aOuterWindow) { mTimeoutManager = @@ -4524,6 +4525,30 @@ nsPIDOMWindowInner::HasOpenWebSockets() const : mNumOfOpenWebSockets) > 0; } +void +nsPIDOMWindowInner::UpdateUserMediaCount(int32_t aDelta) +{ + MOZ_ASSERT(NS_IsMainThread()); + + if (aDelta == 0) { + return; + } + + uint32_t& counter = mTopInnerWindow ? mTopInnerWindow->mNumOfActiveUserMedia + : mNumOfActiveUserMedia; + + MOZ_DIAGNOSTIC_ASSERT(aDelta > 0 || ((aDelta + counter) < counter)); + + counter += aDelta; +} + +bool +nsPIDOMWindowInner::HasActiveUserMedia() const +{ + return (mTopInnerWindow ? mTopInnerWindow->mNumOfActiveUserMedia + : mNumOfActiveUserMedia) > 0; +} + void nsPIDOMWindowOuter::MaybeActiveMediaComponents() { diff --git a/dom/base/nsPIDOMWindow.h b/dom/base/nsPIDOMWindow.h index ce523e577ad2..e836c7c45c8a 100644 --- a/dom/base/nsPIDOMWindow.h +++ b/dom/base/nsPIDOMWindow.h @@ -751,6 +751,9 @@ protected: // The number of open WebSockets. Inner window only. uint32_t mNumOfOpenWebSockets; + + // The number of active user media. Inner window only. + uint32_t mNumOfActiveUserMedia; }; #define NS_PIDOMWINDOWINNER_IID \ @@ -936,6 +939,12 @@ public: // timeout-throttling. bool HasOpenWebSockets() const; + // Increase/Decrease the number of active user media. + void UpdateUserMediaCount(int32_t aDelta); + + // Return true if there are any currently ongoing user media. + bool HasActiveUserMedia() const; + protected: void CreatePerformanceObjectIfNeeded(); }; diff --git a/dom/media/MediaManager.cpp b/dom/media/MediaManager.cpp index 488b8a7015e4..a908dffce7f6 100644 --- a/dom/media/MediaManager.cpp +++ b/dom/media/MediaManager.cpp @@ -2926,6 +2926,12 @@ MediaManager::AddWindowID(uint64_t aWindowId, MOZ_ASSERT(false, "Window already added"); return; } + + auto* window = nsGlobalWindow::GetInnerWindowWithId(aWindowId); + if (window) { + window->AsInner()->UpdateUserMediaCount(1); + } + GetActiveWindows()->Put(aWindowId, aListener); } @@ -2941,6 +2947,8 @@ MediaManager::RemoveWindowID(uint64_t aWindowId) return; } + window->AsInner()->UpdateUserMediaCount(-1); + nsPIDOMWindowOuter* outer = window->AsInner()->GetOuterWindow(); if (!outer) { LOG(("No outer window for inner %" PRIu64, aWindowId));