Bug 1319771 - part1 : only resume the window when there has active media components.
For the first pinned tab, it would be set to visible first and then set to
invisible if there exists other tabs after restarting the whole browser.
If the tab is set to visible, we would activate the media component (set the
|mMediaSuspended| in outer window to none-suspend). In this case, the first
pinned tab would be set to visible briefly, but it doesn't mean the tab is in
the foreground, it's just how DOM manage the tab's visibility.
In that moment, none of the media component has been created yet. Therefore, we
would only activate the media component after the audio channel service exists.
MozReview-Commit-ID: 1FgdMq84yWX
--- a/dom/audiochannel/AudioChannelAgent.cpp
+++ b/dom/audiochannel/AudioChannelAgent.cpp
@@ -36,16 +36,19 @@ NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
AudioChannelAgent::AudioChannelAgent()
: mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
, mInnerWindowID(0)
, mIsRegToService(false)
{
+ // Init service in the begining, it can help us to know whether there is any
+ // created media component via AudioChannelService::IsServiceStarted().
+ RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
}
AudioChannelAgent::~AudioChannelAgent()
{
Shutdown();
}
void
--- a/dom/audiochannel/AudioChannelService.cpp
+++ b/dom/audiochannel/AudioChannelService.cpp
@@ -196,16 +196,23 @@ AudioChannelService::CreateServiceIfNeed
{
MOZ_ASSERT(NS_IsMainThread());
if (!gAudioChannelService) {
gAudioChannelService = new AudioChannelService();
}
}
+/* static */ bool
+AudioChannelService::IsServiceStarted()
+{
+ // The service would start when the first AudioChannelAgent is created.
+ return !!gAudioChannelService;
+}
+
/* static */ already_AddRefed<AudioChannelService>
AudioChannelService::GetOrCreate()
{
if (sXPCOMShuttingDown) {
return nullptr;
}
CreateServiceIfNeeded();
--- a/dom/audiochannel/AudioChannelService.h
+++ b/dom/audiochannel/AudioChannelService.h
@@ -95,16 +95,18 @@ public:
static already_AddRefed<AudioChannelService> GetOrCreate();
static bool IsAudioChannelMutedByDefault();
static PRLogModuleInfo* GetAudioChannelLog();
static bool IsEnableAudioCompeting();
+ static bool IsServiceStarted();
+
/**
* Any audio channel agent that starts playing should register itself to
* this service, sharing the AudioChannel.
*/
void RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
AudibleState aAudible);
/**
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -3,16 +3,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Base class for all our document implementations.
*/
+#include "AudioChannelService.h"
#include "nsDocument.h"
#include "nsIDocumentInlines.h"
#include "mozilla/AnimationComparator.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/EffectSet.h"
@@ -12074,17 +12075,18 @@ nsDocument::MaybeActiveMediaComponents()
return;
}
if (!mWindow) {
return;
}
mEverInForeground = true;
- if (GetWindow()->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK) {
+ if (GetWindow()->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK &&
+ AudioChannelService::IsServiceStarted()) {
GetWindow()->SetMediaSuspend(nsISuspendedTypes::NONE_SUSPENDED);
}
}
NS_IMETHODIMP
nsDocument::GetHidden(bool* aHidden)
{
*aHidden = Hidden();