Bug 798829: Force MediaManager to be created from MainThread r=anant
authorRandell Jesup <rjesup@jesup.org>
Thu, 25 Oct 2012 00:54:38 -0400
changeset 111460 6acf30e09a34b644204b166761d200c589d76631
parent 111459 c107d4e66ee445ee1efbc3cbd074658b823918d6
child 111461 e8a8bf41e0c4e7fbad6261d2c92ec5417cfe2561
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersanant
bugs798829
milestone19.0a1
Bug 798829: Force MediaManager to be created from MainThread r=anant
dom/media/MediaManager.cpp
dom/media/MediaManager.h
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -773,16 +773,23 @@ MediaManager::GetUserMedia(bool aPrivile
           domDoc, aWindow, nullptr, EmptyString(), EmptyString()
         );
         return NS_OK;
       }
     }
   }
 #endif
 
+  static bool created = false;
+  if (!created) {
+    // Force MediaManager to startup before we try to access it from other threads
+    // Hack: should init singleton earlier unless it's expensive (mem or CPU)
+    (void) MediaManager::Get();
+  }
+
   // Store the WindowID in a hash table and mark as active. The entry is removed
   // when this window is closed or navigated away from.
   uint64_t windowID = aWindow->WindowID();
   nsRefPtr<GetUserMediaRunnable> gUMRunnable;
   {
     MutexAutoLock lock(mMutex);
     StreamListeners* listeners = mActiveWindows.Get(windowID);
     if (!listeners) {
--- a/dom/media/MediaManager.h
+++ b/dom/media/MediaManager.h
@@ -270,16 +270,17 @@ private:
 
 class MediaManager MOZ_FINAL : public nsIObserver
 {
 public:
   static MediaManager* Get() {
     if (!sSingleton) {
       sSingleton = new MediaManager();
 
+      NS_ASSERTION(NS_IsMainThread(), "Only create MediaManager on main thread");
       nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
       obs->AddObserver(sSingleton, "xpcom-shutdown", false);
       obs->AddObserver(sSingleton, "getUserMedia:response:allow", false);
       obs->AddObserver(sSingleton, "getUserMedia:response:deny", false);
     }
     return sSingleton;
   }
   static Mutex& GetMutex() {