forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_monitor_device_source_mac.mm
131 lines (104 loc) · 4.29 KB
/
power_monitor_device_source_mac.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Implementation based on sample code from
// http://developer.apple.com/library/mac/#qa/qa1340/_index.html.
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_source.h"
#include <IOKit/IOMessage.h>
#include <IOKit/ps/IOPSKeys.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
namespace base {
void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) {
PowerMonitorSource::ProcessPowerEvent(event);
}
bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo());
base::ScopedCFTypeRef<CFArrayRef> power_sources_list(
IOPSCopyPowerSourcesList(info));
const CFIndex count = CFArrayGetCount(power_sources_list);
for (CFIndex i = 0; i < count; ++i) {
const CFDictionaryRef description = IOPSGetPowerSourceDescription(
info, CFArrayGetValueAtIndex(power_sources_list, i));
if (!description)
continue;
CFStringRef current_state = base::mac::GetValueFromDictionary<CFStringRef>(
description, CFSTR(kIOPSPowerSourceStateKey));
if (!current_state)
continue;
// We only report "on battery power" if no source is on AC power.
if (CFStringCompare(current_state, CFSTR(kIOPSBatteryPowerValue), 0) !=
kCFCompareEqualTo) {
return false;
}
}
return true;
}
namespace {
void BatteryEventCallback(void*) {
ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT);
}
} // namespace
void PowerMonitorDeviceSource::PlatformInit() {
power_manager_port_ = IORegisterForSystemPower(
this,
mac::ScopedIONotificationPortRef::Receiver(notification_port_).get(),
&SystemPowerEventCallback, ¬ifier_);
DCHECK_NE(power_manager_port_, IO_OBJECT_NULL);
// Add the sleep/wake notification event source to the runloop.
CFRunLoopAddSource(
CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notification_port_.get()),
kCFRunLoopCommonModes);
// Create and add the power-source-change event source to the runloop.
power_source_run_loop_source_.reset(
IOPSNotificationCreateRunLoopSource(&BatteryEventCallback, nullptr));
// Verify that the source was created. This may fail if the sandbox does not
// permit the process to access the underlying system service. See
// https://crbug.com/897557 for an example of such a configuration bug.
DCHECK(power_source_run_loop_source_);
CFRunLoopAddSource(CFRunLoopGetCurrent(), power_source_run_loop_source_,
kCFRunLoopDefaultMode);
}
void PowerMonitorDeviceSource::PlatformDestroy() {
CFRunLoopRemoveSource(
CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notification_port_.get()),
kCFRunLoopCommonModes);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
power_source_run_loop_source_.get(),
kCFRunLoopDefaultMode);
// Deregister for system power notifications.
IODeregisterForSystemPower(¬ifier_);
// Close the connection to the IOPMrootDomain that was opened in
// PlatformInit().
IOServiceClose(power_manager_port_);
power_manager_port_ = IO_OBJECT_NULL;
}
void PowerMonitorDeviceSource::SystemPowerEventCallback(
void* refcon,
io_service_t service,
natural_t message_type,
void* message_argument) {
auto* thiz = static_cast<PowerMonitorDeviceSource*>(refcon);
switch (message_type) {
// If this message is not handled the system may delay sleep for 30 seconds.
case kIOMessageCanSystemSleep:
IOAllowPowerChange(thiz->power_manager_port_,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillSleep:
ProcessPowerEventHelper(PowerMonitorSource::SUSPEND_EVENT);
IOAllowPowerChange(thiz->power_manager_port_,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillPowerOn:
ProcessPowerEventHelper(PowerMonitorSource::RESUME_EVENT);
break;
}
}
} // namespace base