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 "StructuredCloneTester.h"
9 #include "js/StructuredClone.h"
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/dom/StructuredCloneTags.h"
12 #include "mozilla/dom/StructuredCloneTesterBinding.h"
13 #include "xpcpublic.h"
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(StructuredCloneTester
)
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(StructuredCloneTester
)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(StructuredCloneTester
)
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StructuredCloneTester
)
25 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
26 NS_INTERFACE_MAP_ENTRY(nsISupports
)
29 StructuredCloneTester::StructuredCloneTester(nsISupports
* aParent
,
30 const bool aSerializable
,
31 const bool aDeserializable
)
33 mSerializable(aSerializable
),
34 mDeserializable(aDeserializable
) {}
37 already_AddRefed
<StructuredCloneTester
> StructuredCloneTester::Constructor(
38 const GlobalObject
& aGlobal
, const bool aSerializable
,
39 const bool aDeserializable
, ErrorResult
& aRv
) {
40 RefPtr
<StructuredCloneTester
> sct
= new StructuredCloneTester(
41 aGlobal
.GetAsSupports(), aSerializable
, aDeserializable
);
45 bool StructuredCloneTester::Serializable() const { return mSerializable
; }
47 bool StructuredCloneTester::Deserializable() const { return mDeserializable
; }
50 JSObject
* StructuredCloneTester::ReadStructuredClone(
51 JSContext
* aCx
, JSStructuredCloneReader
* aReader
) {
52 uint32_t serializable
= 0;
53 uint32_t deserializable
= 0;
55 if (!JS_ReadUint32Pair(aReader
, &serializable
, &deserializable
)) {
59 nsIGlobalObject
* global
= xpc::CurrentNativeGlobal(aCx
);
61 if (NS_WARN_IF(!global
)) {
65 // Prevent the return value from being trashed by a GC during ~RefPtr
66 JS::Rooted
<JSObject
*> result(aCx
);
68 RefPtr
<StructuredCloneTester
> sct
=
69 new StructuredCloneTester(global
, static_cast<bool>(serializable
),
70 static_cast<bool>(deserializable
));
72 // "Fail" deserialization
73 if (!sct
->Deserializable()) {
74 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
78 result
= sct
->WrapObject(aCx
, nullptr);
84 bool StructuredCloneTester::WriteStructuredClone(
85 JSStructuredCloneWriter
* aWriter
) const {
86 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_STRUCTURED_CLONE_TESTER
, 0) &&
87 JS_WriteUint32Pair(aWriter
, static_cast<uint32_t>(Serializable()),
88 static_cast<uint32_t>(Deserializable()));
91 nsISupports
* StructuredCloneTester::GetParentObject() const { return mParent
; }
93 JSObject
* StructuredCloneTester::WrapObject(JSContext
* aCx
,
94 JS::Handle
<JSObject
*> aGivenProto
) {
95 return StructuredCloneTester_Binding::Wrap(aCx
, this, aGivenProto
);
99 } // namespace mozilla