Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_RUN_LOOP_H_ |
michael@0 | 6 | #define BASE_RUN_LOOP_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/base_export.h" |
michael@0 | 9 | #include "base/callback.h" |
michael@0 | 10 | #include "base/memory/weak_ptr.h" |
michael@0 | 11 | #include "base/message_loop/message_loop.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace base { |
michael@0 | 14 | #if defined(OS_ANDROID) |
michael@0 | 15 | class MessagePumpForUI; |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | #if defined(OS_IOS) |
michael@0 | 19 | class MessagePumpUIApplication; |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | // Helper class to Run a nested MessageLoop. Please do not use nested |
michael@0 | 23 | // MessageLoops in production code! If you must, use this class instead of |
michael@0 | 24 | // calling MessageLoop::Run/Quit directly. RunLoop::Run can only be called once |
michael@0 | 25 | // per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run |
michael@0 | 26 | // a nested MessageLoop. |
michael@0 | 27 | class BASE_EXPORT RunLoop { |
michael@0 | 28 | public: |
michael@0 | 29 | RunLoop(); |
michael@0 | 30 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
michael@0 | 31 | !defined(USE_GTK_MESSAGE_PUMP) |
michael@0 | 32 | explicit RunLoop(MessageLoop::Dispatcher* dispatcher); |
michael@0 | 33 | #endif |
michael@0 | 34 | ~RunLoop(); |
michael@0 | 35 | |
michael@0 | 36 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
michael@0 | 37 | !defined(USE_GTK_MESSAGE_PUMP) |
michael@0 | 38 | void set_dispatcher(MessageLoop::Dispatcher* dispatcher) { |
michael@0 | 39 | dispatcher_ = dispatcher; |
michael@0 | 40 | } |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | // Run the current MessageLoop. This blocks until Quit is called. Before |
michael@0 | 44 | // calling Run, be sure to grab an AsWeakPtr or the QuitClosure in order to |
michael@0 | 45 | // stop the MessageLoop asynchronously. MessageLoop::Quit and QuitNow will |
michael@0 | 46 | // also trigger a return from Run, but those are deprecated. |
michael@0 | 47 | void Run(); |
michael@0 | 48 | |
michael@0 | 49 | // Run the current MessageLoop until it doesn't find any tasks or messages in |
michael@0 | 50 | // the queue (it goes idle). WARNING: This may never return! Only use this |
michael@0 | 51 | // when repeating tasks such as animated web pages have been shut down. |
michael@0 | 52 | void RunUntilIdle(); |
michael@0 | 53 | |
michael@0 | 54 | bool running() const { return running_; } |
michael@0 | 55 | |
michael@0 | 56 | // Quit an earlier call to Run(). There can be other nested RunLoops servicing |
michael@0 | 57 | // the same task queue (MessageLoop); Quitting one RunLoop has no bearing on |
michael@0 | 58 | // the others. Quit can be called before, during or after Run. If called |
michael@0 | 59 | // before Run, Run will return immediately when called. Calling Quit after the |
michael@0 | 60 | // RunLoop has already finished running has no effect. |
michael@0 | 61 | // |
michael@0 | 62 | // WARNING: You must NEVER assume that a call to Quit will terminate the |
michael@0 | 63 | // targetted message loop. If a nested message loop continues running, the |
michael@0 | 64 | // target may NEVER terminate. It is very easy to livelock (run forever) in |
michael@0 | 65 | // such a case. |
michael@0 | 66 | void Quit(); |
michael@0 | 67 | |
michael@0 | 68 | // Convenience method to get a closure that safely calls Quit (has no effect |
michael@0 | 69 | // if the RunLoop instance is gone). |
michael@0 | 70 | // |
michael@0 | 71 | // Example: |
michael@0 | 72 | // RunLoop run_loop; |
michael@0 | 73 | // PostTask(run_loop.QuitClosure()); |
michael@0 | 74 | // run_loop.Run(); |
michael@0 | 75 | base::Closure QuitClosure(); |
michael@0 | 76 | |
michael@0 | 77 | private: |
michael@0 | 78 | friend class MessageLoop; |
michael@0 | 79 | #if defined(OS_ANDROID) |
michael@0 | 80 | // Android doesn't support the blocking MessageLoop::Run, so it calls |
michael@0 | 81 | // BeforeRun and AfterRun directly. |
michael@0 | 82 | friend class base::MessagePumpForUI; |
michael@0 | 83 | #endif |
michael@0 | 84 | |
michael@0 | 85 | #if defined(OS_IOS) |
michael@0 | 86 | // iOS doesn't support the blocking MessageLoop::Run, so it calls |
michael@0 | 87 | // BeforeRun directly. |
michael@0 | 88 | friend class base::MessagePumpUIApplication; |
michael@0 | 89 | #endif |
michael@0 | 90 | |
michael@0 | 91 | // Return false to abort the Run. |
michael@0 | 92 | bool BeforeRun(); |
michael@0 | 93 | void AfterRun(); |
michael@0 | 94 | |
michael@0 | 95 | MessageLoop* loop_; |
michael@0 | 96 | |
michael@0 | 97 | // WeakPtrFactory for QuitClosure safety. |
michael@0 | 98 | base::WeakPtrFactory<RunLoop> weak_factory_; |
michael@0 | 99 | |
michael@0 | 100 | // Parent RunLoop or NULL if this is the top-most RunLoop. |
michael@0 | 101 | RunLoop* previous_run_loop_; |
michael@0 | 102 | |
michael@0 | 103 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
michael@0 | 104 | !defined(USE_GTK_MESSAGE_PUMP) |
michael@0 | 105 | MessageLoop::Dispatcher* dispatcher_; |
michael@0 | 106 | #endif |
michael@0 | 107 | |
michael@0 | 108 | // Used to count how many nested Run() invocations are on the stack. |
michael@0 | 109 | int run_depth_; |
michael@0 | 110 | |
michael@0 | 111 | bool run_called_; |
michael@0 | 112 | bool quit_called_; |
michael@0 | 113 | bool running_; |
michael@0 | 114 | |
michael@0 | 115 | // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning |
michael@0 | 116 | // that we should quit Run once it becomes idle. |
michael@0 | 117 | bool quit_when_idle_received_; |
michael@0 | 118 | |
michael@0 | 119 | DISALLOW_COPY_AND_ASSIGN(RunLoop); |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | } // namespace base |
michael@0 | 123 | |
michael@0 | 124 | #endif // BASE_RUN_LOOP_H_ |