2012-11-29 14:52:03 -08:00
|
|
|
/* 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 "nsIDOMClassInfo.h"
|
2013-09-25 04:21:19 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
2013-09-12 05:26:03 -07:00
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "AudioChannelManager.h"
|
2013-02-08 08:34:47 -08:00
|
|
|
#include "mozilla/dom/AudioChannelManagerBinding.h"
|
2012-11-29 14:52:03 -08:00
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace system {
|
|
|
|
|
2013-09-12 05:26:03 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE_INHERITED1(AudioChannelManager, nsDOMEventTargetHelper,
|
|
|
|
nsIDOMEventListener)
|
|
|
|
NS_IMPL_ADDREF_INHERITED(AudioChannelManager, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AudioChannelManager, nsDOMEventTargetHelper)
|
|
|
|
|
2012-11-29 14:52:03 -08:00
|
|
|
AudioChannelManager::AudioChannelManager()
|
|
|
|
: mState(SWITCH_STATE_UNKNOWN)
|
2013-09-12 05:26:03 -07:00
|
|
|
, mVolumeChannel(AUDIO_CHANNEL_DEFAULT)
|
2012-11-29 14:52:03 -08:00
|
|
|
{
|
|
|
|
RegisterSwitchObserver(SWITCH_HEADPHONES, this);
|
|
|
|
mState = GetCurrentSwitchState(SWITCH_HEADPHONES);
|
2013-02-08 08:34:47 -08:00
|
|
|
SetIsDOMBinding();
|
2012-11-29 14:52:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelManager::~AudioChannelManager()
|
|
|
|
{
|
|
|
|
UnregisterSwitchObserver(SWITCH_HEADPHONES, this);
|
2013-09-12 05:26:03 -07:00
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(target);
|
|
|
|
|
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this,
|
|
|
|
/* useCapture = */ true);
|
2012-11-29 14:52:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelManager::Init(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
BindToOwner(aWindow->IsOuterWindow() ?
|
2013-09-12 05:26:03 -07:00
|
|
|
aWindow->GetCurrentInnerWindow() : aWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(target);
|
|
|
|
|
|
|
|
target->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this,
|
|
|
|
/* useCapture = */ true,
|
|
|
|
/* wantsUntrusted = */ false);
|
2012-11-29 14:52:03 -08:00
|
|
|
}
|
|
|
|
|
2013-02-08 08:34:47 -08:00
|
|
|
JSObject*
|
2013-04-25 09:29:54 -07:00
|
|
|
AudioChannelManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2012-11-29 14:52:03 -08:00
|
|
|
{
|
2013-03-11 16:03:47 -07:00
|
|
|
return AudioChannelManagerBinding::Wrap(aCx, aScope, this);
|
2012-11-29 14:52:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelManager::Notify(const SwitchEvent& aEvent)
|
|
|
|
{
|
2013-07-16 23:21:00 -07:00
|
|
|
mState = aEvent.status();
|
2012-11-29 14:52:03 -08:00
|
|
|
|
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("headphoneschange"));
|
|
|
|
}
|
|
|
|
|
2013-09-12 05:26:03 -07:00
|
|
|
bool
|
|
|
|
AudioChannelManager::SetVolumeControlChannel(const nsAString& aChannel)
|
|
|
|
{
|
|
|
|
if (aChannel.EqualsASCII("publicnotification")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelType oldVolumeChannel = mVolumeChannel;
|
|
|
|
// Only normal channel doesn't need permission.
|
|
|
|
if (aChannel.EqualsASCII("normal")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_NORMAL;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
if (!permissionManager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
permissionManager->TestPermissionFromWindow(GetOwner(),
|
|
|
|
nsCString(NS_LITERAL_CSTRING("audio-channel-") +
|
|
|
|
NS_ConvertUTF16toUTF8(aChannel)).get(), &perm);
|
|
|
|
if (perm != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aChannel.EqualsASCII("content")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_CONTENT;
|
|
|
|
} else if (aChannel.EqualsASCII("notification")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_NOTIFICATION;
|
|
|
|
} else if (aChannel.EqualsASCII("alarm")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_ALARM;
|
|
|
|
} else if (aChannel.EqualsASCII("telephony")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_TELEPHONY;
|
|
|
|
} else if (aChannel.EqualsASCII("ringer")) {
|
|
|
|
mVolumeChannel = AUDIO_CHANNEL_RINGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldVolumeChannel == mVolumeChannel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NotifyVolumeControlChannelChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioChannelManager::GetVolumeControlChannel(nsAString & aChannel)
|
|
|
|
{
|
|
|
|
if (mVolumeChannel == AUDIO_CHANNEL_NORMAL) {
|
|
|
|
aChannel.AssignASCII("normal");
|
|
|
|
} else if (mVolumeChannel == AUDIO_CHANNEL_CONTENT) {
|
|
|
|
aChannel.AssignASCII("content");
|
|
|
|
} else if (mVolumeChannel == AUDIO_CHANNEL_NOTIFICATION) {
|
|
|
|
aChannel.AssignASCII("notification");
|
|
|
|
} else if (mVolumeChannel == AUDIO_CHANNEL_ALARM) {
|
|
|
|
aChannel.AssignASCII("alarm");
|
|
|
|
} else if (mVolumeChannel == AUDIO_CHANNEL_TELEPHONY) {
|
|
|
|
aChannel.AssignASCII("telephony");
|
|
|
|
} else if (mVolumeChannel == AUDIO_CHANNEL_RINGER) {
|
|
|
|
aChannel.AssignASCII("ringer");
|
|
|
|
} else {
|
|
|
|
aChannel.AssignASCII("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelManager::NotifyVolumeControlChannelChanged()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docshell = do_GetInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(docshell);
|
|
|
|
|
|
|
|
bool isActive = false;
|
|
|
|
docshell->GetIsActive(&isActive);
|
|
|
|
|
|
|
|
AudioChannelService* service = AudioChannelService::GetAudioChannelService();
|
|
|
|
if (isActive) {
|
|
|
|
service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive);
|
|
|
|
} else {
|
|
|
|
service->SetDefaultVolumeControlChannel(AUDIO_CHANNEL_DEFAULT, isActive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelManager::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
|
|
|
|
if (type.EqualsLiteral("visibilitychange")) {
|
|
|
|
NotifyVolumeControlChannelChanged();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-29 14:52:03 -08:00
|
|
|
} // namespace system
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|