Bug 1916262 - Remove AbortFollower inheritance from AbortSignal r=smaug
[gecko.git] / ipc / glue / NodeChannel.h
blob56f9a19ce1959d3f71914b40d34f6572b8700329
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_NodeChannel_h
8 #define mozilla_ipc_NodeChannel_h
10 #include "mojo/core/ports/node.h"
11 #include "mojo/core/ports/node_delegate.h"
12 #include "base/process.h"
13 #include "chrome/common/ipc_message.h"
14 #include "chrome/common/ipc_channel.h"
15 #include "mozilla/ipc/ProtocolUtils.h"
16 #include "nsISupports.h"
17 #include "nsTHashMap.h"
18 #include "mozilla/Queue.h"
19 #include "mozilla/DataMutex.h"
20 #include "mozilla/UniquePtr.h"
22 #ifdef FUZZING_SNAPSHOT
23 # include "mozilla/fuzzing/IPCFuzzController.h"
24 #endif
26 namespace mozilla::ipc {
28 class GeckoChildProcessHost;
29 class NodeController;
31 // Represents a live connection between our Node and a remote process. This
32 // object acts as an IPC::Channel listener and performs basic processing on
33 // messages as they're passed between processes.
35 class NodeChannel final : public IPC::Channel::Listener {
36 using NodeName = mojo::core::ports::NodeName;
37 using PortName = mojo::core::ports::PortName;
39 #ifdef FUZZING_SNAPSHOT
40 // Required because IPCFuzzController calls OnMessageReceived.
41 friend class mozilla::fuzzing::IPCFuzzController;
42 #endif
44 public:
45 NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(NodeChannel, Destroy())
47 struct Introduction {
48 NodeName mName;
49 IPC::Channel::ChannelHandle mHandle;
50 IPC::Channel::Mode mMode;
51 base::ProcessId mMyPid = base::kInvalidProcessId;
52 base::ProcessId mOtherPid = base::kInvalidProcessId;
55 class Listener {
56 public:
57 virtual ~Listener() = default;
59 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
61 virtual void OnEventMessage(const NodeName& aFromNode,
62 UniquePtr<IPC::Message> aMessage) = 0;
63 virtual void OnBroadcast(const NodeName& aFromNode,
64 UniquePtr<IPC::Message> aMessage) = 0;
65 virtual void OnIntroduce(const NodeName& aFromNode,
66 Introduction aIntroduction) = 0;
67 virtual void OnRequestIntroduction(const NodeName& aFromNode,
68 const NodeName& aName) = 0;
69 virtual void OnAcceptInvite(const NodeName& aFromNode,
70 const NodeName& aRealName,
71 const PortName& aInitialPort) = 0;
72 virtual void OnChannelError(const NodeName& aFromNode) = 0;
75 NodeChannel(const NodeName& aName, UniquePtr<IPC::Channel> aChannel,
76 Listener* aListener,
77 base::ProcessId aPid = base::kInvalidProcessId,
78 GeckoChildProcessHost* aChildProcessHost = nullptr);
80 // Send the given message over this peer channel link. May be called from any
81 // thread.
82 void SendEventMessage(UniquePtr<IPC::Message> aMessage);
84 // Ask the broker process to broadcast this message to every node. May be
85 // called from any thread.
86 void Broadcast(UniquePtr<IPC::Message> aMessage);
88 // Ask the broker process to introduce this node to another node with the
89 // given name. May be called from any thread.
90 void RequestIntroduction(const NodeName& aPeerName);
92 // Send an introduction to the target node. May be called from any thread.
93 void Introduce(Introduction aIntroduction);
95 void AcceptInvite(const NodeName& aRealName, const PortName& aInitialPort);
97 // The PID of the remote process, once known. May be called from any thread.
98 base::ProcessId OtherPid() const { return mOtherPid; }
100 // Start communicating with the remote process using this NodeChannel. MUST BE
101 // CALLED FROM THE IO THREAD.
102 void Start();
104 // Stop communicating with the remote process using this NodeChannel, MUST BE
105 // CALLED FROM THE IO THREAD.
106 void Close();
108 // Only ever called by NodeController to update the name after an invite has
109 // completed. MUST BE CALLED FROM THE IO THREAD.
110 void SetName(const NodeName& aNewName) { mName = aNewName; }
112 #ifdef FUZZING_SNAPSHOT
113 // MUST BE CALLED FROM THE IO THREAD.
114 const NodeName& GetName() { return mName; }
115 #endif
117 // Update the known PID for the remote process. MUST BE CALLED FROM THE IO
118 // THREAD.
119 void SetOtherPid(base::ProcessId aNewPid);
121 #ifdef XP_DARWIN
122 // Called by the GeckoChildProcessHost to provide the task_t for the peer
123 // process. MUST BE CALLED FROM THE IO THREAD.
124 void SetMachTaskPort(task_t aTask);
125 #endif
127 private:
128 ~NodeChannel();
130 void Destroy();
131 void FinalDestroy();
133 void SendMessage(UniquePtr<IPC::Message> aMessage);
135 // IPC::Channel::Listener implementation
136 void OnMessageReceived(UniquePtr<IPC::Message> aMessage) override;
137 void OnChannelConnected(base::ProcessId aPeerPid) override;
138 void OnChannelError() override;
140 // NOTE: This strong reference will create a reference cycle between the
141 // listener and the NodeChannel while it is in use. The Listener must clear
142 // its reference to the NodeChannel to avoid leaks before shutdown.
143 const RefPtr<Listener> mListener;
145 // The apparent name of this Node. This may change during the invite process
146 // while waiting for the remote node name to be communicated to us.
148 // WARNING: This must only be accessed on the IO thread.
149 NodeName mName;
151 // NOTE: This won't change once the connection has been established, but may
152 // be `-1` until then. This will only be written to on the IO thread, but may
153 // be read from other threads.
154 std::atomic<base::ProcessId> mOtherPid;
156 // WARNING: Most methods on the IPC::Channel are only safe to call on the IO
157 // thread, however it is safe to call `Send()` and `IsClosed()` from other
158 // threads. See IPC::Channel's documentation for details.
159 const mozilla::UniquePtr<IPC::Channel> mChannel;
161 // The state will start out as `State::Active`, and will only transition to
162 // `State::Closed` on the IO thread. If a Send fails, the state will
163 // transition to `State::Closing`, and a runnable will be dispatched to the
164 // I/O thread to notify callbacks.
165 enum class State { Active, Closing, Closed };
166 std::atomic<State> mState = State::Active;
168 #ifdef FUZZING_SNAPSHOT
169 std::atomic<bool> mBlockSendRecv = false;
170 #endif
172 // WARNING: Must only be accessed on the IO thread.
173 WeakPtr<mozilla::ipc::GeckoChildProcessHost> mChildProcessHost;
176 } // namespace mozilla::ipc
178 #endif