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.h"
10 #include <IOKit/pwr_mgt/IOPMLib.h>
11 #include <IOKit/IOMessage.h>
15 void ProcessPowerEventHelper(PowerMonitor::PowerEvent event) {
16 DCHECK(PowerMonitor::Get());
17 if (PowerMonitor::Get())
18 PowerMonitor::Get()->ProcessPowerEvent(event);
23 io_connect_t g_system_power_io_port = 0;
24 IONotificationPortRef g_notification_port_ref = 0;
25 io_object_t g_notifier_object = 0;
27 void SystemPowerEventCallback(void*,
29 natural_t message_type,
30 void* message_argument) {
31 switch (message_type) {
32 case kIOMessageSystemWillSleep:
33 ProcessPowerEventHelper(base::PowerMonitor::SUSPEND_EVENT);
34 IOAllowPowerChange(g_system_power_io_port,
35 reinterpret_cast<intptr_t>(message_argument));
38 case kIOMessageSystemWillPowerOn:
39 ProcessPowerEventHelper(PowerMonitor::RESUME_EVENT);
46 // 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
48 // allocated at sandbox initialization time, before there's a runloop.
49 // See crbug.com/83783 .
52 void PowerMonitor::AllocateSystemIOPorts() {
53 DCHECK_EQ(g_system_power_io_port, 0u);
55 // Notification port allocated by IORegisterForSystemPower.
56 g_system_power_io_port = IORegisterForSystemPower(
57 NULL, &g_notification_port_ref, SystemPowerEventCallback,
60 DCHECK_NE(g_system_power_io_port, 0u);
63 void PowerMonitor::PlatformInit() {
64 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor
66 DCHECK_NE(g_system_power_io_port, 0u);
67 if (g_system_power_io_port == 0)
70 // Add the notification port to the application runloop
72 CFRunLoopGetCurrent(),
73 IONotificationPortGetRunLoopSource(g_notification_port_ref),
74 kCFRunLoopCommonModes);
77 void PowerMonitor::PlatformDestroy() {
78 DCHECK_NE(g_system_power_io_port, 0u);
79 if (g_system_power_io_port == 0)
82 // Remove the sleep notification port from the application runloop
83 CFRunLoopRemoveSource(
84 CFRunLoopGetCurrent(),
85 IONotificationPortGetRunLoopSource(g_notification_port_ref),
86 kCFRunLoopCommonModes);
88 // Deregister for system sleep notifications
89 IODeregisterForSystemPower(&g_notifier_object);
91 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService,
92 // so we close it here.
93 IOServiceClose(g_system_power_io_port);
95 g_system_power_io_port = 0;
97 // Destroy the notification port allocated by IORegisterForSystemPower.
98 IONotificationPortDestroy(g_notification_port_ref);