Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Implementation based on sample code from | 5 // Implementation based on sample code from |
| 6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. | 6 // http://developer.apple.com/library/mac/#qa/qa1340/_index.html. |
| 7 | 7 |
| 8 #include "base/power_monitor/power_monitor_device_source.h" | 8 #include "base/power_monitor/power_monitor_device_source.h" |
| 9 | 9 |
| 10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
| 11 #include "base/power_monitor/power_monitor_source.h" | 11 #include "base/power_monitor/power_monitor_source.h" |
| 12 | 12 |
| 13 #include <IOKit/IOMessage.h> | 13 #include <IOKit/IOMessage.h> |
| 14 #include <IOKit/ps/IOPSKeys.h> | |
| 15 #include <IOKit/ps/IOPowerSources.h> | |
| 14 #include <IOKit/pwr_mgt/IOPMLib.h> | 16 #include <IOKit/pwr_mgt/IOPMLib.h> |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 17 | 19 |
| 18 void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) { | 20 void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) { |
| 19 PowerMonitorSource::ProcessPowerEvent(event); | 21 PowerMonitorSource::ProcessPowerEvent(event); |
| 20 } | 22 } |
| 21 | 23 |
| 24 bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() { | |
|
Nico
2016/09/20 00:05:08
how often do we poll this? there's a "this changed
DaleCurtis
2016/09/20 00:15:13
Doesn't look like it's called very much anywhere.
Robert Sesek
2016/09/20 18:19:56
I do think we should use the IOPSNotificationCreat
DaleCurtis
2016/09/21 22:49:39
Done.
| |
| 25 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); | |
| 26 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( | |
| 27 IOPSCopyPowerSourcesList(info)); | |
| 28 | |
| 29 const CFIndex count = CFArrayGetCount(power_sources_list); | |
| 30 for (CFIndex i = 0; i < count; ++i) { | |
| 31 const CFDictionaryRef description = IOPSGetPowerSourceDescription( | |
| 32 info, CFArrayGetValueAtIndex(power_sources_list, i)); | |
| 33 if (!description) | |
| 34 continue; | |
| 35 | |
| 36 CFStringRef current_state = base::mac::GetValueFromDictionary<CFStringRef>( | |
| 37 description, CFSTR(kIOPSPowerSourceStateKey)); | |
| 38 | |
| 39 // We only report "on battery power" if no source is on AC power. | |
| 40 if (!CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue))) | |
| 41 return false; | |
| 42 } | |
| 43 | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 22 namespace { | 47 namespace { |
| 23 | 48 |
| 24 io_connect_t g_system_power_io_port = 0; | 49 io_connect_t g_system_power_io_port = 0; |
| 25 IONotificationPortRef g_notification_port_ref = 0; | 50 IONotificationPortRef g_notification_port_ref = 0; |
| 26 io_object_t g_notifier_object = 0; | 51 io_object_t g_notifier_object = 0; |
| 27 | 52 |
| 28 void SystemPowerEventCallback(void*, | 53 void SystemPowerEventCallback(void*, |
| 29 io_service_t service, | 54 io_service_t service, |
| 30 natural_t message_type, | 55 natural_t message_type, |
| 31 void* message_argument) { | 56 void* message_argument) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 // so we close it here. | 123 // so we close it here. |
| 99 IOServiceClose(g_system_power_io_port); | 124 IOServiceClose(g_system_power_io_port); |
| 100 | 125 |
| 101 g_system_power_io_port = 0; | 126 g_system_power_io_port = 0; |
| 102 | 127 |
| 103 // Destroy the notification port allocated by IORegisterForSystemPower. | 128 // Destroy the notification port allocated by IORegisterForSystemPower. |
| 104 IONotificationPortDestroy(g_notification_port_ref); | 129 IONotificationPortDestroy(g_notification_port_ref); |
| 105 } | 130 } |
| 106 | 131 |
| 107 } // namespace base | 132 } // namespace base |
| OLD | NEW |