OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_channel.h" | 5 #include "ipc/ipc_sync_channel.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 LAZY_INSTANCE_INITIALIZER; | 227 LAZY_INSTANCE_INITIALIZER; |
228 | 228 |
229 SyncChannel::SyncContext::SyncContext( | 229 SyncChannel::SyncContext::SyncContext( |
230 Listener* listener, | 230 Listener* listener, |
231 base::SingleThreadTaskRunner* ipc_task_runner, | 231 base::SingleThreadTaskRunner* ipc_task_runner, |
232 WaitableEvent* shutdown_event) | 232 WaitableEvent* shutdown_event) |
233 : ChannelProxy::Context(listener, ipc_task_runner), | 233 : ChannelProxy::Context(listener, ipc_task_runner), |
234 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), | 234 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), |
235 shutdown_event_(shutdown_event), | 235 shutdown_event_(shutdown_event), |
236 restrict_dispatch_group_(kRestrictDispatchGroup_None) { | 236 restrict_dispatch_group_(kRestrictDispatchGroup_None) { |
| 237 shutdown_watcher_callback_ = base::Bind( |
| 238 &SyncChannel::SyncContext::OnWaitableEventSignaled, this); |
237 } | 239 } |
238 | 240 |
239 SyncChannel::SyncContext::~SyncContext() { | 241 SyncChannel::SyncContext::~SyncContext() { |
240 while (!deserializers_.empty()) | 242 while (!deserializers_.empty()) |
241 Pop(); | 243 Pop(); |
242 } | 244 } |
243 | 245 |
244 // Adds information about an outgoing sync message to the context so that | 246 // Adds information about an outgoing sync message to the context so that |
245 // we know how to deserialize the reply. Returns a handle that's set when | 247 // we know how to deserialize the reply. Returns a handle that's set when |
246 // the reply has arrived. | 248 // the reply has arrived. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 return Context::OnMessageReceivedNoFilter(msg); | 348 return Context::OnMessageReceivedNoFilter(msg); |
347 } | 349 } |
348 | 350 |
349 void SyncChannel::SyncContext::OnChannelError() { | 351 void SyncChannel::SyncContext::OnChannelError() { |
350 CancelPendingSends(); | 352 CancelPendingSends(); |
351 shutdown_watcher_.StopWatching(); | 353 shutdown_watcher_.StopWatching(); |
352 Context::OnChannelError(); | 354 Context::OnChannelError(); |
353 } | 355 } |
354 | 356 |
355 void SyncChannel::SyncContext::OnChannelOpened() { | 357 void SyncChannel::SyncContext::OnChannelOpened() { |
356 shutdown_watcher_.StartWatching(shutdown_event_, this); | 358 shutdown_watcher_.StartWatching(shutdown_event_, shutdown_watcher_callback_); |
357 Context::OnChannelOpened(); | 359 Context::OnChannelOpened(); |
358 } | 360 } |
359 | 361 |
360 void SyncChannel::SyncContext::OnChannelClosed() { | 362 void SyncChannel::SyncContext::OnChannelClosed() { |
361 CancelPendingSends(); | 363 CancelPendingSends(); |
362 shutdown_watcher_.StopWatching(); | 364 shutdown_watcher_.StopWatching(); |
363 Context::OnChannelClosed(); | 365 Context::OnChannelClosed(); |
364 } | 366 } |
365 | 367 |
366 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { | 368 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 | 512 |
511 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { | 513 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { |
512 base::WaitableEventWatcher send_done_watcher; | 514 base::WaitableEventWatcher send_done_watcher; |
513 | 515 |
514 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); | 516 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); |
515 DCHECK(sync_msg_queue != NULL); | 517 DCHECK(sync_msg_queue != NULL); |
516 | 518 |
517 base::WaitableEventWatcher* old_send_done_event_watcher = | 519 base::WaitableEventWatcher* old_send_done_event_watcher = |
518 sync_msg_queue->top_send_done_watcher(); | 520 sync_msg_queue->top_send_done_watcher(); |
519 | 521 |
520 base::WaitableEventWatcher::Delegate* old_delegate = NULL; | 522 base::WaitableEventWatcher::EventCallback old_callback; |
521 base::WaitableEvent* old_event = NULL; | 523 base::WaitableEvent* old_event = NULL; |
522 | 524 |
523 // Maintain a local global stack of send done delegates to ensure that | 525 // Maintain a local global stack of send done delegates to ensure that |
524 // nested sync calls complete in the correct sequence, i.e. the | 526 // nested sync calls complete in the correct sequence, i.e. the |
525 // outermost call completes first, etc. | 527 // outermost call completes first, etc. |
526 if (old_send_done_event_watcher) { | 528 if (old_send_done_event_watcher) { |
527 old_delegate = old_send_done_event_watcher->delegate(); | 529 old_callback = old_send_done_event_watcher->callback(); |
528 old_event = old_send_done_event_watcher->GetWatchedEvent(); | 530 old_event = old_send_done_event_watcher->GetWatchedEvent(); |
529 old_send_done_event_watcher->StopWatching(); | 531 old_send_done_event_watcher->StopWatching(); |
530 } | 532 } |
531 | 533 |
532 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); | 534 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); |
533 | 535 |
534 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context); | 536 send_done_watcher.StartWatching(context->GetSendDoneEvent(), |
| 537 context->shutdown_watcher_callback()); |
535 | 538 |
536 { | 539 { |
537 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 540 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
538 MessageLoop::current()->Run(); | 541 MessageLoop::current()->Run(); |
539 } | 542 } |
540 | 543 |
541 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); | 544 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); |
542 if (old_send_done_event_watcher && old_event) { | 545 if (old_send_done_event_watcher && old_event) { |
543 old_send_done_event_watcher->StartWatching(old_event, old_delegate); | 546 old_send_done_event_watcher->StartWatching(old_event, old_callback); |
544 } | 547 } |
545 } | 548 } |
546 | 549 |
547 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { | 550 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { |
548 DCHECK(event == sync_context()->GetDispatchEvent()); | 551 DCHECK(event == sync_context()->GetDispatchEvent()); |
549 // The call to DispatchMessages might delete this object, so reregister | 552 // The call to DispatchMessages might delete this object, so reregister |
550 // the object watcher first. | 553 // the object watcher first. |
551 event->Reset(); | 554 event->Reset(); |
552 dispatch_watcher_.StartWatching(event, this); | 555 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_); |
553 sync_context()->DispatchMessages(); | 556 sync_context()->DispatchMessages(); |
554 } | 557 } |
555 | 558 |
556 void SyncChannel::StartWatching() { | 559 void SyncChannel::StartWatching() { |
557 // Ideally we only want to watch this object when running a nested message | 560 // Ideally we only want to watch this object when running a nested message |
558 // loop. However, we don't know when it exits if there's another nested | 561 // loop. However, we don't know when it exits if there's another nested |
559 // message loop running under it or not, so we wouldn't know whether to | 562 // message loop running under it or not, so we wouldn't know whether to |
560 // stop or keep watching. So we always watch it, and create the event as | 563 // stop or keep watching. So we always watch it, and create the event as |
561 // manual reset since the object watcher might otherwise reset the event | 564 // manual reset since the object watcher might otherwise reset the event |
562 // when we're doing a WaitMany. | 565 // when we're doing a WaitMany. |
563 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); | 566 dispatch_watcher_callback_ = |
| 567 base::Bind(&SyncChannel::OnWaitableEventSignaled, |
| 568 base::Unretained(this)); |
| 569 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), |
| 570 dispatch_watcher_callback_); |
564 } | 571 } |
565 | 572 |
566 } // namespace IPC | 573 } // namespace IPC |
OLD | NEW |