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
) {
74 std::vector
<Message
*> pending_messages
;
76 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() {
86 base::AutoLock
auto_lock(lock_
);
91 void SyncMessageFilter::OnChannelClosing() {
92 base::AutoLock
auto_lock(lock_
);
97 bool SyncMessageFilter::OnMessageReceived(const Message
& message
) {
98 base::AutoLock
auto_lock(lock_
);
99 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
100 iter
!= pending_sync_messages_
.end(); ++iter
) {
101 if (SyncMessage::IsMessageReplyTo(message
, (*iter
)->id
)) {
102 if (!message
.is_reply_error()) {
103 (*iter
)->send_result
=
104 (*iter
)->deserializer
->SerializeOutputParameters(message
);
106 (*iter
)->done_event
->Signal();
114 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent
* shutdown_event
,
115 bool is_channel_send_thread_safe
)
117 is_channel_send_thread_safe_(is_channel_send_thread_safe
),
118 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
119 shutdown_event_(shutdown_event
) {
122 SyncMessageFilter::~SyncMessageFilter() {
125 void SyncMessageFilter::SendOnIOThread(Message
* message
) {
127 sender_
->Send(message
);
131 if (message
->is_sync()) {
132 // We don't know which thread sent it, but it doesn't matter, just signal
134 base::AutoLock
auto_lock(lock_
);
141 void SyncMessageFilter::SignalAllEvents() {
142 lock_
.AssertAcquired();
143 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
144 iter
!= pending_sync_messages_
.end(); ++iter
) {
145 (*iter
)->done_event
->Signal();