dom/audiochannel/AudioChannelAgent.cpp
author Andrea Marchesini <amarchesini@mozilla.com>
Fri, 25 Jan 2013 16:44:43 +0100
changeset 118339 694c52fd8d8e9dc17e141b8b85b47551a959126a
parent 118047 eebe5140bea59c14b158974bfebf8d8c805eaf4d
child 118968 da523063aa7b33b29fa7808d6c95bb1a0defd1ff
permissions -rw-r--r--
Bug 830648 - AudioChannelAgent::StartPlaying() should check if already registered to AudioChannelService., r=mchen, a=blocking-b2g

/* 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/. */

#include "AudioChannelAgent.h"
#include "AudioChannelCommon.h"
#include "AudioChannelService.h"

using namespace mozilla::dom;

NS_IMPL_ISUPPORTS1(AudioChannelAgent, nsIAudioChannelAgent)

AudioChannelAgent::AudioChannelAgent()
  : mCallback(nullptr)
  , mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
  , mIsRegToService(false)
  , mVisible(true)
{
}

AudioChannelAgent::~AudioChannelAgent()
{
  if (mIsRegToService) {
    StopPlaying();
  }
}

/* readonly attribute long audioChannelType; */
NS_IMETHODIMP AudioChannelAgent::GetAudioChannelType(int32_t *aAudioChannelType)
{
  *aAudioChannelType = mAudioChannelType;
  return NS_OK;
}

/* boolean init (in long channelType); */
NS_IMETHODIMP AudioChannelAgent::Init(int32_t channelType, nsIAudioChannelAgentCallback *callback)
{
  // We syncd the enum of channel type between nsIAudioChannelAgent.idl and
  // AudioChannelCommon.h the same.
  MOZ_STATIC_ASSERT(static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_NORMAL) ==
                    AUDIO_CHANNEL_NORMAL &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_CONTENT) ==
                    AUDIO_CHANNEL_CONTENT &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_NOTIFICATION) ==
                    AUDIO_CHANNEL_NOTIFICATION &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_ALARM) ==
                    AUDIO_CHANNEL_ALARM &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_TELEPHONY) ==
                    AUDIO_CHANNEL_TELEPHONY &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_RINGER) ==
                    AUDIO_CHANNEL_RINGER &&
                    static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION) ==
                    AUDIO_CHANNEL_PUBLICNOTIFICATION,
                    "Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelCommon.h");

  if (mAudioChannelType != AUDIO_AGENT_CHANNEL_ERROR ||
      channelType > AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION ||
      channelType < AUDIO_AGENT_CHANNEL_NORMAL) {
    return NS_ERROR_FAILURE;
  }

  mAudioChannelType = channelType;
  mCallback = callback;
  return NS_OK;
}

/* boolean startPlaying (); */
NS_IMETHODIMP AudioChannelAgent::StartPlaying(bool *_retval)
{
  AudioChannelService *service = AudioChannelService::GetAudioChannelService();
  if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
      service == nullptr || mIsRegToService) {
    return NS_ERROR_FAILURE;
  }

  service->RegisterAudioChannelAgent(this,
    static_cast<AudioChannelType>(mAudioChannelType));
  *_retval = !service->GetMuted(this, !mVisible);
  mIsRegToService = true;
  return NS_OK;
}

/* void stopPlaying (); */
NS_IMETHODIMP AudioChannelAgent::StopPlaying(void)
{
  if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
      !mIsRegToService) {
    return NS_ERROR_FAILURE;
  }

  AudioChannelService *service = AudioChannelService::GetAudioChannelService();
  service->UnregisterAudioChannelAgent(this);
  mIsRegToService = false;
  return NS_OK;
}

/* void setVisibilityState (in boolean visible); */
NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible)
{
  bool oldVisibility = mVisible;

  mVisible = visible;
  if (mIsRegToService && oldVisibility != mVisible && mCallback != nullptr) {
    AudioChannelService *service = AudioChannelService::GetAudioChannelService();
    mCallback->CanPlayChanged(!service->GetMuted(this, !mVisible));
  }
  return NS_OK;
}

void AudioChannelAgent::NotifyAudioChannelStateChanged()
{
  if (mCallback != nullptr) {
    AudioChannelService *service = AudioChannelService::GetAudioChannelService();
    mCallback->CanPlayChanged(!service->GetMuted(this, !mVisible));
  }
}