Bug 1401460 - update principals in OnStartRequest(). draft
authorJW Wang <jwwang@mozilla.com>
Wed, 20 Sep 2017 14:23:59 +0800
changeset 667520 714caa836faafa12b6d6919f70e53eb9b25f05aa
parent 667465 d96338fc228731f08b80d3628910262175a1d535
child 667572 eeb067bc2905dfa6fb5764fd42ebb78e1b71a61d
push id80745
push userjwwang@mozilla.com
push dateWed, 20 Sep 2017 07:55:09 +0000
bugs1401460
milestone57.0a1
Bug 1401460 - update principals in OnStartRequest(). See comment 0 for the rationale. The principal will not change after OnStartRequest(). MozReview-Commit-ID: K2HyWBBzGmC
dom/media/ChannelMediaResource.cpp
--- a/dom/media/ChannelMediaResource.cpp
+++ b/dom/media/ChannelMediaResource.cpp
@@ -259,16 +259,21 @@ ChannelMediaResource::OnStartRequest(nsI
     // and the server isn't sending Accept-Ranges:bytes then we don't
     // support seeking. We also can't seek in compressed streams.
     seekable = !isCompressed && acceptsRanges;
   } else {
     // Not an HTTP channel. Assume data will be sent from position zero.
     startOffset = 0;
   }
 
+  // Update principals before OnDataAvailable() putting the data in the cache.
+  // This is important, we want to make sure all principals are updated before
+  // any consumer can see the new data.
+  UpdatePrincipal();
+
   mCacheStream.NotifyDataStarted(mLoadID, startOffset);
   mCacheStream.SetTransportSeekable(seekable);
   mChannelStatistics.Start();
   mReopenOnError = false;
 
   mSuspendAgent.UpdateSuspendedStatusIfNeeded();
 
   // Fires an initial progress event.
@@ -408,22 +413,16 @@ nsresult
 ChannelMediaResource::OnDataAvailable(uint32_t aLoadID,
                                       nsIInputStream* aStream,
                                       uint32_t aCount)
 {
   // This might happen off the main thread.
   // Don't assert |mChannel.get() == aRequest| since reading mChannel here off
   // the main thread is a data race.
 
-  // Update principals before putting the data in the cache. This is important,
-  // we want to make sure all principals are updated before any consumer can see
-  // the new data.
-  // TODO: Handle the case where OnDataAvailable() runs off the main thread.
-  UpdatePrincipal();
-
   RefPtr<ChannelMediaResource> self = this;
   nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
     "ChannelMediaResource::OnDataAvailable",
     [self, aCount]() { self->mChannelStatistics.AddBytes(aCount); });
   mCallback->AbstractMainThread()->Dispatch(r.forget());
 
   Closure closure{ aLoadID, this };
   uint32_t count = aCount;
@@ -779,19 +778,20 @@ ChannelMediaResource::CacheClientNotifyP
 
   mCallback->NotifyPrincipalChanged();
 }
 
 void
 ChannelMediaResource::UpdatePrincipal()
 {
   MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mChannel);
   nsCOMPtr<nsIPrincipal> principal;
   nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
-  if (secMan && mChannel) {
+  if (secMan) {
     secMan->GetChannelResultPrincipal(mChannel, getter_AddRefs(principal));
     mCacheStream.UpdatePrincipal(principal);
   }
 }
 
 void
 ChannelMediaResource::CacheClientNotifySuspendedStatusChanged()
 {