/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! Base classes to work with IDL callbacks. use std::default::Default; use std::ffi::CStr; use std::rc::Rc; use js::context::JSContext; use js::jsapi::{Heap, IsCallable, JSObject}; use js::jsval::{JSVal, NullValue, ObjectValue, UndefinedValue}; use js::rust::wrappers2::{EnterRealm, JS_GetProperty, JS_WrapObject, LeaveRealm}; use js::rust::{HandleObject, MutableHandleValue}; use crate::codegen::GenericBindings::WindowBinding::Window_Binding::WindowMethods; use crate::error::{Error, Fallible}; use crate::interfaces::{DocumentHelpers, DomHelpers, GlobalScopeHelpers}; use crate::permanent_root::PermanentRoot; use crate::realms::enter_auto_realm; use crate::reflector::DomObject; use crate::root::Dom; use crate::settings_stack::{run_a_callback, run_a_script}; use crate::{DomTypes, cformat}; pub trait ThisReflector { fn jsobject(&self) -> *mut JSObject; } /// Try to obtain a Window object from a callback target. /// This Window may be different than the callback's associated global if the /// owner has been adopted into a different realm than it was created in. /// As such, the default implementation should be used for any callback target /// that cannot be adopted (i.e. is not a descendant of Node). pub trait OwnerWindow { fn owner_window(&self) -> Option> { None } } impl ThisReflector for T { fn jsobject(&self) -> *mut JSObject { self.reflector().get_jsobject().get() } } impl ThisReflector for HandleObject<'_> { fn jsobject(&self) -> *mut JSObject { self.get() } } impl OwnerWindow for HandleObject<'_> {} /// The exception handling used for a call. #[derive(Clone, Copy, PartialEq)] pub enum ExceptionHandling { /// Report any exception and don't throw it to the caller code. Report, /// Throw any exception to the caller code. Rethrow, } /// A WebIDL callback that is treated as a GC root. pub struct RootedCallback(Rc<(T, PermanentRoot)>); impl> RootedCallback { /// Create a new [TracedCallback] value from this rooted callback. pub fn to_traced(&self) -> TracedCallback where T: for<'a> From<&'a CallbackObject>, { let mut duplicate = Rc::new(T::from(self.callback_holder())); // Note: callback cannot be moved after calling init. match Rc::get_mut(&mut duplicate) { Some(ref mut callback) => unsafe { callback .callback_holder_mut() .init_callback(self.callback()) }, None => unreachable!(), }; TracedCallback(duplicate) } } impl Clone for RootedCallback { fn clone(&self) -> Self { Self(self.0.clone()) } } impl std::ops::Deref for RootedCallback { type Target = T; fn deref(&self) -> &Self::Target { &self.0.0 } } impl js::conversions::ToJSValConvertible for RootedCallback { fn to_jsval(&self, cx: &mut JSContext, rval: MutableHandleValue<'_>) { self.0.0.to_jsval(cx, rval) } } #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] #[derive(JSTraceable, MallocSizeOf, PartialEq)] /// A WebIDL callback value that can only be stored in locations that are /// traced by the GC. pub struct TracedCallback(#[conditional_malloc_size_of] Rc); impl js::gc::Rootable for TracedCallback {} impl Clone for TracedCallback { fn clone(&self) -> Self { Self(self.0.clone()) } } impl std::ops::Deref for TracedCallback { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } #[expect(unsafe_code)] pub(crate) unsafe fn create_callback_rooted>( cx: &JSContext, obj: T, callback: *mut JSObject, ) -> RootedCallback { let mut ret = Rc::new((obj, PermanentRoot::default())); let (callback2, permanent_root) = Rc::get_mut(&mut ret).unwrap(); unsafe { callback2.callback_holder_mut().init_callback(callback); permanent_root.init(cx, callback2.callback(), c"Callback::root"); }; RootedCallback(ret) } impl TracedCallback { pub fn root(&self, cx: &JSContext) -> RootedCallback { // Safety: the callback pointer is valid at this point. unsafe { T::new(cx, self.callback()) } } } /// A common base class for representing IDL callback function and /// callback interface types. #[derive(JSTraceable, MallocSizeOf)] #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] pub struct CallbackObject { /// The underlying `JSObject`. #[ignore_malloc_size_of = "measured by mozjs"] callback: Heap<*mut JSObject>, // TODO(47889): Remove this field once no more uses of Rc remain. permanent_js_root: Option, /// The ["callback context"], that is, the global to use as incumbent /// global when calling the callback. /// /// Looking at the WebIDL standard, it appears as though there would always /// be a value here, but [sometimes] callback functions are created by /// hand-waving without defining the value of the callback context, and /// without any JavaScript code on the stack to grab an incumbent global /// from. /// /// ["callback context"]: https://heycam.github.io/webidl/#dfn-callback-context /// [sometimes]: https://github.com/whatwg/html/issues/2248 incumbent: Option>, } impl CallbackObject { fn new_from_existing(other: &CallbackObject) -> Self { Self { callback: Heap::default(), permanent_js_root: None, incumbent: other.incumbent.clone(), } } fn new_with_exterior_root() -> Self { Self { callback: Heap::default(), permanent_js_root: None, incumbent: D::GlobalScope::incumbent().map(|i| Dom::from_ref(&*i)), } } pub fn get(&self) -> *mut JSObject { self.callback.get() } #[expect(unsafe_code)] unsafe fn init_callback(&mut self, callback: *mut JSObject) { self.callback.set(callback); } } impl PartialEq for CallbackObject { fn eq(&self, other: &CallbackObject) -> bool { self.callback.get() == other.callback.get() } } /// A type which can obtain a reference to a CallbackObject member. pub trait HasCallbackHolder { type D: DomTypes; /// Returns the underlying `CallbackObject`. fn callback_holder(&self) -> &CallbackObject; /// Returns the underlying `CallbackObject`. fn callback_holder_mut(&mut self) -> &mut CallbackObject; /// Returns the underlying `JSObject`. fn callback(&self) -> *mut JSObject { self.callback_holder().get() } } /// A trait to be implemented by concrete IDL callback function and /// callback interface types. pub trait CallbackContainer { /// Create a new rooted CallbackContainer object for the given `JSObject`. /// /// # Safety /// `callback` must point to a valid, non-null JSObject. unsafe fn new(cx: &JSContext, callback: *mut JSObject) -> RootedCallback where Self: Sized; } /// A trait to be implemented by concrete IDL callback function and /// callback interface types that have not yet been converted to RootedCallback. pub trait DeprecatedCallbackContainer { /// Create a new CallbackContainer object for the given `JSObject`. /// /// *Deprecated*: Use [CallbackContainer] instead. /// /// # Safety /// `callback` must point to a valid, non-null JSObject. unsafe fn new(cx: &JSContext, callback: *mut JSObject) -> Rc; } /// A common base class for representing IDL callback function types. #[derive(JSTraceable, MallocSizeOf, PartialEq)] #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] pub struct CallbackFunction { object: CallbackObject, } impl<'a, D: DomTypes> From<&'a CallbackObject> for CallbackFunction { fn from(object: &'a CallbackObject) -> Self { Self { object: CallbackObject::new_from_existing(object), } } } impl CallbackFunction { /// Create a new `CallbackFunction` for this object, with rooting provided /// by the caller. pub(crate) fn new_with_exterior_root() -> Self { Self { object: CallbackObject::new_with_exterior_root(), } } } impl HasCallbackHolder for CallbackFunction { type D = D; /// Returns the underlying `CallbackObject`. fn callback_holder(&self) -> &CallbackObject { &self.object } fn callback_holder_mut(&mut self) -> &mut CallbackObject { &mut self.object } } /// A common base class for representing IDL callback interface types. #[derive(JSTraceable, MallocSizeOf, PartialEq)] #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] pub struct CallbackInterface { object: CallbackObject, } impl<'a, D: DomTypes> From<&'a CallbackObject> for CallbackInterface { fn from(object: &'a CallbackObject) -> Self { Self { object: CallbackObject::new_from_existing(object), } } } impl HasCallbackHolder for CallbackInterface { type D = D; /// Returns the underlying `CallbackObject`. fn callback_holder(&self) -> &CallbackObject { &self.object } fn callback_holder_mut(&mut self) -> &mut CallbackObject { &mut self.object } } impl CallbackInterface { /// Create a new CallbackInterface object with rooting provided by the caller. pub(crate) fn new_with_exterior_root() -> Self { Self { object: CallbackObject::new_with_exterior_root(), } } /// Returns the property with the given `name`, if it is a callable object, /// or an error otherwise. pub fn get_callable_property(&self, cx: &mut JSContext, name: &CStr) -> Fallible { rooted!(&in(cx) let mut callable = UndefinedValue()); rooted!(&in(cx) let obj = self.callback_holder().get()); unsafe { if !JS_GetProperty(cx, obj.handle(), name.as_ptr(), callable.handle_mut()) { return Err(Error::JSFailed); } if !callable.is_object() || !IsCallable(callable.to_object()) { return Err(Error::Type(cformat!( "The value of the {} property is not callable", name.to_string_lossy() ))); } } Ok(callable.get()) } } /// Wraps the reflector for `p` into the realm of `cx`. pub(crate) fn wrap_call_this_value( cx: &mut JSContext, p: &T, mut rval: MutableHandleValue, ) -> bool { rooted!(&in(cx) let mut obj = p.jsobject()); if obj.is_null() { rval.set(NullValue()); return true; } unsafe { if !JS_WrapObject(cx, obj.handle_mut()) { return false; } } rval.set(ObjectValue(*obj)); true } /// A function wrapper that performs whatever setup we need to safely make a call. /// /// pub(crate) fn call_setup, R>( cx: &mut JSContext, callback: &T, owner_window: Option<&D::Window>, handling: ExceptionHandling, f: impl FnOnce(&mut JSContext) -> R, ) -> R { if let Some(window) = owner_window { window.Document().ensure_safe_to_run_script_or_layout(); } // The global for reporting exceptions. This is the global object of the // (possibly wrapped) callback object. let global = unsafe { D::GlobalScope::from_object(callback.callback()) }; let global = &global; // Step 8: Prepare to run script with relevant settings. run_a_script::(cx, global, move |cx| { let actual_callback = || { let old_realm = unsafe { EnterRealm(cx, callback.callback()) }; let result = f(cx); unsafe { LeaveRealm(cx, old_realm); } if handling == ExceptionHandling::Report { let mut realm = enter_auto_realm::(cx, &**global); let cx = &mut realm.current_realm(); >::report_pending_exception(cx); } result }; if let Some(incumbent_global) = callback.callback_holder().incumbent.as_deref() { // Step 9: Prepare to run a callback with stored settings. run_a_callback::(incumbent_global, actual_callback) } else { actual_callback() } }) // Step 14.2: Clean up after running script with relevant settings. }