OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_sync_message_filter.h" | 5 #include "ipc/ipc_sync_message_filter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "ipc/ipc_sync_message.h" | 10 #include "ipc/ipc_sync_message.h" |
11 | 11 |
12 namespace IPC { | 12 namespace IPC { |
13 | 13 |
14 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event) | 14 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event) |
15 : channel_(NULL), | 15 : channel_(NULL), |
16 listener_loop_(MessageLoop::current()), | 16 listener_loop_(MessageLoop::current()), |
17 io_loop_(NULL), | 17 io_loop_(NULL), |
18 shutdown_event_(shutdown_event) { | 18 shutdown_event_(shutdown_event) { |
19 } | 19 } |
20 | 20 |
21 SyncMessageFilter::~SyncMessageFilter() { | 21 SyncMessageFilter::~SyncMessageFilter() { |
22 } | 22 } |
23 | 23 |
24 bool SyncMessageFilter::Send(Message* message) { | 24 bool SyncMessageFilter::Send(Message* message) { |
25 { | 25 { |
26 AutoLock auto_lock(lock_); | 26 base::AutoLock auto_lock(lock_); |
27 if (!io_loop_) { | 27 if (!io_loop_) { |
28 delete message; | 28 delete message; |
29 return false; | 29 return false; |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 if (!message->is_sync()) { | 33 if (!message->is_sync()) { |
34 io_loop_->PostTask( | 34 io_loop_->PostTask( |
35 FROM_HERE, | 35 FROM_HERE, |
36 NewRunnableMethod(this, &SyncMessageFilter::SendOnIOThread, message)); | 36 NewRunnableMethod(this, &SyncMessageFilter::SendOnIOThread, message)); |
37 return true; | 37 return true; |
38 } | 38 } |
39 | 39 |
40 base::WaitableEvent done_event(true, false); | 40 base::WaitableEvent done_event(true, false); |
41 PendingSyncMsg pending_message( | 41 PendingSyncMsg pending_message( |
42 SyncMessage::GetMessageId(*message), | 42 SyncMessage::GetMessageId(*message), |
43 reinterpret_cast<SyncMessage*>(message)->GetReplyDeserializer(), | 43 reinterpret_cast<SyncMessage*>(message)->GetReplyDeserializer(), |
44 &done_event); | 44 &done_event); |
45 | 45 |
46 { | 46 { |
47 AutoLock auto_lock(lock_); | 47 base::AutoLock auto_lock(lock_); |
48 // Can't use this class on the main thread or else it can lead to deadlocks. | 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. | 49 // Also by definition, can't use this on IO thread since we're blocking it. |
50 DCHECK(MessageLoop::current() != listener_loop_); | 50 DCHECK(MessageLoop::current() != listener_loop_); |
51 DCHECK(MessageLoop::current() != io_loop_); | 51 DCHECK(MessageLoop::current() != io_loop_); |
52 pending_sync_messages_.insert(&pending_message); | 52 pending_sync_messages_.insert(&pending_message); |
53 } | 53 } |
54 | 54 |
55 io_loop_->PostTask( | 55 io_loop_->PostTask( |
56 FROM_HERE, | 56 FROM_HERE, |
57 NewRunnableMethod(this, &SyncMessageFilter::SendOnIOThread, message)); | 57 NewRunnableMethod(this, &SyncMessageFilter::SendOnIOThread, message)); |
58 | 58 |
59 base::WaitableEvent* events[2] = { shutdown_event_, &done_event }; | 59 base::WaitableEvent* events[2] = { shutdown_event_, &done_event }; |
60 base::WaitableEvent::WaitMany(events, 2); | 60 base::WaitableEvent::WaitMany(events, 2); |
61 | 61 |
62 { | 62 { |
63 AutoLock auto_lock(lock_); | 63 base::AutoLock auto_lock(lock_); |
64 delete pending_message.deserializer; | 64 delete pending_message.deserializer; |
65 pending_sync_messages_.erase(&pending_message); | 65 pending_sync_messages_.erase(&pending_message); |
66 } | 66 } |
67 | 67 |
68 return pending_message.send_result; | 68 return pending_message.send_result; |
69 } | 69 } |
70 | 70 |
71 void SyncMessageFilter::SendOnIOThread(Message* message) { | 71 void SyncMessageFilter::SendOnIOThread(Message* message) { |
72 if (channel_) { | 72 if (channel_) { |
73 channel_->Send(message); | 73 channel_->Send(message); |
74 return; | 74 return; |
75 } | 75 } |
76 | 76 |
77 if (message->is_sync()) { | 77 if (message->is_sync()) { |
78 // We don't know which thread sent it, but it doesn't matter, just signal | 78 // We don't know which thread sent it, but it doesn't matter, just signal |
79 // them all. | 79 // them all. |
80 SignalAllEvents(); | 80 SignalAllEvents(); |
81 } | 81 } |
82 | 82 |
83 delete message; | 83 delete message; |
84 } | 84 } |
85 | 85 |
86 void SyncMessageFilter::SignalAllEvents() { | 86 void SyncMessageFilter::SignalAllEvents() { |
87 AutoLock auto_lock(lock_); | 87 base::AutoLock auto_lock(lock_); |
88 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); | 88 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); |
89 iter != pending_sync_messages_.end(); ++iter) { | 89 iter != pending_sync_messages_.end(); ++iter) { |
90 (*iter)->done_event->Signal(); | 90 (*iter)->done_event->Signal(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 void SyncMessageFilter::OnFilterAdded(Channel* channel) { | 94 void SyncMessageFilter::OnFilterAdded(Channel* channel) { |
95 channel_ = channel; | 95 channel_ = channel; |
96 AutoLock auto_lock(lock_); | 96 base::AutoLock auto_lock(lock_); |
97 io_loop_ = MessageLoop::current(); | 97 io_loop_ = MessageLoop::current(); |
98 } | 98 } |
99 | 99 |
100 void SyncMessageFilter::OnChannelError() { | 100 void SyncMessageFilter::OnChannelError() { |
101 channel_ = NULL; | 101 channel_ = NULL; |
102 SignalAllEvents(); | 102 SignalAllEvents(); |
103 } | 103 } |
104 | 104 |
105 void SyncMessageFilter::OnChannelClosing() { | 105 void SyncMessageFilter::OnChannelClosing() { |
106 channel_ = NULL; | 106 channel_ = NULL; |
107 SignalAllEvents(); | 107 SignalAllEvents(); |
108 } | 108 } |
109 | 109 |
110 bool SyncMessageFilter::OnMessageReceived(const Message& message) { | 110 bool SyncMessageFilter::OnMessageReceived(const Message& message) { |
111 AutoLock auto_lock(lock_); | 111 base::AutoLock auto_lock(lock_); |
112 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); | 112 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); |
113 iter != pending_sync_messages_.end(); ++iter) { | 113 iter != pending_sync_messages_.end(); ++iter) { |
114 if (SyncMessage::IsMessageReplyTo(message, (*iter)->id)) { | 114 if (SyncMessage::IsMessageReplyTo(message, (*iter)->id)) { |
115 if (!message.is_reply_error()) { | 115 if (!message.is_reply_error()) { |
116 (*iter)->send_result = | 116 (*iter)->send_result = |
117 (*iter)->deserializer->SerializeOutputParameters(message); | 117 (*iter)->deserializer->SerializeOutputParameters(message); |
118 } | 118 } |
119 (*iter)->done_event->Signal(); | 119 (*iter)->done_event->Signal(); |
120 return true; | 120 return true; |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 return false; | 124 return false; |
125 } | 125 } |
126 | 126 |
127 } // namespace IPC | 127 } // namespace IPC |
OLD | NEW |