Files
sousa-gecko/xpcom/io/AsyncPlatformPipes.h
T
Nika Layzell b9ca38bc93 Bug 2031881 - Add new PlatformPipeReader type, r=jld
This is a new API which is intended to be used by code which wants to
perform async reads from native filesystem pipes. This does not support
async reads from "regular" files, as those are not compatible with
epoll.

Currently the API uses an owned buffer which is filled on the IPC I/O
thread, and read from on the user's thread. This is technically only
necessary for Windows (as non-Windows OSes could directly invoke read
during nsIInputStream::read method), This is because overlapped I/O on
Windows requires that the buffer be kept alive throughout the async
operation. So far I have made the call to always use the buffer for API
support consistency, but we could potentially change that behaviour.

The actual system file handle management is largely isolated to the
PlatformPipeLink type within the .cpp file. This is done to allow the
lifecycle of the nsIInputStream instance to be disconnected from the
lifecycle of the underlying pipe, as things like the buffer need to be
kept alive potentially longer than the stream to avoid issues on Windows
& handle cancelling I/O on destruction.

Currently there is no way to use this type with a shared file handle,
the file handle must be owned by the PlatformPipeLink.

Previous versions of this patch contained a PlatformPipeWriter type as
well, but that had some issues with lifecycle which I haven't yet
figured out yet, and turns out to not be necessary for the WebSerial
work right now, so are being left to a follow-up.

Differential Revision: https://phabricator.services.mozilla.com/D294317
2026-04-28 17:41:12 +00:00

52 lines
1.6 KiB
C++

/* -*- 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/. */
#ifndef mozilla_AsyncPlatformPipes_h
#define mozilla_AsyncPlatformPipes_h
#include "mozilla/UniquePtrExtensions.h"
#include "nsIAsyncInputStream.h"
namespace mozilla {
namespace platform_pipe_detail {
class PlatformPipeLink;
} // namespace platform_pipe_detail
// PlatformPipeReader wraps an OS-level file handle as a non-blocking xpcom
// input stream. It is intended for use with byte-oriented OS primitives such
// as pipes or sockets.
//
// Handle Requirements:
// - On POSIX, the handle must support non-blocking I/O and must have
// O_NONBLOCK set. Note that regular files ignore O_NONBLOCK: reads and
// writes always complete synchronously and will block the I/O thread, so
// are unsupported.
// - On Windows, the handle must be opened with FILE_FLAG_OVERLAPPED.
//
// aBufferSize controls the size of the internal buffer used to stage data
// between the caller and the OS.
class PlatformPipeReader final : public nsIAsyncInputStream {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAM
NS_DECL_NSIASYNCINPUTSTREAM
PlatformPipeReader(UniqueFileHandle aHandle, uint32_t aBufferSize);
private:
~PlatformPipeReader();
RefPtr<platform_pipe_detail::PlatformPipeLink> mLink;
};
} // namespace mozilla
#endif // mozilla_AsyncPlatformPipes_h