Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/base/nsBufferedStreams.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 nsBufferedStreams_h__
7
#define nsBufferedStreams_h__
8
9
#include "nsIBufferedStreams.h"
10
#include "nsIInputStream.h"
11
#include "nsIOutputStream.h"
12
#include "nsISafeOutputStream.h"
13
#include "nsISeekableStream.h"
14
#include "nsIStreamBufferAccess.h"
15
#include "nsCOMPtr.h"
16
#include "nsIIPCSerializableInputStream.h"
17
#include "nsIAsyncInputStream.h"
18
#include "nsICloneableInputStream.h"
19
#include "nsIInputStreamLength.h"
20
#include "mozilla/Mutex.h"
21
22
////////////////////////////////////////////////////////////////////////////////
23
24
class nsBufferedStream : public nsISeekableStream
25
{
26
public:
27
    NS_DECL_THREADSAFE_ISUPPORTS
28
    NS_DECL_NSISEEKABLESTREAM
29
30
    nsBufferedStream();
31
32
    nsresult Close();
33
34
protected:
35
    virtual ~nsBufferedStream();
36
37
    nsresult Init(nsISupports* stream, uint32_t bufferSize);
38
    nsresult GetData(nsISupports **aResult);
39
    NS_IMETHOD Fill() = 0;
40
    NS_IMETHOD Flush() = 0;
41
42
    uint32_t                    mBufferSize;
43
    char*                       mBuffer;
44
45
    // mBufferStartOffset is the offset relative to the start of mStream.
46
    int64_t                     mBufferStartOffset;
47
48
    // mCursor is the read cursor for input streams, or write cursor for
49
    // output streams, and is relative to mBufferStartOffset.
50
    uint32_t                    mCursor;
51
52
    // mFillPoint is the amount available in the buffer for input streams,
53
    // or the high watermark of bytes written into the buffer, and therefore
54
    // is relative to mBufferStartOffset.
55
    uint32_t                    mFillPoint;
56
57
    nsISupports*                mStream;        // cast to appropriate subclass
58
59
    bool                        mBufferDisabled;
60
    bool                        mEOF;  // True if mStream is at EOF
61
    uint8_t                     mGetBufferCount;
62
};
63
64
////////////////////////////////////////////////////////////////////////////////
65
66
class nsBufferedInputStream final
67
    : public nsBufferedStream,
68
      public nsIBufferedInputStream,
69
      public nsIStreamBufferAccess,
70
      public nsIIPCSerializableInputStream,
71
      public nsIAsyncInputStream,
72
      public nsIInputStreamCallback,
73
      public nsICloneableInputStream,
74
      public nsIInputStreamLength,
75
      public nsIAsyncInputStreamLength,
76
      public nsIInputStreamLengthCallback
77
{
78
public:
79
    NS_DECL_ISUPPORTS_INHERITED
80
    NS_DECL_NSIINPUTSTREAM
81
    NS_DECL_NSIBUFFEREDINPUTSTREAM
82
    NS_DECL_NSISTREAMBUFFERACCESS
83
    NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
84
    NS_DECL_NSIASYNCINPUTSTREAM
85
    NS_DECL_NSIINPUTSTREAMCALLBACK
86
    NS_DECL_NSICLONEABLEINPUTSTREAM
87
    NS_DECL_NSIINPUTSTREAMLENGTH
88
    NS_DECL_NSIASYNCINPUTSTREAMLENGTH
89
    NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
90
91
    nsBufferedInputStream();
92
93
    static nsresult
94
    Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
95
96
0
    nsIInputStream* Source() {
97
0
        return (nsIInputStream*)mStream;
98
0
    }
99
100
protected:
101
0
    virtual ~nsBufferedInputStream() = default;
102
103
    NS_IMETHOD Fill() override;
104
0
    NS_IMETHOD Flush() override { return NS_OK; } // no-op for input streams
105
106
    mozilla::Mutex mMutex;
107
108
    // This value is protected by mutex.
109
    nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
110
111
    // This value is protected by mutex.
112
    nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
113
114
    bool mIsIPCSerializable;
115
    bool mIsAsyncInputStream;
116
    bool mIsCloneableInputStream;
117
    bool mIsInputStreamLength;
118
    bool mIsAsyncInputStreamLength;
119
};
120
121
////////////////////////////////////////////////////////////////////////////////
122
123
class nsBufferedOutputStream  : public nsBufferedStream,
124
                                public nsISafeOutputStream,
125
                                public nsIBufferedOutputStream,
126
                                public nsIStreamBufferAccess
127
{
128
public:
129
    NS_DECL_ISUPPORTS_INHERITED
130
    NS_DECL_NSIOUTPUTSTREAM
131
    NS_DECL_NSISAFEOUTPUTSTREAM
132
    NS_DECL_NSIBUFFEREDOUTPUTSTREAM
133
    NS_DECL_NSISTREAMBUFFERACCESS
134
135
0
    nsBufferedOutputStream() : nsBufferedStream() {}
136
137
    static nsresult
138
    Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
139
140
0
    nsIOutputStream* Sink() {
141
0
        return (nsIOutputStream*)mStream;
142
0
    }
143
144
protected:
145
0
    virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); }
146
147
0
    NS_IMETHOD Fill() override { return NS_OK; } // no-op for output streams
148
149
    nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream
150
};
151
152
////////////////////////////////////////////////////////////////////////////////
153
154
#endif // nsBufferedStreams_h__