Bug 1395842 - tighten up assertions in ChannelSuspendAgent which runs on the main thread only. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 01 Sep 2017 15:44:38 +0800
changeset 659457 88d13ff71c8aabb43674f744586a5114f9bf5cb4
parent 659455 6bfbd3f59c7c7e9f7b7bf098b40aa785a5046097
child 730000 c5e17a3bf11e4c6015edf324cb2c4387d29353d6
push id78140
push userjwwang@mozilla.com
push dateWed, 06 Sep 2017 03:20:30 +0000
bugs1395842
milestone57.0a1
Bug 1395842 - tighten up assertions in ChannelSuspendAgent which runs on the main thread only. MozReview-Commit-ID: AK3YHAz750D
dom/media/MediaResource.cpp
dom/media/MediaResource.h
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -1000,80 +1000,87 @@ ChannelMediaResource::GetLength()
   return mCacheStream.GetLength();
 }
 
 // ChannelSuspendAgent
 
 bool
 ChannelSuspendAgent::Suspend()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   SuspendInternal();
   return (++mSuspendCount == 1);
 }
 
 void
 ChannelSuspendAgent::SuspendInternal()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   if (mChannel) {
     bool isPending = false;
     nsresult rv = mChannel->IsPending(&isPending);
     if (NS_SUCCEEDED(rv) && isPending && !mIsChannelSuspended) {
       mChannel->Suspend();
       mIsChannelSuspended = true;
     }
   }
 }
 
 bool
 ChannelSuspendAgent::Resume()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(IsSuspended(), "Resume without suspend!");
   --mSuspendCount;
 
   if (mSuspendCount == 0) {
     if (mChannel && mIsChannelSuspended) {
       mChannel->Resume();
       mIsChannelSuspended = false;
     }
     return true;
   }
   return false;
 }
 
 void
 ChannelSuspendAgent::UpdateSuspendedStatusIfNeeded()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   if (!mIsChannelSuspended && IsSuspended()) {
     SuspendInternal();
   }
 }
 
 void
 ChannelSuspendAgent::NotifyChannelOpened(nsIChannel* aChannel)
 {
+  MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(aChannel);
   mChannel = aChannel;
 }
 
 void
 ChannelSuspendAgent::NotifyChannelClosing()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mChannel);
   // Before close the channel, it need to be resumed to make sure its internal
   // state is correct. Besides, We need to suspend the channel after recreating.
   if (mIsChannelSuspended) {
     mChannel->Resume();
     mIsChannelSuspended = false;
   }
   mChannel = nullptr;
 }
 
 bool
 ChannelSuspendAgent::IsSuspended()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   return (mSuspendCount > 0);
 }
 
 // FileMediaResource
 
 class FileMediaResource : public BaseMediaResource
 {
 public:
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -367,17 +367,16 @@ protected:
 /**
  * This class is responsible for managing the suspend count and report suspend
  * status of channel.
  **/
 class ChannelSuspendAgent {
 public:
   explicit ChannelSuspendAgent(nsIChannel* aChannel)
   : mChannel(aChannel),
-    mSuspendCount(0),
     mIsChannelSuspended(false)
   {}
 
   // True when the channel has been suspended or needs to be suspended.
   bool IsSuspended();
 
   // Return true when the channel is logically suspended, i.e. the suspend
   // count goes from 0 to 1.
@@ -395,17 +394,17 @@ public:
 
   // Check whether we need to suspend the channel.
   void UpdateSuspendedStatusIfNeeded();
 private:
   // Only suspends channel but not changes the suspend count.
   void SuspendInternal();
 
   nsIChannel* mChannel;
-  Atomic<uint32_t> mSuspendCount;
+  uint32_t mSuspendCount = 0;
   bool mIsChannelSuspended;
 };
 
 /**
  * This is the MediaResource implementation that wraps Necko channels.
  * Much of its functionality is actually delegated to MediaCache via
  * an underlying MediaCacheStream.
  *