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.
5 // Implementation based on sample code from
6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html.
8 #include "base/power_monitor/power_monitor_device_source.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_source.h"
13 #include <IOKit/IOMessage.h>
14 #include <IOKit/pwr_mgt/IOPMLib.h>
18 void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) {
19 PowerMonitorSource::ProcessPowerEvent(event);
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;
28 void SystemPowerEventCallback(void*,
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));
38 case kIOMessageSystemWillSleep:
39 ProcessPowerEventHelper(base::PowerMonitorSource::SUSPEND_EVENT);
40 IOAllowPowerChange(g_system_power_io_port,
41 reinterpret_cast<intptr_t>(message_argument));
44 case kIOMessageSystemWillPowerOn:
45 ProcessPowerEventHelper(PowerMonitorSource::RESUME_EVENT);
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 .
58 void PowerMonitorDeviceSource::AllocateSystemIOPorts() {
59 DCHECK_EQ(g_system_power_io_port, 0u);
61 // Notification port allocated by IORegisterForSystemPower.
62 g_system_power_io_port = IORegisterForSystemPower(
63 NULL, &g_notification_port_ref, SystemPowerEventCallback,
66 DCHECK_NE(g_system_power_io_port, 0u);
69 void PowerMonitorDeviceSource::PlatformInit() {
70 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor
72 DCHECK_NE(g_system_power_io_port, 0u);
73 if (g_system_power_io_port == 0)
76 // Add the notification port to the application runloop
78 CFRunLoopGetCurrent(),
79 IONotificationPortGetRunLoopSource(g_notification_port_ref),
80 kCFRunLoopCommonModes);
83 void PowerMonitorDeviceSource::PlatformDestroy() {
84 DCHECK_NE(g_system_power_io_port, 0u);
85 if (g_system_power_io_port == 0)
88 // Remove the sleep notification port from the application runloop
89 CFRunLoopRemoveSource(
90 CFRunLoopGetCurrent(),
91 IONotificationPortGetRunLoopSource(g_notification_port_ref),
92 kCFRunLoopCommonModes);
94 // Deregister for system sleep notifications
95 IODeregisterForSystemPower(&g_notifier_object);
97 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService,
98 // so we close it here.
99 IOServiceClose(g_system_power_io_port);
101 g_system_power_io_port = 0;
103 // Destroy the notification port allocated by IORegisterForSystemPower.
104 IONotificationPortDestroy(g_notification_port_ref);