Bug 1366930 - Correct error handling in mozilla::MediaShutdownManager::InitStatics. draft
authorJW Wang <jwwang@mozilla.com>
Tue, 23 May 2017 11:02:56 +0800
changeset 582831 bd9b24a2c7409f4c3de6705740bc88e75ec4d41d
parent 582830 59509c690df8b505c8010ab466b69ea39a3800d3
child 629870 c8d2f63d106da4b812dde1f9d0335b9bd07fbe4e
push id60193
push userjwwang@mozilla.com
push dateTue, 23 May 2017 06:17:52 +0000
bugs1366930
milestone55.0a1
Bug 1366930 - Correct error handling in mozilla::MediaShutdownManager::InitStatics. Resetting sInstance in MediaShutdownManager::InitStatics() will cause MediaShutdownManager::Instance().Register() to crash immediately without a chance to handle the error in Register(). Instead, we keep an error code to know whether to bail out in Register(). MozReview-Commit-ID: EGqL1407Gh
dom/media/MediaShutdownManager.cpp
dom/media/MediaShutdownManager.h
--- a/dom/media/MediaShutdownManager.cpp
+++ b/dom/media/MediaShutdownManager.cpp
@@ -73,17 +73,17 @@ MediaShutdownManager::InitStatics()
   sInitDone = true;
   sInstance = new MediaShutdownManager();
 
   nsresult rv = GetShutdownBarrier()->AddBlocker(
     sInstance, NS_LITERAL_STRING(__FILE__), __LINE__,
     NS_LITERAL_STRING("MediaShutdownManager shutdown"));
   if (NS_FAILED(rv)) {
     LOGW("Failed to add shutdown blocker! rv=%x", uint32_t(rv));
-    sInstance = nullptr;
+    sInstance->mError = rv;
   }
 }
 
 void
 MediaShutdownManager::RemoveBlocker()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mIsDoingXPCOMShutDown);
@@ -94,17 +94,17 @@ MediaShutdownManager::RemoveBlocker()
   sInstance = nullptr;
   DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::BlockShutdown() end."));
 }
 
 nsresult
 MediaShutdownManager::Register(MediaDecoder* aDecoder)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (!sInstance) {
+  if (NS_FAILED(mError)) {
     return NS_ERROR_NOT_INITIALIZED;
   }
   if (mIsDoingXPCOMShutDown) {
     return NS_ERROR_ABORT;
   }
   // Don't call Register() after you've Unregistered() all the decoders,
   // that's not going to work.
   MOZ_ASSERT(!mDecoders.Contains(aDecoder));
--- a/dom/media/MediaShutdownManager.h
+++ b/dom/media/MediaShutdownManager.h
@@ -78,13 +78,14 @@ private:
   static StaticRefPtr<MediaShutdownManager> sInstance;
 
   // References to the MediaDecoder. The decoders unregister themselves
   // in their Shutdown() method, so we'll drop the reference naturally when
   // we're shutting down (in the non xpcom-shutdown case).
   nsTHashtable<nsRefPtrHashKey<MediaDecoder>> mDecoders;
 
   bool mIsDoingXPCOMShutDown = false;
+  nsresult mError = NS_OK;
 };
 
 } // namespace mozilla
 
 #endif