summaryrefslogtreecommitdiffstats
path: root/toolkit/components/glean/bindings/private
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/components/glean/bindings/private
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--toolkit/components/glean/bindings/private/Boolean.cpp78
-rw-r--r--toolkit/components/glean/bindings/private/Boolean.h72
-rw-r--r--toolkit/components/glean/bindings/private/Common.cpp33
-rw-r--r--toolkit/components/glean/bindings/private/Common.h24
-rw-r--r--toolkit/components/glean/bindings/private/Counter.cpp80
-rw-r--r--toolkit/components/glean/bindings/private/Counter.h70
-rw-r--r--toolkit/components/glean/bindings/private/CustomDistribution.cpp125
-rw-r--r--toolkit/components/glean/bindings/private/CustomDistribution.h83
-rw-r--r--toolkit/components/glean/bindings/private/Datetime.cpp124
-rw-r--r--toolkit/components/glean/bindings/private/Datetime.h71
-rw-r--r--toolkit/components/glean/bindings/private/Denominator.cpp70
-rw-r--r--toolkit/components/glean/bindings/private/Denominator.h70
-rw-r--r--toolkit/components/glean/bindings/private/DistributionData.h32
-rw-r--r--toolkit/components/glean/bindings/private/Event.cpp183
-rw-r--r--toolkit/components/glean/bindings/private/Event.h162
-rw-r--r--toolkit/components/glean/bindings/private/Labeled.cpp99
-rw-r--r--toolkit/components/glean/bindings/private/Labeled.h75
-rw-r--r--toolkit/components/glean/bindings/private/MemoryDistribution.cpp104
-rw-r--r--toolkit/components/glean/bindings/private/MemoryDistribution.h74
-rw-r--r--toolkit/components/glean/bindings/private/Numerator.cpp89
-rw-r--r--toolkit/components/glean/bindings/private/Numerator.h72
-rw-r--r--toolkit/components/glean/bindings/private/Ping.cpp94
-rw-r--r--toolkit/components/glean/bindings/private/Ping.h83
-rw-r--r--toolkit/components/glean/bindings/private/Quantity.cpp74
-rw-r--r--toolkit/components/glean/bindings/private/Quantity.h68
-rw-r--r--toolkit/components/glean/bindings/private/Rate.cpp105
-rw-r--r--toolkit/components/glean/bindings/private/Rate.h77
-rw-r--r--toolkit/components/glean/bindings/private/String.cpp77
-rw-r--r--toolkit/components/glean/bindings/private/String.h73
-rw-r--r--toolkit/components/glean/bindings/private/StringList.cpp92
-rw-r--r--toolkit/components/glean/bindings/private/StringList.h84
-rw-r--r--toolkit/components/glean/bindings/private/Timespan.cpp133
-rw-r--r--toolkit/components/glean/bindings/private/Timespan.h99
-rw-r--r--toolkit/components/glean/bindings/private/TimingDistribution.cpp196
-rw-r--r--toolkit/components/glean/bindings/private/TimingDistribution.h109
-rw-r--r--toolkit/components/glean/bindings/private/Url.cpp77
-rw-r--r--toolkit/components/glean/bindings/private/Url.h70
-rw-r--r--toolkit/components/glean/bindings/private/Uuid.cpp89
-rw-r--r--toolkit/components/glean/bindings/private/Uuid.h75
39 files changed, 3465 insertions, 0 deletions
diff --git a/toolkit/components/glean/bindings/private/Boolean.cpp b/toolkit/components/glean/bindings/private/Boolean.cpp
new file mode 100644
index 0000000000..05520ece6b
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Boolean.cpp
@@ -0,0 +1,78 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Boolean.h"
+
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "Common.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void BooleanMetric::Set(bool aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId) {
+ Telemetry::ScalarSet(scalarId.extract(), aValue);
+ } else if (IsSubmetricId(mId)) {
+ GetLabeledMirrorLock().apply([&](auto& lock) {
+ auto tuple = lock.ref()->MaybeGet(mId);
+ if (tuple) {
+ Telemetry::ScalarSet(Get<0>(tuple.ref()), Get<1>(tuple.ref()), aValue);
+ }
+ });
+ }
+ fog_boolean_set(mId, int(aValue));
+}
+
+Result<Maybe<bool>, nsCString> BooleanMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_boolean_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_boolean_test_has_value(mId, &aPingName)) {
+ return Maybe<bool>();
+ }
+ return Some(fog_boolean_test_get_value(mId, &aPingName));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanBoolean, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanBoolean, nsIGleanBoolean)
+
+NS_IMETHODIMP
+GleanBoolean::Set(bool aValue) {
+ mBoolean.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanBoolean::TestGetValue(const nsACString& aStorageName,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mBoolean.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ aResult.set(JS::BooleanValue(optresult.value()));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Boolean.h b/toolkit/components/glean/bindings/private/Boolean.h
new file mode 100644
index 0000000000..890820440d
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Boolean.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanBoolean_h
+#define mozilla_glean_GleanBoolean_h
+
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla {
+namespace glean {
+
+namespace impl {
+
+class BooleanMetric {
+ public:
+ constexpr explicit BooleanMetric(uint32_t id) : mId(id) {}
+
+ /**
+ * Set to the specified boolean value.
+ *
+ * @param aValue the value to set.
+ */
+ void Set(bool aValue) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a boolean.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric.
+ */
+ Result<Maybe<bool>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+
+} // namespace impl
+
+class GleanBoolean final : public nsIGleanBoolean {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANBOOLEAN
+
+ explicit GleanBoolean(uint32_t id) : mBoolean(id){};
+
+ private:
+ virtual ~GleanBoolean() = default;
+
+ const impl::BooleanMetric mBoolean;
+};
+
+} // namespace glean
+} // namespace mozilla
+
+#endif /* mozilla_glean_GleanBoolean.h */
diff --git a/toolkit/components/glean/bindings/private/Common.cpp b/toolkit/components/glean/bindings/private/Common.cpp
new file mode 100644
index 0000000000..f84c39c05d
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Common.cpp
@@ -0,0 +1,33 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "Common.h"
+#include "nsComponentManagerUtils.h"
+#include "nsIConsoleService.h"
+#include "nsIScriptError.h"
+#include "nsServiceManagerUtils.h"
+
+namespace mozilla::glean {
+
+// This is copied from TelemetryCommons.cpp (and modified because consoleservice
+// handles threading), but that one is not exported.
+// There's _at least_ a third instance of `LogToBrowserConsole`,
+// but that one is slightly different.
+void LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg) {
+ nsCOMPtr<nsIConsoleService> console(
+ do_GetService("@mozilla.org/consoleservice;1"));
+ if (!console) {
+ NS_WARNING("Failed to log message to console.");
+ return;
+ }
+
+ nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
+ error->Init(aMsg, u""_ns, u""_ns, 0, 0, aLogLevel, "chrome javascript"_ns,
+ false /* from private window */, true /* from chrome context */);
+ console->LogMessage(error);
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Common.h b/toolkit/components/glean/bindings/private/Common.h
new file mode 100644
index 0000000000..e3dd7a0a47
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Common.h
@@ -0,0 +1,24 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_Common_h
+#define mozilla_glean_Common_h
+
+#include "nsIScriptError.h"
+
+namespace mozilla::glean {
+
+/**
+ * Dumps a log message to the Browser Console using the provided level.
+ *
+ * @param aLogLevel The level to use when displaying the message in the browser
+ * console (e.g. nsIScriptError::warningFlag, ...).
+ * @param aMsg The text message to print to the console.
+ */
+void LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg);
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_Common_h */
diff --git a/toolkit/components/glean/bindings/private/Counter.cpp b/toolkit/components/glean/bindings/private/Counter.cpp
new file mode 100644
index 0000000000..2a4477b4fc
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Counter.cpp
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Counter.h"
+
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void CounterMetric::Add(int32_t aAmount) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (aAmount >= 0) {
+ if (scalarId) {
+ Telemetry::ScalarAdd(scalarId.extract(), aAmount);
+ } else if (IsSubmetricId(mId)) {
+ GetLabeledMirrorLock().apply([&](auto& lock) {
+ auto tuple = lock.ref()->MaybeGet(mId);
+ if (tuple && aAmount > 0) {
+ Telemetry::ScalarAdd(Get<0>(tuple.ref()), Get<1>(tuple.ref()),
+ (uint32_t)aAmount);
+ }
+ });
+ }
+ }
+ fog_counter_add(mId, aAmount);
+}
+
+Result<Maybe<int32_t>, nsCString> CounterMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_counter_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_counter_test_has_value(mId, &aPingName)) {
+ return Maybe<int32_t>(); // can't use Nothing() or templates will fail.
+ }
+ return Some(fog_counter_test_get_value(mId, &aPingName));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanCounter, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanCounter, nsIGleanCounter)
+
+NS_IMETHODIMP
+GleanCounter::Add(int32_t aAmount) {
+ mCounter.Add(aAmount);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanCounter::TestGetValue(const nsACString& aStorageName,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mCounter.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ aResult.set(JS::Int32Value(optresult.value()));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Counter.h b/toolkit/components/glean/bindings/private/Counter.h
new file mode 100644
index 0000000000..e1e7190e4e
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Counter.h
@@ -0,0 +1,70 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanCounter_h
+#define mozilla_glean_GleanCounter_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class CounterMetric {
+ public:
+ constexpr explicit CounterMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Increases the counter by `amount`.
+ *
+ * @param aAmount The amount to increase by. Should be positive.
+ */
+ void Add(int32_t aAmount = 1) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as an integer.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<int32_t>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanCounter final : public nsIGleanCounter {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANCOUNTER
+
+ explicit GleanCounter(uint32_t id) : mCounter(id){};
+
+ private:
+ virtual ~GleanCounter() = default;
+
+ const impl::CounterMetric mCounter;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanCounter_h */
diff --git a/toolkit/components/glean/bindings/private/CustomDistribution.cpp b/toolkit/components/glean/bindings/private/CustomDistribution.cpp
new file mode 100644
index 0000000000..4abd0100e7
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/CustomDistribution.cpp
@@ -0,0 +1,125 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/CustomDistribution.h"
+
+#include "Common.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/HistogramGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsJSUtils.h"
+#include "nsPrintfCString.h"
+#include "nsString.h"
+#include "js/PropertyAndElement.h" // JS_DefineProperty
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void CustomDistributionMetric::AccumulateSamples(
+ const nsTArray<uint64_t>& aSamples) const {
+ auto hgramId = HistogramIdForMetric(mId);
+ if (hgramId) {
+ auto id = hgramId.extract();
+ // N.B.: There is an `Accumulate(nsTArray<T>)`, but `T` is `uint32_t` and
+ // we got `uint64_t`s here.
+ for (auto sample : aSamples) {
+ Telemetry::Accumulate(id, sample);
+ }
+ }
+ fog_custom_distribution_accumulate_samples(mId, &aSamples);
+}
+
+void CustomDistributionMetric::AccumulateSamplesSigned(
+ const nsTArray<int64_t>& aSamples) const {
+ auto hgramId = HistogramIdForMetric(mId);
+ if (hgramId) {
+ auto id = hgramId.extract();
+ // N.B.: There is an `Accumulate(nsTArray<T>)`, but `T` is `uint32_t` and
+ // we got `int64_t`s here.
+ for (auto sample : aSamples) {
+ Telemetry::Accumulate(id, sample);
+ }
+ }
+ fog_custom_distribution_accumulate_samples_signed(mId, &aSamples);
+}
+
+Result<Maybe<DistributionData>, nsCString>
+CustomDistributionMetric::TestGetValue(const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_custom_distribution_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_custom_distribution_test_has_value(mId, &aPingName)) {
+ return Maybe<DistributionData>();
+ }
+ nsTArray<uint64_t> buckets;
+ nsTArray<uint64_t> counts;
+ uint64_t sum;
+ fog_custom_distribution_test_get_value(mId, &aPingName, &sum, &buckets,
+ &counts);
+ return Some(DistributionData(buckets, counts, sum));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanCustomDistribution, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanCustomDistribution, nsIGleanCustomDistribution)
+
+NS_IMETHODIMP
+GleanCustomDistribution::AccumulateSamples(const nsTArray<int64_t>& aSamples) {
+ mCustomDist.AccumulateSamplesSigned(aSamples);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanCustomDistribution::TestGetValue(const nsACString& aPingName,
+ JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mCustomDist.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ // Build return value of the form: { sum: #, values: {bucket1: count1, ...}
+ JS::Rooted<JSObject*> root(aCx, JS_NewPlainObject(aCx));
+ if (!root) {
+ return NS_ERROR_FAILURE;
+ }
+ uint64_t sum = optresult.ref().sum;
+ if (!JS_DefineProperty(aCx, root, "sum", static_cast<double>(sum),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ JS::Rooted<JSObject*> valuesObj(aCx, JS_NewPlainObject(aCx));
+ if (!valuesObj ||
+ !JS_DefineProperty(aCx, root, "values", valuesObj, JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ auto& data = optresult.ref().values;
+ for (const auto& entry : data) {
+ const uint64_t bucket = entry.GetKey();
+ const uint64_t count = entry.GetData();
+ if (!JS_DefineProperty(aCx, valuesObj,
+ nsPrintfCString("%" PRIu64, bucket).get(),
+ static_cast<double>(count), JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/CustomDistribution.h b/toolkit/components/glean/bindings/private/CustomDistribution.h
new file mode 100644
index 0000000000..1ed9b78788
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/CustomDistribution.h
@@ -0,0 +1,83 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanCustomDistribution_h
+#define mozilla_glean_GleanCustomDistribution_h
+
+#include "mozilla/glean/bindings/DistributionData.h"
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class CustomDistributionMetric {
+ public:
+ constexpr explicit CustomDistributionMetric(uint32_t aId) : mId(aId) {}
+
+ /**
+ * Accumulates the provided samples in the metric.
+ *
+ * @param aSamples The vector holding the samples to be recorded by the
+ * metric.
+ */
+ void AccumulateSamples(const nsTArray<uint64_t>& aSamples) const;
+
+ /**
+ * Accumulates the provided samples in the metric.
+ *
+ * @param aSamples The vector holding the samples to be recorded by the
+ * metric.
+ *
+ * Notes: Discards any negative value in `samples`
+ * and reports an `InvalidValue` error for each of them.
+ */
+ void AccumulateSamplesSigned(const nsTArray<int64_t>& aSamples) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a DistributionData.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<DistributionData>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanCustomDistribution final : public nsIGleanCustomDistribution {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANCUSTOMDISTRIBUTION
+
+ explicit GleanCustomDistribution(uint64_t aId) : mCustomDist(aId){};
+
+ private:
+ virtual ~GleanCustomDistribution() = default;
+
+ const impl::CustomDistributionMetric mCustomDist;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanCustomDistribution_h */
diff --git a/toolkit/components/glean/bindings/private/Datetime.cpp b/toolkit/components/glean/bindings/private/Datetime.cpp
new file mode 100644
index 0000000000..900b8e6a13
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Datetime.cpp
@@ -0,0 +1,124 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Datetime.h"
+
+#include "jsapi.h"
+#include "js/Date.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "prtime.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void DatetimeMetric::Set(const PRExplodedTime* aValue) const {
+ PRExplodedTime exploded;
+ if (!aValue) {
+ PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &exploded);
+ } else {
+ exploded = *aValue;
+ }
+
+ auto id = ScalarIdForMetric(mId);
+ if (id) {
+ const uint32_t buflen = 64; // More than enough for now.
+ char buf[buflen];
+ uint32_t written = PR_FormatTime(buf, buflen, "%FT%T%z", &exploded);
+ if (written > 2 && written < 64) {
+ // Format's still not quite there. Gotta put a `:` between timezone
+ // hours and minutes
+ buf[written] = '\0';
+ buf[written - 1] = buf[written - 2];
+ buf[written - 2] = buf[written - 3];
+ buf[written - 3] = ':';
+ Telemetry::ScalarSet(id.extract(), NS_ConvertASCIItoUTF16(buf));
+ }
+ }
+
+ int32_t offset =
+ exploded.tm_params.tp_gmt_offset + exploded.tm_params.tp_dst_offset;
+ FogDatetime dt{exploded.tm_year,
+ static_cast<uint32_t>(exploded.tm_month + 1),
+ static_cast<uint32_t>(exploded.tm_mday),
+ static_cast<uint32_t>(exploded.tm_hour),
+ static_cast<uint32_t>(exploded.tm_min),
+ static_cast<uint32_t>(exploded.tm_sec),
+ static_cast<uint32_t>(exploded.tm_usec * 1000),
+ offset};
+ fog_datetime_set(mId, &dt);
+}
+
+Result<Maybe<PRExplodedTime>, nsCString> DatetimeMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_datetime_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_datetime_test_has_value(mId, &aPingName)) {
+ return Maybe<PRExplodedTime>();
+ }
+ FogDatetime ret{0};
+ fog_datetime_test_get_value(mId, &aPingName, &ret);
+ PRExplodedTime pret{0};
+ pret.tm_year = static_cast<PRInt16>(ret.year);
+ pret.tm_month = static_cast<PRInt32>(ret.month - 1);
+ pret.tm_mday = static_cast<PRInt32>(ret.day);
+ pret.tm_hour = static_cast<PRInt32>(ret.hour);
+ pret.tm_min = static_cast<PRInt32>(ret.minute);
+ pret.tm_sec = static_cast<PRInt32>(ret.second);
+ pret.tm_usec = static_cast<PRInt32>(ret.nano / 1000); // truncated is fine
+ pret.tm_params.tp_gmt_offset = static_cast<PRInt32>(ret.offset_seconds);
+ return Some(std::move(pret));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanDatetime, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanDatetime, nsIGleanDatetime)
+
+NS_IMETHODIMP
+GleanDatetime::Set(PRTime aValue, uint8_t aOptionalArgc) {
+ if (aOptionalArgc == 0) {
+ mDatetime.Set();
+ } else {
+ PRExplodedTime exploded;
+ PR_ExplodeTime(aValue, PR_LocalTimeParameters, &exploded);
+ mDatetime.Set(&exploded);
+ }
+
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanDatetime::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mDatetime.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ double millis =
+ static_cast<double>(PR_ImplodeTime(optresult.ptr())) / PR_USEC_PER_MSEC;
+ JS::Rooted<JSObject*> root(aCx,
+ JS::NewDateObject(aCx, JS::TimeClip(millis)));
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Datetime.h b/toolkit/components/glean/bindings/private/Datetime.h
new file mode 100644
index 0000000000..4fec93d251
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Datetime.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanDatetime_h
+#define mozilla_glean_GleanDatetime_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+#include "prtime.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class DatetimeMetric {
+ public:
+ constexpr explicit DatetimeMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Set the datetime to the provided value, or the local now.
+ *
+ * @param amount The date value to set.
+ */
+ void Set(const PRExplodedTime* aValue = nullptr) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a PRExplodedTime.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<PRExplodedTime>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanDatetime final : public nsIGleanDatetime {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANDATETIME
+
+ explicit GleanDatetime(uint32_t aId) : mDatetime(aId){};
+
+ private:
+ virtual ~GleanDatetime() = default;
+
+ const impl::DatetimeMetric mDatetime;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanDatetime_h */
diff --git a/toolkit/components/glean/bindings/private/Denominator.cpp b/toolkit/components/glean/bindings/private/Denominator.cpp
new file mode 100644
index 0000000000..e922c4f145
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Denominator.cpp
@@ -0,0 +1,70 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Denominator.h"
+
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void DenominatorMetric::Add(int32_t aAmount) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId && aAmount >= 0) {
+ Telemetry::ScalarAdd(scalarId.extract(), aAmount);
+ }
+ fog_denominator_add(mId, aAmount);
+}
+
+Result<Maybe<int32_t>, nsCString> DenominatorMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_denominator_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_denominator_test_has_value(mId, &aPingName)) {
+ return Maybe<int32_t>();
+ }
+ return Some(fog_denominator_test_get_value(mId, &aPingName));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanDenominator, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanDenominator, nsIGleanDenominator)
+
+NS_IMETHODIMP
+GleanDenominator::Add(int32_t aAmount) {
+ mDenominator.Add(aAmount);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanDenominator::TestGetValue(const nsACString& aStorageName,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mDenominator.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ aResult.set(JS::Int32Value(optresult.value()));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Denominator.h b/toolkit/components/glean/bindings/private/Denominator.h
new file mode 100644
index 0000000000..c96bf17e8d
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Denominator.h
@@ -0,0 +1,70 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanDenominator_h
+#define mozilla_glean_GleanDenominator_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class DenominatorMetric {
+ public:
+ constexpr explicit DenominatorMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Increases the counter by `amount`.
+ *
+ * @param aAmount The amount to increase by. Should be positive.
+ */
+ void Add(int32_t aAmount = 1) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as an integer.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<int32_t>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanDenominator final : public nsIGleanDenominator {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANDENOMINATOR
+
+ explicit GleanDenominator(uint32_t id) : mDenominator(id){};
+
+ private:
+ virtual ~GleanDenominator() = default;
+
+ const impl::DenominatorMetric mDenominator;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanDenominator_h */
diff --git a/toolkit/components/glean/bindings/private/DistributionData.h b/toolkit/components/glean/bindings/private/DistributionData.h
new file mode 100644
index 0000000000..6ff995f222
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/DistributionData.h
@@ -0,0 +1,32 @@
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_DistributionData_h
+#define mozilla_glean_DistributionData_h
+
+#include "nsTHashMap.h"
+
+namespace mozilla::glean {
+
+struct DistributionData final {
+ uint64_t sum;
+ nsTHashMap<nsUint64HashKey, uint64_t> values;
+
+ /**
+ * Create distribution data from the buckets, counts and sum,
+ * as returned by `fog_*_distribution_test_get_value`.
+ */
+ DistributionData(const nsTArray<uint64_t>& aBuckets,
+ const nsTArray<uint64_t>& aCounts, uint64_t aSum)
+ : sum(aSum) {
+ for (size_t i = 0; i < aBuckets.Length(); ++i) {
+ this->values.InsertOrUpdate(aBuckets[i], aCounts[i]);
+ }
+ }
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_DistributionData_h */
diff --git a/toolkit/components/glean/bindings/private/Event.cpp b/toolkit/components/glean/bindings/private/Event.cpp
new file mode 100644
index 0000000000..d2eb5b3ca6
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Event.cpp
@@ -0,0 +1,183 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Event.h"
+
+#include "Common.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/dom/ToJSValue.h"
+#include "nsIClassInfoImpl.h"
+#include "jsapi.h"
+#include "js/PropertyAndElement.h" // JS_DefineElement, JS_DefineProperty, JS_Enumerate, JS_GetProperty, JS_GetPropertyById
+#include "nsIScriptError.h"
+
+namespace mozilla::glean {
+
+NS_IMPL_CLASSINFO(GleanEvent, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanEvent, nsIGleanEvent)
+
+NS_IMETHODIMP
+GleanEvent::Record(JS::Handle<JS::Value> aExtra, JSContext* aCx) {
+ if (aExtra.isNullOrUndefined()) {
+ mEvent.Record();
+ return NS_OK;
+ }
+
+ if (!aExtra.isObject()) {
+ LogToBrowserConsole(nsIScriptError::warningFlag,
+ u"Extras need to be an object"_ns);
+ return NS_OK;
+ }
+
+ nsTArray<nsCString> extraKeys;
+ nsTArray<nsCString> extraValues;
+ CopyableTArray<Telemetry::EventExtraEntry> telExtras;
+
+ JS::Rooted<JSObject*> obj(aCx, &aExtra.toObject());
+ JS::Rooted<JS::IdVector> ids(aCx, JS::IdVector(aCx));
+ if (!JS_Enumerate(aCx, obj, &ids)) {
+ LogToBrowserConsole(nsIScriptError::warningFlag,
+ u"Failed to enumerate object."_ns);
+ return NS_OK;
+ }
+
+ for (size_t i = 0, n = ids.length(); i < n; i++) {
+ nsAutoJSCString jsKey;
+ if (!jsKey.init(aCx, ids[i])) {
+ LogToBrowserConsole(
+ nsIScriptError::warningFlag,
+ u"Extra dictionary should only contain string keys."_ns);
+ return NS_OK;
+ }
+
+ JS::Rooted<JS::Value> value(aCx);
+ if (!JS_GetPropertyById(aCx, obj, ids[i], &value)) {
+ LogToBrowserConsole(nsIScriptError::warningFlag,
+ u"Failed to get extra property."_ns);
+ return NS_OK;
+ }
+
+ nsAutoJSCString jsValue;
+ if (value.isString() || (value.isInt32() && value.toInt32() >= 0) ||
+ value.isBoolean()) {
+ if (!jsValue.init(aCx, value)) {
+ LogToBrowserConsole(nsIScriptError::warningFlag,
+ u"Can't extract extra property"_ns);
+ return NS_OK;
+ }
+ } else if (value.isNullOrUndefined()) {
+ // The extra key is present, but has an empty value.
+ // Treat as though it weren't here at all.
+ continue;
+ } else {
+ LogToBrowserConsole(
+ nsIScriptError::warningFlag,
+ u"Extra properties should have string, bool or non-negative integer values."_ns);
+ return NS_OK;
+ }
+
+ extraKeys.AppendElement(jsKey);
+ extraValues.AppendElement(jsValue);
+ telExtras.EmplaceBack(Telemetry::EventExtraEntry{jsKey, jsValue});
+ }
+
+ // Since this calls the implementation directly, we need to implement GIFFT
+ // here as well as in EventMetric::Record.
+ auto id = EventIdForMetric(mEvent.mId);
+ if (id) {
+ Telemetry::RecordEvent(id.extract(), Nothing(),
+ telExtras.IsEmpty() ? Nothing() : Some(telExtras));
+ }
+
+ // Calling the implementation directly, because we have a `string->string`
+ // map, not a `T->string` map the C++ API expects.
+ impl::fog_event_record(mEvent.mId, &extraKeys, &extraValues);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanEvent::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto resEvents = mEvent.TestGetValue(aStorageName);
+ if (resEvents.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(resEvents.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optEvents = resEvents.unwrap();
+ if (optEvents.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ return NS_OK;
+ }
+
+ auto events = optEvents.extract();
+
+ auto count = events.Length();
+ JS::Rooted<JSObject*> eventArray(aCx, JS::NewArrayObject(aCx, count));
+ if (NS_WARN_IF(!eventArray)) {
+ return NS_ERROR_FAILURE;
+ }
+
+ for (size_t i = 0; i < count; i++) {
+ auto* value = &events[i];
+
+ JS::Rooted<JSObject*> eventObj(aCx, JS_NewPlainObject(aCx));
+ if (NS_WARN_IF(!eventObj)) {
+ return NS_ERROR_FAILURE;
+ }
+
+ if (!JS_DefineProperty(aCx, eventObj, "timestamp",
+ (double)value->mTimestamp, JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define timestamp for event object.");
+ return NS_ERROR_FAILURE;
+ }
+
+ JS::Rooted<JS::Value> catStr(aCx);
+ if (!dom::ToJSValue(aCx, value->mCategory, &catStr) ||
+ !JS_DefineProperty(aCx, eventObj, "category", catStr,
+ JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define category for event object.");
+ return NS_ERROR_FAILURE;
+ }
+ JS::Rooted<JS::Value> nameStr(aCx);
+ if (!dom::ToJSValue(aCx, value->mName, &nameStr) ||
+ !JS_DefineProperty(aCx, eventObj, "name", nameStr, JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define name for event object.");
+ return NS_ERROR_FAILURE;
+ }
+
+ JS::Rooted<JSObject*> extraObj(aCx, JS_NewPlainObject(aCx));
+ if (!JS_DefineProperty(aCx, eventObj, "extra", extraObj,
+ JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define extra for event object.");
+ return NS_ERROR_FAILURE;
+ }
+
+ for (auto pair : value->mExtra) {
+ auto key = mozilla::Get<0>(pair);
+ auto val = mozilla::Get<1>(pair);
+ JS::Rooted<JS::Value> valStr(aCx);
+ if (!dom::ToJSValue(aCx, val, &valStr) ||
+ !JS_DefineProperty(aCx, extraObj, key.Data(), valStr,
+ JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define extra property for event object.");
+ return NS_ERROR_FAILURE;
+ }
+ }
+
+ if (!JS_DefineElement(aCx, eventArray, i, eventObj, JSPROP_ENUMERATE)) {
+ NS_WARNING("Failed to define item in events array.");
+ return NS_ERROR_FAILURE;
+ }
+ }
+
+ aResult.setObject(*eventArray);
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Event.h b/toolkit/components/glean/bindings/private/Event.h
new file mode 100644
index 0000000000..111be2cbc2
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Event.h
@@ -0,0 +1,162 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanEvent_h
+#define mozilla_glean_GleanEvent_h
+
+#include "nsIGleanMetrics.h"
+#include "mozilla/glean/bindings/EventGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/Tuple.h"
+#include "nsString.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+// forward declaration
+class GleanEvent;
+
+namespace impl {
+
+/**
+ * Represents the recorded data for a single event
+ */
+struct RecordedEvent {
+ public:
+ uint64_t mTimestamp;
+ nsCString mCategory;
+ nsCString mName;
+
+ nsTArray<Tuple<nsCString, nsCString>> mExtra;
+};
+
+template <class T>
+class EventMetric {
+ friend class mozilla::glean::GleanEvent;
+
+ public:
+ constexpr explicit EventMetric(uint32_t id) : mId(id) {}
+
+ /**
+ * Record an event.
+ *
+ * @param aExtras The list of (extra key, value) pairs. Allowed extra keys are
+ * defined in the metric definition.
+ * If the wrong keys are used or values are too large
+ * an error is report and no event is recorded.
+ */
+ void Record(const Maybe<T>& aExtras = Nothing()) const {
+ auto id = EventIdForMetric(mId);
+ if (id) {
+ // NB. In case `aExtras` is filled we call `ToFfiExtra`, causing
+ // twice the required allocation. We could be smarter and reuse the data.
+ // But this is GIFFT-only allocation, so wait to be told it's a problem.
+ Maybe<CopyableTArray<Telemetry::EventExtraEntry>> telExtras;
+ if (aExtras) {
+ CopyableTArray<Telemetry::EventExtraEntry> extras;
+ auto serializedExtras = aExtras->ToFfiExtra();
+ auto keys = std::move(Get<0>(serializedExtras));
+ auto values = std::move(Get<1>(serializedExtras));
+ for (size_t i = 0; i < keys.Length(); i++) {
+ extras.EmplaceBack(Telemetry::EventExtraEntry{keys[i], values[i]});
+ }
+ telExtras = Some(extras);
+ }
+ Telemetry::RecordEvent(id.extract(), Nothing(), telExtras);
+ }
+ if (aExtras) {
+ auto extra = aExtras->ToFfiExtra();
+ fog_event_record(mId, &mozilla::Get<0>(extra), &mozilla::Get<1>(extra));
+ } else {
+ nsTArray<nsCString> keys;
+ nsTArray<nsCString> vals;
+ fog_event_record(mId, &keys, &vals);
+ }
+ }
+
+ /**
+ * **Test-only API**
+ *
+ * Get a list of currently stored events for this event metric.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<nsTArray<RecordedEvent>>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const {
+ nsCString err;
+ if (fog_event_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+
+ if (!fog_event_test_has_value(mId, &aPingName)) {
+ return Maybe<nsTArray<RecordedEvent>>();
+ }
+
+ nsTArray<FfiRecordedEvent> events;
+ fog_event_test_get_value(mId, &aPingName, &events);
+
+ nsTArray<RecordedEvent> result;
+ for (const auto& event : events) {
+ auto ev = result.AppendElement();
+ ev->mTimestamp = event.timestamp;
+ ev->mCategory.Append(event.category);
+ ev->mName.Assign(event.name);
+
+ MOZ_ASSERT(event.extras.Length() % 2 == 0);
+ ev->mExtra.SetCapacity(event.extras.Length() / 2);
+ for (unsigned int i = 0; i < event.extras.Length(); i += 2) {
+ // keys & values are interleaved.
+ nsCString key = std::move(event.extras[i]);
+ nsCString value = std::move(event.extras[i + 1]);
+ ev->mExtra.AppendElement(MakeTuple(std::move(key), std::move(value)));
+ }
+ }
+ return Some(std::move(result));
+ }
+
+ private:
+ static const nsCString ExtraStringForKey(uint32_t aKey);
+
+ const uint32_t mId;
+};
+
+} // namespace impl
+
+struct NoExtraKeys {
+ Tuple<nsTArray<nsCString>, nsTArray<nsCString>> ToFfiExtra() const {
+ nsTArray<nsCString> extraKeys;
+ nsTArray<nsCString> extraValues;
+ return MakeTuple(std::move(extraKeys), std::move(extraValues));
+ }
+};
+
+class GleanEvent final : public nsIGleanEvent {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANEVENT
+
+ explicit GleanEvent(uint32_t id) : mEvent(id){};
+
+ private:
+ virtual ~GleanEvent() = default;
+
+ const impl::EventMetric<NoExtraKeys> mEvent;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanEvent.h */
diff --git a/toolkit/components/glean/bindings/private/Labeled.cpp b/toolkit/components/glean/bindings/private/Labeled.cpp
new file mode 100644
index 0000000000..b01867d729
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Labeled.cpp
@@ -0,0 +1,99 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Labeled.h"
+
+#include "mozilla/dom/GleanBinding.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "mozilla/glean/bindings/GleanJSMetricsLookup.h"
+#include "mozilla/glean/bindings/MetricTypes.h"
+#include "mozilla/UniquePtr.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+template <>
+BooleanMetric Labeled<BooleanMetric>::Get(const nsACString& aLabel) const {
+ auto submetricId = fog_labeled_boolean_get(mId, &aLabel);
+ // If this labeled metric is mirrored, we need to map the submetric id back
+ // to the label string and mirrored scalar so we can mirror its operations.
+ auto mirrorId = ScalarIdForMetric(mId);
+ if (mirrorId) {
+ GetLabeledMirrorLock().apply([&](auto& lock) {
+ auto tuple = MakeTuple<Telemetry::ScalarID, nsString>(
+ mirrorId.extract(), NS_ConvertUTF8toUTF16(aLabel));
+ lock.ref()->InsertOrUpdate(submetricId, std::move(tuple));
+ });
+ }
+ return BooleanMetric(submetricId);
+}
+
+template <>
+CounterMetric Labeled<CounterMetric>::Get(const nsACString& aLabel) const {
+ auto submetricId = fog_labeled_counter_get(mId, &aLabel);
+ // If this labeled metric is mirrored, we need to map the submetric id back
+ // to the label string and mirrored scalar so we can mirror its operations.
+ auto mirrorId = ScalarIdForMetric(mId);
+ if (mirrorId) {
+ GetLabeledMirrorLock().apply([&](auto& lock) {
+ auto tuple = MakeTuple<Telemetry::ScalarID, nsString>(
+ mirrorId.extract(), NS_ConvertUTF8toUTF16(aLabel));
+ lock.ref()->InsertOrUpdate(submetricId, std::move(tuple));
+ });
+ }
+ return CounterMetric(submetricId);
+}
+
+template <>
+StringMetric Labeled<StringMetric>::Get(const nsACString& aLabel) const {
+ auto submetricId = fog_labeled_string_get(mId, &aLabel);
+ // Why no GIFFT map here?
+ // Labeled Strings can't be mirrored. Telemetry has no compatible probe.
+ return StringMetric(submetricId);
+}
+} // namespace impl
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(GleanLabeled)
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(GleanLabeled)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(GleanLabeled)
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GleanLabeled)
+ NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+ NS_INTERFACE_MAP_ENTRY(nsISupports)
+NS_INTERFACE_MAP_END
+
+JSObject* GleanLabeled::WrapObject(JSContext* aCx,
+ JS::Handle<JSObject*> aGivenProto) {
+ return dom::GleanLabeled_Binding::Wrap(aCx, this, aGivenProto);
+}
+
+already_AddRefed<nsISupports> GleanLabeled::NamedGetter(const nsAString& aName,
+ bool& aFound) {
+ auto label = NS_ConvertUTF16toUTF8(aName);
+ aFound = true;
+ uint32_t submetricId = 0;
+ already_AddRefed<nsISupports> submetric =
+ NewSubMetricFromIds(mTypeId, mId, label, &submetricId);
+
+ auto mirrorId = ScalarIdForMetric(mId);
+ if (mirrorId) {
+ GetLabeledMirrorLock().apply([&](auto& lock) {
+ auto tuple = MakeTuple<Telemetry::ScalarID, nsString>(mirrorId.extract(),
+ nsString(aName));
+ lock.ref()->InsertOrUpdate(submetricId, std::move(tuple));
+ });
+ }
+ return submetric;
+}
+
+bool GleanLabeled::NameIsEnumerable(const nsAString& aName) { return false; }
+
+void GleanLabeled::GetSupportedNames(nsTArray<nsString>& aNames) {
+ // We really don't know, so don't do anything.
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Labeled.h b/toolkit/components/glean/bindings/private/Labeled.h
new file mode 100644
index 0000000000..be46b3b56e
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Labeled.h
@@ -0,0 +1,75 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_Labeled_h
+#define mozilla_glean_Labeled_h
+
+#include "nsIGleanMetrics.h"
+#include "nsISupports.h"
+#include "nsWrapperCache.h"
+#include "mozilla/dom/BindingDeclarations.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+template <typename T>
+class Labeled {
+ public:
+ constexpr explicit Labeled<T>(uint32_t id) : mId(id) {}
+
+ /**
+ * Gets a specific metric for a given label.
+ *
+ * If a set of acceptable labels were specified in the `metrics.yaml` file,
+ * and the given label is not in the set, it will be recorded under the
+ * special `OTHER_LABEL` label.
+ *
+ * If a set of acceptable labels was not specified in the `metrics.yaml` file,
+ * only the first 16 unique labels will be used.
+ * After that, any additional labels will be recorded under the special
+ * `OTHER_LABEL` label.
+ *
+ * @param aLabel - a snake_case string under 30 characters in length,
+ * otherwise the metric will be recorded under the special
+ * `OTHER_LABEL` label and an error will be recorded.
+ */
+ T Get(const nsACString& aLabel) const;
+
+ private:
+ const uint32_t mId;
+};
+
+} // namespace impl
+
+class GleanLabeled final : public nsISupports, public nsWrapperCache {
+ public:
+ NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+ NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(GleanLabeled)
+
+ JSObject* WrapObject(JSContext* aCx,
+ JS::Handle<JSObject*> aGivenProto) override;
+ nsISupports* GetParentObject() { return nullptr; }
+
+ explicit GleanLabeled(uint32_t aId, uint32_t aTypeId)
+ : mId(aId), mTypeId(aTypeId){};
+
+ already_AddRefed<nsISupports> NamedGetter(const nsAString& aName,
+ bool& aFound);
+ bool NameIsEnumerable(const nsAString& aName);
+ void GetSupportedNames(nsTArray<nsString>& aNames);
+
+ private:
+ virtual ~GleanLabeled() = default;
+
+ const uint32_t mId;
+ const uint32_t mTypeId;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_Labeled_h */
diff --git a/toolkit/components/glean/bindings/private/MemoryDistribution.cpp b/toolkit/components/glean/bindings/private/MemoryDistribution.cpp
new file mode 100644
index 0000000000..41899bf7c1
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/MemoryDistribution.cpp
@@ -0,0 +1,104 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/MemoryDistribution.h"
+
+#include "mozilla/Components.h"
+#include "mozilla/glean/bindings/HistogramGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsJSUtils.h"
+#include "nsPrintfCString.h"
+#include "nsString.h"
+#include "js/PropertyAndElement.h" // JS_DefineProperty
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void MemoryDistributionMetric::Accumulate(size_t aSample) const {
+ auto hgramId = HistogramIdForMetric(mId);
+ if (hgramId) {
+ Telemetry::Accumulate(hgramId.extract(), aSample);
+ }
+ static_assert(sizeof(size_t) <= sizeof(uint64_t),
+ "Memory distribution samples might overflow.");
+ fog_memory_distribution_accumulate(mId, aSample);
+}
+
+Result<Maybe<DistributionData>, nsCString>
+MemoryDistributionMetric::TestGetValue(const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_memory_distribution_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_memory_distribution_test_has_value(mId, &aPingName)) {
+ return Maybe<DistributionData>();
+ }
+ nsTArray<uint64_t> buckets;
+ nsTArray<uint64_t> counts;
+ uint64_t sum;
+ fog_memory_distribution_test_get_value(mId, &aPingName, &sum, &buckets,
+ &counts);
+ return Some(DistributionData(buckets, counts, sum));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanMemoryDistribution, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanMemoryDistribution, nsIGleanMemoryDistribution)
+
+NS_IMETHODIMP
+GleanMemoryDistribution::Accumulate(uint64_t aSample) {
+ mMemoryDist.Accumulate(aSample);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanMemoryDistribution::TestGetValue(const nsACString& aPingName,
+ JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mMemoryDist.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ // Build return value of the form:
+ // { sum: #, values: {bucket1: count1, ...} }
+ JS::Rooted<JSObject*> root(aCx, JS_NewPlainObject(aCx));
+ if (!root) {
+ return NS_ERROR_FAILURE;
+ }
+ uint64_t sum = optresult.ref().sum;
+ if (!JS_DefineProperty(aCx, root, "sum", static_cast<double>(sum),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ JS::Rooted<JSObject*> valuesObj(aCx, JS_NewPlainObject(aCx));
+ if (!valuesObj ||
+ !JS_DefineProperty(aCx, root, "values", valuesObj, JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ auto& data = optresult.ref().values;
+ for (const auto& entry : data) {
+ const uint64_t bucket = entry.GetKey();
+ const uint64_t count = entry.GetData();
+ if (!JS_DefineProperty(aCx, valuesObj,
+ nsPrintfCString("%" PRIu64, bucket).get(),
+ static_cast<double>(count), JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/MemoryDistribution.h b/toolkit/components/glean/bindings/private/MemoryDistribution.h
new file mode 100644
index 0000000000..136cdb6c91
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/MemoryDistribution.h
@@ -0,0 +1,74 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanMemoryDistribution_h
+#define mozilla_glean_GleanMemoryDistribution_h
+
+#include "mozilla/glean/bindings/DistributionData.h"
+#include "mozilla/Maybe.h"
+#include "nsIGleanMetrics.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class MemoryDistributionMetric {
+ public:
+ constexpr explicit MemoryDistributionMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Accumulates the provided sample in the metric.
+ *
+ * @param aSample The sample to be recorded by the metric. The sample is
+ * assumed to be in the confgured memory unit of the metric.
+ *
+ * Notes: Values bigger than 1 Terabyte (2^40 bytes) are truncated and an
+ * InvalidValue error is recorded.
+ */
+ void Accumulate(size_t aSample) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a DistributionData.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<DistributionData>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanMemoryDistribution final : public nsIGleanMemoryDistribution {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANMEMORYDISTRIBUTION
+
+ explicit GleanMemoryDistribution(uint64_t aId) : mMemoryDist(aId){};
+
+ private:
+ virtual ~GleanMemoryDistribution() = default;
+
+ const impl::MemoryDistributionMetric mMemoryDist;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanMemoryDistribution_h */
diff --git a/toolkit/components/glean/bindings/private/Numerator.cpp b/toolkit/components/glean/bindings/private/Numerator.cpp
new file mode 100644
index 0000000000..79a211773b
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Numerator.cpp
@@ -0,0 +1,89 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Numerator.h"
+
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void NumeratorMetric::AddToNumerator(int32_t aAmount) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId && aAmount >= 0) {
+ Telemetry::ScalarAdd(scalarId.extract(), u"numerator"_ns, aAmount);
+ }
+ fog_numerator_add_to_numerator(mId, aAmount);
+}
+
+Result<Maybe<std::pair<int32_t, int32_t>>, nsCString>
+NumeratorMetric::TestGetValue(const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_numerator_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_numerator_test_has_value(mId, &aPingName)) {
+ return Maybe<std::pair<int32_t, int32_t>>();
+ }
+ int32_t num = 0;
+ int32_t den = 0;
+ fog_numerator_test_get_value(mId, &aPingName, &num, &den);
+ return Some(std::make_pair(num, den));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanNumerator, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanNumerator, nsIGleanNumerator)
+
+NS_IMETHODIMP
+GleanNumerator::AddToNumerator(int32_t aAmount) {
+ mNumerator.AddToNumerator(aAmount);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanNumerator::TestGetValue(const nsACString& aPingName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mNumerator.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ // Build return value of the form: { numerator: n, denominator: d }
+ JS::Rooted<JSObject*> root(aCx, JS_NewPlainObject(aCx));
+ if (!root) {
+ return NS_ERROR_FAILURE;
+ }
+ auto pair = optresult.extract();
+ int32_t num = pair.first;
+ int32_t den = pair.second;
+ if (!JS_DefineProperty(aCx, root, "numerator", static_cast<double>(num),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ if (!JS_DefineProperty(aCx, root, "denominator", static_cast<double>(den),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Numerator.h b/toolkit/components/glean/bindings/private/Numerator.h
new file mode 100644
index 0000000000..66b7eef295
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Numerator.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanNumerator_h
+#define mozilla_glean_GleanNumerator_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+// Actually a RateMetric, but one whose denominator is a CounterMetric external
+// to the RateMetric.
+class NumeratorMetric {
+ public:
+ constexpr explicit NumeratorMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Increases the numerator by `amount`.
+ *
+ * @param aAmount The amount to increase by. Should be positive.
+ */
+ void AddToNumerator(int32_t aAmount = 1) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a pair of integers.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<std::pair<int32_t, int32_t>>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanNumerator final : public nsIGleanNumerator {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANNUMERATOR
+
+ explicit GleanNumerator(uint32_t id) : mNumerator(id){};
+
+ private:
+ virtual ~GleanNumerator() = default;
+
+ const impl::NumeratorMetric mNumerator;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanNumerator_h */
diff --git a/toolkit/components/glean/bindings/private/Ping.cpp b/toolkit/components/glean/bindings/private/Ping.cpp
new file mode 100644
index 0000000000..c341a61148
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Ping.cpp
@@ -0,0 +1,94 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Ping.h"
+
+#ifndef MOZ_GLEAN_ANDROID
+# include "mozilla/AppShutdown.h"
+# include "mozilla/ClearOnShutdown.h"
+#endif
+#include "mozilla/Components.h"
+#include "nsIClassInfoImpl.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+#ifndef MOZ_GLEAN_ANDROID
+using CallbackMapType = nsTHashMap<uint32_t, PingTestCallback>;
+using MetricIdToCallbackMutex = StaticDataMutex<UniquePtr<CallbackMapType>>;
+static Maybe<MetricIdToCallbackMutex::AutoLock> GetCallbackMapLock() {
+ static MetricIdToCallbackMutex sCallbacks("sCallbacks");
+ auto lock = sCallbacks.Lock();
+ // Test callbacks will continue to work until the end of AppShutdownTelemetry
+ if (AppShutdown::IsInOrBeyond(ShutdownPhase::XPCOMWillShutdown)) {
+ return Nothing();
+ }
+ if (!*lock) {
+ *lock = MakeUnique<CallbackMapType>();
+ RunOnShutdown(
+ [&] {
+ auto lock = sCallbacks.Lock();
+ *lock = nullptr; // deletes, see UniquePtr.h
+ },
+ ShutdownPhase::XPCOMWillShutdown);
+ }
+ return Some(std::move(lock));
+}
+#endif
+
+void Ping::Submit(const nsACString& aReason) const {
+#ifdef MOZ_GLEAN_ANDROID
+ Unused << mId;
+#else
+ {
+ GetCallbackMapLock().apply([&](auto& lock) {
+ auto callback = lock.ref()->Extract(mId);
+ if (callback) {
+ callback.extract()(aReason);
+ }
+ });
+ }
+ fog_submit_ping_by_id(mId, &aReason);
+#endif
+}
+
+void Ping::TestBeforeNextSubmit(PingTestCallback&& aCallback) const {
+#ifdef MOZ_GLEAN_ANDROID
+ return;
+#else
+ {
+ GetCallbackMapLock().apply(
+ [&](auto& lock) { lock.ref()->InsertOrUpdate(mId, aCallback); });
+ }
+#endif
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanPing, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanPing, nsIGleanPing)
+
+NS_IMETHODIMP
+GleanPing::Submit(const nsACString& aReason) {
+ mPing.Submit(aReason);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanPing::TestBeforeNextSubmit(nsIGleanPingTestCallback* aCallback) {
+ if (NS_WARN_IF(!aCallback)) {
+ return NS_ERROR_INVALID_ARG;
+ }
+ // Throw the bare ptr into a COM ptr to keep it around in the lambda.
+ nsCOMPtr<nsIGleanPingTestCallback> callback = aCallback;
+ mPing.TestBeforeNextSubmit(
+ [callback](const nsACString& aReason) { callback->Call(aReason); });
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Ping.h b/toolkit/components/glean/bindings/private/Ping.h
new file mode 100644
index 0000000000..6bcd4cb478
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Ping.h
@@ -0,0 +1,83 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_Ping_h
+#define mozilla_glean_Ping_h
+
+#include "mozilla/DataMutex.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "mozilla/Maybe.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+typedef std::function<void(const nsACString& aReason)> PingTestCallback;
+
+namespace impl {
+
+class Ping {
+ public:
+ constexpr explicit Ping(uint32_t aId) : mId(aId) {}
+
+ /**
+ * Collect and submit the ping for eventual upload.
+ *
+ * This will collect all stored data to be included in the ping.
+ * Data with lifetime `ping` will then be reset.
+ *
+ * If the ping is configured with `send_if_empty = false`
+ * and the ping currently contains no content,
+ * it will not be queued for upload.
+ * If the ping is configured with `send_if_empty = true`
+ * it will be queued for upload even if empty.
+ *
+ * Pings always contain the `ping_info` and `client_info` sections.
+ * See [ping
+ * sections](https://mozilla.github.io/glean/book/user/pings/index.html#ping-sections)
+ * for details.
+ *
+ * @param aReason - Optional. The reason the ping is being submitted.
+ * Must match one of the configured `reason_codes`.
+ */
+ void Submit(const nsACString& aReason = nsCString()) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Register a callback to be called right before this ping is next submitted.
+ * The provided function is called exactly once before submitting.
+ *
+ * Note: The callback will be called on any call to submit.
+ * A ping may not be sent afterwards, e.g. if the ping is empty and
+ * `send_if_empty` is `false`
+ *
+ * @param aCallback - The callback to call on the next submit.
+ */
+ void TestBeforeNextSubmit(PingTestCallback&& aCallback) const;
+
+ private:
+ const uint32_t mId;
+};
+
+} // namespace impl
+
+class GleanPing final : public nsIGleanPing {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANPING
+
+ explicit GleanPing(uint32_t aId) : mPing(aId) {}
+
+ private:
+ virtual ~GleanPing() = default;
+
+ const impl::Ping mPing;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_Ping_h */
diff --git a/toolkit/components/glean/bindings/private/Quantity.cpp b/toolkit/components/glean/bindings/private/Quantity.cpp
new file mode 100644
index 0000000000..62591e6c90
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Quantity.cpp
@@ -0,0 +1,74 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Quantity.h"
+
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void QuantityMetric::Set(int64_t aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId && aValue >= 0) {
+ uint32_t theValue = static_cast<uint32_t>(aValue);
+ if (aValue > std::numeric_limits<uint32_t>::max()) {
+ theValue = std::numeric_limits<uint32_t>::max();
+ }
+ Telemetry::ScalarSet(scalarId.extract(), theValue);
+ }
+ fog_quantity_set(mId, aValue);
+}
+
+Result<Maybe<int64_t>, nsCString> QuantityMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_quantity_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_quantity_test_has_value(mId, &aPingName)) {
+ return Maybe<int64_t>();
+ }
+ return Some(fog_quantity_test_get_value(mId, &aPingName));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanQuantity, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanQuantity, nsIGleanQuantity)
+
+NS_IMETHODIMP
+GleanQuantity::Set(int64_t aValue) {
+ mQuantity.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanQuantity::TestGetValue(const nsACString& aPingName,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mQuantity.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ aResult.set(JS::DoubleValue(static_cast<double>(optresult.value())));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Quantity.h b/toolkit/components/glean/bindings/private/Quantity.h
new file mode 100644
index 0000000000..9de1007d10
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Quantity.h
@@ -0,0 +1,68 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanQuantity_h
+#define mozilla_glean_GleanQuantity_h
+
+#include "nsIGleanMetrics.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class QuantityMetric {
+ public:
+ constexpr explicit QuantityMetric(uint32_t id) : mId(id) {}
+
+ /**
+ * Set to the specified value.
+ *
+ * @param aValue the value to set.
+ */
+ void Set(int64_t aValue) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric.
+ */
+ Result<Maybe<int64_t>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+
+} // namespace impl
+
+class GleanQuantity final : public nsIGleanQuantity {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANQUANTITY
+
+ explicit GleanQuantity(uint32_t id) : mQuantity(id){};
+
+ private:
+ virtual ~GleanQuantity() = default;
+
+ const impl::QuantityMetric mQuantity;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanQuantity.h */
diff --git a/toolkit/components/glean/bindings/private/Rate.cpp b/toolkit/components/glean/bindings/private/Rate.cpp
new file mode 100644
index 0000000000..6bc6ad61c9
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Rate.cpp
@@ -0,0 +1,105 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Rate.h"
+
+#include "jsapi.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/Common.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void RateMetric::AddToNumerator(int32_t aAmount) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId && aAmount >= 0) {
+ Telemetry::ScalarAdd(scalarId.extract(), u"numerator"_ns, aAmount);
+ }
+ fog_rate_add_to_numerator(mId, aAmount);
+}
+
+void RateMetric::AddToDenominator(int32_t aAmount) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId && aAmount >= 0) {
+ Telemetry::ScalarAdd(scalarId.extract(), u"denominator"_ns, aAmount);
+ }
+ fog_rate_add_to_denominator(mId, aAmount);
+}
+
+Result<Maybe<std::pair<int32_t, int32_t>>, nsCString> RateMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_rate_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_rate_test_has_value(mId, &aPingName)) {
+ return Maybe<std::pair<int32_t, int32_t>>();
+ }
+ int32_t num = 0;
+ int32_t den = 0;
+ fog_rate_test_get_value(mId, &aPingName, &num, &den);
+ return Some(std::make_pair(num, den));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanRate, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanRate, nsIGleanRate)
+
+NS_IMETHODIMP
+GleanRate::AddToNumerator(int32_t aAmount) {
+ mRate.AddToNumerator(aAmount);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanRate::AddToDenominator(int32_t aAmount) {
+ mRate.AddToDenominator(aAmount);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanRate::TestGetValue(const nsACString& aPingName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mRate.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ // Build return value of the form: { numerator: n, denominator: d }
+ JS::Rooted<JSObject*> root(aCx, JS_NewPlainObject(aCx));
+ if (!root) {
+ return NS_ERROR_FAILURE;
+ }
+ auto pair = optresult.extract();
+ int32_t num = pair.first;
+ int32_t den = pair.second;
+ if (!JS_DefineProperty(aCx, root, "numerator", static_cast<double>(num),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ if (!JS_DefineProperty(aCx, root, "denominator", static_cast<double>(den),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Rate.h b/toolkit/components/glean/bindings/private/Rate.h
new file mode 100644
index 0000000000..85e8b4e1d6
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Rate.h
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanRate_h
+#define mozilla_glean_GleanRate_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class RateMetric {
+ public:
+ constexpr explicit RateMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Increases the numerator by `amount`.
+ *
+ * @param aAmount The amount to increase by. Should be positive.
+ */
+ void AddToNumerator(int32_t aAmount = 1) const;
+
+ /*
+ * Increases the denominator by `amount`.
+ *
+ * @param aAmount The amount to increase by. Should be positive.
+ */
+ void AddToDenominator(int32_t aAmount = 1) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a pair of integers.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<std::pair<int32_t, int32_t>>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanRate final : public nsIGleanRate {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANRATE
+
+ explicit GleanRate(uint32_t id) : mRate(id){};
+
+ private:
+ virtual ~GleanRate() = default;
+
+ const impl::RateMetric mRate;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanRate_h */
diff --git a/toolkit/components/glean/bindings/private/String.cpp b/toolkit/components/glean/bindings/private/String.cpp
new file mode 100644
index 0000000000..bc191a7400
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/String.cpp
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/String.h"
+
+#include "Common.h"
+#include "jsapi.h"
+#include "js/String.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void StringMetric::Set(const nsACString& aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId) {
+ Telemetry::ScalarSet(scalarId.extract(), NS_ConvertUTF8toUTF16(aValue));
+ }
+ fog_string_set(mId, &aValue);
+}
+
+Result<Maybe<nsCString>, nsCString> StringMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_string_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_string_test_has_value(mId, &aPingName)) {
+ return Maybe<nsCString>();
+ }
+ nsCString ret;
+ fog_string_test_get_value(mId, &aPingName, &ret);
+ return Some(ret);
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanString, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanString, nsIGleanString)
+
+NS_IMETHODIMP
+GleanString::Set(const nsACString& aValue) {
+ mString.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanString::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mString.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ const NS_ConvertUTF8toUTF16 str(optresult.ref());
+ aResult.set(
+ JS::StringValue(JS_NewUCStringCopyN(aCx, str.Data(), str.Length())));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/String.h b/toolkit/components/glean/bindings/private/String.h
new file mode 100644
index 0000000000..8c3c6ae05e
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/String.h
@@ -0,0 +1,73 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanString_h
+#define mozilla_glean_GleanString_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class StringMetric {
+ public:
+ constexpr explicit StringMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Set to the specified value.
+ *
+ * Truncates the value if it is longer than 100 bytes and logs an error.
+ * See https://mozilla.github.io/glean/book/user/metrics/string.html#limits.
+ *
+ * @param aValue The string to set the metric to.
+ */
+ void Set(const nsACString& aValue) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a string.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<nsCString>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanString final : public nsIGleanString {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANSTRING
+
+ explicit GleanString(uint32_t aId) : mString(aId){};
+
+ private:
+ virtual ~GleanString() = default;
+
+ const impl::StringMetric mString;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanString_h */
diff --git a/toolkit/components/glean/bindings/private/StringList.cpp b/toolkit/components/glean/bindings/private/StringList.cpp
new file mode 100644
index 0000000000..8882922551
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/StringList.cpp
@@ -0,0 +1,92 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/StringList.h"
+
+#include "Common.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/dom/ToJSValue.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsString.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void StringListMetric::Add(const nsACString& aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId) {
+ Telemetry::ScalarSet(scalarId.extract(), NS_ConvertUTF8toUTF16(aValue),
+ true);
+ }
+ fog_string_list_add(mId, &aValue);
+}
+
+void StringListMetric::Set(const nsTArray<nsCString>& aValue) const {
+ // Calling `Set` on a mirrored labeled_string is likely an error.
+ // We can't remove keys from the mirror scalar and handle this 'properly',
+ // so you shouldn't use this operation at all.
+ (void)NS_WARN_IF(ScalarIdForMetric(mId).isSome());
+ fog_string_list_set(mId, &aValue);
+}
+
+Result<Maybe<nsTArray<nsCString>>, nsCString> StringListMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_string_list_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_string_list_test_has_value(mId, &aPingName)) {
+ return Maybe<nsTArray<nsCString>>();
+ }
+ nsTArray<nsCString> ret;
+ fog_string_list_test_get_value(mId, &aPingName, &ret);
+ return Some(std::move(ret));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanStringList, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanStringList, nsIGleanStringList)
+
+NS_IMETHODIMP
+GleanStringList::Add(const nsACString& aValue) {
+ mStringList.Add(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanStringList::Set(const nsTArray<nsCString>& aValue) {
+ mStringList.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanStringList::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mStringList.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ if (!dom::ToJSValue(aCx, optresult.ref(), aResult)) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/StringList.h b/toolkit/components/glean/bindings/private/StringList.h
new file mode 100644
index 0000000000..6b7b9358e2
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/StringList.h
@@ -0,0 +1,84 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanStringList_h
+#define mozilla_glean_GleanStringList_h
+
+#include "mozilla/Maybe.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class StringListMetric {
+ public:
+ constexpr explicit StringListMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Adds a new string to the list.
+ *
+ * Truncates the value if it is longer than 50 bytes and logs an error.
+ *
+ * @param aValue The string to add.
+ */
+ void Add(const nsACString& aValue) const;
+
+ /*
+ * Set to a specific list of strings.
+ *
+ * Truncates any values longer than 50 bytes and logs an error.
+ * Truncates the list if it is over 20 items long.
+ * See
+ * https://mozilla.github.io/glean/book/user/metrics/string_list.html#limits.
+ *
+ * @param aValue The list of strings to set the metric to.
+ */
+ void Set(const nsTArray<nsCString>& aValue) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<nsTArray<nsCString>>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanStringList final : public nsIGleanStringList {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANSTRINGLIST
+
+ explicit GleanStringList(uint32_t aId) : mStringList(aId){};
+
+ private:
+ virtual ~GleanStringList() = default;
+
+ const impl::StringListMetric mStringList;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanStringList_h */
diff --git a/toolkit/components/glean/bindings/private/Timespan.cpp b/toolkit/components/glean/bindings/private/Timespan.cpp
new file mode 100644
index 0000000000..e8377c8fcf
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Timespan.cpp
@@ -0,0 +1,133 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Timespan.h"
+
+#include "Common.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void TimespanMetric::Start() const {
+ auto optScalarId = ScalarIdForMetric(mId);
+ if (optScalarId) {
+ auto scalarId = optScalarId.extract();
+ GetTimesToStartsLock().apply([&](auto& lock) {
+ (void)NS_WARN_IF(lock.ref()->Remove(scalarId));
+ lock.ref()->InsertOrUpdate(scalarId, TimeStamp::Now());
+ });
+ }
+ fog_timespan_start(mId);
+}
+
+void TimespanMetric::Stop() const {
+ auto optScalarId = ScalarIdForMetric(mId);
+ if (optScalarId) {
+ auto scalarId = optScalarId.extract();
+ GetTimesToStartsLock().apply([&](auto& lock) {
+ auto optStart = lock.ref()->Extract(scalarId);
+ if (!NS_WARN_IF(!optStart)) {
+ double delta = (TimeStamp::Now() - optStart.extract()).ToMilliseconds();
+ uint32_t theDelta = static_cast<uint32_t>(delta);
+ if (delta > std::numeric_limits<uint32_t>::max()) {
+ theDelta = std::numeric_limits<uint32_t>::max();
+ } else if (MOZ_UNLIKELY(delta < 0)) {
+ theDelta = 0;
+ }
+ Telemetry::ScalarSet(scalarId, theDelta);
+ }
+ });
+ }
+ fog_timespan_stop(mId);
+}
+
+void TimespanMetric::Cancel() const {
+ auto optScalarId = ScalarIdForMetric(mId);
+ if (optScalarId) {
+ auto scalarId = optScalarId.extract();
+ GetTimesToStartsLock().apply(
+ [&](auto& lock) { lock.ref()->Remove(scalarId); });
+ }
+ fog_timespan_cancel(mId);
+}
+
+void TimespanMetric::SetRaw(uint32_t aDuration) const {
+ auto optScalarId = ScalarIdForMetric(mId);
+ if (optScalarId) {
+ auto scalarId = optScalarId.extract();
+ Telemetry::ScalarSet(scalarId, aDuration);
+ }
+ fog_timespan_set_raw(mId, aDuration);
+}
+
+Result<Maybe<uint64_t>, nsCString> TimespanMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_timespan_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_timespan_test_has_value(mId, &aPingName)) {
+ return Maybe<uint64_t>();
+ }
+ return Some(fog_timespan_test_get_value(mId, &aPingName));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanTimespan, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanTimespan, nsIGleanTimespan)
+
+NS_IMETHODIMP
+GleanTimespan::Start() {
+ mTimespan.Start();
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimespan::Stop() {
+ mTimespan.Stop();
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimespan::Cancel() {
+ mTimespan.Cancel();
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimespan::SetRaw(uint32_t aDuration) {
+ mTimespan.SetRaw(aDuration);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimespan::TestGetValue(const nsACString& aStorageName,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mTimespan.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ aResult.set(JS::DoubleValue(static_cast<double>(optresult.value())));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Timespan.h b/toolkit/components/glean/bindings/private/Timespan.h
new file mode 100644
index 0000000000..b82a174034
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Timespan.h
@@ -0,0 +1,99 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanTimespan_h
+#define mozilla_glean_GleanTimespan_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class TimespanMetric {
+ public:
+ constexpr explicit TimespanMetric(uint32_t aId) : mId(aId) {}
+
+ /**
+ * Start tracking time for the provided metric.
+ *
+ * This records an error if it’s already tracking time (i.e. start was already
+ * called with no corresponding [stop]): in that case the original
+ * start time will be preserved.
+ */
+ void Start() const;
+
+ /**
+ * Stop tracking time for the provided metric.
+ *
+ * Sets the metric to the elapsed time, but does not overwrite an already
+ * existing value.
+ * This will record an error if no [start] was called or there is an already
+ * existing value.
+ */
+ void Stop() const;
+
+ /**
+ * Abort a previous Start.
+ *
+ * No error will be recorded if no Start was called.
+ */
+ void Cancel() const;
+
+ /**
+ * Explicitly sets the timespan value
+ *
+ * This API should only be used if you cannot make use of
+ * `start`/`stop`/`cancel`.
+ *
+ * @param aDuration The duration of this timespan, in units matching the
+ * `time_unit` of this metric's definition.
+ */
+ void SetRaw(uint32_t aDuration) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as an integer.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<uint64_t>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanTimespan final : public nsIGleanTimespan {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANTIMESPAN
+
+ explicit GleanTimespan(uint32_t aId) : mTimespan(aId){};
+
+ private:
+ virtual ~GleanTimespan() = default;
+
+ const impl::TimespanMetric mTimespan;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanTimespan_h */
diff --git a/toolkit/components/glean/bindings/private/TimingDistribution.cpp b/toolkit/components/glean/bindings/private/TimingDistribution.cpp
new file mode 100644
index 0000000000..7ec20f94b7
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/TimingDistribution.cpp
@@ -0,0 +1,196 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/TimingDistribution.h"
+
+#include "Common.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/Tuple.h"
+#include "mozilla/dom/ToJSValue.h"
+#include "mozilla/glean/bindings/HistogramGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsJSUtils.h"
+#include "nsPrintfCString.h"
+#include "nsString.h"
+#include "js/PropertyAndElement.h" // JS_DefineProperty
+
+// Called from within FOG's Rust impl.
+extern "C" NS_EXPORT void GIFFT_TimingDistributionStart(
+ uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
+ auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
+ if (mirrorId) {
+ mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) {
+ auto tuple = mozilla::MakeTuple(aMetricId, aTimerId);
+ // It should be all but impossible for anyone to have already inserted
+ // this timer for this metric given the monotonicity of timer ids.
+ (void)NS_WARN_IF(lock.ref()->Remove(tuple));
+ lock.ref()->InsertOrUpdate(tuple, mozilla::TimeStamp::Now());
+ });
+ }
+}
+
+// Called from within FOG's Rust impl.
+extern "C" NS_EXPORT void GIFFT_TimingDistributionStopAndAccumulate(
+ uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
+ auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
+ if (mirrorId) {
+ mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) {
+ auto optStart =
+ lock.ref()->Extract(mozilla::MakeTuple(aMetricId, aTimerId));
+ // The timer might not be in the map to be removed if it's already been
+ // cancelled or stop_and_accumulate'd.
+ if (!NS_WARN_IF(!optStart)) {
+ AccumulateTimeDelta(mirrorId.extract(), optStart.extract());
+ }
+ });
+ }
+}
+
+// Called from within FOG's Rust impl.
+extern "C" NS_EXPORT void GIFFT_TimingDistributionAccumulateRawMillis(
+ uint32_t aMetricId, uint32_t aMS) {
+ auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
+ if (mirrorId) {
+ Accumulate(mirrorId.extract(), aMS);
+ }
+}
+
+// Called from within FOG's Rust impl.
+extern "C" NS_EXPORT void GIFFT_TimingDistributionCancel(
+ uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
+ auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
+ if (mirrorId) {
+ mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) {
+ // The timer might not be in the map to be removed if it's already been
+ // cancelled or stop_and_accumulate'd.
+ (void)NS_WARN_IF(
+ !lock.ref()->Remove(mozilla::MakeTuple(aMetricId, aTimerId)));
+ });
+ }
+}
+
+namespace mozilla::glean {
+
+namespace impl {
+
+TimerId TimingDistributionMetric::Start() const {
+ return fog_timing_distribution_start(mId);
+}
+
+void TimingDistributionMetric::StopAndAccumulate(const TimerId&& aId) const {
+ fog_timing_distribution_stop_and_accumulate(mId, aId);
+}
+
+// Intentionally not exposed to JS for lack of use case and a time duration
+// type.
+void TimingDistributionMetric::AccumulateRawDuration(
+ const TimeDuration& aDuration) const {
+ fog_timing_distribution_accumulate_raw_nanos(
+ mId, uint64_t(aDuration.ToMicroseconds() * 1000.00));
+}
+
+void TimingDistributionMetric::Cancel(const TimerId&& aId) const {
+ fog_timing_distribution_cancel(mId, aId);
+}
+
+Result<Maybe<DistributionData>, nsCString>
+TimingDistributionMetric::TestGetValue(const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_timing_distribution_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_timing_distribution_test_has_value(mId, &aPingName)) {
+ return Maybe<DistributionData>();
+ }
+ nsTArray<uint64_t> buckets;
+ nsTArray<uint64_t> counts;
+ uint64_t sum;
+ fog_timing_distribution_test_get_value(mId, &aPingName, &sum, &buckets,
+ &counts);
+ return Some(DistributionData(buckets, counts, sum));
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanTimingDistribution, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanTimingDistribution, nsIGleanTimingDistribution)
+
+NS_IMETHODIMP
+GleanTimingDistribution::Start(JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ if (!dom::ToJSValue(aCx, mTimingDist.Start(), aResult)) {
+ return NS_ERROR_FAILURE;
+ }
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimingDistribution::StopAndAccumulate(uint64_t aId) {
+ mTimingDist.StopAndAccumulate(std::move(aId));
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimingDistribution::Cancel(uint64_t aId) {
+ mTimingDist.Cancel(std::move(aId));
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimingDistribution::TestGetValue(const nsACString& aPingName,
+ JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mTimingDist.TestGetValue(aPingName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ // Build return value of the form: { sum: #, values: {bucket1: count1,
+ // ...}
+ JS::Rooted<JSObject*> root(aCx, JS_NewPlainObject(aCx));
+ if (!root) {
+ return NS_ERROR_FAILURE;
+ }
+ uint64_t sum = optresult.ref().sum;
+ if (!JS_DefineProperty(aCx, root, "sum", static_cast<double>(sum),
+ JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ JS::Rooted<JSObject*> valuesObj(aCx, JS_NewPlainObject(aCx));
+ if (!valuesObj ||
+ !JS_DefineProperty(aCx, root, "values", valuesObj, JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ auto& data = optresult.ref().values;
+ for (const auto& entry : data) {
+ const uint64_t bucket = entry.GetKey();
+ const uint64_t count = entry.GetData();
+ if (!JS_DefineProperty(aCx, valuesObj,
+ nsPrintfCString("%" PRIu64, bucket).get(),
+ static_cast<double>(count), JSPROP_ENUMERATE)) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+ aResult.setObject(*root);
+ }
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanTimingDistribution::TestAccumulateRawMillis(uint64_t aSample) {
+ mTimingDist.AccumulateRawDuration(TimeDuration::FromMilliseconds(aSample));
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/TimingDistribution.h b/toolkit/components/glean/bindings/private/TimingDistribution.h
new file mode 100644
index 0000000000..7c2e69d89e
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/TimingDistribution.h
@@ -0,0 +1,109 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanTimingDistribution_h
+#define mozilla_glean_GleanTimingDistribution_h
+
+#include "mozilla/glean/bindings/DistributionData.h"
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "mozilla/TimeStamp.h"
+#include "nsIGleanMetrics.h"
+#include "nsTArray.h"
+
+namespace mozilla::glean {
+
+typedef uint64_t TimerId;
+
+namespace impl {
+class TimingDistributionMetric {
+ public:
+ constexpr explicit TimingDistributionMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Starts tracking time for the provided metric.
+ *
+ * @returns A unique TimerId for the new timer
+ */
+ TimerId Start() const;
+
+ /*
+ * Stops tracking time for the provided metric and associated timer id.
+ *
+ * Adds a count to the corresponding bucket in the timing distribution.
+ * This will record an error if no `Start` was called on this TimerId or
+ * if this TimerId was used to call `Cancel`.
+ *
+ * @param aId The TimerId to associate with this timing. This allows for
+ * concurrent timing of events associated with different ids.
+ */
+ void StopAndAccumulate(const TimerId&& aId) const;
+
+ /*
+ * Adds a duration sample to a timing distribution metric.
+ *
+ * Adds a count to the corresponding bucket in the timing distribution.
+ * Prefer Start() and StopAndAccumulate() where possible.
+ * Users of this API are responsible for ensuring the timing source used
+ * to calculate the TimeDuration is monotonic and consistent accross
+ * platforms.
+ *
+ * NOTE: Negative durations are not handled and will saturate to INT64_MAX
+ * nanoseconds.
+ *
+ * @param aDuration The duration of the sample to add to the distribution.
+ */
+ void AccumulateRawDuration(const TimeDuration& aDuration) const;
+
+ /*
+ * Aborts a previous `Start` call. No error is recorded if no `Start` was
+ * called.
+ *
+ * @param aId The TimerId whose `Start` you wish to abort.
+ */
+ void Cancel(const TimerId&& aId) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a DistributionData.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<DistributionData>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanTimingDistribution final : public nsIGleanTimingDistribution {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANTIMINGDISTRIBUTION
+
+ explicit GleanTimingDistribution(uint64_t aId) : mTimingDist(aId){};
+
+ private:
+ virtual ~GleanTimingDistribution() = default;
+
+ const impl::TimingDistributionMetric mTimingDist;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanTimingDistribution_h */
diff --git a/toolkit/components/glean/bindings/private/Url.cpp b/toolkit/components/glean/bindings/private/Url.cpp
new file mode 100644
index 0000000000..b86c724e17
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Url.cpp
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Url.h"
+
+#include "Common.h"
+#include "jsapi.h"
+#include "js/String.h"
+#include "nsString.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void UrlMetric::Set(const nsACString& aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId) {
+ Telemetry::ScalarSet(scalarId.extract(), NS_ConvertUTF8toUTF16(aValue));
+ }
+ fog_url_set(mId, &aValue);
+}
+
+Result<Maybe<nsCString>, nsCString> UrlMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_url_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_url_test_has_value(mId, &aPingName)) {
+ return Maybe<nsCString>();
+ }
+ nsCString ret;
+ fog_url_test_get_value(mId, &aPingName, &ret);
+ return Some(ret);
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanUrl, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanUrl, nsIGleanUrl)
+
+NS_IMETHODIMP
+GleanUrl::Set(const nsACString& aValue) {
+ mUrl.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanUrl::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mUrl.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ const NS_ConvertUTF8toUTF16 str(optresult.ref());
+ aResult.set(
+ JS::StringValue(JS_NewUCStringCopyN(aCx, str.Data(), str.Length())));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Url.h b/toolkit/components/glean/bindings/private/Url.h
new file mode 100644
index 0000000000..115eb073f5
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Url.h
@@ -0,0 +1,70 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanUrl_h
+#define mozilla_glean_GleanUrl_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class UrlMetric {
+ public:
+ constexpr explicit UrlMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Set to the specified value.
+ *
+ * @param aValue The stringified Url to set the metric to.
+ */
+ void Set(const nsACString& aValue) const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a string.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<nsCString>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanUrl final : public nsIGleanUrl {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANURL
+
+ explicit GleanUrl(uint32_t aId) : mUrl(aId){};
+
+ private:
+ virtual ~GleanUrl() = default;
+
+ const impl::UrlMetric mUrl;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanUrl_h */
diff --git a/toolkit/components/glean/bindings/private/Uuid.cpp b/toolkit/components/glean/bindings/private/Uuid.cpp
new file mode 100644
index 0000000000..205dc94ec7
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Uuid.cpp
@@ -0,0 +1,89 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/glean/bindings/Uuid.h"
+
+#include "Common.h"
+#include "jsapi.h"
+#include "mozilla/Components.h"
+#include "mozilla/ResultVariant.h"
+#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
+#include "mozilla/glean/fog_ffi_generated.h"
+#include "nsIClassInfoImpl.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+void UuidMetric::Set(const nsACString& aValue) const {
+ auto scalarId = ScalarIdForMetric(mId);
+ if (scalarId) {
+ Telemetry::ScalarSet(scalarId.extract(), NS_ConvertUTF8toUTF16(aValue));
+ }
+ fog_uuid_set(mId, &aValue);
+}
+
+void UuidMetric::GenerateAndSet() const {
+ // We don't have the generated value to mirror to the scalar,
+ // so calling this function on a mirrored metric is likely an error.
+ (void)NS_WARN_IF(ScalarIdForMetric(mId).isSome());
+ fog_uuid_generate_and_set(mId);
+}
+
+Result<Maybe<nsCString>, nsCString> UuidMetric::TestGetValue(
+ const nsACString& aPingName) const {
+ nsCString err;
+ if (fog_uuid_test_get_error(mId, &err)) {
+ return Err(err);
+ }
+ if (!fog_uuid_test_has_value(mId, &aPingName)) {
+ return Maybe<nsCString>();
+ }
+ nsCString ret;
+ fog_uuid_test_get_value(mId, &aPingName, &ret);
+ return Some(ret);
+}
+
+} // namespace impl
+
+NS_IMPL_CLASSINFO(GleanUuid, nullptr, 0, {0})
+NS_IMPL_ISUPPORTS_CI(GleanUuid, nsIGleanUuid)
+
+NS_IMETHODIMP
+GleanUuid::Set(const nsACString& aValue) {
+ mUuid.Set(aValue);
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanUuid::GenerateAndSet() {
+ mUuid.GenerateAndSet();
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+GleanUuid::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
+ JS::MutableHandle<JS::Value> aResult) {
+ auto result = mUuid.TestGetValue(aStorageName);
+ if (result.isErr()) {
+ aResult.set(JS::UndefinedValue());
+ LogToBrowserConsole(nsIScriptError::errorFlag,
+ NS_ConvertUTF8toUTF16(result.unwrapErr()));
+ return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
+ }
+ auto optresult = result.unwrap();
+ if (optresult.isNothing()) {
+ aResult.set(JS::UndefinedValue());
+ } else {
+ const NS_ConvertUTF8toUTF16 str(optresult.value());
+ aResult.set(
+ JS::StringValue(JS_NewUCStringCopyN(aCx, str.Data(), str.Length())));
+ }
+ return NS_OK;
+}
+
+} // namespace mozilla::glean
diff --git a/toolkit/components/glean/bindings/private/Uuid.h b/toolkit/components/glean/bindings/private/Uuid.h
new file mode 100644
index 0000000000..941ce42540
--- /dev/null
+++ b/toolkit/components/glean/bindings/private/Uuid.h
@@ -0,0 +1,75 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_glean_GleanUuid_h
+#define mozilla_glean_GleanUuid_h
+
+#include "mozilla/Maybe.h"
+#include "mozilla/Result.h"
+#include "nsIGleanMetrics.h"
+#include "nsString.h"
+
+namespace mozilla::glean {
+
+namespace impl {
+
+class UuidMetric {
+ public:
+ constexpr explicit UuidMetric(uint32_t aId) : mId(aId) {}
+
+ /*
+ * Sets to the specified value.
+ *
+ * @param aValue The UUID to set the metric to.
+ */
+ void Set(const nsACString& aValue) const;
+
+ /*
+ * Generate a new random UUID and set the metric to it.
+ */
+ void GenerateAndSet() const;
+
+ /**
+ * **Test-only API**
+ *
+ * Gets the currently stored value as a hyphenated string.
+ *
+ * This function will attempt to await the last parent-process task (if any)
+ * writing to the the metric's storage engine before returning a value.
+ * This function will not wait for data from child processes.
+ *
+ * This doesn't clear the stored value.
+ * Parent process only. Panics in child processes.
+ *
+ * @param aPingName The (optional) name of the ping to retrieve the metric
+ * for. Defaults to the first value in `send_in_pings`.
+ *
+ * @return value of the stored metric, or Nothing() if there is no value.
+ */
+ Result<Maybe<nsCString>, nsCString> TestGetValue(
+ const nsACString& aPingName = nsCString()) const;
+
+ private:
+ const uint32_t mId;
+};
+} // namespace impl
+
+class GleanUuid final : public nsIGleanUuid {
+ public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSIGLEANUUID
+
+ explicit GleanUuid(uint32_t aId) : mUuid(aId){};
+
+ private:
+ virtual ~GleanUuid() = default;
+
+ const impl::UuidMetric mUuid;
+};
+
+} // namespace mozilla::glean
+
+#endif /* mozilla_glean_GleanUuid_h */