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 // 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/power_monitor/power_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; | 18 IONotificationPortRef g_notification_port_ref = 0; |
19 io_object_t g_notifier_object = 0; | 19 io_object_t g_notifier_object = 0; |
20 PowerMonitor::Signaler* g_power_signaler = NULL; | |
20 | 21 |
21 void SystemPowerEventCallback(void*, | 22 void SystemPowerEventCallback(void*, |
22 io_service_t service, | 23 io_service_t service, |
23 natural_t message_type, | 24 natural_t message_type, |
24 void* message_argument) { | 25 void* message_argument) { |
25 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 26 PowerMonitor* power_monitor = PowerMonitor::Get(); |
26 DCHECK(sys_monitor); | 27 CHECK(power_monitor); |
28 if (!g_power_signaler) | |
Hongbo Min
2012/10/31 13:11:38
g_power_signaler gets initialized here instead of
vandebo (ex-Chrome)
2012/10/31 17:30:34
Hmm, I think it's better to get it in PlatformInit
Hongbo Min
2012/11/01 09:26:24
Right now the PlatformInit/PlatformDestroy is avai
| |
29 g_power_signaler = power_monitor->GetSignalerOnce(); | |
30 | |
27 switch (message_type) { | 31 switch (message_type) { |
28 case kIOMessageSystemWillSleep: | 32 case kIOMessageSystemWillSleep: |
29 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 33 if (g_power_signaler) |
34 g_power_signaler->ProcessPowerEvent(base::PowerMonitor::SUSPEND_EVENT); | |
30 IOAllowPowerChange(g_system_power_io_port, | 35 IOAllowPowerChange(g_system_power_io_port, |
31 reinterpret_cast<intptr_t>(message_argument)); | 36 reinterpret_cast<intptr_t>(message_argument)); |
32 break; | 37 break; |
33 | 38 |
34 case kIOMessageSystemWillPowerOn: | 39 case kIOMessageSystemWillPowerOn: |
35 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 40 if (g_power_signaler) |
41 g_power_signaler->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | |
36 break; | 42 break; |
37 } | 43 } |
38 } | 44 } |
39 | 45 |
40 } // namespace | 46 } // namespace |
41 | 47 |
42 // The reason we can't include this code in the constructor is because | 48 // 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 | 49 // PlatformInit() requires an active runloop and the IO port needs to be |
44 // allocated at sandbox initialization time, before there's a runloop. | 50 // allocated at sandbox initialization time, before there's a runloop. |
45 // See crbug.com/83783 . | 51 // See crbug.com/83783 . |
46 | 52 |
47 // static | 53 // static |
48 void SystemMonitor::AllocateSystemIOPorts() { | 54 void PowerMonitor::AllocateSystemIOPorts() { |
49 DCHECK_EQ(g_system_power_io_port, 0u); | 55 DCHECK_EQ(g_system_power_io_port, 0u); |
56 if (g_system_power_io_port != 0u) | |
57 return; | |
50 | 58 |
51 // Notification port allocated by IORegisterForSystemPower. | 59 // Notification port allocated by IORegisterForSystemPower. |
52 | 60 |
53 g_system_power_io_port = IORegisterForSystemPower( | 61 g_system_power_io_port = IORegisterForSystemPower( |
54 NULL, &g_notification_port_ref, SystemPowerEventCallback, | 62 NULL, &g_notification_port_ref, SystemPowerEventCallback, |
55 &g_notifier_object); | 63 &g_notifier_object); |
56 | 64 |
57 DCHECK_NE(g_system_power_io_port, 0u); | 65 DCHECK_NE(g_system_power_io_port, 0u); |
58 } | 66 } |
59 | 67 |
60 void SystemMonitor::PlatformInit() { | 68 void PowerMonitor::PlatformInit() { |
61 // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor | 69 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor |
62 // object. | 70 // object. |
63 DCHECK_NE(g_system_power_io_port, 0u); | 71 CHECK_NE(g_system_power_io_port, 0u); |
64 if (g_system_power_io_port == 0) | 72 |
65 return; | |
66 | 73 |
67 // Add the notification port to the application runloop | 74 // Add the notification port to the application runloop |
68 CFRunLoopAddSource( | 75 CFRunLoopAddSource( |
69 CFRunLoopGetCurrent(), | 76 CFRunLoopGetCurrent(), |
70 IONotificationPortGetRunLoopSource(g_notification_port_ref), | 77 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
71 kCFRunLoopCommonModes); | 78 kCFRunLoopCommonModes); |
72 } | 79 } |
73 | 80 |
74 void SystemMonitor::PlatformDestroy() { | 81 |
82 void PowerMonitor::PlatformDestroy() { | |
75 DCHECK_NE(g_system_power_io_port, 0u); | 83 DCHECK_NE(g_system_power_io_port, 0u); |
84 | |
76 if (g_system_power_io_port == 0) | 85 if (g_system_power_io_port == 0) |
77 return; | 86 return; |
78 | 87 |
79 // Remove the sleep notification port from the application runloop | 88 // Remove the sleep notification port from the application runloop |
80 CFRunLoopRemoveSource( | 89 CFRunLoopRemoveSource( |
81 CFRunLoopGetCurrent(), | 90 CFRunLoopGetCurrent(), |
82 IONotificationPortGetRunLoopSource(g_notification_port_ref), | 91 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
83 kCFRunLoopCommonModes); | 92 kCFRunLoopCommonModes); |
84 | 93 |
85 // Deregister for system sleep notifications | 94 // Deregister for system sleep notifications |
86 IODeregisterForSystemPower(&g_notifier_object); | 95 IODeregisterForSystemPower(&g_notifier_object); |
87 | 96 |
88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, | 97 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, |
89 // so we close it here. | 98 // so we close it here. |
90 IOServiceClose(g_system_power_io_port); | 99 IOServiceClose(g_system_power_io_port); |
91 | 100 |
92 g_system_power_io_port = 0; | 101 g_system_power_io_port = 0; |
93 | 102 |
94 // Destroy the notification port allocated by IORegisterForSystemPower. | 103 // Destroy the notification port allocated by IORegisterForSystemPower. |
95 IONotificationPortDestroy(g_notification_port_ref); | 104 IONotificationPortDestroy(g_notification_port_ref); |
96 } | 105 } |
97 | 106 |
98 } // namespace base | 107 } // namespace base |
OLD | NEW |