Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/RecursiveMutex.h"
15
16
class nsIInputStream;
17
class nsILoadGroup;
18
class nsIStreamListener;
19
20
class nsInputStreamPump final : public nsIInputStreamPump,
21
public nsIInputStreamCallback,
22
public nsIThreadRetargetableRequest {
23
~nsInputStreamPump() = default;
24
25
public:
26
typedef mozilla::RecursiveMutexAutoLock RecursiveMutexAutoLock;
27
typedef mozilla::RecursiveMutexAutoUnlock RecursiveMutexAutoUnlock;
28
NS_DECL_THREADSAFE_ISUPPORTS
29
NS_DECL_NSIREQUEST
30
NS_DECL_NSIINPUTSTREAMPUMP
31
NS_DECL_NSIINPUTSTREAMCALLBACK
32
NS_DECL_NSITHREADRETARGETABLEREQUEST
33
34
nsInputStreamPump();
35
36
static nsresult Create(nsInputStreamPump** result, nsIInputStream* stream,
37
uint32_t segsize = 0, uint32_t segcount = 0,
38
bool closeWhenDone = false,
39
nsIEventTarget* mainThreadTarget = nullptr);
40
41
typedef void (*PeekSegmentFun)(void* closure, const uint8_t* buf,
42
uint32_t bufLen);
43
/**
44
* Peek into the first chunk of data that's in the stream. Note that this
45
* method will not call the callback when there is no data in the stream.
46
* The callback will be called at most once.
47
*
48
* The data from the stream will not be consumed, i.e. the pump's listener
49
* can still read all the data.
50
*
51
* Do not call before asyncRead. Do not call after onStopRequest.
52
*/
53
nsresult PeekStream(PeekSegmentFun callback, void* closure);
54
55
/**
56
* Dispatched (to the main thread) by OnStateStop if it's called off main
57
* thread. Updates mState based on return value of OnStateStop.
58
*/
59
nsresult CallOnStateStop();
60
61
protected:
62
enum { STATE_IDLE, STATE_START, STATE_TRANSFER, STATE_STOP };
63
64
nsresult EnsureWaiting();
65
uint32_t OnStateStart();
66
uint32_t OnStateTransfer();
67
uint32_t OnStateStop();
68
nsresult CreateBufferedStreamIfNeeded();
69
70
uint32_t mState;
71
nsCOMPtr<nsILoadGroup> mLoadGroup;
72
nsCOMPtr<nsIStreamListener> mListener;
73
nsCOMPtr<nsIEventTarget> mTargetThread;
74
nsCOMPtr<nsIEventTarget> mLabeledMainThreadTarget;
75
nsCOMPtr<nsIInputStream> mStream;
76
nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
77
uint64_t mStreamOffset;
78
uint64_t mStreamLength;
79
uint32_t mSegSize;
80
uint32_t mSegCount;
81
nsresult mStatus;
82
uint32_t mSuspendCount;
83
uint32_t mLoadFlags;
84
bool mIsPending;
85
// True while in OnInputStreamReady, calling OnStateStart, OnStateTransfer
86
// and OnStateStop. Used to prevent calls to AsyncWait during callbacks.
87
bool mProcessingCallbacks;
88
// True if waiting on the "input stream ready" callback.
89
bool mWaitingForInputStreamReady;
90
bool mCloseWhenDone;
91
bool mRetargeting;
92
bool mAsyncStreamIsBuffered;
93
// Protects state/member var accesses across multiple threads.
94
mozilla::RecursiveMutex mMutex;
95
};
96
97
#endif // !nsInputStreamChannel_h__