Bug 1353629 - PBlob refactoring - part 10 - Make nsFileInputStream cloneable, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 24 Apr 2017 12:09:41 +0200
changeset 354543 1c786e61d5c88e404cc47a2a92cea86028235ba2
parent 354542 04d9349c61846108e9b2b99b207adaecdd366042
child 354544 a2d2d49d3e5774f788787cd375e46a69be5cf011
push id89486
push useramarchesini@mozilla.com
push dateMon, 24 Apr 2017 10:10:00 +0000
treeherdermozilla-inbound@6e18a14aa213 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1353629
milestone55.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 1353629 - PBlob refactoring - part 10 - Make nsFileInputStream cloneable, r=smaug This is important to avoid extra copy when IPCInputStreamStorage::GetStream() returns a file inputStream
netwerk/base/nsFileStreams.cpp
netwerk/base/nsFileStreams.h
--- a/netwerk/base/nsFileStreams.cpp
+++ b/netwerk/base/nsFileStreams.cpp
@@ -392,16 +392,17 @@ NS_IMPL_CLASSINFO(nsFileInputStream, nul
                   NS_LOCALFILEINPUTSTREAM_CID)
 
 NS_INTERFACE_MAP_BEGIN(nsFileInputStream)
     NS_INTERFACE_MAP_ENTRY(nsIInputStream)
     NS_INTERFACE_MAP_ENTRY(nsIFileInputStream)
     NS_INTERFACE_MAP_ENTRY(nsILineInputStream)
     NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
     NS_IMPL_QUERY_CLASSINFO(nsFileInputStream)
+    NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream, IsCloneable())
 NS_INTERFACE_MAP_END_INHERITING(nsFileStreamBase)
 
 NS_IMPL_CI_INTERFACE_GETTER(nsFileInputStream,
                             nsIInputStream,
                             nsIFileInputStream,
                             nsISeekableStream,
                             nsILineInputStream)
 
@@ -677,16 +678,37 @@ nsFileInputStream::Deserialize(const Inp
 }
 
 Maybe<uint64_t>
 nsFileInputStream::ExpectedSerializedLength()
 {
     return Nothing();
 }
 
+bool
+nsFileInputStream::IsCloneable() const
+{
+    return XRE_IsParentProcess();
+}
+
+NS_IMETHODIMP
+nsFileInputStream::GetCloneable(bool* aCloneable)
+{
+    *aCloneable = IsCloneable();
+    return NS_OK;
+}
+
+NS_IMETHODIMP
+nsFileInputStream::Clone(nsIInputStream** aResult)
+{
+    MOZ_ASSERT(IsCloneable());
+    return NS_NewLocalFileInputStream(aResult, mFile, mIOFlags, mPerm,
+                                      mBehaviorFlags);
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // nsFileOutputStream
 
 NS_IMPL_ISUPPORTS_INHERITED(nsFileOutputStream,
                             nsFileStreamBase,
                             nsIOutputStream,
                             nsIFileOutputStream)
 
--- a/netwerk/base/nsFileStreams.h
+++ b/netwerk/base/nsFileStreams.h
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsFileStreams_h__
 #define nsFileStreams_h__
 
 #include "nsAutoPtr.h"
 #include "nsIFileStreams.h"
 #include "nsIFile.h"
+#include "nsICloneableInputStream.h"
 #include "nsIInputStream.h"
 #include "nsIOutputStream.h"
 #include "nsISafeOutputStream.h"
 #include "nsISeekableStream.h"
 #include "nsILineInputStream.h"
 #include "nsCOMPtr.h"
 #include "nsIIPCSerializableInputStream.h"
 #include "nsReadLine.h"
@@ -108,26 +109,31 @@ protected:
      * nothing. If the return value is not NS_OK, please, return it back to the
      * callee.
      */
     inline nsresult DoPendingOpen();
 };
 
 ////////////////////////////////////////////////////////////////////////////////
 
-class nsFileInputStream : public nsFileStreamBase,
-                          public nsIFileInputStream,
-                          public nsILineInputStream,
-                          public nsIIPCSerializableInputStream
+// nsFileInputStream is cloneable only on the parent process because only there
+// it can open the same file multiple times.
+
+class nsFileInputStream : public nsFileStreamBase
+                        , public nsIFileInputStream
+                        , public nsILineInputStream
+                        , public nsIIPCSerializableInputStream
+                        , public nsICloneableInputStream
 {
 public:
     NS_DECL_ISUPPORTS_INHERITED
     NS_DECL_NSIFILEINPUTSTREAM
     NS_DECL_NSILINEINPUTSTREAM
     NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
+    NS_DECL_NSICLONEABLEINPUTSTREAM
 
     NS_IMETHOD Close() override;
     NS_IMETHOD Tell(int64_t *aResult) override;
     NS_IMETHOD Available(uint64_t* _retval) override;
     NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* _retval) override;
     NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void *aClosure,
                             uint32_t aCount, uint32_t* _retval) override
     {
@@ -178,16 +184,18 @@ protected:
     int64_t mCachedPosition;
 
 protected:
     /**
      * Internal, called to open a file.  Parameters are the same as their
      * Init() analogues.
      */
     nsresult Open(nsIFile* file, int32_t ioFlags, int32_t perm);
+
+    bool IsCloneable() const;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
 
 class nsFileOutputStream : public nsFileStreamBase,
                            public nsIFileOutputStream
 {
 public: