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 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
| 9 #include <dlfcn.h> |
9 #include <limits> | 10 #include <limits> |
10 #include <stack> | 11 #include <stack> |
11 | 12 |
12 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/message_loop/timer_slack.h" |
16 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
19 | 21 |
20 #if !defined(OS_IOS) | 22 #if !defined(OS_IOS) |
21 #import <AppKit/AppKit.h> | 23 #import <AppKit/AppKit.h> |
22 #endif // !defined(OS_IOS) | 24 #endif // !defined(OS_IOS) |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 void NoOp(void* info) { | 28 void NoOp(void* info) { |
27 } | 29 } |
28 | 30 |
29 const CFTimeInterval kCFTimeIntervalMax = | 31 const CFTimeInterval kCFTimeIntervalMax = |
30 std::numeric_limits<CFTimeInterval>::max(); | 32 std::numeric_limits<CFTimeInterval>::max(); |
31 | 33 |
32 #if !defined(OS_IOS) | 34 #if !defined(OS_IOS) |
33 // Set to true if MessagePumpMac::Create() is called before NSApp is | 35 // Set to true if MessagePumpMac::Create() is called before NSApp is |
34 // initialized. Only accessed from the main thread. | 36 // initialized. Only accessed from the main thread. |
35 bool g_not_using_cr_app = false; | 37 bool g_not_using_cr_app = false; |
36 #endif | 38 #endif |
37 | 39 |
| 40 // Call through to CFRunLoopTimerSetTolerance(), which is only available on |
| 41 // OS X 10.9. |
| 42 void SetTimerTolerance(CFRunLoopTimerRef timer, CFTimeInterval tolerance) { |
| 43 typedef void (*CFRunLoopTimerSetTolerancePtr)(CFRunLoopTimerRef timer, |
| 44 CFTimeInterval tolerance); |
| 45 |
| 46 static CFRunLoopTimerSetTolerancePtr settimertolerance_function_ptr = NULL; |
| 47 static bool function_lookup_error = false; |
| 48 |
| 49 if (function_lookup_error) |
| 50 return; |
| 51 |
| 52 if (!settimertolerance_function_ptr) { |
| 53 settimertolerance_function_ptr = |
| 54 reinterpret_cast<CFRunLoopTimerSetTolerancePtr>( |
| 55 dlsym(RTLD_DEFAULT, "CFRunLoopTimerSetTolerance")); |
| 56 if (!settimertolerance_function_ptr) { |
| 57 function_lookup_error = true; |
| 58 return; |
| 59 } |
| 60 } |
| 61 |
| 62 settimertolerance_function_ptr(timer, tolerance); |
| 63 } |
| 64 |
38 } // namespace | 65 } // namespace |
39 | 66 |
40 namespace base { | 67 namespace base { |
41 | 68 |
42 // A scoper for autorelease pools created from message pump run loops. | 69 // A scoper for autorelease pools created from message pump run loops. |
43 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare | 70 // Avoids dirtying up the ScopedNSAutoreleasePool interface for the rare |
44 // case where an autorelease pool needs to be passed in. | 71 // case where an autorelease pool needs to be passed in. |
45 class MessagePumpScopedAutoreleasePool { | 72 class MessagePumpScopedAutoreleasePool { |
46 public: | 73 public: |
47 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : | 74 explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) : |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 CFRunLoopSourceSignal(work_source_); | 458 CFRunLoopSourceSignal(work_source_); |
432 CFRunLoopWakeUp(run_loop_); | 459 CFRunLoopWakeUp(run_loop_); |
433 } | 460 } |
434 | 461 |
435 // Must be called on the run loop thread. | 462 // Must be called on the run loop thread. |
436 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( | 463 void MessagePumpCFRunLoopBase::ScheduleDelayedWork( |
437 const TimeTicks& delayed_work_time) { | 464 const TimeTicks& delayed_work_time) { |
438 TimeDelta delta = delayed_work_time - TimeTicks::Now(); | 465 TimeDelta delta = delayed_work_time - TimeTicks::Now(); |
439 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); | 466 delayed_work_fire_time_ = CFAbsoluteTimeGetCurrent() + delta.InSecondsF(); |
440 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); | 467 CFRunLoopTimerSetNextFireDate(delayed_work_timer_, delayed_work_fire_time_); |
| 468 if (GetTimerSlack() == TIMER_SLACK_MAXIMUM) { |
| 469 SetTimerTolerance(delayed_work_timer_, delta.InSecondsF() * 0.5); |
| 470 } else { |
| 471 SetTimerTolerance(delayed_work_timer_, 0); |
| 472 } |
441 } | 473 } |
442 | 474 |
443 // Called from the run loop. | 475 // Called from the run loop. |
444 // static | 476 // static |
445 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, | 477 void MessagePumpCFRunLoopBase::RunDelayedWorkTimer(CFRunLoopTimerRef timer, |
446 void* info) { | 478 void* info) { |
447 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info); | 479 MessagePumpCFRunLoopBase* self = static_cast<MessagePumpCFRunLoopBase*>(info); |
448 | 480 |
449 // The timer won't fire again until it's reset. | 481 // The timer won't fire again until it's reset. |
450 self->delayed_work_fire_time_ = kCFTimeIntervalMax; | 482 self->delayed_work_fire_time_ = kCFTimeIntervalMax; |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
954 [NSApplication sharedApplication]; | 986 [NSApplication sharedApplication]; |
955 g_not_using_cr_app = true; | 987 g_not_using_cr_app = true; |
956 return new MessagePumpNSApplication; | 988 return new MessagePumpNSApplication; |
957 #endif | 989 #endif |
958 } | 990 } |
959 | 991 |
960 return new MessagePumpNSRunLoop; | 992 return new MessagePumpNSRunLoop; |
961 } | 993 } |
962 | 994 |
963 } // namespace base | 995 } // namespace base |
OLD | NEW |