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_DOMJSProxyHandler_h
8 #define mozilla_dom_DOMJSProxyHandler_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Maybe.h"
14 #include "js/Object.h" // JS::GetClass
17 namespace mozilla::dom
{
20 * DOM proxies store the expando object in the private slot.
22 * The expando object is a plain JSObject whose properties correspond to
23 * "expandos" (custom properties set by the script author).
25 * The exact value stored in the proxy's private slot depends on whether the
26 * interface is annotated with the [OverrideBuiltins] extended attribute.
28 * If it is, the proxy is initialized with a PrivateValue, which contains a
29 * pointer to a JS::ExpandoAndGeneration object; this contains a pointer to
30 * the actual expando object as well as the "generation" of the object. The
31 * proxy handler will trace the expando object stored in the
32 * JS::ExpandoAndGeneration while the proxy itself is alive.
34 * If it is not, the proxy is initialized with an UndefinedValue. In
35 * EnsureExpandoObject, it is set to an ObjectValue that points to the
36 * expando object directly. (It is set back to an UndefinedValue only when
37 * the object is about to die.)
40 class BaseDOMProxyHandler
: public js::BaseProxyHandler
{
42 explicit constexpr BaseDOMProxyHandler(const void* aProxyFamily
,
43 bool aHasPrototype
= false)
44 : js::BaseProxyHandler(aProxyFamily
, aHasPrototype
) {}
46 // Implementations of methods that can be implemented in terms of
47 // other lower-level methods.
48 bool getOwnPropertyDescriptor(
49 JSContext
* cx
, JS::Handle
<JSObject
*> proxy
, JS::Handle
<jsid
> id
,
50 JS::MutableHandle
<Maybe
<JS::PropertyDescriptor
>> desc
) const override
;
51 virtual bool ownPropertyKeys(
52 JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
53 JS::MutableHandleVector
<jsid
> props
) const override
;
55 virtual bool getPrototypeIfOrdinary(
56 JSContext
* cx
, JS::Handle
<JSObject
*> proxy
, bool* isOrdinary
,
57 JS::MutableHandle
<JSObject
*> proto
) const override
;
59 // We override getOwnEnumerablePropertyKeys() and implement it directly
60 // instead of using the default implementation, which would call
61 // ownPropertyKeys and then filter out the non-enumerable ones. This avoids
62 // unnecessary work during enumeration.
63 virtual bool getOwnEnumerablePropertyKeys(
64 JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
65 JS::MutableHandleVector
<jsid
> props
) const override
;
68 // Hook for subclasses to implement shared ownPropertyKeys()/keys()
69 // functionality. The "flags" argument is either JSITER_OWNONLY (for keys())
70 // or JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS (for
71 // ownPropertyKeys()).
72 virtual bool ownPropNames(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
74 JS::MutableHandleVector
<jsid
> props
) const = 0;
76 // Hook for subclasses to allow set() to ignore named props while other things
77 // that look at property descriptors see them. This is intentionally not
78 // named getOwnPropertyDescriptor to avoid subclasses that override it hiding
79 // our public getOwnPropertyDescriptor.
80 virtual bool getOwnPropDescriptor(
81 JSContext
* cx
, JS::Handle
<JSObject
*> proxy
, JS::Handle
<jsid
> id
,
82 bool ignoreNamedProps
,
83 JS::MutableHandle
<Maybe
<JS::PropertyDescriptor
>> desc
) const = 0;
86 class DOMProxyHandler
: public BaseDOMProxyHandler
{
88 constexpr DOMProxyHandler() : BaseDOMProxyHandler(&family
) {}
90 bool defineProperty(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
92 JS::Handle
<JS::PropertyDescriptor
> desc
,
93 JS::ObjectOpResult
& result
) const override
{
95 return defineProperty(cx
, proxy
, id
, desc
, result
, &unused
);
97 virtual bool defineProperty(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
99 JS::Handle
<JS::PropertyDescriptor
> desc
,
100 JS::ObjectOpResult
& result
, bool* done
) const;
101 bool delete_(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
, JS::Handle
<jsid
> id
,
102 JS::ObjectOpResult
& result
) const override
;
103 bool preventExtensions(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
104 JS::ObjectOpResult
& result
) const override
;
105 bool isExtensible(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
106 bool* extensible
) const override
;
107 bool set(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
, JS::Handle
<jsid
> id
,
108 JS::Handle
<JS::Value
> v
, JS::Handle
<JS::Value
> receiver
,
109 JS::ObjectOpResult
& result
) const override
;
112 * If assigning to proxy[id] hits a named setter with OverrideBuiltins or
113 * an indexed setter, call it and set *done to true on success. Otherwise, set
116 virtual bool setCustom(JSContext
* cx
, JS::Handle
<JSObject
*> proxy
,
117 JS::Handle
<jsid
> id
, JS::Handle
<JS::Value
> v
,
121 * Get the expando object for the given DOM proxy.
123 static JSObject
* GetExpandoObject(JSObject
* obj
);
126 * Clear the expando object for the given DOM proxy and return it. This
127 * function will ensure that the returned object is exposed to active JS if
128 * the given object is exposed.
130 * GetAndClearExpandoObject does not DROP or clear the preserving wrapper
133 static JSObject
* GetAndClearExpandoObject(JSObject
* obj
);
136 * Ensure that the given proxy (obj) has an expando object, and return it.
137 * Returns null on failure.
139 static JSObject
* EnsureExpandoObject(JSContext
* cx
,
140 JS::Handle
<JSObject
*> obj
);
142 static const char family
;
145 // Class used by shadowing handlers (the ones that have [OverrideBuiltins].
146 // This handles tracing the expando in JS::ExpandoAndGeneration.
147 class ShadowingDOMProxyHandler
: public DOMProxyHandler
{
148 virtual void trace(JSTracer
* trc
, JSObject
* proxy
) const override
;
151 inline bool IsDOMProxy(JSObject
* obj
) {
152 return js::IsProxy(obj
) &&
153 js::GetProxyHandler(obj
)->family() == &DOMProxyHandler::family
;
156 inline const DOMProxyHandler
* GetDOMProxyHandler(JSObject
* obj
) {
157 MOZ_ASSERT(IsDOMProxy(obj
));
158 return static_cast<const DOMProxyHandler
*>(js::GetProxyHandler(obj
));
161 } // namespace mozilla::dom
163 #endif /* mozilla_dom_DOMProxyHandler_h */