Bug 1340921 - Introduce PMemoryStream for having PBlob and Multi-e10s happy - part 1 - Expose DataOwnerAdapter, r=mrbkap a=gchang
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 29 Mar 2017 10:40:38 +0200
changeset 395618 e9e6146c820b389b6c31c11062cf831954b37998
parent 395617 0032f065b456fef31bc6deb4ead372dd8faa9611
child 395619 05da998218c41edc200f71692e030057ee1e4935
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap, gchang
bugs1340921
milestone54.0a2
Bug 1340921 - Introduce PMemoryStream for having PBlob and Multi-e10s happy - part 1 - Expose DataOwnerAdapter, r=mrbkap a=gchang
dom/file/MemoryBlobImpl.cpp
dom/file/MemoryBlobImpl.h
--- a/dom/file/MemoryBlobImpl.cpp
+++ b/dom/file/MemoryBlobImpl.cpp
@@ -2,95 +2,51 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "MemoryBlobImpl.h"
 #include "mozilla/IntegerPrintfMacros.h"
 #include "mozilla/SHA1.h"
-#include "nsIIPCSerializableInputStream.h"
 #include "nsPrintfCString.h"
 
 namespace mozilla {
 namespace dom {
 
-// XXXkhuey the input stream that we pass out of a File
-// can outlive the actual File object.  Thus, we must
-// ensure that the buffer underlying the stream we get
-// from NS_NewByteInputStream is held alive as long as the
-// stream is.  We do that by passing back this class instead.
-class DataOwnerAdapter final : public nsIInputStream,
-                               public nsISeekableStream,
-                               public nsIIPCSerializableInputStream
-{
-  typedef MemoryBlobImpl::DataOwner DataOwner;
-public:
-  static nsresult Create(DataOwner* aDataOwner,
-                         uint32_t aStart,
-                         uint32_t aLength,
-                         nsIInputStream** _retval);
-
-  NS_DECL_THREADSAFE_ISUPPORTS
-
-  // These are mandatory.
-  NS_FORWARD_NSIINPUTSTREAM(mStream->)
-  NS_FORWARD_NSISEEKABLESTREAM(mSeekableStream->)
+NS_IMPL_ADDREF(MemoryBlobImpl::DataOwnerAdapter)
+NS_IMPL_RELEASE(MemoryBlobImpl::DataOwnerAdapter)
 
-  // This is optional. We use a conditional QI to keep it from being called
-  // if the underlying stream doesn't support it.
-  NS_FORWARD_NSIIPCSERIALIZABLEINPUTSTREAM(mSerializableInputStream->)
-
-private:
-  ~DataOwnerAdapter() {}
-
-  DataOwnerAdapter(DataOwner* aDataOwner,
-                   nsIInputStream* aStream)
-    : mDataOwner(aDataOwner), mStream(aStream),
-      mSeekableStream(do_QueryInterface(aStream)),
-      mSerializableInputStream(do_QueryInterface(aStream))
-  {
-    MOZ_ASSERT(mSeekableStream, "Somebody gave us the wrong stream!");
-  }
-
-  RefPtr<DataOwner> mDataOwner;
-  nsCOMPtr<nsIInputStream> mStream;
-  nsCOMPtr<nsISeekableStream> mSeekableStream;
-  nsCOMPtr<nsIIPCSerializableInputStream> mSerializableInputStream;
-};
-
-NS_IMPL_ADDREF(DataOwnerAdapter)
-NS_IMPL_RELEASE(DataOwnerAdapter)
-
-NS_INTERFACE_MAP_BEGIN(DataOwnerAdapter)
+NS_INTERFACE_MAP_BEGIN(MemoryBlobImpl::DataOwnerAdapter)
   NS_INTERFACE_MAP_ENTRY(nsIInputStream)
   NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
   NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
                                      mSerializableInputStream)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
 NS_INTERFACE_MAP_END
 
-nsresult DataOwnerAdapter::Create(DataOwner* aDataOwner,
-                                  uint32_t aStart,
-                                  uint32_t aLength,
-                                  nsIInputStream** _retval)
+nsresult MemoryBlobImpl::DataOwnerAdapter::Create(DataOwner* aDataOwner,
+                                                  uint32_t aStart,
+                                                  uint32_t aLength,
+                                                  nsIInputStream** _retval)
 {
   nsresult rv;
   MOZ_ASSERT(aDataOwner, "Uh ...");
 
   nsCOMPtr<nsIInputStream> stream;
 
   rv = NS_NewByteInputStream(getter_AddRefs(stream),
                              static_cast<const char*>(aDataOwner->mData) +
                              aStart,
                              (int32_t)aLength,
                              NS_ASSIGNMENT_DEPEND);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  NS_ADDREF(*_retval = new DataOwnerAdapter(aDataOwner, stream));
+  NS_ADDREF(*_retval =
+              new MemoryBlobImpl::DataOwnerAdapter(aDataOwner, stream));
 
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS_INHERITED0(MemoryBlobImpl, BlobImpl)
 
 already_AddRefed<BlobImpl>
 MemoryBlobImpl::CreateSlice(uint64_t aStart, uint64_t aLength,
@@ -105,17 +61,18 @@ MemoryBlobImpl::CreateSlice(uint64_t aSt
 void
 MemoryBlobImpl::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
 {
   if (mLength > INT32_MAX) {
     aRv.Throw(NS_ERROR_FAILURE);
     return;
   }
 
-  aRv = DataOwnerAdapter::Create(mDataOwner, mStart, mLength, aStream);
+  aRv = MemoryBlobImpl::DataOwnerAdapter::Create(mDataOwner, mStart, mLength,
+                                                 aStream);
 }
 
 /* static */ StaticMutex
 MemoryBlobImpl::DataOwner::sDataOwnerMutex;
 
 /* static */ StaticAutoPtr<LinkedList<MemoryBlobImpl::DataOwner>>
 MemoryBlobImpl::DataOwner::sDataOwners;
 
--- a/dom/file/MemoryBlobImpl.h
+++ b/dom/file/MemoryBlobImpl.h
@@ -6,17 +6,20 @@
 
 #ifndef mozilla_dom_MemoryBlobImpl_h
 #define mozilla_dom_MemoryBlobImpl_h
 
 #include "mozilla/dom/BaseBlobImpl.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/StaticMutex.h"
 #include "mozilla/StaticPtr.h"
+#include "nsIInputStream.h"
+#include "nsIIPCSerializableInputStream.h"
 #include "nsIMemoryReporter.h"
+#include "nsISeekableStream.h"
 
 namespace mozilla {
 namespace dom {
 
 class MemoryBlobImpl final : public BaseBlobImpl
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
@@ -89,16 +92,55 @@ public:
     static mozilla::StaticMutex sDataOwnerMutex;
     static mozilla::StaticAutoPtr<mozilla::LinkedList<DataOwner> > sDataOwners;
     static bool sMemoryReporterRegistered;
 
     void* mData;
     uint64_t mLength;
   };
 
+  class DataOwnerAdapter final : public nsIInputStream,
+                                 public nsISeekableStream,
+                                 public nsIIPCSerializableInputStream
+  {
+    typedef MemoryBlobImpl::DataOwner DataOwner;
+  public:
+    static nsresult Create(DataOwner* aDataOwner,
+                           uint32_t aStart,
+                           uint32_t aLength,
+                           nsIInputStream** _retval);
+
+    NS_DECL_THREADSAFE_ISUPPORTS
+
+    // These are mandatory.
+    NS_FORWARD_NSIINPUTSTREAM(mStream->)
+    NS_FORWARD_NSISEEKABLESTREAM(mSeekableStream->)
+
+    // This is optional. We use a conditional QI to keep it from being called
+    // if the underlying stream doesn't support it.
+    NS_FORWARD_NSIIPCSERIALIZABLEINPUTSTREAM(mSerializableInputStream->)
+
+  private:
+    ~DataOwnerAdapter() {}
+
+    DataOwnerAdapter(DataOwner* aDataOwner,
+                     nsIInputStream* aStream)
+      : mDataOwner(aDataOwner), mStream(aStream),
+        mSeekableStream(do_QueryInterface(aStream)),
+        mSerializableInputStream(do_QueryInterface(aStream))
+    {
+      MOZ_ASSERT(mSeekableStream, "Somebody gave us the wrong stream!");
+    }
+
+    RefPtr<DataOwner> mDataOwner;
+    nsCOMPtr<nsIInputStream> mStream;
+    nsCOMPtr<nsISeekableStream> mSeekableStream;
+    nsCOMPtr<nsIIPCSerializableInputStream> mSerializableInputStream;
+  };
+
 private:
   // Create slice
   MemoryBlobImpl(const MemoryBlobImpl* aOther, uint64_t aStart,
                  uint64_t aLength, const nsAString& aContentType)
     : BaseBlobImpl(aContentType, aOther->mStart + aStart, aLength)
     , mDataOwner(aOther->mDataOwner)
   {
     MOZ_ASSERT(mDataOwner && mDataOwner->mData, "must have data");