Chromium Code Reviews| 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 // A helper function as a friend function of PowerMonitor. | |
| 16 void ProcessPowerEventHelper(PowerMonitor::PowerEvent power_event) { | |
| 17 PowerMonitor* power_monitor = PowerMonitor::GetInstance(); | |
| 18 power_monitor->ProcessPowerEvent(power_event); | |
| 19 } | |
| 20 | |
| 15 namespace { | 21 namespace { |
| 16 | 22 |
| 17 io_connect_t g_system_power_io_port = 0; | 23 io_connect_t g_system_power_io_port = 0; |
| 18 IONotificationPortRef g_notification_port_ref = 0; | 24 IONotificationPortRef g_notification_port_ref = 0; |
| 19 io_object_t g_notifier_object = 0; | 25 io_object_t g_notifier_object = 0; |
| 20 | 26 |
| 21 void SystemPowerEventCallback(void*, | 27 void SystemPowerEventCallback(void*, |
| 22 io_service_t service, | 28 io_service_t service, |
| 23 natural_t message_type, | 29 natural_t message_type, |
| 24 void* message_argument) { | 30 void* message_argument) { |
| 25 SystemMonitor* sys_monitor = SystemMonitor::Get(); | |
| 26 DCHECK(sys_monitor); | |
| 27 switch (message_type) { | 31 switch (message_type) { |
| 28 case kIOMessageSystemWillSleep: | 32 case kIOMessageSystemWillSleep: |
| 29 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 33 ProcessPowerEventHelper(PowerMonitor::SUSPEND_EVENT); |
| 30 IOAllowPowerChange(g_system_power_io_port, | 34 IOAllowPowerChange(g_system_power_io_port, |
| 31 reinterpret_cast<intptr_t>(message_argument)); | 35 reinterpret_cast<intptr_t>(message_argument)); |
| 32 break; | 36 break; |
| 33 | 37 |
| 34 case kIOMessageSystemWillPowerOn: | 38 case kIOMessageSystemWillPowerOn: |
| 35 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 39 ProcessPowerEventHelper(PowerMonitor::RESUME_EVENT); |
| 36 break; | 40 break; |
| 37 } | 41 } |
| 38 } | 42 } |
| 39 | 43 |
| 40 } // namespace | 44 } // namespace |
| 41 | 45 |
| 42 // The reason we can't include this code in the constructor is because | 46 // 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 | 47 // PlatformInit() requires an active runloop and the IO port needs to be |
| 44 // allocated at sandbox initialization time, before there's a runloop. | 48 // allocated at sandbox initialization time, before there's a runloop. |
| 45 // See crbug.com/83783 . | 49 // See crbug.com/83783 . |
| 46 | 50 |
| 47 // static | 51 // static |
| 48 void SystemMonitor::AllocateSystemIOPorts() { | 52 void PowerMonitor::AllocateSystemIOPorts() { |
| 49 DCHECK_EQ(g_system_power_io_port, 0u); | 53 DCHECK_EQ(g_system_power_io_port, 0u); |
| 50 | 54 |
| 51 // Notification port allocated by IORegisterForSystemPower. | 55 // Notification port allocated by IORegisterForSystemPower. |
| 52 | 56 |
| 53 g_system_power_io_port = IORegisterForSystemPower( | 57 g_system_power_io_port = IORegisterForSystemPower( |
| 54 NULL, &g_notification_port_ref, SystemPowerEventCallback, | 58 NULL, &g_notification_port_ref, SystemPowerEventCallback, |
| 55 &g_notifier_object); | 59 &g_notifier_object); |
| 56 | 60 |
| 57 DCHECK_NE(g_system_power_io_port, 0u); | 61 DCHECK_NE(g_system_power_io_port, 0u); |
| 58 } | 62 } |
| 59 | 63 |
| 60 void SystemMonitor::PlatformInit() { | 64 void PowerMonitor::PlatformInit() { |
| 61 // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor | 65 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor |
| 62 // object. | 66 // object. |
| 63 DCHECK_NE(g_system_power_io_port, 0u); | 67 DCHECK_NE(g_system_power_io_port, 0u); |
| 64 if (g_system_power_io_port == 0) | 68 if (g_system_power_io_port == 0) |
| 65 return; | 69 return; |
|
willchan no longer on Chromium
2012/10/12 18:17:15
This makes me minorly nervous, especially now that
vandebo (ex-Chrome)
2012/10/12 22:12:30
SGTM
Hongbo Min
2012/10/13 06:36:07
Done.
Hongbo Min
2012/10/13 08:22:15
Willchan, I try to use CHECK_NE instead, but some
vandebo (ex-Chrome)
2012/10/15 23:41:25
It's important to get this stuff right because it
| |
| 66 | 70 |
| 67 // Add the notification port to the application runloop | 71 // Add the notification port to the application runloop |
| 68 CFRunLoopAddSource( | 72 CFRunLoopAddSource( |
| 69 CFRunLoopGetCurrent(), | 73 CFRunLoopGetCurrent(), |
| 70 IONotificationPortGetRunLoopSource(g_notification_port_ref), | 74 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
| 71 kCFRunLoopCommonModes); | 75 kCFRunLoopCommonModes); |
| 72 } | 76 } |
| 73 | 77 |
| 74 void SystemMonitor::PlatformDestroy() { | 78 |
| 79 void PowerMonitor::PlatformDestroy() { | |
| 75 DCHECK_NE(g_system_power_io_port, 0u); | 80 DCHECK_NE(g_system_power_io_port, 0u); |
| 76 if (g_system_power_io_port == 0) | 81 if (g_system_power_io_port == 0) |
| 77 return; | 82 return; |
| 78 | 83 |
| 79 // Remove the sleep notification port from the application runloop | 84 // Remove the sleep notification port from the application runloop |
| 80 CFRunLoopRemoveSource( | 85 CFRunLoopRemoveSource( |
| 81 CFRunLoopGetCurrent(), | 86 CFRunLoopGetCurrent(), |
| 82 IONotificationPortGetRunLoopSource(g_notification_port_ref), | 87 IONotificationPortGetRunLoopSource(g_notification_port_ref), |
| 83 kCFRunLoopCommonModes); | 88 kCFRunLoopCommonModes); |
| 84 | 89 |
| 85 // Deregister for system sleep notifications | 90 // Deregister for system sleep notifications |
| 86 IODeregisterForSystemPower(&g_notifier_object); | 91 IODeregisterForSystemPower(&g_notifier_object); |
| 87 | 92 |
| 88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, | 93 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, |
| 89 // so we close it here. | 94 // so we close it here. |
| 90 IOServiceClose(g_system_power_io_port); | 95 IOServiceClose(g_system_power_io_port); |
| 91 | 96 |
| 92 g_system_power_io_port = 0; | 97 g_system_power_io_port = 0; |
| 93 | 98 |
| 94 // Destroy the notification port allocated by IORegisterForSystemPower. | 99 // Destroy the notification port allocated by IORegisterForSystemPower. |
| 95 IONotificationPortDestroy(g_notification_port_ref); | 100 IONotificationPortDestroy(g_notification_port_ref); |
| 96 } | 101 } |
| 97 | 102 |
| 98 } // namespace base | 103 } // namespace base |
| OLD | NEW |