license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | #include "chrome/common/ipc_sync_channel.h" |
| 6 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 7 | #include "base/lazy_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 9 | #include "base/thread_local.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 10 | #include "base/message_loop.h" |
| 11 | #include "base/waitable_event.h" |
| 12 | #include "base/waitable_event_watcher.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "chrome/common/ipc_logging.h" |
| 14 | #include "chrome/common/ipc_sync_message.h" |
| 15 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 16 | #if !defined(OS_WIN) |
| 17 | #define INFINITE -1 |
| 18 | #endif |
| 19 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 20 | using base::TimeDelta; |
| 21 | using base::TimeTicks; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 22 | using base::WaitableEvent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
| 24 | namespace IPC { |
| 25 | // When we're blocked in a Send(), we need to process incoming synchronous |
| 26 | // messages right away because it could be blocking our reply (either |
| 27 | // directly from the same object we're calling, or indirectly through one or |
| 28 | // more other channels). That means that in SyncContext's OnMessageReceived, |
| 29 | // we need to process sync message right away if we're blocked. However a |
| 30 | // simple check isn't sufficient, because the listener thread can be in the |
| 31 | // process of calling Send. |
| 32 | // To work around this, when SyncChannel filters a sync message, it sets |
| 33 | // an event that the listener thread waits on during its Send() call. This |
| 34 | // allows us to dispatch incoming sync messages when blocked. The race |
| 35 | // condition is handled because if Send is in the process of being called, it |
| 36 | // will check the event. In case the listener thread isn't sending a message, |
| 37 | // we queue a task on the listener thread to dispatch the received messages. |
| 38 | // The messages are stored in this queue object that's shared among all |
| 39 | // SyncChannel objects on the same thread (since one object can receive a |
| 40 | // sync message while another one is blocked). |
| 41 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | class SyncChannel::ReceivedSyncMsgQueue : |
| 43 | public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> { |
| 44 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 45 | // Returns the ReceivedSyncMsgQueue instance for this thread, creating one |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 46 | // if necessary. Call RemoveContext on the same thread when done. |
| 47 | static ReceivedSyncMsgQueue* AddContext() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 48 | // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple |
| 49 | // SyncChannel objects can block the same thread). |
| 50 | ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get(); |
| 51 | if (!rv) { |
| 52 | rv = new ReceivedSyncMsgQueue(); |
| 53 | ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv); |
| 54 | } |
| 55 | rv->listener_count_++; |
| 56 | return rv; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ~ReceivedSyncMsgQueue() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Called on IPC thread when a synchronous message or reply arrives. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 63 | void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | bool was_task_pending; |
| 65 | { |
| 66 | AutoLock auto_lock(message_lock_); |
| 67 | |
| 68 | was_task_pending = task_pending_; |
| 69 | task_pending_ = true; |
| 70 | |
| 71 | // We set the event in case the listener thread is blocked (or is about |
| 72 | // to). In case it's not, the PostTask dispatches the messages. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 73 | message_queue_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 76 | dispatch_event_.Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | if (!was_task_pending) { |
| 78 | listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 79 | this, &ReceivedSyncMsgQueue::DispatchMessagesTask)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void QueueReply(const Message &msg, SyncChannel::SyncContext* context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 84 | received_replies_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 87 | // Called on the listener's thread to process any queues synchronous |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | // messages. |
| 89 | void DispatchMessagesTask() { |
| 90 | { |
| 91 | AutoLock auto_lock(message_lock_); |
| 92 | task_pending_ = false; |
| 93 | } |
| 94 | DispatchMessages(); |
| 95 | } |
| 96 | |
| 97 | void DispatchMessages() { |
| 98 | while (true) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 99 | Message* message; |
| 100 | scoped_refptr<SyncChannel::SyncContext> context; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | { |
| 102 | AutoLock auto_lock(message_lock_); |
| 103 | if (message_queue_.empty()) |
| 104 | break; |
| 105 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 106 | message = message_queue_.front().message; |
| 107 | context = message_queue_.front().context; |
| 108 | message_queue_.pop_front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 112 | Logging* logger = Logging::current(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | if (logger->Enabled()) |
| 114 | logger->OnPreDispatchMessage(*message); |
| 115 | #endif |
| 116 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 117 | if (context->listener()) |
| 118 | context->listener()->OnMessageReceived(*message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
| 120 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 121 | if (logger->Enabled()) |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 122 | logger->OnPostDispatchMessage(*message, context->channel_id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | #endif |
| 124 | |
| 125 | delete message; |
| 126 | } |
| 127 | } |
| 128 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | // SyncChannel calls this in its destructor. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 130 | void RemoveContext(SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | AutoLock auto_lock(message_lock_); |
| 132 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 133 | SyncMessageQueue::iterator iter = message_queue_.begin(); |
| 134 | while (iter != message_queue_.end()) { |
| 135 | if (iter->context == context) { |
| 136 | delete iter->message; |
| 137 | iter = message_queue_.erase(iter); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | } else { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 139 | iter++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 142 | |
| 143 | if (--listener_count_ == 0) { |
| 144 | DCHECK(lazy_tls_ptr_.Pointer()->Get()); |
| 145 | lazy_tls_ptr_.Pointer()->Set(NULL); |
| 146 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 149 | WaitableEvent* dispatch_event() { return &dispatch_event_; } |
[email protected] | 8fd8de9 | 2008-08-12 23:50:30 | [diff] [blame] | 150 | MessageLoop* listener_message_loop() { return listener_message_loop_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 152 | // Holds a pointer to the per-thread ReceivedSyncMsgQueue object. |
| 153 | static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> > |
| 154 | lazy_tls_ptr_; |
| 155 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | // Called on the ipc thread to check if we can unblock any current Send() |
| 157 | // calls based on a queued reply. |
| 158 | void DispatchReplies() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | for (size_t i = 0; i < received_replies_.size(); ++i) { |
| 160 | Message* message = received_replies_[i].message; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 161 | if (received_replies_[i].context->TryToUnblockListener(message)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | delete message; |
| 163 | received_replies_.erase(received_replies_.begin() + i); |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 169 | private: |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 170 | // See the comment in SyncChannel::SyncChannel for why this event is created |
| 171 | // as manual reset. |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 172 | ReceivedSyncMsgQueue() : |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 173 | dispatch_event_(true, false), |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 174 | listener_message_loop_(MessageLoop::current()), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 175 | task_pending_(false), |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 176 | listener_count_(0) { |
| 177 | } |
| 178 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 179 | // Holds information about a queued synchronous message or reply. |
| 180 | struct QueuedMessage { |
| 181 | QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | Message* message; |
| 183 | scoped_refptr<SyncChannel::SyncContext> context; |
| 184 | }; |
| 185 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 186 | typedef std::deque<QueuedMessage> SyncMessageQueue; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 187 | SyncMessageQueue message_queue_; |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 188 | |
| 189 | std::vector<QueuedMessage> received_replies_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 190 | |
| 191 | // Set when we got a synchronous message that we must respond to as the |
| 192 | // sender needs its reply before it can reply to our original synchronous |
| 193 | // message. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 194 | WaitableEvent dispatch_event_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 195 | MessageLoop* listener_message_loop_; |
| 196 | Lock message_lock_; |
| 197 | bool task_pending_; |
| 198 | int listener_count_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | }; |
| 200 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 201 | base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> > |
| 202 | SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_(base::LINKER_INITIALIZED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | |
| 204 | SyncChannel::SyncContext::SyncContext( |
| 205 | Channel::Listener* listener, |
| 206 | MessageFilter* filter, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 207 | MessageLoop* ipc_thread, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 208 | WaitableEvent* shutdown_event) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | : ChannelProxy::Context(listener, filter, ipc_thread), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 210 | received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), |
| 211 | shutdown_event_(shutdown_event) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | SyncChannel::SyncContext::~SyncContext() { |
| 215 | while (!deserializers_.empty()) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 216 | Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Adds information about an outgoing sync message to the context so that |
| 220 | // we know how to deserialize the reply. Returns a handle that's set when |
| 221 | // the reply has arrived. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 222 | void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 223 | // The event is created as manual reset because in between Signal and |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 224 | // OnObjectSignalled, another Send can happen which would stop the watcher |
| 225 | // from being called. The event would get watched later, when the nested |
| 226 | // Send completes, so the event will need to remain set. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 227 | PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | sync_msg->GetReplyDeserializer(), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 229 | new WaitableEvent(true, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 231 | deserializers_.push_back(pending); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 234 | bool SyncChannel::SyncContext::Pop() { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 235 | bool result; |
| 236 | { |
| 237 | AutoLock auto_lock(deserializers_lock_); |
| 238 | PendingSyncMsg msg = deserializers_.back(); |
| 239 | delete msg.deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 240 | delete msg.done_event; |
| 241 | msg.done_event = NULL; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 242 | deserializers_.pop_back(); |
| 243 | result = msg.send_result; |
| 244 | } |
| 245 | |
| 246 | // We got a reply to a synchronous Send() call that's blocking the listener |
| 247 | // thread. However, further down the call stack there could be another |
| 248 | // blocking Send() call, whose reply we received after we made this last |
| 249 | // Send() call. So check if we have any queued replies available that |
| 250 | // can now unblock the listener thread. |
| 251 | ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 252 | received_sync_msgs_.get(), &ReceivedSyncMsgQueue::DispatchReplies)); |
| 253 | |
| 254 | return result; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 257 | WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 258 | AutoLock auto_lock(deserializers_lock_); |
| 259 | return deserializers_.back().done_event; |
| 260 | } |
| 261 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 262 | WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 263 | return received_sync_msgs_->dispatch_event(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void SyncChannel::SyncContext::DispatchMessages() { |
| 267 | received_sync_msgs_->DispatchMessages(); |
| 268 | } |
| 269 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 270 | bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 271 | AutoLock auto_lock(deserializers_lock_); |
| 272 | if (deserializers_.empty() || |
| 273 | !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) { |
| 274 | return false; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 275 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 277 | if (!msg->is_reply_error()) { |
| 278 | deserializers_.back().send_result = deserializers_.back().deserializer-> |
| 279 | SerializeOutputParameters(*msg); |
| 280 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 281 | deserializers_.back().done_event->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 283 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 286 | void SyncChannel::SyncContext::Clear() { |
| 287 | CancelPendingSends(); |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 288 | received_sync_msgs_->RemoveContext(this); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 289 | |
| 290 | Context::Clear(); |
| 291 | } |
| 292 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | void SyncChannel::SyncContext::OnMessageReceived(const Message& msg) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 294 | // Give the filters a chance at processing this message. |
| 295 | if (TryFilters(msg)) |
| 296 | return; |
| 297 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 298 | if (TryToUnblockListener(&msg)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | return; |
| 300 | |
| 301 | if (msg.should_unblock()) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 302 | received_sync_msgs_->QueueMessage(msg, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
| 306 | if (msg.is_reply()) { |
| 307 | received_sync_msgs_->QueueReply(msg, this); |
| 308 | return; |
| 309 | } |
| 310 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 311 | return Context::OnMessageReceivedNoFilter(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 312 | } |
| 313 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | void SyncChannel::SyncContext::OnChannelError() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 315 | CancelPendingSends(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 316 | Context::OnChannelError(); |
| 317 | } |
| 318 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 319 | void SyncChannel::SyncContext::OnChannelOpened() { |
| 320 | shutdown_watcher_.StartWatching(shutdown_event_, this); |
| 321 | Context::OnChannelOpened(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 324 | void SyncChannel::SyncContext::OnChannelClosed() { |
| 325 | shutdown_watcher_.StopWatching(); |
| 326 | Context::OnChannelClosed(); |
| 327 | } |
| 328 | |
| 329 | void SyncChannel::SyncContext::OnSendTimeout(int message_id) { |
| 330 | AutoLock auto_lock(deserializers_lock_); |
| 331 | PendingSyncMessageQueue::iterator iter; |
| 332 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 333 | if (iter->id == message_id) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 334 | iter->done_event->Signal(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void SyncChannel::SyncContext::CancelPendingSends() { |
| 341 | AutoLock auto_lock(deserializers_lock_); |
| 342 | PendingSyncMessageQueue::iterator iter; |
| 343 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 344 | iter->done_event->Signal(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 347 | void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) { |
| 348 | DCHECK(event == shutdown_event_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 349 | // Process shut down before we can get a reply to a synchronous message. |
| 350 | // Cancel pending Send calls, which will end up setting the send done event. |
| 351 | CancelPendingSends(); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | SyncChannel::SyncChannel( |
| 356 | const std::wstring& channel_id, Channel::Mode mode, |
| 357 | Channel::Listener* listener, MessageFilter* filter, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 358 | MessageLoop* ipc_message_loop, bool create_pipe_now, |
| 359 | WaitableEvent* shutdown_event) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 360 | : ChannelProxy( |
| 361 | channel_id, mode, ipc_message_loop, |
| 362 | new SyncContext(listener, filter, ipc_message_loop, shutdown_event), |
| 363 | create_pipe_now), |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 364 | sync_messages_with_no_timeout_allowed_(true) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 365 | // Ideally we only want to watch this object when running a nested message |
| 366 | // loop. However, we don't know when it exits if there's another nested |
| 367 | // message loop running under it or not, so we wouldn't know whether to |
| 368 | // stop or keep watching. So we always watch it, and create the event as |
| 369 | // manual reset since the object watcher might otherwise reset the event |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 370 | // when we're doing a WaitMany. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 371 | dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | SyncChannel::~SyncChannel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | } |
| 376 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 377 | bool SyncChannel::Send(Message* message) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 378 | return SendWithTimeout(message, INFINITE); |
| 379 | } |
| 380 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 381 | bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) { |
| 382 | if (!message->is_sync()) { |
| 383 | ChannelProxy::Send(message); |
| 384 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | } |
| 386 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 387 | // *this* might get deleted in WaitForReply. |
| 388 | scoped_refptr<SyncContext> context(sync_context()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 389 | if (context->shutdown_event()->IsSignaled()) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 390 | delete message; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | DCHECK(sync_messages_with_no_timeout_allowed_ || timeout_ms != INFINITE); |
| 395 | SyncMessage* sync_msg = static_cast<SyncMessage*>(message); |
| 396 | context->Push(sync_msg); |
| 397 | int message_id = SyncMessage::GetMessageId(*sync_msg); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 398 | WaitableEvent* pump_messages_event = sync_msg->pump_messages_event(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 399 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | ChannelProxy::Send(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 402 | if (timeout_ms != INFINITE) { |
| 403 | // We use the sync message id so that when a message times out, we don't |
| 404 | // confuse it with another send that is either above/below this Send in |
| 405 | // the call stack. |
| 406 | context->ipc_message_loop()->PostDelayedTask(FROM_HERE, |
| 407 | NewRunnableMethod(context.get(), |
| 408 | &SyncContext::OnSendTimeout, message_id), timeout_ms); |
| 409 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 411 | // Wait for reply, or for any other incoming synchronous messages. |
| 412 | WaitForReply(pump_messages_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 414 | return context->Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 417 | void SyncChannel::WaitForReply(WaitableEvent* pump_messages_event) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 418 | while (true) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 419 | WaitableEvent* objects[] = { |
| 420 | sync_context()->GetDispatchEvent(), |
| 421 | sync_context()->GetSendDoneEvent(), |
| 422 | pump_messages_event |
| 423 | }; |
| 424 | |
| 425 | unsigned count = pump_messages_event ? 3: 2; |
| 426 | unsigned result = WaitableEvent::WaitMany(objects, count); |
| 427 | if (result == 0 /* dispatch event */) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 428 | // We're waiting for a reply, but we received a blocking synchronous |
| 429 | // call. We must process it or otherwise a deadlock might occur. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 430 | sync_context()->GetDispatchEvent()->Reset(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 431 | sync_context()->DispatchMessages(); |
| 432 | continue; |
| 433 | } |
| 434 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 435 | if (result == 2 /* pump_messages_event */) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 436 | WaitForReplyWithNestedMessageLoop(); // Start a nested message loop. |
| 437 | |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void SyncChannel::WaitForReplyWithNestedMessageLoop() { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 443 | WaitableEvent* old_done_event = send_done_watcher_.GetWatchedEvent(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 444 | send_done_watcher_.StopWatching(); |
| 445 | send_done_watcher_.StartWatching(sync_context()->GetSendDoneEvent(), this); |
| 446 | bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 447 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 448 | MessageLoop::current()->Run(); |
| 449 | MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 450 | if (old_done_event) |
| 451 | send_done_watcher_.StartWatching(old_done_event, this); |
| 452 | } |
| 453 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 454 | void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { |
| 455 | WaitableEvent* dispatch_event = sync_context()->GetDispatchEvent(); |
| 456 | if (event == dispatch_event) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 457 | // The call to DispatchMessages might delete this object, so reregister |
| 458 | // the object watcher first. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 459 | dispatch_event->Reset(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 460 | dispatch_watcher_.StartWatching(dispatch_event, this); |
| 461 | sync_context()->DispatchMessages(); |
| 462 | } else { |
| 463 | // We got the reply, timed out or the process shutdown. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 464 | DCHECK(event == sync_context()->GetSendDoneEvent()); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 465 | MessageLoop::current()->Quit(); |
| 466 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | } // namespace IPC |