Bug 1660051 [wpt PR 25111] - Origin isolation: expand getter test coverage, a=testonly
[gecko.git] / dom / base / nsStructuredCloneContainer.cpp
blob1accf471a524cab6de0319c85d48247154d32cd2
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 "nsStructuredCloneContainer.h"
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"
18 #include "mozilla/Base64.h"
19 #include "mozilla/dom/ScriptSettings.h"
21 using namespace mozilla;
22 using namespace mozilla::dom;
24 NS_IMPL_ADDREF(nsStructuredCloneContainer)
25 NS_IMPL_RELEASE(nsStructuredCloneContainer)
27 NS_INTERFACE_MAP_BEGIN(nsStructuredCloneContainer)
28 NS_INTERFACE_MAP_ENTRY(nsIStructuredCloneContainer)
29 NS_INTERFACE_MAP_ENTRY(nsISupports)
30 NS_INTERFACE_MAP_END
32 nsStructuredCloneContainer::nsStructuredCloneContainer() : mVersion(0) {}
34 nsStructuredCloneContainer::~nsStructuredCloneContainer() = default;
36 NS_IMETHODIMP
37 nsStructuredCloneContainer::InitFromJSVal(JS::Handle<JS::Value> aData,
38 JSContext* aCx) {
39 if (DataLength()) {
40 return NS_ERROR_FAILURE;
43 ErrorResult rv;
44 Write(aCx, aData, rv);
45 if (NS_WARN_IF(rv.Failed())) {
46 // XXX propagate the error message as well.
47 // We cannot StealNSResult because we threw a DOM exception.
48 rv.SuppressException();
49 return NS_ERROR_DOM_DATA_CLONE_ERR;
52 mVersion = JS_STRUCTURED_CLONE_VERSION;
53 return NS_OK;
56 NS_IMETHODIMP
57 nsStructuredCloneContainer::InitFromBase64(const nsAString& aData,
58 uint32_t aFormatVersion) {
59 if (DataLength()) {
60 return NS_ERROR_FAILURE;
63 NS_ConvertUTF16toUTF8 data(aData);
65 nsAutoCString binaryData;
66 nsresult rv = Base64Decode(data, binaryData);
67 NS_ENSURE_SUCCESS(rv, rv);
69 if (!CopyExternalData(binaryData.get(), binaryData.Length())) {
70 return NS_ERROR_OUT_OF_MEMORY;
73 mVersion = aFormatVersion;
74 return NS_OK;
77 nsresult nsStructuredCloneContainer::DeserializeToJsval(
78 JSContext* aCx, JS::MutableHandle<JS::Value> aValue) {
79 aValue.setNull();
80 JS::Rooted<JS::Value> jsStateObj(aCx);
82 ErrorResult rv;
83 Read(aCx, &jsStateObj, rv);
84 if (NS_WARN_IF(rv.Failed())) {
85 // XXX propagate the error message as well.
86 // We cannot StealNSResult because we threw a DOM exception.
87 rv.SuppressException();
88 return NS_ERROR_DOM_DATA_CLONE_ERR;
91 aValue.set(jsStateObj);
92 return NS_OK;
95 NS_IMETHODIMP
96 nsStructuredCloneContainer::DeserializeToVariant(JSContext* aCx,
97 nsIVariant** aData) {
98 NS_ENSURE_ARG_POINTER(aData);
99 *aData = nullptr;
101 if (!DataLength()) {
102 return NS_ERROR_FAILURE;
105 // Deserialize to a JS::Value.
106 JS::Rooted<JS::Value> jsStateObj(aCx);
107 nsresult rv = DeserializeToJsval(aCx, &jsStateObj);
108 if (NS_WARN_IF(NS_FAILED(rv))) {
109 return rv;
112 // Now wrap the JS::Value as an nsIVariant.
113 nsCOMPtr<nsIVariant> varStateObj;
114 nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
115 NS_ENSURE_STATE(xpconnect);
116 xpconnect->JSValToVariant(aCx, jsStateObj, getter_AddRefs(varStateObj));
117 NS_ENSURE_STATE(varStateObj);
119 varStateObj.forget(aData);
120 return NS_OK;
123 NS_IMETHODIMP
124 nsStructuredCloneContainer::GetDataAsBase64(nsAString& aOut) {
125 aOut.Truncate();
127 if (!DataLength()) {
128 return NS_ERROR_FAILURE;
131 if (HasClonedDOMObjects()) {
132 return NS_ERROR_FAILURE;
135 auto iter = Data().Start();
136 size_t size = Data().Size();
137 nsAutoCString binaryData;
138 if (!binaryData.SetLength(size, fallible)) {
139 return NS_ERROR_OUT_OF_MEMORY;
142 DebugOnly<bool> res = Data().ReadBytes(iter, binaryData.BeginWriting(), size);
143 MOZ_ASSERT(res);
145 nsresult rv = Base64Encode(binaryData, aOut);
146 if (NS_WARN_IF(NS_FAILED(rv))) {
147 return rv;
150 return NS_OK;
153 NS_IMETHODIMP
154 nsStructuredCloneContainer::GetSerializedNBytes(uint64_t* aSize) {
155 NS_ENSURE_ARG_POINTER(aSize);
157 if (!DataLength()) {
158 return NS_ERROR_FAILURE;
161 *aSize = DataLength();
162 return NS_OK;
165 NS_IMETHODIMP
166 nsStructuredCloneContainer::GetFormatVersion(uint32_t* aFormatVersion) {
167 NS_ENSURE_ARG_POINTER(aFormatVersion);
169 if (!DataLength()) {
170 return NS_ERROR_FAILURE;
173 *aFormatVersion = mVersion;
174 return NS_OK;