ReadStream.h (3373B)
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_dom_cache_ReadStream_h 8 #define mozilla_dom_cache_ReadStream_h 9 10 #include "mozilla/dom/SafeRefPtr.h" 11 #include "mozilla/ipc/FileDescriptor.h" 12 #include "nsCOMPtr.h" 13 #include "nsID.h" 14 #include "nsIInputStream.h" 15 #include "nsISupportsImpl.h" 16 #include "nsTArrayForwardDeclare.h" 17 18 namespace mozilla { 19 class ErrorResult; 20 21 namespace dom::cache { 22 23 class CacheReadStream; 24 class PCacheStreamControlParent; 25 26 // IID for the dom::cache::ReadStream interface 27 #define NS_DOM_CACHE_READSTREAM_IID \ 28 {0x8e5da7c9, 0x0940, 0x4f1d, {0x97, 0x25, 0x5c, 0x59, 0x38, 0xdd, 0xb9, 0x9f}} 29 30 // Custom stream class for Request and Response bodies being read from 31 // a Cache. The main purpose of this class is to report back to the 32 // Cache's Manager when the stream is closed. This allows the Cache to 33 // accurately determine when the underlying body file can be deleted, 34 // etc. 35 // 36 // The ReadStream class also provides us with a convenient QI'able 37 // interface that we can use to pass additional meta-data with the 38 // stream channel. For example, Cache.put() can detect that the content 39 // script is passing a Cache-originated-stream back into the Cache 40 // again. This enables certain optimizations. 41 class ReadStream final : public nsIInputStream { 42 public: 43 // Interface that lets the StreamControl classes interact with 44 // our private inner stream. 45 class Controllable : public AtomicSafeRefCounted<Controllable> { 46 public: 47 virtual ~Controllable() = default; 48 49 // Closes the stream, notifies the stream control, and then forgets 50 // the stream control. 51 virtual void CloseStream() = 0; 52 53 // Closes the stream and then forgets the stream control. Does not 54 // notify. 55 virtual void CloseStreamWithoutReporting() = 0; 56 57 virtual bool HasEverBeenRead() const = 0; 58 59 MOZ_DECLARE_REFCOUNTED_TYPENAME(ReadStream::Controllable); 60 }; 61 62 static already_AddRefed<ReadStream> Create( 63 const Maybe<CacheReadStream>& aMaybeReadStream); 64 65 static already_AddRefed<ReadStream> Create( 66 const CacheReadStream& aReadStream); 67 68 static already_AddRefed<ReadStream> Create( 69 PCacheStreamControlParent* aControl, const nsID& aId, 70 nsIInputStream* aStream); 71 72 void Serialize(Maybe<CacheReadStream>* aReadStreamOut, ErrorResult& aRv); 73 void Serialize(CacheReadStream* aReadStreamOut, ErrorResult& aRv); 74 75 private: 76 class Inner; 77 78 ~ReadStream(); 79 80 // Hold a strong ref to an inner class that actually implements the 81 // majority of the stream logic. Before releasing this ref the outer 82 // ReadStream guarantees it will call Close() on the inner stream. 83 // This is essential for the inner stream to avoid dealing with the 84 // implicit close that can happen when a stream is destroyed. 85 SafeRefPtr<ReadStream::Inner> mInner; 86 87 public: 88 explicit ReadStream(SafeRefPtr<ReadStream::Inner> aInner); 89 90 NS_INLINE_DECL_STATIC_IID(NS_DOM_CACHE_READSTREAM_IID); 91 NS_DECL_THREADSAFE_ISUPPORTS 92 NS_DECL_NSIINPUTSTREAM 93 }; 94 95 } // namespace dom::cache 96 } // namespace mozilla 97 98 #endif // mozilla_dom_cache_ReadStream_h