OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Implementation based on sample code from | |
6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. | |
7 | |
8 #include "base/system_monitor/system_monitor.h" | |
9 | |
10 #include <IOKit/pwr_mgt/IOPMLib.h> | |
11 #include <IOKit/IOMessage.h> | |
12 | |
13 namespace base { | |
14 | |
15 namespace { | |
16 | |
17 io_connect_t g_system_power_io_port = 0; | |
18 | |
19 void SystemPowerEventCallback(void* system_monitor, | |
20 io_service_t service, | |
21 natural_t message_type, | |
22 void* message_argument) { | |
23 DCHECK(system_monitor); | |
24 SystemMonitor* sys_monitor = reinterpret_cast<SystemMonitor*>(system_monitor); | |
25 switch (message_type) { | |
26 case kIOMessageSystemWillSleep: | |
27 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | |
28 IOAllowPowerChange(g_system_power_io_port, | |
29 reinterpret_cast<int>(message_argument)); | |
30 break; | |
31 | |
32 case kIOMessageSystemWillPowerOn: | |
33 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
34 break; | |
35 } | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 void SystemMonitor::PlatformInit() { | |
41 DCHECK_EQ(g_system_power_io_port, 0u); | |
42 | |
43 // Notification port allocated by IORegisterForSystemPower. | |
44 | |
45 g_system_power_io_port = IORegisterForSystemPower( | |
46 this, ¬ification_port_ref_, SystemPowerEventCallback, | |
47 ¬ifier_object_); | |
48 DCHECK_NE(g_system_power_io_port, 0u); | |
49 | |
50 // Add the notification port to the application runloop | |
51 CFRunLoopAddSource(CFRunLoopGetCurrent(), | |
52 IONotificationPortGetRunLoopSource(notification_port_ref_), | |
53 kCFRunLoopCommonModes); | |
54 } | |
55 | |
56 void SystemMonitor::PlatformDestroy() { | |
57 DCHECK_NE(g_system_power_io_port, 0u); | |
58 | |
59 // Remove the sleep notification port from the application runloop | |
60 CFRunLoopRemoveSource( | |
61 CFRunLoopGetCurrent(), | |
62 IONotificationPortGetRunLoopSource(notification_port_ref_), | |
63 kCFRunLoopCommonModes); | |
64 | |
65 // Deregister for system sleep notifications | |
66 IODeregisterForSystemPower(¬ifier_object_); | |
67 | |
68 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, | |
69 // so we close it here. | |
70 IOServiceClose(g_system_power_io_port); | |
71 | |
72 g_system_power_io_port = 0; | |
73 | |
74 // Destroy the notification port allocated by IORegisterForSystemPower. | |
75 IONotificationPortDestroy(notification_port_ref_); | |
76 } | |
77 | |
78 } // namespace base | |
OLD | NEW |