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