Chromium Code Reviews| 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, | |
| 239 base::Unretained(this)); | |
|
dmichael (off chromium)
2013/02/01 17:27:18
Okay, so the problem is if you don't use base::Unr
| |
| 237 } | 240 } |
| 238 | 241 |
| 239 SyncChannel::SyncContext::~SyncContext() { | 242 SyncChannel::SyncContext::~SyncContext() { |
| 240 while (!deserializers_.empty()) | 243 while (!deserializers_.empty()) |
| 241 Pop(); | 244 Pop(); |
| 242 } | 245 } |
| 243 | 246 |
| 244 // Adds information about an outgoing sync message to the context so that | 247 // 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 | 248 // we know how to deserialize the reply. Returns a handle that's set when |
| 246 // the reply has arrived. | 249 // the reply has arrived. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 return Context::OnMessageReceivedNoFilter(msg); | 349 return Context::OnMessageReceivedNoFilter(msg); |
| 347 } | 350 } |
| 348 | 351 |
| 349 void SyncChannel::SyncContext::OnChannelError() { | 352 void SyncChannel::SyncContext::OnChannelError() { |
| 350 CancelPendingSends(); | 353 CancelPendingSends(); |
| 351 shutdown_watcher_.StopWatching(); | 354 shutdown_watcher_.StopWatching(); |
| 352 Context::OnChannelError(); | 355 Context::OnChannelError(); |
| 353 } | 356 } |
| 354 | 357 |
| 355 void SyncChannel::SyncContext::OnChannelOpened() { | 358 void SyncChannel::SyncContext::OnChannelOpened() { |
| 356 shutdown_watcher_.StartWatching(shutdown_event_, this); | 359 shutdown_watcher_.StartWatching(shutdown_event_, shutdown_watcher_callback_); |
| 357 Context::OnChannelOpened(); | 360 Context::OnChannelOpened(); |
| 358 } | 361 } |
| 359 | 362 |
| 360 void SyncChannel::SyncContext::OnChannelClosed() { | 363 void SyncChannel::SyncContext::OnChannelClosed() { |
| 361 CancelPendingSends(); | 364 CancelPendingSends(); |
| 362 shutdown_watcher_.StopWatching(); | 365 shutdown_watcher_.StopWatching(); |
| 363 Context::OnChannelClosed(); | 366 Context::OnChannelClosed(); |
| 364 } | 367 } |
| 365 | 368 |
| 366 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { | 369 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 | 513 |
| 511 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { | 514 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { |
| 512 base::WaitableEventWatcher send_done_watcher; | 515 base::WaitableEventWatcher send_done_watcher; |
| 513 | 516 |
| 514 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); | 517 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); |
| 515 DCHECK(sync_msg_queue != NULL); | 518 DCHECK(sync_msg_queue != NULL); |
| 516 | 519 |
| 517 base::WaitableEventWatcher* old_send_done_event_watcher = | 520 base::WaitableEventWatcher* old_send_done_event_watcher = |
| 518 sync_msg_queue->top_send_done_watcher(); | 521 sync_msg_queue->top_send_done_watcher(); |
| 519 | 522 |
| 520 base::WaitableEventWatcher::Delegate* old_delegate = NULL; | 523 base::WaitableEventWatcher::EventCallback old_callback; |
| 521 base::WaitableEvent* old_event = NULL; | 524 base::WaitableEvent* old_event = NULL; |
| 522 | 525 |
| 523 // Maintain a local global stack of send done delegates to ensure that | 526 // Maintain a local global stack of send done delegates to ensure that |
| 524 // nested sync calls complete in the correct sequence, i.e. the | 527 // nested sync calls complete in the correct sequence, i.e. the |
| 525 // outermost call completes first, etc. | 528 // outermost call completes first, etc. |
| 526 if (old_send_done_event_watcher) { | 529 if (old_send_done_event_watcher) { |
| 527 old_delegate = old_send_done_event_watcher->delegate(); | 530 old_callback = old_send_done_event_watcher->callback(); |
|
dmichael (off chromium)
2013/02/01 17:27:18
If you instead just make the callback here (e.g.,
| |
| 528 old_event = old_send_done_event_watcher->GetWatchedEvent(); | 531 old_event = old_send_done_event_watcher->GetWatchedEvent(); |
| 529 old_send_done_event_watcher->StopWatching(); | 532 old_send_done_event_watcher->StopWatching(); |
| 530 } | 533 } |
| 531 | 534 |
| 532 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); | 535 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); |
| 533 | 536 |
| 534 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context); | 537 send_done_watcher.StartWatching(context->GetSendDoneEvent(), |
| 538 context->shutdown_watcher_callback()); | |
| 535 | 539 |
| 536 { | 540 { |
| 537 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 541 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 538 MessageLoop::current()->Run(); | 542 MessageLoop::current()->Run(); |
| 539 } | 543 } |
| 540 | 544 |
| 541 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); | 545 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); |
| 542 if (old_send_done_event_watcher && old_event) { | 546 if (old_send_done_event_watcher && old_event) { |
| 543 old_send_done_event_watcher->StartWatching(old_event, old_delegate); | 547 old_send_done_event_watcher->StartWatching(old_event, old_callback); |
| 544 } | 548 } |
| 545 } | 549 } |
| 546 | 550 |
| 547 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { | 551 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { |
| 548 DCHECK(event == sync_context()->GetDispatchEvent()); | 552 DCHECK(event == sync_context()->GetDispatchEvent()); |
| 549 // The call to DispatchMessages might delete this object, so reregister | 553 // The call to DispatchMessages might delete this object, so reregister |
| 550 // the object watcher first. | 554 // the object watcher first. |
| 551 event->Reset(); | 555 event->Reset(); |
| 552 dispatch_watcher_.StartWatching(event, this); | 556 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_); |
| 553 sync_context()->DispatchMessages(); | 557 sync_context()->DispatchMessages(); |
| 554 } | 558 } |
| 555 | 559 |
| 556 void SyncChannel::StartWatching() { | 560 void SyncChannel::StartWatching() { |
| 557 // Ideally we only want to watch this object when running a nested message | 561 // 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 | 562 // 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 | 563 // 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 | 564 // 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 | 565 // manual reset since the object watcher might otherwise reset the event |
| 562 // when we're doing a WaitMany. | 566 // when we're doing a WaitMany. |
| 563 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); | 567 dispatch_watcher_callback_ = |
| 568 base::Bind(&SyncChannel::OnWaitableEventSignaled, | |
| 569 base::Unretained(this)); | |
| 570 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), | |
| 571 dispatch_watcher_callback_); | |
| 564 } | 572 } |
| 565 | 573 |
| 566 } // namespace IPC | 574 } // namespace IPC |
| OLD | NEW |