Revert of Fix cancellation of a pair of URLRequestJob subclasses (patchset #6 id...
[chromium-blink-merge.git] / ipc / ipc_sync_message_filter.cc
blobf6d485dd4b77d96ec81ba4395142ecb7750d0179
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 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);
24 return true;
25 } else if (!io_task_runner_.get()) {
26 pending_messages_.push_back(message);
27 return true;
30 io_task_runner_->PostTask(
31 FROM_HERE,
32 base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
33 return true;
36 base::WaitableEvent done_event(true, false);
37 PendingSyncMsg pending_message(
38 SyncMessage::GetMessageId(*message),
39 static_cast<SyncMessage*>(message)->GetReplyDeserializer(),
40 &done_event);
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(
54 FROM_HERE,
55 base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
56 } else {
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 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)
82 SendOnIOThread(msg);
85 void SyncMessageFilter::OnChannelError() {
86 sender_ = NULL;
87 SignalAllEvents();
90 void SyncMessageFilter::OnChannelClosing() {
91 sender_ = NULL;
92 SignalAllEvents();
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();
105 return true;
109 return false;
112 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event,
113 bool is_channel_send_thread_safe)
114 : sender_(NULL),
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) {
124 if (sender_) {
125 sender_->Send(message);
126 return;
129 if (message->is_sync()) {
130 // We don't know which thread sent it, but it doesn't matter, just signal
131 // them all.
132 SignalAllEvents();
135 delete message;
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();
146 } // namespace IPC