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