Bug 1758813 [wpt PR 33142] - Implement RP sign out, a=testonly
[gecko.git] / ipc / glue / MiniTransceiver.h
blobf7b482e3ca70491ecfbafa679251531b39a9ed71
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef __MINITRANSCEIVER_H_
7 #define __MINITRANSCEIVER_H_
9 #include "chrome/common/ipc_message.h"
10 #include "mozilla/Assertions.h"
12 struct msghdr;
14 namespace mozilla {
15 namespace ipc {
17 enum class DataBufferClear { None, AfterReceiving };
19 /**
20 * This simple implementation handles the transmissions of IPC
21 * messages.
23 * It works according to a strict request-response paradigm, no
24 * concurrent messaging is allowed. Sending a message from A to B must
25 * be followed by another one from B to A. Because of this we don't
26 * need to handle data crossing the boundaries of a
27 * message. Transmission is done via blocking I/O to avoid the
28 * complexity of asynchronous I/O.
30 class MiniTransceiver {
31 public:
32 /**
33 * \param aFd should be a blocking, no O_NONBLOCK, fd.
34 * \param aClearDataBuf is true to clear data buffers after
35 * receiving a message.
37 explicit MiniTransceiver(
38 int aFd, DataBufferClear aDataBufClear = DataBufferClear::None);
40 bool Send(IPC::Message& aMsg);
41 inline bool SendInfallible(IPC::Message& aMsg, const char* aCrashMessage) {
42 bool Ok = Send(aMsg);
43 if (!Ok) {
44 MOZ_CRASH_UNSAFE(aCrashMessage);
46 return Ok;
49 /**
50 * \param aMsg will hold the content of the received message.
51 * \return false if the fd is closed or with an error.
53 bool Recv(IPC::Message& aMsg);
54 inline bool RecvInfallible(IPC::Message& aMsg, const char* aCrashMessage) {
55 bool Ok = Recv(aMsg);
56 if (!Ok) {
57 MOZ_CRASH_UNSAFE(aCrashMessage);
59 return Ok;
62 int GetFD() { return mFd; }
64 private:
65 /**
66 * Set control buffer to make file descriptors ready to be sent
67 * through a socket.
69 void PrepareFDs(msghdr* aHdr, IPC::Message& aMsg);
70 /**
71 * Collect buffers of the message and make them ready to be sent.
73 * \param aHdr is the structure going to be passed to sendmsg().
74 * \param aMsg is the Message to send.
76 size_t PrepareBuffers(msghdr* aHdr, IPC::Message& aMsg);
77 /**
78 * Collect file descriptors received.
80 * \param aAllFds is where to store file descriptors.
81 * \param aMaxFds is how many file descriptors can be stored in aAllFds.
82 * \return the number of received file descriptors.
84 unsigned RecvFDs(msghdr* aHdr, int* aAllFds, unsigned aMaxFds);
85 /**
86 * Received data from the socket.
88 * \param aDataBuf is where to store the data from the socket.
89 * \param aBufSize is the size of the buffer.
90 * \param aMsgSize returns how many bytes were readed from the socket.
91 * \param aFdsBuf is the buffer to return file desriptors received.
92 * \param aMaxFds is the number of file descriptors that can be held.
93 * \param aNumFds returns the number of file descriptors received.
94 * \return true if sucess, or false for error.
96 bool RecvData(char* aDataBuf, size_t aBufSize, uint32_t* aMsgSize,
97 int* aFdsBuf, unsigned aMaxFds, unsigned* aNumFds);
99 int mFd; // The file descriptor of the socket for IPC.
101 #ifdef DEBUG
102 enum State {
103 STATE_NONE,
104 STATE_SENDING,
105 STATE_RECEIVING,
107 State mState;
108 #endif
110 // Clear all received data in temp buffers to avoid data leaking.
111 DataBufferClear mDataBufClear;
114 } // namespace ipc
115 } // namespace mozilla
117 #endif // __MINITRANSCEIVER_H_