Bug 1453869 part 5. Remove nsIDOMParser::ParseFromBuffer. r=mrbkap
authorBoris Zbarsky <bzbarsky@mit.edu>
Fri, 20 Apr 2018 23:01:25 -0400
changeset 468455 00340263aeb2a34301a5046be85d4f55069e9bcd
parent 468454 432d897ff4a93f466666163dff182feebd4f40a0
child 468456 ba157de852effc07e6c66e8df40aa74f29c37a5f
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs1453869
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1453869 part 5. Remove nsIDOMParser::ParseFromBuffer. r=mrbkap MozReview-Commit-ID: 4KuM0HRI2BC
dom/base/DOMParser.cpp
dom/base/DOMParser.h
dom/base/nsIDOMParser.idl
dom/base/test/chrome/test_domparsing.xul
dom/webidl/DOMParser.webidl
toolkit/components/search/nsSearchService.js
toolkit/modules/PropertyListUtils.jsm
--- a/dom/base/DOMParser.cpp
+++ b/dom/base/DOMParser.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/DOMParser.h"
 
 #include "nsIDOMDocument.h"
 #include "nsNetUtil.h"
+#include "nsDOMString.h"
 #include "nsIStreamListener.h"
 #include "nsStringStream.h"
 #include "nsIScriptError.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsCRT.h"
 #include "nsStreamUtils.h"
 #include "nsContentUtils.h"
 #include "nsDOMJSUtils.h"
@@ -98,80 +99,51 @@ DOMParser::ParseFromString(const nsAStri
     return nullptr;
   }
 
   return ParseFromStream(stream,  NS_LITERAL_STRING("UTF-8"),
                          utf8str.Length(), aType, aRv);
 }
 
 already_AddRefed<nsIDocument>
-DOMParser::ParseFromBuffer(const Sequence<uint8_t>& aBuf, uint32_t aBufLen,
-                           SupportedType aType, ErrorResult& rv)
+DOMParser::ParseFromBuffer(const Uint8Array& aBuf, SupportedType aType,
+                           ErrorResult& aRv)
 {
-  if (aBufLen > aBuf.Length()) {
-    rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
-    return nullptr;
-  }
-  nsCOMPtr<nsIDOMDocument> domDocument;
-  rv = DOMParser::ParseFromBuffer(aBuf.Elements(), aBufLen,
-                                  StringFromSupportedType(aType),
-                                  getter_AddRefs(domDocument));
-  nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
-  return document.forget();
+  aBuf.ComputeLengthAndData();
+  return ParseFromBuffer(MakeSpan(aBuf.Data(), aBuf.Length()), aType, aRv);
 }
 
 already_AddRefed<nsIDocument>
-DOMParser::ParseFromBuffer(const Uint8Array& aBuf, uint32_t aBufLen,
-                           SupportedType aType, ErrorResult& rv)
+DOMParser::ParseFromBuffer(Span<const uint8_t> aBuf, SupportedType aType,
+                           ErrorResult& aRv)
 {
-  aBuf.ComputeLengthAndData();
-
-  if (aBufLen > aBuf.Length()) {
-    rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
-    return nullptr;
-  }
-  nsCOMPtr<nsIDOMDocument> domDocument;
-  rv = DOMParser::ParseFromBuffer(aBuf.Data(), aBufLen,
-                                    StringFromSupportedType(aType),
-                                    getter_AddRefs(domDocument));
-  nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
-  return document.forget();
-}
-
-NS_IMETHODIMP
-DOMParser::ParseFromBuffer(const uint8_t *buf,
-                           uint32_t bufLen,
-                           const char *contentType,
-                           nsIDOMDocument **aResult)
-{
-  NS_ENSURE_ARG_POINTER(buf);
-  NS_ENSURE_ARG_POINTER(aResult);
-
   // The new stream holds a reference to the buffer
   nsCOMPtr<nsIInputStream> stream;
   nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
-                                      reinterpret_cast<const char *>(buf),
-                                      bufLen, NS_ASSIGNMENT_DEPEND);
-  if (NS_FAILED(rv))
-    return rv;
+                                      reinterpret_cast<const char *>(aBuf.Elements()),
+                                      aBuf.Length(), NS_ASSIGNMENT_DEPEND);
+  if (NS_FAILED(rv)) {
+    aRv.Throw(rv);
+    return nullptr;
+  }
 
