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 | 10 |
11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/metrics/histogram.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" | 17 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; | 23 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; |
23 | 24 |
(...skipping 21 matching lines...) Expand all Loading... |
45 | 46 |
46 return boolean ? CFBooleanGetValue(boolean) : default_value; | 47 return boolean ? CFBooleanGetValue(boolean) : default_value; |
47 } | 48 } |
48 | 49 |
49 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) { | 50 bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) { |
50 if (!string1 || !string2) | 51 if (!string1 || !string2) |
51 return false; | 52 return false; |
52 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; | 53 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; |
53 } | 54 } |
54 | 55 |
| 56 void UpdateNumberBatteriesHistogram(int count) { |
| 57 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 58 "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6); |
| 59 } |
| 60 |
55 void FetchBatteryStatus(CFDictionaryRef description, | 61 void FetchBatteryStatus(CFDictionaryRef description, |
56 blink::WebBatteryStatus& status) { | 62 blink::WebBatteryStatus& status) { |
57 CFStringRef current_state = | 63 CFStringRef current_state = |
58 base::mac::GetValueFromDictionary<CFStringRef>(description, | 64 base::mac::GetValueFromDictionary<CFStringRef>(description, |
59 CFSTR(kIOPSPowerSourceStateKey)); | 65 CFSTR(kIOPSPowerSourceStateKey)); |
60 | 66 |
61 bool on_battery_power = | 67 bool on_battery_power = |
62 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); | 68 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); |
63 bool is_charging = | 69 bool is_charging = |
64 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); | 70 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 115 } |
110 | 116 |
111 void OnBatteryStatusChanged(const BatteryCallback& callback) { | 117 void OnBatteryStatusChanged(const BatteryCallback& callback) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
113 blink::WebBatteryStatus status; | 119 blink::WebBatteryStatus status; |
114 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); | 120 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); |
115 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( | 121 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( |
116 IOPSCopyPowerSourcesList(info)); | 122 IOPSCopyPowerSourcesList(info)); |
117 CFIndex count = CFArrayGetCount(power_sources_list); | 123 CFIndex count = CFArrayGetCount(power_sources_list); |
118 | 124 |
119 bool internal_source_found = false; | 125 int num_internal_sources = 0; |
120 | 126 |
121 for (CFIndex i = 0; i < count; ++i) { | 127 for (CFIndex i = 0; i < count; ++i) { |
122 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, | 128 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, |
123 CFArrayGetValueAtIndex(power_sources_list, i)); | 129 CFArrayGetValueAtIndex(power_sources_list, i)); |
124 | 130 |
125 if (!description) | 131 if (!description) |
126 continue; | 132 continue; |
127 | 133 |
128 CFStringRef transport_type = | 134 CFStringRef transport_type = |
129 base::mac::GetValueFromDictionary<CFStringRef>(description, | 135 base::mac::GetValueFromDictionary<CFStringRef>(description, |
130 CFSTR(kIOPSTransportTypeKey)); | 136 CFSTR(kIOPSTransportTypeKey)); |
131 | 137 |
132 bool internal_source = | 138 bool internal_source = |
133 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); | 139 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); |
134 bool source_present = | 140 bool source_present = |
135 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); | 141 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); |
136 | 142 |
137 if (internal_source && source_present) { | 143 if (internal_source && source_present) { |
138 // TODO(timvolodine): implement the case when there are multiple internal | 144 // TODO(timvolodine): implement the case when there are multiple internal |
139 // sources, e.g. when multiple batteries are present. Currently this will | 145 // sources, e.g. when multiple batteries are present. Currently this will |
140 // fail a DCHECK. | 146 // fail a DCHECK. |
141 DCHECK(!internal_source_found); | 147 DCHECK(num_internal_sources == 0); |
142 FetchBatteryStatus(description, status); | 148 FetchBatteryStatus(description, status); |
143 internal_source_found = true; | 149 num_internal_sources++; |
144 } | 150 } |
145 } | 151 } |
146 | 152 |
| 153 UpdateNumberBatteriesHistogram(num_internal_sources); |
147 callback.Run(status); | 154 callback.Run(status); |
148 } | 155 } |
149 | 156 |
150 class BatteryStatusObserver | 157 class BatteryStatusObserver |
151 : public base::RefCountedThreadSafe<BatteryStatusObserver> { | 158 : public base::RefCountedThreadSafe<BatteryStatusObserver> { |
152 public: | 159 public: |
153 explicit BatteryStatusObserver(const BatteryCallback& callback) | 160 explicit BatteryStatusObserver(const BatteryCallback& callback) |
154 : callback_(callback) {} | 161 : callback_(callback) {} |
155 | 162 |
156 void Start() { | 163 void Start() { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 } // end namespace | 266 } // end namespace |
260 | 267 |
261 // static | 268 // static |
262 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 269 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
263 const BatteryStatusService::BatteryUpdateCallback& callback) { | 270 const BatteryStatusService::BatteryUpdateCallback& callback) { |
264 return scoped_ptr<BatteryStatusManager>( | 271 return scoped_ptr<BatteryStatusManager>( |
265 new BatteryStatusManagerMac(callback)); | 272 new BatteryStatusManagerMac(callback)); |
266 } | 273 } |
267 | 274 |
268 } // namespace content | 275 } // namespace content |
OLD | NEW |