Bug 1732409 let fake:true getUserMedia() parameter override loopback prefs r=jib
[gecko.git] / dom / bindings / SpiderMonkeyInterface.h
bloba8fe8e638d6558a1ec81bd444e4355fbe5510bcc
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_SpiderMonkeyInterface_h
8 #define mozilla_dom_SpiderMonkeyInterface_h
10 #include "jsapi.h"
11 #include "js/RootingAPI.h"
12 #include "js/TracingAPI.h"
14 namespace mozilla {
15 namespace dom {
18 * Class that just handles the JSObject storage and tracing for spidermonkey
19 * interfaces
21 struct SpiderMonkeyInterfaceObjectStorage {
22 protected:
23 JSObject* mImplObj;
24 JSObject* mWrappedObj;
26 SpiderMonkeyInterfaceObjectStorage()
27 : mImplObj(nullptr), mWrappedObj(nullptr) {}
29 SpiderMonkeyInterfaceObjectStorage(
30 SpiderMonkeyInterfaceObjectStorage&& aOther)
31 : mImplObj(aOther.mImplObj), mWrappedObj(aOther.mWrappedObj) {
32 aOther.mImplObj = nullptr;
33 aOther.mWrappedObj = nullptr;
36 public:
37 inline void TraceSelf(JSTracer* trc) {
38 JS::TraceRoot(trc, &mImplObj,
39 "SpiderMonkeyInterfaceObjectStorage.mImplObj");
40 JS::TraceRoot(trc, &mWrappedObj,
41 "SpiderMonkeyInterfaceObjectStorage.mWrappedObj");
44 inline bool inited() const { return !!mImplObj; }
46 inline bool WrapIntoNewCompartment(JSContext* cx) {
47 return JS_WrapObject(
48 cx, JS::MutableHandle<JSObject*>::fromMarkedLocation(&mWrappedObj));
51 inline JSObject* Obj() const {
52 MOZ_ASSERT(inited());
53 return mWrappedObj;
56 private:
57 SpiderMonkeyInterfaceObjectStorage(
58 const SpiderMonkeyInterfaceObjectStorage&) = delete;
61 // A class for rooting an existing SpiderMonkey Interface struct
62 template <typename InterfaceType>
63 class MOZ_RAII SpiderMonkeyInterfaceRooter : private JS::CustomAutoRooter {
64 public:
65 template <typename CX>
66 SpiderMonkeyInterfaceRooter(const CX& cx, InterfaceType* aInterface)
67 : JS::CustomAutoRooter(cx), mInterface(aInterface) {}
69 virtual void trace(JSTracer* trc) override { mInterface->TraceSelf(trc); }
71 private:
72 SpiderMonkeyInterfaceObjectStorage* const mInterface;
75 // And a specialization for dealing with nullable SpiderMonkey interfaces
76 template <typename Inner>
77 struct Nullable;
78 template <typename InterfaceType>
79 class MOZ_RAII SpiderMonkeyInterfaceRooter<Nullable<InterfaceType>>
80 : private JS::CustomAutoRooter {
81 public:
82 template <typename CX>
83 SpiderMonkeyInterfaceRooter(const CX& cx, Nullable<InterfaceType>* aInterface)
84 : JS::CustomAutoRooter(cx), mInterface(aInterface) {}
86 virtual void trace(JSTracer* trc) override {
87 if (!mInterface->IsNull()) {
88 mInterface->Value().TraceSelf(trc);
92 private:
93 Nullable<InterfaceType>* const mInterface;
96 // Class for easily setting up a rooted SpiderMonkey interface object on the
97 // stack
98 template <typename InterfaceType>
99 class MOZ_RAII RootedSpiderMonkeyInterface final
100 : public InterfaceType,
101 private SpiderMonkeyInterfaceRooter<InterfaceType> {
102 public:
103 template <typename CX>
104 explicit RootedSpiderMonkeyInterface(const CX& cx)
105 : InterfaceType(), SpiderMonkeyInterfaceRooter<InterfaceType>(cx, this) {}
107 template <typename CX>
108 RootedSpiderMonkeyInterface(const CX& cx, JSObject* obj)
109 : InterfaceType(obj),
110 SpiderMonkeyInterfaceRooter<InterfaceType>(cx, this) {}
113 } // namespace dom
114 } // namespace mozilla
116 #endif /* mozilla_dom_SpiderMonkeyInterface_h */