|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsInputStreamPump_h__ |
|
7 #define nsInputStreamPump_h__ |
|
8 |
|
9 #include "nsIInputStreamPump.h" |
|
10 #include "nsIAsyncInputStream.h" |
|
11 #include "nsIThreadRetargetableRequest.h" |
|
12 #include "nsCOMPtr.h" |
|
13 #include "mozilla/Attributes.h" |
|
14 #include "mozilla/ReentrantMonitor.h" |
|
15 |
|
16 class nsIInputStream; |
|
17 class nsILoadGroup; |
|
18 class nsIStreamListener; |
|
19 |
|
20 class nsInputStreamPump MOZ_FINAL : public nsIInputStreamPump |
|
21 , public nsIInputStreamCallback |
|
22 , public nsIThreadRetargetableRequest |
|
23 { |
|
24 public: |
|
25 typedef mozilla::ReentrantMonitorAutoEnter ReentrantMonitorAutoEnter; |
|
26 NS_DECL_THREADSAFE_ISUPPORTS |
|
27 NS_DECL_NSIREQUEST |
|
28 NS_DECL_NSIINPUTSTREAMPUMP |
|
29 NS_DECL_NSIINPUTSTREAMCALLBACK |
|
30 NS_DECL_NSITHREADRETARGETABLEREQUEST |
|
31 |
|
32 nsInputStreamPump(); |
|
33 ~nsInputStreamPump(); |
|
34 |
|
35 static NS_HIDDEN_(nsresult) |
|
36 Create(nsInputStreamPump **result, |
|
37 nsIInputStream *stream, |
|
38 int64_t streamPos = -1, |
|
39 int64_t streamLen = -1, |
|
40 uint32_t segsize = 0, |
|
41 uint32_t segcount = 0, |
|
42 bool closeWhenDone = false); |
|
43 |
|
44 typedef void (*PeekSegmentFun)(void *closure, const uint8_t *buf, |
|
45 uint32_t bufLen); |
|
46 /** |
|
47 * Peek into the first chunk of data that's in the stream. Note that this |
|
48 * method will not call the callback when there is no data in the stream. |
|
49 * The callback will be called at most once. |
|
50 * |
|
51 * The data from the stream will not be consumed, i.e. the pump's listener |
|
52 * can still read all the data. |
|
53 * |
|
54 * Do not call before asyncRead. Do not call after onStopRequest. |
|
55 */ |
|
56 NS_HIDDEN_(nsresult) PeekStream(PeekSegmentFun callback, void *closure); |
|
57 |
|
58 /** |
|
59 * Dispatched (to the main thread) by OnStateStop if it's called off main |
|
60 * thread. Updates mState based on return value of OnStateStop. |
|
61 */ |
|
62 nsresult CallOnStateStop(); |
|
63 |
|
64 protected: |
|
65 |
|
66 enum { |
|
67 STATE_IDLE, |
|
68 STATE_START, |
|
69 STATE_TRANSFER, |
|
70 STATE_STOP |
|
71 }; |
|
72 |
|
73 nsresult EnsureWaiting(); |
|
74 uint32_t OnStateStart(); |
|
75 uint32_t OnStateTransfer(); |
|
76 uint32_t OnStateStop(); |
|
77 |
|
78 uint32_t mState; |
|
79 nsCOMPtr<nsILoadGroup> mLoadGroup; |
|
80 nsCOMPtr<nsIStreamListener> mListener; |
|
81 nsCOMPtr<nsISupports> mListenerContext; |
|
82 nsCOMPtr<nsIEventTarget> mTargetThread; |
|
83 nsCOMPtr<nsIInputStream> mStream; |
|
84 nsCOMPtr<nsIAsyncInputStream> mAsyncStream; |
|
85 uint64_t mStreamOffset; |
|
86 uint64_t mStreamLength; |
|
87 uint32_t mSegSize; |
|
88 uint32_t mSegCount; |
|
89 nsresult mStatus; |
|
90 uint32_t mSuspendCount; |
|
91 uint32_t mLoadFlags; |
|
92 bool mIsPending; |
|
93 // True while in OnInputStreamReady, calling OnStateStart, OnStateTransfer |
|
94 // and OnStateStop. Used to prevent calls to AsyncWait during callbacks. |
|
95 bool mProcessingCallbacks; |
|
96 // True if waiting on the "input stream ready" callback. |
|
97 bool mWaitingForInputStreamReady; |
|
98 bool mCloseWhenDone; |
|
99 bool mRetargeting; |
|
100 // Protects state/member var accesses across multiple threads. |
|
101 mozilla::ReentrantMonitor mMonitor; |
|
102 }; |
|
103 |
|
104 #endif // !nsInputStreamChannel_h__ |