/src/mozilla-central/dom/file/ipc/IPCBlobInputStream.h
Line | Count | Source (jump to first uncovered line) |
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_ipc_IPCBlobInputStream_h |
8 | | #define mozilla_dom_ipc_IPCBlobInputStream_h |
9 | | |
10 | | #include "mozilla/Mutex.h" |
11 | | #include "nsIAsyncInputStream.h" |
12 | | #include "nsICloneableInputStream.h" |
13 | | #include "nsIFileStreams.h" |
14 | | #include "nsIIPCSerializableInputStream.h" |
15 | | #include "nsIInputStreamLength.h" |
16 | | #include "nsCOMPtr.h" |
17 | | |
18 | | namespace mozilla { |
19 | | namespace dom { |
20 | | |
21 | | class IPCBlobInputStreamChild; |
22 | | |
23 | | #define IPCBLOBINPUTSTREAM_IID \ |
24 | | { 0xbcfa38fc, 0x8b7f, 0x4d79, \ |
25 | | { 0xbe, 0x3a, 0x1e, 0x7b, 0xbe, 0x52, 0x38, 0xcd } } |
26 | | |
27 | | class nsIIPCBlobInputStream : public nsISupports |
28 | | { |
29 | | public: |
30 | | NS_DECLARE_STATIC_IID_ACCESSOR(IPCBLOBINPUTSTREAM_IID) |
31 | | |
32 | | virtual nsIInputStream* |
33 | | GetInternalStream() const = 0; |
34 | | }; |
35 | | |
36 | | NS_DEFINE_STATIC_IID_ACCESSOR(nsIIPCBlobInputStream, |
37 | | IPCBLOBINPUTSTREAM_IID) |
38 | | |
39 | | class IPCBlobInputStream final : public nsIAsyncInputStream |
40 | | , public nsIInputStreamCallback |
41 | | , public nsICloneableInputStreamWithRange |
42 | | , public nsIIPCSerializableInputStream |
43 | | , public nsIAsyncFileMetadata |
44 | | , public nsIInputStreamLength |
45 | | , public nsIAsyncInputStreamLength |
46 | | , public nsIIPCBlobInputStream |
47 | | { |
48 | | public: |
49 | | NS_DECL_THREADSAFE_ISUPPORTS |
50 | | NS_DECL_NSIINPUTSTREAM |
51 | | NS_DECL_NSIASYNCINPUTSTREAM |
52 | | NS_DECL_NSIINPUTSTREAMCALLBACK |
53 | | NS_DECL_NSICLONEABLEINPUTSTREAM |
54 | | NS_DECL_NSICLONEABLEINPUTSTREAMWITHRANGE |
55 | | NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM |
56 | | NS_DECL_NSIFILEMETADATA |
57 | | NS_DECL_NSIASYNCFILEMETADATA |
58 | | NS_DECL_NSIINPUTSTREAMLENGTH |
59 | | NS_DECL_NSIASYNCINPUTSTREAMLENGTH |
60 | | |
61 | | explicit IPCBlobInputStream(IPCBlobInputStreamChild* aActor); |
62 | | |
63 | | void |
64 | | StreamReady(already_AddRefed<nsIInputStream> aInputStream); |
65 | | |
66 | | void |
67 | | LengthReady(int64_t aLength); |
68 | | |
69 | | // nsIIPCBlobInputStream |
70 | | nsIInputStream* |
71 | | GetInternalStream() const override |
72 | 0 | { |
73 | 0 | if (mRemoteStream) { |
74 | 0 | return mRemoteStream; |
75 | 0 | } |
76 | 0 | |
77 | 0 | if (mAsyncRemoteStream) { |
78 | 0 | return mAsyncRemoteStream; |
79 | 0 | } |
80 | 0 | |
81 | 0 | return nullptr; |
82 | 0 | } |
83 | | |
84 | | private: |
85 | | ~IPCBlobInputStream(); |
86 | | |
87 | | nsresult |
88 | | EnsureAsyncRemoteStream(const MutexAutoLock& aProofOfLock); |
89 | | |
90 | | void |
91 | | InitWithExistingRange(uint64_t aStart, uint64_t aLength, |
92 | | const MutexAutoLock& aProofOfLock); |
93 | | |
94 | | RefPtr<IPCBlobInputStreamChild> mActor; |
95 | | |
96 | | // This is the list of possible states. |
97 | | enum { |
98 | | // The initial state. Only ::Available() can be used without receiving an |
99 | | // error. The available size is known by the actor. |
100 | | eInit, |
101 | | |
102 | | // AsyncWait() has been called for the first time. SendStreamNeeded() has |
103 | | // been called and we are waiting for the 'real' inputStream. |
104 | | ePending, |
105 | | |
106 | | // When the child receives the stream from the parent, we move to this |
107 | | // state. The received stream is stored in mRemoteStream. From now on, any |
108 | | // method call will be forwared to mRemoteStream. |
109 | | eRunning, |
110 | | |
111 | | // If Close() or CloseWithStatus() is called, we move to this state. |
112 | | // mRemoveStream is released and any method will return |
113 | | // NS_BASE_STREAM_CLOSED. |
114 | | eClosed, |
115 | | } mState; |
116 | | |
117 | | uint64_t mStart; |
118 | | uint64_t mLength; |
119 | | |
120 | | // Set to true if the stream is used via Read/ReadSegments or Close. |
121 | | bool mConsumed; |
122 | | |
123 | | nsCOMPtr<nsIInputStream> mRemoteStream; |
124 | | nsCOMPtr<nsIAsyncInputStream> mAsyncRemoteStream; |
125 | | |
126 | | // These 2 values are set only if mState is ePending. |
127 | | nsCOMPtr<nsIInputStreamCallback> mInputStreamCallback; |
128 | | nsCOMPtr<nsIEventTarget> mInputStreamCallbackEventTarget; |
129 | | |
130 | | // These 2 values are set only if mState is ePending. |
131 | | nsCOMPtr<nsIFileMetadataCallback> mFileMetadataCallback; |
132 | | nsCOMPtr<nsIEventTarget> mFileMetadataCallbackEventTarget; |
133 | | |
134 | | // These 2 values are set only when nsIAsyncInputStreamLength::asyncWait() is |
135 | | // called. |
136 | | nsCOMPtr<nsIInputStreamLengthCallback> mLengthCallback; |
137 | | nsCOMPtr<nsIEventTarget> mLengthCallbackEventTarget; |
138 | | |
139 | | // Any member of this class is protected by mutex because touched on |
140 | | // multiple threads. |
141 | | Mutex mMutex; |
142 | | }; |
143 | | |
144 | | } // namespace dom |
145 | | } // namespace mozilla |
146 | | |
147 | | #endif // mozilla_dom_ipc_IPCBlobInputStream_h |