1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #include "nscore.h"
8 #include "plstr.h"
9 #include <stdio.h>
10 #include "nsString.h"
11 #include <windows.h>
12 
13 // mmsystem.h is needed to build with WIN32_LEAN_AND_MEAN
14 #include <mmsystem.h>
15 
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"
24 
25 #include "mozilla/Logging.h"
26 #include "prtime.h"
27 
28 #include "nsNativeCharsetUtils.h"
29 #include "nsThreadUtils.h"
30 #include "mozilla/ClearOnShutdown.h"
31 #include "gfxPlatform.h"
32 
33 using mozilla::LogLevel;
34 
35 static mozilla::LazyLogModule gWin32SoundLog("nsSound");
36 
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.
ShouldSuppressPlaySound()48 static bool ShouldSuppressPlaySound() {
49 #if defined(_M_AMD64)
50   if (::GetModuleHandle(L"wslbdhm64.dll") &&
51       !::GetModuleHandle(L"wslbscrwh64.dll")) {
52     return true;
53   }
54 #endif  // defined(_M_AMD64)
55   return false;
56 }
57 
58 class nsSoundPlayer : public mozilla::Runnable {
59  public:
nsSoundPlayer(const nsAString & aSoundName)60   explicit nsSoundPlayer(const nsAString& aSoundName)
61       : mozilla::Runnable("nsSoundPlayer"),
62         mSoundName(aSoundName),
63         mSoundData(nullptr) {}
64 
nsSoundPlayer(const uint8_t * aData,size_t aSize)65   nsSoundPlayer(const uint8_t* aData, size_t aSize)
66       : mozilla::Runnable("nsSoundPlayer"), mSoundName(EmptyString()) {
67     MOZ_ASSERT(aSize > 0, "Size should not be zero");
68     MOZ_ASSERT(aData, "Data shoud not be null");
69 
70     // We will disptach nsSoundPlayer to playerthread, so keep a data copy
71     mSoundData = new uint8_t[aSize];
72     memcpy(mSoundData, aData, aSize);
73   }
74 
75   NS_DECL_NSIRUNNABLE
76 
77  protected:
78   ~nsSoundPlayer();
79 
80   nsString mSoundName;
81   uint8_t* mSoundData;
82 };
83 
84 NS_IMETHODIMP
Run()85 nsSoundPlayer::Run() {
86   if (ShouldSuppressPlaySound()) {
87     return NS_OK;
88   }
89 
90   MOZ_ASSERT(!mSoundName.IsEmpty() || mSoundData,
91              "Sound name or sound data should be specified");
92   DWORD flags = SND_NODEFAULT | SND_ASYNC;
93 
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);
100   }
101   return NS_OK;
102 }
103 
~nsSoundPlayer()104 nsSoundPlayer::~nsSoundPlayer() { delete[] mSoundData; }
105 
106 mozilla::StaticRefPtr<nsISound> nsSound::sInstance;
107 
108 /* static */
GetInstance()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;
118       }
119       sInstance = sound.forget();
120     }
121     ClearOnShutdown(&sInstance);
122   }
123 
124   RefPtr<nsISound> service = sInstance;
125   return service.forget();
126 }
127 
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
133 
NS_IMPL_ISUPPORTS(nsSound,nsISound,nsIStreamLoaderObserver,nsIObserver)134 NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver, nsIObserver)
135 
136 nsSound::nsSound() : mInited(false) {}
137 
~nsSound()138 nsSound::~nsSound() {}
139 
PurgeLastSound()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;
153                                    }
154                                    ::PlaySoundW(nullptr, nullptr, SND_PURGE);
155                                  }),
156           NS_DISPATCH_NORMAL);
157     }
158   }
159 }
160 
Beep()161 NS_IMETHODIMP nsSound::Beep() {
162   ::MessageBeep(0);
163 
164   return NS_OK;
165 }
166 
OnStreamComplete(nsIStreamLoader * aLoader,nsISupports * context,nsresult aStatus,uint32_t dataLen,const uint8_t * data)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()));
187         }
188       }
189     }
190 #endif
191     return aStatus;
192   }
193 
194   PurgeLastSound();
195 
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");
200 
201     nsresult rv = mPlayerThread->Dispatch(mSoundPlayer, NS_DISPATCH_NORMAL);
202     if (NS_WARN_IF(FAILED(rv))) {
203       return rv;
204     }
205   }
206 
207   return NS_OK;
208 }
209 
Play(nsIURL * aURL)210 NS_IMETHODIMP nsSound::Play(nsIURL* aURL) {
211   nsresult rv;
212 
213 #ifdef DEBUG_SOUND
214   char* url;
215   aURL->GetSpec(&url);
216   MOZ_LOG(gWin32SoundLog, LogLevel::Info, ("%s\n", url));
217 #endif
218 
219   nsCOMPtr<nsIStreamLoader> loader;
220   rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL,
221                           this,  // aObserver
222                           nsContentUtils::GetSystemPrincipal(),
223                           nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
224                           nsIContentPolicy::TYPE_OTHER);
225   return rv;
226 }
227 
CreatePlayerThread()228 nsresult nsSound::CreatePlayerThread() {
229   if (mPlayerThread) {
230     return NS_OK;
231   }
232   if (NS_WARN_IF(NS_FAILED(NS_NewNamedThread("PlayEventSound",
233                                              getter_AddRefs(mPlayerThread))))) {
234     return NS_ERROR_FAILURE;
235   }
236 
237   // Add an observer for shutdown event to release the thread at that time
238   nsCOMPtr<nsIObserverService> observerService =
239       mozilla::services::GetObserverService();
240   if (!observerService) {
241     return NS_ERROR_FAILURE;
242   }
243 
244   observerService->AddObserver(this, "xpcom-shutdown-threads", false);
245   return NS_OK;
246 }
247 
248 NS_IMETHODIMP
Observe(nsISupports * aSubject,const char * aTopic,const char16_t * aData)249 nsSound::Observe(nsISupports* aSubject, const char* aTopic,
250                  const char16_t* aData) {
251   if (!strcmp(aTopic, "xpcom-shutdown-threads")) {
252     PurgeLastSound();
253 
254     if (mPlayerThread) {
255       mPlayerThread->Shutdown();
256       mPlayerThread = nullptr;
257     }
258   }
259 
260   return NS_OK;
261 }
262 
Init()263 NS_IMETHODIMP nsSound::Init() {
264   if (mInited) {
265     return NS_OK;
266   }
267 
268   MOZ_ASSERT(mPlayerThread, "player thread should not be null ");
269   // This call halts a sound if it was still playing.
270   // We have to use the sound library for something to make sure
271   // it is initialized.
272   // If we wait until the first sound is played, there will
273   // be a time lag as the library gets loaded.
274   // This should be done in player thread otherwise it will block main thread
275   // at the first time loading sound library.
276   mPlayerThread->Dispatch(
277       NS_NewRunnableFunction("nsSound::Init",
278                              []() {
279                                if (ShouldSuppressPlaySound()) {
280                                  return;
281                                }
282                                ::PlaySoundW(nullptr, nullptr, SND_PURGE);
283                              }),
284       NS_DISPATCH_NORMAL);
285 
286   mInited = true;
287 
288   return NS_OK;
289 }
290 
PlayEventSound(uint32_t aEventId)291 NS_IMETHODIMP nsSound::PlayEventSound(uint32_t aEventId) {
292   MOZ_ASSERT(mPlayerThread, "player thread should not be null ");
293   PurgeLastSound();
294 
295   const wchar_t* sound = nullptr;
296   switch (aEventId) {
297     case EVENT_NEW_MAIL_RECEIVED:
298       sound = L"MailBeep";
299       break;
300     case EVENT_ALERT_DIALOG_OPEN:
301       sound = L"SystemExclamation";
302       break;
303     case EVENT_CONFIRM_DIALOG_OPEN:
304       sound = L"SystemQuestion";
305       break;
306     case EVENT_MENU_EXECUTE:
307       sound = L"MenuCommand";
308       break;
309     case EVENT_MENU_POPUP:
310       sound = L"MenuPopup";
311       break;
312     case EVENT_EDITOR_MAX_LEN:
313       sound = L".Default";
314       break;
315     default:
316       // Win32 plays no sounds at NS_SYSSOUND_PROMPT_DIALOG and
317       // NS_SYSSOUND_SELECT_DIALOG.
318       return NS_OK;
319   }
320   NS_ASSERTION(sound, "sound is null");
321   MOZ_ASSERT(!mSoundPlayer, "mSoundPlayer should be null");
322   mSoundPlayer = new nsSoundPlayer(nsDependentString(sound));
323   MOZ_ASSERT(mSoundPlayer, "Could not create player");
324   nsresult rv = mPlayerThread->Dispatch(mSoundPlayer, NS_DISPATCH_NORMAL);
325   if (NS_WARN_IF(NS_FAILED(rv))) {
326     return rv;
327   }
328   return NS_OK;
329 }
330