Bug 1834537 - Part 6: Simplify GCRuntime::checkAllocatorState a little r=sfink
[gecko.git] / ipc / glue / NodeChannel.h
blobeae6cb655e3e37267619a1b5b580a91900cc277c
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 NodeController;
30 // Represents a live connection between our Node and a remote process. This
31 // object acts as an IPC::Channel listener and performs basic processing on
32 // messages as they're passed between processes.
34 class NodeChannel final : public IPC::Channel::Listener {
35 using NodeName = mojo::core::ports::NodeName;
36 using PortName = mojo::core::ports::PortName;
38 #ifdef FUZZING_SNAPSHOT
39 // Required because IPCFuzzController calls OnMessageReceived.
40 friend class mozilla::fuzzing::IPCFuzzController;
41 #endif
43 public:
44 NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(NodeChannel, Destroy())
46 struct Introduction {
47 NodeName mName;
48 IPC::Channel::ChannelHandle mHandle;
49 IPC::Channel::Mode mMode;
50 base::ProcessId mMyPid = base::kInvalidProcessId;
51 base::ProcessId mOtherPid = base::kInvalidProcessId;
54 class Listener {
55 public:
56 virtual ~Listener() = default;
58 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
60 virtual void OnEventMessage(const NodeName& aFromNode,
61 UniquePtr<IPC::Message> aMessage) = 0;
62 virtual void OnBroadcast(const NodeName& aFromNode,
63 UniquePtr<IPC::Message> aMessage) = 0;
64 virtual void OnIntroduce(const NodeName& aFromNode,
65 Introduction aIntroduction) = 0;
66 virtual void OnRequestIntroduction(const NodeName& aFromNode,
67 const NodeName& aName) = 0;
68 virtual void OnAcceptInvite(const NodeName& aFromNode,
69 const NodeName& aRealName,
70 const PortName& aInitialPort) = 0;
71 virtual void OnChannelError(const NodeName& aFromNode) = 0;
74 NodeChannel(const NodeName& aName, UniquePtr<IPC::Channel> aChannel,
75 Listener* aListener,
76 base::ProcessId aPid = base::kInvalidProcessId);
78 // Send the given message over this peer channel link. May be called from any
79 // thread.
80 void SendEventMessage(UniquePtr<IPC::Message> aMessage);
82 // Ask the broker process to broadcast this message to every node. May be
83 // called from any thread.
84 void Broadcast(UniquePtr<IPC::Message> aMessage);
86 // Ask the broker process to introduce this node to another node with the
87 // given name. May be called from any thread.
88 void RequestIntroduction(const NodeName& aPeerName);
90 // Send an introduction to the target node. May be called from any thread.
91 void Introduce(Introduction aIntroduction);
93 void AcceptInvite(const NodeName& aRealName, const PortName& aInitialPort);
95 // The PID of the remote process, once known. May be called from any thread.
96 base::ProcessId OtherPid() const { return mOtherPid; }
98 // Start communicating with the remote process using this NodeChannel. MUST BE
99 // CALLED FROM THE IO THREAD.
100 void Start(bool aCallConnect = true);
102 // Stop communicating with the remote process using this NodeChannel, MUST BE
103 // CALLED FROM THE IO THREAD.
104 void Close();
106 // Only ever called by NodeController to update the name after an invite has
107 // completed. MUST BE CALLED FROM THE IO THREAD.
108 void SetName(const NodeName& aNewName) { mName = aNewName; }
110 #ifdef FUZZING_SNAPSHOT
111 // MUST BE CALLED FROM THE IO THREAD.
112 const NodeName& GetName() { return mName; }
113 #endif
115 #ifdef XP_MACOSX
116 // Called by the GeckoChildProcessHost to provide the task_t for the peer
117 // process. MUST BE CALLED FROM THE IO THREAD.
118 void SetMachTaskPort(task_t aTask);
119 #endif
121 private:
122 ~NodeChannel();
124 void Destroy();
125 void FinalDestroy();
127 // Update the known PID for the remote process. IO THREAD ONLY
128 void SetOtherPid(base::ProcessId aNewPid);
130 void SendMessage(UniquePtr<IPC::Message> aMessage);
132 // IPC::Channel::Listener implementation
133 void OnMessageReceived(UniquePtr<IPC::Message> aMessage) override;
134 void OnChannelConnected(base::ProcessId aPeerPid) override;
135 void OnChannelError() override;
137 // NOTE: This strong reference will create a reference cycle between the
138 // listener and the NodeChannel while it is in use. The Listener must clear
139 // its reference to the NodeChannel to avoid leaks before shutdown.
140 const RefPtr<Listener> mListener;
142 // The apparent name of this Node. This may change during the invite process
143 // while waiting for the remote node name to be communicated to us.
145 // WARNING: This must only be accessed on the IO thread.
146 NodeName mName;
148 // NOTE: This won't change once the connection has been established, but may
149 // be `-1` until then. This will only be written to on the IO thread, but may
150 // be read from other threads.
151 std::atomic<base::ProcessId> mOtherPid;
153 // WARNING: Most methods on the IPC::Channel are only safe to call on the IO
154 // thread, however it is safe to call `Send()` and `IsClosed()` from other
155 // threads. See IPC::Channel's documentation for details.
156 const mozilla::UniquePtr<IPC::Channel> mChannel;
158 // The state will start out as `State::Active`, and will only transition to
159 // `State::Closed` on the IO thread. If a Send fails, the state will
160 // transition to `State::Closing`, and a runnable will be dispatched to the
161 // I/O thread to notify callbacks.
162 enum class State { Active, Closing, Closed };
163 std::atomic<State> mState = State::Active;
165 #ifdef FUZZING_SNAPSHOT
166 std::atomic<bool> mBlockSendRecv = false;
167 #endif
169 // WARNING: Must only be accessed on the IO thread.
170 WeakPtr<IPC::Channel::Listener> mExistingListener;
173 } // namespace mozilla::ipc
175 #endif