summaryrefslogtreecommitdiffstats
path: root/dom/base/StructuredCloneTester.cpp
blob: 40ed321f697e7146588b15e7bdc7b6f79b71f030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "StructuredCloneTester.h"

#include "js/StructuredClone.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/StructuredCloneTags.h"
#include "mozilla/dom/StructuredCloneTesterBinding.h"
#include "nsIGlobalObject.h"
#include "xpcpublic.h"

namespace mozilla {

class ErrorResult;

namespace dom {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(StructuredCloneTester)
NS_IMPL_CYCLE_COLLECTING_ADDREF(StructuredCloneTester)
NS_IMPL_CYCLE_COLLECTING_RELEASE(StructuredCloneTester)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StructuredCloneTester)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

StructuredCloneTester::StructuredCloneTester(nsISupports* aParent,
                                             const bool aSerializable,
                                             const bool aDeserializable)
    : mParent(aParent),
      mSerializable(aSerializable),
      mDeserializable(aDeserializable) {}

/* static */
already_AddRefed<StructuredCloneTester> StructuredCloneTester::Constructor(
    const GlobalObject& aGlobal, const bool aSerializable,
    const bool aDeserializable) {
  RefPtr<StructuredCloneTester> sct = new StructuredCloneTester(
      aGlobal.GetAsSupports(), aSerializable, aDeserializable);
  return sct.forget();
}

bool StructuredCloneTester::Serializable() const { return mSerializable; }

bool StructuredCloneTester::Deserializable() const { return mDeserializable; }

/* static */
already_AddRefed<StructuredCloneTester>
StructuredCloneTester::ReadStructuredClone(JSContext* aCx,
                                           nsIGlobalObject* aGlobal,
                                           JSStructuredCloneReader* aReader) {
  uint32_t serializable = 0;
  uint32_t deserializable = 0;

  if (!JS_ReadUint32Pair(aReader, &serializable, &deserializable)) {
    return nullptr;
  }

  RefPtr<StructuredCloneTester> sct =
      new StructuredCloneTester(aGlobal, static_cast<bool>(serializable),
                                static_cast<bool>(deserializable));

  // "Fail" deserialization
  if (!sct->Deserializable()) {
    xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
    return nullptr;
  }

  return sct.forget();
}

bool StructuredCloneTester::WriteStructuredClone(
    JSContext* aCx, JSStructuredCloneWriter* aWriter) const {
  if (!Serializable()) {
    return xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
  }
  return JS_WriteUint32Pair(aWriter, static_cast<uint32_t>(Serializable()),
                            static_cast<uint32_t>(Deserializable()));
}

nsISupports* StructuredCloneTester::GetParentObject() const { return mParent; }

JSObject* StructuredCloneTester::WrapObject(JSContext* aCx,
                                            JS::Handle<JSObject*> aGivenProto) {
  return StructuredCloneTester_Binding::Wrap(aCx, this, aGivenProto);
}

}  // namespace dom
}  // namespace mozilla