Bug 1384162 part 2 - Mondernize nsContentDLF::CreateBlankDocument. r?bz draft
authorXidorn Quan <me@upsuper.org>
Wed, 26 Jul 2017 19:47:18 +1000
changeset 615801 caef389697d2574703670b693297ad4c4c66b2ec
parent 615797 bfc102b367952aa96249ef81ffa02b37f2609ac6
child 615802 352a27aa89fa66a73560940abd1ea4fbcbc4c040
child 616305 f6b2f32fb4e7b3cb1022d1fef0722435d25d0d46
push id70477
push userxquan@mozilla.com
push dateWed, 26 Jul 2017 10:22:08 +0000
reviewersbz
bugs1384162
milestone56.0a1
Bug 1384162 part 2 - Mondernize nsContentDLF::CreateBlankDocument. r?bz MozReview-Commit-ID: CxAeysZbbuw
docshell/base/nsDocShell.cpp
layout/build/nsContentDLF.cpp
layout/build/nsContentDLF.h
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -8182,18 +8182,17 @@ nsDocShell::CreateAboutBlankContentViewe
         principal = NullPrincipal::CreateWithInheritedAttributes(aPrincipal);
       } else {
         principal = NullPrincipal::CreateWithInheritedAttributes(this);
       }
     } else {
       principal = aPrincipal;
     }
     // generate (about:blank) document to load
-    nsContentDLF::CreateBlankDocument(mLoadGroup, principal,
-                                      getter_AddRefs(blankDoc));
+    blankDoc = nsContentDLF::CreateBlankDocument(mLoadGroup, principal);
     if (blankDoc) {
       // Hack: set the base URI manually, since this document never
       // got Reset() with a channel.
       blankDoc->SetBaseURI(aBaseURI);
 
       blankDoc->SetContainer(this);
 
       // Copy our sandbox flags to the document. These are immutable
--- a/layout/build/nsContentDLF.cpp
+++ b/layout/build/nsContentDLF.cpp
@@ -258,88 +258,73 @@ nsContentDLF::CreateInstanceForDocument(
   nsCOMPtr<nsIContentViewer> contentViewer = NS_NewContentViewer();
 
   // Bind the document to the Content Viewer
   contentViewer->LoadStart(aDocument);
   contentViewer.forget(aContentViewer);
   return NS_OK;
 }
 
-/* static */ nsresult
+/* static */ already_AddRefed<nsIDocument>
 nsContentDLF::CreateBlankDocument(nsILoadGroup* aLoadGroup,
-                                  nsIPrincipal* aPrincipal,
-                                  nsIDocument** aDocument)
+                                  nsIPrincipal* aPrincipal)
 {
-  *aDocument = nullptr;
-
-  nsresult rv = NS_ERROR_FAILURE;
-
   // create a new blank HTML document
   nsCOMPtr<nsIDocument> blankDoc(do_CreateInstance(kHTMLDocumentCID));
 
-  if (blankDoc) {
-    // initialize
-    nsCOMPtr<nsIURI> uri;
-    NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:blank"));
-    if (uri) {
-      blankDoc->ResetToURI(uri, aLoadGroup, aPrincipal);
-      rv = NS_OK;
-    }
+  if (!blankDoc) {
+    return nullptr;
   }
 
+  // initialize
+  nsCOMPtr<nsIURI> uri;
+  NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:blank"));
+  if (!uri) {
+    return nullptr;
+  }
+  blankDoc->ResetToURI(uri, aLoadGroup, aPrincipal);
+
   // add some simple content structure
-  if (NS_SUCCEEDED(rv)) {
-    rv = NS_ERROR_FAILURE;
+  nsNodeInfoManager *nim = blankDoc->NodeInfoManager();
 
-    nsNodeInfoManager *nim = blankDoc->NodeInfoManager();
-
-    RefPtr<mozilla::dom::NodeInfo> htmlNodeInfo;
+  RefPtr<mozilla::dom::NodeInfo> htmlNodeInfo;
 
-    // generate an html html element
-    htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML,
-                                    nsIDOMNode::ELEMENT_NODE);
-    nsCOMPtr<nsIContent> htmlElement =
-      NS_NewHTMLHtmlElement(htmlNodeInfo.forget());
+  // generate an html html element
+  htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML,
+                                  nsIDOMNode::ELEMENT_NODE);
+  nsCOMPtr<nsIContent> htmlElement =
+    NS_NewHTMLHtmlElement(htmlNodeInfo.forget());
 
-    // generate an html head element
-    htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML,
-                                    nsIDOMNode::ELEMENT_NODE);
-    nsCOMPtr<nsIContent> headElement =
-      NS_NewHTMLHeadElement(htmlNodeInfo.forget());
+  // generate an html head element
+  htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML,
+                                  nsIDOMNode::ELEMENT_NODE);
+  nsCOMPtr<nsIContent> headElement =
+    NS_NewHTMLHeadElement(htmlNodeInfo.forget());
 
