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
) {
26 base::AutoLock
auto_lock(lock_
);
27 if (!io_task_runner_
.get()) {
33 if (!message
->is_sync()) {
34 io_task_runner_
->PostTask(
36 base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
40 base::WaitableEvent
done_event(true, false);
41 PendingSyncMsg
pending_message(
42 SyncMessage::GetMessageId(*message
),
43 static_cast<SyncMessage
*>(message
)->GetReplyDeserializer(),
47 base::AutoLock
auto_lock(lock_
);
48 // Can't use this class on the main thread or else it can lead to deadlocks.
49 // Also by definition, can't use this on IO thread since we're blocking it.
50 if (base::ThreadTaskRunnerHandle::IsSet()) {
51 DCHECK(base::ThreadTaskRunnerHandle::Get() != listener_task_runner_
);
52 DCHECK(base::ThreadTaskRunnerHandle::Get() != io_task_runner_
);
54 pending_sync_messages_
.insert(&pending_message
);
57 io_task_runner_
->PostTask(
58 FROM_HERE
, base::Bind(&SyncMessageFilter::SendOnIOThread
, this, message
));
60 base::WaitableEvent
* events
[2] = { shutdown_event_
, &done_event
};
61 base::WaitableEvent::WaitMany(events
, 2);
64 base::AutoLock
auto_lock(lock_
);
65 delete pending_message
.deserializer
;
66 pending_sync_messages_
.erase(&pending_message
);
69 return pending_message
.send_result
;
72 void SyncMessageFilter::OnFilterAdded(Sender
* sender
) {
74 base::AutoLock
auto_lock(lock_
);
75 io_task_runner_
= base::ThreadTaskRunnerHandle::Get();
78 void SyncMessageFilter::OnChannelError() {
83 void SyncMessageFilter::OnChannelClosing() {
88 bool SyncMessageFilter::OnMessageReceived(const Message
& message
) {
89 base::AutoLock
auto_lock(lock_
);
90 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
91 iter
!= pending_sync_messages_
.end(); ++iter
) {
92 if (SyncMessage::IsMessageReplyTo(message
, (*iter
)->id
)) {
93 if (!message
.is_reply_error()) {
94 (*iter
)->send_result
=
95 (*iter
)->deserializer
->SerializeOutputParameters(message
);
97 (*iter
)->done_event
->Signal();
105 SyncMessageFilter::~SyncMessageFilter() {
108 void SyncMessageFilter::SendOnIOThread(Message
* message
) {
110 sender_
->Send(message
);
114 if (message
->is_sync()) {
115 // We don't know which thread sent it, but it doesn't matter, just signal
123 void SyncMessageFilter::SignalAllEvents() {
124 base::AutoLock
auto_lock(lock_
);
125 for (PendingSyncMessages::iterator iter
= pending_sync_messages_
.begin();
126 iter
!= pending_sync_messages_
.end(); ++iter
) {
127 (*iter
)->done_event
->Signal();