Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsStructuredCloneContainer.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 "nsStructuredCloneContainer.h"
8
9
#include "nsCOMPtr.h"
10
#include "nsIGlobalObject.h"
11
#include "nsIVariant.h"
12
#include "nsIXPConnect.h"
13
#include "nsServiceManagerUtils.h"
14
#include "nsContentUtils.h"
15
#include "jsapi.h"
16
#include "xpcpublic.h"
17
18
#include "mozilla/Base64.h"
19
#include "mozilla/dom/ScriptSettings.h"
20
21
using namespace mozilla;
22
using namespace mozilla::dom;
23
24
NS_IMPL_ADDREF(nsStructuredCloneContainer)
25
NS_IMPL_RELEASE(nsStructuredCloneContainer)
26
27
0
NS_INTERFACE_MAP_BEGIN(nsStructuredCloneContainer)
28
0
  NS_INTERFACE_MAP_ENTRY(nsIStructuredCloneContainer)
29
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
30
0
NS_INTERFACE_MAP_END
31
32
nsStructuredCloneContainer::nsStructuredCloneContainer()
33
  : mVersion(0)
34
0
{
35
0
}
36
37
nsStructuredCloneContainer::~nsStructuredCloneContainer()
38
0
{
39
0
}
40
41
NS_IMETHODIMP
42
nsStructuredCloneContainer::InitFromJSVal(JS::Handle<JS::Value> aData,
43
                                          JSContext* aCx)
44
0
{
45
0
  if (DataLength()) {
46
0
    return NS_ERROR_FAILURE;
47
0
  }
48
0
49
0
  ErrorResult rv;
50
0
  Write(aCx, aData, rv);
51
0
  if (NS_WARN_IF(rv.Failed())) {
52
0
    return rv.StealNSResult();
53
0
  }
54
0
55
0
  mVersion = JS_STRUCTURED_CLONE_VERSION;
56
0
  return NS_OK;
57
0
}
58
59
NS_IMETHODIMP
60
nsStructuredCloneContainer::InitFromBase64(const nsAString &aData,
61
                                           uint32_t aFormatVersion)
62
0
{
63
0
  if (DataLength()) {
64
0
    return NS_ERROR_FAILURE;
65
0
  }
66
0
67
0
  NS_ConvertUTF16toUTF8 data(aData);
68
0
69
0
  nsAutoCString binaryData;
70
0
  nsresult rv = Base64Decode(data, binaryData);
71
0
  NS_ENSURE_SUCCESS(rv, rv);
72
0
73
0
  if (!CopyExternalData(binaryData.get(), binaryData.Length())) {
74
0
    return NS_ERROR_OUT_OF_MEMORY;
75
0
  }
76
0
77
0
  mVersion = aFormatVersion;
78
0
  return NS_OK;
79
0
}
80
81
nsresult
82
nsStructuredCloneContainer::DeserializeToJsval(JSContext* aCx,
83
                                               JS::MutableHandle<JS::Value> aValue)
84
0
{
85
0
  aValue.setNull();
86
0
  JS::Rooted<JS::Value> jsStateObj(aCx);
87
0
88
0
  ErrorResult rv;
89
0
  Read(aCx, &jsStateObj, rv);
90
0
  if (NS_WARN_IF(rv.Failed())) {
91
0
    return rv.StealNSResult();
92
0
  }
93
0
94
0
  aValue.set(jsStateObj);
95
0
  return NS_OK;
96
0
}
97
98
NS_IMETHODIMP
99
nsStructuredCloneContainer::DeserializeToVariant(JSContext* aCx,
100
                                                 nsIVariant** aData)
101
0
{
102
0
  NS_ENSURE_ARG_POINTER(aData);
103
0
  *aData = nullptr;
104
0
105
0
  if (!DataLength()) {
106
0
    return NS_ERROR_FAILURE;
107
0
  }
108
0
109
0
  // Deserialize to a JS::Value.
110
0
  JS::Rooted<JS::Value> jsStateObj(aCx);
111
0
  nsresult rv = DeserializeToJsval(aCx, &jsStateObj);
112
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
113
0
    return rv;
114
0
  }
115
0
116
0
  // Now wrap the JS::Value as an nsIVariant.
117
0
  nsCOMPtr<nsIVariant> varStateObj;
118
0
  nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
119
0
  NS_ENSURE_STATE(xpconnect);
120
0
  xpconnect->JSValToVariant(aCx, jsStateObj, getter_AddRefs(varStateObj));
121
0
  NS_ENSURE_STATE(varStateObj);
122
0
123
0
  varStateObj.forget(aData);
124
0
  return NS_OK;
125
0
}
126
127
NS_IMETHODIMP
128
nsStructuredCloneContainer::GetDataAsBase64(nsAString &aOut)
129
0
{
130
0
  aOut.Truncate();
131
0
132
0
  if (!DataLength()) {
133
0
    return NS_ERROR_FAILURE;
134
0
  }
135
0
136
0
  if (HasClonedDOMObjects()) {
137
0
    return NS_ERROR_FAILURE;
138
0
  }
139
0
140
0
  auto iter = Data().Start();
141
0
  size_t size = Data().Size();
142
0
  nsAutoCString binaryData;
143
0
  binaryData.SetLength(size);
144
0
  Data().ReadBytes(iter, binaryData.BeginWriting(), size);
145
0
  nsAutoCString base64Data;
146
0
  nsresult rv = Base64Encode(binaryData, base64Data);
147
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
148
0
    return rv;
149
0
  }
150
0
151
0
  if (!CopyASCIItoUTF16(base64Data, aOut, fallible)) {
152
0
    return NS_ERROR_OUT_OF_MEMORY;
153
0
  }
154
0
155
0
  return NS_OK;
156
0
}
157
158
NS_IMETHODIMP
159
nsStructuredCloneContainer::GetSerializedNBytes(uint64_t* aSize)
160
0
{
161
0
  NS_ENSURE_ARG_POINTER(aSize);
162
0
163
0
  if (!DataLength()) {
164
0
    return NS_ERROR_FAILURE;
165
0
  }
166
0
167
0
  *aSize = DataLength();
168
0
  return NS_OK;
169
0
}
170
171
NS_IMETHODIMP
172
nsStructuredCloneContainer::GetFormatVersion(uint32_t* aFormatVersion)
173
0
{
174
0
  NS_ENSURE_ARG_POINTER(aFormatVersion);
175
0
176
0
  if (!DataLength()) {
177
0
    return NS_ERROR_FAILURE;
178
0
  }
179
0
180
0
  *aFormatVersion = mVersion;
181
0
  return NS_OK;
182
0
}