diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:14:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:14:29 +0000 |
commit | fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch) | |
tree | 4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /toolkit/components/glean/bindings/private/Object.cpp | |
parent | Releasing progress-linux version 124.0.1-1~progress7.99u1. (diff) | |
download | firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip |
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/glean/bindings/private/Object.cpp')
-rw-r--r-- | toolkit/components/glean/bindings/private/Object.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/toolkit/components/glean/bindings/private/Object.cpp b/toolkit/components/glean/bindings/private/Object.cpp new file mode 100644 index 0000000000..817b14dc0f --- /dev/null +++ b/toolkit/components/glean/bindings/private/Object.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/Object.h" + +#include "Common.h" +#include "mozilla/dom/GleanMetricsBinding.h" +#include "mozilla/dom/ToJSValue.h" +#include "mozilla/Logging.h" +#include "jsapi.h" +#include "js/JSON.h" +#include "nsContentUtils.h" + +using namespace mozilla::dom; + +namespace mozilla::glean { + +/* virtual */ +JSObject* GleanObject::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + return dom::GleanObject_Binding::Wrap(aCx, this, aGivenProto); +} + +void GleanObject::Set(JSContext* aCx, JS::Handle<JSObject*> aObj) { + // We take in an `object`. Cannot be `null`! + // But at this point the type system doesn't know that. + JS::Rooted<JS::Value> value(aCx); + value.setObjectOrNull(aObj); + + nsAutoString serializedValue; + bool res = nsContentUtils::StringifyJSON(aCx, value, serializedValue, + UndefinedIsNullStringLiteral); + if (!res) { + // JS_Stringify throws an exception, e.g. on cyclic objects. + // We don't want this rethrown. + JS_ClearPendingException(aCx); + + LogToBrowserConsole(nsIScriptError::warningFlag, + u"passed in object cannot be serialized"_ns); + return; + } + + NS_ConvertUTF16toUTF8 payload(serializedValue); + mObject.SetStr(payload); +} + +void GleanObject::TestGetValue(JSContext* aCx, const nsACString& aPingName, + JS::MutableHandle<JSObject*> aResult, + ErrorResult& aRv) { + aResult.set(nullptr); + + auto result = mObject.TestGetValue(aPingName); + if (result.isErr()) { + aRv.ThrowDataError(result.unwrapErr()); + return; + } + auto optresult = result.unwrap(); + if (optresult.isNothing()) { + return; + } + + const NS_ConvertUTF8toUTF16 str(optresult.ref()); + JS::Rooted<JS::Value> json(aCx); + bool res = JS_ParseJSON(aCx, str.get(), str.Length(), &json); + if (!res) { + aRv.ThrowDataError("couldn't parse stored object"); + return; + } + if (!json.isObject()) { + aRv.ThrowDataError("stored data does not represent a valid object"); + return; + } + + aResult.set(&json.toObject()); +} + +} // namespace mozilla::glean |