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 bool SyncMessageFilter::Send(Message
* message
) {
19 if (!message
->is_sync()) {
21 base::AutoLock
auto_lock(lock_
);
22 if (sender_
&& is_channel_send_thread_safe_
) {
23 sender_
->Send(message
);
25 } else if (!io_task_runner_
.get()) {
26 pending_messages_
.push_back(message
);
30 io_task_runner_
->PostTask(
32 base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
36 base::WaitableEvent
done_event(true, false);
37 PendingSyncMsg
pending_message(
38 SyncMessage::GetMessageId(*message
),
39 static_cast<SyncMessage
*>(message
)->GetReplyDeserializer(),
43 base::AutoLock
auto_lock(lock_
);
44 // Can't use this class on the main thread or else it can lead to deadlocks.
45 // Also by definition, can't use this on IO thread since we're blocking it.
46 if (base::ThreadTaskRunnerHandle::IsSet()) {
47 DCHECK(base::ThreadTaskRunnerHandle::Get() != listener_task_runner_
);
48 DCHECK(base::ThreadTaskRunnerHandle::Get() != io_task_runner_
);
50 pending_sync_messages_
.insert(&pending_message
);
52 if (io_task_runner_
.get()) {
53 io_task_runner_
->PostTask(
55 base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
57 pending_messages_
.push_back(message
);
61 base::WaitableEvent
* events
[2] = { shutdown_event_
, &done_event
};
62 base::WaitableEvent::WaitMany(events
, 2);
65 base::AutoLock
auto_lock(lock_
);
66 delete pending_message
.deserializer
;
67 pending_sync_messages_
.erase(&pending_message
);
70 return pending_message
.send_result
;
73 void SyncMessageFilter::OnFilterAdded(Sender
* sender
) {
75 std::vector
<Message
*> pending_messages
;
77 base::AutoLock
auto_lock(lock_
);
78 io_task_runner_
= base::ThreadTaskRunnerHandle::Get();
79 pending_messages_
.release(&pending_messages
);
81 for (auto* msg
: pending_messages
)
85 void SyncMessageFilter::OnChannelError() {
90 void SyncMessageFilter::OnChannelClosing() {
95 bool SyncMessageFilter::OnMessageReceived(const Message
& message
) {
96 base::AutoLock
auto_lock(lock_
);
97 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
98 iter
!= pending_sync_messages_
.end(); ++iter
) {
99 if (SyncMessage::IsMessageReplyTo(message
, (*iter
)->id
)) {
100 if (!message
.is_reply_error()) {
101 (*iter
)->send_result
=
102 (*iter
)->deserializer
->SerializeOutputParameters(message
);
104 (*iter
)->done_event
->Signal();
112 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent
* shutdown_event
,
113 bool is_channel_send_thread_safe
)
115 is_channel_send_thread_safe_(is_channel_send_thread_safe
),
116 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
117 shutdown_event_(shutdown_event
) {
120 SyncMessageFilter::~SyncMessageFilter() {
123 void SyncMessageFilter::SendOnIOThread(Message
* message
) {
125 sender_
->Send(message
);
129 if (message
->is_sync()) {
130 // We don't know which thread sent it, but it doesn't matter, just signal
138 void SyncMessageFilter::SignalAllEvents() {
139 base::AutoLock
auto_lock(lock_
);
140 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
141 iter
!= pending_sync_messages_
.end(); ++iter
) {
142 (*iter
)->done_event
->Signal();