Bug 1452235 part 1. Remove nsIDOMSerializer::SerializeToStream. r=qdot
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 09 Apr 2018 16:30:32 -0400
changeset 779613 f7391f0f3b60ee70ffb22f4ecd94e725156759e0
parent 779612 8949e2fb8bb34256a872dd6244f74ada4c63bbfc
child 779614 a53d6207c403caa86b205e56aa15dd49b5aeb7ae
push id105824
push usernnethercote@mozilla.com
push dateTue, 10 Apr 2018 09:55:33 +0000
reviewersqdot
bugs1452235
milestone61.0a1
Bug 1452235 part 1. Remove nsIDOMSerializer::SerializeToStream. r=qdot MozReview-Commit-ID: IB4W7R7Rg2P
dom/base/nsDOMSerializer.cpp
dom/base/nsDOMSerializer.h
dom/base/nsIDOMSerializer.idl
dom/fetch/BodyExtractor.cpp
dom/fetch/moz.build
dom/webidl/XMLSerializer.webidl
--- a/dom/base/nsDOMSerializer.cpp
+++ b/dom/base/nsDOMSerializer.cpp
@@ -112,35 +112,34 @@ nsDOMSerializer::SerializeToString(nsIDO
   if (NS_FAILED(rv))
     return rv;
 
   return encoder->EncodeToString(_retval);
 }
 
 void
 nsDOMSerializer::SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
-                                   const nsAString& aCharset, ErrorResult& rv)
+                                   const nsAString& aCharset,
+                                   ErrorResult& aRv)
 {
-  rv = nsDOMSerializer::SerializeToStream(aRoot.AsDOMNode(), aStream,
-                                          NS_ConvertUTF16toUTF8(aCharset));
-}
+  if (NS_WARN_IF(!aStream)) {
+    aRv.Throw(NS_ERROR_INVALID_ARG);
+    return;
+  }
 
-NS_IMETHODIMP
-nsDOMSerializer::SerializeToStream(nsIDOMNode *aRoot,
-                                   nsIOutputStream *aStream,
-                                   const nsACString& aCharset)
-{
-  NS_ENSURE_ARG_POINTER(aRoot);
-  NS_ENSURE_ARG_POINTER(aStream);
   // The charset arg can be empty, in which case we get the document's
   // charset and use that when serializing.
 
-  if (!nsContentUtils::CanCallerAccess(aRoot)) {
-    return NS_ERROR_DOM_SECURITY_ERR;
+  // 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);
+    return;
   }
 
-  nsCOMPtr<nsIDocumentEncoder> encoder;
-  nsresult rv = SetUpEncoder(aRoot, aCharset, getter_AddRefs(encoder));
-  if (NS_FAILED(rv))
-    return rv;
-
-  return encoder->EncodeToStream(aStream);
+  rv = encoder->EncodeToStream(aStream);
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+  }
 }
