1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsPreloadedStream.h"
#include "nsIRunnable.h"
#include "nsThreadUtils.h"
#include <algorithm>
namespace mozilla {
namespace net {
NS_IMPL_ISUPPORTS(nsPreloadedStream, nsIInputStream, nsIAsyncInputStream,
nsIInputStreamCallback)
nsPreloadedStream::nsPreloadedStream(nsIAsyncInputStream* aStream,
const char* data, uint32_t datalen)
: mStream(aStream),
mOffset(0),
mLen(datalen),
mCallback("nsPreloadedStream") {
mBuf = (char*)moz_xmalloc(datalen);
memcpy(mBuf, data, datalen);
}
nsPreloadedStream::~nsPreloadedStream() { free(mBuf); }
NS_IMETHODIMP
nsPreloadedStream::Close() {
mLen = 0;
return mStream->Close();
}
NS_IMETHODIMP
nsPreloadedStream::Available(uint64_t* _retval) {
uint64_t avail = 0;
nsresult rv = mStream->Available(&avail);
if (NS_FAILED(rv)) return rv;
*_retval = avail + mLen;
return NS_OK;
}
NS_IMETHODIMP
nsPreloadedStream::StreamStatus() { return mStream->StreamStatus(); }
NS_IMETHODIMP
nsPreloadedStream::Read(char* aBuf, uint32_t aCount, uint32_t* _retval) {
if (!mLen) return mStream->Read(aBuf, aCount, _retval);
uint32_t toRead = std::min(mLen, aCount);
memcpy(aBuf, mBuf + mOffset, toRead);
mOffset += toRead;
mLen -= toRead;
*_retval = toRead;
return NS_OK;
}
NS_IMETHODIMP
nsPreloadedStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
uint32_t aCount, uint32_t* result) {
if (!mLen) return mStream->ReadSegments(aWriter, aClosure, aCount, result);
*result = 0;
while (mLen > 0 && aCount > 0) {
uint32_t toRead = std::min(mLen, aCount);
uint32_t didRead = 0;
nsresult rv;
rv = aWriter(this, aClosure, mBuf + mOffset, *result, toRead, &didRead);
if (NS_FAILED(rv)) return NS_OK;
*result += didRead;
mOffset += didRead;
mLen -= didRead;
aCount -= didRead;
}
return NS_OK;
}
NS_IMETHODIMP
nsPreloadedStream::IsNonBlocking(bool* _retval) {
return mStream->IsNonBlocking(_retval);
}
NS_IMETHODIMP
nsPreloadedStream::CloseWithStatus(nsresult aStatus) {
mLen = 0;
return mStream->CloseWithStatus(aStatus);
}
class RunOnThread : public Runnable {
public:
RunOnThread(nsIAsyncInputStream* aStream, nsIInputStreamCallback* aCallback)
: Runnable("net::RunOnThread"), mStream(aStream), mCallback(aCallback) {}
virtual ~RunOnThread() = default;
NS_IMETHOD Run() override {
mCallback->OnInputStreamReady(mStream);
return NS_OK;
}
private:
nsCOMPtr<nsIAsyncInputStream> mStream;
nsCOMPtr<nsIInputStreamCallback> mCallback;
};
NS_IMETHODIMP
nsPreloadedStream::AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags,
uint32_t aRequestedCount,
nsIEventTarget* aEventTarget) {
if (!mLen) {
{
auto lock = mCallback.Lock();
*lock = aCallback;
}
return mStream->AsyncWait(aCallback ? this : nullptr, aFlags,
aRequestedCount, aEventTarget);
}
if (!aCallback) return NS_OK;
if (!aEventTarget) return aCallback->OnInputStreamReady(this);
nsCOMPtr<nsIRunnable> event = new RunOnThread(this, aCallback);
return aEventTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
}
NS_IMETHODIMP
nsPreloadedStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
nsCOMPtr<nsIInputStreamCallback> callback;
{
auto lock = mCallback.Lock();
callback = lock->forget();
}
if (callback) {
return callback->OnInputStreamReady(this);
}
return NS_OK;
}
} // namespace net
} // namespace mozilla
|