tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StructuredCloneTester.cpp (3218B)


      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 "StructuredCloneTester.h"
      8 
      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"
     15 
     16 namespace mozilla {
     17 
     18 class ErrorResult;
     19 
     20 namespace dom {
     21 
     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)
     28 NS_INTERFACE_MAP_END
     29 
     30 StructuredCloneTester::StructuredCloneTester(nsISupports* aParent,
     31                                             const bool aSerializable,
     32                                             const bool aDeserializable)
     33    : mParent(aParent),
     34      mSerializable(aSerializable),
     35      mDeserializable(aDeserializable) {}
     36 
     37 /* static */
     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);
     43  return sct.forget();
     44 }
     45 
     46 bool StructuredCloneTester::Serializable() const { return mSerializable; }
     47 
     48 bool StructuredCloneTester::Deserializable() const { return mDeserializable; }
     49 
     50 /* static */
     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;
     57 
     58  if (!JS_ReadUint32Pair(aReader, &serializable, &deserializable)) {
     59    return nullptr;
     60  }
     61 
     62  RefPtr<StructuredCloneTester> sct =
     63      new StructuredCloneTester(aGlobal, static_cast<bool>(serializable),
     64                                static_cast<bool>(deserializable));
     65 
     66  // "Fail" deserialization
     67  if (!sct->Deserializable()) {
     68    xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
     69    return nullptr;
     70  }
     71 
     72  return sct.forget();
     73 }
     74 
     75 bool StructuredCloneTester::WriteStructuredClone(
     76    JSContext* aCx, JSStructuredCloneWriter* aWriter) const {
     77  if (!Serializable()) {
     78    return xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
     79  }
     80  return JS_WriteUint32Pair(aWriter, static_cast<uint32_t>(Serializable()),
     81                            static_cast<uint32_t>(Deserializable()));
     82 }
     83 
     84 nsISupports* StructuredCloneTester::GetParentObject() const { return mParent; }
     85 
     86 JSObject* StructuredCloneTester::WrapObject(JSContext* aCx,
     87                                            JS::Handle<JSObject*> aGivenProto) {
     88  return StructuredCloneTester_Binding::Wrap(aCx, this, aGivenProto);
     89 }
     90 
     91 }  // namespace dom
     92 }  // namespace mozilla