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, HandleObject as RawHandleObject, JSContext as RawJSContext, 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::realms::InRealm;
20use crate::reflector::{DomObject, DomObjectWrap};
21use crate::root::DomRoot;
22use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
23use crate::settings_stack::StackEntry;
24use crate::utils::ProtoOrIfaceArray;
25
26/// Operations that can be invoked for a WebIDL interface against
27/// a global object.
28///
29/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/WebIDLGlobalNameHash.h#L24>
30pub struct Interface {
31    /// Define the JS object for this interface on the given global.
32    pub define: fn(&mut JSContext, HandleObject),
33    /// Returns true if this interface's conditions are met for the given global.
34    pub enabled: fn(&mut JSContext, HandleObject) -> bool,
35}
36
37/// Operations that must be invoked from the generated bindings.
38pub trait DomHelpers<D: DomTypes> {
39    fn throw_dom_exception(cx: &mut JSContext, global: &D::GlobalScope, result: Error);
40
41    fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
42        cx: &mut JSContext,
43        args: &CallArgs,
44        global: &D::GlobalScope,
45        proto_id: PrototypeList::ID,
46        creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray),
47    ) -> bool;
48
49    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
50
51    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
52
53    fn is_platform_object_same_origin(cx: &CurrentRealm, obj: RawHandleObject) -> bool;
54
55    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
56
57    fn push_new_element_queue();
58    fn pop_current_element_queue(cx: &mut JSContext);
59
60    fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
61    where
62        T: DomObject + DomObjectWrap<D>,
63        U: DerivedFrom<D::GlobalScope>;
64
65    fn report_pending_exception(cx: &mut CurrentRealm);
66}
67
68/// Operations that must be invoked from the generated bindings.
69#[expect(unsafe_code)]
70pub trait GlobalScopeHelpers<D: DomTypes> {
71    fn from_current_realm(realm: &'_ CurrentRealm) -> DomRoot<D::GlobalScope>;
72    /// # Safety
73    /// `cx` must point to a valid, non-null RawJSContext.
74    unsafe fn from_context(cx: *mut RawJSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
75    fn get_cx() -> SafeJSContext;
76    /// # Safety
77    /// `obj` must point to a valid, non-null JSObject.
78    unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
79    fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
80
81    fn origin(&self) -> &MutableOrigin;
82
83    fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
84
85    fn perform_a_microtask_checkpoint(&self, cx: &mut JSContext);
86
87    fn get_url(&self) -> ServoUrl;
88
89    fn is_secure_context(&self) -> bool;
90}
91
92pub trait DocumentHelpers {
93    fn ensure_safe_to_run_script_or_layout(&self);
94}
95
96pub trait ServoInternalsHelpers {
97    fn is_servo_internal(cx: &mut JSContext, global: HandleObject) -> bool;
98}
99
100pub trait TestBindingHelpers {
101    fn condition_satisfied(cx: &mut JSContext, global: HandleObject) -> bool;
102    fn condition_unsatisfied(cx: &mut JSContext, global: HandleObject) -> bool;
103}
104
105pub trait WebGL2RenderingContextHelpers {
106    fn is_webgl2_enabled(cx: &mut JSContext, global: HandleObject) -> bool;
107}
108
109pub trait WindowHelpers {
110    fn create_named_properties_object(
111        cx: &mut JSContext,
112        proto: HandleObject,
113        object: MutableHandleObject,
114    );
115}