GoogleGit

blob: 002410892f667252d8216ecd3b36e1945c332af6 [file] [log] [blame]
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_RUN_LOOP_H_
  5. #define BASE_RUN_LOOP_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/message_loop/message_loop.h"
  10. namespace base {
  11. #if defined(OS_ANDROID)
  12. class MessagePumpForUI;
  13. #endif
  14. #if defined(OS_WIN)
  15. class MessagePumpDispatcher;
  16. #endif
  17. #if defined(OS_IOS)
  18. class MessagePumpUIApplication;
  19. #endif
  20. // Helper class to Run a nested MessageLoop. Please do not use nested
  21. // MessageLoops in production code! If you must, use this class instead of
  22. // calling MessageLoop::Run/Quit directly. RunLoop::Run can only be called once
  23. // per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run
  24. // a nested MessageLoop.
  25. class BASE_EXPORT RunLoop {
  26. public:
  27. RunLoop();
  28. #if defined(OS_WIN)
  29. explicit RunLoop(MessagePumpDispatcher* dispatcher);
  30. #endif
  31. ~RunLoop();
  32. // Run the current MessageLoop. This blocks until Quit is called. Before
  33. // calling Run, be sure to grab an AsWeakPtr or the QuitClosure in order to
  34. // stop the MessageLoop asynchronously. MessageLoop::Quit and QuitNow will
  35. // also trigger a return from Run, but those are deprecated.
  36. void Run();
  37. // Run the current MessageLoop until it doesn't find any tasks or messages in
  38. // the queue (it goes idle). WARNING: This may never return! Only use this
  39. // when repeating tasks such as animated web pages have been shut down.
  40. void RunUntilIdle();
  41. bool running() const { return running_; }
  42. // Quit an earlier call to Run(). There can be other nested RunLoops servicing
  43. // the same task queue (MessageLoop); Quitting one RunLoop has no bearing on
  44. // the others. Quit can be called before, during or after Run. If called
  45. // before Run, Run will return immediately when called. Calling Quit after the
  46. // RunLoop has already finished running has no effect.
  47. //
  48. // WARNING: You must NEVER assume that a call to Quit will terminate the
  49. // targetted message loop. If a nested message loop continues running, the
  50. // target may NEVER terminate. It is very easy to livelock (run forever) in
  51. // such a case.
  52. void Quit();
  53. // Convenience method to get a closure that safely calls Quit (has no effect
  54. // if the RunLoop instance is gone).
  55. //
  56. // Example:
  57. // RunLoop run_loop;
  58. // PostTask(run_loop.QuitClosure());
  59. // run_loop.Run();
  60. base::Closure QuitClosure();
  61. private:
  62. friend class MessageLoop;
  63. #if defined(OS_ANDROID)
  64. // Android doesn't support the blocking MessageLoop::Run, so it calls
  65. // BeforeRun and AfterRun directly.
  66. friend class base::MessagePumpForUI;
  67. #endif
  68. #if defined(OS_IOS)
  69. // iOS doesn't support the blocking MessageLoop::Run, so it calls
  70. // BeforeRun directly.
  71. friend class base::MessagePumpUIApplication;
  72. #endif
  73. // Return false to abort the Run.
  74. bool BeforeRun();
  75. void AfterRun();
  76. MessageLoop* loop_;
  77. // Parent RunLoop or NULL if this is the top-most RunLoop.
  78. RunLoop* previous_run_loop_;
  79. #if defined(OS_WIN)
  80. MessagePumpDispatcher* dispatcher_;
  81. #endif
  82. // Used to count how many nested Run() invocations are on the stack.
  83. int run_depth_;
  84. bool run_called_;
  85. bool quit_called_;
  86. bool running_;
  87. // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning
  88. // that we should quit Run once it becomes idle.
  89. bool quit_when_idle_received_;
  90. // WeakPtrFactory for QuitClosure safety.
  91. base::WeakPtrFactory<RunLoop> weak_factory_;
  92. DISALLOW_COPY_AND_ASSIGN(RunLoop);
  93. };
  94. } // namespace base
  95. #endif // BASE_RUN_LOOP_H_