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

Side by Side Diff: services/device/battery/battery_status_manager_mac.cc

Issue 2818673003: [DeviceService] Expose battery monitoring solely via the Device Service (Closed)
Patch Set: Rebase only Created 3 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "device/battery/battery_status_manager.h" 5 #include "services/device/battery/battery_status_manager.h"
6 6
7 #include <CoreFoundation/CoreFoundation.h> 7 #include <CoreFoundation/CoreFoundation.h>
8 #include <IOKit/ps/IOPSKeys.h> 8 #include <IOKit/ps/IOPSKeys.h>
9 #include <IOKit/ps/IOPowerSources.h> 9 #include <IOKit/ps/IOPowerSources.h>
10 10
11 #include <memory> 11 #include <memory>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/mac/foundation_util.h" 14 #include "base/mac/foundation_util.h"
15 #include "base/mac/scoped_cftyperef.h" 15 #include "base/mac/scoped_cftyperef.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 return boolean ? CFBooleanGetValue(boolean) : default_value; 48 return boolean ? CFBooleanGetValue(boolean) : default_value;
49 } 49 }
50 50
51 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) { 51 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) {
52 if (!string1 || !string2) 52 if (!string1 || !string2)
53 return false; 53 return false;
54 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; 54 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo;
55 } 55 }
56 56
57 void UpdateNumberBatteriesHistogram(int count) { 57 void UpdateNumberBatteriesHistogram(int count) {
58 UMA_HISTOGRAM_CUSTOM_COUNTS( 58 UMA_HISTOGRAM_CUSTOM_COUNTS("BatteryStatus.NumberBatteriesMac", count, 1, 5,
59 "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6); 59 6);
60 } 60 }
61 61
62 void FetchBatteryStatus(CFDictionaryRef description, 62 void FetchBatteryStatus(CFDictionaryRef description,
63 mojom::BatteryStatus* status) { 63 mojom::BatteryStatus* status) {
64 CFStringRef current_state = 64 CFStringRef current_state = base::mac::GetValueFromDictionary<CFStringRef>(
65 base::mac::GetValueFromDictionary<CFStringRef>(description, 65 description, CFSTR(kIOPSPowerSourceStateKey));
66 CFSTR(kIOPSPowerSourceStateKey));
67 66
68 bool on_battery_power = 67 bool on_battery_power =
69 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); 68 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue));
70 bool is_charging = 69 bool is_charging =
71 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); 70 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true);
72 bool is_charged = 71 bool is_charged =
73 GetValueAsBoolean(description, CFSTR(kIOPSIsChargedKey), false); 72 GetValueAsBoolean(description, CFSTR(kIOPSIsChargedKey), false);
74 73
75 status->charging = !on_battery_power || is_charging; 74 status->charging = !on_battery_power || is_charging;
76 75
77 SInt64 current_capacity = 76 SInt64 current_capacity =
78 GetValueAsSInt64(description, CFSTR(kIOPSCurrentCapacityKey), -1); 77 GetValueAsSInt64(description, CFSTR(kIOPSCurrentCapacityKey), -1);
79 SInt64 max_capacity = 78 SInt64 max_capacity =
80 GetValueAsSInt64(description, CFSTR(kIOPSMaxCapacityKey), -1); 79 GetValueAsSInt64(description, CFSTR(kIOPSMaxCapacityKey), -1);
81 80
82 // Set level if it is available and valid. Otherwise leave the default value, 81 // Set level if it is available and valid. Otherwise leave the default value,
83 // which is 1. 82 // which is 1.
84 if (current_capacity != -1 && max_capacity != -1 && 83 if (current_capacity != -1 && max_capacity != -1 &&
85 current_capacity <= max_capacity && max_capacity != 0) { 84 current_capacity <= max_capacity && max_capacity != 0) {
86 status->level = current_capacity / static_cast<double>(max_capacity); 85 status->level = current_capacity / static_cast<double>(max_capacity);
87 } 86 }
88 87
89 if (is_charging) { 88 if (is_charging) {
90 SInt64 charging_time = 89 SInt64 charging_time =
91 GetValueAsSInt64(description, CFSTR(kIOPSTimeToFullChargeKey), -1); 90 GetValueAsSInt64(description, CFSTR(kIOPSTimeToFullChargeKey), -1);
92 91
93 // Battery is charging: set the charging time if it's available, otherwise 92 // Battery is charging: set the charging time if it's available, otherwise
94 // set to +infinity. 93 // set to +infinity.
95 status->charging_time = charging_time != -1 94 status->charging_time =
96 ? base::TimeDelta::FromMinutes(charging_time).InSeconds() 95 charging_time != -1
97 : std::numeric_limits<double>::infinity(); 96 ? base::TimeDelta::FromMinutes(charging_time).InSeconds()
97 : std::numeric_limits<double>::infinity();
98 } else { 98 } else {
99 // Battery is not charging. 99 // Battery is not charging.
100 // Set chargingTime to +infinity if the battery is not charged. Otherwise 100 // Set chargingTime to +infinity if the battery is not charged. Otherwise
101 // leave the default value, which is 0. 101 // leave the default value, which is 0.
102 if (!is_charged) 102 if (!is_charged)
103 status->charging_time = std::numeric_limits<double>::infinity(); 103 status->charging_time = std::numeric_limits<double>::infinity();
104 104
105 // Set dischargingTime if it's available and valid, i.e. when on battery 105 // Set dischargingTime if it's available and valid, i.e. when on battery
106 // power. Otherwise leave the default value, which is +infinity. 106 // power. Otherwise leave the default value, which is +infinity.
107 if (on_battery_power) { 107 if (on_battery_power) {
108 SInt64 discharging_time = 108 SInt64 discharging_time =
109 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1); 109 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1);
110 if (discharging_time != -1) { 110 if (discharging_time != -1) {
111 status->discharging_time = 111 status->discharging_time =
112 base::TimeDelta::FromMinutes(discharging_time).InSeconds(); 112 base::TimeDelta::FromMinutes(discharging_time).InSeconds();
113 } 113 }
114 } 114 }
115 } 115 }
116 } 116 }
117 117
118 std::vector<mojom::BatteryStatus> GetInternalBatteriesStates() { 118 std::vector<mojom::BatteryStatus> GetInternalBatteriesStates() {
119 std::vector<mojom::BatteryStatus> internal_sources; 119 std::vector<mojom::BatteryStatus> internal_sources;
120 120
121 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); 121 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo());
122 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( 122 base::ScopedCFTypeRef<CFArrayRef> power_sources_list(
123 IOPSCopyPowerSourcesList(info)); 123 IOPSCopyPowerSourcesList(info));
124 CFIndex count = CFArrayGetCount(power_sources_list); 124 CFIndex count = CFArrayGetCount(power_sources_list);
125 125
126 for (CFIndex i = 0; i < count; ++i) { 126 for (CFIndex i = 0; i < count; ++i) {
127 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, 127 CFDictionaryRef description = IOPSGetPowerSourceDescription(
128 CFArrayGetValueAtIndex(power_sources_list, i)); 128 info, CFArrayGetValueAtIndex(power_sources_list, i));
129 129
130 if (!description) 130 if (!description)
131 continue; 131 continue;
132 132
133 CFStringRef transport_type = 133 CFStringRef transport_type = base::mac::GetValueFromDictionary<CFStringRef>(
134 base::mac::GetValueFromDictionary<CFStringRef>(description, 134 description, CFSTR(kIOPSTransportTypeKey));
135 CFSTR(kIOPSTransportTypeKey));
136 135
137 bool internal_source = 136 bool internal_source =
138 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); 137 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType));
139 bool source_present = 138 bool source_present =
140 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); 139 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false);
141 140
142 if (internal_source && source_present) { 141 if (internal_source && source_present) {
143 mojom::BatteryStatus status; 142 mojom::BatteryStatus status;
144 FetchBatteryStatus(description, &status); 143 FetchBatteryStatus(description, &status);
145 internal_sources.push_back(status); 144 internal_sources.push_back(status);
(...skipping 22 matching lines...) Expand all
168 public: 167 public:
169 explicit BatteryStatusObserver(const BatteryCallback& callback) 168 explicit BatteryStatusObserver(const BatteryCallback& callback)
170 : callback_(callback) {} 169 : callback_(callback) {}
171 170
172 ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); } 171 ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); }
173 172
174 void Start() { 173 void Start() {
175 if (notifier_run_loop_source_) 174 if (notifier_run_loop_source_)
176 return; 175 return;
177 176
178 notifier_run_loop_source_.reset( 177 notifier_run_loop_source_.reset(IOPSNotificationCreateRunLoopSource(
179 IOPSNotificationCreateRunLoopSource(CallOnBatteryStatusChanged, 178 CallOnBatteryStatusChanged, static_cast<void*>(&callback_)));
180 static_cast<void*>(&callback_)));
181 if (!notifier_run_loop_source_) { 179 if (!notifier_run_loop_source_) {
182 LOG(ERROR) << "Failed to create battery status notification run loop"; 180 LOG(ERROR) << "Failed to create battery status notification run loop";
183 // Make sure to execute to callback with the default values. 181 // Make sure to execute to callback with the default values.
184 callback_.Run(mojom::BatteryStatus()); 182 callback_.Run(mojom::BatteryStatus());
185 return; 183 return;
186 } 184 }
187 185
188 CallOnBatteryStatusChanged(static_cast<void*>(&callback_)); 186 CallOnBatteryStatusChanged(static_cast<void*>(&callback_));
189 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, 187 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_,
190 kCFRunLoopDefaultMode); 188 kCFRunLoopDefaultMode);
(...skipping 26 matching lines...) Expand all
217 : notifier_(new BatteryStatusObserver(callback)) {} 215 : notifier_(new BatteryStatusObserver(callback)) {}
218 216
219 ~BatteryStatusManagerMac() override { notifier_->Stop(); } 217 ~BatteryStatusManagerMac() override { notifier_->Stop(); }
220 218
221 // BatteryStatusManager: 219 // BatteryStatusManager:
222 bool StartListeningBatteryChange() override { 220 bool StartListeningBatteryChange() override {
223 notifier_->Start(); 221 notifier_->Start();
224 return true; 222 return true;
225 } 223 }
226 224
227 void StopListeningBatteryChange() override { 225 void StopListeningBatteryChange() override { notifier_->Stop(); }
228 notifier_->Stop();
229 }
230 226
231 private: 227 private:
232 std::unique_ptr<BatteryStatusObserver> notifier_; 228 std::unique_ptr<BatteryStatusObserver> notifier_;
233 229
234 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerMac); 230 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerMac);
235 }; 231 };
236 232
237 } // end namespace 233 } // end namespace
238 234
239 // static 235 // static
240 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( 236 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create(
241 const BatteryStatusService::BatteryUpdateCallback& callback) { 237 const BatteryStatusService::BatteryUpdateCallback& callback) {
242 return std::unique_ptr<BatteryStatusManager>( 238 return std::unique_ptr<BatteryStatusManager>(
243 new BatteryStatusManagerMac(callback)); 239 new BatteryStatusManagerMac(callback));
244 } 240 }
245 241
246 } // namespace device 242 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698