2022-02-14 23:59:35 +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 https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ipc_DataPipe_h
|
|
|
|
#define mozilla_ipc_DataPipe_h
|
|
|
|
|
|
|
|
#include "mozilla/ipc/SharedMemoryBasic.h"
|
|
|
|
#include "mozilla/ipc/NodeController.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
namespace data_pipe_detail {
|
|
|
|
|
|
|
|
class DataPipeAutoLock;
|
|
|
|
class DataPipeLink;
|
|
|
|
|
|
|
|
class DataPipeBase {
|
|
|
|
public:
|
|
|
|
DataPipeBase(const DataPipeBase&) = delete;
|
|
|
|
DataPipeBase& operator=(const DataPipeBase&) = delete;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit DataPipeBase(bool aReceiverSide, nsresult aError);
|
2023-09-15 15:26:51 +00:00
|
|
|
DataPipeBase(bool aReceiverSide, ScopedPort aPort,
|
|
|
|
SharedMemoryBasic::Handle aShmemHandle, SharedMemory* aShmem,
|
2022-02-14 23:59:35 +00:00
|
|
|
uint32_t aCapacity, nsresult aPeerStatus, uint32_t aOffset,
|
|
|
|
uint32_t aAvailable);
|
|
|
|
|
2022-08-03 16:39:41 +00:00
|
|
|
void CloseInternal(DataPipeAutoLock&, nsresult aStatus) MOZ_REQUIRES(*mMutex);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
void AsyncWaitInternal(already_AddRefed<nsIRunnable> aCallback,
|
|
|
|
already_AddRefed<nsIEventTarget> aTarget,
|
2022-08-03 16:39:41 +00:00
|
|
|
bool aClosureOnly) MOZ_EXCLUDES(*mMutex);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
// Like `nsWriteSegmentFun` or `nsReadSegmentFun`.
|
|
|
|
using ProcessSegmentFun =
|
|
|
|
FunctionRef<nsresult(Span<char> aSpan, uint32_t aProcessedThisCall,
|
|
|
|
uint32_t* aProcessedCount)>;
|
|
|
|
nsresult ProcessSegmentsInternal(uint32_t aCount,
|
|
|
|
ProcessSegmentFun aProcessSegment,
|
2022-08-03 16:39:41 +00:00
|
|
|
uint32_t* aProcessedCount)
|
|
|
|
MOZ_EXCLUDES(*mMutex);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
2022-08-03 16:39:41 +00:00
|
|
|
nsresult CheckStatus(DataPipeAutoLock&) MOZ_REQUIRES(*mMutex);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
2022-08-03 16:39:41 +00:00
|
|
|
nsCString Describe(DataPipeAutoLock&) MOZ_REQUIRES(*mMutex);
|
2022-03-22 14:59:40 +00:00
|
|
|
|
|
|
|
// Thread safety helper to tell the analysis that `mLink->mMutex` is held when
|
|
|
|
// `mMutex` is held.
|
2022-08-03 16:39:41 +00:00
|
|
|
void AssertSameMutex(const std::shared_ptr<Mutex>& aMutex)
|
|
|
|
MOZ_REQUIRES(*mMutex) MOZ_ASSERT_CAPABILITY(*aMutex) {
|
2022-03-22 14:59:40 +00:00
|
|
|
MOZ_ASSERT(mMutex == aMutex);
|
|
|
|
}
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
virtual ~DataPipeBase();
|
|
|
|
|
|
|
|
const std::shared_ptr<Mutex> mMutex;
|
2022-08-03 16:39:41 +00:00
|
|
|
nsresult mStatus MOZ_GUARDED_BY(*mMutex) = NS_OK;
|
|
|
|
RefPtr<DataPipeLink> mLink MOZ_GUARDED_BY(*mMutex);
|
2022-02-14 23:59:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2022-03-04 15:39:41 +00:00
|
|
|
void DataPipeWrite(IPC::MessageWriter* aWriter, T* aParam);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2022-03-04 15:39:41 +00:00
|
|
|
bool DataPipeRead(IPC::MessageReader* aReader, RefPtr<T>* aResult);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
} // namespace data_pipe_detail
|
|
|
|
|
|
|
|
class DataPipeSender;
|
|
|
|
class DataPipeReceiver;
|
|
|
|
|
|
|
|
#define NS_DATAPIPESENDER_IID \
|
|
|
|
{ \
|
|
|
|
0x6698ed77, 0x9fff, 0x425d, { \
|
|
|
|
0xb0, 0xa6, 0x1d, 0x30, 0x66, 0xee, 0xb8, 0x16 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper class for streaming data to another process.
|
|
|
|
class DataPipeSender final : public nsIAsyncOutputStream,
|
|
|
|
public data_pipe_detail::DataPipeBase {
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DATAPIPESENDER_IID)
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCOUTPUTSTREAM
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend nsresult NewDataPipe(uint32_t, DataPipeSender**, DataPipeReceiver**);
|
|
|
|
friend void data_pipe_detail::DataPipeWrite<DataPipeSender>(
|
2022-03-04 15:39:41 +00:00
|
|
|
IPC::MessageWriter* aWriter, DataPipeSender* aParam);
|
2022-02-14 23:59:35 +00:00
|
|
|
friend bool data_pipe_detail::DataPipeRead<DataPipeSender>(
|
2022-03-04 15:39:41 +00:00
|
|
|
IPC::MessageReader* aReader, RefPtr<DataPipeSender>* aResult);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
explicit DataPipeSender(nsresult aError)
|
|
|
|
: data_pipe_detail::DataPipeBase(/* aReceiverSide */ false, aError) {}
|
2023-09-15 15:26:51 +00:00
|
|
|
DataPipeSender(ScopedPort aPort, SharedMemoryBasic::Handle aShmemHandle,
|
|
|
|
SharedMemory* aShmem, uint32_t aCapacity, nsresult aPeerStatus,
|
|
|
|
uint32_t aOffset, uint32_t aAvailable)
|
|
|
|
: data_pipe_detail::DataPipeBase(
|
|
|
|
/* aReceiverSide */ false, std::move(aPort),
|
|
|
|
std::move(aShmemHandle), aShmem, aCapacity, aPeerStatus, aOffset,
|
|
|
|
aAvailable) {}
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
~DataPipeSender() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DataPipeSender, NS_DATAPIPESENDER_IID)
|
|
|
|
|
|
|
|
#define NS_DATAPIPERECEIVER_IID \
|
|
|
|
{ \
|
|
|
|
0x0a185f83, 0x499e, 0x450c, { \
|
|
|
|
0x95, 0x82, 0x27, 0x67, 0xad, 0x6d, 0x64, 0xb5 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper class for streaming data from another process.
|
|
|
|
class DataPipeReceiver final : public nsIAsyncInputStream,
|
|
|
|
public nsIIPCSerializableInputStream,
|
|
|
|
public data_pipe_detail::DataPipeBase {
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DATAPIPERECEIVER_IID)
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend nsresult NewDataPipe(uint32_t, DataPipeSender**, DataPipeReceiver**);
|
|
|
|
friend void data_pipe_detail::DataPipeWrite<DataPipeReceiver>(
|
2022-03-04 15:39:41 +00:00
|
|
|
IPC::MessageWriter* aWriter, DataPipeReceiver* aParam);
|
2022-02-14 23:59:35 +00:00
|
|
|
friend bool data_pipe_detail::DataPipeRead<DataPipeReceiver>(
|
2022-03-04 15:39:41 +00:00
|
|
|
IPC::MessageReader* aReader, RefPtr<DataPipeReceiver>* aResult);
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
explicit DataPipeReceiver(nsresult aError)
|
|
|
|
: data_pipe_detail::DataPipeBase(/* aReceiverSide */ true, aError) {}
|
2023-09-15 15:26:51 +00:00
|
|
|
DataPipeReceiver(ScopedPort aPort, SharedMemoryBasic::Handle aShmemHandle,
|
|
|
|
SharedMemory* aShmem, uint32_t aCapacity,
|
2022-02-14 23:59:35 +00:00
|
|
|
nsresult aPeerStatus, uint32_t aOffset, uint32_t aAvailable)
|
2023-09-15 15:26:51 +00:00
|
|
|
: data_pipe_detail::DataPipeBase(
|
|
|
|
/* aReceiverSide */ true, std::move(aPort), std::move(aShmemHandle),
|
|
|
|
aShmem, aCapacity, aPeerStatus, aOffset, aAvailable) {}
|
2022-02-14 23:59:35 +00:00
|
|
|
|
|
|
|
~DataPipeReceiver() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DataPipeReceiver, NS_DATAPIPERECEIVER_IID)
|
|
|
|
|
|
|
|
constexpr uint32_t kDefaultDataPipeCapacity = 64 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new DataPipe pair. The sender and receiver ends of the pipe may be
|
|
|
|
* used to transfer data between processes. |aCapacity| is the capacity of the
|
|
|
|
* underlying ring buffer. If `0` is passed, `kDefaultDataPipeCapacity` will be
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
nsresult NewDataPipe(uint32_t aCapacity, DataPipeSender** aSender,
|
|
|
|
DataPipeReceiver** aReceiver);
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ParamTraits<mozilla::ipc::DataPipeSender*> {
|
2022-03-04 15:39:41 +00:00
|
|
|
static void Write(MessageWriter* aWriter,
|
|
|
|
mozilla::ipc::DataPipeSender* aParam);
|
|
|
|
static bool Read(MessageReader* aReader,
|
2022-02-14 23:59:35 +00:00
|
|
|
RefPtr<mozilla::ipc::DataPipeSender>* aResult);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ParamTraits<mozilla::ipc::DataPipeReceiver*> {
|
2022-03-04 15:39:41 +00:00
|
|
|
static void Write(MessageWriter* aWriter,
|
|
|
|
mozilla::ipc::DataPipeReceiver* aParam);
|
|
|
|
static bool Read(MessageReader* aReader,
|
2022-02-14 23:59:35 +00:00
|
|
|
RefPtr<mozilla::ipc::DataPipeReceiver>* aResult);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace IPC
|
|
|
|
|
|
|
|
#endif // mozilla_ipc_DataPipe_h
|