tor-browser

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

JSIPCValueUtils.h (3432B)


      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 #ifndef MOZILLA_DOM_IPC_JSACTOR_JSIPCVALUEUTILS_H_
      8 #define MOZILLA_DOM_IPC_JSACTOR_JSIPCVALUEUTILS_H_
      9 
     10 #include "js/RootingAPI.h"
     11 #include "js/Value.h"
     12 #include "jstypes.h"
     13 #include "mozilla/Maybe.h"
     14 #include "mozilla/dom/JSActor.h"
     15 #include "mozilla/dom/JSIPCValue.h"
     16 
     17 // This file contains a number of useful methods for JSIPCValue, mostly dealing
     18 // with turning it to and from a JS value.
     19 
     20 namespace mozilla::dom {
     21 
     22 // Return true if a message for an actor with the given name should be
     23 // sent typed.
     24 bool JSActorSupportsTypedSend(const nsACString& aName);
     25 
     26 class JSIPCValueUtils {
     27 public:
     28  struct Context {
     29    explicit Context(JSContext* aCx, bool aStrict = true)
     30        : mCx(aCx), mStrict(aStrict) {}
     31 
     32    MOZ_IMPLICIT operator JSContext*() const { return mCx; }
     33 
     34    JSContext* mCx;
     35 
     36    // If we encounter a JS value that can't be directly serialized to
     37    // JSIPCValue, we fall back to using structured cloning. mStrict
     38    // determines the behavior if this structured cloning fails. If mStrict is
     39    // true, then the entire serialization will fail. If it is false, we'll
     40    // instead serialize to a fallback value. See UntypedFromJSVal for details.
     41    bool mStrict;
     42  };
     43 
     44  // Convert a JS value to an IPDL representation of that value, or return
     45  // Nothing if the value isn't supported. If aSendTyped is false, the result
     46  // will always just be a wrapper around a StructuredCloneData.
     47  static JSIPCValue FromJSVal(Context& aCx, JS::Handle<JS::Value> aVal,
     48                              bool aSendTyped, ErrorResult& aError);
     49 
     50  // Same as the above, except with support for a transfers object, if needed.
     51  static JSIPCValue FromJSVal(Context& aCx, JS::Handle<JS::Value> aVal,
     52                              JS::Handle<JS::Value> aTransferable,
     53                              bool aSendTyped, ErrorResult& aError);
     54 
     55  // This is equivalent to calling FromJSVal with aSendTyped equal to true.
     56  static JSIPCValue TypedFromJSVal(Context& aCx, JS::Handle<JS::Value> aVal,
     57                                   ErrorResult& aError);
     58 
     59  // Wrapper class to abstract away the details of the auxiliary data structure
     60  // needed for PrepareForSending.
     61  class SCDHolder final {
     62   public:
     63    SCDHolder() = default;
     64    ~SCDHolder() = default;
     65    friend class JSIPCValueUtils;
     66 
     67   private:
     68    nsTArray<UniquePtr<ipc::StructuredCloneData>> mSCDs;
     69  };
     70 
     71  // Prepare a JSIPCValue for IPC by turning any StructuredCloneData it
     72  // contains into ClonedMessageData. Auxiliary data needed for IPC
     73  // serialization will be added to aHolder, so it needs to be kept alive
     74  // until aValue is sent over IPC.
     75  [[nodiscard]] static bool PrepareForSending(SCDHolder& aHolder,
     76                                              JSIPCValue& aValue);
     77 
     78  // Convert the IPDL representation of a JS value back into the equivalent
     79  // JS value. This will return false on failure.
     80  static void ToJSVal(JSContext* aCx, JSIPCValue&& aIn,
     81                      JS::MutableHandle<JS::Value> aOut, ErrorResult& aError);
     82 };
     83 
     84 }  // namespace mozilla::dom
     85 
     86 #endif  // MOZILLA_DOM_IPC_JSACTOR_JSIPCVALUEUTILS_H_