| 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 19 matching lines...) Expand all Loading... |
| 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 | 36 |
| 37 #if !defined(__OBJC__) | 37 #if !defined(__OBJC__) |
| 38 class NSAutoreleasePool; | 38 class NSAutoreleasePool; |
| 39 #else // !defined(__OBJC__) | 39 #else // !defined(__OBJC__) |
| 40 #if defined(OS_IOS) |
| 41 #import <Foundation/Foundation.h> |
| 42 #else |
| 40 #import <AppKit/AppKit.h> | 43 #import <AppKit/AppKit.h> |
| 41 | 44 |
| 42 // Clients must subclass NSApplication and implement this protocol if they use | 45 // Clients must subclass NSApplication and implement this protocol if they use |
| 43 // MessagePumpMac. | 46 // MessagePumpMac. |
| 44 @protocol CrAppProtocol | 47 @protocol CrAppProtocol |
| 45 // Must return true if -[NSApplication sendEvent:] is currently on the stack. | 48 // Must return true if -[NSApplication sendEvent:] is currently on the stack. |
| 46 // See the comment for |CreateAutoreleasePool()| in the cc file for why this is | 49 // See the comment for |CreateAutoreleasePool()| in the cc file for why this is |
| 47 // necessary. | 50 // necessary. |
| 48 - (BOOL)isHandlingSendEvent; | 51 - (BOOL)isHandlingSendEvent; |
| 49 @end | 52 @end |
| 53 #endif // !defined(OS_IOS) |
| 50 #endif // !defined(__OBJC__) | 54 #endif // !defined(__OBJC__) |
| 51 | 55 |
| 52 namespace base { | 56 namespace base { |
| 53 | 57 |
| 58 class RunLoop; |
| 54 class TimeTicks; | 59 class TimeTicks; |
| 55 | 60 |
| 56 class MessagePumpCFRunLoopBase : public MessagePump { | 61 class MessagePumpCFRunLoopBase : public MessagePump { |
| 57 // Needs access to CreateAutoreleasePool. | 62 // Needs access to CreateAutoreleasePool. |
| 58 friend class MessagePumpScopedAutoreleasePool; | 63 friend class MessagePumpScopedAutoreleasePool; |
| 59 public: | 64 public: |
| 60 MessagePumpCFRunLoopBase(); | 65 MessagePumpCFRunLoopBase(); |
| 61 | 66 |
| 62 // 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 |
| 63 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. | 68 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. |
| 64 // This arrangement is used because MessagePumpCFRunLoopBase needs to set | 69 // This arrangement is used because MessagePumpCFRunLoopBase needs to set |
| 65 // 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. |
| 66 virtual void Run(Delegate* delegate) OVERRIDE; | 71 virtual void Run(Delegate* delegate) OVERRIDE; |
| 67 virtual void DoRun(Delegate* delegate) = 0; | 72 virtual void DoRun(Delegate* delegate) = 0; |
| 68 | 73 |
| 69 virtual void ScheduleWork() OVERRIDE; | 74 virtual void ScheduleWork() OVERRIDE; |
| 70 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; | 75 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; |
| 71 | 76 |
| 72 protected: | 77 protected: |
| 73 virtual ~MessagePumpCFRunLoopBase(); | 78 virtual ~MessagePumpCFRunLoopBase(); |
| 74 | 79 |
| 75 // Accessors for private data members to be used by subclasses. | 80 // Accessors for private data members to be used by subclasses. |
| 76 CFRunLoopRef run_loop() const { return run_loop_; } | 81 CFRunLoopRef run_loop() const { return run_loop_; } |
| 77 int nesting_level() const { return nesting_level_; } | 82 int nesting_level() const { return nesting_level_; } |
| 78 int run_nesting_level() const { return run_nesting_level_; } | 83 int run_nesting_level() const { return run_nesting_level_; } |
| 79 | 84 |
| 85 // Sets this pump's delegate. Signals the appropriate sources if |
| 86 // |delegateless_work_| is true. |delegate| can be NULL. |
| 87 void SetDelegate(Delegate* delegate); |
| 88 |
| 80 // Return an autorelease pool to wrap around any work being performed. | 89 // Return an autorelease pool to wrap around any work being performed. |
| 81 // In some cases, CreateAutoreleasePool may return nil intentionally to | 90 // In some cases, CreateAutoreleasePool may return nil intentionally to |
| 82 // preventing an autorelease pool from being created, allowing any | 91 // preventing an autorelease pool from being created, allowing any |
| 83 // objects autoreleased by work to fall into the current autorelease pool. | 92 // objects autoreleased by work to fall into the current autorelease pool. |
| 84 virtual NSAutoreleasePool* CreateAutoreleasePool(); | 93 virtual NSAutoreleasePool* CreateAutoreleasePool(); |
| 85 | 94 |
| 86 private: | 95 private: |
| 87 // Timer callback scheduled by ScheduleDelayedWork. This does not do any | 96 // Timer callback scheduled by ScheduleDelayedWork. This does not do any |
| 88 // work, but it signals work_source_ so that delayed work can be performed | 97 // work, but it signals work_source_ so that delayed work can be performed |
| 89 // within the appropriate priority constraints. | 98 // within the appropriate priority constraints. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 // 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 |
| 224 // 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. |
| 225 CFRunLoopSourceRef quit_source_; | 234 CFRunLoopSourceRef quit_source_; |
| 226 | 235 |
| 227 // False after Quit is called. | 236 // False after Quit is called. |
| 228 bool keep_running_; | 237 bool keep_running_; |
| 229 | 238 |
| 230 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); | 239 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); |
| 231 }; | 240 }; |
| 232 | 241 |
| 242 #if defined(OS_IOS) |
| 243 // 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 |
| 245 // directly, so calling Run() or Quit() are errors. |
| 246 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase { |
| 247 public: |
| 248 MessagePumpUIApplication(); |
| 249 virtual void DoRun(Delegate* delegate) OVERRIDE; |
| 250 virtual void Quit() OVERRIDE; |
| 251 |
| 252 // 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()|. |
| 254 virtual void Attach(Delegate* delegate); |
| 255 |
| 256 protected: |
| 257 virtual ~MessagePumpUIApplication(); |
| 258 |
| 259 private: |
| 260 base::RunLoop* run_loop_; |
| 261 |
| 262 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication); |
| 263 }; |
| 264 |
| 265 #else |
| 266 |
| 233 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { | 267 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { |
| 234 public: | 268 public: |
| 235 MessagePumpNSApplication(); | 269 MessagePumpNSApplication(); |
| 236 | 270 |
| 237 virtual void DoRun(Delegate* delegate) OVERRIDE; | 271 virtual void DoRun(Delegate* delegate) OVERRIDE; |
| 238 virtual void Quit() OVERRIDE; | 272 virtual void Quit() OVERRIDE; |
| 239 | 273 |
| 240 protected: | 274 protected: |
| 241 virtual ~MessagePumpNSApplication(); | 275 virtual ~MessagePumpNSApplication(); |
| 242 | 276 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 260 protected: | 294 protected: |
| 261 virtual ~MessagePumpCrApplication() {} | 295 virtual ~MessagePumpCrApplication() {} |
| 262 | 296 |
| 263 // Returns nil if NSApp is currently in the middle of calling | 297 // Returns nil if NSApp is currently in the middle of calling |
| 264 // -sendEvent. Requires NSApp implementing CrAppProtocol. | 298 // -sendEvent. Requires NSApp implementing CrAppProtocol. |
| 265 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; | 299 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; |
| 266 | 300 |
| 267 private: | 301 private: |
| 268 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); | 302 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); |
| 269 }; | 303 }; |
| 304 #endif // !defined(OS_IOS) |
| 270 | 305 |
| 271 class MessagePumpMac { | 306 class MessagePumpMac { |
| 272 public: | 307 public: |
| 273 // If not on the main thread, returns a new instance of | 308 // If not on the main thread, returns a new instance of |
| 274 // MessagePumpNSRunLoop. | 309 // MessagePumpNSRunLoop. |
| 275 // | 310 // |
| 276 // On the main thread, if NSApp exists and conforms to | 311 // On the main thread, if NSApp exists and conforms to |
| 277 // CrAppProtocol, creates an instances of MessagePumpCrApplication. | 312 // CrAppProtocol, creates an instances of MessagePumpCrApplication. |
| 278 // | 313 // |
| 279 // Otherwise creates an instance of MessagePumpNSApplication using a | 314 // Otherwise creates an instance of MessagePumpNSApplication using a |
| 280 // default NSApplication. | 315 // default NSApplication. |
| 281 static MessagePump* Create(); | 316 static MessagePump* Create(); |
| 282 | 317 |
| 318 #if !defined(OS_IOS) |
| 283 // If a pump is created before the required CrAppProtocol is | 319 // If a pump is created before the required CrAppProtocol is |
| 284 // created, the wrong MessagePump subclass could be used. | 320 // created, the wrong MessagePump subclass could be used. |
| 285 // UsingCrApp() returns false if the message pump was created before | 321 // UsingCrApp() returns false if the message pump was created before |
| 286 // NSApp was initialized, or if NSApp does not implement | 322 // NSApp was initialized, or if NSApp does not implement |
| 287 // CrAppProtocol. NSApp must be initialized before calling. | 323 // CrAppProtocol. NSApp must be initialized before calling. |
| 288 BASE_EXPORT static bool UsingCrApp(); | 324 BASE_EXPORT static bool UsingCrApp(); |
| 289 | 325 |
| 290 // Wrapper to query -[NSApp isHandlingSendEvent] from C++ code. | 326 // Wrapper to query -[NSApp isHandlingSendEvent] from C++ code. |
| 291 // Requires NSApp to implement CrAppProtocol. | 327 // Requires NSApp to implement CrAppProtocol. |
| 292 BASE_EXPORT static bool IsHandlingSendEvent(); | 328 BASE_EXPORT static bool IsHandlingSendEvent(); |
| 329 #endif // !defined(OS_IOS) |
| 293 | 330 |
| 294 private: | 331 private: |
| 295 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); | 332 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); |
| 296 }; | 333 }; |
| 297 | 334 |
| 298 } // namespace base | 335 } // namespace base |
| 299 | 336 |
| 300 #endif // BASE_MESSAGE_PUMP_MAC_H_ | 337 #endif // BASE_MESSAGE_PUMP_MAC_H_ |
| OLD | NEW |