Skip to main content

script/dom/debugger/
debuggerblackboxevent.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 devtools_traits::BlackboxCoverage;
6use devtools_traits::BlackboxCoverage::{Full, Partial};
7use dom_struct::dom_struct;
8use script_bindings::codegen::GenericBindings::DebuggerBlackboxEventBinding::DebuggerBlackboxEventMethods;
9use script_bindings::reflector::reflect_dom_object;
10
11use crate::dom::bindings::codegen::Bindings::DebuggerGlobalScopeBinding::DebuggerSourceLocation;
12use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::event::Event;
15use crate::dom::types::GlobalScope;
16use crate::script_runtime::CanGc;
17
18#[dom_struct]
19/// Event for Rust → JS calls in [`crate::dom::debugger::DebuggerGlobalScope`].
20pub(crate) struct DebuggerBlackboxEvent {
21    event: Event,
22    spidermonkey_id: u32,
23    covers_full_source: bool,
24
25    // Only relevant if covers_full_source is false
26    start_line: u32,
27    start_column: u32,
28    end_line: u32,
29    end_column: u32,
30}
31
32impl DebuggerBlackboxEvent {
33    pub(crate) fn new(
34        debugger_global: &GlobalScope,
35        spidermonkey_id: u32,
36        coverage: BlackboxCoverage,
37        can_gc: CanGc,
38    ) -> DomRoot<Self> {
39        let result = Box::new(match coverage {
40            Partial((start_line, start_column), (end_line, end_column)) => Self {
41                event: Event::new_inherited(),
42                spidermonkey_id,
43                covers_full_source: false,
44                start_line,
45                start_column,
46                end_line,
47                end_column,
48            },
49            Full => Self {
50                event: Event::new_inherited(),
51                spidermonkey_id,
52                covers_full_source: true,
53                start_line: 0,
54                start_column: 0,
55                end_line: 0,
56                end_column: 0,
57            },
58        });
59
60        let result = reflect_dom_object(result, debugger_global, can_gc);
61        result.event.init_event("blackbox".into(), false, false);
62
63        result
64    }
65}
66
67impl DebuggerBlackboxEventMethods<crate::DomTypeHolder> for DebuggerBlackboxEvent {
68    // check-tidy: no specs after this line
69    fn SpidermonkeyId(&self) -> u32 {
70        self.spidermonkey_id
71    }
72
73    fn CoversFullSource(&self) -> bool {
74        self.covers_full_source
75    }
76
77    fn Start(&self) -> DebuggerSourceLocation {
78        DebuggerSourceLocation {
79            line: self.start_line,
80            column: self.start_column,
81        }
82    }
83
84    fn End(&self) -> DebuggerSourceLocation {
85        DebuggerSourceLocation {
86            line: self.end_line,
87            column: self.end_column,
88        }
89    }
90
91    fn IsTrusted(&self) -> bool {
92        self.event.IsTrusted()
93    }
94}