Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: ipc/ipc_sync_channel.cc

Issue 11953112: Refactor: Simplify WaitableEventWatcher. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed on windows Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 return Context::OnMessageReceivedNoFilter(msg); 346 return Context::OnMessageReceivedNoFilter(msg);
347 } 347 }
348 348
349 void SyncChannel::SyncContext::OnChannelError() { 349 void SyncChannel::SyncContext::OnChannelError() {
350 CancelPendingSends(); 350 CancelPendingSends();
351 shutdown_watcher_.StopWatching(); 351 shutdown_watcher_.StopWatching();
352 Context::OnChannelError(); 352 Context::OnChannelError();
353 } 353 }
354 354
355 void SyncChannel::SyncContext::OnChannelOpened() { 355 void SyncChannel::SyncContext::OnChannelOpened() {
356 shutdown_watcher_.StartWatching(shutdown_event_, this); 356 shutdown_watcher_callback_ = base::Bind(
357 &SyncChannel::SyncContext::OnWaitableEventSignaled, 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
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::Callback<void(base::WaitableEvent*)>* old_callback = NULL;
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
OLDNEW
« chrome/browser/browsing_data/browsing_data_remover.h ('K') | « ipc/ipc_sync_channel.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698