OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 // The basis for all native run loops on the Mac is the CFRunLoop. It can be | 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 | 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 | 7 // Foundation NSRunLoop, and it can be used to implement higher-level event |
8 // loops such as the NSApplication event loop. | 8 // loops such as the NSApplication event loop. |
9 // | 9 // |
10 // This file introduces a basic CFRunLoop-based implementation of the | 10 // This file introduces a basic CFRunLoop-based implementation of the |
11 // MessagePump interface called CFRunLoopBase. CFRunLoopBase contains all | 11 // MessagePump interface called CFRunLoopBase. CFRunLoopBase contains all |
(...skipping 16 matching lines...) Expand all Loading... |
28 // called on. | 28 // called on. |
29 | 29 |
30 #ifndef BASE_MESSAGE_PUMP_MAC_H_ | 30 #ifndef BASE_MESSAGE_PUMP_MAC_H_ |
31 #define BASE_MESSAGE_PUMP_MAC_H_ | 31 #define BASE_MESSAGE_PUMP_MAC_H_ |
32 | 32 |
33 #include "base/message_pump.h" | 33 #include "base/message_pump.h" |
34 | 34 |
35 #include <CoreFoundation/CoreFoundation.h> | 35 #include <CoreFoundation/CoreFoundation.h> |
36 #include <IOKit/IOKitLib.h> | 36 #include <IOKit/IOKitLib.h> |
37 | 37 |
38 #if defined(__OBJC__) | |
39 @class NSAutoreleasePool; | |
40 #else // __OBJC__ | |
41 class NSAutoreleasePool; | |
42 #endif // __OBJC__ | |
43 | |
44 | |
45 namespace base { | 38 namespace base { |
46 | 39 |
47 class Time; | 40 class Time; |
48 | 41 |
49 class MessagePumpCFRunLoopBase : public MessagePump { | 42 class MessagePumpCFRunLoopBase : public MessagePump { |
50 public: | 43 public: |
51 MessagePumpCFRunLoopBase(); | 44 MessagePumpCFRunLoopBase(); |
52 virtual ~MessagePumpCFRunLoopBase(); | 45 virtual ~MessagePumpCFRunLoopBase(); |
53 | 46 |
54 // Subclasses should implement the work they need to do in MessagePump::Run | 47 // Subclasses should implement the work they need to do in MessagePump::Run |
55 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. | 48 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. |
56 // This arrangement is used because MessagePumpCFRunLoopBase needs to set | 49 // This arrangement is used because MessagePumpCFRunLoopBase needs to set |
57 // up and tear down things before and after the "meat" of DoRun. | 50 // up and tear down things before and after the "meat" of DoRun. |
58 virtual void Run(Delegate* delegate); | 51 virtual void Run(Delegate* delegate); |
59 virtual void DoRun(Delegate* delegate) = 0; | 52 virtual void DoRun(Delegate* delegate) = 0; |
60 | 53 |
61 virtual void ScheduleWork(); | 54 virtual void ScheduleWork(); |
62 virtual void ScheduleDelayedWork(const Time& delayed_work_time); | 55 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
63 | 56 |
64 protected: | 57 protected: |
65 // Accessors for private data members to be used by subclasses. | 58 // Accessors for private data members to be used by subclasses. |
66 CFRunLoopRef run_loop() const { return run_loop_; } | 59 CFRunLoopRef run_loop() const { return run_loop_; } |
67 int nesting_level() const { return nesting_level_; } | 60 int nesting_level() const { return nesting_level_; } |
68 int run_nesting_level() const { return run_nesting_level_; } | 61 int run_nesting_level() const { return run_nesting_level_; } |
69 | 62 |
70 // Factory method for creating an autorelease pool. Not all message | |
71 // pumps work with autorelease pools in the same way. | |
72 virtual NSAutoreleasePool* CreateAutoreleasePool(); | |
73 | |
74 private: | 63 private: |
75 // Timer callback scheduled by ScheduleDelayedWork. This does not do any | 64 // Timer callback scheduled by ScheduleDelayedWork. This does not do any |
76 // work, but it signals delayed_work_source_ so that delayed work can be | 65 // work, but it signals delayed_work_source_ so that delayed work can be |
77 // performed within the appropriate priority constraints. | 66 // performed within the appropriate priority constraints. |
78 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); | 67 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); |
79 | 68 |
80 // Perform highest-priority work. This is associated with work_source_ | 69 // Perform highest-priority work. This is associated with work_source_ |
81 // signalled by ScheduleWork. The static method calls the instance method; | 70 // signalled by ScheduleWork. The static method calls the instance method; |
82 // the instance method returns true if work was done. | 71 // the instance method returns true if work was done. |
83 static void RunWorkSource(void* info); | 72 static void RunWorkSource(void* info); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // is called, to cause the loop to wake up so that it can stop. | 216 // is called, to cause the loop to wake up so that it can stop. |
228 CFRunLoopSourceRef quit_source_; | 217 CFRunLoopSourceRef quit_source_; |
229 | 218 |
230 // False after Quit is called. | 219 // False after Quit is called. |
231 bool keep_running_; | 220 bool keep_running_; |
232 | 221 |
233 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); | 222 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); |
234 }; | 223 }; |
235 | 224 |
236 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { | 225 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { |
237 // ObjC objects and C++ objects can't be friends, so this function | |
238 // will act as a friendly trampoline for | |
239 // MessagePumpNSAppDeferredAutoReleasePool to call | |
240 // set_needs_event_loop_wakeup. | |
241 friend void SetNeedsEventLoopWakeUpTrue(MessagePumpNSApplication* pump); | |
242 | |
243 public: | 226 public: |
244 MessagePumpNSApplication(); | 227 MessagePumpNSApplication(); |
245 | 228 |
246 virtual void DoRun(Delegate* delegate); | 229 virtual void DoRun(Delegate* delegate); |
247 virtual void Quit(); | 230 virtual void Quit(); |
248 | 231 |
249 protected: | |
250 // MessagePumpNSApplications need a special autorelease pool that works | |
251 // correctly when nested inside another autorelease pool. | |
252 virtual NSAutoreleasePool* CreateAutoreleasePool(); | |
253 | |
254 // Sets a flag that will trigger the sending of an NSEvent to wake up the | |
255 // NSApplication event loop when the run loop exits. | |
256 void set_needs_event_loop_wake_up_true() { | |
257 needs_event_loop_wake_up_ = true; | |
258 } | |
259 | |
260 private: | 232 private: |
261 virtual void EnterExitRunLoop(CFRunLoopActivity activity); | |
262 | |
263 // Send a null event through to the event loop if necessary. | |
264 void WakeUpEventLoop(); | |
265 | |
266 // False after Quit is called. | 233 // False after Quit is called. |
267 bool keep_running_; | 234 bool keep_running_; |
268 | 235 |
269 // True if DoRun is managing its own run loop as opposed to letting | 236 // True if DoRun is managing its own run loop as opposed to letting |
270 // -[NSApplication run] handle it. The outermost run loop in the application | 237 // -[NSApplication run] handle it. The outermost run loop in the application |
271 // is managed by -[NSApplication run], inner run loops are handled by a loop | 238 // is managed by -[NSApplication run], inner run loops are handled by a loop |
272 // in DoRun. | 239 // in DoRun. |
273 bool running_own_loop_; | 240 bool running_own_loop_; |
274 | 241 |
275 // True if an event should be sent to the event loop to cause it to spin | |
276 // when the run loop is exiting. | |
277 bool needs_event_loop_wake_up_; | |
278 | |
279 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); | 242 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); |
280 }; | 243 }; |
281 | 244 |
282 class MessagePumpMac { | 245 class MessagePumpMac { |
283 public: | 246 public: |
284 // Returns a new instance of MessagePumpNSApplication if called on the main | 247 // Returns a new instance of MessagePumpNSApplication if called on the main |
285 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop. | 248 // thread. Otherwise, returns a new instance of MessagePumpNSRunLoop. |
286 static MessagePump* Create(); | 249 static MessagePump* Create(); |
287 | 250 |
288 private: | 251 private: |
289 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | 252 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
290 }; | 253 }; |
291 | 254 |
292 } // namespace base | 255 } // namespace base |
293 | 256 |
294 #endif // BASE_MESSAGE_PUMP_MAC_H_ | 257 #endif // BASE_MESSAGE_PUMP_MAC_H_ |
OLD | NEW |