Bug 1452235 part 2. Remove nsIDOMSerializer::SerializeToString. r=qdot
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 09 Apr 2018 16:30:32 -0400
changeset 779614 a53d6207c403caa86b205e56aa15dd49b5aeb7ae
parent 779613 f7391f0f3b60ee70ffb22f4ecd94e725156759e0
child 779615 04c975671b123b6ff23f2b0a77ed66a9b5f0a067
push id105824
push usernnethercote@mozilla.com
push dateTue, 10 Apr 2018 09:55:33 +0000
reviewersqdot
bugs1452235
milestone61.0a1
Bug 1452235 part 2. Remove nsIDOMSerializer::SerializeToString. r=qdot MozReview-Commit-ID: BuIhA13GuLj
dom/base/nsDOMSerializer.cpp
dom/base/nsIDOMSerializer.idl
dom/webidl/XMLSerializer.webidl
--- a/dom/base/nsDOMSerializer.cpp
+++ b/dom/base/nsDOMSerializer.cpp
@@ -34,90 +34,86 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
 NS_INTERFACE_MAP_END
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsDOMSerializer, mOwner)
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMSerializer)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMSerializer)
 
 
-static nsresult
-SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
-             nsIDocumentEncoder **aEncoder)
+static already_AddRefed<nsIDocumentEncoder>
+SetUpEncoder(nsINode& aRoot, const nsAString& aCharset, ErrorResult& aRv)
 {
-  *aEncoder = nullptr;
-
   nsresult rv;
   nsCOMPtr<nsIDocumentEncoder> encoder =
     do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "application/xhtml+xml", &rv);
-  if (NS_FAILED(rv))
-    return rv;
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+    return nullptr;
+  }
 
-  nsCOMPtr<nsINode> root = do_QueryInterface(aRoot);
-  MOZ_ASSERT(root);
-
-  nsIDocument* doc = root->OwnerDoc();
-  bool entireDocument = (doc == root);
-  nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(doc));
+  nsIDocument* doc = aRoot.OwnerDoc();
+  bool entireDocument = (doc == &aRoot);
 
   // This method will fail if no document
-  rv = encoder->Init(domDoc, NS_LITERAL_STRING("application/xhtml+xml"),
-                     nsIDocumentEncoder::OutputRaw |
-                     nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
+  rv = encoder->
+    NativeInit(doc, NS_LITERAL_STRING("application/xhtml+xml"),
+               nsIDocumentEncoder::OutputRaw |
+               nsIDocumentEncoder::OutputDontRewriteEncodingDeclaration);
 
-  if (NS_FAILED(rv))
-    return rv;
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+    return nullptr;
+  }
 
-  nsAutoCString charset(aCharset);
+  NS_ConvertUTF16toUTF8 charset(aCharset);
   if (charset.IsEmpty()) {
-    nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
-    NS_ASSERTION(doc, "Need a document");
     doc->GetDocumentCharacterSet()->Name(charset);
   }
   rv = encoder->SetCharset(charset);
-  if (NS_FAILED(rv))
-    return rv;
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+    return nullptr;
+  }
 
   // If we are working on the entire document we do not need to
   // specify which part to serialize
   if (!entireDocument) {
-    rv = encoder->SetNode(aRoot);
+    rv = encoder->SetNativeNode(&aRoot);
   }
 
-  if (NS_SUCCEEDED(rv)) {
-    encoder.forget(aEncoder);
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+    return nullptr;
   }
 
-  return rv;
+  return encoder.forget();
 }
 
 void
 nsDOMSerializer::SerializeToString(nsINode& aRoot, nsAString& aStr,
-                                   ErrorResult& rv)
+                                   ErrorResult& aRv)
 {
-  rv = nsDOMSerializer::SerializeToString(aRoot.AsDOMNode(), aStr);
-}
+  aStr.Truncate();
 
