mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 22:01:30 +00:00
Bug 1201597 - Part 1: Implement an nsIOutputStream for ipc::FileDescriptor; r=mrbkap
This commit is contained in:
parent
02fd124a0c
commit
1b86ebf34b
85
devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp
Normal file
85
devtools/shared/heapsnapshot/FileDescriptorOutputStream.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
/* -*- 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;
|
||||
|
||||
PRFileDesc* prfd = PR_ImportFile(PROsfd(fileDescriptor.PlatformHandle()));
|
||||
if (NS_WARN_IF(!prfd))
|
||||
return nullptr;
|
||||
|
||||
nsRefPtr<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::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
|
41
devtools/shared/heapsnapshot/FileDescriptorOutputStream.h
Normal file
41
devtools/shared/heapsnapshot/FileDescriptorOutputStream.h
Normal file
@ -0,0 +1,41 @@
|
||||
/* -*- 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/. */
|
||||
|
||||
#ifndef mozilla_devtools_FileDescriptorOutputStream_h
|
||||
#define mozilla_devtools_FileDescriptorOutputStream_h
|
||||
|
||||
#include <prio.h>
|
||||
|
||||
#include "mozilla/AlreadyAddRefed.h"
|
||||
#include "mozilla/ipc/FileDescriptor.h"
|
||||
#include "nsIOutputStream.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace devtools {
|
||||
|
||||
class FileDescriptorOutputStream final : public nsIOutputStream
|
||||
{
|
||||
private:
|
||||
PRFileDesc* fd;
|
||||
|
||||
public:
|
||||
static already_AddRefed<FileDescriptorOutputStream>
|
||||
Create(const ipc::FileDescriptor& fileDescriptor);
|
||||
|
||||
private:
|
||||
explicit FileDescriptorOutputStream(PRFileDesc* prfd)
|
||||
: fd(prfd)
|
||||
{ }
|
||||
|
||||
virtual ~FileDescriptorOutputStream() { }
|
||||
|
||||
NS_DECL_THREADSAFE_ISUPPORTS
|
||||
NS_DECL_NSIOUTPUTSTREAM
|
||||
};
|
||||
|
||||
} // namespace devtools
|
||||
} // namespace mozilla
|
||||
|
||||
#endif // mozilla_devtools_FileDescriptorOutputStream_h
|
@ -14,6 +14,7 @@ EXPORTS.mozilla.devtools += [
|
||||
'AutoMemMap.h',
|
||||
'CoreDump.pb.h',
|
||||
'DeserializedNode.h',
|
||||
'FileDescriptorOutputStream.h',
|
||||
'HeapSnapshot.h',
|
||||
'ZeroCopyNSIOutputStream.h',
|
||||
]
|
||||
@ -22,6 +23,7 @@ SOURCES += [
|
||||
'AutoMemMap.cpp',
|
||||
'CoreDump.pb.cc',
|
||||
'DeserializedNode.cpp',
|
||||
'FileDescriptorOutputStream.cpp',
|
||||
'HeapSnapshot.cpp',
|
||||
'ZeroCopyNSIOutputStream.cpp',
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user