Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / glue / MessageLink.cpp
blobf0fc5ac2f376737d96a6494ccb41dd72044d291f
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/ProtocolUtils.h"
13 #include "mozilla/ipc/NodeController.h"
14 #include "chrome/common/ipc_channel.h"
15 #include "base/task.h"
17 #include "mozilla/Assertions.h"
18 #include "mozilla/DebugOnly.h"
19 #include "nsDebug.h"
20 #include "nsExceptionHandler.h"
21 #include "nsISupportsImpl.h"
22 #include "nsPrintfCString.h"
23 #include "nsXULAppAPI.h"
25 using namespace mozilla;
27 namespace mozilla {
28 namespace ipc {
30 const char* StringFromIPCSide(Side side) {
31 switch (side) {
32 case ChildSide:
33 return "Child";
34 case ParentSide:
35 return "Parent";
36 default:
37 return "Unknown";
41 MessageLink::MessageLink(MessageChannel* aChan) : mChan(aChan) {}
43 MessageLink::~MessageLink() {
44 #ifdef DEBUG
45 mChan = nullptr;
46 #endif
49 class PortLink::PortObserverThunk : public NodeController::PortObserver {
50 public:
51 PortObserverThunk(RefCountedMonitor* aMonitor, PortLink* aLink)
52 : mMonitor(aMonitor), mLink(aLink) {}
54 void OnPortStatusChanged() override {
55 MonitorAutoLock lock(*mMonitor);
56 if (mLink) {
57 mLink->OnPortStatusChanged();
61 private:
62 friend class PortLink;
64 // The monitor from our PortLink's MessageChannel. Guards access to `mLink`.
65 RefPtr<RefCountedMonitor> mMonitor;
67 // Cleared by `PortLink` in `PortLink::Clear()`.
68 PortLink* MOZ_NON_OWNING_REF mLink;
71 PortLink::PortLink(MessageChannel* aChan, ScopedPort aPort)
72 : MessageLink(aChan), mNode(aPort.Controller()), mPort(aPort.Release()) {
73 mChan->mMonitor->AssertCurrentThreadOwns();
75 mObserver = new PortObserverThunk(mChan->mMonitor, this);
76 mNode->SetPortObserver(mPort, mObserver);
78 // Dispatch an event to the IO loop to trigger an initial
79 // `OnPortStatusChanged` to deliver any pending messages. This needs to be run
80 // asynchronously from a different thread (or in the case of a same-thread
81 // channel, from the current thread), for now due to assertions in
82 // `MessageChannel`.
83 nsCOMPtr<nsIRunnable> openRunnable = NewRunnableMethod(
84 "PortLink::Open", mObserver, &PortObserverThunk::OnPortStatusChanged);
85 if (aChan->mIsSameThreadChannel) {
86 aChan->mWorkerThread->Dispatch(openRunnable.forget());
87 } else {
88 XRE_GetAsyncIOEventTarget()->Dispatch(openRunnable.forget());
92 PortLink::~PortLink() {
93 MOZ_RELEASE_ASSERT(!mObserver, "PortLink destroyed without being closed!");
96 void PortLink::SendMessage(UniquePtr<Message> aMessage) {
97 mChan->mMonitor->AssertCurrentThreadOwns();
99 if (aMessage->size() > IPC::Channel::kMaximumMessageSize) {
100 CrashReporter::RecordAnnotationCString(
101 CrashReporter::Annotation::IPCMessageName, aMessage->name());
102 CrashReporter::RecordAnnotationU32(
103 CrashReporter::Annotation::IPCMessageSize, aMessage->size());
104 CrashReporter::RecordAnnotationU32(
105 CrashReporter::Annotation::IPCMessageLargeBufferShmemFailureSize,
106 aMessage->LargeBufferShmemFailureSize());
107 MOZ_CRASH("IPC message size is too large");
109 aMessage->AssertAsLargeAsHeader();
111 RefPtr<PortObserverThunk> observer = mObserver;
112 if (!observer) {
113 NS_WARNING("Ignoring message to closed PortLink");
114 return;
117 // Make local copies of relevant member variables, so we can unlock the
118 // monitor for the rest of this function. This protects us in case `this` is
119 // deleted during the call (although that shouldn't happen in practice).
121 // We don't want the monitor to be held when calling into ports, as we may be
122 // re-entrantly called by our `PortObserverThunk` which will attempt to
123 // acquire the monitor.
124 RefPtr<RefCountedMonitor> monitor = mChan->mMonitor;
125 RefPtr<NodeController> node = mNode;
126 PortRef port = mPort;
128 bool ok = false;
129 monitor->AssertCurrentThreadOwns();
131 MonitorAutoUnlock guard(*monitor);
132 ok = node->SendUserMessage(port, std::move(aMessage));
134 if (!ok) {
135 // The send failed, but double-check that we weren't closed racily while
136 // sending, which could lead to an invalid state error.
137 if (observer->mLink) {
138 MOZ_CRASH("Invalid argument to SendUserMessage");
140 NS_WARNING("Message dropped as PortLink was closed");
144 void PortLink::Close() {
145 mChan->mMonitor->AssertCurrentThreadOwns();
147 if (!mObserver) {
148 // We're already being closed.
149 return;
152 Clear();
155 void PortLink::Clear() {
156 mChan->mMonitor->AssertCurrentThreadOwns();
158 // NOTE: We're calling into `ports` with our monitor held! Usually, this could
159 // lead to deadlocks due to the PortObserverThunk acquiring the lock
160 // re-entrantly, but is OK here as we're immediately clearing the port's
161 // observer. We shouldn't have issues with any re-entrant calls on this thread
162 // acquiring this MessageChannel's monitor.
164 // We also clear out the reference in `mObserver` back to this type so that
165 // notifications from other threads won't try to call us again once we release
166 // the monitor.
167 mNode->SetPortObserver(mPort, nullptr);
168 mObserver->mLink = nullptr;
169 mObserver = nullptr;
170 mNode->ClosePort(mPort);
173 void PortLink::OnPortStatusChanged() {
174 mChan->mMonitor->AssertCurrentThreadOwns();
176 // Check if the port's remoteness status has updated, and tell our channel if
177 // it has.
178 if (Maybe<PortStatus> status = mNode->GetStatus(mPort);
179 status && status->peer_remote != mChan->IsCrossProcess()) {
180 mChan->SetIsCrossProcess(status->peer_remote);
183 while (mObserver) {
184 UniquePtr<IPC::Message> message;
185 if (!mNode->GetMessage(mPort, &message)) {
186 Clear();
187 mChan->OnChannelErrorFromLink();
188 return;
190 if (!message) {
191 return;
194 mChan->OnMessageReceivedFromLink(std::move(message));
198 bool PortLink::IsClosed() const {
199 if (Maybe<PortStatus> status = mNode->GetStatus(mPort)) {
200 return !(status->has_messages || status->receiving_messages);
202 return true;
205 } // namespace ipc
206 } // namespace mozilla