| 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 #import "base/message_loop/message_pump_mac.h" | 5 #import "base/message_loop/message_pump_mac.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | |
| 8 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 9 | 8 |
| 10 #include <limits> | 9 #include <limits> |
| 11 | 10 |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/mac/call_with_eh_frame.h" | 12 #include "base/mac/call_with_eh_frame.h" |
| 14 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/macros.h" | 14 #include "base/macros.h" |
| 16 #include "base/message_loop/timer_slack.h" | 15 #include "base/message_loop/timer_slack.h" |
| 17 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 64 |
| 66 const CFTimeInterval kCFTimeIntervalMax = | 65 const CFTimeInterval kCFTimeIntervalMax = |
| 67 std::numeric_limits<CFTimeInterval>::max(); | 66 std::numeric_limits<CFTimeInterval>::max(); |
| 68 | 67 |
| 69 #if !defined(OS_IOS) | 68 #if !defined(OS_IOS) |
| 70 // Set to true if MessagePumpMac::Create() is called before NSApp is | 69 // Set to true if MessagePumpMac::Create() is called before NSApp is |
| 71 // initialized. Only accessed from the main thread. | 70 // initialized. Only accessed from the main thread. |
| 72 bool g_not_using_cr_app = false; | 71 bool g_not_using_cr_app = false; |
| 73 #endif | 72 #endif |
| 74 | 73 |
| 75 // Call through to CFRunLoopTimerSetTolerance(), which is only available on | |
| 76 // OS X 10.9. | |
| 77 void SetTimerTolerance(CFRunLoopTimerRef timer, CFTimeInterval tolerance) { | |
| 78 typedef void (*CFRunLoopTimerSetTolerancePtr)(CFRunLoopTimerRef timer, | |
| 79 CFTimeInterval tolerance); | |
| 80 | |
| 81 static CFRunLoopTimerSetTolerancePtr settimertolerance_function_ptr; | |
| 82 | |
| 83 static dispatch_once_t get_timer_tolerance_function_ptr_once; | |
| 84 dispatch_once(&get_timer_tolerance_function_ptr_once, ^{ | |
| 85 NSBundle* bundle =[NSBundle | |
| 86 bundleWithPath:@"/System/Library/Frameworks/CoreFoundation.framework"]; | |
| 87 const char* path = [[bundle executablePath] fileSystemRepresentation]; | |
| 88 CHECK(path); | |
| 89 void* library_handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL); | |
| 90 CHECK(library_handle) << dlerror(); | |
| 91 settimertolerance_function_ptr = | |
| 92 reinterpret_cast<CFRunLoopTimerSetTolerancePtr>( | |
| 93 dlsym(library_handle, "CFRunLoopTimerSetTolerance")); | |
| 94 | |
| 95 dlclose(library_handle); | |
| 96 }); | |
| 97 | |
| 98 if (settimertolerance_function_ptr) | |
| 99 settimertolerance_function_ptr(timer, tolerance); | |
| 100 } | |
| 101 | |
| 102 } // namespace | 74 } // namespace |
| 103 | 75 |
| 104 // static | 76 // static |
| 105 const CFStringRef kMessageLoopExclusiveRunLoopMode = | 77 const CFStringRef kMessageLoopExclusiveRunLoopMode = |
| 106 CFSTR("kMessageLoopExclusiveRunLoopMode"); | 78 CFSTR("kMessageLoopExclusiveRunLoopMode"); |
| 107 | 79 |
| 108 // A scoper for autorelease pools created from message pump run loops. | 80 // A scoper for autorelease pools created from message pump run loops. |
| 109 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare | 81 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare |
| 110 // case where an autorelease pool needs to be passed in. | 82 // case where an autorelease pool needs to be passed in. |
| 111 class MessagePumpScopedAutoreleasePool { | 83 class MessagePumpScopedAutoreleasePool { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 CFRunLoopWakeUp(run_loop_); | 238 CFRunLoopWakeUp(run_loop_); |
| 267 } | 239 } |
| 268 | 240 |
| 269 // Must be called on the run loop thread. | 241 // Must be called on the run loop thread. |
| 270 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( | 242 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( |
| 271 const TimeTicks& delayed_work_time) { | 243 const TimeTicks& delayed_work_time) { |
| 272 TimeDelta delta = delayed_work_time - TimeTicks::Now(); | 244 TimeDelta delta = delayed_work_time - TimeTicks::Now(); |
| 273 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); | 245 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); |
| 274 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); | 246 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); |
| 275 if (timer_slack_ == TIMER_SLACK_MAXIMUM) { | 247 if (timer_slack_ == TIMER_SLACK_MAXIMUM) { |
| 276 SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5); | 248 CFRunLoopTimerSetTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5); |
| 277 } else { | 249 } else { |
| 278 SetTimerTolerance(delayed_work_timer_, 0); | 250 CFRunLoopTimerSetTolerance(delayed_work_timer_, 0); |
| 279 } | 251 } |
| 280 } | 252 } |
| 281 | 253 |
| 282 void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) { | 254 void MessagePumpCFRunLoopBase::SetTimerSlack(TimerSlack timer_slack) { |
| 283 timer_slack_ = timer_slack; | 255 timer_slack_ = timer_slack; |
| 284 } | 256 } |
| 285 | 257 |
| 286 // Called from the run loop. | 258 // Called from the run loop. |
| 287 // static | 259 // static |
| 288 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, | 260 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 [NSApplication sharedApplication]; | 756 [NSApplication sharedApplication]; |
| 785 g_not_using_cr_app = true; | 757 g_not_using_cr_app = true; |
| 786 return new MessagePumpNSApplication; | 758 return new MessagePumpNSApplication; |
| 787 #endif | 759 #endif |
| 788 } | 760 } |
| 789 | 761 |
| 790 return new MessagePumpNSRunLoop; | 762 return new MessagePumpNSRunLoop; |
| 791 } | 763 } |
| 792 | 764 |
| 793 } // namespace base | 765 } // namespace base |
| OLD | NEW |