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