Bug 859824 - MediaElement creates a WakeLock when playing audio. r=jlebar

This commit is contained in:
Andrea Marchesini 2013-05-02 12:05:00 -04:00
parent 15a1dca0e3
commit 61eecbe753
2 changed files with 52 additions and 13 deletions

View File

@ -518,14 +518,25 @@ protected:
class WakeLockBoolWrapper {
public:
WakeLockBoolWrapper(bool val = false) : mValue(val), mOuter(nullptr), mWakeLock(nullptr) {}
void SetOuter(HTMLMediaElement* outer) { mOuter = outer; }
operator bool() const { return mValue; }
WakeLockBoolWrapper(bool val = false)
: mValue(val), mCanPlay(true), mOuter(nullptr) {}
void SetOuter(nsHTMLMediaElement* outer) { mOuter = outer; }
void SetCanPlay(bool aCanPlay);
operator bool() { return mValue; }
WakeLockBoolWrapper& operator=(bool val);
bool operator !() const { return !mValue; }
private:
void UpdateWakeLock();
bool mValue;
HTMLMediaElement* mOuter;
bool mCanPlay;
nsHTMLMediaElement* mOuter;
nsCOMPtr<nsIDOMMozWakeLock> mWakeLock;
};

View File

@ -2088,21 +2088,47 @@ NS_IMETHODIMP HTMLMediaElement::Play()
return rv.ErrorCode();
}
HTMLMediaElement::WakeLockBoolWrapper& HTMLMediaElement::WakeLockBoolWrapper::operator=(bool val) {
if (mValue == val)
nsHTMLMediaElement::WakeLockBoolWrapper&
nsHTMLMediaElement::WakeLockBoolWrapper::operator=(bool val) {
if (mValue == val) {
return *this;
if (!mWakeLock && !val && mOuter) {
}
mValue = val;
UpdateWakeLock();
return *this;
}
void
nsHTMLMediaElement::WakeLockBoolWrapper::SetCanPlay(bool aCanPlay)
{
mCanPlay = aCanPlay;
UpdateWakeLock();
}
void
nsHTMLMediaElement::WakeLockBoolWrapper::UpdateWakeLock()
{
if (!mOuter) {
return;
}
bool playing = (!mValue && mCanPlay);
if (playing) {
nsCOMPtr<nsIPowerManagerService> pmService =
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
NS_ENSURE_TRUE(pmService, *this);
NS_ENSURE_TRUE_VOID(pmService);
pmService->NewWakeLock(NS_LITERAL_STRING("Playing_media"), mOuter->OwnerDoc()->GetWindow(), getter_AddRefs(mWakeLock));
} else if (mWakeLock && val) {
mWakeLock->Unlock();
if (!mWakeLock) {
pmService->NewWakeLock(NS_LITERAL_STRING("cpu"),
mOuter->OwnerDoc()->GetWindow(),
getter_AddRefs(mWakeLock));
}
} else if (mWakeLock) {
// Wakelock 'unlocks' itself in its destructor.
mWakeLock = nullptr;
}
mValue = val;
return *this;
}
bool HTMLMediaElement::ParseAttribute(int32_t aNamespaceID,
@ -3715,6 +3741,7 @@ void HTMLMediaElement::UpdateAudioChannelPlayingState()
if (mPlayingThroughTheAudioChannel) {
bool canPlay;
mAudioChannelAgent->StartPlaying(&canPlay);
mPaused.SetCanPlay(canPlay);
} else {
mAudioChannelAgent->StopPlaying();
mAudioChannelAgent = nullptr;
@ -3728,6 +3755,7 @@ NS_IMETHODIMP HTMLMediaElement::CanPlayChanged(bool canPlay)
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_NOT_AVAILABLE);
UpdateChannelMuteState(canPlay);
mPaused.SetCanPlay(canPlay);
return NS_OK;
}