--- a/dom/base/nsDOMSerializer.h
+++ b/dom/base/nsDOMSerializer.h
@@ -36,17 +36,18 @@ public:
   }
 
   void
   SerializeToString(nsINode& aRoot, nsAString& aStr,
                     mozilla::ErrorResult& rv);
 
   void
   SerializeToStream(nsINode& aRoot, nsIOutputStream* aStream,
-                    const nsAString& aCharset, mozilla::ErrorResult& rv);
+                    const nsAString& aCharset,
+                    mozilla::ErrorResult& aRv);
 
   nsISupports* GetParentObject() const
   {
     return mOwner;
   }
 
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
   {
--- a/dom/base/nsIDOMSerializer.idl
+++ b/dom/base/nsIDOMSerializer.idl
@@ -22,29 +22,16 @@ 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);
-
-  /**
-   * 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
-   *             be any node, including a Document.
-   * @param stream The byte stream to which the subtree is serialized.
-   * @param charset The name of the character set to use for the encoding
-   *                to a byte stream.  If this string is empty and root is
-   *                a document, the document's character set will be used.
-   */
-  void serializeToStream(in nsIDOMNode root, in nsIOutputStream stream,
-                         in AUTF8String charset);
 };
 
 %{ 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/fetch/BodyExtractor.cpp
+++ b/dom/fetch/BodyExtractor.cpp
@@ -6,18 +6,17 @@
 
 #include "BodyExtractor.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/FormData.h"
 #include "mozilla/dom/TypedArray.h"
 #include "mozilla/dom/URLSearchParams.h"
 #include "mozilla/dom/XMLHttpRequest.h"
 #include "nsContentUtils.h"
-#include "nsIDOMDocument.h"
-#include "nsIDOMSerializer.h"
+#include "nsDOMSerializer.h"
 #include "nsIGlobalObject.h"
 #include "nsIInputStream.h"
 #include "nsIOutputStream.h"
 #include "nsIStorageStream.h"
 #include "nsStringStream.h"
 
 namespace mozilla {
 namespace dom {
@@ -68,18 +67,17 @@ BodyExtractor<const ArrayBufferView>::Ge
 }
 
 template<> nsresult
 BodyExtractor<nsIDocument>::GetAsStream(nsIInputStream** aResult,
                                         uint64_t* aContentLength,
                                         nsACString& aContentTypeWithCharset,
                                         nsACString& aCharset) const
 {
-  nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mBody));
-  NS_ENSURE_STATE(domdoc);
+  NS_ENSURE_STATE(mBody);
   aCharset.AssignLiteral("UTF-8");
 
   nsresult rv;
   nsCOMPtr<nsIStorageStream> storStream;
   rv = NS_NewStorageStream(4096, UINT32_MAX, getter_AddRefs(storStream));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIOutputStream> output;
@@ -102,23 +100,25 @@ BodyExtractor<nsIDocument>::GetAsStream(
     uint32_t written;
     rv = output->Write(utf8Serialized.get(), utf8Serialized.Length(), &written);
     NS_ENSURE_SUCCESS(rv, rv);
 
     MOZ_ASSERT(written == utf8Serialized.Length());
   } else {
     aContentTypeWithCharset.AssignLiteral("application/xml;charset=UTF-8");
 
-    nsCOMPtr<nsIDOMSerializer> serializer =
-      do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+    RefPtr<nsDOMSerializer> serializer = new nsDOMSerializer();
 
     // Make sure to use the encoding we'll send
-    rv = serializer->SerializeToStream(domdoc, output, aCharset);
-    NS_ENSURE_SUCCESS(rv, rv);
+    ErrorResult res;
+    serializer->SerializeToStream(*mBody, output, NS_LITERAL_STRING("UTF-8"),
+                                  res);
+    if (NS_WARN_IF(res.Failed())) {
+      return res.StealNSResult();
+    }
   }
 
   output->Close();
 
   uint32_t length;
   rv = storStream->GetLength(&length);
   NS_ENSURE_SUCCESS(rv, rv);
   *aContentLength = length;
--- a/dom/fetch/moz.build
+++ b/dom/fetch/moz.build
@@ -43,16 +43,18 @@ UNIFIED_SOURCES += [
 ]
 
 IPDL_SOURCES += [
     'ChannelInfo.ipdlh',
     'FetchTypes.ipdlh',
 ]
 
 LOCAL_INCLUDES += [
+    # For nsDOMSerializer
+    '/dom/base',
     # For HttpBaseChannel.h dependencies
     '/netwerk/base',
     # For nsDataHandler.h
     '/netwerk/protocol/data',
     # For HttpBaseChannel.h
     '/netwerk/protocol/http',
 ]
 
--- a/dom/webidl/XMLSerializer.webidl
+++ b/dom/webidl/XMLSerializer.webidl
@@ -9,12 +9,22 @@
 interface OutputStream;
 
 [Constructor]
 interface XMLSerializer {
   [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
+   *             be any node, including a Document.
+   * @param stream The byte stream to which the subtree is serialized.
+   * @param charset The name of the character set to use for the encoding
+   *                to a byte stream.  If this string is empty and root is
+   *                a document, the document's character set will be used.
+   */
   [Throws, ChromeOnly]
   void serializeToStream(Node root, OutputStream stream, DOMString? charset);
 };