summaryrefslogtreecommitdiffstats
path: root/dom/file/StreamBlobImpl.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/file/StreamBlobImpl.cpp
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/file/StreamBlobImpl.cpp')
-rw-r--r--dom/file/StreamBlobImpl.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/dom/file/StreamBlobImpl.cpp b/dom/file/StreamBlobImpl.cpp
new file mode 100644
index 0000000000..43ff3c526b
--- /dev/null
+++ b/dom/file/StreamBlobImpl.cpp
@@ -0,0 +1,167 @@
+/* -*- 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 "nsStreamUtils.h"
+#include "nsStringStream.h"
+#include "nsICloneableInputStream.h"
+
+namespace mozilla::dom {
+
+NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)
+
+/* 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);
+
+ RefPtr<StreamBlobImpl> blobImplStream = new StreamBlobImpl(
+ inputStream.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);
+
+ RefPtr<StreamBlobImpl> blobImplStream =
+ new StreamBlobImpl(inputStream.forget(), aName, aContentType,
+ aLastModifiedDate, aLength, aBlobImplType);
+ blobImplStream->MaybeRegisterMemoryReporter();
+ return blobImplStream.forget();
+}
+
+StreamBlobImpl::StreamBlobImpl(already_AddRefed<nsIInputStream> 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<nsIInputStream> 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() { UnregisterWeakMemoryReporter(this); }
+
+void StreamBlobImpl::CreateInputStream(nsIInputStream** aStream,
+ ErrorResult& aRv) {
+ 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 = std::move(replacementStream);
+ }
+
+ 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) {
+ 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);
+
+ RefPtr<BlobImpl> impl = new StreamBlobImpl(
+ clonedStream.forget(), aContentType, aLength, mBlobImplType);
+ 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->SizeOfIncludingThisIfUnshared(MallocSizeOf),
+ "Memory used to back a File/Blob based on an input stream.");
+
+ return NS_OK;
+}
+
+size_t StreamBlobImpl::GetAllocationSize() const {
+ 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