Bug 633051 - Remove LOAD_BACKGROUND flag from media channel when load completes. r=roc a=roc
authorMatthew Gregan <kinetik@flim.org>
Tue, 15 Feb 2011 12:21:32 +1300
changeset 62568 4f3ad7dc68394b21e1c3643215ee33c9abb1f8f1
parent 62567 b6dc8964dea823ede0cb5d54ff42f211abab4a28
child 62569 9487445572ced105c1875cb32e04503ca6874ba6
push id1
push userroot
push dateTue, 10 Dec 2013 15:46:25 +0000
reviewersroc, roc
bugs633051
milestone2.0b12pre
Bug 633051 - Remove LOAD_BACKGROUND flag from media channel when load completes. r=roc a=roc
content/media/nsMediaStream.cpp
content/media/nsMediaStream.h
--- a/content/media/nsMediaStream.cpp
+++ b/content/media/nsMediaStream.cpp
@@ -314,16 +314,30 @@ nsMediaChannelStream::OnStopRequest(nsIR
     if (NS_SUCCEEDED(rv))
       return rv;
     // If the reopen/reseek fails, just fall through and treat this
     // error as fatal.
   }
 
   if (!mIgnoreClose) {
     mCacheStream.NotifyDataEnded(aStatus);
+
+    // Move this request back into the foreground.  This is necessary for
+    // requests owned by video documents to ensure the load group fires
+    // OnStopRequest when restoring from session history.
+    if (mLoadInBackground) {
+      mLoadInBackground = PR_FALSE;
+
+      nsLoadFlags loadFlags;
+      nsresult rv = mChannel->GetLoadFlags(&loadFlags);
+      NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
+
+      loadFlags &= ~nsIRequest::LOAD_BACKGROUND;
+      ModifyLoadFlags(loadFlags);
+    }
   }
 
   return NS_OK;
 }
 
 nsresult
 nsMediaChannelStream::OnChannelRedirect(nsIChannel* aOld, nsIChannel* aNew,
                                         PRUint32 aFlags)
@@ -1150,34 +1164,46 @@ void nsMediaStream::MoveLoadsToBackgroun
   }
 
   nsresult rv;
   nsHTMLMediaElement* element = mDecoder->GetMediaElement();
   if (!element) {
     NS_WARNING("Null element in nsMediaStream::MoveLoadsToBackground()");
     return;
   }
-  nsCOMPtr<nsILoadGroup> loadGroup;
-  rv = mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
-  NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadGroup() failed!");
-  nsresult status;
-  mChannel->GetStatus(&status);
-  // Note: if (NS_FAILED(status)), the channel won't be in the load group.
+
   PRBool isPending = PR_FALSE;
-  if (loadGroup &&
-      NS_SUCCEEDED(status) &&
-      NS_SUCCEEDED(mChannel->IsPending(&isPending)) &&
+  if (NS_SUCCEEDED(mChannel->IsPending(&isPending)) &&
       isPending) {
-    rv = loadGroup->RemoveRequest(mChannel, nsnull, status);
-    NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveRequest() failed!");
-
     nsLoadFlags loadFlags;
     rv = mChannel->GetLoadFlags(&loadFlags);
     NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadFlags() failed!");
 
     loadFlags |= nsIRequest::LOAD_BACKGROUND;
-    rv = mChannel->SetLoadFlags(loadFlags);
-    NS_ASSERTION(NS_SUCCEEDED(rv), "SetLoadFlags() failed!");
+    ModifyLoadFlags(loadFlags);
+  }
+}
+
+void nsMediaStream::ModifyLoadFlags(nsLoadFlags aFlags)
+{
+  nsCOMPtr<nsILoadGroup> loadGroup;
+  nsresult rv = mChannel->GetLoadGroup(getter_AddRefs(loadGroup));
+  NS_ASSERTION(NS_SUCCEEDED(rv), "GetLoadGroup() failed!");
+
+  nsresult status;
+  mChannel->GetStatus(&status);
 
+  // Note: if (NS_FAILED(status)), the channel won't be in the load group.
+  if (loadGroup &&
+      NS_SUCCEEDED(status)) {
+    rv = loadGroup->RemoveRequest(mChannel, nsnull, status);
+    NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveRequest() failed!");
+  }
+
+  rv = mChannel->SetLoadFlags(aFlags);
+  NS_ASSERTION(NS_SUCCEEDED(rv), "SetLoadFlags() failed!");
+
+  if (loadGroup &&
+      NS_SUCCEEDED(status)) {
     rv = loadGroup->AddRequest(mChannel, nsnull);
     NS_ASSERTION(NS_SUCCEEDED(rv), "AddRequest() failed!");
   }
 }
--- a/content/media/nsMediaStream.h
+++ b/content/media/nsMediaStream.h
@@ -280,16 +280,21 @@ protected:
     mDecoder(aDecoder),
     mChannel(aChannel),
     mURI(aURI),
     mLoadInBackground(PR_FALSE)
   {
     MOZ_COUNT_CTOR(nsMediaStream);
   }
 
+  // Set the request's load flags to aFlags.  If the request is part of a
+  // load group, the request is removed from the group, the flags are set, and
+  // then the request is added back to the load group.
+  void ModifyLoadFlags(nsLoadFlags aFlags);
+
   // This is not an nsCOMPointer to prevent a circular reference
   // between the decoder to the media stream object. The stream never
   // outlives the lifetime of the decoder.
   nsMediaDecoder* mDecoder;
 
   // Channel used to download the media data. Must be accessed
   // from the main thread only.
   nsCOMPtr<nsIChannel> mChannel;