1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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"
20 #include "nsExceptionHandler.h"
21 #include "nsISupportsImpl.h"
22 #include "nsPrintfCString.h"
23 #include "nsXULAppAPI.h"
25 using namespace mozilla
;
30 const char* StringFromIPCSide(Side side
) {
41 MessageLink::MessageLink(MessageChannel
* aChan
) : mChan(aChan
) {}
43 MessageLink::~MessageLink() {
49 class PortLink::PortObserverThunk
: public NodeController::PortObserver
{
51 PortObserverThunk(RefCountedMonitor
* aMonitor
, PortLink
* aLink
)
52 : mMonitor(aMonitor
), mLink(aLink
) {}
54 void OnPortStatusChanged() override
{
55 MonitorAutoLock
lock(*mMonitor
);
57 mLink
->OnPortStatusChanged();
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
83 nsCOMPtr
<nsIRunnable
> openRunnable
= NewRunnableMethod(
84 "PortLink::Open", mObserver
, &PortObserverThunk::OnPortStatusChanged
);
85 if (aChan
->mIsSameThreadChannel
) {
86 aChan
->mWorkerThread
->Dispatch(openRunnable
.forget());
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
;
113 NS_WARNING("Ignoring message to closed PortLink");
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
;
129 monitor
->AssertCurrentThreadOwns();
131 MonitorAutoUnlock
guard(*monitor
);
132 ok
= node
->SendUserMessage(port
, std::move(aMessage
));
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();
148 // We're already being closed.
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
167 mNode
->SetPortObserver(mPort
, nullptr);
168 mObserver
->mLink
= 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
178 if (Maybe
<PortStatus
> status
= mNode
->GetStatus(mPort
);
179 status
&& status
->peer_remote
!= mChan
->IsCrossProcess()) {
180 mChan
->SetIsCrossProcess(status
->peer_remote
);
184 UniquePtr
<IPC::Message
> message
;
185 if (!mNode
->GetMessage(mPort
, &message
)) {
187 mChan
->OnChannelErrorFromLink();
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
);
206 } // namespace mozilla