Line data Source code
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/ipc/FileDescriptor.h"
11 : #include "nsCOMPtr.h"
12 : #include "nsID.h"
13 : #include "nsIInputStream.h"
14 : #include "nsISupportsImpl.h"
15 : #include "mozilla/RefPtr.h"
16 : #include "nsTArrayForwardDeclare.h"
17 :
18 : namespace mozilla {
19 : namespace ipc {
20 : class AutoIPCStream;
21 : } // namespace ipc
22 : namespace dom {
23 : namespace cache {
24 :
25 : class CacheReadStream;
26 : class CacheReadStreamOrVoid;
27 : class PCacheStreamControlParent;
28 :
29 : // IID for the dom::cache::ReadStream interface
30 : #define NS_DOM_CACHE_READSTREAM_IID \
31 : {0x8e5da7c9, 0x0940, 0x4f1d, \
32 : {0x97, 0x25, 0x5c, 0x59, 0x38, 0xdd, 0xb9, 0x9f}}
33 :
34 :
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,
39 : // etc.
40 : //
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
47 : {
48 : public:
49 : // Interface that lets the StreamControl classes interact with
50 : // our private inner stream.
51 0 : class Controllable
52 : {
53 : public:
54 : // Closes the stream, notifies the stream control, and then forgets
55 : // the stream control.
56 : virtual void
57 : CloseStream() = 0;
58 :
59 : // Closes the stream and then forgets the stream control. Does not
60 : // notify.
61 : virtual void
62 : CloseStreamWithoutReporting() = 0;
63 :
64 : virtual bool
65 : MatchId(const nsID& aId) const = 0;
66 :
67 : virtual bool
68 : HasEverBeenRead() const = 0;
69 :
70 : NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
71 : };
72 :
73 : static already_AddRefed<ReadStream>
74 : Create(const CacheReadStreamOrVoid& aReadStreamOrVoid);
75 :
76 : static already_AddRefed<ReadStream>
77 : Create(const CacheReadStream& aReadStream);
78 :
79 : static already_AddRefed<ReadStream>
80 : Create(PCacheStreamControlParent* aControl, const nsID& aId,
81 : nsIInputStream* aStream);
82 :
83 : void Serialize(CacheReadStreamOrVoid* aReadStreamOut,
84 : nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
85 : ErrorResult& aRv);
86 : void Serialize(CacheReadStream* aReadStreamOut,
87 : nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
88 : ErrorResult& aRv);
89 :
90 : private:
91 : class Inner;
92 :
93 : explicit ReadStream(Inner* aInner);
94 : ~ReadStream();
95 :
96 : // Hold a strong ref to an inner class that actually implements the
97 : // majority of the stream logic. Before releasing this ref the outer
98 : // ReadStream guarantees it will call Close() on the inner stream.
99 : // This is essential for the inner stream to avoid dealing with the
100 : // implicit close that can happen when a stream is destroyed.
101 : RefPtr<Inner> mInner;
102 :
103 : public:
104 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_CACHE_READSTREAM_IID);
105 : NS_DECL_THREADSAFE_ISUPPORTS
106 : NS_DECL_NSIINPUTSTREAM
107 : };
108 :
109 : NS_DEFINE_STATIC_IID_ACCESSOR(ReadStream, NS_DOM_CACHE_READSTREAM_IID);
110 :
111 : } // namespace cache
112 : } // namespace dom
113 : } // namespace mozilla
114 :
115 : #endif // mozilla_dom_cache_ReadStream_h
|