1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
ipc / ipc_sync_message_filter.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ipc/ipc_sync_message_filter.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_sync_message.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/sync_handle_registry.h"
namespace IPC {
namespace {
// A generic callback used when watching handles synchronously. Sets |*signal|
// to true.
void OnEventReady(bool* signal) {
*signal = true;
}
} // namespace
bool SyncMessageFilter::Send(Message* message) {
if (!message->is_sync()) {
{
base::AutoLock auto_lock(lock_);
if (!io_task_runner_.get()) {
pending_messages_.emplace_back(base::WrapUnique(message));
return true;
}
}
io_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&SyncMessageFilter::SendOnIOThread, this, message));
return true;
}
auto owned_event = std::make_unique<base::WaitableEvent>(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent* done_event = owned_event.get();
PendingSyncMsg pending_message(
SyncMessage::GetMessageId(*message),
static_cast<SyncMessage*>(message)->TakeReplyDeserializer(),
std::move(owned_event));
{
base::AutoLock auto_lock(lock_);
// Can't use this class on the main thread or else it can lead to deadlocks.
// Also by definition, can't use this on IO thread since we're blocking it.
if (base::SingleThreadTaskRunner::HasCurrentDefault()) {
DCHECK(base::SingleThreadTaskRunner::GetCurrentDefault() !=
listener_task_runner_);
DCHECK(base::SingleThreadTaskRunner::GetCurrentDefault() !=
io_task_runner_);
}
pending_sync_messages_.insert(&pending_message);
if (io_task_runner_.get()) {
io_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&SyncMessageFilter::SendOnIOThread, this, message));
} else {
pending_messages_.emplace_back(base::WrapUnique(message));
}
}
{
bool done = false;
bool shutdown = false;
scoped_refptr<mojo::SyncHandleRegistry> registry =
mojo::SyncHandleRegistry::current();
mojo::SyncHandleRegistry::EventCallbackSubscription shutdown_subscription =
registry->RegisterEvent(shutdown_event_,
base::BindRepeating(&OnEventReady, &shutdown));
mojo::SyncHandleRegistry::EventCallbackSubscription done_subscription =
registry->RegisterEvent(done_event,
base::BindRepeating(&OnEventReady, &done));
const bool* stop_flags[] = {&done, &shutdown};
registry->Wait(stop_flags, 2);
if (done) {
TRACE_EVENT_WITH_FLOW0("toplevel.flow", "SyncMessageFilter::Send",
done_event, TRACE_EVENT_FLAG_FLOW_IN);
}
}
{
base::AutoLock auto_lock(lock_);
pending_sync_messages_.erase(&pending_message);
}
return pending_message.send_result;
}
void SyncMessageFilter::OnFilterAdded(Channel* channel) {
std::vector<std::unique_ptr<Message>> pending_messages;
{
base::AutoLock auto_lock(lock_);
channel_ = channel;
io_task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
std::swap(pending_messages_, pending_messages);
}
for (auto& msg : pending_messages)
SendOnIOThread(msg.release());
}
void SyncMessageFilter::OnChannelError() {
base::AutoLock auto_lock(lock_);
channel_ = nullptr;
SignalAllEvents();
}
void SyncMessageFilter::OnChannelClosing() {
base::AutoLock auto_lock(lock_);
channel_ = nullptr;
SignalAllEvents();
}
bool SyncMessageFilter::OnMessageReceived(const Message& message) {
base::AutoLock auto_lock(lock_);
for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin();
iter != pending_sync_messages_.end(); ++iter) {
if (SyncMessage::IsMessageReplyTo(message, (*iter)->id)) {
if (!message.is_reply_error()) {
(*iter)->send_result =
(*iter)->deserializer->SerializeOutputParameters(message);
}
TRACE_EVENT_WITH_FLOW0(
"toplevel.flow", "SyncMessageFilter::OnMessageReceived",
(*iter)->done_event.get(), TRACE_EVENT_FLAG_FLOW_OUT);
(*iter)->done_event->Signal();
return true;
}
}
return false;
}
SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event)
: channel_(nullptr),
listener_task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
shutdown_event_(shutdown_event) {}
SyncMessageFilter::~SyncMessageFilter() = default;
void SyncMessageFilter::SendOnIOThread(Message* message) {
if (channel_) {
channel_->Send(message);
return;
}
if (message->is_sync()) {
// We don't know which thread sent it, but it doesn't matter, just signal
// them all.
base::AutoLock auto_lock(lock_);
SignalAllEvents();
}
delete message;
}
void SyncMessageFilter::SignalAllEvents() {
lock_.AssertAcquired();
for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin();
iter != pending_sync_messages_.end(); ++iter) {
TRACE_EVENT_WITH_FLOW0(
"toplevel.flow", "SyncMessageFilter::SignalAllEvents",
(*iter)->done_event.get(), TRACE_EVENT_FLAG_FLOW_OUT);
(*iter)->done_event->Signal();
}
}
void SyncMessageFilter::GetRemoteAssociatedInterface(
mojo::GenericPendingAssociatedReceiver receiver) {
base::AutoLock auto_lock(lock_);
DCHECK(io_task_runner_ && io_task_runner_->BelongsToCurrentThread());
if (!channel_) {
// Attach the associated interface to a disconnected pipe, so that the
// associated interface pointer can be used to make calls (which are
// dropped).
mojo::AssociateWithDisconnectedPipe(receiver.PassHandle());
return;
}
Channel::AssociatedInterfaceSupport* support =
channel_->GetAssociatedInterfaceSupport();
support->GetRemoteAssociatedInterface(std::move(receiver));
}
} // namespace IPC