2017-02-16 17:26:38 +00: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 13:51:42 +00:00
|
|
|
#include "EmptyBlobImpl.h"
|
2018-06-13 15:37:26 +00:00
|
|
|
#include "mozilla/InputStreamLengthWrapper.h"
|
2018-06-02 13:51:42 +00:00
|
|
|
#include "mozilla/SlicedInputStream.h"
|
2017-02-16 17:26:38 +00:00
|
|
|
#include "StreamBlobImpl.h"
|
2018-06-02 13:51:42 +00:00
|
|
|
#include "nsStreamUtils.h"
|
2017-02-16 17:26:38 +00:00
|
|
|
#include "nsStringStream.h"
|
2017-09-11 15:29:15 +00:00
|
|
|
#include "nsICloneableInputStream.h"
|
2017-02-16 17:26:38 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
|
2018-06-13 15:37:26 +00:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream,
|
2019-01-04 10:29:48 +00:00
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType) {
|
2018-06-13 15:37:26 +00:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
|
|
|
|
2019-01-04 10:29:48 +00:00
|
|
|
RefPtr<StreamBlobImpl> blobImplStream = new StreamBlobImpl(
|
|
|
|
inputStream.forget(), aContentType, aLength, aBlobImplType);
|
2017-02-16 17:26:38 +00:00
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
|
2018-06-13 15:37:26 +00:00
|
|
|
already_AddRefed<nsIInputStream> aInputStream, const nsAString& aName,
|
2019-01-04 10:29:48 +00:00
|
|
|
const nsAString& aContentType, int64_t aLastModifiedDate, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType) {
|
2018-06-13 15:37:26 +00:00
|
|
|
nsCOMPtr<nsIInputStream> inputStream = std::move(aInputStream);
|
|
|
|
|
2019-01-04 10:29:48 +00:00
|
|
|
RefPtr<StreamBlobImpl> blobImplStream =
|
|
|
|
new StreamBlobImpl(inputStream.forget(), aName, aContentType,
|
|
|
|
aLastModifiedDate, aLength, aBlobImplType);
|
2017-02-16 17:26:38 +00:00
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-13 15:37:26 +00:00
|
|
|
StreamBlobImpl::StreamBlobImpl(already_AddRefed<nsIInputStream> aInputStream,
|
2019-01-04 10:29:48 +00:00
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType)
|
|
|
|
: BaseBlobImpl(aBlobImplType, aContentType, aLength),
|
2018-06-13 15:37:26 +00:00
|
|
|
mInputStream(std::move(aInputStream)),
|
2017-05-02 14:38:19 +00:00
|
|
|
mIsDirectory(false),
|
2017-05-23 05:51:13 +00:00
|
|
|
mFileId(-1) {
|
2017-02-16 17:26:38 +00:00
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
2018-06-13 15:37:26 +00:00
|
|
|
StreamBlobImpl::StreamBlobImpl(already_AddRefed<nsIInputStream> aInputStream,
|
2017-02-16 17:26:38 +00:00
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
2019-01-04 10:29:48 +00:00
|
|
|
int64_t aLastModifiedDate, uint64_t aLength,
|
|
|
|
const nsAString& aBlobImplType)
|
|
|
|
: BaseBlobImpl(aBlobImplType, aName, aContentType, aLength,
|
|
|
|
aLastModifiedDate),
|
2018-06-13 15:37:26 +00:00
|
|
|
mInputStream(std::move(aInputStream)),
|
2017-05-02 14:38:19 +00:00
|
|
|
mIsDirectory(false),
|
2017-05-23 05:51:13 +00:00
|
|
|
mFileId(-1) {
|
2017-02-16 17:26:38 +00:00
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamBlobImpl::~StreamBlobImpl() { UnregisterWeakMemoryReporter(this); }
|
|
|
|
|
2017-10-02 11:53:12 +00:00
|
|
|
void StreamBlobImpl::CreateInputStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv) {
|
2017-02-16 17:26:38 +00:00
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
nsCOMPtr<nsIInputStream> replacementStream;
|
|
|
|
|
|
|
|
aRv = NS_CloneInputStream(mInputStream, getter_AddRefs(clonedStream),
|
|
|
|
getter_AddRefs(replacementStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replacementStream) {
|
|
|
|
mInputStream = replacementStream.forget();
|
|
|
|
}
|
|
|
|
|
2018-06-13 15:37:26 +00:00
|
|
|
nsCOMPtr<nsIInputStream> wrappedStream =
|
|
|
|
InputStreamLengthWrapper::MaybeWrap(clonedStream.forget(), mLength);
|
|
|
|
|
|
|
|
wrappedStream.forget(aStream);
|
2017-02-16 17:26:38 +00: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 05:38:48 +00:00
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
|
2017-09-11 15:29:15 +00: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 05:38:48 +00:00
|
|
|
} else {
|
|
|
|
CreateInputStream(getter_AddRefs(clonedStream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-11 15:29:15 +00:00
|
|
|
|
2017-10-05 05:38:48 +00:00
|
|
|
clonedStream =
|
|
|
|
new SlicedInputStream(clonedStream.forget(), aStart, aLength);
|
2017-09-11 15:29:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 05:38:48 +00:00
|
|
|
MOZ_ASSERT(clonedStream);
|
|
|
|
|
2019-01-04 10:29:48 +00:00
|
|
|
RefPtr<BlobImpl> impl = new StreamBlobImpl(
|
|
|
|
clonedStream.forget(), aContentType, aLength, mBlobImplType);
|
2017-02-16 17:26:38 +00:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
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->SizeOfIncludingThis(MallocSizeOf),
|
|
|
|
"Memory used to back a File/Blob based on an input stream.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-09-12 09:46:51 +00:00
|
|
|
size_t StreamBlobImpl::GetAllocationSize() const {
|
|
|
|
nsCOMPtr<nsIStringInputStream> stringInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (!stringInputStream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringInputStream->SizeOfIncludingThis(MallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:29:48 +00:00
|
|
|
void StreamBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
|
|
|
|
aBlobImplType.AssignLiteral("StreamBlobImpl[");
|
|
|
|
aBlobImplType.Append(mBlobImplType);
|
|
|
|
aBlobImplType.AppendLiteral("]");
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:26:38 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|