dom/audiochannel/AudioChannelAgent.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "AudioChannelAgent.h"
michael@0 6 #include "AudioChannelCommon.h"
michael@0 7 #include "AudioChannelService.h"
michael@0 8 #include "nsIDOMWindow.h"
michael@0 9 #include "nsPIDOMWindow.h"
michael@0 10 #include "nsXULAppAPI.h"
michael@0 11
michael@0 12 using namespace mozilla::dom;
michael@0 13
michael@0 14 NS_IMPL_CYCLE_COLLECTION(AudioChannelAgent, mWindow, mCallback)
michael@0 15
michael@0 16 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioChannelAgent)
michael@0 17 NS_INTERFACE_MAP_ENTRY(nsIAudioChannelAgent)
michael@0 18 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 19 NS_INTERFACE_MAP_END
michael@0 20
michael@0 21 NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
michael@0 22 NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
michael@0 23
michael@0 24 AudioChannelAgent::AudioChannelAgent()
michael@0 25 : mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
michael@0 26 , mIsRegToService(false)
michael@0 27 , mVisible(true)
michael@0 28 , mWithVideo(false)
michael@0 29 {
michael@0 30 }
michael@0 31
michael@0 32 AudioChannelAgent::~AudioChannelAgent()
michael@0 33 {
michael@0 34 if (mIsRegToService) {
michael@0 35 StopPlaying();
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 /* readonly attribute long audioChannelType; */
michael@0 40 NS_IMETHODIMP AudioChannelAgent::GetAudioChannelType(int32_t *aAudioChannelType)
michael@0 41 {
michael@0 42 *aAudioChannelType = mAudioChannelType;
michael@0 43 return NS_OK;
michael@0 44 }
michael@0 45
michael@0 46 /* boolean init (in nsIDOMWindow window, in long channelType,
michael@0 47 * in nsIAudioChannelAgentCallback callback); */
michael@0 48 NS_IMETHODIMP
michael@0 49 AudioChannelAgent::Init(nsIDOMWindow* aWindow, int32_t aChannelType,
michael@0 50 nsIAudioChannelAgentCallback *aCallback)
michael@0 51 {
michael@0 52 return InitInternal(aWindow, aChannelType, aCallback,
michael@0 53 /* useWeakRef = */ false);
michael@0 54 }
michael@0 55
michael@0 56 /* boolean initWithWeakCallback (in nsIDOMWindow window, in long channelType,
michael@0 57 * in nsIAudioChannelAgentCallback callback); */
michael@0 58 NS_IMETHODIMP
michael@0 59 AudioChannelAgent::InitWithWeakCallback(nsIDOMWindow* aWindow,
michael@0 60 int32_t aChannelType,
michael@0 61 nsIAudioChannelAgentCallback *aCallback)
michael@0 62 {
michael@0 63 return InitInternal(aWindow, aChannelType, aCallback,
michael@0 64 /* useWeakRef = */ true);
michael@0 65 }
michael@0 66
michael@0 67 /* void initWithVideo(in nsIDOMWindow window, in long channelType,
michael@0 68 * in nsIAudioChannelAgentCallback callback, in boolean weak); */
michael@0 69 NS_IMETHODIMP
michael@0 70 AudioChannelAgent::InitWithVideo(nsIDOMWindow* aWindow, int32_t aChannelType,
michael@0 71 nsIAudioChannelAgentCallback *aCallback,
michael@0 72 bool aUseWeakRef)
michael@0 73 {
michael@0 74 return InitInternal(aWindow, aChannelType, aCallback, aUseWeakRef,
michael@0 75 /* withVideo = */ true);
michael@0 76 }
michael@0 77
michael@0 78 nsresult
michael@0 79 AudioChannelAgent::InitInternal(nsIDOMWindow* aWindow, int32_t aChannelType,
michael@0 80 nsIAudioChannelAgentCallback *aCallback,
michael@0 81 bool aUseWeakRef, bool aWithVideo)
michael@0 82 {
michael@0 83 // We need the window only for IPC.
michael@0 84 MOZ_ASSERT(aWindow || XRE_GetProcessType() == GeckoProcessType_Default);
michael@0 85
michael@0 86 // We syncd the enum of channel type between nsIAudioChannelAgent.idl and
michael@0 87 // AudioChannelBinding.h the same.
michael@0 88 MOZ_ASSERT(int(AUDIO_AGENT_CHANNEL_NORMAL) == int(AudioChannel::Normal) &&
michael@0 89 int(AUDIO_AGENT_CHANNEL_CONTENT) == int(AudioChannel::Content) &&
michael@0 90 int(AUDIO_AGENT_CHANNEL_NOTIFICATION) == int(AudioChannel::Notification) &&
michael@0 91 int(AUDIO_AGENT_CHANNEL_ALARM) == int(AudioChannel::Alarm) &&
michael@0 92 int(AUDIO_AGENT_CHANNEL_TELEPHONY) == int(AudioChannel::Telephony) &&
michael@0 93 int(AUDIO_AGENT_CHANNEL_RINGER) == int(AudioChannel::Ringer) &&
michael@0 94 int(AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION) == int(AudioChannel::Publicnotification),
michael@0 95 "Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelBinding.h");
michael@0 96
michael@0 97 if (mAudioChannelType != AUDIO_AGENT_CHANNEL_ERROR ||
michael@0 98 aChannelType > AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION ||
michael@0 99 aChannelType < AUDIO_AGENT_CHANNEL_NORMAL) {
michael@0 100 return NS_ERROR_FAILURE;
michael@0 101 }
michael@0 102
michael@0 103 mWindow = aWindow;
michael@0 104 mAudioChannelType = aChannelType;
michael@0 105
michael@0 106 if (aUseWeakRef) {
michael@0 107 mWeakCallback = do_GetWeakReference(aCallback);
michael@0 108 } else {
michael@0 109 mCallback = aCallback;
michael@0 110 }
michael@0 111
michael@0 112 mWithVideo = aWithVideo;
michael@0 113
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117 /* boolean startPlaying (); */
michael@0 118 NS_IMETHODIMP AudioChannelAgent::StartPlaying(int32_t *_retval)
michael@0 119 {
michael@0 120 AudioChannelService *service = AudioChannelService::GetAudioChannelService();
michael@0 121 if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
michael@0 122 service == nullptr || mIsRegToService) {
michael@0 123 return NS_ERROR_FAILURE;
michael@0 124 }
michael@0 125
michael@0 126 service->RegisterAudioChannelAgent(this,
michael@0 127 static_cast<AudioChannel>(mAudioChannelType), mWithVideo);
michael@0 128 *_retval = service->GetState(this, !mVisible);
michael@0 129 mIsRegToService = true;
michael@0 130 return NS_OK;
michael@0 131 }
michael@0 132
michael@0 133 /* void stopPlaying (); */
michael@0 134 NS_IMETHODIMP AudioChannelAgent::StopPlaying(void)
michael@0 135 {
michael@0 136 if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
michael@0 137 !mIsRegToService) {
michael@0 138 return NS_ERROR_FAILURE;
michael@0 139 }
michael@0 140
michael@0 141 AudioChannelService *service = AudioChannelService::GetAudioChannelService();
michael@0 142 service->UnregisterAudioChannelAgent(this);
michael@0 143 mIsRegToService = false;
michael@0 144 return NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 /* void setVisibilityState (in boolean visible); */
michael@0 148 NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible)
michael@0 149 {
michael@0 150 bool oldVisibility = mVisible;
michael@0 151
michael@0 152 nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
michael@0 153
michael@0 154 mVisible = visible;
michael@0 155 if (mIsRegToService && oldVisibility != mVisible && callback) {
michael@0 156 AudioChannelService *service = AudioChannelService::GetAudioChannelService();
michael@0 157 callback->CanPlayChanged(service->GetState(this, !mVisible));
michael@0 158 }
michael@0 159 return NS_OK;
michael@0 160 }
michael@0 161
michael@0 162 void AudioChannelAgent::NotifyAudioChannelStateChanged()
michael@0 163 {
michael@0 164 nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
michael@0 165 if (callback) {
michael@0 166 AudioChannelService *service = AudioChannelService::GetAudioChannelService();
michael@0 167 callback->CanPlayChanged(service->GetState(this, !mVisible));
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 already_AddRefed<nsIAudioChannelAgentCallback>
michael@0 172 AudioChannelAgent::GetCallback()
michael@0 173 {
michael@0 174 nsCOMPtr<nsIAudioChannelAgentCallback> callback = mCallback;
michael@0 175 if (!callback) {
michael@0 176 callback = do_QueryReferent(mWeakCallback);
michael@0 177 }
michael@0 178 return callback.forget();
michael@0 179 }
michael@0 180
michael@0 181 void
michael@0 182 AudioChannelAgent::WindowVolumeChanged()
michael@0 183 {
michael@0 184 nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
michael@0 185 if (!callback) {
michael@0 186 return;
michael@0 187 }
michael@0 188
michael@0 189 callback->WindowVolumeChanged();
michael@0 190 }
michael@0 191
michael@0 192 NS_IMETHODIMP
michael@0 193 AudioChannelAgent::GetWindowVolume(float* aVolume)
michael@0 194 {
michael@0 195 NS_ENSURE_ARG_POINTER(aVolume);
michael@0 196
michael@0 197 nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mWindow);
michael@0 198 if (!win) {
michael@0 199 *aVolume = 1.0f;
michael@0 200 return NS_OK;
michael@0 201 }
michael@0 202
michael@0 203 *aVolume = win->GetAudioGlobalVolume();
michael@0 204 return NS_OK;
michael@0 205 }

mercurial