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

Unified Diff: base/android/record_histogram.cc

Issue 794273004: Add a Java API for UMA histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Maria's comments. Created 6 years 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
Index: base/android/record_histogram.cc
diff --git a/base/android/record_histogram.cc b/base/android/record_histogram.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1cba67c73465f08dac79aa6aec4f955a94d0da2b
--- /dev/null
+++ b/base/android/record_histogram.cc
@@ -0,0 +1,69 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/android/record_histogram.h"
+#include "base/metrics/histogram.h"
+#include "base/metrics/statistics_recorder.h"
+#include "jni/RecordHistogram_jni.h"
+
+namespace base {
+namespace android {
+
+void RecordBooleanHistogram(JNIEnv* env,
+ jclass,
Alexei Svitkine (slow) 2015/01/05 15:54:41 Nit: I thought Chromium style doesn't permit omitt
ppi 2015/01/08 10:54:22 Done.
+ jstring j_histogram_name,
+ jboolean j_sample) {
+ std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name);
+ bool sample = static_cast<bool>(j_sample);
+
+ BooleanHistogram::FactoryGet(histogram_name,
+ base::HistogramBase::kUmaTargetedHistogramFlag)
Alexei Svitkine (slow) 2015/01/05 15:54:41 Nit: This is already in base namespace right, so y
ppi 2015/01/08 10:54:22 Done.
+ ->AddBoolean(sample);
+}
+
+void RecordEnumeratedHistogram(JNIEnv* env,
+ jclass,
+ jstring j_histogram_name,
+ jint j_sample,
+ jint j_boundary) {
+ std::string histogram_name = ConvertJavaStringToUTF8(env, j_histogram_name);
+ int sample = static_cast<int>(j_sample);
+ int boundary = static_cast<int>(j_boundary);
+
+ base::LinearHistogram::FactoryGet(
+ histogram_name, 1, boundary, boundary + 1,
+ base::HistogramBase::kUmaTargetedHistogramFlag)->Add(sample);
+}
+
+void Initialize(JNIEnv* env, jclass) {
+ StatisticsRecorder::Initialize();
+}
+
+// This backs a Java test util for testing histograms -
+// MetricsUtils.HistogramDelta. It should live in a test-specific file, but we
+// currently can't have test-specific native code packaged in test-specific Java
+// targets - see http://crbug.com/415945.
+jint GetHistogramValueCountForTesting(JNIEnv* env,
+ jclass,
+ jstring histogram_name,
+ jint sample) {
+ base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram(
+ base::android::ConvertJavaStringToUTF8(env, histogram_name));
+ if (histogram == nullptr) {
+ // No samples have been recorded for this histogram (yet?).
+ return 0;
+ }
+
+ scoped_ptr<base::HistogramSamples> samples = histogram->SnapshotSamples();
+ return samples->GetCount(static_cast<int>(sample));
+}
+
+bool RegisterRecordHistogram(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698