Bug 1315551 - part2 : check we have initialized agent when we want to call the agent's function. r=jwwang

Because the agent's initialization might fail if we don't get the valid inner window, we need to
check whether the agent exists before calling the agent's method.

MozReview-Commit-ID: IUuvyGh7CMd
This commit is contained in:
Alastor Wu
2016-11-08 11:08:02 +08:00
parent 1c96ba72b4
commit 8beec95340
2 changed files with 31 additions and 15 deletions

View File

@@ -2952,7 +2952,7 @@ HTMLMediaElement::HTMLMediaElement(already_AddRefed<mozilla::dom::NodeInfo>& aNo
mWatchManager.Watch(mReadyState, &HTMLMediaElement::UpdateReadyStateInternal);
mShutdownObserver->Subscribe(this);
CreateAudioChannelAgent();
MaybeCreateAudioChannelAgent();
}
HTMLMediaElement::~HTMLMediaElement()
@@ -2993,6 +2993,7 @@ HTMLMediaElement::~HTMLMediaElement()
}
WakeLockRelease();
mAudioChannelAgent = nullptr;
}
void HTMLMediaElement::StopSuspendingAfterFirstFrame()
@@ -5755,17 +5756,26 @@ ImageContainer* HTMLMediaElement::GetImageContainer()
return container ? container->GetImageContainer() : nullptr;
}
void
HTMLMediaElement::CreateAudioChannelAgent()
bool
HTMLMediaElement::MaybeCreateAudioChannelAgent()
{
if (mAudioChannelAgent) {
return;
return true;
}
mAudioChannelAgent = new AudioChannelAgent();
mAudioChannelAgent->InitWithWeakCallback(OwnerDoc()->GetInnerWindow(),
nsresult rv = mAudioChannelAgent->InitWithWeakCallback(OwnerDoc()->GetInnerWindow(),
static_cast<int32_t>(mAudioChannel),
this);
if (NS_WARN_IF(NS_FAILED(rv))) {
mAudioChannelAgent = nullptr;
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
("HTMLMediaElement, Fail to initialize the audio channel agent,"
" this = %p\n", this));
return false;
}
return true;
}
bool
@@ -5821,6 +5831,10 @@ HTMLMediaElement::UpdateAudioChannelPlayingState(bool aForcePlaying)
aForcePlaying || IsPlayingThroughTheAudioChannel();
if (playingThroughTheAudioChannel != mPlayingThroughTheAudioChannel) {
if (!MaybeCreateAudioChannelAgent()) {
return;
}
mPlayingThroughTheAudioChannel = playingThroughTheAudioChannel;
NotifyAudioChannelAgent(mPlayingThroughTheAudioChannel);
}
@@ -6031,7 +6045,7 @@ HTMLMediaElement::IsAllowedToPlay()
// If the tab hasn't been activated yet, the media element in that tab can't
// be playback now until the tab goes to foreground first time or user clicks
// the unblocking tab icon.
if (!IsTabActivated()) {
if (MaybeCreateAudioChannelAgent() && !IsTabActivated()) {
// Even we haven't start playing yet, we still need to notify the audio
// channe system because we need to receive the resume notification later.
UpdateAudioChannelPlayingState(true /* force to start */);
@@ -6044,6 +6058,7 @@ HTMLMediaElement::IsAllowedToPlay()
bool
HTMLMediaElement::IsTabActivated() const
{
MOZ_ASSERT(mAudioChannelAgent);
return !mAudioChannelAgent->ShouldBlockMedia();
}
@@ -6455,9 +6470,8 @@ HTMLMediaElement::SetAudibleState(bool aAudible)
void
HTMLMediaElement::NotifyAudioPlaybackChanged(AudibleChangedReasons aReason)
{
MOZ_ASSERT(mAudioChannelAgent);
if (!mAudioChannelAgent->IsPlayingStarted()) {
if (MaybeCreateAudioChannelAgent() &&
!mAudioChannelAgent->IsPlayingStarted()) {
return;
}
@@ -6500,6 +6514,7 @@ HTMLMediaElement::MaybeNotifyMediaResumed(SuspendTypes aSuspend)
return;
}
MOZ_ASSERT(mAudioChannelAgent);
uint64_t windowID = mAudioChannelAgent->WindowID();
NS_DispatchToMainThread(NS_NewRunnableFunction([windowID]() -> void {
nsCOMPtr<nsIObserverService> observerService =
@@ -6576,14 +6591,13 @@ HTMLMediaElement::SetMediaInfo(const MediaInfo& aInfo)
void
HTMLMediaElement::AudioCaptureStreamChangeIfNeeded()
{
MOZ_ASSERT(mAudioChannelAgent);
// No need to capture a silence media element.
if (!HasAudio()) {
return;
}
if (!mAudioChannelAgent->IsPlayingStarted()) {
if (MaybeCreateAudioChannelAgent() &&
!mAudioChannelAgent->IsPlayingStarted()) {
return;
}