Skip to main content

script_bindings/
interfaces.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::RefCell;
6use std::thread::LocalKey;
7
8use js::context::JSContext;
9use js::glue::JSPrincipalsCallbacks;
10use js::jsapi::{CallArgs, JSObject};
11use js::realm::CurrentRealm;
12use js::rust::{HandleObject, MutableHandleObject};
13use servo_url::{MutableOrigin, ServoUrl};
14
15use crate::DomTypes;
16use crate::codegen::PrototypeList;
17use crate::conversions::DerivedFrom;
18use crate::error::Error;
19use crate::reflector::{DomObject, DomObjectWrap};
20use crate::root::DomRoot;
21use crate::settings_stack::StackEntry;
22use crate::utils::ProtoOrIfaceArray;
23
24/// Operations that can be invoked for a WebIDL interface against
25/// a global object.
26///
27/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/WebIDLGlobalNameHash.h#L24>
28pub struct Interface {
29    /// Define the JS object for this interface on the given global.
30    pub define: fn(&mut JSContext, HandleObject),
31    /// Returns true if this interface's conditions are met for the given global.
32    pub enabled: fn(&mut JSContext, HandleObject) -> bool,
33}
34
35/// Operations that must be invoked from the generated bindings.
36pub trait DomHelpers<D: DomTypes> {
37    fn throw_dom_exception(cx: &mut JSContext, global: &D::GlobalScope, result: Error);
38
39    fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
40        cx: &mut JSContext,
41        args: &CallArgs,
42        global: &D::GlobalScope,
43        proto_id: PrototypeList::ID,
44        creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray),
45    ) -> bool;
46
47    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
48
49    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
50
51    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
52
53    fn push_new_element_queue();
54    fn pop_current_element_queue(cx: &mut JSContext);
55
56    fn reflect_dom_object_with_cx<T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T>
57    where
58        T: DomObject + DomObjectWrap<D>,
59        U: DerivedFrom<D::GlobalScope>;
60
61    fn report_pending_exception(cx: &mut CurrentRealm);
62}
63
64/// Operations that must be invoked from the generated bindings.
65#[expect(unsafe_code)]
66pub trait GlobalScopeHelpers<D: DomTypes> {
67    fn from_current_realm(realm: &'_ CurrentRealm) -> DomRoot<D::GlobalScope>;
68
69    /// # Safety
70    /// `obj` must point to a valid, non-null JSObject.
71    unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
72    fn from_reflector(reflector: &impl DomObject) -> DomRoot<D::GlobalScope>;
73
74    fn origin(&self) -> MutableOrigin;
75
76    fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
77
78    fn perform_a_microtask_checkpoint(&self, cx: &mut JSContext);
79
80    fn get_url(&self) -> ServoUrl;
81
82    fn is_secure_context(&self) -> bool;
83}
84
85pub trait DocumentHelpers {
86    fn ensure_safe_to_run_script_or_layout(&self);
87}
88
89pub trait ServoInternalsHelpers {
90    fn is_servo_internal(cx: &mut JSContext, global: HandleObject) -> bool;
91}
92
93pub trait TestBindingHelpers {
94    fn condition_satisfied(cx: &mut JSContext, global: HandleObject) -> bool;
95    fn condition_unsatisfied(cx: &mut JSContext, global: HandleObject) -> bool;
96}
97
98pub trait WebGL2RenderingContextHelpers {
99    fn is_webgl2_enabled(cx: &mut JSContext, global: HandleObject) -> bool;
100}
101
102pub trait WindowHelpers {
103    fn create_named_properties_object(
104        cx: &mut JSContext,
105        proto: HandleObject,
106        object: MutableHandleObject,
107    );
108}
109
110pub trait HasOrigin {
111    fn origin(&self) -> MutableOrigin;
112}