2017-02-16 20:26:38 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "EmptyBlobImpl.h"
|
2018-06-13 18:37:26 +03:00
|
|
|
#include "mozilla/InputStreamLengthWrapper.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/SlicedInputStream.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "StreamBlobImpl.h"
|
2022-03-17 21:09:36 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "nsStringStream.h"
|
2022-03-17 21:09:36 +03:00
|
|
|
#include "nsIAsyncOutputStream.h"
|
2017-09-11 18:29:15 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2022-03-17 22:02:08 +03:00
|
|
|
#include "nsIEventTarget.h"
|
2022-03-17 21:09:36 +03:00
|
|
|
#include "nsIPipe.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2017-02-16 20:26:38 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)
|
|
|
|
|
2022-03-17 21:09:36 +03:00
|
|
|
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;
|
|
|
|
nsresult rv = NS_NewPipe2(getter_AddRefs(reader), getter_AddRefs(writer),
|
|
|
|
true, true, segmentSize, UINT32_MAX);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
|
2018-06-13 18:37:26 +03:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream,
|
2019-01-04 13:29:48 +03:00
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType) {
|
2018-06-13 18:37:26 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2022-03-17 21:09:36 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneable =
|
|
|
|
EnsureCloneableStream(inputStream, aLength);
|
2018-06-13 18:37:26 +03:00
|
|
|
|
2019-01-04 13:29:48 +03:00
|
|
|
RefPtr<StreamBlobImpl> blobImplStream = new StreamBlobImpl(
|
2022-03-17 21:09:36 +03:00
|
|
|
cloneable.forget(), aContentType, aLength, aBlobImplType);
|
2017-02-16 20:26:38 +03:00
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
|
2018-06-13 18:37:26 +03:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream, const nsAString& aName,
|
2019-01-04 13:29:48 +03:00
|
|
|
const nsAString& aContentType, int64_t aLastModifiedDate, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType) {
|
2018-06-13 18:37:26 +03:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
2022-03-17 21:09:36 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneable =
|
|
|
|
EnsureCloneableStream(inputStream, aLength);
|
2018-06-13 18:37:26 +03:00
|
|
|
|
2019-01-04 13:29:48 +03:00
|
|
|
RefPtr<StreamBlobImpl> blobImplStream =
|
2022-03-17 21:09:36 +03:00
|
|
|
new StreamBlobImpl(cloneable.forget(), aName, aContentType,
|
2019-01-04 13:29:48 +03:00
|
|
|
aLastModifiedDate, aLength, aBlobImplType);
|
2017-02-16 20:26:38 +03:00
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
|
|
|
|
2022-03-17 21:09:36 +03:00
|
|
|
StreamBlobImpl::StreamBlobImpl(
|
|
|
|
already_AddRefed<nsICloneableInputStream> aInputStream,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType)
|
2020-03-06 15:08:48 +03:00
|
|
|
: BaseBlobImpl(aContentType, aLength),
|
2018-06-13 18:37:26 +03:00
|
|
|
mInputStream(std::move(aInputStream)),
|
2020-03-06 15:08:48 +03:00
|
|
|
mBlobImplType(aBlobImplType),
|
2017-05-02 17:38:19 +03:00
|
|
|
mIsDirectory(false),
|
2020-01-07 18:02:00 +03:00
|
|
|
mFileId(-1) {}
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2022-03-17 21:09:36 +03:00
|
|
|
StreamBlobImpl::StreamBlobImpl(
|
|
|
|
already_AddRefed<nsICloneableInputStream> aInputStream,
|
|
|
|
const nsAString& aName, const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate, uint64_t aLength, const nsAString& aBlobImplType)
|
2020-03-06 15:08:48 +03:00
|
|
|
: BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate),
|
2018-06-13 18:37:26 +03:00
|
|
|
mInputStream(std::move(aInputStream)),
|
2020-03-06 15:08:48 +03:00
|
|
|
mBlobImplType(aBlobImplType),
|
2017-05-02 17:38:19 +03:00
|
|
|
mIsDirectory(false),
|
2020-01-07 18:02:00 +03:00
|
|
|
mFileId(-1) {}
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2022-01-12 16:21:11 +03:00
|
|
|
StreamBlobImpl::~StreamBlobImpl() {
|
|
|
|
if (mInputStream) {
|
2022-03-17 21:09:36 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = do_QueryInterface(mInputStream);
|
|
|
|
stream->Close();
|
2022-01-12 16:21:11 +03:00
|
|
|
}
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2017-10-02 14:53:12 +03:00
|
|
|
void StreamBlobImpl::CreateInputStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) {
|
2022-03-17 21:09:36 +03:00
|
|
|
if (!mInputStream) {
|
|
|
|
// We failed to clone the input stream in EnsureCloneableStream.
|
|
|
|
*aStream = nullptr;
|
|
|
|
aRv.ThrowUnknownError("failed to read blob data");
|
2017-02-16 20:26:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-17 21:09:36 +03:00
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
aRv = mInputStream->Clone(getter_AddRefs(clonedStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 18:37:26 +03:00
|
|
|
nsCOMPtr<nsIInputStream> wrappedStream =
|
|
|
|
InputStreamLengthWrapper::MaybeWrap(clonedStream.forget(), mLength);
|
|
|
|
|
|
|
|
wrappedStream.forget(aStream);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BlobImpl> StreamBlobImpl::CreateSlice(
|
|
|
|
uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!aLength) {
|
|
|
|
RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
|
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
|
2017-09-11 18:29:15 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStreamWithRange> stream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (stream) {
|
|
|
|
aRv = stream->CloneWithRange(aStart, aLength, getter_AddRefs(clonedStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-10-05 08:38:48 +03:00
|
|
|
} else {
|
|
|
|
CreateInputStream(getter_AddRefs(clonedStream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-11 18:29:15 +03:00
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
clonedStream =
|
|
|
|
new SlicedInputStream(clonedStream.forget(), aStart, aLength);
|
2017-09-11 18:29:15 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 08:38:48 +03:00
|
|
|
MOZ_ASSERT(clonedStream);
|
|
|
|
|
2022-03-17 21:09:36 +03:00
|
|
|
return StreamBlobImpl::Create(clonedStream.forget(), aContentType, aLength,
|
|
|
|
mBlobImplType);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-04-30 13:53:42 +03:00
|
|
|
stringInputStream->SizeOfIncludingThisIfUnshared(MallocSizeOf),
|
2017-02-16 20:26:38 +03:00
|
|
|
"Memory used to back a File/Blob based on an input stream.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:46:51 +03:00
|
|
|
size_t StreamBlobImpl::GetAllocationSize() const {
|
|
|
|
nsCOMPtr<nsIStringInputStream> stringInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (!stringInputStream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:53:42 +03:00
|
|
|
return stringInputStream->SizeOfIncludingThisEvenIfShared(MallocSizeOf);
|
2017-09-12 12:46:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-04 13:29:48 +03:00
|
|
|
void StreamBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
|
|
|
|
aBlobImplType.AssignLiteral("StreamBlobImpl[");
|
|
|
|
aBlobImplType.Append(mBlobImplType);
|
|
|
|
aBlobImplType.AppendLiteral("]");
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|