2012-11-29 22:52:03 +00: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 11:21:19 +00:00
|
|
|
#include "nsIDOMEvent.h"
|
2013-09-12 12:26:03 +00:00
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "AudioChannelManager.h"
|
2013-02-08 16:34:47 +00:00
|
|
|
#include "mozilla/dom/AudioChannelManagerBinding.h"
|
2014-04-29 17:27:26 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-11-29 22:52:03 +00:00
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace system {
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE_INHERITED(AudioChannelManager, DOMEventTargetHelper,
|
|
|
|
nsIDOMEventListener)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(AudioChannelManager, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AudioChannelManager, DOMEventTargetHelper)
|
2013-09-12 12:26:03 +00:00
|
|
|
|
2012-11-29 22:52:03 +00:00
|
|
|
AudioChannelManager::AudioChannelManager()
|
|
|
|
: mState(SWITCH_STATE_UNKNOWN)
|
2014-04-10 17:39:20 +00:00
|
|
|
, mVolumeChannel(-1)
|
2012-11-29 22:52:03 +00:00
|
|
|
{
|
|
|
|
RegisterSwitchObserver(SWITCH_HEADPHONES, this);
|
|
|
|
mState = GetCurrentSwitchState(SWITCH_HEADPHONES);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelManager::~AudioChannelManager()
|
|
|
|
{
|
|
|
|
UnregisterSwitchObserver(SWITCH_HEADPHONES, this);
|
2013-09-12 12:26:03 +00:00
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(target);
|
|
|
|
|
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this,
|
|
|
|
/* useCapture = */ true);
|
2012-11-29 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelManager::Init(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
BindToOwner(aWindow->IsOuterWindow() ?
|
2013-09-12 12:26:03 +00: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 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 16:34:47 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
AudioChannelManager::WrapObject(JSContext* aCx)
|
2012-11-29 22:52:03 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return AudioChannelManagerBinding::Wrap(aCx, this);
|
2012-11-29 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelManager::Notify(const SwitchEvent& aEvent)
|
|
|
|
{
|
2013-07-17 06:21:00 +00:00
|
|
|
mState = aEvent.status();
|
2012-11-29 22:52:03 +00:00
|
|
|
|
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("headphoneschange"));
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:26:03 +00:00
|
|
|
bool
|
|
|
|
AudioChannelManager::SetVolumeControlChannel(const nsAString& aChannel)
|
|
|
|
{
|
|
|
|
if (aChannel.EqualsASCII("publicnotification")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:39:20 +00:00
|
|
|
AudioChannel newChannel = AudioChannelService::GetAudioChannel(aChannel);
|
|
|
|
|
2013-09-12 12:26:03 +00:00
|
|
|
// Only normal channel doesn't need permission.
|
2014-04-10 17:39:20 +00:00
|
|
|
if (newChannel != AudioChannel::Normal) {
|
2013-09-12 12:26:03 +00:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
2014-04-29 17:27:26 +00:00
|
|
|
services::GetPermissionManager();
|
2013-09-12 12:26:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:39:20 +00:00
|
|
|
if (mVolumeChannel == (int32_t)newChannel) {
|
2013-09-12 12:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-10 17:39:20 +00:00
|
|
|
|
|
|
|
mVolumeChannel = (int32_t)newChannel;
|
|
|
|
|
2013-09-12 12:26:03 +00:00
|
|
|
NotifyVolumeControlChannelChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioChannelManager::GetVolumeControlChannel(nsAString & aChannel)
|
|
|
|
{
|
2014-04-10 17:39:20 +00:00
|
|
|
if (mVolumeChannel >= 0) {
|
|
|
|
AudioChannelService::GetAudioChannelString(
|
|
|
|
static_cast<AudioChannel>(mVolumeChannel),
|
|
|
|
aChannel);
|
2013-09-12 12:26:03 +00:00
|
|
|
} 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);
|
|
|
|
|
2014-08-29 06:10:16 +00:00
|
|
|
AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService();
|
2013-09-12 12:26:03 +00:00
|
|
|
if (isActive) {
|
|
|
|
service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive);
|
|
|
|
} else {
|
2014-04-10 17:39:20 +00:00
|
|
|
service->SetDefaultVolumeControlChannel(-1, isActive);
|
2013-09-12 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelManager::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
|
|
|
|
if (type.EqualsLiteral("visibilitychange")) {
|
|
|
|
NotifyVolumeControlChannelChanged();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-29 22:52:03 +00:00
|
|
|
} // namespace system
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|