Bug 676828 - Use RAII for AudioSession instead. r=bholley
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 22 Jan 2016 16:08:14 +0900
changeset 324320 a32c1b49c2edb30f7fd456c3155b3cf6a4ea376d
parent 324262 cb5a01dc56fb199aed6ff627d17ced5bc25342ce
child 324321 5ee4a20245a0b4724e3d2120ef146a42dc10e6c5
push id9883
push useratolfsen@mozilla.com
push dateFri, 22 Jan 2016 13:24:29 +0000
reviewersbholley
bugs676828
milestone46.0a1
Bug 676828 - Use RAII for AudioSession instead. r=bholley
js/xpconnect/src/XPCShellImpl.cpp
--- a/js/xpconnect/src/XPCShellImpl.cpp
+++ b/js/xpconnect/src/XPCShellImpl.cpp
@@ -93,16 +93,30 @@ public:
 private:
     nsCOMPtr<nsIFile> mGREDir;
     nsCOMPtr<nsIFile> mGREBinDir;
     nsCOMPtr<nsIFile> mAppDir;
     nsCOMPtr<nsIFile> mPluginDir;
     nsCOMPtr<nsIFile> mAppFile;
 };
 
+#ifdef XP_WIN
+class MOZ_STACK_CLASS AutoAudioSession
+{
+public:
+    AutoAudioSession() {
+        widget::StartAudioSession();
+    }
+
+    ~AutoAudioSession() {
+        widget::StopAudioSession();
+    }
+};
+#endif
+
 static const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
 
 #define EXITCODE_RUNTIME_ERROR 3
 #define EXITCODE_FILE_NOT_FOUND 4
 
 static FILE* gOutFile = nullptr;
 static FILE* gErrFile = nullptr;
 static FILE* gInFile = nullptr;
@@ -1496,17 +1510,17 @@ XRE_XPCShellMain(int argc, char** argv, 
 
         // Initialize graphics prefs on the main thread, if not already done
         gfxPrefs::GetSingleton();
         // Initialize e10s check on the main thread, if not already done
         BrowserTabsRemoteAutostart();
 #ifdef XP_WIN
         // Plugin may require audio session if installed plugin can initialize
         // asynchronized.
-        widget::StartAudioSession();
+        AutoAudioSession audioSession;
 #endif
 
         {
             JS::Rooted<JSObject*> glob(cx, holder->GetJSObject());
             if (!glob) {
                 return 1;
             }
 
@@ -1566,19 +1580,16 @@ XRE_XPCShellMain(int argc, char** argv, 
             }
 
             JS_DropPrincipals(rt, gJSPrincipals);
             JS_SetAllNonReservedSlotsToUndefined(cx, glob);
             JS_SetAllNonReservedSlotsToUndefined(cx, JS_GlobalLexicalScope(glob));
             JS_GC(rt);
         }
         JS_GC(rt);
-#ifdef XP_WIN
-        widget::StopAudioSession();
-#endif
     } // this scopes the nsCOMPtrs
 
     if (!XRE_ShutdownTestShell())
         NS_ERROR("problem shutting down testshell");
 
     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
     rv = NS_ShutdownXPCOM( nullptr );
     MOZ_ASSERT(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");