From ae7bb9dd457f55932190046f5814da9bd60b5676 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Wed, 26 Jul 2017 19:47:18 +1000 Subject: [PATCH] Bug 1384162 part 2 - Mondernize nsContentDLF::CreateBlankDocument. r=bz MozReview-Commit-ID: CxAeysZbbuw --HG-- extra : rebase_source : 86e3a24c20db5cd8db958333ab61e2aafa0865aa --- docshell/base/nsDocShell.cpp | 3 +- layout/build/nsContentDLF.cpp | 97 +++++++++++++++-------------------- layout/build/nsContentDLF.h | 5 +- 3 files changed, 44 insertions(+), 61 deletions(-) diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 946900bc7460..f09788bea688 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -8187,8 +8187,7 @@ nsDocShell::CreateAboutBlankContentViewer(nsIPrincipal* aPrincipal, 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. diff --git a/layout/build/nsContentDLF.cpp b/layout/build/nsContentDLF.cpp index 30aedf642716..7deb2aaaf9b0 100644 --- a/layout/build/nsContentDLF.cpp +++ b/layout/build/nsContentDLF.cpp @@ -263,78 +263,63 @@ nsContentDLF::CreateInstanceForDocument(nsISupports* aContainer, return NS_OK; } -/* static */ nsresult +/* static */ already_AddRefed nsContentDLF::CreateBlankDocument(nsILoadGroup* aLoadGroup, - nsIPrincipal* aPrincipal, - nsIDocument** aDocument) + nsIPrincipal* aPrincipal) { - *aDocument = nullptr; - - nsresult rv = NS_ERROR_FAILURE; - // create a new blank HTML document nsCOMPtr blankDoc(do_CreateInstance(kHTMLDocumentCID)); - if (blankDoc) { - // initialize - nsCOMPtr 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 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 htmlNodeInfo; - RefPtr htmlNodeInfo; + // generate an html html element + htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + nsCOMPtr htmlElement = + NS_NewHTMLHtmlElement(htmlNodeInfo.forget()); - // generate an html html element - htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::html, 0, kNameSpaceID_XHTML, - nsIDOMNode::ELEMENT_NODE); - nsCOMPtr htmlElement = - NS_NewHTMLHtmlElement(htmlNodeInfo.forget()); + // generate an html head element + htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + nsCOMPtr headElement = + NS_NewHTMLHeadElement(htmlNodeInfo.forget()); - // generate an html head element - htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::head, 0, kNameSpaceID_XHTML, - nsIDOMNode::ELEMENT_NODE); - nsCOMPtr headElement = - NS_NewHTMLHeadElement(htmlNodeInfo.forget()); + // generate an html body elemment + htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + nsCOMPtr bodyElement = + NS_NewHTMLBodyElement(htmlNodeInfo.forget()); - // generate an html body elemment - htmlNodeInfo = nim->GetNodeInfo(nsGkAtoms::body, 0, kNameSpaceID_XHTML, - nsIDOMNode::ELEMENT_NODE); - nsCOMPtr 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(); } diff --git a/layout/build/nsContentDLF.h b/layout/build/nsContentDLF.h index 66797e310443..249f9b1293d5 100644 --- a/layout/build/nsContentDLF.h +++ b/layout/build/nsContentDLF.h @@ -51,9 +51,8 @@ public: * 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 + CreateBlankDocument(nsILoadGroup* aLoadGroup, nsIPrincipal* aPrincipal); private: static nsresult EnsureUAStyleSheet();