Bug 1735628 - Expose setExperiment(In)Active on the FOG JS global r=TravisLong

Depends on D128926

Differential Revision: https://phabricator.services.mozilla.com/D128927
This commit is contained in:
Chris H-C 2021-10-19 20:32:38 +00:00
Родитель c1db31f7b1
Коммит c0766b0829
3 изменённых файлов: 101 добавлений и 0 удалений

Просмотреть файл

@ -31,6 +31,7 @@ EXPORTS.mozilla.glean.bindings += [
"bindings/GleanPings.h", "bindings/GleanPings.h",
"bindings/MetricTypes.h", "bindings/MetricTypes.h",
"bindings/private/Boolean.h", "bindings/private/Boolean.h",
"bindings/private/Common.h",
"bindings/private/Counter.h", "bindings/private/Counter.h",
"bindings/private/CustomDistribution.h", "bindings/private/CustomDistribution.h",
"bindings/private/Datetime.h", "bindings/private/Datetime.h",

Просмотреть файл

@ -9,6 +9,7 @@
#include "mozilla/ClearOnShutdown.h" #include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/Promise.h" #include "mozilla/dom/Promise.h"
#include "mozilla/FOGIPC.h" #include "mozilla/FOGIPC.h"
#include "mozilla/glean/bindings/Common.h"
#include "mozilla/glean/fog_ffi_generated.h" #include "mozilla/glean/fog_ffi_generated.h"
#include "mozilla/glean/GleanMetrics.h" #include "mozilla/glean/GleanMetrics.h"
#include "mozilla/MozPromise.h" #include "mozilla/MozPromise.h"
@ -21,6 +22,8 @@
namespace mozilla { namespace mozilla {
using glean::LogToBrowserConsole;
#ifdef MOZ_GLEAN_ANDROID #ifdef MOZ_GLEAN_ANDROID
// Defined by `glean_ffi`. We reexport it here for later use. // Defined by `glean_ffi`. We reexport it here for later use.
extern "C" NS_EXPORT void glean_enable_logging(void); extern "C" NS_EXPORT void glean_enable_logging(void);
@ -120,6 +123,76 @@ FOG::SendPing(const nsACString& aPingName) {
#endif #endif
} }
NS_IMETHODIMP
FOG::SetExperimentActive(const nsACString& aExperimentId,
const nsACString& aBranch, JS::HandleValue aExtra,
JSContext* aCx) {
#ifdef MOZ_GLEAN_ANDROID
NS_WARNING("Don't set experiments from Gecko in Android. Ignoring.");
return NS_OK;
#else
nsTArray<nsCString> extraKeys;
nsTArray<nsCString> extraValues;
if (!aExtra.isNullOrUndefined()) {
JS::RootedObject obj(aCx, &aExtra.toObject());
JS::Rooted<JS::IdVector> keys(aCx, JS::IdVector(aCx));
if (!JS_Enumerate(aCx, obj, &keys)) {
LogToBrowserConsole(nsIScriptError::warningFlag,
u"Failed to enumerate experiment extras object."_ns);
return NS_OK;
}
for (size_t i = 0, n = keys.length(); i < n; i++) {
nsAutoJSCString jsKey;
if (!jsKey.init(aCx, keys[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, keys[i], &value)) {
LogToBrowserConsole(nsIScriptError::warningFlag,
u"Failed to get experiment extra property."_ns);
return NS_OK;
}
nsAutoJSCString jsValue;
if (!value.isString()) {
LogToBrowserConsole(
nsIScriptError::warningFlag,
u"Experiment extra properties must have string values."_ns);
return NS_OK;
}
if (!jsValue.init(aCx, value)) {
LogToBrowserConsole(nsIScriptError::warningFlag,
u"Can't extract experiment extra property"_ns);
return NS_OK;
}
extraKeys.AppendElement(jsKey);
extraValues.AppendElement(jsValue);
}
}
glean::impl::fog_set_experiment_active(&aExperimentId, &aBranch, &extraKeys,
&extraValues);
return NS_OK;
#endif
}
NS_IMETHODIMP
FOG::SetExperimentInactive(const nsACString& aExperimentId) {
#ifdef MOZ_GLEAN_ANDROID
NS_WARNING("Don't unset experiments from Gecko in Android. Ignoring.");
return NS_OK;
#else
glean::impl::fog_set_experiment_inactive(&aExperimentId);
return NS_OK;
#endif
}
NS_IMETHODIMP NS_IMETHODIMP
FOG::TestFlushAllChildren(JSContext* aCx, mozilla::dom::Promise** aOutPromise) { FOG::TestFlushAllChildren(JSContext* aCx, mozilla::dom::Promise** aOutPromise) {
NS_ENSURE_ARG(aOutPromise); NS_ENSURE_ARG(aOutPromise);

Просмотреть файл

@ -53,6 +53,33 @@ interface nsIFOG : nsISupports
*/ */
void sendPing(in ACString aPingName); void sendPing(in ACString aPingName);
/**
* Indicate that an experiment is running.
* Glean will add an experiment annotation which is sent with pings.
* This information is not persisted between runs.
*
* See `glean_core::Glean::set_experiment_active`.
*
* Logs on error, but does not throw.
*
* @param aExperimentId - The id/slug of the experiment.
* @param aBranch - The name of the active branch of the experiment.
* @param aExtra - Optional string -> string dictionary of extra information.
*/
[implicit_jscontext]
void setExperimentActive(in ACString aExperimentId, in ACString aBranch, [optional] in jsval aExtra);
/**
* Indicate that an experiment is no longer running.
*
* See `glean_core::Glean::set_experiment_inactive`.
*
* Logs on error, but does not throw.
*
* @param aExperimentId - The id/slug of the experiment from setExperimentActive.
*/
void setExperimentInactive(in ACString aExperimentId);
/** /**
* ** Test-only Method ** * ** Test-only Method **
* *