-  return ParseFromStream(stream, nullptr, bufLen, contentType, aResult);
+  return ParseFromStream(stream, VoidString(), aBuf.Length(), aType, aRv);
 }
 
 
 already_AddRefed<nsIDocument>
 DOMParser::ParseFromStream(nsIInputStream* aStream,
                            const nsAString& aCharset,
                            int32_t aContentLength,
                            SupportedType aType,
                            ErrorResult& rv)
 {
   nsCOMPtr<nsIDOMDocument> domDocument;
   rv = DOMParser::ParseFromStream(aStream,
-                                  NS_ConvertUTF16toUTF8(aCharset).get(),
+                                  DOMStringIsNull(aCharset) ? nullptr : NS_ConvertUTF16toUTF8(aCharset).get(),
                                   aContentLength,
                                   StringFromSupportedType(aType),
                                   getter_AddRefs(domDocument));
   nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
   return document.forget();
 }
 
 NS_IMETHODIMP
--- a/dom/base/DOMParser.h
+++ b/dom/base/DOMParser.h
@@ -8,16 +8,17 @@
 #define mozilla_dom_DOMParser_h_
 
 #include "nsCOMPtr.h"
 #include "nsIDocument.h"
 #include "nsIDOMParser.h"
 #include "nsWeakReference.h"
 #include "nsWrapperCache.h"
 #include "mozilla/ErrorResult.h"
