1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_RemoteLazyInputStream_h 8 #define mozilla_RemoteLazyInputStream_h 9 10 #include "mozilla/Mutex.h" 11 #include "mozIRemoteLazyInputStream.h" 12 #include "nsIAsyncInputStream.h" 13 #include "nsICloneableInputStream.h" 14 #include "nsIFileStreams.h" 15 #include "nsIIPCSerializableInputStream.h" 16 #include "nsIInputStreamLength.h" 17 #include "nsCOMPtr.h" 18 19 namespace mozilla { 20 21 class RemoteLazyInputStreamChild; 22 23 class RemoteLazyInputStream final : public nsIAsyncInputStream, 24 public nsIInputStreamCallback, 25 public nsICloneableInputStreamWithRange, 26 public nsIIPCSerializableInputStream, 27 public nsIAsyncFileMetadata, 28 public nsIInputStreamLength, 29 public nsIAsyncInputStreamLength, 30 public mozIRemoteLazyInputStream { 31 public: 32 NS_DECL_THREADSAFE_ISUPPORTS 33 NS_DECL_NSIINPUTSTREAM 34 NS_DECL_NSIASYNCINPUTSTREAM 35 NS_DECL_NSIINPUTSTREAMCALLBACK 36 NS_DECL_NSICLONEABLEINPUTSTREAM 37 NS_DECL_NSICLONEABLEINPUTSTREAMWITHRANGE 38 NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM 39 NS_DECL_NSIFILEMETADATA 40 NS_DECL_NSIASYNCFILEMETADATA 41 NS_DECL_NSIINPUTSTREAMLENGTH 42 NS_DECL_NSIASYNCINPUTSTREAMLENGTH 43 44 explicit RemoteLazyInputStream(RemoteLazyInputStreamChild* aActor); 45 46 void StreamReady(already_AddRefed<nsIInputStream> aInputStream); 47 48 void LengthReady(int64_t aLength); 49 50 // mozIRemoteLazyInputStream GetInternalStream()51 NS_IMETHOD_(nsIInputStream*) GetInternalStream() override { 52 if (mRemoteStream) { 53 return mRemoteStream; 54 } 55 56 if (mAsyncRemoteStream) { 57 return mAsyncRemoteStream; 58 } 59 60 return nullptr; 61 } 62 63 private: 64 ~RemoteLazyInputStream(); 65 66 nsresult EnsureAsyncRemoteStream(const MutexAutoLock& aProofOfLock); 67 68 void InitWithExistingRange(uint64_t aStart, uint64_t aLength, 69 const MutexAutoLock& aProofOfLock); 70 71 RefPtr<RemoteLazyInputStreamChild> mActor; 72 73 // This is the list of possible states. 74 enum { 75 // The initial state. Only ::Available() can be used without receiving an 76 // error. The available size is known by the actor. 77 eInit, 78 79 // AsyncWait() has been called for the first time. SendStreamNeeded() has 80 // been called and we are waiting for the 'real' inputStream. 81 ePending, 82 83 // When the child receives the stream from the parent, we move to this 84 // state. The received stream is stored in mRemoteStream. From now on, any 85 // method call will be forwared to mRemoteStream. 86 eRunning, 87 88 // If Close() or CloseWithStatus() is called, we move to this state. 89 // mRemoveStream is released and any method will return 90 // NS_BASE_STREAM_CLOSED. 91 eClosed, 92 } mState; 93 94 uint64_t mStart; 95 uint64_t mLength; 96 97 // Set to true if the stream is used via Read/ReadSegments or Close. 98 bool mConsumed; 99 100 nsCOMPtr<nsIInputStream> mRemoteStream; 101 nsCOMPtr<nsIAsyncInputStream> mAsyncRemoteStream; 102 103 // These 2 values are set only if mState is ePending. 104 nsCOMPtr<nsIInputStreamCallback> mInputStreamCallback; 105 nsCOMPtr<nsIEventTarget> mInputStreamCallbackEventTarget; 106 107 // These 2 values are set only if mState is ePending. 108 nsCOMPtr<nsIFileMetadataCallback> mFileMetadataCallback; 109 nsCOMPtr<nsIEventTarget> mFileMetadataCallbackEventTarget; 110 111 // These 2 values are set only when nsIAsyncInputStreamLength::asyncWait() is 112 // called. 113 nsCOMPtr<nsIInputStreamLengthCallback> mLengthCallback; 114 nsCOMPtr<nsIEventTarget> mLengthCallbackEventTarget; 115 116 // Any member of this class is protected by mutex because touched on 117 // multiple threads. 118 Mutex mMutex; 119 }; 120 121 } // namespace mozilla 122 123 #endif // mozilla_RemoteLazyInputStream_h 124