Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/payments/PaymentRequestUtils.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "js/JSON.h"
8
#include "nsArrayUtils.h"
9
#include "PaymentRequestUtils.h"
10
#include "nsIMutableArray.h"
11
#include "nsISupportsPrimitives.h"
12
13
namespace mozilla {
14
namespace dom {
15
16
nsresult
17
SerializeFromJSObject(JSContext* aCx, JS::HandleObject aObject, nsAString& aSerializedObject)
18
0
{
19
0
  MOZ_ASSERT(aCx);
20
0
  JS::RootedValue value(aCx, JS::ObjectValue(*aObject));
21
0
  return SerializeFromJSVal(aCx, value, aSerializedObject);
22
0
}
23
24
nsresult
25
SerializeFromJSVal(JSContext* aCx, JS::HandleValue aValue, nsAString& aSerializedValue)
26
0
{
27
0
  aSerializedValue.Truncate();
28
0
  nsAutoString serializedValue;
29
0
  JS::RootedValue value(aCx, aValue.get());
30
0
  NS_ENSURE_TRUE(nsContentUtils::StringifyJSON(aCx, &value, serializedValue), NS_ERROR_XPC_BAD_CONVERT_JS);
31
0
  NS_ENSURE_TRUE(!serializedValue.IsEmpty(), NS_ERROR_FAILURE);
32
0
  aSerializedValue = serializedValue;
33
0
  return NS_OK;
34
0
}
35
36
nsresult
37
DeserializeToJSObject(const nsAString& aSerializedObject, JSContext* aCx, JS::MutableHandleObject aObject)
38
0
{
39
0
  MOZ_ASSERT(aCx);
40
0
  JS::RootedValue value(aCx);
41
0
  nsresult rv = DeserializeToJSValue(aSerializedObject, aCx, &value);
42
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
43
0
    return rv;
44
0
  }
45
0
  if (value.isObject()) {
46
0
    aObject.set(&value.toObject());
47
0
  } else {
48
0
    aObject.set(nullptr);
49
0
  }
50
0
  return NS_OK;
51
0
}
52
53
nsresult
54
DeserializeToJSValue(const nsAString& aSerializedObject, JSContext* aCx, JS::MutableHandleValue aValue)
55
0
{
56
0
  MOZ_ASSERT(aCx);
57
0
  if (!JS_ParseJSON(aCx, static_cast<const char16_t*>(PromiseFlatString(aSerializedObject).get()),
58
0
                    aSerializedObject.Length(), aValue)) {
59
0
    return NS_ERROR_UNEXPECTED;
60
0
  }
61
0
  return NS_OK;
62
0
}
63
64
} // end of namespace dom
65
} // end of namespace mozilla