+#include "mozilla/Span.h"
 #include "mozilla/dom/DOMParserBinding.h"
 #include "mozilla/dom/TypedArray.h"
 
 class nsIDocument;
 
 namespace mozilla {
 namespace dom {
 
@@ -42,25 +43,25 @@ public:
   // WebIDL API
   static already_AddRefed<DOMParser>
   Constructor(const GlobalObject& aOwner,
               mozilla::ErrorResult& rv);
 
   already_AddRefed<nsIDocument>
   ParseFromString(const nsAString& aStr, SupportedType aType, ErrorResult& aRv);
 
+  // Sequence converts to Span, so we can use this overload for both
+  // the Sequence case and our internal uses.
   already_AddRefed<nsIDocument>
-  ParseFromBuffer(const mozilla::dom::Sequence<uint8_t>& aBuf,
-                  uint32_t aBufLen, mozilla::dom::SupportedType aType,
-                  mozilla::ErrorResult& rv);
+  ParseFromBuffer(Span<const uint8_t> aBuf, SupportedType aType,
+                  ErrorResult& aRv);
 
   already_AddRefed<nsIDocument>
-  ParseFromBuffer(const mozilla::dom::Uint8Array& aBuf, uint32_t aBufLen,
-                  mozilla::dom::SupportedType aType,
-                  mozilla::ErrorResult& rv);
+  ParseFromBuffer(const Uint8Array& aBuf, SupportedType aType,
+                  ErrorResult& aRv);
 
   already_AddRefed<nsIDocument>
   ParseFromStream(nsIInputStream* aStream, const nsAString& aCharset,
                   int32_t aContentLength, mozilla::dom::SupportedType aType,
                   mozilla::ErrorResult& rv);
 
   void
   ForceEnableXULXBL()
--- a/dom/base/nsIDOMParser.idl
+++ b/dom/base/nsIDOMParser.idl
@@ -18,29 +18,16 @@ interface nsIGlobalObject;
  * returned from the parsing methods. This is as opposed to loading and
  * parsing with the XMLHttpRequest interface, which can be used for
  * asynchronous (callback-based) loading.
  */
 [shim(DOMParser), uuid(70b9600e-8622-4c93-9ad8-22c28058dc44)]
 interface nsIDOMParser : nsISupports
 {
   /**
-   * The buffer is parsed into a DOM document.
-   * The charset is determined from the xml entity decl.
-   *
-   * @param buf The octet array data to be parsed
-   * @param bufLen Length (in bytes) of the data
-   * @param contentType The content type of the data (see parseFromStream)
-   * @returns The DOM document created as a result of parsing the 
-   *          string
-   */
-  nsIDOMDocument parseFromBuffer([const,array,size_is(bufLen)] in octet buf,
-                                 in uint32_t bufLen, in string contentType);
-
-  /**
    * The byte stream passed in is parsed into a DOM document.
    *
    * Not accessible from web content.
    *
    * @param stream The byte stream whose contents are parsed
    * @param charset The character set that was used to encode the byte
    *                stream. NULL if not specified.
    * @param contentLength The number of bytes in the input stream.
--- a/dom/base/test/chrome/test_domparsing.xul
+++ b/dom/base/test/chrome/test_domparsing.xul
@@ -88,23 +88,18 @@ function runTest(parser, serializer) {
 
     let array = Array.map(t.input, function(c) { return c.charCodeAt(c); });
     let inputs = [
       {array: array, name: "parseFromBuffer (array)"},
       {array: new Uint8Array(array), name: "parseFromBuffer (Uint8Array)"},
     ];
     for (let input of inputs) {
       let a = input.array;
-      is(serializer.serializeToString(parser.parseFromBuffer(a, a.length, t.type)), t.expected,
+      is(serializer.serializeToString(parser.parseFromBuffer(a, t.type)), t.expected,
          input.name + " test for " + t.type);
-      throws(function() {
-          parser.parseFromBuffer(a, a.length + 1, t.type);
-        }, "NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY",
-        input.name + " should throw if bufLen parameter is greater than actual length"
-      );
     }
 
     let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                   createInstance(Ci.nsIStringInputStream);
     for (let charset of [null, "UTF-8"]) {
       istream.setData(t.input, -1);
       is(serializer.serializeToString(parser.parseFromStream(istream, charset, t.input.length, t.type)),
          t.expected, "parseFromStream test for " + t.type + ", charset=" + charset);
--- a/dom/webidl/DOMParser.webidl
+++ b/dom/webidl/DOMParser.webidl
@@ -20,24 +20,20 @@ enum SupportedType {
 
 // the latter is Mozilla-specific
 [Constructor]
 interface DOMParser {
   [NewObject, Throws]
   Document parseFromString(DOMString str, SupportedType type);
 
   // Mozilla-specific stuff
-  // Throws if the passed-in length is greater than the actual sequence length
   [NewObject, Throws, ChromeOnly]
-  Document parseFromBuffer(sequence<octet> buf, unsigned long bufLen,
-                           SupportedType type);
-  // Throws if the passed-in length is greater than the actual typed array length
+  Document parseFromBuffer(sequence<octet> buf, SupportedType type);
   [NewObject, Throws, ChromeOnly]
-  Document parseFromBuffer(Uint8Array buf, unsigned long bufLen,
-                           SupportedType type);
+  Document parseFromBuffer(Uint8Array buf, SupportedType type);
   [NewObject, Throws, ChromeOnly]
   Document parseFromStream(InputStream stream, DOMString? charset,
                            long contentLength, SupportedType type);
   // Can be used to allow a DOMParser to parse XUL/XBL no matter what
   // principal it's using for the document.
   [ChromeOnly]
   void forceEnableXULXBL();
 };
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -1555,17 +1555,17 @@ Engine.prototype = {
 
     if (!aBytes) {
       promptError();
       return;
     }
 
     var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
                  createInstance(Ci.nsIDOMParser);
-    var doc = parser.parseFromBuffer(aBytes, aBytes.length, "text/xml");
+    var doc = parser.parseFromBuffer(aBytes, "text/xml");
     aEngine._data = doc.documentElement;
 
     try {
       // Initialize the engine from the obtained data
       aEngine._initFromData();
     } catch (ex) {
       LOG("_onLoad: Failed to init engine!\n" + ex);
       // Report an error to the user
--- a/toolkit/modules/PropertyListUtils.jsm
+++ b/toolkit/modules/PropertyListUtils.jsm
@@ -134,18 +134,17 @@ var PropertyListUtils = Object.freeze({
     if (BinaryPropertyListReader.prototype.canProcess(aBuffer))
       return new BinaryPropertyListReader(aBuffer).root;
 
     // Convert the buffer into an XML tree.
     let domParser = Cc["@mozilla.org/xmlextras/domparser;1"].
                     createInstance(Ci.nsIDOMParser);
     let bytesView = new Uint8Array(aBuffer);
     try {
-      let doc = domParser.parseFromBuffer(bytesView, bytesView.length,
-                                          "application/xml");
+      let doc = domParser.parseFromBuffer(bytesView, "application/xml");
       return new XMLPropertyListReader(doc).root;
     } catch (ex) {
       throw new Error("aBuffer cannot be parsed as a DOM document: " + ex);
     }
   },
 
   TYPE_PRIMITIVE:    0,
   TYPE_DATE:         1,