Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / ipc / glue / MessageLink.cpp
blob872ff48b27dad2ba254bd93ec7ec156a9395462b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=4 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/ipc/MessageLink.h"
9 #include "mojo/core/ports/event.h"
10 #include "mojo/core/ports/node.h"
11 #include "mozilla/ipc/MessageChannel.h"
12 #include "mozilla/ipc/BrowserProcessSubThread.h"
13 #include "mozilla/ipc/ProtocolUtils.h"
14 #include "mozilla/ipc/NodeController.h"
15 #include "chrome/common/ipc_channel.h"
16 #include "base/task.h"
18 #include "mozilla/Assertions.h"
19 #include "mozilla/DebugOnly.h"
20 #include "nsDebug.h"
21 #include "nsExceptionHandler.h"
22 #include "nsISupportsImpl.h"
23 #include "nsPrintfCString.h"
24 #include "nsXULAppAPI.h"
26 using namespace mozilla;
28 namespace mozilla {
29 namespace ipc {
31 MessageLink::MessageLink(MessageChannel* aChan) : mChan(aChan) {}
33 MessageLink::~MessageLink() {
34 #ifdef DEBUG
35 mChan = nullptr;
36 #endif
39 class PortLink::PortObserverThunk : public NodeController::PortObserver {
40 public:
41 PortObserverThunk(RefCountedMonitor* aMonitor, PortLink* aLink)
42 : mMonitor(aMonitor), mLink(aLink) {}
44 void OnPortStatusChanged() override {
45 MonitorAutoLock lock(*mMonitor);
46 if (mLink) {
47 mLink->OnPortStatusChanged();
51 private:
52 friend class PortLink;
54 // The monitor from our PortLink's MessageChannel. Guards access to `mLink`.
55 RefPtr<RefCountedMonitor> mMonitor;
57 // Cleared by `PortLink` in `PortLink::Clear()`.
58 PortLink* MOZ_NON_OWNING_REF mLink;
61 PortLink::PortLink(MessageChannel* aChan, ScopedPort aPort)
62 : MessageLink(aChan), mNode(aPort.Controller()), mPort(aPort.Release()) {
63 mChan->mMonitor->AssertCurrentThreadOwns();
65 mObserver = new PortObserverThunk(mChan->mMonitor, this);
66 mNode->SetPortObserver(mPort, mObserver);
68 // Dispatch an event to the IO loop to trigger an initial
69 // `OnPortStatusChanged` to deliver any pending messages. This needs to be run
70 // asynchronously from a different thread (or in the case of a same-thread
71 // channel, from the current thread), for now due to assertions in
72 // `MessageChannel`.
73 nsCOMPtr<nsIRunnable> openRunnable = NewRunnableMethod(
74 "PortLink::Open", mObserver, &PortObserverThunk::OnPortStatusChanged);
75 if (aChan->mIsSameThreadChannel) {
76 aChan->mWorkerThread->Dispatch(openRunnable.forget());
77 } else {
78 XRE_GetIOMessageLoop()->PostTask(openRunnable.forget());
82 PortLink::~PortLink() {
83 MOZ_RELEASE_ASSERT(!mObserver, "PortLink destroyed without being closed!");
86 void PortLink::SendMessage(UniquePtr<Message> aMessage) {
87 mChan->mMonitor->AssertCurrentThreadOwns();
89 if (aMessage->size() > IPC::Channel::kMaximumMessageSize) {
90 CrashReporter::AnnotateCrashReport(
91 CrashReporter::Annotation::IPCMessageName,
92 nsDependentCString(aMessage->name()));
93 CrashReporter::AnnotateCrashReport(
94 CrashReporter::Annotation::IPCMessageSize,
95 static_cast<unsigned int>(aMessage->size()));
96 MOZ_CRASH("IPC message size is too large");
98 aMessage->AssertAsLargeAsHeader();
100 RefPtr<PortObserverThunk> observer = mObserver;
101 if (!observer) {
102 NS_WARNING("Ignoring message to closed PortLink");
103 return;
106 // Make local copies of relevant member variables, so we can unlock the
107 // monitor for the rest of this function. This protects us in case `this` is
108 // deleted during the call (although that shouldn't happen in practice).
110 // We don't want the monitor to be held when calling into ports, as we may be
111 // re-entrantly called by our `PortObserverThunk` which will attempt to
112 // acquire the monitor.
113 RefPtr<RefCountedMonitor> monitor = mChan->mMonitor;
114 RefPtr<NodeController> node = mNode;
115 PortRef port = mPort;
117 bool ok = false;
118 monitor->AssertCurrentThreadOwns();
120 MonitorAutoUnlock guard(*monitor);
121 ok = node->SendUserMessage(port, std::move(aMessage));
123 if (!ok) {
124 // The send failed, but double-check that we weren't closed racily while
125 // sending, which could lead to an invalid state error.
126 if (observer->mLink) {
127 MOZ_CRASH("Invalid argument to SendUserMessage");
129 NS_WARNING("Message dropped as PortLink was closed");
133 void PortLink::Close() {
134 mChan->mMonitor->AssertCurrentThreadOwns();
136 if (!mObserver) {
137 // We're already being closed.
138 return;
141 Clear();
144 void PortLink::Clear() {
145 mChan->mMonitor->AssertCurrentThreadOwns();
147 // NOTE: We're calling into `ports` with our monitor held! Usually, this could
148 // lead to deadlocks due to the PortObserverThunk acquiring the lock
149 // re-entrantly, but is OK here as we're immediately clearing the port's
150 // observer. We shouldn't have issues with any re-entrant calls on this thread
151 // acquiring this MessageChannel's monitor.
153 // We also clear out the reference in `mObserver` back to this type so that
154 // notifications from other threads won't try to call us again once we release
155 // the monitor.
156 mNode->SetPortObserver(mPort, nullptr);
157 mObserver->mLink = nullptr;
158 mObserver = nullptr;
159 mNode->ClosePort(mPort);
162 void PortLink::OnPortStatusChanged() {
163 mChan->mMonitor->AssertCurrentThreadOwns();
165 // Check if the port's remoteness status has updated, and tell our channel if
166 // it has.
167 if (Maybe<PortStatus> status = mNode->GetStatus(mPort);
168 status && status->peer_remote != mChan->IsCrossProcess()) {
169 mChan->SetIsCrossProcess(status->peer_remote);
172 while (mObserver) {
173 UniquePtr<IPC::Message> message;
174 if (!mNode->GetMessage(mPort, &message)) {
175 Clear();
176 mChan->OnChannelErrorFromLink();
177 return;
179 if (!message) {
180 return;
183 mChan->OnMessageReceivedFromLink(std::move(message));
187 bool PortLink::IsClosed() const {
188 if (Maybe<PortStatus> status = mNode->GetStatus(mPort)) {
189 return !(status->has_messages || status->receiving_messages);
191 return true;
194 } // namespace ipc
195 } // namespace mozilla