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