OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2008 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 |
| 5 // The basis for all native run loops on the Mac is the CFRunLoop. It can be |
| 6 // used directly, it can be used as the driving force behind the similar |
| 7 // Foundation NSRunLoop, and it can be used to implement higher-level event |
| 8 // loops such as the NSApplication event loop. |
| 9 // |
| 10 // This file introduces a basic CFRunLoop-based implementation of the |
| 11 // MessagePump interface called CFRunLoopBase. CFRunLoopBase contains all |
| 12 // of the machinery necessary to dispatch events to a delegate, but does not |
| 13 // implement the specific run loop. Concrete subclasses must provide their |
| 14 // own DoRun and Quit implementations. |
| 15 // |
| 16 // A concrete subclass that just runs a CFRunLoop loop is provided in |
| 17 // MessagePumpCFRunLoop. For an NSRunLoop, the similar MessagePumpNSRunLoop |
| 18 // is provided. |
| 19 // |
| 20 // For the application's event loop, an implementation based on AppKit's |
| 21 // NSApplication event system is provided in MessagePumpNSApplication. |
| 22 // |
| 23 // Typically, MessagePumpNSApplication only makes sense on a Cocoa |
| 24 // application's main thread. If a CFRunLoop-based message pump is needed on |
| 25 // any other thread, one of the other concrete subclasses is preferrable. |
| 26 // MessagePumpMac::Create is defined, which returns a new NSApplication-based |
| 27 // or NSRunLoop-based MessagePump subclass depending on which thread it is |
| 28 // called on. |
| 29 |
| 30 #ifndef BASE_MESSAGE_PUMP_MAC_H_ |
| 31 #define BASE_MESSAGE_PUMP_MAC_H_ |
| 32 |
| 33 #include "base/message_pump.h" |
| 34 |
| 35 #include <CoreFoundation/CoreFoundation.h> |
| 36 |
| 37 #include "base/time.h" |
| 38 |
| 39 namespace base { |
| 40 |
| 41 class MessagePumpCFRunLoopBase : public MessagePump { |
| 42 public: |
| 43 MessagePumpCFRunLoopBase(); |
| 44 virtual ~MessagePumpCFRunLoopBase(); |
| 45 |
| 46 // Subclasses should implement the work they need to do in MessagePump::Run |
| 47 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. |
| 48 // This arrangement is used because MessagePumpCFRunLoopBase needs to set |
| 49 // up and tear down things before and after the "meat" of DoRun. |
| 50 virtual void Run(Delegate* delegate); |
| 51 virtual void DoRun(Delegate* delegate) = 0; |
| 52 |
| 53 virtual void ScheduleWork(); |
| 54 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
| 55 |
| 56 protected: |
| 57 // The thread's run loop. |
| 58 CFRunLoopRef run_loop_; |
| 59 |
| 60 private: |
| 61 // Timer callback scheduled by ScheduleDelayedWork. This does not do any |
| 62 // work, but it signals delayed_work_source_ so that delayed work can be |
| 63 // performed within the appropriate priority constraints. |
| 64 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); |
| 65 |
| 66 // Perform highest-priority work. This is associated with work_source_ |
| 67 // signalled by ScheduleWork. |
| 68 static void RunWork(void* info); |
| 69 |
| 70 // Perform delayed-priority work. This is associated with |
| 71 // delayed_work_source_ signalled by RunDelayedWorkTimer, and is responsible |
| 72 // for calling ScheduleDelayedWork again if appropriate. |
| 73 static void RunDelayedWork(void* info); |
| 74 |
| 75 // Observer callback responsible for performing idle-priority work, before |
| 76 // the run loop goes to sleep. Associated with idle_work_observer_. |
| 77 static void RunIdleWork(CFRunLoopObserverRef observer, |
| 78 CFRunLoopActivity activity, void* info); |
| 79 |
| 80 // The timer, sources, and observer are described above alongside their |
| 81 // callbacks. |
| 82 CFRunLoopTimerRef delayed_work_timer_; |
| 83 CFRunLoopSourceRef work_source_; |
| 84 CFRunLoopSourceRef delayed_work_source_; |
| 85 CFRunLoopObserverRef idle_work_observer_; |
| 86 |
| 87 // (weak) Delegate passed as an argument to the innermost Run call. |
| 88 Delegate* delegate_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); |
| 91 }; |
| 92 |
| 93 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { |
| 94 public: |
| 95 MessagePumpCFRunLoop(); |
| 96 virtual ~MessagePumpCFRunLoop(); |
| 97 |
| 98 virtual void DoRun(Delegate* delegate); |
| 99 virtual void Quit(); |
| 100 |
| 101 private: |
| 102 // Observer callback called when the run loop starts and stops, at the |
| 103 // beginning and end of calls to CFRunLoopRun. This is used to maintain |
| 104 // nesting_level_ and to handle deferred loop quits. Associated with |
| 105 // enter_exit_observer_. |
| 106 static void EnterExitRunLoop(CFRunLoopObserverRef observer, |
| 107 CFRunLoopActivity activity, void* info); |
| 108 |
| 109 // Observer for EnterExitRunLoop. |
| 110 CFRunLoopObserverRef enter_exit_observer_; |
| 111 |
| 112 // The recursion depth of the currently-executing CFRunLoopRun loop on the |
| 113 // run loop's thread. 0 if no run loops are running inside of whatever scope |
| 114 // the object was created in. |
| 115 int nesting_level_; |
| 116 |
| 117 // The recursion depth (calculated in the same way as nesting_level_) of the |
| 118 // innermost executing CFRunLoopRun loop started by a call to Run. |
| 119 int innermost_quittable_; |
| 120 |
| 121 // True if Quit is called to stop the innermost MessagePump |
| 122 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) |
| 123 // is running inside the MessagePump's innermost Run call. |
| 124 bool quit_pending_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop); |
| 127 }; |
| 128 |
| 129 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase { |
| 130 public: |
| 131 MessagePumpNSRunLoop(); |
| 132 virtual ~MessagePumpNSRunLoop(); |
| 133 |
| 134 virtual void DoRun(Delegate* delegate); |
| 135 virtual void Quit(); |
| 136 |
| 137 private: |
| 138 // A source that doesn't do anything but provide something signalable |
| 139 // attached to the run loop. This source will be signalled when Quit |
| 140 // is called, to cause the loop to wake up so that it can stop. |
| 141 CFRunLoopSourceRef quit_source_; |
| 142 |
| 143 // False after Quit is called. |
| 144 bool keep_running_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); |
| 147 }; |
| 148 |
| 149 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { |
| 150 public: |
| 151 MessagePumpNSApplication(); |
| 152 |
| 153 virtual void DoRun(Delegate* delegate); |
| 154 virtual void Quit(); |
| 155 |
| 156 private: |
| 157 // False after Quit is called. |
| 158 bool keep_running_; |
| 159 |
| 160 // True if DoRun is managing its own run loop as opposed to letting |
| 161 // -[NSApplication run] handle it. The outermost run loop in the application |
| 162 // is managed by -[NSApplication run], inner run loops are handled by a loop |
| 163 // in DoRun. |
| 164 bool running_own_loop_; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); |
| 167 }; |
| 168 |
| 169 class MessagePumpMac { |
| 170 public: |
| 171 // Returns a new instance of MessagePumpNSApplication if called on the main |
| 172 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop. |
| 173 static MessagePump* Create(); |
| 174 |
| 175 private: |
| 176 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
| 177 }; |
| 178 |
| 179 } // namespace base |
| 180 |
| 181 #endif // BASE_MESSAGE_PUMP_MAC_H_ |
OLD | NEW |