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