Bug 1758813 [wpt PR 33142] - Implement RP sign out, a=testonly
[gecko.git] / ipc / glue / MessageLink.cpp
blobeaa5ffe388dee41c9148fb70eff54cb653ded127
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 mChan->mChannelState = ChannelConnected;
70 // Dispatch an event to the IO loop to trigger an initial
71 // `OnPortStatusChanged` to deliver any pending messages. This needs to be run
72 // asynchronously from a different thread (or in the case of a same-thread
73 // channel, from the current thread), for now due to assertions in
74 // `MessageChannel`.
75 nsCOMPtr<nsIRunnable> openRunnable = NewRunnableMethod(
76 "PortLink::Open", mObserver, &PortObserverThunk::OnPortStatusChanged);
77 if (aChan->mIsSameThreadChannel) {
78 aChan->mWorkerThread->Dispatch(openRunnable.forget());
79 } else {
80 XRE_GetIOMessageLoop()->PostTask(openRunnable.forget());
84 PortLink::~PortLink() {
85 MOZ_RELEASE_ASSERT(!mObserver, "PortLink destroyed without being closed!");
88 void PortLink::SendMessage(UniquePtr<Message> aMessage) {
89 mChan->mMonitor->AssertCurrentThreadOwns();
91 if (aMessage->size() > IPC::Channel::kMaximumMessageSize) {
92 CrashReporter::AnnotateCrashReport(
93 CrashReporter::Annotation::IPCMessageName,
94 nsDependentCString(aMessage->name()));
95 CrashReporter::AnnotateCrashReport(
96 CrashReporter::Annotation::IPCMessageSize,
97 static_cast<unsigned int>(aMessage->size()));
98 MOZ_CRASH("IPC message size is too large");
100 aMessage->AssertAsLargeAsHeader();
102 RefPtr<PortObserverThunk> observer = mObserver;
103 if (!observer) {
104 NS_WARNING("Ignoring message to closed PortLink");
105 return;
108 // Make local copies of relevant member variables, so we can unlock the
109 // monitor for the rest of this function. This protects us in case `this` is
110 // deleted during the call (although that shouldn't happen in practice).
112 // We don't want the monitor to be held when calling into ports, as we may be
113 // re-entrantly called by our `PortObserverThunk` which will attempt to
114 // acquire the monitor.
115 RefPtr<RefCountedMonitor> monitor = mChan->mMonitor;
116 RefPtr<NodeController> node = mNode;
117 PortRef port = mPort;
119 bool ok = false;
120 monitor->AssertCurrentThreadOwns();
122 MonitorAutoUnlock guard(*monitor);
123 ok = node->SendUserMessage(port, std::move(aMessage));
125 if (!ok) {
126 // The send failed, but double-check that we weren't closed racily while
127 // sending, which could lead to an invalid state error.
128 if (observer->mLink) {
129 MOZ_CRASH("Invalid argument to SendUserMessage");
131 NS_WARNING("Message dropped as PortLink was closed");
135 void PortLink::SendClose() {
136 mChan->mMonitor->AssertCurrentThreadOwns();
138 // Our channel has been closed, mark it as such.
139 mChan->mChannelState = ChannelClosed;
140 mChan->mMonitor->Notify();
142 if (!mObserver) {
143 // We're already being closed.
144 return;
147 Clear();
150 void PortLink::Clear() {
151 mChan->mMonitor->AssertCurrentThreadOwns();
153 // NOTE: We're calling into `ports` with our monitor held! Usually, this could
154 // lead to deadlocks due to the PortObserverThunk acquiring the lock
155 // re-entrantly, but is OK here as we're immediately clearing the port's
156 // observer. We shouldn't have issues with any re-entrant calls on this thread
157 // acquiring this MessageChannel's monitor.
159 // We also clear out the reference in `mObserver` back to this type so that
160 // notifications from other threads won't try to call us again once we release
161 // the monitor.
162 mNode->SetPortObserver(mPort, nullptr);
163 mObserver->mLink = nullptr;
164 mObserver = nullptr;
165 mNode->ClosePort(mPort);
168 void PortLink::OnPortStatusChanged() {
169 mChan->mMonitor->AssertCurrentThreadOwns();
171 // Check if the port's remoteness status has updated, and tell our channel if
172 // it has.
173 if (Maybe<PortStatus> status = mNode->GetStatus(mPort);
174 status && status->peer_remote != mChan->IsCrossProcess()) {
175 mChan->SetIsCrossProcess(status->peer_remote);
178 while (mObserver) {
179 UniquePtr<IPC::Message> message;
180 if (!mNode->GetMessage(mPort, &message)) {
181 Clear();
182 mChan->OnChannelErrorFromLink();
183 return;
185 if (!message) {
186 return;
189 mChan->OnMessageReceivedFromLink(std::move(*message));
193 bool PortLink::IsClosed() const {
194 if (Maybe<PortStatus> status = mNode->GetStatus(mPort)) {
195 return !(status->has_messages || status->receiving_messages);
197 return true;
200 } // namespace ipc
201 } // namespace mozilla