-NS_IMETHODIMP
-nsDOMSerializer::SerializeToString(nsIDOMNode *aRoot, nsAString& _retval)
-{
-  NS_ENSURE_ARG_POINTER(aRoot);
-
-  _retval.Truncate();
-
-  if (!nsContentUtils::CanCallerAccess(aRoot)) {
-    return NS_ERROR_DOM_SECURITY_ERR;
+  if (!nsContentUtils::CanCallerAccess(&aRoot)) {
+    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
+    return;
   }
 
-  nsCOMPtr<nsIDocumentEncoder> encoder;
-  nsresult rv = SetUpEncoder(aRoot, EmptyCString(), getter_AddRefs(encoder));
-  if (NS_FAILED(rv))
-    return rv;
+  nsCOMPtr<nsIDocumentEncoder> encoder =
+    SetUpEncoder(aRoot, EmptyString(), aRv);
+  if (aRv.Failed()) {
+    return;
+  }
 
-  return encoder->EncodeToString(_retval);
+  nsresult rv = encoder->EncodeToString(aStr);
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+  }
 }
 
 void
 nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
                                    const nsAString& aCharset,
                                    ErrorResult& aRv)
 {
   if (NS_WARN_IF(!aStream)) {
@@ -125,21 +121,19 @@ nsDOMSerializer::SerializeToStream(nsINo
     return;
   }
 
   // The charset arg can be empty, in which case we get the document's
   // charset and use that when serializing.
 
   // No point doing a CanCallerAccess check, because we can only be
   // called by system JS or C++.
-  nsCOMPtr<nsIDocumentEncoder> encoder;
-  nsresult rv = SetUpEncoder(aRoot.AsDOMNode(), NS_ConvertUTF16toUTF8(aCharset),
-                             getter_AddRefs(encoder));
-  if (NS_FAILED(rv)) {
-    aRv.Throw(rv);
+  nsCOMPtr<nsIDocumentEncoder> encoder =
+    SetUpEncoder(aRoot, aCharset, aRv);
+  if (aRv.Failed()) {
     return;
   }
 
-  rv = encoder->EncodeToStream(aStream);
+  nsresult rv = encoder->EncodeToStream(aStream);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
   }
 }
--- a/dom/base/nsIDOMSerializer.idl
+++ b/dom/base/nsIDOMSerializer.idl
@@ -13,25 +13,16 @@ interface nsIDOMNode;
  * DOM Working Group defines a mechanism for serializing DOM nodes.
  * An instance of this interface can be used to serialize a DOM document
  * or any DOM subtree.
  */
 
 [uuid(9fd4ba15-e67c-4c98-b52c-7715f62c9196)]
 interface nsIDOMSerializer : nsISupports
 {
-  /**
-   * The subtree rooted by the specified element is serialized to
-   * a string.
-   * 
-   * @param root The root of the subtree to be serialized. This could
-   *             be any node, including a Document.
-   * @returns The serialized subtree in the form of a Unicode string
-   */
-  AString serializeToString(in nsIDOMNode root);
 };
 
 %{ C++
 #define NS_XMLSERIALIZER_CID                        \
  { /* a6cf9124-15b3-11d2-932e-00805f8add32 */       \
    0xa6cf9124, 0x15b3, 0x11d2,                      \
   {0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32} }
 #define NS_XMLSERIALIZER_CONTRACTID \
--- a/dom/webidl/XMLSerializer.webidl
+++ b/dom/webidl/XMLSerializer.webidl
@@ -5,16 +5,24 @@
  * The origin of this IDL file is
  * http://domparsing.spec.whatwg.org/#the-xmlserializer-interface
  */
 
 interface OutputStream;
 
 [Constructor]
 interface XMLSerializer {
+  /**
+   * The subtree rooted by the specified element is serialized to
+   * a string.
+   *
+   * @param root The root of the subtree to be serialized. This could
+   *             be any node, including a Document.
+   * @returns The serialized subtree in the form of a Unicode string
+   */
   [Throws]
   DOMString serializeToString(Node root);
 
   // Mozilla-specific stuff
   /**
    * The subtree rooted by the specified element is serialized to
    * a byte stream using the character set specified.
    * @param root The root of the subtree to be serialized. This could