ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / ipc / ipc_sync_message_filter.cc
blob6a408ec12cf3b044f2426da46f659d640bec9e6d
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) {
26 base::AutoLock auto_lock(lock_);
27 if (!io_task_runner_.get()) {
28 delete message;
29 return false;
33 if (!message->is_sync()) {
34 io_task_runner_->PostTask(
35 FROM_HERE,
36 base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
37 return true;
40 base::WaitableEvent done_event(true, false);
41 PendingSyncMsg pending_message(
42 SyncMessage::GetMessageId(*message),
43 static_cast<SyncMessage*>(message)->GetReplyDeserializer(),
44 &done_event);
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) {
73 sender_ = sender;
74 base::AutoLock auto_lock(lock_);
75 io_task_runner_ = base::ThreadTaskRunnerHandle::Get();
78 void SyncMessageFilter::OnChannelError() {
79 sender_ = NULL;
80 SignalAllEvents();
83 void SyncMessageFilter::OnChannelClosing() {
84 sender_ = NULL;
85 SignalAllEvents();
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();
98 return true;
102 return false;
105 SyncMessageFilter::~SyncMessageFilter() {
108 void SyncMessageFilter::SendOnIOThread(Message* message) {
109 if (sender_) {
110 sender_->Send(message);
111 return;
114 if (message->is_sync()) {
115 // We don't know which thread sent it, but it doesn't matter, just signal
116 // them all.
117 SignalAllEvents();
120 delete message;
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();
131 } // namespace IPC