blob: 4f1e71f9d99cc33f4a7e59fc8023dec19d0ff0b0 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295#include "chrome/common/ipc_sync_channel.h"
6
[email protected]f886b7bf2008-09-10 10:54:067#include "base/lazy_instance.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/logging.h"
[email protected]f886b7bf2008-09-10 10:54:069#include "base/thread_local.h"
[email protected]1c4947f2009-01-15 22:25:1110#include "base/message_loop.h"
11#include "base/waitable_event.h"
12#include "base/waitable_event_watcher.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/common/ipc_logging.h"
14#include "chrome/common/ipc_sync_message.h"
15
[email protected]1c4947f2009-01-15 22:25:1116#if !defined(OS_WIN)
17#define INFINITE -1
18#endif
19
[email protected]e1acf6f2008-10-27 20:43:3320using base::TimeDelta;
21using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1122using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2923
24namespace 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.commit09911bf2008-07-26 23:55:2942class SyncChannel::ReceivedSyncMsgQueue :
43 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
44 public:
[email protected]3cdb7af812008-10-24 19:21:1345 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2046 // if necessary. Call RemoveContext on the same thread when done.
47 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1348 // 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.commit09911bf2008-07-26 23:55:2957 }
58
59 ~ReceivedSyncMsgQueue() {
initial.commit09911bf2008-07-26 23:55:2960 }
61
62 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2063 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2964 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]d3ae7a072008-12-05 20:27:2073 message_queue_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2974 }
75
[email protected]1c4947f2009-01-15 22:25:1176 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2977 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]d3ae7a072008-12-05 20:27:2084 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2985 }
86
[email protected]d3ae7a072008-12-05 20:27:2087 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2988 // 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]d3ae7a072008-12-05 20:27:2099 Message* message;
100 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:29101 {
102 AutoLock auto_lock(message_lock_);
103 if (message_queue_.empty())
104 break;
105
[email protected]d3ae7a072008-12-05 20:27:20106 message = message_queue_.front().message;
107 context = message_queue_.front().context;
108 message_queue_.pop_front();
initial.commit09911bf2008-07-26 23:55:29109 }
110
111#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]3cdb7af812008-10-24 19:21:13112 Logging* logger = Logging::current();
initial.commit09911bf2008-07-26 23:55:29113 if (logger->Enabled())
114 logger->OnPreDispatchMessage(*message);
115#endif
116
[email protected]d3ae7a072008-12-05 20:27:20117 if (context->listener())
118 context->listener()->OnMessageReceived(*message);
initial.commit09911bf2008-07-26 23:55:29119
120#ifdef IPC_MESSAGE_LOG_ENABLED
121 if (logger->Enabled())
[email protected]d3ae7a072008-12-05 20:27:20122 logger->OnPostDispatchMessage(*message, context->channel_id());
initial.commit09911bf2008-07-26 23:55:29123#endif
124
125 delete message;
126 }
127 }
128
initial.commit09911bf2008-07-26 23:55:29129 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20130 void RemoveContext(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:29131 AutoLock auto_lock(message_lock_);
132
[email protected]d3ae7a072008-12-05 20:27:20133 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.commit09911bf2008-07-26 23:55:29138 } else {
[email protected]d3ae7a072008-12-05 20:27:20139 iter++;
initial.commit09911bf2008-07-26 23:55:29140 }
initial.commit09911bf2008-07-26 23:55:29141 }
[email protected]3cdb7af812008-10-24 19:21:13142
143 if (--listener_count_ == 0) {
144 DCHECK(lazy_tls_ptr_.Pointer()->Get());
145 lazy_tls_ptr_.Pointer()->Set(NULL);
146 }
initial.commit09911bf2008-07-26 23:55:29147 }
148
[email protected]1c4947f2009-01-15 22:25:11149 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]8fd8de92008-08-12 23:50:30150 MessageLoop* listener_message_loop() { return listener_message_loop_; }
initial.commit09911bf2008-07-26 23:55:29151
[email protected]f886b7bf2008-09-10 10:54:06152 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
153 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
154 lazy_tls_ptr_;
155
initial.commit09911bf2008-07-26 23:55:29156 // 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.commit09911bf2008-07-26 23:55:29159 for (size_t i = 0; i < received_replies_.size(); ++i) {
160 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13161 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29162 delete message;
163 received_replies_.erase(received_replies_.begin() + i);
164 return;
165 }
166 }
167 }
168
[email protected]63a7bb82008-10-25 00:46:00169 private:
[email protected]4df10d612008-11-12 00:38:26170 // See the comment in SyncChannel::SyncChannel for why this event is created
171 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00172 ReceivedSyncMsgQueue() :
[email protected]1c4947f2009-01-15 22:25:11173 dispatch_event_(true, false),
[email protected]63a7bb82008-10-25 00:46:00174 listener_message_loop_(MessageLoop::current()),
[email protected]1c4947f2009-01-15 22:25:11175 task_pending_(false),
[email protected]63a7bb82008-10-25 00:46:00176 listener_count_(0) {
177 }
178
[email protected]d3ae7a072008-12-05 20:27:20179 // Holds information about a queued synchronous message or reply.
180 struct QueuedMessage {
181 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29182 Message* message;
183 scoped_refptr<SyncChannel::SyncContext> context;
184 };
185
[email protected]d3ae7a072008-12-05 20:27:20186 typedef std::deque<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11187 SyncMessageQueue message_queue_;
[email protected]d3ae7a072008-12-05 20:27:20188
189 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13190
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]1c4947f2009-01-15 22:25:11194 WaitableEvent dispatch_event_;
[email protected]3cdb7af812008-10-24 19:21:13195 MessageLoop* listener_message_loop_;
196 Lock message_lock_;
197 bool task_pending_;
198 int listener_count_;
initial.commit09911bf2008-07-26 23:55:29199};
200
[email protected]f886b7bf2008-09-10 10:54:06201base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
202 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_(base::LINKER_INITIALIZED);
initial.commit09911bf2008-07-26 23:55:29203
204SyncChannel::SyncContext::SyncContext(
205 Channel::Listener* listener,
206 MessageFilter* filter,
[email protected]3cdb7af812008-10-24 19:21:13207 MessageLoop* ipc_thread,
[email protected]1c4947f2009-01-15 22:25:11208 WaitableEvent* shutdown_event)
initial.commit09911bf2008-07-26 23:55:29209 : ChannelProxy::Context(listener, filter, ipc_thread),
[email protected]1c4947f2009-01-15 22:25:11210 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
211 shutdown_event_(shutdown_event) {
initial.commit09911bf2008-07-26 23:55:29212}
213
214SyncChannel::SyncContext::~SyncContext() {
215 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13216 Pop();
initial.commit09911bf2008-07-26 23:55:29217}
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]3cdb7af812008-10-24 19:21:13222void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]1c4947f2009-01-15 22:25:11223 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26224 // 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]3cdb7af812008-10-24 19:21:13227 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29228 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11229 new WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29230 AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13231 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29232}
233
[email protected]3cdb7af812008-10-24 19:21:13234bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00235 bool result;
236 {
237 AutoLock auto_lock(deserializers_lock_);
238 PendingSyncMsg msg = deserializers_.back();
239 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11240 delete msg.done_event;
241 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00242 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]3cdb7af812008-10-24 19:21:13255}
256
[email protected]1c4947f2009-01-15 22:25:11257WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]3cdb7af812008-10-24 19:21:13258 AutoLock auto_lock(deserializers_lock_);
259 return deserializers_.back().done_event;
260}
261
[email protected]1c4947f2009-01-15 22:25:11262WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13263 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29264}
265
266void SyncChannel::SyncContext::DispatchMessages() {
267 received_sync_msgs_->DispatchMessages();
268}
269
[email protected]3cdb7af812008-10-24 19:21:13270bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]63a7bb82008-10-25 00:46:00271 AutoLock auto_lock(deserializers_lock_);
272 if (deserializers_.empty() ||
273 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
274 return false;
[email protected]3cdb7af812008-10-24 19:21:13275 }
initial.commit09911bf2008-07-26 23:55:29276
[email protected]63a7bb82008-10-25 00:46:00277 if (!msg->is_reply_error()) {
278 deserializers_.back().send_result = deserializers_.back().deserializer->
279 SerializeOutputParameters(*msg);
280 }
[email protected]1c4947f2009-01-15 22:25:11281 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29282
[email protected]3cdb7af812008-10-24 19:21:13283 return true;
initial.commit09911bf2008-07-26 23:55:29284}
285
[email protected]3cdb7af812008-10-24 19:21:13286void SyncChannel::SyncContext::Clear() {
287 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20288 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13289
290 Context::Clear();
291}
292
initial.commit09911bf2008-07-26 23:55:29293void SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41294 // Give the filters a chance at processing this message.
295 if (TryFilters(msg))
296 return;
297
[email protected]3cdb7af812008-10-24 19:21:13298 if (TryToUnblockListener(&msg))
initial.commit09911bf2008-07-26 23:55:29299 return;
300
301 if (msg.should_unblock()) {
[email protected]d3ae7a072008-12-05 20:27:20302 received_sync_msgs_->QueueMessage(msg, this);
initial.commit09911bf2008-07-26 23:55:29303 return;
304 }
305
306 if (msg.is_reply()) {
307 received_sync_msgs_->QueueReply(msg, this);
308 return;
309 }
310
[email protected]3cdb7af812008-10-24 19:21:13311 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29312}
313
initial.commit09911bf2008-07-26 23:55:29314void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13315 CancelPendingSends();
initial.commit09911bf2008-07-26 23:55:29316 Context::OnChannelError();
317}
318
[email protected]3cdb7af812008-10-24 19:21:13319void SyncChannel::SyncContext::OnChannelOpened() {
320 shutdown_watcher_.StartWatching(shutdown_event_, this);
321 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29322}
323
[email protected]3cdb7af812008-10-24 19:21:13324void SyncChannel::SyncContext::OnChannelClosed() {
325 shutdown_watcher_.StopWatching();
326 Context::OnChannelClosed();
327}
328
329void 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]d3ae7a072008-12-05 20:27:20333 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11334 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13335 break;
336 }
337 }
338}
339
340void SyncChannel::SyncContext::CancelPendingSends() {
341 AutoLock auto_lock(deserializers_lock_);
342 PendingSyncMessageQueue::iterator iter;
343 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11344 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13345}
346
[email protected]1c4947f2009-01-15 22:25:11347void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
348 DCHECK(event == shutdown_event_);
[email protected]3cdb7af812008-10-24 19:21:13349 // 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
355SyncChannel::SyncChannel(
356 const std::wstring& channel_id, Channel::Mode mode,
357 Channel::Listener* listener, MessageFilter* filter,
[email protected]1c4947f2009-01-15 22:25:11358 MessageLoop* ipc_message_loop, bool create_pipe_now,
359 WaitableEvent* shutdown_event)
[email protected]3cdb7af812008-10-24 19:21:13360 : ChannelProxy(
361 channel_id, mode, ipc_message_loop,
362 new SyncContext(listener, filter, ipc_message_loop, shutdown_event),
363 create_pipe_now),
[email protected]d65cab7a2008-08-12 01:25:41364 sync_messages_with_no_timeout_allowed_(true) {
[email protected]3cdb7af812008-10-24 19:21:13365 // 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]1c4947f2009-01-15 22:25:11370 // when we're doing a WaitMany.
[email protected]3cdb7af812008-10-24 19:21:13371 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this);
initial.commit09911bf2008-07-26 23:55:29372}
373
374SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29375}
376
[email protected]3cdb7af812008-10-24 19:21:13377bool SyncChannel::Send(Message* message) {
[email protected]d65cab7a2008-08-12 01:25:41378 return SendWithTimeout(message, INFINITE);
379}
380
[email protected]3cdb7af812008-10-24 19:21:13381bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
382 if (!message->is_sync()) {
383 ChannelProxy::Send(message);
384 return true;
initial.commit09911bf2008-07-26 23:55:29385 }
386
[email protected]3cdb7af812008-10-24 19:21:13387 // *this* might get deleted in WaitForReply.
388 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11389 if (context->shutdown_event()->IsSignaled()) {
[email protected]3cdb7af812008-10-24 19:21:13390 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]1c4947f2009-01-15 22:25:11398 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13399
initial.commit09911bf2008-07-26 23:55:29400 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29401
[email protected]3cdb7af812008-10-24 19:21:13402 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.commit09911bf2008-07-26 23:55:29410
[email protected]3cdb7af812008-10-24 19:21:13411 // Wait for reply, or for any other incoming synchronous messages.
412 WaitForReply(pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29413
[email protected]3cdb7af812008-10-24 19:21:13414 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29415}
416
[email protected]1c4947f2009-01-15 22:25:11417void SyncChannel::WaitForReply(WaitableEvent* pump_messages_event) {
[email protected]3cdb7af812008-10-24 19:21:13418 while (true) {
[email protected]1c4947f2009-01-15 22:25:11419 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]3cdb7af812008-10-24 19:21:13428 // 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]1c4947f2009-01-15 22:25:11430 sync_context()->GetDispatchEvent()->Reset();
[email protected]3cdb7af812008-10-24 19:21:13431 sync_context()->DispatchMessages();
432 continue;
433 }
434
[email protected]1c4947f2009-01-15 22:25:11435 if (result == 2 /* pump_messages_event */)
[email protected]3cdb7af812008-10-24 19:21:13436 WaitForReplyWithNestedMessageLoop(); // Start a nested message loop.
437
438 break;
439 }
440}
441
442void SyncChannel::WaitForReplyWithNestedMessageLoop() {
[email protected]1c4947f2009-01-15 22:25:11443 WaitableEvent* old_done_event = send_done_watcher_.GetWatchedEvent();
[email protected]3cdb7af812008-10-24 19:21:13444 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]1c4947f2009-01-15 22:25:11454void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
455 WaitableEvent* dispatch_event = sync_context()->GetDispatchEvent();
456 if (event == dispatch_event) {
[email protected]3cdb7af812008-10-24 19:21:13457 // The call to DispatchMessages might delete this object, so reregister
458 // the object watcher first.
[email protected]1c4947f2009-01-15 22:25:11459 dispatch_event->Reset();
[email protected]3cdb7af812008-10-24 19:21:13460 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]1c4947f2009-01-15 22:25:11464 DCHECK(event == sync_context()->GetSendDoneEvent());
[email protected]3cdb7af812008-10-24 19:21:13465 MessageLoop::current()->Quit();
466 }
initial.commit09911bf2008-07-26 23:55:29467}
468
469} // namespace IPC