Bug 1404198: Part 1 - Add non-virtual constructor for nsIObject(Input|Output)Stream and update existing callers. r=njn draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 04 Oct 2017 20:06:28 -0700
changeset 675278 ab3a17f381889b0f1c5b038928b83a0ee5a5afe4
parent 669204 5d42f8d147c677923fb04ed83a7e41e9a27d4c91
child 675279 510710d692a11aea638b94e3bf3fb1be4a342756
push id83088
push usermaglione.k@gmail.com
push dateThu, 05 Oct 2017 03:07:30 +0000
reviewersnjn
bugs1404198
milestone58.0a1
Bug 1404198: Part 1 - Add non-virtual constructor for nsIObject(Input|Output)Stream and update existing callers. r=njn MozReview-Commit-ID: 3eoh6AwDJyz
dom/cache/FileUtils.cpp
dom/events/DataTransfer.cpp
dom/quota/ActorsParent.cpp
dom/xul/nsXULPrototypeCache.cpp
netwerk/base/nsSerializationHelper.cpp
startupcache/StartupCacheUtils.cpp
xpcom/io/nsBinaryStream.cpp
xpcom/io/nsBinaryStream.h
xpcom/io/nsIObjectInputStream.idl
xpcom/io/nsIObjectOutputStream.idl
--- a/dom/cache/FileUtils.cpp
+++ b/dom/cache/FileUtils.cpp
@@ -6,18 +6,18 @@
 
 #include "mozilla/dom/cache/FileUtils.h"
 
 #include "mozilla/dom/InternalResponse.h"
 #include "mozilla/dom/quota/FileStreams.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "mozilla/SnappyCompressOutputStream.h"
 #include "mozilla/Unused.h"
-#include "nsIBinaryInputStream.h"
-#include "nsIBinaryOutputStream.h"
+#include "nsIObjectInputStream.h"
+#include "nsIObjectOutputStream.h"
 #include "nsIFile.h"
 #include "nsIUUIDGenerator.h"
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
 #include "nsISimpleEnumerator.h"
 #include "nsServiceManagerUtils.h"
 #include "nsString.h"
 #include "nsThreadUtils.h"
@@ -415,24 +415,20 @@ LockedDirectoryPaddingWrite(nsIFile* aBa
     rv = file->Append(NS_LITERAL_STRING(PADDING_FILE_NAME));
   }
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   nsCOMPtr<nsIOutputStream> outputStream;
   rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
-  nsCOMPtr<nsIBinaryOutputStream> binaryStream =
-    do_CreateInstance("@mozilla.org/binaryoutputstream;1");
-  if (NS_WARN_IF(!binaryStream)) { return NS_ERROR_FAILURE; }
+  nsCOMPtr<nsIObjectOutputStream> objectStream =
+    NS_NewObjectOutputStream(outputStream);
 
-  rv = binaryStream->SetOutputStream(outputStream);
-  if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
-
-  rv = binaryStream->Write64(aPaddingSize);
+  rv = objectStream->Write64(aPaddingSize);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   return rv;
 }
 
 } // namespace
 
 nsresult
@@ -739,25 +735,21 @@ LockedDirectoryPaddingGet(nsIFile* aBase
   nsCOMPtr<nsIInputStream> stream;
   rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   nsCOMPtr<nsIInputStream> bufferedStream;
   rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, 512);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
-  nsCOMPtr<nsIBinaryInputStream> binaryStream =
-    do_CreateInstance("@mozilla.org/binaryinputstream;1");
-  if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
-
-  rv = binaryStream->SetInputStream(bufferedStream);
-  if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
+  nsCOMPtr<nsIObjectInputStream> objectStream =
+    NS_NewObjectInputStream(bufferedStream);
 
   uint64_t paddingSize = 0;
-  rv = binaryStream->Read64(&paddingSize);
+  rv = objectStream->Read64(&paddingSize);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   *aPaddingSizeOut = paddingSize;
 
   return rv;
 }
 
 // static
