Bug 1793629 - Implement attention indicator for the unified extensions button, r...
[gecko.git] / widget / windows / nsSound.cpp
blobc1871747053aecabcb82804e5b3a2178e617437a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nscore.h"
8 #include "plstr.h"
9 #include <stdio.h>
10 #include "nsString.h"
11 #include <windows.h>
13 // mmsystem.h is needed to build with WIN32_LEAN_AND_MEAN
14 #include <mmsystem.h>
16 #include "HeadlessSound.h"
17 #include "nsSound.h"
18 #include "nsIURL.h"
19 #include "nsNetUtil.h"
20 #include "nsIChannel.h"
21 #include "nsContentUtils.h"
22 #include "nsCRT.h"
23 #include "nsIObserverService.h"
25 #include "mozilla/Logging.h"
26 #include "prtime.h"
28 #include "nsNativeCharsetUtils.h"
29 #include "nsThreadUtils.h"
30 #include "mozilla/ClearOnShutdown.h"
31 #include "gfxPlatform.h"
33 using mozilla::LogLevel;
35 static mozilla::LazyLogModule gWin32SoundLog("nsSound");
37 // Hackaround for bug 1644240
38 // When we call PlaySound for the first time in the process, winmm.dll creates
39 // a new thread and starts a message loop in winmm!mciwindow. After that,
40 // every call of PlaySound communicates with that thread via Window messages.
41 // It seems that Warsaw application hooks USER32!GetMessageA, and there is
42 // a timing window where they free their trampoline region without reverting
43 // the hook on USER32!GetMessageA, resulting in crash when winmm!mciwindow
44 // receives a message because it tries to jump to a freed buffer.
45 // Based on the crash reports, it happened on all versions of Windows x64, and
46 // the possible condition was wslbdhm64.dll was loaded but wslbscrwh64.dll was
47 // unloaded. Therefore we suppress playing a sound under such a condition.
48 static bool ShouldSuppressPlaySound() {
49 #if defined(_M_AMD64)
50 if (::GetModuleHandle(L"wslbdhm64.dll") &&
51 !::GetModuleHandle(L"wslbscrwh64.dll")) {
52 return true;
54 #endif // defined(_M_AMD64)
55 return false;
58 class nsSoundPlayer : public mozilla::Runnable {
59 public:
60 explicit nsSoundPlayer(const nsAString& aSoundName)
61 : mozilla::Runnable("nsSoundPlayer"),
62 mSoundName(aSoundName),
63 mSoundData(nullptr) {}
65 nsSoundPlayer(const uint8_t* aData, size_t aSize)
66 : mozilla::Runnable("nsSoundPlayer"), mSoundName(u""_ns) {
67 MOZ_ASSERT(aSize > 0, "Size should not be zero");
68 MOZ_ASSERT(aData, "Data shoud not be null");
70 // We will disptach nsSoundPlayer to playerthread, so keep a data copy
71 mSoundData = new uint8_t[aSize];
72 memcpy(mSoundData, aData, aSize);
75 NS_DECL_NSIRUNNABLE
77 protected:
78 ~nsSoundPlayer();
80 nsString mSoundName;
81 uint8_t* mSoundData;
84 NS_IMETHODIMP
85 nsSoundPlayer::Run() {
86 if (ShouldSuppressPlaySound()) {
87 return NS_OK;
90 MOZ_ASSERT(!mSoundName.IsEmpty() || mSoundData,
91 "Sound name or sound data should be specified");
92 DWORD flags = SND_NODEFAULT | SND_ASYNC;
94 if (mSoundData) {
95 flags |= SND_MEMORY;
96 ::PlaySoundW(reinterpret_cast<LPCWSTR>(mSoundData), nullptr, flags);
97 } else {
98 flags |= SND_ALIAS;
99 ::PlaySoundW(mSoundName.get(), nullptr, flags);
101 return NS_OK;
104 nsSoundPlayer::~nsSoundPlayer() { delete[] mSoundData; }
106 mozilla::StaticRefPtr<nsISound> nsSound::sInstance;
108 /* static */
109 already_AddRefed<nsISound> nsSound::GetInstance() {
110 if (!sInstance) {
111 if (gfxPlatform::IsHeadless()) {
112 sInstance = new mozilla::widget::HeadlessSound();
113 } else {
114 RefPtr<nsSound> sound = new nsSound();
115 nsresult rv = sound->CreatePlayerThread();
116 if (NS_WARN_IF(NS_FAILED(rv))) {
117 return nullptr;
119 sInstance = sound.forget();
121 ClearOnShutdown(&sInstance);
124 RefPtr<nsISound> service = sInstance;
125 return service.forget();
128 #ifndef SND_PURGE
129 // Not available on Windows CE, and according to MSDN
130 // doesn't do anything on recent windows either.
131 # define SND_PURGE 0
132 #endif
134 NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver, nsIObserver)
136 nsSound::nsSound() : mInited(false) {}
138 nsSound::~nsSound() {}
140 void nsSound::PurgeLastSound() {
141 // Halt any currently playing sound.
142 if (mSoundPlayer) {
143 if (mPlayerThread) {
144 mPlayerThread->Dispatch(
145 NS_NewRunnableFunction("nsSound::PurgeLastSound",
146 [player = std::move(mSoundPlayer)]() {
147 // Capture move mSoundPlayer to lambda then
148 // PlaySoundW(nullptr, nullptr, SND_PURGE)
149 // will be called before freeing the
150 // nsSoundPlayer.
151 if (ShouldSuppressPlaySound()) {
152 return;
154 ::PlaySoundW(nullptr, nullptr, SND_PURGE);
156 NS_DISPATCH_NORMAL);
161 NS_IMETHODIMP nsSound::Beep() {
162 ::MessageBeep(0);
164 return NS_OK;
167 NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader* aLoader,
168 nsISupports* context, nsresult aStatus,
169 uint32_t dataLen, const uint8_t* data) {
170 MOZ_ASSERT(mPlayerThread, "player thread should not be null ");
171 // print a load error on bad status
172 if (NS_FAILED(aStatus)) {
173 #ifdef DEBUG
174 if (aLoader) {
175 nsCOMPtr<nsIRequest> request;
176 nsCOMPtr<nsIChannel> channel;
177 aLoader->GetRequest(getter_AddRefs(request));
178 if (request) channel = do_QueryInterface(request);
179 if (channel) {
180 nsCOMPtr<nsIURI> uri;
181 channel->GetURI(getter_AddRefs(uri));
182 if (uri) {
183 nsAutoCString uriSpec;
184 uri->GetSpec(uriSpec);
185 MOZ_LOG(gWin32SoundLog, LogLevel::Info,
186 ("Failed to load %s\n", uriSpec.get()));
190 #endif
191 return aStatus;
194 PurgeLastSound();
196 if (data && dataLen > 0) {
197 MOZ_ASSERT(!mSoundPlayer, "mSoundPlayer should be null");
198 mSoundPlayer = new nsSoundPlayer(data, dataLen);
199 MOZ_ASSERT(mSoundPlayer, "Could not create player");
201 nsresult rv = mPlayerThread->Dispatch(mSoundPlayer, NS_DISPATCH_NORMAL);
202 if (NS_WARN_IF(FAILED(rv))) {
203 return rv;
207 return NS_OK;
210 NS_IMETHODIMP nsSound::Play(nsIURL* aURL) {
211 nsresult rv;
213 #ifdef DEBUG_SOUND
214 char* url;
215 aURL->GetSpec(&url);
216 MOZ_LOG(gWin32SoundLog, LogLevel::Info, ("%s\n", url));
217 #endif
219 nsCOMPtr<nsIStreamLoader> loader;
220 rv = NS_NewStreamLoader(
221 getter_AddRefs(loader), aURL,
222 this, // aObserver
223 nsContentUtils::GetSystemPrincipal(),
224 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
225 nsIContentPolicy::TYPE_OTHER);
226 return rv;
229 nsresult nsSound::CreatePlayerThread() {
230 if (mPlayerThread) {
231 return NS_OK;
233 if (NS_WARN_IF(NS_FAILED(NS_NewNamedThread("PlayEventSound",
234 getter_AddRefs(mPlayerThread))))) {
235 return NS_ERROR_FAILURE;
238 // Add an observer for shutdown event to release the thread at that time
239 nsCOMPtr<nsIObserverService> observerService =
240 mozilla::services::GetObserverService();
241 if (!observerService) {
242 return NS_ERROR_FAILURE;
245 observerService->AddObserver(this, "xpcom-shutdown-threads", false);
246 return NS_OK;
249 NS_IMETHODIMP
250 nsSound::Observe(nsISupports* aSubject, const char* aTopic,
251 const char16_t* aData) {
252 if (!strcmp(aTopic, "xpcom-shutdown-threads")) {
253 PurgeLastSound();
255 if (mPlayerThread) {
256 mPlayerThread->Shutdown();
257 mPlayerThread = nullptr;
261 return NS_OK;
264 NS_IMETHODIMP nsSound::Init() {
265 if (mInited) {
266 return NS_OK;
269 MOZ_ASSERT(mPlayerThread, "player thread should not be null ");
270 // This call halts a sound if it was still playing.
271 // We have to use the sound library for something to make sure
272 // it is initialized.
273 // If we wait until the first sound is played, there will
274 // be a time lag as the library gets loaded.
275 // This should be done in player thread otherwise it will block main thread
276 // at the first time loading sound library.
277 mPlayerThread->Dispatch(
278 NS_NewRunnableFunction("nsSound::Init",
279 []() {
280 if (ShouldSuppressPlaySound()) {
281 return;
283 ::PlaySoundW(nullptr, nullptr, SND_PURGE);
285 NS_DISPATCH_NORMAL);
287 mInited = true;
289 return NS_OK;
292 NS_IMETHODIMP nsSound::PlayEventSound(uint32_t aEventId) {
293 MOZ_ASSERT(mPlayerThread, "player thread should not be null ");
294 PurgeLastSound();
296 const wchar_t* sound = nullptr;
297 switch (aEventId) {
298 case EVENT_NEW_MAIL_RECEIVED:
299 sound = L"MailBeep";
300 break;
301 case EVENT_ALERT_DIALOG_OPEN:
302 sound = L"SystemExclamation";
303 break;
304 case EVENT_CONFIRM_DIALOG_OPEN:
305 sound = L"SystemQuestion";
306 break;
307 case EVENT_MENU_EXECUTE:
308 sound = L"MenuCommand";
309 break;
310 case EVENT_MENU_POPUP:
311 sound = L"MenuPopup";
312 break;
313 case EVENT_EDITOR_MAX_LEN:
314 sound = L".Default";
315 break;
316 default:
317 // Win32 plays no sounds at NS_SYSSOUND_PROMPT_DIALOG and
318 // NS_SYSSOUND_SELECT_DIALOG.
319 return NS_OK;
321 NS_ASSERTION(sound, "sound is null");
322 MOZ_ASSERT(!mSoundPlayer, "mSoundPlayer should be null");
323 mSoundPlayer = new nsSoundPlayer(nsDependentString(sound));
324 MOZ_ASSERT(mSoundPlayer, "Could not create player");
325 nsresult rv = mPlayerThread->Dispatch(mSoundPlayer, NS_DISPATCH_NORMAL);
326 if (NS_WARN_IF(NS_FAILED(rv))) {
327 return rv;
329 return NS_OK;