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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 namespace base { | 56 namespace base { |
57 | 57 |
58 class RunLoop; | 58 class RunLoop; |
59 class TimeTicks; | 59 class TimeTicks; |
60 | 60 |
61 class MessagePumpCFRunLoopBase : public MessagePump { | 61 class MessagePumpCFRunLoopBase : public MessagePump { |
62 // Needs access to CreateAutoreleasePool. | 62 // Needs access to CreateAutoreleasePool. |
63 friend class MessagePumpScopedAutoreleasePool; | 63 friend class MessagePumpScopedAutoreleasePool; |
64 public: | 64 public: |
65 MessagePumpCFRunLoopBase(); | 65 MessagePumpCFRunLoopBase(); |
66 virtual ~MessagePumpCFRunLoopBase(); | |
67 | 66 |
68 // Subclasses should implement the work they need to do in MessagePump::Run | 67 // Subclasses should implement the work they need to do in MessagePump::Run |
69 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. | 68 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. |
70 // This arrangement is used because MessagePumpCFRunLoopBase needs to set | 69 // This arrangement is used because MessagePumpCFRunLoopBase needs to set |
71 // up and tear down things before and after the "meat" of DoRun. | 70 // up and tear down things before and after the "meat" of DoRun. |
72 virtual void Run(Delegate* delegate) OVERRIDE; | 71 virtual void Run(Delegate* delegate) OVERRIDE; |
73 virtual void DoRun(Delegate* delegate) = 0; | 72 virtual void DoRun(Delegate* delegate) = 0; |
74 | 73 |
75 virtual void ScheduleWork() OVERRIDE; | 74 virtual void ScheduleWork() OVERRIDE; |
76 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; | 75 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; |
77 | 76 |
78 protected: | 77 protected: |
| 78 virtual ~MessagePumpCFRunLoopBase(); |
| 79 |
79 // Accessors for private data members to be used by subclasses. | 80 // Accessors for private data members to be used by subclasses. |
80 CFRunLoopRef run_loop() const { return run_loop_; } | 81 CFRunLoopRef run_loop() const { return run_loop_; } |
81 int nesting_level() const { return nesting_level_; } | 82 int nesting_level() const { return nesting_level_; } |
82 int run_nesting_level() const { return run_nesting_level_; } | 83 int run_nesting_level() const { return run_nesting_level_; } |
83 | 84 |
84 // Sets this pump's delegate. Signals the appropriate sources if | 85 // Sets this pump's delegate. Signals the appropriate sources if |
85 // |delegateless_work_| is true. |delegate| can be NULL. | 86 // |delegateless_work_| is true. |delegate| can be NULL. |
86 void SetDelegate(Delegate* delegate); | 87 void SetDelegate(Delegate* delegate); |
87 | 88 |
88 // Return an autorelease pool to wrap around any work being performed. | 89 // Return an autorelease pool to wrap around any work being performed. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // work on entry and redispatch it as needed once a delegate is available. | 192 // work on entry and redispatch it as needed once a delegate is available. |
192 bool delegateless_work_; | 193 bool delegateless_work_; |
193 bool delegateless_idle_work_; | 194 bool delegateless_idle_work_; |
194 | 195 |
195 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); | 196 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); |
196 }; | 197 }; |
197 | 198 |
198 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { | 199 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { |
199 public: | 200 public: |
200 MessagePumpCFRunLoop(); | 201 MessagePumpCFRunLoop(); |
201 virtual ~MessagePumpCFRunLoop(); | |
202 | 202 |
203 virtual void DoRun(Delegate* delegate) OVERRIDE; | 203 virtual void DoRun(Delegate* delegate) OVERRIDE; |
204 virtual void Quit() OVERRIDE; | 204 virtual void Quit() OVERRIDE; |
205 | 205 |
| 206 protected: |
| 207 virtual ~MessagePumpCFRunLoop(); |
| 208 |
206 private: | 209 private: |
207 virtual void EnterExitRunLoop(CFRunLoopActivity activity) OVERRIDE; | 210 virtual void EnterExitRunLoop(CFRunLoopActivity activity) OVERRIDE; |
208 | 211 |
209 // True if Quit is called to stop the innermost MessagePump | 212 // True if Quit is called to stop the innermost MessagePump |
210 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) | 213 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) |
211 // is running inside the MessagePump's innermost Run call. | 214 // is running inside the MessagePump's innermost Run call. |
212 bool quit_pending_; | 215 bool quit_pending_; |
213 | 216 |
214 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop); | 217 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop); |
215 }; | 218 }; |
216 | 219 |
217 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase { | 220 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase { |
218 public: | 221 public: |
219 BASE_EXPORT MessagePumpNSRunLoop(); | 222 BASE_EXPORT MessagePumpNSRunLoop(); |
220 virtual ~MessagePumpNSRunLoop(); | |
221 | 223 |
222 virtual void DoRun(Delegate* delegate) OVERRIDE; | 224 virtual void DoRun(Delegate* delegate) OVERRIDE; |
223 virtual void Quit() OVERRIDE; | 225 virtual void Quit() OVERRIDE; |
224 | 226 |
| 227 protected: |
| 228 virtual ~MessagePumpNSRunLoop(); |
| 229 |
225 private: | 230 private: |
226 // A source that doesn't do anything but provide something signalable | 231 // A source that doesn't do anything but provide something signalable |
227 // attached to the run loop. This source will be signalled when Quit | 232 // attached to the run loop. This source will be signalled when Quit |
228 // is called, to cause the loop to wake up so that it can stop. | 233 // is called, to cause the loop to wake up so that it can stop. |
229 CFRunLoopSourceRef quit_source_; | 234 CFRunLoopSourceRef quit_source_; |
230 | 235 |
231 // False after Quit is called. | 236 // False after Quit is called. |
232 bool keep_running_; | 237 bool keep_running_; |
233 | 238 |
234 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); | 239 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); |
235 }; | 240 }; |
236 | 241 |
237 #if defined(OS_IOS) | 242 #if defined(OS_IOS) |
238 // This is a fake message pump. It attaches sources to the main thread's | 243 // This is a fake message pump. It attaches sources to the main thread's |
239 // CFRunLoop, so PostTask() will work, but it is unable to drive the loop | 244 // CFRunLoop, so PostTask() will work, but it is unable to drive the loop |
240 // directly, so calling Run() or Quit() are errors. | 245 // directly, so calling Run() or Quit() are errors. |
241 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase { | 246 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase { |
242 public: | 247 public: |
243 MessagePumpUIApplication(); | 248 MessagePumpUIApplication(); |
244 virtual ~MessagePumpUIApplication(); | |
245 virtual void DoRun(Delegate* delegate) OVERRIDE; | 249 virtual void DoRun(Delegate* delegate) OVERRIDE; |
246 virtual void Quit() OVERRIDE; | 250 virtual void Quit() OVERRIDE; |
247 | 251 |
248 // This message pump can not spin the main message loop directly. Instead, | 252 // This message pump can not spin the main message loop directly. Instead, |
249 // call |Attach()| to set up a delegate. It is an error to call |Run()|. | 253 // call |Attach()| to set up a delegate. It is an error to call |Run()|. |
250 virtual void Attach(Delegate* delegate); | 254 virtual void Attach(Delegate* delegate); |
251 | 255 |
| 256 protected: |
| 257 virtual ~MessagePumpUIApplication(); |
| 258 |
252 private: | 259 private: |
253 RunLoop* run_loop_; | 260 RunLoop* run_loop_; |
254 | 261 |
255 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication); | 262 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication); |
256 }; | 263 }; |
257 | 264 |
258 #else | 265 #else |
259 | 266 |
260 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { | 267 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { |
261 public: | 268 public: |
262 MessagePumpNSApplication(); | 269 MessagePumpNSApplication(); |
263 virtual ~MessagePumpNSApplication(); | |
264 | 270 |
265 virtual void DoRun(Delegate* delegate) OVERRIDE; | 271 virtual void DoRun(Delegate* delegate) OVERRIDE; |
266 virtual void Quit() OVERRIDE; | 272 virtual void Quit() OVERRIDE; |
267 | 273 |
| 274 protected: |
| 275 virtual ~MessagePumpNSApplication(); |
| 276 |
268 private: | 277 private: |
269 // False after Quit is called. | 278 // False after Quit is called. |
270 bool keep_running_; | 279 bool keep_running_; |
271 | 280 |
272 // True if DoRun is managing its own run loop as opposed to letting | 281 // True if DoRun is managing its own run loop as opposed to letting |
273 // -[NSApplication run] handle it. The outermost run loop in the application | 282 // -[NSApplication run] handle it. The outermost run loop in the application |
274 // is managed by -[NSApplication run], inner run loops are handled by a loop | 283 // is managed by -[NSApplication run], inner run loops are handled by a loop |
275 // in DoRun. | 284 // in DoRun. |
276 bool running_own_loop_; | 285 bool running_own_loop_; |
277 | 286 |
278 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); | 287 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); |
279 }; | 288 }; |
280 | 289 |
281 class MessagePumpCrApplication : public MessagePumpNSApplication { | 290 class MessagePumpCrApplication : public MessagePumpNSApplication { |
282 public: | 291 public: |
283 MessagePumpCrApplication(); | 292 MessagePumpCrApplication(); |
284 virtual ~MessagePumpCrApplication(); | |
285 | 293 |
286 protected: | 294 protected: |
| 295 virtual ~MessagePumpCrApplication() {} |
| 296 |
287 // Returns nil if NSApp is currently in the middle of calling | 297 // Returns nil if NSApp is currently in the middle of calling |
288 // -sendEvent. Requires NSApp implementing CrAppProtocol. | 298 // -sendEvent. Requires NSApp implementing CrAppProtocol. |
289 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; | 299 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; |
290 | 300 |
291 private: | 301 private: |
292 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); | 302 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); |
293 }; | 303 }; |
294 #endif // !defined(OS_IOS) | 304 #endif // !defined(OS_IOS) |
295 | 305 |
296 class MessagePumpMac { | 306 class MessagePumpMac { |
(...skipping 21 matching lines...) Expand all Loading... |
318 BASE_EXPORT static bool IsHandlingSendEvent(); | 328 BASE_EXPORT static bool IsHandlingSendEvent(); |
319 #endif // !defined(OS_IOS) | 329 #endif // !defined(OS_IOS) |
320 | 330 |
321 private: | 331 private: |
322 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | 332 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
323 }; | 333 }; |
324 | 334 |
325 } // namespace base | 335 } // namespace base |
326 | 336 |
327 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ | 337 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ |
OLD | NEW |