script/dom/
debuggeradddebuggeeevent.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::ptr::NonNull;
6
7use dom_struct::dom_struct;
8use js::jsapi::{Heap, JSObject, Value};
9use script_bindings::conversions::SafeToJSValConvertible;
10use script_bindings::reflector::DomObject;
11use script_bindings::str::DOMString;
12
13use crate::dom::bindings::codegen::Bindings::DebuggerAddDebuggeeEventBinding::DebuggerAddDebuggeeEventMethods;
14use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
15use crate::dom::bindings::reflector::reflect_dom_object;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::event::Event;
18use crate::dom::types::{GlobalScope, PipelineId};
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22/// Event for Rust → JS calls in [`crate::dom::DebuggerGlobalScope`].
23pub(crate) struct DebuggerAddDebuggeeEvent {
24    event: Event,
25    #[ignore_malloc_size_of = "Measured by the JS engine"]
26    global: Heap<*mut JSObject>,
27    pipeline_id: Dom<PipelineId>,
28    worker_id: Option<DOMString>,
29}
30
31impl DebuggerAddDebuggeeEvent {
32    pub(crate) fn new(
33        debugger_global: &GlobalScope,
34        global: &GlobalScope,
35        pipeline_id: &PipelineId,
36        worker_id: Option<DOMString>,
37        can_gc: CanGc,
38    ) -> DomRoot<Self> {
39        let result = Box::new(Self {
40            event: Event::new_inherited(),
41            global: Heap::default(),
42            pipeline_id: Dom::from_ref(pipeline_id),
43            worker_id,
44        });
45        let result = reflect_dom_object(result, debugger_global, can_gc);
46        result.event.init_event("addDebuggee".into(), false, false);
47
48        // Convert the debuggee global’s reflector to a Value, wrapping it from its originating realm (debuggee realm)
49        // into the active realm (debugger realm) so that it can be passed across compartments.
50        let cx = GlobalScope::get_cx();
51        rooted!(in(*cx) let mut wrapped_global: Value);
52        global
53            .reflector()
54            .safe_to_jsval(cx, wrapped_global.handle_mut(), can_gc);
55        result.global.set(wrapped_global.to_object());
56
57        result
58    }
59}
60
61impl DebuggerAddDebuggeeEventMethods<crate::DomTypeHolder> for DebuggerAddDebuggeeEvent {
62    // check-tidy: no specs after this line
63    fn Global(&self, _cx: script_bindings::script_runtime::JSContext) -> NonNull<JSObject> {
64        NonNull::new(self.global.get()).unwrap()
65    }
66
67    fn PipelineId(
68        &self,
69    ) -> DomRoot<<crate::DomTypeHolder as script_bindings::DomTypes>::PipelineId> {
70        DomRoot::from_ref(&self.pipeline_id)
71    }
72
73    fn GetWorkerId(&self) -> Option<DOMString> {
74        self.worker_id.clone()
75    }
76
77    fn IsTrusted(&self) -> bool {
78        self.event.IsTrusted()
79    }
80}