OLD | NEW |
| (Empty) |
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 | |
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 #if !defined(__OBJC__) | |
38 class NSAutoreleasePool; | |
39 #else // !defined(__OBJC__) | |
40 #if defined(OS_IOS) | |
41 #import <Foundation/Foundation.h> | |
42 #else | |
43 #import <AppKit/AppKit.h> | |
44 | |
45 // Clients must subclass NSApplication and implement this protocol if they use | |
46 // MessagePumpMac. | |
47 @protocol CrAppProtocol | |
48 // Must return true if -[NSApplication sendEvent:] is currently on the stack. | |
49 // See the comment for |CreateAutoreleasePool()| in the cc file for why this is | |
50 // necessary. | |
51 - (BOOL)isHandlingSendEvent; | |
52 @end | |
53 #endif // !defined(OS_IOS) | |
54 #endif // !defined(__OBJC__) | |
55 | |
56 namespace base { | |
57 | |
58 class RunLoop; | |
59 class TimeTicks; | |
60 | |
61 class MessagePumpCFRunLoopBase : public MessagePump { | |
62 // Needs access to CreateAutoreleasePool. | |
63 friend class MessagePumpScopedAutoreleasePool; | |
64 public: | |
65 MessagePumpCFRunLoopBase(); | |
66 | |
67 // Subclasses should implement the work they need to do in MessagePump::Run | |
68 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. | |
69 // This arrangement is used because MessagePumpCFRunLoopBase needs to set | |
70 // up and tear down things before and after the "meat" of DoRun. | |
71 virtual void Run(Delegate* delegate) OVERRIDE; | |
72 virtual void DoRun(Delegate* delegate) = 0; | |
73 | |
74 virtual void ScheduleWork() OVERRIDE; | |
75 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; | |
76 | |
77 protected: | |
78 virtual ~MessagePumpCFRunLoopBase(); | |
79 | |
80 // Accessors for private data members to be used by subclasses. | |
81 CFRunLoopRef run_loop() const { return run_loop_; } | |
82 int nesting_level() const { return nesting_level_; } | |
83 int run_nesting_level() const { return run_nesting_level_; } | |
84 | |
85 // Sets this pump's delegate. Signals the appropriate sources if | |
86 // |delegateless_work_| is true. |delegate| can be NULL. | |
87 void SetDelegate(Delegate* delegate); | |
88 | |
89 // Return an autorelease pool to wrap around any work being performed. | |
90 // In some cases, CreateAutoreleasePool may return nil intentionally to | |
91 // preventing an autorelease pool from being created, allowing any | |
92 // objects autoreleased by work to fall into the current autorelease pool. | |
93 virtual NSAutoreleasePool* CreateAutoreleasePool(); | |
94 | |
95 private: | |
96 // Timer callback scheduled by ScheduleDelayedWork. This does not do any | |
97 // work, but it signals work_source_ so that delayed work can be performed | |
98 // within the appropriate priority constraints. | |
99 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); | |
100 | |
101 // Perform highest-priority work. This is associated with work_source_ | |
102 // signalled by ScheduleWork or RunDelayedWorkTimer. The static method calls | |
103 // the instance method; the instance method returns true if it resignalled | |
104 // work_source_ to be called again from the loop. | |
105 static void RunWorkSource(void* info); | |
106 bool RunWork(); | |
107 | |
108 // Perform idle-priority work. This is normally called by PreWaitObserver, | |
109 // but is also associated with idle_work_source_. When this function | |
110 // actually does perform idle work, it will resignal that source. The | |
111 // static method calls the instance method; the instance method returns | |
112 // true if idle work was done. | |
113 static void RunIdleWorkSource(void* info); | |
114 bool RunIdleWork(); | |
115 | |
116 // Perform work that may have been deferred because it was not runnable | |
117 // within a nested run loop. This is associated with | |
118 // nesting_deferred_work_source_ and is signalled by | |
119 // MaybeScheduleNestingDeferredWork when returning from a nested loop, | |
120 // so that an outer loop will be able to perform the necessary tasks if it | |
121 // permits nestable tasks. | |
122 static void RunNestingDeferredWorkSource(void* info); | |
123 bool RunNestingDeferredWork(); | |
124 | |
125 // Schedules possible nesting-deferred work to be processed before the run | |
126 // loop goes to sleep, exits, or begins processing sources at the top of its | |
127 // loop. If this function detects that a nested loop had run since the | |
128 // previous attempt to schedule nesting-deferred work, it will schedule a | |
129 // call to RunNestingDeferredWorkSource. | |
130 void MaybeScheduleNestingDeferredWork(); | |
131 | |
132 // Observer callback responsible for performing idle-priority work, before | |
133 // the run loop goes to sleep. Associated with idle_work_observer_. | |
134 static void PreWaitObserver(CFRunLoopObserverRef observer, | |
135 CFRunLoopActivity activity, void* info); | |
136 | |
137 // Observer callback called before the run loop processes any sources. | |
138 // Associated with pre_source_observer_. | |
139 static void PreSourceObserver(CFRunLoopObserverRef observer, | |
140 CFRunLoopActivity activity, void* info); | |
141 | |
142 // Observer callback called when the run loop starts and stops, at the | |
143 // beginning and end of calls to CFRunLoopRun. This is used to maintain | |
144 // nesting_level_. Associated with enter_exit_observer_. | |
145 static void EnterExitObserver(CFRunLoopObserverRef observer, | |
146 CFRunLoopActivity activity, void* info); | |
147 | |
148 // Called by EnterExitObserver after performing maintenance on nesting_level_. | |
149 // This allows subclasses an opportunity to perform additional processing on | |
150 // the basis of run loops starting and stopping. | |
151 virtual void EnterExitRunLoop(CFRunLoopActivity activity); | |
152 | |
153 // The thread's run loop. | |
154 CFRunLoopRef run_loop_; | |
155 | |
156 // The timer, sources, and observers are described above alongside their | |
157 // callbacks. | |
158 CFRunLoopTimerRef delayed_work_timer_; | |
159 CFRunLoopSourceRef work_source_; | |
160 CFRunLoopSourceRef idle_work_source_; | |
161 CFRunLoopSourceRef nesting_deferred_work_source_; | |
162 CFRunLoopObserverRef pre_wait_observer_; | |
163 CFRunLoopObserverRef pre_source_observer_; | |
164 CFRunLoopObserverRef enter_exit_observer_; | |
165 | |
166 // (weak) Delegate passed as an argument to the innermost Run call. | |
167 Delegate* delegate_; | |
168 | |
169 // The time that delayed_work_timer_ is scheduled to fire. This is tracked | |
170 // independently of CFRunLoopTimerGetNextFireDate(delayed_work_timer_) | |
171 // to be able to reset the timer properly after waking from system sleep. | |
172 // See PowerStateNotification. | |
173 CFAbsoluteTime delayed_work_fire_time_; | |
174 | |
175 // The recursion depth of the currently-executing CFRunLoopRun loop on the | |
176 // run loop's thread. 0 if no run loops are running inside of whatever scope | |
177 // the object was created in. | |
178 int nesting_level_; | |
179 | |
180 // The recursion depth (calculated in the same way as nesting_level_) of the | |
181 // innermost executing CFRunLoopRun loop started by a call to Run. | |
182 int run_nesting_level_; | |
183 | |
184 // The deepest (numerically highest) recursion depth encountered since the | |
185 // most recent attempt to run nesting-deferred work. | |
186 int deepest_nesting_level_; | |
187 | |
188 // "Delegateless" work flags are set when work is ready to be performed but | |
189 // must wait until a delegate is available to process it. This can happen | |
190 // when a MessagePumpCFRunLoopBase is instantiated and work arrives without | |
191 // any call to Run on the stack. The Run method will check for delegateless | |
192 // work on entry and redispatch it as needed once a delegate is available. | |
193 bool delegateless_work_; | |
194 bool delegateless_idle_work_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); | |
197 }; | |
198 | |
199 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { | |
200 public: | |
201 MessagePumpCFRunLoop(); | |
202 | |
203 virtual void DoRun(Delegate* delegate) OVERRIDE; | |
204 virtual void Quit() OVERRIDE; | |
205 | |
206 protected: | |
207 virtual ~MessagePumpCFRunLoop(); | |
208 | |
209 private: | |
210 virtual void EnterExitRunLoop(CFRunLoopActivity activity) OVERRIDE; | |
211 | |
212 // True if Quit is called to stop the innermost MessagePump | |
213 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) | |
214 // is running inside the MessagePump's innermost Run call. | |
215 bool quit_pending_; | |
216 | |
217 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop); | |
218 }; | |
219 | |
220 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase { | |
221 public: | |
222 BASE_EXPORT MessagePumpNSRunLoop(); | |
223 | |
224 virtual void DoRun(Delegate* delegate) OVERRIDE; | |
225 virtual void Quit() OVERRIDE; | |
226 | |
227 protected: | |
228 virtual ~MessagePumpNSRunLoop(); | |
229 | |
230 private: | |
231 // A source that doesn't do anything but provide something signalable | |
232 // attached to the run loop. This source will be signalled when Quit | |
233 // is called, to cause the loop to wake up so that it can stop. | |
234 CFRunLoopSourceRef quit_source_; | |
235 | |
236 // False after Quit is called. | |
237 bool keep_running_; | |
238 | |
239 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); | |
240 }; | |
241 | |
242 #if defined(OS_IOS) | |
243 // This is a fake message pump. It attaches sources to the main thread's | |
244 // CFRunLoop, so PostTask() will work, but it is unable to drive the loop | |
245 // directly, so calling Run() or Quit() are errors. | |
246 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase { | |
247 public: | |
248 MessagePumpUIApplication(); | |
249 virtual void DoRun(Delegate* delegate) OVERRIDE; | |
250 virtual void Quit() OVERRIDE; | |
251 | |
252 // This message pump can not spin the main message loop directly. Instead, | |
253 // call |Attach()| to set up a delegate. It is an error to call |Run()|. | |
254 virtual void Attach(Delegate* delegate); | |
255 | |
256 protected: | |
257 virtual ~MessagePumpUIApplication(); | |
258 | |
259 private: | |
260 base::RunLoop* run_loop_; | |
261 | |
262 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication); | |
263 }; | |
264 | |
265 #else | |
266 | |
267 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { | |
268 public: | |
269 MessagePumpNSApplication(); | |
270 | |
271 virtual void DoRun(Delegate* delegate) OVERRIDE; | |
272 virtual void Quit() OVERRIDE; | |
273 | |
274 protected: | |
275 virtual ~MessagePumpNSApplication(); | |
276 | |
277 private: | |
278 // False after Quit is called. | |
279 bool keep_running_; | |
280 | |
281 // True if DoRun is managing its own run loop as opposed to letting | |
282 // -[NSApplication run] handle it. The outermost run loop in the application | |
283 // is managed by -[NSApplication run], inner run loops are handled by a loop | |
284 // in DoRun. | |
285 bool running_own_loop_; | |
286 | |
287 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); | |
288 }; | |
289 | |
290 class MessagePumpCrApplication : public MessagePumpNSApplication { | |
291 public: | |
292 MessagePumpCrApplication(); | |
293 | |
294 protected: | |
295 virtual ~MessagePumpCrApplication() {} | |
296 | |
297 // Returns nil if NSApp is currently in the middle of calling | |
298 // -sendEvent. Requires NSApp implementing CrAppProtocol. | |
299 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; | |
300 | |
301 private: | |
302 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); | |
303 }; | |
304 #endif // !defined(OS_IOS) | |
305 | |
306 class MessagePumpMac { | |
307 public: | |
308 // If not on the main thread, returns a new instance of | |
309 // MessagePumpNSRunLoop. | |
310 // | |
311 // On the main thread, if NSApp exists and conforms to | |
312 // CrAppProtocol, creates an instances of MessagePumpCrApplication. | |
313 // | |
314 // Otherwise creates an instance of MessagePumpNSApplication using a | |
315 // default NSApplication. | |
316 static MessagePump* Create(); | |
317 | |
318 #if !defined(OS_IOS) | |
319 // If a pump is created before the required CrAppProtocol is | |
320 // created, the wrong MessagePump subclass could be used. | |
321 // UsingCrApp() returns false if the message pump was created before | |
322 // NSApp was initialized, or if NSApp does not implement | |
323 // CrAppProtocol. NSApp must be initialized before calling. | |
324 BASE_EXPORT static bool UsingCrApp(); | |
325 | |
326 // Wrapper to query -[NSApp isHandlingSendEvent] from C++ code. | |
327 // Requires NSApp to implement CrAppProtocol. | |
328 BASE_EXPORT static bool IsHandlingSendEvent(); | |
329 #endif // !defined(OS_IOS) | |
330 | |
331 private: | |
332 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | |
333 }; | |
334 | |
335 } // namespace base | |
336 | |
337 #endif // BASE_MESSAGE_PUMP_MAC_H_ | |
OLD | NEW |