Coverage Report

Created: 2018-09-25 14:53

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