Bug 1261536 - Create a synthetic document after the window is set - r?bz draft
authorGerald Squelart <gsquelart@mozilla.com>
Sat, 02 Apr 2016 10:40:09 +1100
changeset 347114 2371b5eccf59d9cc6278473c5d8a91943326975c
parent 347113 c40c0b2f3b4c778af4307e090b4063b63c806cda
child 517550 945b4a8bbfe41ae7ae0efb7173f6ef4b638ab813
push id14495
push usergsquelart@mozilla.com
push dateFri, 01 Apr 2016 23:40:31 +0000
reviewersbz
bugs1261536
milestone48.0a1
Bug 1261536 - Create a synthetic document after the window is set - r?bz MozReview-Commit-ID: 7SBC2dFZAkP
dom/html/VideoDocument.cpp
--- a/dom/html/VideoDocument.cpp
+++ b/dom/html/VideoDocument.cpp
@@ -28,18 +28,17 @@ public:
                                      nsIContentSink*     aSink = nullptr);
   virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject);
 
 protected:
 
   // Sets document <title> to reflect the file name and description.
   void UpdateTitle(nsIChannel* aChannel);
 
-  nsresult CreateSyntheticVideoDocument(nsIChannel* aChannel,
-                                        nsIStreamListener** aListener);
+  nsresult CreateSyntheticVideoDocument();
 
   RefPtr<MediaDocumentStreamListener> mStreamListener;
 };
 
 nsresult
 VideoDocument::StartDocumentLoad(const char*         aCommand,
                                  nsIChannel*         aChannel,
                                  nsILoadGroup*       aLoadGroup,
@@ -49,47 +48,49 @@ VideoDocument::StartDocumentLoad(const c
                                  nsIContentSink*     aSink)
 {
   nsresult rv =
     MediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer,
                                      aDocListener, aReset, aSink);
   NS_ENSURE_SUCCESS(rv, rv);
 
   mStreamListener = new MediaDocumentStreamListener(this);
-
-  // Create synthetic document
-  rv = CreateSyntheticVideoDocument(aChannel,
-      getter_AddRefs(mStreamListener->mNextStream));
-  NS_ENSURE_SUCCESS(rv, rv);
-
   NS_ADDREF(*aDocListener = mStreamListener);
   return rv;
 }
 
 void
 VideoDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
 {
   // Set the script global object on the superclass before doing
   // anything that might require it....
   MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
 
   if (aScriptGlobalObject) {
+    if (!GetRootElement()) {
+      // Create synthetic document
+#ifdef DEBUG
+      nsresult rv =
+#endif
+        CreateSyntheticVideoDocument();
+      NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic video document");
+    }
+
     if (!nsContentUtils::IsChildOfSameType(this) &&
         GetReadyStateEnum() != nsIDocument::READYSTATE_COMPLETE) {
       LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelVideoDocument.css"));
       LinkStylesheet(NS_LITERAL_STRING("chrome://global/skin/media/TopLevelVideoDocument.css"));
       LinkScript(NS_LITERAL_STRING("chrome://global/content/TopLevelVideoDocument.js"));
     }
     BecomeInteractive();
   }
 }
 
 nsresult
-VideoDocument::CreateSyntheticVideoDocument(nsIChannel* aChannel,
-                                            nsIStreamListener** aListener)
+VideoDocument::CreateSyntheticVideoDocument()
 {
   // make our generic document
   nsresult rv = MediaDocument::CreateSyntheticDocument();
   NS_ENSURE_SUCCESS(rv, rv);
 
   Element* body = GetBodyElement();
   if (!body) {
     NS_WARNING("no body on video document!");
@@ -104,18 +105,19 @@ VideoDocument::CreateSyntheticVideoDocum
 
   RefPtr<HTMLMediaElement> element =
     static_cast<HTMLMediaElement*>(NS_NewHTMLVideoElement(nodeInfo.forget(),
                                                           NOT_FROM_PARSER));
   if (!element)
     return NS_ERROR_OUT_OF_MEMORY;
   element->SetAutoplay(true);
   element->SetControls(true);
-  element->LoadWithChannel(aChannel, aListener);
-  UpdateTitle(aChannel);
+  element->LoadWithChannel(mChannel,
+                           getter_AddRefs(mStreamListener->mNextStream));
+  UpdateTitle(mChannel);
 
   if (nsContentUtils::IsChildOfSameType(this)) {
     // Video documents that aren't toplevel should fill their frames and
     // not have margins
     element->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
         NS_LITERAL_STRING("position:absolute; top:0; left:0; width:100%; height:100%"),
         true);
   }