--- a/dom/events/DataTransfer.cpp
+++ b/dom/events/DataTransfer.cpp
@@ -14,18 +14,18 @@
 #include "nsIScriptSecurityManager.h"
 #include "mozilla/dom/DOMStringList.h"
 #include "nsArray.h"
 #include "nsError.h"
 #include "nsIDragService.h"
 #include "nsIClipboard.h"
 #include "nsContentUtils.h"
 #include "nsIContent.h"
-#include "nsIBinaryInputStream.h"
-#include "nsIBinaryOutputStream.h"
+#include "nsIObjectInputStream.h"
+#include "nsIObjectOutputStream.h"
 #include "nsIStorageStream.h"
 #include "nsStringStream.h"
 #include "nsCRT.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIScriptContext.h"
 #include "nsIDocument.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsVariant.h"
@@ -991,17 +991,17 @@ DataTransfer::GetTransferable(uint32_t a
   nsCOMPtr<nsITransferable> transferable =
     do_CreateInstance("@mozilla.org/widget/transferable;1");
   if (!transferable) {
     return nullptr;
   }
   transferable->Init(aLoadContext);
 
   nsCOMPtr<nsIStorageStream> storageStream;
-  nsCOMPtr<nsIBinaryOutputStream> stream;
+  nsCOMPtr<nsIObjectOutputStream> stream;
 
   bool added = false;
   bool handlingCustomFormats = true;
 
   // When writing the custom data, we need to ensure that there is sufficient
   // space for a (uint32_t) data ending type, and the null byte character at
   // the end of the nsCString. We claim that space upfront and store it in
   // baseLength. This value will be set to zero if a write error occurs
@@ -1082,18 +1082,17 @@ DataTransfer::GetTransferable(uint32_t a
 
             if (!stream) {
               // Create a storage stream to write to.
               NS_NewStorageStream(1024, UINT32_MAX, getter_AddRefs(storageStream));
 
               nsCOMPtr<nsIOutputStream> outputStream;
               storageStream->GetOutputStream(0, getter_AddRefs(outputStream));
 
-              stream = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
-              stream->SetOutputStream(outputStream);
+              stream = NS_NewObjectOutputStream(outputStream);
             }
 
             CheckedInt<uint32_t> formatLength =
               CheckedInt<uint32_t>(type.Length()) * sizeof(nsString::char_type);
 
             // The total size of the stream is the format length, the data
             // length, two integers to hold the lengths and one integer for
             // the string flag. Guard against large data by ignoring any that
@@ -1588,24 +1587,18 @@ DataTransfer::FillInExternalCustomTypes(
   }
 
   nsAutoCString str;
   str.Adopt(chrs, len);
 
   nsCOMPtr<nsIInputStream> stringStream;
   NS_NewCStringInputStream(getter_AddRefs(stringStream), str);
 
-  nsCOMPtr<nsIBinaryInputStream> stream =
-    do_CreateInstance("@mozilla.org/binaryinputstream;1");
-  if (!stream) {
-    return;
-  }
-
-  rv = stream->SetInputStream(stringStream);
-  NS_ENSURE_SUCCESS_VOID(rv);
+  nsCOMPtr<nsIObjectInputStream> stream =
+    NS_NewObjectInputStream(stringStream);
 
   uint32_t type;
   do {
     rv = stream->Read32(&type);
     NS_ENSURE_SUCCESS_VOID(rv);
     if (type == eCustomClipboardTypeId_String) {
       uint32_t formatLength;
       rv = stream->Read32(&formatLength);
--- a/dom/quota/ActorsParent.cpp
+++ b/dom/quota/ActorsParent.cpp
@@ -3,18 +3,18 @@
 /* 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 "ActorsParent.h"
 
 #include "mozIStorageConnection.h"
 #include "mozIStorageService.h"
-#include "nsIBinaryInputStream.h"
-#include "nsIBinaryOutputStream.h"
+#include "nsIObjectInputStream.h"
+#include "nsIObjectOutputStream.h"
 #include "nsIFile.h"
 #include "nsIFileStreams.h"
 #include "nsIObserverService.h"
 #include "nsIPermissionManager.h"
 #include "nsIPrincipal.h"
 #include "nsIRunnable.h"
 #include "nsISimpleEnumerator.h"
 #include "nsIScriptObjectPrincipal.h"
@@ -2135,28 +2135,24 @@ GetBinaryOutputStream(nsIFile* aFile,
   nsCOMPtr<nsIOutputStream> outputStream;
   nsresult rv = GetOutputStream(aFile,
                                 aFileFlag,
                                 getter_AddRefs(outputStream));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  nsCOMPtr<nsIBinaryOutputStream> binaryStream =
-    do_CreateInstance("@mozilla.org/binaryoutputstream;1");
-  if (NS_WARN_IF(!binaryStream)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  rv = binaryStream->SetOutputStream(outputStream);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  binaryStream.forget(aStream);
+  if (NS_WARN_IF(!outputStream)) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  nsCOMPtr<nsIObjectOutputStream> objectOutputStream =
+    NS_NewObjectOutputStream(outputStream);
+
+  objectOutputStream.forget(aStream);
   return NS_OK;
 }
 
 void
 GetJarPrefix(uint32_t aAppId,
              bool aInIsolatedMozBrowser,
              nsACString& aJarPrefix)
 {
--- a/dom/xul/nsXULPrototypeCache.cpp
+++ b/dom/xul/nsXULPrototypeCache.cpp
@@ -394,21 +394,25 @@ nsXULPrototypeCache::FinishInputStream(n
 nsresult
 nsXULPrototypeCache::GetOutputStream(nsIURI* uri, nsIObjectOutputStream** stream)
 {
     nsresult rv;
     nsCOMPtr<nsIObjectOutputStream> objectOutput;
     nsCOMPtr<nsIStorageStream> storageStream;
     bool found = mOutputStreamTable.Get(uri, getter_AddRefs(storageStream));
     if (found) {
-        objectOutput = do_CreateInstance("mozilla.org/binaryoutputstream;1");
-        if (!objectOutput) return NS_ERROR_OUT_OF_MEMORY;
+        // Setting an output stream here causes crashes on Windows. The previous
+        // version of this code always returned NS_ERROR_OUT_OF_MEMORY here,
+        // because it used a mistyped contract ID to create its object stream.
+        return NS_ERROR_NOT_IMPLEMENTED;
+#if 0
         nsCOMPtr<nsIOutputStream> outputStream
             = do_QueryInterface(storageStream);
-        objectOutput->SetOutputStream(outputStream);
+        objectOutput = NS_NewObjectOutputStream(outputStream);
+#endif
     } else {
         rv = NewObjectOutputWrappedStorageStream(getter_AddRefs(objectOutput),
                                                  getter_AddRefs(storageStream),
                                                  false);
         NS_ENSURE_SUCCESS(rv, rv);
         mOutputStreamTable.Put(uri, storageStream);
     }
     objectOutput.forget(stream);
--- a/netwerk/base/nsSerializationHelper.cpp
+++ b/netwerk/base/nsSerializationHelper.cpp
@@ -20,21 +20,17 @@ using namespace mozilla;
 nsresult
 NS_SerializeToString(nsISerializable* obj, nsACString& str)
 {
   RefPtr<nsBase64Encoder> stream(new nsBase64Encoder());
   if (!stream)
     return NS_ERROR_OUT_OF_MEMORY;
 
   nsCOMPtr<nsIObjectOutputStream> objstream =
-      do_CreateInstance("@mozilla.org/binaryoutputstream;1");
-  if (!objstream)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  objstream->SetOutputStream(stream);
+    NS_NewObjectOutputStream(stream);
   nsresult rv =
       objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true);
   NS_ENSURE_SUCCESS(rv, rv);
   return stream->Finish(str);
 }
 
 nsresult
 NS_DeserializeObject(const nsACString& str, nsISupports** obj)
@@ -43,21 +39,17 @@ NS_DeserializeObject(const nsACString& s
   nsresult rv = Base64Decode(str, decodedData);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIInputStream> stream;
   rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIObjectInputStream> objstream =
-      do_CreateInstance("@mozilla.org/binaryinputstream;1");
-  if (!objstream)
-    return NS_ERROR_OUT_OF_MEMORY;
-
-  objstream->SetInputStream(stream);
+    NS_NewObjectInputStream(stream);
   return objstream->ReadObject(true, obj);
 }
 
 NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper)
 
 NS_IMETHODIMP
 nsSerializationHelper::SerializeToString(nsISerializable *serializable,
                                          nsACString & _retval)
--- a/startupcache/StartupCacheUtils.cpp
+++ b/startupcache/StartupCacheUtils.cpp
@@ -6,58 +6,56 @@
 #include "nsIInputStream.h"
 #include "nsIStringStream.h"
 #include "nsNetUtil.h"
 #include "nsIFileURL.h"
 #include "nsIJARURI.h"
 #include "nsIResProtocolHandler.h"
 #include "nsIChromeRegistry.h"
 #include "nsAutoPtr.h"
+#include "nsStringStream.h"
 #include "StartupCacheUtils.h"
 #include "mozilla/scache/StartupCache.h"
 #include "mozilla/Omnijar.h"
 
 namespace mozilla {
 namespace scache {
 
 nsresult
 NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
                                nsIObjectInputStream** stream)
 {
-  nsCOMPtr<nsIStringInputStream> stringStream =
-    do_CreateInstance("@mozilla.org/io/string-input-stream;1");
-  NS_ENSURE_TRUE(stringStream, NS_ERROR_FAILURE);
+  nsCOMPtr<nsIInputStream> stringStream;
+  nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream),
+                                      buffer.release(), len,
+                                      NS_ASSIGNMENT_ADOPT);
+  MOZ_ALWAYS_SUCCEEDS(rv);
 
   nsCOMPtr<nsIObjectInputStream> objectInput =
-    do_CreateInstance("@mozilla.org/binaryinputstream;1");
-  NS_ENSURE_TRUE(objectInput, NS_ERROR_FAILURE);
-
-  stringStream->AdoptData(buffer.release(), len);
-  objectInput->SetInputStream(stringStream);
+    NS_NewObjectInputStream(stringStream);
 
   objectInput.forget(stream);
   return NS_OK;
 }
 
 nsresult
 NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream,
                                     nsIStorageStream** stream,
                                     bool wantDebugStream)
 {
   nsCOMPtr<nsIStorageStream> storageStream;
 
   nsresult rv = NS_NewStorageStream(256, UINT32_MAX, getter_AddRefs(storageStream));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIObjectOutputStream> objectOutput
-    = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
   nsCOMPtr<nsIOutputStream> outputStream
     = do_QueryInterface(storageStream);
 
-  objectOutput->SetOutputStream(outputStream);
+  nsCOMPtr<nsIObjectOutputStream> objectOutput
+    = NS_NewObjectOutputStream(outputStream);
 
 #ifdef DEBUG
   if (wantDebugStream) {
     // Wrap in debug stream to detect unsupported writes of
     // multiply-referenced non-singleton objects
     StartupCache* sc = StartupCache::GetSingleton();
     NS_ENSURE_TRUE(sc, NS_ERROR_UNEXPECTED);
     nsCOMPtr<nsIObjectOutputStream> debugStream;
--- a/xpcom/io/nsBinaryStream.cpp
+++ b/xpcom/io/nsBinaryStream.cpp
@@ -20,32 +20,53 @@
  */
 #include <algorithm>
 #include <string.h>
 
 #include "nsBinaryStream.h"
 
 #include "mozilla/EndianUtils.h"
 #include "mozilla/PodOperations.h"
+#include "mozilla/RefPtr.h"
 #include "mozilla/UniquePtr.h"
 
 #include "nsCRT.h"
 #include "nsString.h"
 #include "nsISerializable.h"
 #include "nsIClassInfo.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIURI.h" // for NS_IURI_IID
 #include "nsIX509Cert.h" // for NS_IX509CERT_IID
 
 #include "jsfriendapi.h"
 
 using mozilla::MakeUnique;
 using mozilla::PodCopy;
 using mozilla::UniquePtr;
 
+already_AddRefed<nsIObjectOutputStream>
+NS_NewObjectOutputStream(nsIOutputStream* aOutputStream)
+{
+  MOZ_ASSERT(aOutputStream);
+  auto stream = mozilla::MakeRefPtr<nsBinaryOutputStream>();
+
+  MOZ_ALWAYS_SUCCEEDS(stream->SetOutputStream(aOutputStream));
+  return stream.forget();
+}
+
+already_AddRefed<nsIObjectInputStream>
+NS_NewObjectInputStream(nsIInputStream* aInputStream)
+{
+  MOZ_ASSERT(aInputStream);
+  auto stream = mozilla::MakeRefPtr<nsBinaryInputStream>();
+
+  MOZ_ALWAYS_SUCCEEDS(stream->SetInputStream(aInputStream));
+  return stream.forget();
+}
+
 NS_IMPL_ISUPPORTS(nsBinaryOutputStream,
                   nsIObjectOutputStream,
                   nsIBinaryOutputStream,
                   nsIOutputStream)
 
 NS_IMETHODIMP
 nsBinaryOutputStream::Flush()
 {
--- a/xpcom/io/nsBinaryStream.h
+++ b/xpcom/io/nsBinaryStream.h
@@ -28,16 +28,18 @@
 class nsBinaryOutputStream final : public nsIObjectOutputStream
 {
 public:
   nsBinaryOutputStream()
   {
   }
 
 protected:
+  friend already_AddRefed<nsIObjectOutputStream> NS_NewObjectOutputStream(nsIOutputStream*);
+
   // nsISupports methods
   NS_DECL_ISUPPORTS
 
   // nsIOutputStream methods
   NS_DECL_NSIOUTPUTSTREAM
 
   // nsIBinaryOutputStream methods
   NS_DECL_NSIBINARYOUTPUTSTREAM
@@ -71,16 +73,18 @@ private:
 class nsBinaryInputStream final : public nsIObjectInputStream
 {
 public:
   nsBinaryInputStream()
   {
   }
 
 protected:
+  friend already_AddRefed<nsIObjectInputStream> NS_NewObjectInputStream(nsIInputStream*);
+
   // nsISupports methods
   NS_DECL_ISUPPORTS
 
   // nsIInputStream methods
   NS_DECL_NSIINPUTSTREAM
 
   // nsIBinaryInputStream methods
   NS_DECL_NSIBINARYINPUTSTREAM
--- a/xpcom/io/nsIObjectInputStream.idl
+++ b/xpcom/io/nsIObjectInputStream.idl
@@ -30,16 +30,19 @@ interface nsIObjectInputStream : nsIBina
      * Optimized deserialization support -- see nsIStreamBufferAccess.idl.
      */
     [notxpcom] charPtr getBuffer(in uint32_t aLength, in uint32_t aAlignMask);
     [notxpcom] void    putBuffer(in charPtr aBuffer, in uint32_t aLength);
 };
 
 %{C++
 
+already_AddRefed<nsIObjectInputStream>
+NS_NewObjectInputStream(nsIInputStream* aOutputStream);
+
 inline nsresult
 NS_ReadOptionalObject(nsIObjectInputStream* aStream, bool aIsStrongRef,
                       nsISupports* *aResult)
 {
     bool nonnull;
     nsresult rv = aStream->ReadBoolean(&nonnull);
     if (NS_SUCCEEDED(rv)) {
         if (nonnull)
--- a/xpcom/io/nsIObjectOutputStream.idl
+++ b/xpcom/io/nsIObjectOutputStream.idl
@@ -53,16 +53,18 @@ interface nsIObjectOutputStream : nsIBin
     /**
      * Optimized serialization support -- see nsIStreamBufferAccess.idl.
      */
     [notxpcom] charPtr getBuffer(in uint32_t aLength, in uint32_t aAlignMask);
     [notxpcom] void    putBuffer(in charPtr aBuffer, in uint32_t aLength);
 };
 
 %{C++
+already_AddRefed<nsIObjectOutputStream>
+NS_NewObjectOutputStream(nsIOutputStream* aOutputStream);
 
 inline nsresult
 NS_WriteOptionalObject(nsIObjectOutputStream* aStream, nsISupports* aObject,
                        bool aIsStrongRef)
 {
     bool nonnull = (aObject != nullptr);
     nsresult rv = aStream->WriteBoolean(nonnull);
     if (NS_SUCCEEDED(rv) && nonnull)