Make RuleBasedHostResolverProc methods to add/remove rules threadsafe.
[chromium-blink-merge.git] / ipc / ipc_sync_message_filter.cc
blobd86835d11973ec5427cd7a461652fc32dbf06ae9
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"
7 #include "base/bind.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"
16 namespace IPC {
18 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event)
19 : sender_(NULL),
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);
30 return true;
33 io_task_runner_->PostTask(
34 FROM_HERE,
35 base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
36 return true;
39 base::WaitableEvent done_event(true, false);
40 PendingSyncMsg pending_message(
41 SyncMessage::GetMessageId(*message),
42 static_cast<SyncMessage*>(message)->GetReplyDeserializer(),
43 &done_event);
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(
57 FROM_HERE,
58 base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
59 } else {
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) {
77 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)
85 SendOnIOThread(msg);
88 void SyncMessageFilter::OnChannelError() {
89 sender_ = NULL;
90 SignalAllEvents();
93 void SyncMessageFilter::OnChannelClosing() {
94 sender_ = NULL;
95 SignalAllEvents();
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();
108 return true;
112 return false;
115 SyncMessageFilter::~SyncMessageFilter() {
118 void SyncMessageFilter::SendOnIOThread(Message* message) {
119 if (sender_) {
120 sender_->Send(message);
121 return;
124 if (message->is_sync()) {
125 // We don't know which thread sent it, but it doesn't matter, just signal
126 // them all.
127 SignalAllEvents();
130 delete message;
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();
141 } // namespace IPC