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 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
public:
26
NS_DECL_THREADSAFE_ISUPPORTS
27
NS_DECL_NSISEEKABLESTREAM
28
NS_DECL_NSITELLABLESTREAM
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
nsCOMPtr<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 : public nsBufferedStream,
67
public nsIBufferedInputStream,
68
public nsIStreamBufferAccess,
69
public nsIIPCSerializableInputStream,
70
public nsIAsyncInputStream,
71
public nsIInputStreamCallback,
72
public nsICloneableInputStream,
73
public nsIInputStreamLength,
74
public nsIAsyncInputStreamLength,
75
public nsIInputStreamLengthCallback {
76
public:
77
NS_DECL_ISUPPORTS_INHERITED
78
NS_DECL_NSIINPUTSTREAM
79
NS_DECL_NSIBUFFEREDINPUTSTREAM
80
NS_DECL_NSISTREAMBUFFERACCESS
81
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
82
NS_DECL_NSIASYNCINPUTSTREAM
83
NS_DECL_NSIINPUTSTREAMCALLBACK
84
NS_DECL_NSICLONEABLEINPUTSTREAM
85
NS_DECL_NSIINPUTSTREAMLENGTH
86
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
87
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
88
89
nsBufferedInputStream();
90
91
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
92
93
nsIInputStream* Source() { return (nsIInputStream*)mStream.get(); }
94
95
protected:
96
virtual ~nsBufferedInputStream() = default;
97
98
template <typename M>
99
void SerializeInternal(mozilla::ipc::InputStreamParams& aParams,
100
FileDescriptorArray& aFileDescriptors,
101
bool aDelayedStart, uint32_t aMaxSize,
102
uint32_t* aSizeUsed, M* aManager);
103
104
NS_IMETHOD Fill() override;
105
NS_IMETHOD Flush() override { return NS_OK; } // no-op for input streams
106
107
mozilla::Mutex mMutex;
108
109
// This value is protected by mutex.
110
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
111
112
// This value is protected by mutex.
113
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
114
115
bool mIsIPCSerializable;
116
bool mIsAsyncInputStream;
117
bool mIsCloneableInputStream;
118
bool mIsInputStreamLength;
119
bool mIsAsyncInputStreamLength;
120
};
121
122
////////////////////////////////////////////////////////////////////////////////
123
124
class nsBufferedOutputStream : public nsBufferedStream,
125
public nsISafeOutputStream,
126
public nsIBufferedOutputStream,
127
public nsIStreamBufferAccess {
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
nsBufferedOutputStream() : nsBufferedStream() {}
136
137
static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
138
139
nsIOutputStream* Sink() { return (nsIOutputStream*)mStream.get(); }
140
141
protected:
142
virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); }
143
144
NS_IMETHOD Fill() override { return NS_OK; } // no-op for output streams
145
146
nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream
147
};
148
149
////////////////////////////////////////////////////////////////////////////////
150
151
#endif // nsBufferedStreams_h__