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