Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: base/power_monitor/power_monitor_mac.mm

Issue 17074009: Created multi-process-friendly PowerMonitor interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing windows unit test errors Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/power_monitor/power_monitor_ios.mm ('k') | base/power_monitor/power_monitor_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.h"
9
10 #include <IOKit/pwr_mgt/IOPMLib.h>
11 #include <IOKit/IOMessage.h>
12
13 namespace base {
14
15 void ProcessPowerEventHelper(PowerMonitor::PowerEvent event) {
16 DCHECK(PowerMonitor::Get());
17 if (PowerMonitor::Get())
18 PowerMonitor::Get()->ProcessPowerEvent(event);
19 }
20
21 namespace {
22
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;
26
27 void SystemPowerEventCallback(void*,
28 io_service_t service,
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));
36 break;
37
38 case kIOMessageSystemWillPowerOn:
39 ProcessPowerEventHelper(PowerMonitor::RESUME_EVENT);
40 break;
41 }
42 }
43
44 } // namespace
45
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 .
50
51 // static
52 void PowerMonitor::AllocateSystemIOPorts() {
53 DCHECK_EQ(g_system_power_io_port, 0u);
54
55 // Notification port allocated by IORegisterForSystemPower.
56 g_system_power_io_port = IORegisterForSystemPower(
57 NULL, &g_notification_port_ref, SystemPowerEventCallback,
58 &g_notifier_object);
59
60 DCHECK_NE(g_system_power_io_port, 0u);
61 }
62
63 void PowerMonitor::PlatformInit() {
64 // Need to call AllocateSystemIOPorts() before creating a PowerMonitor
65 // object.
66 DCHECK_NE(g_system_power_io_port, 0u);
67 if (g_system_power_io_port == 0)
68 return;
69
70 // Add the notification port to the application runloop
71 CFRunLoopAddSource(
72 CFRunLoopGetCurrent(),
73 IONotificationPortGetRunLoopSource(g_notification_port_ref),
74 kCFRunLoopCommonModes);
75 }
76
77 void PowerMonitor::PlatformDestroy() {
78 DCHECK_NE(g_system_power_io_port, 0u);
79 if (g_system_power_io_port == 0)
80 return;
81
82 // Remove the sleep notification port from the application runloop
83 CFRunLoopRemoveSource(
84 CFRunLoopGetCurrent(),
85 IONotificationPortGetRunLoopSource(g_notification_port_ref),
86 kCFRunLoopCommonModes);
87
88 // Deregister for system sleep notifications
89 IODeregisterForSystemPower(&g_notifier_object);
90
91 // IORegisterForSystemPower implicitly opens the Root Power Domain IOService,
92 // so we close it here.
93 IOServiceClose(g_system_power_io_port);
94
95 g_system_power_io_port = 0;
96
97 // Destroy the notification port allocated by IORegisterForSystemPower.
98 IONotificationPortDestroy(g_notification_port_ref);
99 }
100
101 } // namespace base
OLDNEW
« no previous file with comments | « base/power_monitor/power_monitor_ios.mm ('k') | base/power_monitor/power_monitor_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698