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
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/system_monitor/system_monitor.h"
10 #include <IOKit/pwr_mgt/IOPMLib.h>
11 #include <IOKit/IOMessage.h>
17 io_connect_t g_system_power_io_port = 0;
18 IONotificationPortRef g_notification_port_ref = 0;
19 io_object_t g_notifier_object = 0;
21 void SystemPowerEventCallback(void*,
23 natural_t message_type,
24 void* message_argument) {
25 SystemMonitor* sys_monitor = SystemMonitor::Get();
27 switch (message_type) {
28 case kIOMessageSystemWillSleep:
29 sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
30 IOAllowPowerChange(g_system_power_io_port,
31 reinterpret_cast<intptr_t>(message_argument));
34 case kIOMessageSystemWillPowerOn:
35 sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
42 // 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
44 // allocated at sandbox initialization time, before there's a runloop.
45 // See crbug.com/83783 .
48 void SystemMonitor::AllocateSystemIOPorts() {
49 DCHECK_EQ(g_system_power_io_port, 0u);
51 // Notification port allocated by IORegisterForSystemPower.
53 g_system_power_io_port = IORegisterForSystemPower(
54 NULL, &g_notification_port_ref, SystemPowerEventCallback,
57 DCHECK_NE(g_system_power_io_port, 0u);
60 void SystemMonitor::PlatformInit() {
61 // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor
63 DCHECK_NE(g_system_power_io_port, 0u);
64 if (g_system_power_io_port == 0)
67 // Add the notification port to the application runloop
69 CFRunLoopGetCurrent(),
70 IONotificationPortGetRunLoopSource(g_notification_port_ref),
71 kCFRunLoopCommonModes);
74 void SystemMonitor::PlatformDestroy() {
75 DCHECK_NE(g_system_power_io_port, 0u);
76 if (g_system_power_io_port == 0)
79 // Remove the sleep notification port from the application runloop
80 CFRunLoopRemoveSource(
81 CFRunLoopGetCurrent(),
82 IONotificationPortGetRunLoopSource(g_notification_port_ref),
83 kCFRunLoopCommonModes);
85 // Deregister for system sleep notifications
86 IODeregisterForSystemPower(&g_notifier_object);
88 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService,
89 // so we close it here.
90 IOServiceClose(g_system_power_io_port);
92 g_system_power_io_port = 0;
94 // Destroy the notification port allocated by IORegisterForSystemPower.
95 IONotificationPortDestroy(g_notification_port_ref);