dom/system/gonk/AudioChannelManager.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/system/gonk/AudioChannelManager.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsIDOMClassInfo.h"
     1.9 +#include "nsIDOMEvent.h"
    1.10 +#include "nsIDOMEventListener.h"
    1.11 +#include "nsPIDOMWindow.h"
    1.12 +#include "nsIDocShell.h"
    1.13 +#include "nsIPermissionManager.h"
    1.14 +#include "nsIInterfaceRequestorUtils.h"
    1.15 +#include "AudioChannelManager.h"
    1.16 +#include "mozilla/dom/AudioChannelManagerBinding.h"
    1.17 +
    1.18 +using namespace mozilla::hal;
    1.19 +
    1.20 +namespace mozilla {
    1.21 +namespace dom {
    1.22 +namespace system {
    1.23 +
    1.24 +NS_IMPL_QUERY_INTERFACE_INHERITED(AudioChannelManager, DOMEventTargetHelper,
    1.25 +                                  nsIDOMEventListener)
    1.26 +NS_IMPL_ADDREF_INHERITED(AudioChannelManager, DOMEventTargetHelper)
    1.27 +NS_IMPL_RELEASE_INHERITED(AudioChannelManager, DOMEventTargetHelper)
    1.28 +
    1.29 +AudioChannelManager::AudioChannelManager()
    1.30 +  : mState(SWITCH_STATE_UNKNOWN)
    1.31 +  , mVolumeChannel(-1)
    1.32 +{
    1.33 +  RegisterSwitchObserver(SWITCH_HEADPHONES, this);
    1.34 +  mState = GetCurrentSwitchState(SWITCH_HEADPHONES);
    1.35 +  SetIsDOMBinding();
    1.36 +}
    1.37 +
    1.38 +AudioChannelManager::~AudioChannelManager()
    1.39 +{
    1.40 +  UnregisterSwitchObserver(SWITCH_HEADPHONES, this);
    1.41 +
    1.42 +  nsCOMPtr<EventTarget> target = do_QueryInterface(GetOwner());
    1.43 +  NS_ENSURE_TRUE_VOID(target);
    1.44 +
    1.45 +  target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
    1.46 +                                    this,
    1.47 +                                    /* useCapture = */ true);
    1.48 +}
    1.49 +
    1.50 +void
    1.51 +AudioChannelManager::Init(nsPIDOMWindow* aWindow)
    1.52 +{
    1.53 +  BindToOwner(aWindow->IsOuterWindow() ?
    1.54 +              aWindow->GetCurrentInnerWindow() : aWindow);
    1.55 +
    1.56 +  nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(GetOwner());
    1.57 +  NS_ENSURE_TRUE_VOID(target);
    1.58 +
    1.59 +  target->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
    1.60 +                                 this,
    1.61 +                                 /* useCapture = */ true,
    1.62 +                                 /* wantsUntrusted = */ false);
    1.63 +}
    1.64 +
    1.65 +JSObject*
    1.66 +AudioChannelManager::WrapObject(JSContext* aCx)
    1.67 +{
    1.68 +  return AudioChannelManagerBinding::Wrap(aCx, this);
    1.69 +}
    1.70 +
    1.71 +void
    1.72 +AudioChannelManager::Notify(const SwitchEvent& aEvent)
    1.73 +{
    1.74 +  mState = aEvent.status();
    1.75 +
    1.76 +  DispatchTrustedEvent(NS_LITERAL_STRING("headphoneschange"));
    1.77 +}
    1.78 +
    1.79 +bool
    1.80 +AudioChannelManager::SetVolumeControlChannel(const nsAString& aChannel)
    1.81 +{
    1.82 +  if (aChannel.EqualsASCII("publicnotification")) {
    1.83 +    return false;
    1.84 +  }
    1.85 +
    1.86 +  AudioChannel newChannel = AudioChannelService::GetAudioChannel(aChannel);
    1.87 +
    1.88 +  // Only normal channel doesn't need permission.
    1.89 +  if (newChannel != AudioChannel::Normal) {
    1.90 +    nsCOMPtr<nsIPermissionManager> permissionManager =
    1.91 +      do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
    1.92 +    if (!permissionManager) {
    1.93 +      return false;
    1.94 +    }
    1.95 +    uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
    1.96 +    permissionManager->TestPermissionFromWindow(GetOwner(),
    1.97 +      nsCString(NS_LITERAL_CSTRING("audio-channel-") +
    1.98 +      NS_ConvertUTF16toUTF8(aChannel)).get(), &perm);
    1.99 +    if (perm != nsIPermissionManager::ALLOW_ACTION) {
   1.100 +      return false;
   1.101 +    }
   1.102 +  }
   1.103 +
   1.104 +  if (mVolumeChannel == (int32_t)newChannel) {
   1.105 +    return true;
   1.106 +  }
   1.107 +
   1.108 +  mVolumeChannel = (int32_t)newChannel;
   1.109 +
   1.110 +  NotifyVolumeControlChannelChanged();
   1.111 +  return true;
   1.112 +}
   1.113 +
   1.114 +bool
   1.115 +AudioChannelManager::GetVolumeControlChannel(nsAString & aChannel)
   1.116 +{
   1.117 +  if (mVolumeChannel >= 0) {
   1.118 +    AudioChannelService::GetAudioChannelString(
   1.119 +                                      static_cast<AudioChannel>(mVolumeChannel),
   1.120 +                                      aChannel);
   1.121 +  } else {
   1.122 +    aChannel.AssignASCII("");
   1.123 +  }
   1.124 +
   1.125 +  return true;
   1.126 +}
   1.127 +
   1.128 +void
   1.129 +AudioChannelManager::NotifyVolumeControlChannelChanged()
   1.130 +{
   1.131 +  nsCOMPtr<nsIDocShell> docshell = do_GetInterface(GetOwner());
   1.132 +  NS_ENSURE_TRUE_VOID(docshell);
   1.133 +
   1.134 +  bool isActive = false;
   1.135 +  docshell->GetIsActive(&isActive);
   1.136 +
   1.137 +  AudioChannelService* service = AudioChannelService::GetAudioChannelService();
   1.138 +  if (isActive) {
   1.139 +    service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive);
   1.140 +  } else {
   1.141 +    service->SetDefaultVolumeControlChannel(-1, isActive);
   1.142 +  }
   1.143 +}
   1.144 +
   1.145 +NS_IMETHODIMP
   1.146 +AudioChannelManager::HandleEvent(nsIDOMEvent* aEvent)
   1.147 +{
   1.148 +  nsAutoString type;
   1.149 +  aEvent->GetType(type);
   1.150 +
   1.151 +  if (type.EqualsLiteral("visibilitychange")) {
   1.152 +    NotifyVolumeControlChannelChanged();
   1.153 +  }
   1.154 +  return NS_OK;
   1.155 +}
   1.156 +
   1.157 +} // namespace system
   1.158 +} // namespace dom
   1.159 +} // namespace mozilla

mercurial