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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "EmptyBlobImpl.h"
#include "mozilla/InputStreamLengthWrapper.h"
#include "mozilla/SlicedInputStream.h"
#include "StreamBlobImpl.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"
#include "nsStreamUtils.h"
#include "nsStringStream.h"
#include "nsIAsyncOutputStream.h"
#include "nsICloneableInputStream.h"
#include "nsIEventTarget.h"
#include "nsIPipe.h"
#include "js/GCAPI.h"
namespace mozilla::dom {
NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)
static already_AddRefed<nsICloneableInputStream> EnsureCloneableStream(
nsIInputStream* aInputStream, uint64_t aLength) {
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(aInputStream);
if (cloneable && cloneable->GetCloneable()) {
return cloneable.forget();
}
// If the stream we're copying is known to be small, specify the size of the
// pipe's segments precisely to limit wasted space. An extra byte above length
// is included to avoid allocating an extra segment immediately before reading
// EOF from the source stream. Otherwise, allocate 64k buffers rather than
// the default of 4k buffers to reduce segment counts for very large payloads.
static constexpr uint32_t kBaseSegmentSize = 64 * 1024;
uint32_t segmentSize = kBaseSegmentSize;
if (aLength + 1 <= kBaseSegmentSize * 4) {
segmentSize = aLength + 1;
}
// NOTE: We specify unlimited segments to eagerly build a complete copy of the
// source stream locally without waiting for the blob to be consumed.
nsCOMPtr<nsIAsyncInputStream> reader;
nsCOMPtr<nsIAsyncOutputStream> writer;
NS_NewPipe2(getter_AddRefs(reader), getter_AddRefs(writer), true, true,
segmentSize, UINT32_MAX);
nsresult rv;
nsCOMPtr<nsIEventTarget> target =
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
rv = NS_AsyncCopy(aInputStream, writer, target,
NS_ASYNCCOPY_VIA_WRITESEGMENTS, segmentSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
cloneable = do_QueryInterface(reader);
MOZ_ASSERT(cloneable && cloneable->GetCloneable());
return cloneable.forget();
}
/* static */
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
already_AddRefed<nsIInputStream> aInputStream,
const nsAString& aContentType, uint64_t aLength,
const nsAString& aBlobImplType) {
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
nsCOMPtr<nsICloneableInputStream> cloneable =
EnsureCloneableStream(inputStream, aLength);
RefPtr<StreamBlobImpl> blobImplStream = new StreamBlobImpl(
cloneable.forget(), aContentType, aLength, aBlobImplType);
blobImplStream->MaybeRegisterMemoryReporter();
return blobImplStream.forget();
}
/* static */
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
already_AddRefed<nsIInputStream> aInputStream, const nsAString& aName,
const nsAString& aContentType, int64_t aLastModifiedDate, uint64_t aLength,
const nsAString& aBlobImplType) {
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
nsCOMPtr<nsICloneableInputStream> cloneable =
EnsureCloneableStream(inputStream, aLength);
RefPtr<StreamBlobImpl> blobImplStream =
new StreamBlobImpl(cloneable.forget(), aName, aContentType,
aLastModifiedDate, aLength, aBlobImplType);
blobImplStream->MaybeRegisterMemoryReporter();
return blobImplStream.forget();
}
StreamBlobImpl::StreamBlobImpl(
already_AddRefed<nsICloneableInputStream> aInputStream,
const nsAString& aContentType, uint64_t aLength,
const nsAString& aBlobImplType)
: BaseBlobImpl(aContentType, aLength),
mInputStream(std::move(aInputStream)),
mBlobImplType(aBlobImplType),
mIsDirectory(false),
mFileId(-1) {}
StreamBlobImpl::StreamBlobImpl(
already_AddRefed<nsICloneableInputStream> aInputStream,
const nsAString& aName, const nsAString& aContentType,
int64_t aLastModifiedDate, uint64_t aLength, const nsAString& aBlobImplType)
: BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate),
mInputStream(std::move(aInputStream)),
mBlobImplType(aBlobImplType),
mIsDirectory(false),
mFileId(-1) {}
StreamBlobImpl::~StreamBlobImpl() {
if (mInputStream) {
nsCOMPtr<nsIInputStream> stream = do_QueryInterface(mInputStream);
stream->Close();
}
UnregisterWeakMemoryReporter(this);
}
void StreamBlobImpl::CreateInputStream(nsIInputStream** aStream,
ErrorResult& aRv) const {
if (!mInputStream) {
// We failed to clone the input stream in EnsureCloneableStream.
*aStream = nullptr;
aRv.ThrowUnknownError("failed to read blob data");
return;
}
nsCOMPtr<nsIInputStream> clonedStream;
aRv = mInputStream->Clone(getter_AddRefs(clonedStream));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
nsCOMPtr<nsIInputStream> wrappedStream =
InputStreamLengthWrapper::MaybeWrap(clonedStream.forget(), mLength);
wrappedStream.forget(aStream);
}
already_AddRefed<BlobImpl> StreamBlobImpl::CreateSlice(
uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
ErrorResult& aRv) const {
if (!aLength) {
RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
return impl.forget();
}
nsCOMPtr<nsIInputStream> clonedStream;
nsCOMPtr<nsICloneableInputStreamWithRange> stream =
do_QueryInterface(mInputStream);
if (stream) {
aRv = stream->CloneWithRange(aStart, aLength, getter_AddRefs(clonedStream));
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
} else {
CreateInputStream(getter_AddRefs(clonedStream), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
clonedStream =
new SlicedInputStream(clonedStream.forget(), aStart, aLength);
}
MOZ_ASSERT(clonedStream);
return StreamBlobImpl::Create(clonedStream.forget(), aContentType, aLength,
mBlobImplType);
}
void StreamBlobImpl::MaybeRegisterMemoryReporter() {
// We report only stringInputStream.
nsCOMPtr<nsIStringInputStream> stringInputStream =
do_QueryInterface(mInputStream);
if (!stringInputStream) {
return;
}
RegisterWeakMemoryReporter(this);
}
NS_IMETHODIMP
StreamBlobImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize) {
nsCOMPtr<nsIStringInputStream> stringInputStream =
do_QueryInterface(mInputStream);
if (!stringInputStream) {
return NS_OK;
}
MOZ_COLLECT_REPORT(
"explicit/dom/memory-file-data/stream", KIND_HEAP, UNITS_BYTES,
stringInputStream->SizeOfIncludingThisIfUnshared(MallocSizeOf),
"Memory used to back a File/Blob based on an input stream.");
return NS_OK;
}
size_t StreamBlobImpl::GetAllocationSize() const {
// do_QueryInterface may run GC if the object is implemented in JS, but
// mInputStream is nsICloneableInputStream which doesn't have a JS
// implementation.
JS::AutoSuppressGCAnalysis nogc;
nsCOMPtr<nsIStringInputStream> stringInputStream =
do_QueryInterface(mInputStream);
if (!stringInputStream) {
return 0;
}
return stringInputStream->SizeOfIncludingThisEvenIfShared(MallocSizeOf);
}
void StreamBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
aBlobImplType.AssignLiteral("StreamBlobImpl[");
aBlobImplType.Append(mBlobImplType);
aBlobImplType.AppendLiteral("]");
}
} // namespace mozilla::dom
|