Index: base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
diff --git a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
index c1b04892e87362dfc459bcbacdb75d817f4fb975..1fc245bdb2bc6520070252184a49a92d23c6c31d 100644 |
--- a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
+++ b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
@@ -4,8 +4,10 @@ |
package org.chromium.base.metrics; |
+import org.chromium.base.Log; |
import org.chromium.base.VisibleForTesting; |
import org.chromium.base.annotations.JNINamespace; |
+import org.chromium.base.annotations.RemovableInRelease; |
import java.util.concurrent.TimeUnit; |
@@ -18,6 +20,29 @@ import java.util.concurrent.TimeUnit; |
*/ |
@JNINamespace("base::android") |
public class RecordHistogram { |
+ private static final String TAG = "RecordHistogram"; |
+ |
+ private static boolean sNativeInitialized = false; |
+ |
+ @RemovableInRelease |
+ private static void raiseExceptionInDebug(String name) { |
+ throw new RuntimeException("Calling RecordHistogram for " + name + " before initialized."); |
+ } |
+ |
+ /** |
+ * Ensures that an exception is thrown in debug builds when histograms are used before being |
+ * initialized. In release just log a warning. |
+ * @param name Name of the histogram. |
+ * @return Whether histograms have been initialized. |
+ */ |
+ private static boolean isNativeInitialized(String name) { |
+ if (!sNativeInitialized) { |
+ raiseExceptionInDebug(name); |
+ Log.w(TAG, "Calling RecordHistogram for " + name + " before initialized."); |
+ } |
+ return sNativeInitialized; |
+ } |
+ |
/** |
* Records a sample in a boolean UMA histogram of the given name. Boolean histogram has two |
* buckets, corresponding to success (true) and failure (false). This is the Java equivalent of |
@@ -26,7 +51,9 @@ public class RecordHistogram { |
* @param sample sample to be recorded, either true or false |
*/ |
public static void recordBooleanHistogram(String name, boolean sample) { |
- nativeRecordBooleanHistogram(name, System.identityHashCode(name), sample); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordBooleanHistogram(name, System.identityHashCode(name), sample); |
+ } |
} |
/** |
@@ -39,7 +66,9 @@ public class RecordHistogram { |
* lower than |boundary| |
*/ |
public static void recordEnumeratedHistogram(String name, int sample, int boundary) { |
- nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, boundary); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, boundary); |
+ } |
} |
/** |
@@ -83,8 +112,10 @@ public class RecordHistogram { |
*/ |
public static void recordCustomCountHistogram( |
String name, int sample, int min, int max, int numBuckets) { |
- nativeRecordCustomCountHistogram( |
- name, System.identityHashCode(name), sample, min, max, numBuckets); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordCustomCountHistogram( |
+ name, System.identityHashCode(name), sample, min, max, numBuckets); |
+ } |
} |
/** |
@@ -98,8 +129,10 @@ public class RecordHistogram { |
*/ |
public static void recordLinearCountHistogram( |
String name, int sample, int min, int max, int numBuckets) { |
- nativeRecordLinearCountHistogram( |
- name, System.identityHashCode(name), sample, min, max, numBuckets); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordLinearCountHistogram( |
+ name, System.identityHashCode(name), sample, min, max, numBuckets); |
+ } |
} |
/** |
@@ -109,7 +142,9 @@ public class RecordHistogram { |
* @param sample sample to be recorded, at least 0 and at most 100. |
*/ |
public static void recordPercentageHistogram(String name, int sample) { |
- nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, 101); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, 101); |
+ } |
} |
/** |
@@ -119,7 +154,9 @@ public class RecordHistogram { |
* values. |
*/ |
public static void recordSparseSlowlyHistogram(String name, int sample) { |
- nativeRecordSparseHistogram(name, System.identityHashCode(name), sample); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordSparseHistogram(name, System.identityHashCode(name), sample); |
+ } |
} |
/** |
@@ -176,8 +213,10 @@ public class RecordHistogram { |
private static void recordCustomTimesHistogramMilliseconds( |
String name, long duration, long min, long max, int numBuckets) { |
- nativeRecordCustomTimesHistogramMilliseconds( |
- name, System.identityHashCode(name), duration, min, max, numBuckets); |
+ if (isNativeInitialized(name)) { |
+ nativeRecordCustomTimesHistogramMilliseconds( |
+ name, System.identityHashCode(name), duration, min, max, numBuckets); |
+ } |
} |
/** |
@@ -187,6 +226,7 @@ public class RecordHistogram { |
*/ |
@VisibleForTesting |
public static int getHistogramValueCountForTesting(String name, int sample) { |
+ // Should fail if native is not loaded since this is testing-only. |
return nativeGetHistogramValueCountForTesting(name, sample); |
} |
@@ -195,6 +235,7 @@ public class RecordHistogram { |
*/ |
public static void initialize() { |
nativeInitialize(); |
+ sNativeInitialized = true; |
} |
private static native void nativeRecordCustomTimesHistogramMilliseconds( |