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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 } | 296 } |
| 297 | 297 |
| 298 void MessageLoop::PostNonNestableDelayedTask( | 298 void MessageLoop::PostNonNestableDelayedTask( |
| 299 const tracked_objects::Location& from_here, | 299 const tracked_objects::Location& from_here, |
| 300 const base::Closure& task, | 300 const base::Closure& task, |
| 301 base::TimeDelta delay) { | 301 base::TimeDelta delay) { |
| 302 PostNonNestableDelayedTask(from_here, task, delay.InMillisecondsRoundedUp()); | 302 PostNonNestableDelayedTask(from_here, task, delay.InMillisecondsRoundedUp()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void MessageLoop::Run() { | 305 void MessageLoop::Run() { |
| 306 AutoRunState save_state(this); | 306 AutoRunState save_state(this, NULL); |
| 307 RunHandler(); | |
| 308 } | |
| 309 | |
| 310 void MessageLoop::RunWithID(const RunID& run_id) { | |
| 311 DCHECK(run_id.get() && !run_id->used_); | |
| 312 AutoRunState save_state(this, run_id); | |
| 313 if (run_id->quit_now_received_) | |
|
jar (doing other things)
2012/06/18 19:43:21
If you want to make the DCHECK in line 311 more va
jbates
2012/06/18 23:14:03
It's done by the AutoRunState constructor. I moved
| |
| 314 return; | |
| 307 RunHandler(); | 315 RunHandler(); |
| 308 } | 316 } |
| 309 | 317 |
| 310 void MessageLoop::RunAllPending() { | 318 void MessageLoop::RunAllPending() { |
| 311 AutoRunState save_state(this); | 319 AutoRunState save_state(this, NULL); |
| 312 state_->quit_received = true; // Means run until we would otherwise block. | 320 state_->quit_received = true; // Means run until we would otherwise block. |
| 313 RunHandler(); | 321 RunHandler(); |
| 314 } | 322 } |
| 315 | 323 |
| 316 void MessageLoop::Quit() { | 324 void MessageLoop::QuitWhenIdle() { |
| 317 DCHECK_EQ(this, current()); | 325 DCHECK_EQ(this, current()); |
| 318 if (state_) { | 326 if (state_) { |
| 319 state_->quit_received = true; | 327 state_->quit_received = true; |
| 320 } else { | 328 } else { |
| 321 NOTREACHED() << "Must be inside Run to call Quit"; | 329 NOTREACHED() << "Must be inside Run to call Quit"; |
| 322 } | 330 } |
| 323 } | 331 } |
| 324 | 332 |
| 325 void MessageLoop::QuitNow() { | 333 void MessageLoop::QuitNow() { |
| 326 DCHECK_EQ(this, current()); | 334 DCHECK_EQ(this, current()); |
| 327 if (state_) { | 335 if (state_) { |
| 328 pump_->Quit(); | 336 pump_->Quit(); |
| 329 } else { | 337 } else { |
| 330 NOTREACHED() << "Must be inside Run to call Quit"; | 338 NOTREACHED() << "Must be inside Run to call Quit"; |
| 331 } | 339 } |
| 332 } | 340 } |
| 333 | 341 |
| 334 static void QuitCurrent() { | 342 void MessageLoop::QuitNowWithID(const RunID& run_id) { |
| 335 MessageLoop::current()->Quit(); | 343 DCHECK_EQ(this, current()); |
| 344 if (!state_) | |
| 345 return; | |
| 346 // If the state_ pointer matches that of the RunIDObject, we're at the same | |
| 347 // Run scope, so quit now. | |
| 348 if (state_ == run_id->run_state_) | |
| 349 QuitNow(); | |
| 350 | |
| 351 // Set quit_now_received_ so that the corresponding Run scope is quit as soon | |
| 352 // as it is returned to. It's also possible that the corresponding Run has not | |
| 353 // begun; in that case, this will cause the Run to immediately return. | |
|
jar (doing other things)
2012/06/18 19:43:21
Is the case of calling quit before the RunWithId()
jbates
2012/06/18 23:14:03
Explained in response to your other comment in mes
| |
| 354 run_id->quit_now_received_ = true; | |
| 355 } | |
| 356 | |
| 357 static void QuitCurrentWhenIdle() { | |
| 358 MessageLoop::current()->QuitWhenIdle(); | |
| 359 } | |
| 360 | |
| 361 static void QuitCurrentNow() { | |
| 362 MessageLoop::current()->QuitNow(); | |
| 363 } | |
| 364 | |
| 365 static void QuitCurrentNowWithID(const MessageLoop::RunID& run_id) { | |
| 366 MessageLoop::current()->QuitNowWithID(run_id); | |
| 336 } | 367 } |
| 337 | 368 |
| 338 // static | 369 // static |
| 339 base::Closure MessageLoop::QuitClosure() { | 370 base::Closure MessageLoop::QuitWhenIdleClosure() { |
| 340 return base::Bind(&QuitCurrent); | 371 return base::Bind(&QuitCurrentWhenIdle); |
| 372 } | |
| 373 | |
| 374 // static | |
| 375 base::Closure MessageLoop::QuitNowClosure() { | |
| 376 return base::Bind(&QuitCurrentNow); | |
| 377 } | |
| 378 | |
| 379 // static | |
| 380 base::Closure MessageLoop::QuitNowWithIDClosure( | |
| 381 const RunID& run_id) { | |
| 382 return base::Bind(&QuitCurrentNowWithID, run_id); | |
| 383 } | |
| 384 | |
| 385 MessageLoop::RunID MessageLoop::GetRunID() { | |
| 386 return new RunIDObject(); | |
| 341 } | 387 } |
| 342 | 388 |
| 343 void MessageLoop::SetNestableTasksAllowed(bool allowed) { | 389 void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
| 344 if (nestable_tasks_allowed_ != allowed) { | 390 if (nestable_tasks_allowed_ != allowed) { |
| 345 nestable_tasks_allowed_ = allowed; | 391 nestable_tasks_allowed_ = allowed; |
| 346 if (!nestable_tasks_allowed_) | 392 if (!nestable_tasks_allowed_) |
| 347 return; | 393 return; |
| 348 // Start the native pump if we are not already pumping. | 394 // Start the native pump if we are not already pumping. |
| 349 pump_->ScheduleWork(); | 395 pump_->ScheduleWork(); |
| 350 } | 396 } |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 } | 755 } |
| 710 | 756 |
| 711 void MessageLoop::ReleaseSoonInternal( | 757 void MessageLoop::ReleaseSoonInternal( |
| 712 const tracked_objects::Location& from_here, | 758 const tracked_objects::Location& from_here, |
| 713 void(*releaser)(const void*), | 759 void(*releaser)(const void*), |
| 714 const void* object) { | 760 const void* object) { |
| 715 PostNonNestableTask(from_here, base::Bind(releaser, object)); | 761 PostNonNestableTask(from_here, base::Bind(releaser, object)); |
| 716 } | 762 } |
| 717 | 763 |
| 718 //------------------------------------------------------------------------------ | 764 //------------------------------------------------------------------------------ |
| 765 // MessageLoop::RunState | |
| 766 | |
| 767 MessageLoop::RunState::RunState() { | |
| 768 } | |
| 769 | |
| 770 MessageLoop::RunState::~RunState() { | |
| 771 } | |
| 772 | |
| 773 //------------------------------------------------------------------------------ | |
| 719 // MessageLoop::AutoRunState | 774 // MessageLoop::AutoRunState |
| 720 | 775 |
| 721 MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { | 776 MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop, const RunID& run_id) |
| 777 : loop_(loop) { | |
| 722 // Make the loop reference us. | 778 // Make the loop reference us. |
| 723 previous_state_ = loop_->state_; | 779 previous_state_ = loop_->state_; |
| 724 if (previous_state_) { | 780 if (previous_state_) { |
| 725 run_depth = previous_state_->run_depth + 1; | 781 run_depth = previous_state_->run_depth + 1; |
| 726 } else { | 782 } else { |
| 727 run_depth = 1; | 783 run_depth = 1; |
| 728 } | 784 } |
| 729 loop_->state_ = this; | 785 loop_->state_ = this; |
| 730 | 786 |
| 787 if (run_id.get()) { | |
| 788 run_state_id = run_id; | |
| 789 run_state_id->used_ = true; | |
| 790 run_state_id->run_state_ = this; | |
| 791 } | |
| 792 | |
| 731 // Initialize the other fields: | 793 // Initialize the other fields: |
| 732 quit_received = false; | 794 quit_received = false; |
| 733 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 795 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 734 dispatcher = NULL; | 796 dispatcher = NULL; |
| 735 #endif | 797 #endif |
| 736 } | 798 } |
| 737 | 799 |
| 738 MessageLoop::AutoRunState::~AutoRunState() { | 800 MessageLoop::AutoRunState::~AutoRunState() { |
| 739 loop_->state_ = previous_state_; | 801 loop_->state_ = previous_state_; |
| 802 if (run_state_id.get()) { | |
| 803 // run_state_id->run_state_ points to 'this', so clear it. | |
| 804 run_state_id->run_state_ = NULL; | |
| 805 } | |
| 806 if (previous_state_ && previous_state_->run_state_id.get() | |
| 807 && previous_state_->run_state_id->quit_now_received_) | |
| 808 MessageLoop::current()->QuitNow(); | |
| 740 } | 809 } |
| 741 | 810 |
| 742 //------------------------------------------------------------------------------ | 811 //------------------------------------------------------------------------------ |
| 743 // MessageLoopForUI | 812 // MessageLoopForUI |
| 744 | 813 |
| 745 #if defined(OS_WIN) | 814 #if defined(OS_WIN) |
| 746 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 815 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 747 pump_win()->DidProcessMessage(message); | 816 pump_win()->DidProcessMessage(message); |
| 748 } | 817 } |
| 749 #endif // defined(OS_WIN) | 818 #endif // defined(OS_WIN) |
| 750 | 819 |
| 751 #if defined(OS_ANDROID) | 820 #if defined(OS_ANDROID) |
| 752 void MessageLoopForUI::Start() { | 821 void MessageLoopForUI::Start() { |
| 753 // No Histogram support for UI message loop as it is managed by Java side | 822 // No Histogram support for UI message loop as it is managed by Java side |
| 754 static_cast<base::MessagePumpForUI*>(pump_.get())->Start(this); | 823 static_cast<base::MessagePumpForUI*>(pump_.get())->Start(this); |
| 755 } | 824 } |
| 756 #endif | 825 #endif |
| 757 | 826 |
| 758 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) | 827 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| 759 void MessageLoopForUI::AddObserver(Observer* observer) { | 828 void MessageLoopForUI::AddObserver(Observer* observer) { |
| 760 pump_ui()->AddObserver(observer); | 829 pump_ui()->AddObserver(observer); |
| 761 } | 830 } |
| 762 | 831 |
| 763 void MessageLoopForUI::RemoveObserver(Observer* observer) { | 832 void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 764 pump_ui()->RemoveObserver(observer); | 833 pump_ui()->RemoveObserver(observer); |
| 765 } | 834 } |
| 766 | 835 |
| 767 void MessageLoopForUI::RunWithDispatcher(Dispatcher* dispatcher) { | 836 void MessageLoopForUI::RunWithDispatcher(Dispatcher* dispatcher) { |
| 768 AutoRunState save_state(this); | 837 AutoRunState save_state(this, NULL); |
| 838 state_->dispatcher = dispatcher; | |
| 839 RunHandler(); | |
| 840 } | |
| 841 | |
| 842 void MessageLoopForUI::RunWithDispatcher(Dispatcher* dispatcher, | |
| 843 const RunID& run_id) { | |
| 844 AutoRunState save_state(this, run_id); | |
| 769 state_->dispatcher = dispatcher; | 845 state_->dispatcher = dispatcher; |
| 770 RunHandler(); | 846 RunHandler(); |
| 771 } | 847 } |
| 772 | 848 |
| 773 void MessageLoopForUI::RunAllPendingWithDispatcher(Dispatcher* dispatcher) { | 849 void MessageLoopForUI::RunAllPendingWithDispatcher(Dispatcher* dispatcher) { |
| 774 AutoRunState save_state(this); | 850 AutoRunState save_state(this, NULL); |
| 775 state_->dispatcher = dispatcher; | 851 state_->dispatcher = dispatcher; |
| 776 state_->quit_received = true; // Means run until we would otherwise block. | 852 state_->quit_received = true; // Means run until we would otherwise block. |
| 777 RunHandler(); | 853 RunHandler(); |
| 778 } | 854 } |
| 779 | 855 |
| 780 #endif // !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) | 856 #endif // !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| 781 | 857 |
| 782 //------------------------------------------------------------------------------ | 858 //------------------------------------------------------------------------------ |
| 783 // MessageLoopForIO | 859 // MessageLoopForIO |
| 784 | 860 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 801 Watcher *delegate) { | 877 Watcher *delegate) { |
| 802 return pump_libevent()->WatchFileDescriptor( | 878 return pump_libevent()->WatchFileDescriptor( |
| 803 fd, | 879 fd, |
| 804 persistent, | 880 persistent, |
| 805 static_cast<base::MessagePumpLibevent::Mode>(mode), | 881 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 806 controller, | 882 controller, |
| 807 delegate); | 883 delegate); |
| 808 } | 884 } |
| 809 | 885 |
| 810 #endif | 886 #endif |
| OLD | NEW |