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 PartiallySeekableInputStream_h
7
#define PartiallySeekableInputStream_h
8
9
#include "mozilla/Attributes.h"
10
#include "mozilla/Mutex.h"
11
#include "nsCOMPtr.h"
12
#include "nsIAsyncInputStream.h"
13
#include "nsICloneableInputStream.h"
14
#include "nsIInputStreamLength.h"
15
#include "nsIIPCSerializableInputStream.h"
16
#include "nsISeekableStream.h"
17
18
namespace mozilla {
19
namespace net {
20
21
// A wrapper for making a stream seekable for the first |aBufferSize| bytes.
22
// Note that this object takes the ownership of the underlying stream.
23
24
class PartiallySeekableInputStream final : public nsISeekableStream,
25
public nsIAsyncInputStream,
26
public nsICloneableInputStream,
27
public nsIIPCSerializableInputStream,
28
public nsIInputStreamCallback,
29
public nsIInputStreamLength,
30
public nsIAsyncInputStreamLength,
31
public nsIInputStreamLengthCallback {
32
public:
33
NS_DECL_THREADSAFE_ISUPPORTS
34
NS_DECL_NSIINPUTSTREAM
35
NS_DECL_NSISEEKABLESTREAM
36
NS_DECL_NSITELLABLESTREAM
37
NS_DECL_NSIASYNCINPUTSTREAM
38
NS_DECL_NSICLONEABLEINPUTSTREAM
39
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
40
NS_DECL_NSIINPUTSTREAMCALLBACK
41
NS_DECL_NSIINPUTSTREAMLENGTH
42
NS_DECL_NSIASYNCINPUTSTREAMLENGTH
43
NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK
44
45
explicit PartiallySeekableInputStream(
46
already_AddRefed<nsIInputStream> aInputStream,
47
uint64_t aBufferSize = 4096);
48
49
private:
50
PartiallySeekableInputStream(
51
already_AddRefed<nsIInputStream> aClonedBaseStream,
52
PartiallySeekableInputStream* aClonedFrom);
53
54
~PartiallySeekableInputStream() = default;
55
56
void Init();
57
58
template <typename M>
59
void SerializeInternal(mozilla::ipc::InputStreamParams& aParams,
60
FileDescriptorArray& aFileDescriptors,
61
bool aDelayedStart, uint32_t aMaxSize,
62
uint32_t* aSizeUsed, M* aManager);
63
64
nsCOMPtr<nsIInputStream> mInputStream;
65
66
// Raw pointers because these are just QI of mInputStream.
67
nsICloneableInputStream* mWeakCloneableInputStream;
68
nsIIPCSerializableInputStream* mWeakIPCSerializableInputStream;
69
nsIAsyncInputStream* mWeakAsyncInputStream;
70
nsIInputStreamLength* mWeakInputStreamLength;
71
nsIAsyncInputStreamLength* mWeakAsyncInputStreamLength;
72
73
// Protected by mutex.
74
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
75
76
// Protected by mutex.
77
nsCOMPtr<nsIInputStreamLengthCallback> mAsyncInputStreamLengthCallback;
78
79
nsTArray<char> mCachedBuffer;
80
81
uint64_t mBufferSize;
82
uint64_t mPos;
83
bool mClosed;
84
85
Mutex mMutex;
86
};
87
88
} // namespace net
89
} // namespace mozilla
90
91
#endif // PartiallySeekableInputStream_h