diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp')
-rw-r--r-- | devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp b/devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp new file mode 100644 index 0000000000..651d7b9496 --- /dev/null +++ b/devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp @@ -0,0 +1,81 @@ +/* -*- Mode: C++; tab-width: 2; 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 "mozilla/devtools/FileDescriptorOutputStream.h" +#include "private/pprio.h" + +namespace mozilla { +namespace devtools { + +/* static */ +already_AddRefed<FileDescriptorOutputStream> FileDescriptorOutputStream::Create( + const ipc::FileDescriptor& fileDescriptor) { + if (NS_WARN_IF(!fileDescriptor.IsValid())) return nullptr; + + auto rawFD = fileDescriptor.ClonePlatformHandle(); + PRFileDesc* prfd = PR_ImportFile(PROsfd(rawFD.release())); + if (NS_WARN_IF(!prfd)) return nullptr; + + RefPtr<FileDescriptorOutputStream> stream = + new FileDescriptorOutputStream(prfd); + return stream.forget(); +} + +NS_IMPL_ISUPPORTS(FileDescriptorOutputStream, nsIOutputStream); + +NS_IMETHODIMP +FileDescriptorOutputStream::Close() { + // Repeatedly closing is idempotent. + if (!fd) return NS_OK; + + if (PR_Close(fd) != PR_SUCCESS) return NS_ERROR_FAILURE; + fd = nullptr; + return NS_OK; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::StreamStatus() { + return fd ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::Write(const char* buf, uint32_t count, + uint32_t* retval) { + if (NS_WARN_IF(!fd)) return NS_ERROR_FAILURE; + + auto written = PR_Write(fd, buf, count); + if (written < 0) return NS_ERROR_FAILURE; + *retval = written; + return NS_OK; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::Flush() { + if (NS_WARN_IF(!fd)) return NS_ERROR_FAILURE; + + return PR_Sync(fd) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::WriteFrom(nsIInputStream* fromStream, + uint32_t count, uint32_t* retval) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::WriteSegments(nsReadSegmentFun reader, + void* closure, uint32_t count, + uint32_t* retval) { + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +FileDescriptorOutputStream::IsNonBlocking(bool* retval) { + *retval = false; + return NS_OK; +} + +} // namespace devtools +} // namespace mozilla |