Skip to main content

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