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::{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    global: Dom<GlobalScope>,
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: Dom::from_ref(global),
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        result
48    }
49}
50
51impl DebuggerAddDebuggeeEventMethods<crate::DomTypeHolder> for DebuggerAddDebuggeeEvent {
52    // check-tidy: no specs after this line
53    fn Global(&self, cx: script_bindings::script_runtime::JSContext) -> NonNull<JSObject> {
54        // Convert the debuggee global’s reflector to a Value, wrapping it from its originating realm (debuggee realm)
55        // into the active realm (debugger realm) so that it can be passed across compartments.
56        rooted!(in(*cx) let mut result: Value);
57        self.global
58            .reflector()
59            .safe_to_jsval(cx, result.handle_mut());
60        NonNull::new(result.to_object()).unwrap()
61    }
62
63    fn PipelineId(
64        &self,
65    ) -> DomRoot<<crate::DomTypeHolder as script_bindings::DomTypes>::PipelineId> {
66        DomRoot::from_ref(&self.pipeline_id)
67    }
68
69    fn GetWorkerId(&self) -> Option<DOMString> {
70        self.worker_id.clone()
71    }
72
73    fn IsTrusted(&self) -> bool {
74        self.event.IsTrusted()
75    }
76}