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