OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // "NSAutoreleasePool" in such TUs. Finally, in Manual Retain Release (MRR) | 71 // "NSAutoreleasePool" in such TUs. Finally, in Manual Retain Release (MRR) |
72 // Objective-C TUs, it is a type alias for NSAutoreleasePool. In all cases, a | 72 // Objective-C TUs, it is a type alias for NSAutoreleasePool. In all cases, a |
73 // method that takes or returns an NSAutoreleasePool* can use | 73 // method that takes or returns an NSAutoreleasePool* can use |
74 // AutoreleasePoolType* instead. | 74 // AutoreleasePoolType* instead. |
75 #if !defined(__OBJC__) || __has_feature(objc_arc) | 75 #if !defined(__OBJC__) || __has_feature(objc_arc) |
76 class AutoreleasePoolType; | 76 class AutoreleasePoolType; |
77 #else // !defined(__OBJC__) || __has_feature(objc_arc) | 77 #else // !defined(__OBJC__) || __has_feature(objc_arc) |
78 typedef NSAutoreleasePool AutoreleasePoolType; | 78 typedef NSAutoreleasePool AutoreleasePoolType; |
79 #endif // !defined(__OBJC__) || __has_feature(objc_arc) | 79 #endif // !defined(__OBJC__) || __has_feature(objc_arc) |
80 | 80 |
81 class MessagePumpCFRunLoopBase : public MessagePump { | 81 class BASE_EXPORT MessagePumpCFRunLoopBase : public MessagePump { |
82 // Needs access to CreateAutoreleasePool. | 82 // Needs access to CreateAutoreleasePool. |
83 friend class MessagePumpScopedAutoreleasePool; | 83 friend class MessagePumpScopedAutoreleasePool; |
| 84 friend class TestMessagePumpCFRunLoopBase; |
| 85 |
84 public: | 86 public: |
85 MessagePumpCFRunLoopBase(); | 87 MessagePumpCFRunLoopBase(); |
86 ~MessagePumpCFRunLoopBase() override; | 88 ~MessagePumpCFRunLoopBase() override; |
87 | 89 |
88 // Subclasses should implement the work they need to do in MessagePump::Run | 90 // Subclasses should implement the work they need to do in MessagePump::Run |
89 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. | 91 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. |
90 // This arrangement is used because MessagePumpCFRunLoopBase needs to set | 92 // This arrangement is used because MessagePumpCFRunLoopBase needs to set |
91 // up and tear down things before and after the "meat" of DoRun. | 93 // up and tear down things before and after the "meat" of DoRun. |
92 void Run(Delegate* delegate) override; | 94 void Run(Delegate* delegate) override; |
93 virtual void DoRun(Delegate* delegate) = 0; | 95 virtual void DoRun(Delegate* delegate) = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
106 // |delegateless_work_| is true. |delegate| can be NULL. | 108 // |delegateless_work_| is true. |delegate| can be NULL. |
107 void SetDelegate(Delegate* delegate); | 109 void SetDelegate(Delegate* delegate); |
108 | 110 |
109 // Return an autorelease pool to wrap around any work being performed. | 111 // Return an autorelease pool to wrap around any work being performed. |
110 // In some cases, CreateAutoreleasePool may return nil intentionally to | 112 // In some cases, CreateAutoreleasePool may return nil intentionally to |
111 // preventing an autorelease pool from being created, allowing any | 113 // preventing an autorelease pool from being created, allowing any |
112 // objects autoreleased by work to fall into the current autorelease pool. | 114 // objects autoreleased by work to fall into the current autorelease pool. |
113 virtual AutoreleasePoolType* CreateAutoreleasePool(); | 115 virtual AutoreleasePoolType* CreateAutoreleasePool(); |
114 | 116 |
115 private: | 117 private: |
| 118 // Marking timers as invalid at the right time helps significantly reduce |
| 119 // power use (see the comment in RunDelayedWorkTimer()), however there is no |
| 120 // public API for doing so. CFRuntime.h states that CFRuntimeBase, upon which |
| 121 // the above timer invalidation functions are based, can change from release |
| 122 // to release and should not be accessed directly (this struct last changed at |
| 123 // least in 2008 in CF-476). |
| 124 // |
| 125 // This function uses private API to modify a test timer's valid state and |
| 126 // uses public API to confirm that the private API changed the right bit. |
| 127 static bool CanInvalidateCFRunLoopTimers(); |
| 128 |
| 129 // Sets a Core Foundation object's "invalid" bit to |valid|. Based on code |
| 130 // from CFRunLoop.c. |
| 131 static void ChromeCFRunLoopTimerSetValid(CFRunLoopTimerRef timer, bool valid); |
| 132 |
116 // Timer callback scheduled by ScheduleDelayedWork. This does not do any | 133 // Timer callback scheduled by ScheduleDelayedWork. This does not do any |
117 // work, but it signals work_source_ so that delayed work can be performed | 134 // work, but it signals work_source_ so that delayed work can be performed |
118 // within the appropriate priority constraints. | 135 // within the appropriate priority constraints. |
119 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); | 136 static void RunDelayedWorkTimer(CFRunLoopTimerRef timer, void* info); |
120 | 137 |
121 // Perform highest-priority work. This is associated with work_source_ | 138 // Perform highest-priority work. This is associated with work_source_ |
122 // signalled by ScheduleWork or RunDelayedWorkTimer. The static method calls | 139 // signalled by ScheduleWork or RunDelayedWorkTimer. The static method calls |
123 // the instance method; the instance method returns true if it resignalled | 140 // the instance method; the instance method returns true if it resignalled |
124 // work_source_ to be called again from the loop. | 141 // work_source_ to be called again from the loop. |
125 static void RunWorkSource(void* info); | 142 static void RunWorkSource(void* info); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | 362 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
346 }; | 363 }; |
347 | 364 |
348 // Tasks posted to the message loop are posted under this mode, as well | 365 // Tasks posted to the message loop are posted under this mode, as well |
349 // as kCFRunLoopCommonModes. | 366 // as kCFRunLoopCommonModes. |
350 extern const CFStringRef BASE_EXPORT kMessageLoopExclusiveRunLoopMode; | 367 extern const CFStringRef BASE_EXPORT kMessageLoopExclusiveRunLoopMode; |
351 | 368 |
352 } // namespace base | 369 } // namespace base |
353 | 370 |
354 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ | 371 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ |
OLD | NEW |