Bug 1405976 - PartiallySeekableInputStream must take the ownership of the underlying stream, r=qdot
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 13 Oct 2017 10:07:32 +0200
changeset 386131 09796501dad641249aea7702179c5a287d0f773a
parent 386130 b56bf15126d4483075efcb384c8d1c609ea11457
child 386132 c4725ef0925b59ae520b7b0d6b46aadf7a98157a
push id32676
push userarchaeopteryx@coole-files.de
push dateFri, 13 Oct 2017 21:38:18 +0000
treeherdermozilla-central@a31334a65a1c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs1405976
milestone58.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 1405976 - PartiallySeekableInputStream must take the ownership of the underlying stream, r=qdot
netwerk/base/PartiallySeekableInputStream.cpp
netwerk/base/PartiallySeekableInputStream.h
netwerk/protocol/http/HttpBaseChannel.cpp
netwerk/test/gtest/TestPartiallySeekableInputStream.cpp
--- a/netwerk/base/PartiallySeekableInputStream.cpp
+++ b/netwerk/base/PartiallySeekableInputStream.cpp
@@ -23,49 +23,49 @@ NS_INTERFACE_MAP_BEGIN(PartiallySeekable
                                      mWeakIPCSerializableInputStream)
   NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream,
                                      mWeakAsyncInputStream)
   NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamCallback,
                                      mWeakAsyncInputStream)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
 NS_INTERFACE_MAP_END
 
-PartiallySeekableInputStream::PartiallySeekableInputStream(nsIInputStream* aInputStream,
+PartiallySeekableInputStream::PartiallySeekableInputStream(already_AddRefed<nsIInputStream> aInputStream,
                                                            uint64_t aBufferSize)
-  : mInputStream(aInputStream)
+  : mInputStream(Move(aInputStream))
   , mWeakCloneableInputStream(nullptr)
   , mWeakIPCSerializableInputStream(nullptr)
   , mWeakAsyncInputStream(nullptr)
   , mBufferSize(aBufferSize)
   , mPos(0)
   , mClosed(false)
 {
-  MOZ_ASSERT(aInputStream);
+  MOZ_ASSERT(mInputStream);
 
 #ifdef DEBUG
-  nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(aInputStream);
+  nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(mInputStream);
   MOZ_ASSERT(!seekableStream);
 #endif
 
   nsCOMPtr<nsICloneableInputStream> cloneableStream =
-    do_QueryInterface(aInputStream);
-  if (cloneableStream && SameCOMIdentity(aInputStream, cloneableStream)) {
+    do_QueryInterface(mInputStream);
+  if (cloneableStream && SameCOMIdentity(mInputStream, cloneableStream)) {
     mWeakCloneableInputStream = cloneableStream;
   }
 
   nsCOMPtr<nsIIPCSerializableInputStream> serializableStream =
-    do_QueryInterface(aInputStream);
+    do_QueryInterface(mInputStream);
   if (serializableStream &&
-      SameCOMIdentity(aInputStream, serializableStream)) {
+      SameCOMIdentity(mInputStream, serializableStream)) {
     mWeakIPCSerializableInputStream = serializableStream;
   }
 
   nsCOMPtr<nsIAsyncInputStream> asyncInputStream =
-    do_QueryInterface(aInputStream);
-  if (asyncInputStream && SameCOMIdentity(aInputStream, asyncInputStream)) {
+    do_QueryInterface(mInputStream);
+  if (asyncInputStream && SameCOMIdentity(mInputStream, asyncInputStream)) {
     mWeakAsyncInputStream = asyncInputStream;
   }
 }
 
 PartiallySeekableInputStream::~PartiallySeekableInputStream()
 {}
 
 NS_IMETHODIMP
@@ -178,17 +178,17 @@ PartiallySeekableInputStream::Clone(nsII
 
   nsCOMPtr<nsIInputStream> clonedStream;
   nsresult rv = mWeakCloneableInputStream->Clone(getter_AddRefs(clonedStream));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   nsCOMPtr<nsIInputStream> stream =
-    new PartiallySeekableInputStream(clonedStream, mBufferSize);
+    new PartiallySeekableInputStream(clonedStream.forget(), mBufferSize);
 
   stream.forget(aResult);
   return NS_OK;
 }
 
 // nsIAsyncInputStream interface
 
 NS_IMETHODIMP
--- a/netwerk/base/PartiallySeekableInputStream.h
+++ b/netwerk/base/PartiallySeekableInputStream.h
@@ -12,33 +12,34 @@
 #include "nsICloneableInputStream.h"
 #include "nsIIPCSerializableInputStream.h"
 #include "nsISeekableStream.h"
 
 namespace mozilla {
 namespace net {
 
 // A wrapper for making a stream seekable for the first |aBufferSize| bytes.
+// Note that this object takes the ownership of the underlying stream.
 
 class PartiallySeekableInputStream final : public nsISeekableStream
                                          , public nsIAsyncInputStream
                                          , public nsICloneableInputStream
                                          , public nsIIPCSerializableInputStream
                                          , public nsIInputStreamCallback
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIINPUTSTREAM
   NS_DECL_NSISEEKABLESTREAM
   NS_DECL_NSIASYNCINPUTSTREAM
   NS_DECL_NSICLONEABLEINPUTSTREAM
   NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
   NS_DECL_NSIINPUTSTREAMCALLBACK
 
-  explicit PartiallySeekableInputStream(nsIInputStream* aInputStream,
+  explicit PartiallySeekableInputStream(already_AddRefed<nsIInputStream> aInputStream,
                                         uint64_t aBufferSize = 4096);
 
 private:
   ~PartiallySeekableInputStream();
 
   nsCOMPtr<nsIInputStream> mInputStream;
 
   // Raw pointers because these are just QI of mInputStream.
--- a/netwerk/protocol/http/HttpBaseChannel.cpp
+++ b/netwerk/protocol/http/HttpBaseChannel.cpp
@@ -1063,17 +1063,18 @@ HttpBaseChannel::ExplicitSetUploadStream
   // We already have the content length. We don't need to determinate it.
   if (aContentLength > 0) {
     mReqContentLength = aContentLength;
     mReqContentLengthDetermined = true;
   }
 
   nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(aStream);
   if (!seekable) {
-    aStream = new PartiallySeekableInputStream(aStream);
+    nsCOMPtr<nsIInputStream> stream = aStream;
+    aStream = new PartiallySeekableInputStream(stream.forget());
   }
 
   mUploadStream = aStream;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HttpBaseChannel::GetUploadStreamHasHeaders(bool *hasHeaders)
--- a/netwerk/test/gtest/TestPartiallySeekableInputStream.cpp
+++ b/netwerk/test/gtest/TestPartiallySeekableInputStream.cpp
@@ -64,17 +64,17 @@ PartiallySeekableInputStream*
 CreateStream(uint32_t aSize, uint64_t aStreamSize, nsCString& aBuffer)
 {
   aBuffer.SetLength(aSize);
   for (uint32_t i = 0; i < aSize; ++i) {
     aBuffer.BeginWriting()[i] = i % 10;
   }
 
   RefPtr<NonSeekableStream> stream = new NonSeekableStream(aBuffer);
-  return new PartiallySeekableInputStream(stream, aStreamSize);
+  return new PartiallySeekableInputStream(stream.forget(), aStreamSize);
 }
 
 // Simple reading.
 TEST(TestPartiallySeekableInputStream, SimpleRead) {
   const size_t kBufSize = 10;
 
   nsCString buf;
   RefPtr<PartiallySeekableInputStream> psi = CreateStream(kBufSize, 5, buf);