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