Bug 1461726 - Move synthetic doc creation to StartDocumentLoad, add null checks draft
authorKyle Machulis <kyle@nonpolynomial.com>
Thu, 17 May 2018 18:22:02 -0700
changeset 796635 98d4c777ac7a3c95ca32adee47b59a7a0f601278
parent 796393 24bae072acb09114c367e6b9ffde9261b2ad8a58
push id110326
push userbmo:kyle@nonpolynomial.com
push dateFri, 18 May 2018 01:25:27 +0000
bugs1461726
milestone62.0a1
Bug 1461726 - Move synthetic doc creation to StartDocumentLoad, add null checks Move synthetic document creation for ImageDocument to StartDocumentLoad, so we can skip the element check, making it similar to how VideoDocument works. Also add null element checks to the stylesheet and script linking functions. MozReview-Commit-ID: Ek7YZ9TJVRM
dom/html/ImageDocument.cpp
dom/html/MediaDocument.cpp
--- a/dom/html/ImageDocument.cpp
+++ b/dom/html/ImageDocument.cpp
@@ -223,16 +223,22 @@ ImageDocument::StartDocumentLoad(const c
 #if defined(MOZ_WIDGET_ANDROID)
   mOriginalResolution = GetResolution();
 #endif
 
   NS_ASSERTION(aDocListener, "null aDocListener");
   *aDocListener = new ImageListener(this);
   NS_ADDREF(*aDocListener);
 
+#ifdef DEBUG
+    rv =
+#endif
+      CreateSyntheticDocument();
+  NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
+
   return NS_OK;
 }
 
 void
 ImageDocument::Destroy()
 {
   if (mImageContent) {
     // Remove our event listener from the image content.
@@ -262,34 +268,31 @@ ImageDocument::SetScriptGlobalObject(nsI
   nsCOMPtr<EventTarget> target;
   if (mScriptGlobalObject &&
       aScriptGlobalObject != mScriptGlobalObject) {
     target = do_QueryInterface(mScriptGlobalObject);
     target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, false);
     target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this,
                                 false);
   }
+  if (mImageContent) {
+    target = do_QueryInterface(mImageContent);
+    target->RemoveEventListener(NS_LITERAL_STRING("load"), this, false);
+    target->RemoveEventListener(NS_LITERAL_STRING("click"), this, false);
+
+  }
 
   // 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
-        CreateSyntheticDocument();
-      NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
-
-      target = do_QueryInterface(mImageContent);
-      target->AddEventListener(NS_LITERAL_STRING("load"), this, false);
-      target->AddEventListener(NS_LITERAL_STRING("click"), this, false);
-    }
+    target = do_QueryInterface(mImageContent);
+    target->AddEventListener(NS_LITERAL_STRING("load"), this, false);
+    target->AddEventListener(NS_LITERAL_STRING("click"), this, false);
 
     target = do_QueryInterface(aScriptGlobalObject);
     target->AddEventListener(NS_LITERAL_STRING("resize"), this, false);
     target->AddEventListener(NS_LITERAL_STRING("keypress"), this, false);
 
     if (GetReadyStateEnum() != nsIDocument::READYSTATE_COMPLETE) {
       LinkStylesheet(NS_LITERAL_STRING("resource://content-accessible/ImageDocument.css"));
       if (!nsContentUtils::IsChildOfSameType(this)) {
--- a/dom/html/MediaDocument.cpp
+++ b/dom/html/MediaDocument.cpp
@@ -325,16 +325,19 @@ MediaDocument::LinkStylesheet(const nsAS
   NS_ENSURE_TRUE(link, NS_ERROR_OUT_OF_MEMORY);
 
   link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel,
                 NS_LITERAL_STRING("stylesheet"), true);
 
   link->SetAttr(kNameSpaceID_None, nsGkAtoms::href, aStylesheet, true);
 
   Element* head = GetHeadElement();
+  if (!head) {
+    return NS_ERROR_FAILURE;
+  }
   return head->AppendChildTo(link, false);
 }
 
 nsresult
 MediaDocument::LinkScript(const nsAString& aScript)
 {
   RefPtr<mozilla::dom::NodeInfo> nodeInfo;
   nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::script, nullptr,
@@ -345,16 +348,19 @@ MediaDocument::LinkScript(const nsAStrin
   NS_ENSURE_TRUE(script, NS_ERROR_OUT_OF_MEMORY);
 
   script->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
                   NS_LITERAL_STRING("text/javascript"), true);
 
   script->SetAttr(kNameSpaceID_None, nsGkAtoms::src, aScript, true);
 
   Element* head = GetHeadElement();
+  if (!head) {
+    return NS_ERROR_FAILURE;
+  }
   return head->AppendChildTo(script, false);
 }
 
 void
 MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr,
                                      nsIChannel* aChannel,
                                      const char* const* aFormatNames,
                                      int32_t aWidth, int32_t aHeight,