OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Implementation based on sample code from | 5 // Implementation based on sample code from |
6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. | 6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. |
7 | 7 |
8 #include "base/system_monitor/system_monitor.h" | 8 #include "base/system_monitor/system_monitor.h" |
9 | 9 |
10 #include <IOKit/pwr_mgt/IOPMLib.h> | 10 #include <IOKit/pwr_mgt/IOPMLib.h> |
11 #include <IOKit/IOMessage.h> | 11 #include <IOKit/IOMessage.h> |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 io_connect_t g_system_power_io_port = 0; | 17 io_connect_t g_system_power_io_port = 0; |
| 18 IONotificationPortRef g_notification_port_ref = 0; |
| 19 io_object_t g_notifier_object = 0; |
18 | 20 |
19 void SystemPowerEventCallback(void* system_monitor, | 21 void SystemPowerEventCallback(void*, |
20 io_service_t service, | 22 io_service_t service, |
21 natural_t message_type, | 23 natural_t message_type, |
22 void* message_argument) { | 24 void* message_argument) { |
23 DCHECK(system_monitor); | 25 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
24 SystemMonitor* sys_monitor = reinterpret_cast<SystemMonitor*>(system_monitor); | 26 DCHECK(sys_monitor); |
25 switch (message_type) { | 27 switch (message_type) { |
26 case kIOMessageSystemWillSleep: | 28 case kIOMessageSystemWillSleep: |
27 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 29 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); |
28 IOAllowPowerChange(g_system_power_io_port, | 30 IOAllowPowerChange(g_system_power_io_port, |
29 reinterpret_cast<int>(message_argument)); | 31 reinterpret_cast<int>(message_argument)); |
30 break; | 32 break; |
31 | 33 |
32 case kIOMessageSystemWillPowerOn: | 34 case kIOMessageSystemWillPowerOn: |
33 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 35 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
34 break; | 36 break; |
35 } | 37 } |
36 } | 38 } |
37 | 39 |
38 } // namespace | 40 } // namespace |
39 | 41 |
40 void SystemMonitor::PlatformInit() { | 42 // The reason we can't include this code in the constructor is because |
| 43 // PlatformInit() requires an active runloop and the IO port needs to be |
| 44 // allocated at sandbox initialization time, before there's a runloop. |
| 45 // See crbug.com/83783 . |
| 46 |
| 47 // static |
| 48 void SystemMonitor::AllocateSystemIOPorts() { |
41 DCHECK_EQ(g_system_power_io_port, 0u); | 49 DCHECK_EQ(g_system_power_io_port, 0u); |
42 | 50 |
43 // Notification port allocated by IORegisterForSystemPower. | 51 // Notification port allocated by IORegisterForSystemPower. |
44 | 52 |
45 g_system_power_io_port = IORegisterForSystemPower( | 53 g_system_power_io_port = IORegisterForSystemPower( |
46 this, ¬ification_port_ref_, SystemPowerEventCallback, | 54 NULL, &g_notification_port_ref, SystemPowerEventCallback, |
47 ¬ifier_object_); | 55 &g_notifier_object); |
| 56 |
| 57 DCHECK_NE(g_system_power_io_port, 0u); |
| 58 } |
| 59 |
| 60 void SystemMonitor::PlatformInit() { |
| 61 // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor |
| 62 // object. |
48 DCHECK_NE(g_system_power_io_port, 0u); | 63 DCHECK_NE(g_system_power_io_port, 0u); |
49 if (g_system_power_io_port == 0) | 64 if (g_system_power_io_port == 0) |
50 return; | 65 return; |
51 | 66 |
52 // Add the notification port to the application runloop | 67 // Add the notification port to the application runloop |
53 CFRunLoopAddSource(CFRunLoopGetCurrent(), | 68 CFRunLoopAddSource( |
54 IONotificationPortGetRunLoopSource(notification_port_ref_), | 69 CFRunLoopGetCurrent(), |
55 kCFRunLoopCommonModes); | 70 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
| 71 kCFRunLoopCommonModes); |
56 } | 72 } |
57 | 73 |
58 void SystemMonitor::PlatformDestroy() { | 74 void SystemMonitor::PlatformDestroy() { |
59 DCHECK_NE(g_system_power_io_port, 0u); | 75 DCHECK_NE(g_system_power_io_port, 0u); |
60 if (g_system_power_io_port == 0) | 76 if (g_system_power_io_port == 0) |
61 return; | 77 return; |
62 | 78 |
63 // Remove the sleep notification port from the application runloop | 79 // Remove the sleep notification port from the application runloop |
64 CFRunLoopRemoveSource( | 80 CFRunLoopRemoveSource( |
65 CFRunLoopGetCurrent(), | 81 CFRunLoopGetCurrent(), |
66 IONotificationPortGetRunLoopSource(notification_port_ref_), | 82 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
67 kCFRunLoopCommonModes); | 83 kCFRunLoopCommonModes); |
68 | 84 |
69 // Deregister for system sleep notifications | 85 // Deregister for system sleep notifications |
70 IODeregisterForSystemPower(¬ifier_object_); | 86 IODeregisterForSystemPower(&g_notifier_object); |
71 | 87 |
72 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, | 88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, |
73 // so we close it here. | 89 // so we close it here. |
74 IOServiceClose(g_system_power_io_port); | 90 IOServiceClose(g_system_power_io_port); |
75 | 91 |
76 g_system_power_io_port = 0; | 92 g_system_power_io_port = 0; |
77 | 93 |
78 // Destroy the notification port allocated by IORegisterForSystemPower. | 94 // Destroy the notification port allocated by IORegisterForSystemPower. |
79 IONotificationPortDestroy(notification_port_ref_); | 95 IONotificationPortDestroy(g_notification_port_ref); |
80 } | 96 } |
81 | 97 |
82 } // namespace base | 98 } // namespace base |
OLD | NEW |