1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ipc/ipc_sync_message_filter.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "ipc/ipc_channel.h"
14 #include "ipc/ipc_sync_message.h"
18 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent
* shutdown_event
)
20 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
21 shutdown_event_(shutdown_event
) {
24 bool SyncMessageFilter::Send(Message
* message
) {
25 if (!message
->is_sync()) {
27 base::AutoLock
auto_lock(lock_
);
28 if (!io_task_runner_
.get()) {
29 pending_messages_
.push_back(message
);
33 io_task_runner_
->PostTask(
35 base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
39 base::WaitableEvent
done_event(true, false);
40 PendingSyncMsg
pending_message(
41 SyncMessage::GetMessageId(*message
),
42 static_cast<SyncMessage
*>(message
)->GetReplyDeserializer(),
46 base::AutoLock
auto_lock(lock_
);
47 // Can't use this class on the main thread or else it can lead to deadlocks.
48 // Also by definition, can't use this on IO thread since we're blocking it.
49 if (base::ThreadTaskRunnerHandle::IsSet()) {
50 DCHECK(base::ThreadTaskRunnerHandle::Get() != listener_task_runner_
);
51 DCHECK(base::ThreadTaskRunnerHandle::Get() != io_task_runner_
);
53 pending_sync_messages_
.insert(&pending_message
);
55 if (io_task_runner_
.get()) {
56 io_task_runner_
->PostTask(
58 base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
60 pending_messages_
.push_back(message
);
64 base::WaitableEvent
* events
[2] = { shutdown_event_
, &done_event
};
65 base::WaitableEvent::WaitMany(events
, 2);
68 base::AutoLock
auto_lock(lock_
);
69 delete pending_message
.deserializer
;
70 pending_sync_messages_
.erase(&pending_message
);
73 return pending_message
.send_result
;
76 void SyncMessageFilter::OnFilterAdded(Sender
* sender
) {
78 std::vector
<Message
*> pending_messages
;
80 base::AutoLock
auto_lock(lock_
);
81 io_task_runner_
= base::ThreadTaskRunnerHandle::Get();
82 pending_messages_
.release(&pending_messages
);
84 for (auto* msg
: pending_messages
)
88 void SyncMessageFilter::OnChannelError() {
93 void SyncMessageFilter::OnChannelClosing() {
98 bool SyncMessageFilter::OnMessageReceived(const Message
& message
) {
99 base::AutoLock
auto_lock(lock_
);
100 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
101 iter
!= pending_sync_messages_
.end(); ++iter
) {
102 if (SyncMessage::IsMessageReplyTo(message
, (*iter
)->id
)) {
103 if (!message
.is_reply_error()) {
104 (*iter
)->send_result
=
105 (*iter
)->deserializer
->SerializeOutputParameters(message
);
107 (*iter
)->done_event
->Signal();
115 SyncMessageFilter::~SyncMessageFilter() {
118 void SyncMessageFilter::SendOnIOThread(Message
* message
) {
120 sender_
->Send(message
);
124 if (message
->is_sync()) {
125 // We don't know which thread sent it, but it doesn't matter, just signal
133 void SyncMessageFilter::SignalAllEvents() {
134 base::AutoLock
auto_lock(lock_
);
135 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
136 iter
!= pending_sync_messages_
.end(); ++iter
) {
137 (*iter
)->done_event
->Signal();