diff --git a/toolkit/components/telemetry/Telemetry.cpp b/toolkit/components/telemetry/Telemetry.cpp index 302415451940..9412181aec6e 100644 --- a/toolkit/components/telemetry/Telemetry.cpp +++ b/toolkit/components/telemetry/Telemetry.cpp @@ -461,6 +461,19 @@ JSHistogram_Snapshot(JSContext *cx, unsigned argc, jsval *vp) } } +JSBool +JSHistogram_Clear(JSContext *cx, unsigned argc, jsval *vp) +{ + JSObject *obj = JS_THIS_OBJECT(cx, vp); + if (!obj) { + return JS_FALSE; + } + + Histogram *h = static_cast(JS_GetPrivate(obj)); + h->Clear(); + return JS_TRUE; +} + nsresult WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret) { @@ -475,8 +488,9 @@ WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret) if (!obj) return NS_ERROR_FAILURE; JS::AutoObjectRooter root(cx, obj); - if (!(JS_DefineFunction (cx, obj, "add", JSHistogram_Add, 1, 0) - && JS_DefineFunction (cx, obj, "snapshot", JSHistogram_Snapshot, 1, 0))) { + if (!(JS_DefineFunction(cx, obj, "add", JSHistogram_Add, 1, 0) + && JS_DefineFunction(cx, obj, "snapshot", JSHistogram_Snapshot, 0, 0) + && JS_DefineFunction(cx, obj, "clear", JSHistogram_Clear, 0, 0))) { return NS_ERROR_FAILURE; } *ret = OBJECT_TO_JSVAL(obj); diff --git a/toolkit/components/telemetry/nsITelemetry.idl b/toolkit/components/telemetry/nsITelemetry.idl index 61a7e50c91ec..fe7b3d06b6aa 100644 --- a/toolkit/components/telemetry/nsITelemetry.idl +++ b/toolkit/components/telemetry/nsITelemetry.idl @@ -158,6 +158,7 @@ interface nsITelemetry : nsISupports * The returned object has the following functions: * add(int) - Adds an int value to the appropriate bucket * snapshot() - Returns a snapshot of the histogram with the same data fields as in histogramSnapshots() + * clear() - Zeros out the histogram's buckets and sum */ [implicit_jscontext] jsval newHistogram(in ACString name, in PRUint32 min, in PRUint32 max, in PRUint32 bucket_count, in unsigned long histogram_type); diff --git a/toolkit/components/telemetry/tests/unit/test_nsITelemetry.js b/toolkit/components/telemetry/tests/unit/test_nsITelemetry.js index 80cbd2620298..96296726aa5d 100644 --- a/toolkit/components/telemetry/tests/unit/test_nsITelemetry.js +++ b/toolkit/components/telemetry/tests/unit/test_nsITelemetry.js @@ -39,6 +39,20 @@ function test_histogram(histogram_type, name, min, max, bucket_count) { var s = h.snapshot().counts; do_check_eq(s[0], 2) do_check_eq(s[1], 2) + + // Check that clearing works. + h.clear(); + var s = h.snapshot(); + for each(var i in s.counts) { + do_check_eq(i, 0); + } + do_check_eq(s.sum, 0); + + h.add(0); + h.add(1); + var c = h.snapshot().counts; + do_check_eq(c[0], 1); + do_check_eq(c[1], 1); } function expect_fail(f) {