Skip to main content

script/dom/debugger/
debuggergetenvironmentevent.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::fmt::Debug;
6
7use devtools_traits::GetEnvironmentRequest;
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use script_bindings::inheritance::Castable;
11use script_bindings::reflector::reflect_dom_object_with_cx;
12use script_bindings::str::DOMString;
13
14use crate::dom::GlobalScope;
15use crate::dom::bindings::codegen::Bindings::DebuggerGetEnvironmentEventBinding::DebuggerGetEnvironmentEventMethods;
16use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::event::Event;
19use crate::dom::pipelineid::PipelineId;
20use crate::dom::types::DebuggerGlobalScope;
21
22#[dom_struct]
23/// Event for Rust → JS calls in [`crate::dom::debugger::DebuggerGlobalScope`].
24pub(crate) struct DebuggerGetEnvironmentEvent {
25    event: Event,
26    frame_actor_id: Option<DOMString>,
27    pipeline_id: Option<DomRoot<PipelineId>>,
28}
29
30impl DebuggerGetEnvironmentEvent {
31    pub(crate) fn new(
32        cx: &mut JSContext,
33        debugger_global_scope: &DebuggerGlobalScope,
34        request: GetEnvironmentRequest,
35    ) -> DomRoot<Self> {
36        let result = Box::new(match request {
37            GetEnvironmentRequest::Frame(frame_actor_id) => Self {
38                event: Event::new_inherited(),
39                frame_actor_id: Some(frame_actor_id.into()),
40                pipeline_id: None,
41            },
42            GetEnvironmentRequest::Global(debuggee_pipeline_id) => {
43                let debuggee_pipeline_id = crate::dom::pipelineid::PipelineId::new(
44                    cx,
45                    debugger_global_scope.upcast(),
46                    debuggee_pipeline_id,
47                );
48                Self {
49                    event: Event::new_inherited(),
50                    frame_actor_id: None,
51                    pipeline_id: Some(debuggee_pipeline_id),
52                }
53            },
54        });
55        let result =
56            reflect_dom_object_with_cx(result, debugger_global_scope.upcast::<GlobalScope>(), cx);
57        result
58            .event
59            .init_event("getEnvironment".into(), false, false);
60
61        result
62    }
63}
64
65impl DebuggerGetEnvironmentEventMethods<crate::DomTypeHolder> for DebuggerGetEnvironmentEvent {
66    // check-tidy: no specs after this line
67    fn GetFrameActorId(&self) -> Option<DOMString> {
68        self.frame_actor_id.clone()
69    }
70
71    fn GetPipelineId(
72        &self,
73    ) -> Option<DomRoot<<crate::DomTypeHolder as script_bindings::DomTypes>::PipelineId>> {
74        self.pipeline_id.clone()
75    }
76
77    fn IsTrusted(&self) -> bool {
78        self.event.IsTrusted()
79    }
80}
81
82impl Debug for DebuggerGetEnvironmentEvent {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        f.debug_struct("DebuggerGetEnvironmentEvent").finish()
85    }
86}