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::glue::JSPrincipalsCallbacks;
9use js::jsapi::{CallArgs, HandleObject as RawHandleObject, JSContext as RawJSContext, JSObject};
10use js::rust::{HandleObject, MutableHandleObject};
11use servo_url::{MutableOrigin, ServoUrl};
12
13use crate::DomTypes;
14use crate::codegen::PrototypeList;
15use crate::conversions::DerivedFrom;
16use crate::error::Error;
17use crate::realms::InRealm;
18use crate::reflector::{DomObject, DomObjectWrap};
19use crate::root::DomRoot;
20use crate::script_runtime::{CanGc, JSContext};
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(JSContext, HandleObject),
31    /// Returns true if this interface's conditions are met for the given global.
32    pub enabled: fn(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: JSContext, global: &D::GlobalScope, result: Error, can_gc: CanGc);
38
39    fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
40        cx: JSContext,
41        args: &CallArgs,
42        global: &D::GlobalScope,
43        proto_id: PrototypeList::ID,
44        creator: unsafe fn(JSContext, HandleObject, *mut ProtoOrIfaceArray),
45        can_gc: CanGc,
46    ) -> bool;
47
48    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
49
50    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
51
52    fn is_platform_object_same_origin(cx: JSContext, obj: RawHandleObject) -> bool;
53
54    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
55
56    fn push_new_element_queue();
57    fn pop_current_element_queue(can_gc: CanGc);
58
59    fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
60    where
61        T: DomObject + DomObjectWrap<D>,
62        U: DerivedFrom<D::GlobalScope>;
63
64    fn report_pending_exception(cx: JSContext, dispatch_event: bool, realm: InRealm, can_gc: CanGc);
65}
66
67/// Operations that must be invoked from the generated bindings.
68#[allow(unsafe_code)]
69pub trait GlobalScopeHelpers<D: DomTypes> {
70    /// # Safety
71    /// `cx` must point to a valid, non-null RawJSContext.
72    unsafe fn from_context(cx: *mut RawJSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
73    fn get_cx() -> JSContext;
74    /// # Safety
75    /// `obj` must point to a valid, non-null JSObject.
76    unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
77    fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
78
79    fn origin(&self) -> &MutableOrigin;
80
81    fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
82
83    fn perform_a_microtask_checkpoint(&self, can_gc: CanGc);
84
85    fn get_url(&self) -> ServoUrl;
86
87    fn is_secure_context(&self) -> bool;
88}
89
90pub trait DocumentHelpers {
91    fn ensure_safe_to_run_script_or_layout(&self);
92}
93
94pub trait ServoInternalsHelpers {
95    fn is_servo_internal(cx: JSContext, global: HandleObject) -> bool;
96}
97
98pub trait TestBindingHelpers {
99    fn condition_satisfied(cx: JSContext, global: HandleObject) -> bool;
100    fn condition_unsatisfied(cx: JSContext, global: HandleObject) -> bool;
101}
102
103pub trait WebGL2RenderingContextHelpers {
104    fn is_webgl2_enabled(cx: JSContext, global: HandleObject) -> bool;
105}
106
107pub trait WindowHelpers {
108    fn create_named_properties_object(
109        cx: JSContext,
110        proto: HandleObject,
111        object: MutableHandleObject,
112    );
113}