Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / payments / PaymentRequestUtils.cpp
blob8a8626c328d8665478ae72e7a0359bc5ffd0426d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "js/JSON.h"
8 #include "nsContentUtils.h"
9 #include "nsArrayUtils.h"
10 #include "nsTString.h"
11 #include "PaymentRequestUtils.h"
13 namespace mozilla::dom {
15 nsresult SerializeFromJSObject(JSContext* aCx, JS::Handle<JSObject*> aObject,
16 nsAString& aSerializedObject) {
17 MOZ_ASSERT(aCx);
18 JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*aObject));
19 return SerializeFromJSVal(aCx, value, aSerializedObject);
22 nsresult SerializeFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aValue,
23 nsAString& aSerializedValue) {
24 aSerializedValue.Truncate();
25 NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, aValue, aSerializedValue,
26 UndefinedIsNullStringLiteral),
27 NS_ERROR_XPC_BAD_CONVERT_JS);
28 NS_ENSURE_TRUE(!aSerializedValue.IsEmpty(), NS_ERROR_FAILURE);
29 return NS_OK;
32 nsresult DeserializeToJSObject(const nsAString& aSerializedObject,
33 JSContext* aCx,
34 JS::MutableHandle<JSObject*> aObject) {
35 MOZ_ASSERT(aCx);
36 JS::Rooted<JS::Value> value(aCx);
37 nsresult rv = DeserializeToJSValue(aSerializedObject, aCx, &value);
38 if (NS_WARN_IF(NS_FAILED(rv))) {
39 return rv;
41 if (value.isObject()) {
42 aObject.set(&value.toObject());
43 } else {
44 aObject.set(nullptr);
46 return NS_OK;
49 nsresult DeserializeToJSValue(const nsAString& aSerializedObject,
50 JSContext* aCx,
51 JS::MutableHandle<JS::Value> aValue) {
52 MOZ_ASSERT(aCx);
53 if (!JS_ParseJSON(aCx, aSerializedObject.BeginReading(),
54 aSerializedObject.Length(), aValue)) {
55 return NS_ERROR_UNEXPECTED;
57 return NS_OK;
60 } // namespace mozilla::dom