-    // generate an html body elemment
-    htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML,
-                                    nsIDOMNode::ELEMENT_NODE);
-    nsCOMPtr<nsIContent> bodyElement =
-      NS_NewHTMLBodyElement(htmlNodeInfo.forget());
+  // generate an html body elemment
+  htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML,
+                                  nsIDOMNode::ELEMENT_NODE);
+  nsCOMPtr<nsIContent> bodyElement =
+    NS_NewHTMLBodyElement(htmlNodeInfo.forget());
 
-    // blat in the structure
-    if (htmlElement && headElement && bodyElement) {
-      NS_ASSERTION(blankDoc->GetChildCount() == 0,
-                   "Shouldn't have children");
-      rv = blankDoc->AppendChildTo(htmlElement, false);
-      if (NS_SUCCEEDED(rv)) {
-        rv = htmlElement->AppendChildTo(headElement, false);
-
-        if (NS_SUCCEEDED(rv)) {
-          // XXXbz Why not notifying here?
-          htmlElement->AppendChildTo(bodyElement, false);
-        }
-      }
-    }
+  // blat in the structure
+  NS_ASSERTION(blankDoc->GetChildCount() == 0,
+                "Shouldn't have children");
+  if (!htmlElement || !headElement || !bodyElement ||
+      NS_FAILED(blankDoc->AppendChildTo(htmlElement, false)) ||
+      NS_FAILED(htmlElement->AppendChildTo(headElement, false)) ||
+      // XXXbz Why not notifying here?
+      NS_FAILED(htmlElement->AppendChildTo(bodyElement, false))) {
+    return nullptr;
   }
 
   // add a nice bow
-  if (NS_SUCCEEDED(rv)) {
-    blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
-    blankDoc->SetDocumentCharacterSet(UTF_8_ENCODING);
-
-    blankDoc.forget(aDocument);
-  }
-  return rv;
+  blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
+  blankDoc->SetDocumentCharacterSet(UTF_8_ENCODING);
+  return blankDoc.forget();
 }
 
 
 nsresult
 nsContentDLF::CreateDocument(const char* aCommand,
                              nsIChannel* aChannel,
                              nsILoadGroup* aLoadGroup,
                              nsIDocShell* aContainer,
--- a/layout/build/nsContentDLF.h
+++ b/layout/build/nsContentDLF.h
@@ -46,19 +46,18 @@ public:
                              nsIStreamListener** aDocListener,
                              nsIContentViewer** aContentViewer);
 
   /**
    * Create a blank document using the given loadgroup and given
    * principal.  aPrincipal is allowed to be null, in which case the
    * new document will get the about:blank codebase principal.
    */
-  static nsresult CreateBlankDocument(nsILoadGroup* aLoadGroup,
-                                      nsIPrincipal* aPrincipal,
-                                      nsIDocument** aDocument);
+  static already_AddRefed<nsIDocument>
+  CreateBlankDocument(nsILoadGroup* aLoadGroup, nsIPrincipal* aPrincipal);
 
 private:
   static nsresult EnsureUAStyleSheet();
   static bool IsImageContentType(const char* aContentType);
 };
 
 nsresult
 NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult);