Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsIDOMClassInfo.h" |
michael@0 | 6 | #include "nsIDOMEvent.h" |
michael@0 | 7 | #include "nsIDOMEventListener.h" |
michael@0 | 8 | #include "nsPIDOMWindow.h" |
michael@0 | 9 | #include "nsIDocShell.h" |
michael@0 | 10 | #include "nsIPermissionManager.h" |
michael@0 | 11 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 12 | #include "AudioChannelManager.h" |
michael@0 | 13 | #include "mozilla/dom/AudioChannelManagerBinding.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla::hal; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace dom { |
michael@0 | 19 | namespace system { |
michael@0 | 20 | |
michael@0 | 21 | NS_IMPL_QUERY_INTERFACE_INHERITED(AudioChannelManager, DOMEventTargetHelper, |
michael@0 | 22 | nsIDOMEventListener) |
michael@0 | 23 | NS_IMPL_ADDREF_INHERITED(AudioChannelManager, DOMEventTargetHelper) |
michael@0 | 24 | NS_IMPL_RELEASE_INHERITED(AudioChannelManager, DOMEventTargetHelper) |
michael@0 | 25 | |
michael@0 | 26 | AudioChannelManager::AudioChannelManager() |
michael@0 | 27 | : mState(SWITCH_STATE_UNKNOWN) |
michael@0 | 28 | , mVolumeChannel(-1) |
michael@0 | 29 | { |
michael@0 | 30 | RegisterSwitchObserver(SWITCH_HEADPHONES, this); |
michael@0 | 31 | mState = GetCurrentSwitchState(SWITCH_HEADPHONES); |
michael@0 | 32 | SetIsDOMBinding(); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | AudioChannelManager::~AudioChannelManager() |
michael@0 | 36 | { |
michael@0 | 37 | UnregisterSwitchObserver(SWITCH_HEADPHONES, this); |
michael@0 | 38 | |
michael@0 | 39 | nsCOMPtr<EventTarget> target = do_QueryInterface(GetOwner()); |
michael@0 | 40 | NS_ENSURE_TRUE_VOID(target); |
michael@0 | 41 | |
michael@0 | 42 | target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"), |
michael@0 | 43 | this, |
michael@0 | 44 | /* useCapture = */ true); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | void |
michael@0 | 48 | AudioChannelManager::Init(nsPIDOMWindow* aWindow) |
michael@0 | 49 | { |
michael@0 | 50 | BindToOwner(aWindow->IsOuterWindow() ? |
michael@0 | 51 | aWindow->GetCurrentInnerWindow() : aWindow); |
michael@0 | 52 | |
michael@0 | 53 | nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(GetOwner()); |
michael@0 | 54 | NS_ENSURE_TRUE_VOID(target); |
michael@0 | 55 | |
michael@0 | 56 | target->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"), |
michael@0 | 57 | this, |
michael@0 | 58 | /* useCapture = */ true, |
michael@0 | 59 | /* wantsUntrusted = */ false); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | JSObject* |
michael@0 | 63 | AudioChannelManager::WrapObject(JSContext* aCx) |
michael@0 | 64 | { |
michael@0 | 65 | return AudioChannelManagerBinding::Wrap(aCx, this); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | void |
michael@0 | 69 | AudioChannelManager::Notify(const SwitchEvent& aEvent) |
michael@0 | 70 | { |
michael@0 | 71 | mState = aEvent.status(); |
michael@0 | 72 | |
michael@0 | 73 | DispatchTrustedEvent(NS_LITERAL_STRING("headphoneschange")); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | bool |
michael@0 | 77 | AudioChannelManager::SetVolumeControlChannel(const nsAString& aChannel) |
michael@0 | 78 | { |
michael@0 | 79 | if (aChannel.EqualsASCII("publicnotification")) { |
michael@0 | 80 | return false; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | AudioChannel newChannel = AudioChannelService::GetAudioChannel(aChannel); |
michael@0 | 84 | |
michael@0 | 85 | // Only normal channel doesn't need permission. |
michael@0 | 86 | if (newChannel != AudioChannel::Normal) { |
michael@0 | 87 | nsCOMPtr<nsIPermissionManager> permissionManager = |
michael@0 | 88 | do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); |
michael@0 | 89 | if (!permissionManager) { |
michael@0 | 90 | return false; |
michael@0 | 91 | } |
michael@0 | 92 | uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION; |
michael@0 | 93 | permissionManager->TestPermissionFromWindow(GetOwner(), |
michael@0 | 94 | nsCString(NS_LITERAL_CSTRING("audio-channel-") + |
michael@0 | 95 | NS_ConvertUTF16toUTF8(aChannel)).get(), &perm); |
michael@0 | 96 | if (perm != nsIPermissionManager::ALLOW_ACTION) { |
michael@0 | 97 | return false; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | if (mVolumeChannel == (int32_t)newChannel) { |
michael@0 | 102 | return true; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | mVolumeChannel = (int32_t)newChannel; |
michael@0 | 106 | |
michael@0 | 107 | NotifyVolumeControlChannelChanged(); |
michael@0 | 108 | return true; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | bool |
michael@0 | 112 | AudioChannelManager::GetVolumeControlChannel(nsAString & aChannel) |
michael@0 | 113 | { |
michael@0 | 114 | if (mVolumeChannel >= 0) { |
michael@0 | 115 | AudioChannelService::GetAudioChannelString( |
michael@0 | 116 | static_cast<AudioChannel>(mVolumeChannel), |
michael@0 | 117 | aChannel); |
michael@0 | 118 | } else { |
michael@0 | 119 | aChannel.AssignASCII(""); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | return true; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void |
michael@0 | 126 | AudioChannelManager::NotifyVolumeControlChannelChanged() |
michael@0 | 127 | { |
michael@0 | 128 | nsCOMPtr<nsIDocShell> docshell = do_GetInterface(GetOwner()); |
michael@0 | 129 | NS_ENSURE_TRUE_VOID(docshell); |
michael@0 | 130 | |
michael@0 | 131 | bool isActive = false; |
michael@0 | 132 | docshell->GetIsActive(&isActive); |
michael@0 | 133 | |
michael@0 | 134 | AudioChannelService* service = AudioChannelService::GetAudioChannelService(); |
michael@0 | 135 | if (isActive) { |
michael@0 | 136 | service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive); |
michael@0 | 137 | } else { |
michael@0 | 138 | service->SetDefaultVolumeControlChannel(-1, isActive); |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | NS_IMETHODIMP |
michael@0 | 143 | AudioChannelManager::HandleEvent(nsIDOMEvent* aEvent) |
michael@0 | 144 | { |
michael@0 | 145 | nsAutoString type; |
michael@0 | 146 | aEvent->GetType(type); |
michael@0 | 147 | |
michael@0 | 148 | if (type.EqualsLiteral("visibilitychange")) { |
michael@0 | 149 | NotifyVolumeControlChannelChanged(); |
michael@0 | 150 | } |
michael@0 | 151 | return NS_OK; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | } // namespace system |
michael@0 | 155 | } // namespace dom |
michael@0 | 156 | } // namespace mozilla |