OLD | NEW |
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 "content/browser/battery_status/battery_status_manager.h" | 5 #include "content/browser/battery_status/battery_status_manager.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <IOKit/ps/IOPowerSources.h> | 8 #include <IOKit/ps/IOPowerSources.h> |
9 #include <IOKit/ps/IOPSKeys.h> | 9 #include <IOKit/ps/IOPSKeys.h> |
| 10 #include <vector> |
10 | 11 |
11 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/metrics/histogram.h" |
14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
16 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" | 18 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; | 24 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; |
23 | 25 |
(...skipping 21 matching lines...) Expand all Loading... |
45 | 47 |
46 return boolean ? CFBooleanGetValue(boolean) : default_value; | 48 return boolean ? CFBooleanGetValue(boolean) : default_value; |
47 } | 49 } |
48 | 50 |
49 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) { | 51 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) { |
50 if (!string1 || !string2) | 52 if (!string1 || !string2) |
51 return false; | 53 return false; |
52 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; | 54 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; |
53 } | 55 } |
54 | 56 |
| 57 void UpdateNumberBatteriesHistogram(int count) { |
| 58 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 59 "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6); |
| 60 } |
| 61 |
55 void FetchBatteryStatus(CFDictionaryRef description, | 62 void FetchBatteryStatus(CFDictionaryRef description, |
56 blink::WebBatteryStatus& status) { | 63 blink::WebBatteryStatus& status) { |
57 CFStringRef current_state = | 64 CFStringRef current_state = |
58 base::mac::GetValueFromDictionary<CFStringRef>(description, | 65 base::mac::GetValueFromDictionary<CFStringRef>(description, |
59 CFSTR(kIOPSPowerSourceStateKey)); | 66 CFSTR(kIOPSPowerSourceStateKey)); |
60 | 67 |
61 bool on_battery_power = | 68 bool on_battery_power = |
62 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); | 69 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); |
63 bool is_charging = | 70 bool is_charging = |
64 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); | 71 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 SInt64 discharging_time = | 108 SInt64 discharging_time = |
102 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1); | 109 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1); |
103 if (discharging_time != -1) { | 110 if (discharging_time != -1) { |
104 status.dischargingTime = | 111 status.dischargingTime = |
105 base::TimeDelta::FromMinutes(discharging_time).InSeconds(); | 112 base::TimeDelta::FromMinutes(discharging_time).InSeconds(); |
106 } | 113 } |
107 } | 114 } |
108 } | 115 } |
109 } | 116 } |
110 | 117 |
111 void OnBatteryStatusChanged(const BatteryCallback& callback) { | 118 std::vector<blink::WebBatteryStatus> GetInternalBatteriesStates() { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 119 std::vector<blink::WebBatteryStatus> internal_sources; |
113 blink::WebBatteryStatus status; | 120 |
114 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); | 121 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); |
115 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( | 122 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( |
116 IOPSCopyPowerSourcesList(info)); | 123 IOPSCopyPowerSourcesList(info)); |
117 CFIndex count = CFArrayGetCount(power_sources_list); | 124 CFIndex count = CFArrayGetCount(power_sources_list); |
118 | 125 |
119 bool internal_source_found = false; | |
120 | |
121 for (CFIndex i = 0; i < count; ++i) { | 126 for (CFIndex i = 0; i < count; ++i) { |
122 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, | 127 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, |
123 CFArrayGetValueAtIndex(power_sources_list, i)); | 128 CFArrayGetValueAtIndex(power_sources_list, i)); |
124 | 129 |
125 if (!description) | 130 if (!description) |
126 continue; | 131 continue; |
127 | 132 |
128 CFStringRef transport_type = | 133 CFStringRef transport_type = |
129 base::mac::GetValueFromDictionary<CFStringRef>(description, | 134 base::mac::GetValueFromDictionary<CFStringRef>(description, |
130 CFSTR(kIOPSTransportTypeKey)); | 135 CFSTR(kIOPSTransportTypeKey)); |
131 | 136 |
132 bool internal_source = | 137 bool internal_source = |
133 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); | 138 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); |
134 bool source_present = | 139 bool source_present = |
135 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); | 140 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); |
136 | 141 |
137 if (internal_source && source_present) { | 142 if (internal_source && source_present) { |
138 // TODO(timvolodine): implement the case when there are multiple internal | 143 blink::WebBatteryStatus status; |
139 // sources, e.g. when multiple batteries are present. Currently this will | |
140 // fail a DCHECK. | |
141 DCHECK(!internal_source_found); | |
142 FetchBatteryStatus(description, status); | 144 FetchBatteryStatus(description, status); |
143 internal_source_found = true; | 145 internal_sources.push_back(status); |
144 } | 146 } |
145 } | 147 } |
146 | 148 |
147 callback.Run(status); | 149 return internal_sources; |
| 150 } |
| 151 |
| 152 void OnBatteryStatusChanged(const BatteryCallback& callback) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 154 |
| 155 std::vector<blink::WebBatteryStatus> batteries(GetInternalBatteriesStates()); |
| 156 |
| 157 if (batteries.empty()) { |
| 158 callback.Run(blink::WebBatteryStatus()); |
| 159 return; |
| 160 } |
| 161 |
| 162 // TODO(timvolodine): implement the case when there are multiple internal |
| 163 // sources, e.g. when multiple batteries are present. Currently this will |
| 164 // fail a DCHECK. |
| 165 DCHECK(batteries.size() == 1); |
| 166 callback.Run(batteries.front()); |
148 } | 167 } |
149 | 168 |
150 class BatteryStatusObserver | 169 class BatteryStatusObserver |
151 : public base::RefCountedThreadSafe<BatteryStatusObserver> { | 170 : public base::RefCountedThreadSafe<BatteryStatusObserver> { |
152 public: | 171 public: |
153 explicit BatteryStatusObserver(const BatteryCallback& callback) | 172 explicit BatteryStatusObserver(const BatteryCallback& callback) |
154 : callback_(callback) {} | 173 : callback_(callback) {} |
155 | 174 |
156 void Start() { | 175 void Start() { |
157 // Need to start on a thread with UI-type message loop for | 176 // Need to start on a thread with UI-type message loop for |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (!notifier_run_loop_source_) { | 212 if (!notifier_run_loop_source_) { |
194 LOG(ERROR) << "Failed to create battery status notification run loop"; | 213 LOG(ERROR) << "Failed to create battery status notification run loop"; |
195 // Make sure to execute to callback with the default values. | 214 // Make sure to execute to callback with the default values. |
196 callback_.Run(blink::WebBatteryStatus()); | 215 callback_.Run(blink::WebBatteryStatus()); |
197 return; | 216 return; |
198 } | 217 } |
199 | 218 |
200 OnBatteryStatusChangedUI(static_cast<void*>(&callback_)); | 219 OnBatteryStatusChangedUI(static_cast<void*>(&callback_)); |
201 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, | 220 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, |
202 kCFRunLoopDefaultMode); | 221 kCFRunLoopDefaultMode); |
| 222 UpdateNumberBatteriesHistogram(GetInternalBatteriesStates().size()); |
203 } | 223 } |
204 | 224 |
205 void StopOnUI() { | 225 void StopOnUI() { |
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
207 | 227 |
208 if (!notifier_run_loop_source_) | 228 if (!notifier_run_loop_source_) |
209 return; | 229 return; |
210 | 230 |
211 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, | 231 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, |
212 kCFRunLoopDefaultMode); | 232 kCFRunLoopDefaultMode); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 } // end namespace | 279 } // end namespace |
260 | 280 |
261 // static | 281 // static |
262 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 282 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
263 const BatteryStatusService::BatteryUpdateCallback& callback) { | 283 const BatteryStatusService::BatteryUpdateCallback& callback) { |
264 return scoped_ptr<BatteryStatusManager>( | 284 return scoped_ptr<BatteryStatusManager>( |
265 new BatteryStatusManagerMac(callback)); | 285 new BatteryStatusManagerMac(callback)); |
266 } | 286 } |
267 | 287 |
268 } // namespace content | 288 } // namespace content |
OLD | NEW |