| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 #include "base/message_pump_mac.h" | 5 #include "base/message_pump_mac.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 #include <IOKit/IOMessage.h> | 9 #include <IOKit/IOMessage.h> |
| 10 #include <IOKit/pwr_mgt/IOPMLib.h> | 10 #include <IOKit/pwr_mgt/IOPMLib.h> |
| 11 | 11 |
| 12 #include <limits> | 12 #include <limits> |
| 13 | 13 |
| 14 #import "base/chrome_application_mac.h" | |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/time.h" | 15 #include "base/time.h" |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 void NoOp(void* info) { | 19 void NoOp(void* info) { |
| 21 } | 20 } |
| 22 | 21 |
| 23 const CFTimeInterval kCFTimeIntervalMax = | 22 const CFTimeInterval kCFTimeIntervalMax = |
| 24 std::numeric_limits<CFTimeInterval>::max(); | 23 std::numeric_limits<CFTimeInterval>::max(); |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 } | 665 } |
| 667 | 666 |
| 668 MessagePumpNSApplication::MessagePumpNSApplication() | 667 MessagePumpNSApplication::MessagePumpNSApplication() |
| 669 : keep_running_(true), | 668 : keep_running_(true), |
| 670 running_own_loop_(false) { | 669 running_own_loop_(false) { |
| 671 } | 670 } |
| 672 | 671 |
| 673 void MessagePumpNSApplication::DoRun(Delegate* delegate) { | 672 void MessagePumpNSApplication::DoRun(Delegate* delegate) { |
| 674 bool last_running_own_loop_ = running_own_loop_; | 673 bool last_running_own_loop_ = running_own_loop_; |
| 675 | 674 |
| 676 // TODO(dmaclach): Get rid of this gratuitous sharedApplication. | |
| 677 // Tests should be setting up their applications on their own. | |
| 678 [CrApplication sharedApplication]; | |
| 679 | |
| 680 if (![NSApp isRunning]) { | 675 if (![NSApp isRunning]) { |
| 681 running_own_loop_ = false; | 676 running_own_loop_ = false; |
| 682 // NSApplication manages autorelease pools itself when run this way. | 677 // NSApplication manages autorelease pools itself when run this way. |
| 683 [NSApp run]; | 678 [NSApp run]; |
| 684 } else { | 679 } else { |
| 685 running_own_loop_ = true; | 680 running_own_loop_ = true; |
| 686 NSDate* distant_future = [NSDate distantFuture]; | 681 NSDate* distant_future = [NSDate distantFuture]; |
| 687 while (keep_running_) { | 682 while (keep_running_) { |
| 688 MessagePumpScopedAutoreleasePool autorelease_pool(this); | 683 MessagePumpScopedAutoreleasePool autorelease_pool(this); |
| 689 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask | 684 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 // controller, which autoreleased itself in frame #0, to release itself, and | 737 // controller, which autoreleased itself in frame #0, to release itself, and |
| 743 // possibly free itself. Unfortunately this window controller controls the | 738 // possibly free itself. Unfortunately this window controller controls the |
| 744 // window in frame #5. When the stack is unwound to frame #5, the window would | 739 // window in frame #5. When the stack is unwound to frame #5, the window would |
| 745 // no longer exists and crashes may occur. Apple gets around this by never | 740 // no longer exists and crashes may occur. Apple gets around this by never |
| 746 // releasing the pool it creates in frame #4, and letting frame #7 clean it up | 741 // releasing the pool it creates in frame #4, and letting frame #7 clean it up |
| 747 // when it cleans up the pool that wraps frame #7. When an autorelease pool is | 742 // when it cleans up the pool that wraps frame #7. When an autorelease pool is |
| 748 // released it releases all other pools that were created after it on the | 743 // released it releases all other pools that were created after it on the |
| 749 // autorelease pool stack. | 744 // autorelease pool stack. |
| 750 // | 745 // |
| 751 // CrApplication is responsible for setting handlingSendEvent to true just | 746 // CrApplication is responsible for setting handlingSendEvent to true just |
| 752 // before it sends the event throught the event handling mechanism, and | 747 // before it sends the event through the event handling mechanism, and |
| 753 // returning it to its previous value once the event has been sent. | 748 // returning it to its previous value once the event has been sent. |
| 754 NSAutoreleasePool* MessagePumpNSApplication::CreateAutoreleasePool() { | 749 NSAutoreleasePool* MessagePumpNSApplication::CreateAutoreleasePool() { |
| 755 NSAutoreleasePool* pool = nil; | 750 NSAutoreleasePool* pool = nil; |
| 756 DCHECK([NSApp isKindOfClass:[CrApplication class]]); | 751 DCHECK([NSApp conformsToProtocol:@protocol(CrAppProtocol)]); |
| 757 if (![static_cast<CrApplication*>(NSApp) isHandlingSendEvent]) { | 752 if (![NSApp isHandlingSendEvent]) { |
| 758 pool = MessagePumpCFRunLoopBase::CreateAutoreleasePool(); | 753 pool = MessagePumpCFRunLoopBase::CreateAutoreleasePool(); |
| 759 } | 754 } |
| 760 return pool; | 755 return pool; |
| 761 } | 756 } |
| 762 | 757 |
| 763 // static | 758 // static |
| 764 MessagePump* MessagePumpMac::Create() { | 759 MessagePump* MessagePumpMac::Create() { |
| 765 if ([NSThread isMainThread]) { | 760 if ([NSThread isMainThread]) { |
| 766 return new MessagePumpNSApplication; | 761 return new MessagePumpNSApplication; |
| 767 } | 762 } |
| 768 | 763 |
| 769 return new MessagePumpNSRunLoop; | 764 return new MessagePumpNSRunLoop; |
| 770 } | 765 } |
| 771 | 766 |
| 772 } // namespace base | 767 } // namespace base |
| OLD | NEW |