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