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

Unified Diff: content/browser/battery_status/battery_status_manager_mac.cc

Issue 545503002: Battery Status API: add UMA logging for Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix histogram updates only on start Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/battery_status/battery_status_manager_mac.cc
diff --git a/content/browser/battery_status/battery_status_manager_mac.cc b/content/browser/battery_status/battery_status_manager_mac.cc
index 8d6f35b3166d608ab9a2f7c7dd21e3a072304a3b..74ac272bb564680875edf2440195353e1ff12bf3 100644
--- a/content/browser/battery_status/battery_status_manager_mac.cc
+++ b/content/browser/battery_status/battery_status_manager_mac.cc
@@ -7,10 +7,12 @@
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>
+#include <vector>
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/ref_counted.h"
+#include "base/metrics/histogram.h"
#include "base/time/time.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/WebKit/public/platform/WebBatteryStatus.h"
@@ -52,6 +54,11 @@ bool CFStringsAreEqual(CFStringRef string1, CFStringRef string2) {
return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo;
}
+void UpdateNumberBatteriesHistogram(int count) {
+ UMA_HISTOGRAM_CUSTOM_COUNTS(
+ "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6);
+}
+
void FetchBatteryStatus(CFDictionaryRef description,
blink::WebBatteryStatus& status) {
CFStringRef current_state =
@@ -108,16 +115,14 @@ void FetchBatteryStatus(CFDictionaryRef description,
}
}
-void OnBatteryStatusChanged(const BatteryCallback& callback) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- blink::WebBatteryStatus status;
+std::vector<blink::WebBatteryStatus> GetInternalBatteriesStates() {
+ std::vector<blink::WebBatteryStatus> internal_sources;
+
base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo());
base::ScopedCFTypeRef<CFArrayRef> power_sources_list(
IOPSCopyPowerSourcesList(info));
CFIndex count = CFArrayGetCount(power_sources_list);
- bool internal_source_found = false;
-
for (CFIndex i = 0; i < count; ++i) {
CFDictionaryRef description = IOPSGetPowerSourceDescription(info,
CFArrayGetValueAtIndex(power_sources_list, i));
@@ -135,16 +140,30 @@ void OnBatteryStatusChanged(const BatteryCallback& callback) {
GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false);
if (internal_source && source_present) {
- // TODO(timvolodine): implement the case when there are multiple internal
- // sources, e.g. when multiple batteries are present. Currently this will
- // fail a DCHECK.
- DCHECK(!internal_source_found);
+ blink::WebBatteryStatus status;
FetchBatteryStatus(description, status);
- internal_source_found = true;
+ internal_sources.push_back(status);
}
}
- callback.Run(status);
+ return internal_sources;
+}
+
+void OnBatteryStatusChanged(const BatteryCallback& callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ std::vector<blink::WebBatteryStatus> batteries(GetInternalBatteriesStates());
+
+ if (batteries.empty()) {
+ callback.Run(blink::WebBatteryStatus());
+ return;
+ }
+
+ // TODO(timvolodine): implement the case when there are multiple internal
+ // sources, e.g. when multiple batteries are present. Currently this will
+ // fail a DCHECK.
+ DCHECK(batteries.size() == 1);
+ callback.Run(batteries.front());
}
class BatteryStatusObserver
@@ -200,6 +219,7 @@ class BatteryStatusObserver
OnBatteryStatusChangedUI(static_cast<void*>(&callback_));
CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_,
kCFRunLoopDefaultMode);
+ UpdateNumberBatteriesHistogram(GetInternalBatteriesStates().size());
}
void StopOnUI() {
« no previous file with comments | « no previous file | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698