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 "nsIGlobalObject.h"
14 #include "xpcpublic.h"
22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(StructuredCloneTester
)
23 NS_IMPL_CYCLE_COLLECTING_ADDREF(StructuredCloneTester
)
24 NS_IMPL_CYCLE_COLLECTING_RELEASE(StructuredCloneTester
)
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StructuredCloneTester
)
26 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27 NS_INTERFACE_MAP_ENTRY(nsISupports
)
30 StructuredCloneTester::StructuredCloneTester(nsISupports
* aParent
,
31 const bool aSerializable
,
32 const bool aDeserializable
)
34 mSerializable(aSerializable
),
35 mDeserializable(aDeserializable
) {}
38 already_AddRefed
<StructuredCloneTester
> StructuredCloneTester::Constructor(
39 const GlobalObject
& aGlobal
, const bool aSerializable
,
40 const bool aDeserializable
) {
41 RefPtr
<StructuredCloneTester
> sct
= new StructuredCloneTester(
42 aGlobal
.GetAsSupports(), aSerializable
, aDeserializable
);
46 bool StructuredCloneTester::Serializable() const { return mSerializable
; }
48 bool StructuredCloneTester::Deserializable() const { return mDeserializable
; }
51 already_AddRefed
<StructuredCloneTester
>
52 StructuredCloneTester::ReadStructuredClone(JSContext
* aCx
,
53 nsIGlobalObject
* aGlobal
,
54 JSStructuredCloneReader
* aReader
) {
55 uint32_t serializable
= 0;
56 uint32_t deserializable
= 0;
58 if (!JS_ReadUint32Pair(aReader
, &serializable
, &deserializable
)) {
62 RefPtr
<StructuredCloneTester
> sct
=
63 new StructuredCloneTester(aGlobal
, static_cast<bool>(serializable
),
64 static_cast<bool>(deserializable
));
66 // "Fail" deserialization
67 if (!sct
->Deserializable()) {
68 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
75 bool StructuredCloneTester::WriteStructuredClone(
76 JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) const {
77 if (!Serializable()) {
78 return xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
80 return JS_WriteUint32Pair(aWriter
, static_cast<uint32_t>(Serializable()),
81 static_cast<uint32_t>(Deserializable()));
84 nsISupports
* StructuredCloneTester::GetParentObject() const { return mParent
; }
86 JSObject
* StructuredCloneTester::WrapObject(JSContext
* aCx
,
87 JS::Handle
<JSObject
*> aGivenProto
) {
88 return StructuredCloneTester_Binding::Wrap(aCx
, this, aGivenProto
);
92 } // namespace mozilla