Bug 1904446 - Update wpt subtest in navigation-redirect.https.html?default on linux...
[gecko.git] / ipc / glue / MessageLink.cpp
bloba3efc8218f92dedf65001146891951e2e46582bc
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 const char* StringFromIPCSide(Side side) {
32 switch (side) {
33 case ChildSide:
34 return "Child";
35 case ParentSide:
36 return "Parent";
37 default:
38 return "Unknown";
42 MessageLink::MessageLink(MessageChannel* aChan) : mChan(aChan) {}
44 MessageLink::~MessageLink() {
45 #ifdef DEBUG
46 mChan = nullptr;
47 #endif
50 class PortLink::PortObserverThunk : public NodeController::PortObserver {
51 public:
52 PortObserverThunk(RefCountedMonitor* aMonitor, PortLink* aLink)
53 : mMonitor(aMonitor), mLink(aLink) {}
55 void OnPortStatusChanged() override {
56 MonitorAutoLock lock(*mMonitor);
57 if (mLink) {
58 mLink->OnPortStatusChanged();
62 private:
63 friend class PortLink;
65 // The monitor from our PortLink's MessageChannel. Guards access to `mLink`.
66 RefPtr<RefCountedMonitor> mMonitor;
68 // Cleared by `PortLink` in `PortLink::Clear()`.
69 PortLink* MOZ_NON_OWNING_REF mLink;
72 PortLink::PortLink(MessageChannel* aChan, ScopedPort aPort)
73 : MessageLink(aChan), mNode(aPort.Controller()), mPort(aPort.Release()) {
74 mChan->mMonitor->AssertCurrentThreadOwns();
76 mObserver = new PortObserverThunk(mChan->mMonitor, this);
77 mNode->SetPortObserver(mPort, mObserver);
79 // Dispatch an event to the IO loop to trigger an initial
80 // `OnPortStatusChanged` to deliver any pending messages. This needs to be run
81 // asynchronously from a different thread (or in the case of a same-thread
82 // channel, from the current thread), for now due to assertions in
83 // `MessageChannel`.
84 nsCOMPtr<nsIRunnable> openRunnable = NewRunnableMethod(
85 "PortLink::Open", mObserver, &PortObserverThunk::OnPortStatusChanged);
86 if (aChan->mIsSameThreadChannel) {
87 aChan->mWorkerThread->Dispatch(openRunnable.forget());
88 } else {
89 XRE_GetIOMessageLoop()->PostTask(openRunnable.forget());
93 PortLink::~PortLink() {
94 MOZ_RELEASE_ASSERT(!mObserver, "PortLink destroyed without being closed!");
97 void PortLink::SendMessage(UniquePtr<Message> aMessage) {
98 mChan->mMonitor->AssertCurrentThreadOwns();
100 if (aMessage->size() > IPC::Channel::kMaximumMessageSize) {
101 CrashReporter::RecordAnnotationCString(
102 CrashReporter::Annotation::IPCMessageName, aMessage->name());
103 CrashReporter::RecordAnnotationU32(
104 CrashReporter::Annotation::IPCMessageSize, aMessage->size());
105 CrashReporter::RecordAnnotationU32(
106 CrashReporter::Annotation::IPCMessageLargeBufferShmemFailureSize,
107 aMessage->LargeBufferShmemFailureSize());
108 MOZ_CRASH("IPC message size is too large");
110 aMessage->AssertAsLargeAsHeader();
112 RefPtr<PortObserverThunk> observer = mObserver;
113 if (!observer) {
114 NS_WARNING("Ignoring message to closed PortLink");
115 return;
118 // Make local copies of relevant member variables, so we can unlock the
119 // monitor for the rest of this function. This protects us in case `this` is
120 // deleted during the call (although that shouldn't happen in practice).
122 // We don't want the monitor to be held when calling into ports, as we may be
123 // re-entrantly called by our `PortObserverThunk` which will attempt to
124 // acquire the monitor.
125 RefPtr<RefCountedMonitor> monitor = mChan->mMonitor;
126 RefPtr<NodeController> node = mNode;
127 PortRef port = mPort;
129 bool ok = false;
130 monitor->AssertCurrentThreadOwns();
132 MonitorAutoUnlock guard(*monitor);
133 ok = node->SendUserMessage(port, std::move(aMessage));
135 if (!ok) {
136 // The send failed, but double-check that we weren't closed racily while
137 // sending, which could lead to an invalid state error.
138 if (observer->mLink) {
139 MOZ_CRASH("Invalid argument to SendUserMessage");
141 NS_WARNING("Message dropped as PortLink was closed");
145 void PortLink::Close() {
146 mChan->mMonitor->AssertCurrentThreadOwns();
148 if (!mObserver) {
149 // We're already being closed.
150 return;
153 Clear();
156 void PortLink::Clear() {
157 mChan->mMonitor->AssertCurrentThreadOwns();
159 // NOTE: We're calling into `ports` with our monitor held! Usually, this could
160 // lead to deadlocks due to the PortObserverThunk acquiring the lock
161 // re-entrantly, but is OK here as we're immediately clearing the port's
162 // observer. We shouldn't have issues with any re-entrant calls on this thread
163 // acquiring this MessageChannel's monitor.
165 // We also clear out the reference in `mObserver` back to this type so that
166 // notifications from other threads won't try to call us again once we release
167 // the monitor.
168 mNode->SetPortObserver(mPort, nullptr);
169 mObserver->mLink = nullptr;
170 mObserver = nullptr;
171 mNode->ClosePort(mPort);
174 void PortLink::OnPortStatusChanged() {
175 mChan->mMonitor->AssertCurrentThreadOwns();
177 // Check if the port's remoteness status has updated, and tell our channel if
178 // it has.
179 if (Maybe<PortStatus> status = mNode->GetStatus(mPort);
180 status && status->peer_remote != mChan->IsCrossProcess()) {
181 mChan->SetIsCrossProcess(status->peer_remote);
184 while (mObserver) {
185 UniquePtr<IPC::Message> message;
186 if (!mNode->GetMessage(mPort, &message)) {
187 Clear();
188 mChan->OnChannelErrorFromLink();
189 return;
191 if (!message) {
192 return;
195 mChan->OnMessageReceivedFromLink(std::move(message));
199 bool PortLink::IsClosed() const {
200 if (Maybe<PortStatus> status = mNode->GetStatus(mPort)) {
201 return !(status->has_messages || status->receiving_messages);
203 return true;
206 } // namespace ipc
207 } // namespace mozilla