script/dom/
debuggerglobalscope.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::cell::RefCell;
6
7use base::id::{Index, PipelineId, PipelineNamespaceId};
8use constellation_traits::ScriptToConstellationChan;
9use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, SourceInfo, WorkerId};
10use dom_struct::dom_struct;
11use embedder_traits::resources::{self, Resource};
12use embedder_traits::{JavaScriptEvaluationError, ScriptToEmbedderChan};
13use ipc_channel::ipc::IpcSender;
14use js::jsval::UndefinedValue;
15use js::rust::wrappers::JS_DefineDebuggerObject;
16use net_traits::ResourceThreads;
17use profile_traits::{mem, time};
18use script_bindings::codegen::GenericBindings::DebuggerGetPossibleBreakpointsEventBinding::RecommendedBreakpointLocation;
19use script_bindings::codegen::GenericBindings::DebuggerGlobalScopeBinding::{
20    DebuggerGlobalScopeMethods, NotifyNewSource,
21};
22use script_bindings::realms::InRealm;
23use script_bindings::reflector::DomObject;
24use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
25use storage_traits::StorageThreads;
26
27use crate::dom::bindings::codegen::Bindings::DebuggerGlobalScopeBinding;
28use crate::dom::bindings::error::report_pending_exception;
29use crate::dom::bindings::inheritance::Castable;
30use crate::dom::bindings::root::DomRoot;
31use crate::dom::bindings::utils::define_all_exposed_interfaces;
32use crate::dom::globalscope::GlobalScope;
33use crate::dom::types::{DebuggerAddDebuggeeEvent, DebuggerGetPossibleBreakpointsEvent, Event};
34#[cfg(feature = "testbinding")]
35#[cfg(feature = "webgpu")]
36use crate::dom::webgpu::identityhub::IdentityHub;
37use crate::realms::enter_realm;
38use crate::script_module::ScriptFetchOptions;
39use crate::script_runtime::{CanGc, IntroductionType, JSContext};
40
41#[dom_struct]
42/// Global scope for interacting with the devtools Debugger API.
43///
44/// <https://firefox-source-docs.mozilla.org/js/Debugger/>
45pub(crate) struct DebuggerGlobalScope {
46    global_scope: GlobalScope,
47    #[no_trace]
48    devtools_to_script_sender: IpcSender<DevtoolScriptControlMsg>,
49    #[no_trace]
50    get_possible_breakpoints_result_sender:
51        RefCell<Option<IpcSender<Vec<devtools_traits::RecommendedBreakpointLocation>>>>,
52}
53
54impl DebuggerGlobalScope {
55    /// Create a new heap-allocated `DebuggerGlobalScope`.
56    ///
57    /// `debugger_pipeline_id` is the pipeline id to use when creating the debugger’s [`GlobalScope`]:
58    /// - in normal script threads, it should be set to `PipelineId::new()`, because those threads can generate
59    ///   pipeline ids, and they may contain debuggees from more than one pipeline
60    /// - in web worker threads, it should be set to the pipeline id of the page that created the thread, because
61    ///   those threads can’t generate pipeline ids, and they only contain one debuggee from one pipeline
62    #[allow(unsafe_code, clippy::too_many_arguments)]
63    pub(crate) fn new(
64        debugger_pipeline_id: PipelineId,
65        script_to_devtools_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
66        devtools_to_script_sender: IpcSender<DevtoolScriptControlMsg>,
67        mem_profiler_chan: mem::ProfilerChan,
68        time_profiler_chan: time::ProfilerChan,
69        script_to_constellation_chan: ScriptToConstellationChan,
70        script_to_embedder_chan: ScriptToEmbedderChan,
71        resource_threads: ResourceThreads,
72        storage_threads: StorageThreads,
73        #[cfg(feature = "webgpu")] gpu_id_hub: std::sync::Arc<IdentityHub>,
74        can_gc: CanGc,
75    ) -> DomRoot<Self> {
76        let global = Box::new(Self {
77            global_scope: GlobalScope::new_inherited(
78                debugger_pipeline_id,
79                script_to_devtools_sender,
80                mem_profiler_chan,
81                time_profiler_chan,
82                script_to_constellation_chan,
83                script_to_embedder_chan,
84                resource_threads,
85                storage_threads,
86                MutableOrigin::new(ImmutableOrigin::new_opaque()),
87                ServoUrl::parse_with_base(None, "about:internal/debugger")
88                    .expect("Guaranteed by argument"),
89                None,
90                Default::default(),
91                #[cfg(feature = "webgpu")]
92                gpu_id_hub,
93                None,
94                false,
95                None, // font_context
96            ),
97            devtools_to_script_sender,
98            get_possible_breakpoints_result_sender: RefCell::new(None),
99        });
100        let global =
101            DebuggerGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(GlobalScope::get_cx(), global);
102
103        let realm = enter_realm(&*global);
104        define_all_exposed_interfaces(global.upcast(), InRealm::entered(&realm), can_gc);
105        assert!(unsafe {
106            // Invariants: `cx` must be a non-null, valid JSContext pointer,
107            // and `obj` must be a handle to a JS global object.
108            JS_DefineDebuggerObject(
109                *Self::get_cx(),
110                global.global_scope.reflector().get_jsobject(),
111            )
112        });
113
114        global
115    }
116
117    /// Get the JS context.
118    pub(crate) fn get_cx() -> JSContext {
119        GlobalScope::get_cx()
120    }
121
122    pub(crate) fn as_global_scope(&self) -> &GlobalScope {
123        self.upcast::<GlobalScope>()
124    }
125
126    fn evaluate_js(&self, script: &str, can_gc: CanGc) -> Result<(), JavaScriptEvaluationError> {
127        rooted!(in (*Self::get_cx()) let mut rval = UndefinedValue());
128        self.global_scope.evaluate_js_on_global_with_result(
129            script,
130            rval.handle_mut(),
131            ScriptFetchOptions::default_classic_script(&self.global_scope),
132            self.global_scope.api_base_url(),
133            can_gc,
134            None,
135        )
136    }
137
138    pub(crate) fn execute(&self, can_gc: CanGc) {
139        if self
140            .evaluate_js(&resources::read_string(Resource::DebuggerJS), can_gc)
141            .is_err()
142        {
143            let ar = enter_realm(self);
144            report_pending_exception(Self::get_cx(), true, InRealm::Entered(&ar), can_gc);
145        }
146    }
147
148    pub(crate) fn fire_add_debuggee(
149        &self,
150        can_gc: CanGc,
151        debuggee_global: &GlobalScope,
152        debuggee_pipeline_id: PipelineId,
153        debuggee_worker_id: Option<WorkerId>,
154    ) {
155        let debuggee_pipeline_id =
156            crate::dom::pipelineid::PipelineId::new(self.upcast(), debuggee_pipeline_id, can_gc);
157        let event = DomRoot::upcast::<Event>(DebuggerAddDebuggeeEvent::new(
158            self.upcast(),
159            debuggee_global,
160            &debuggee_pipeline_id,
161            debuggee_worker_id.map(|id| id.to_string().into()),
162            can_gc,
163        ));
164        assert!(
165            DomRoot::upcast::<Event>(event).fire(self.upcast(), can_gc),
166            "Guaranteed by DebuggerAddDebuggeeEvent::new"
167        );
168    }
169
170    pub(crate) fn fire_get_possible_breakpoints(
171        &self,
172        can_gc: CanGc,
173        spidermonkey_id: u32,
174        result_sender: IpcSender<Vec<devtools_traits::RecommendedBreakpointLocation>>,
175    ) {
176        assert!(
177            self.get_possible_breakpoints_result_sender
178                .replace(Some(result_sender))
179                .is_none()
180        );
181        let event = DomRoot::upcast::<Event>(DebuggerGetPossibleBreakpointsEvent::new(
182            self.upcast(),
183            spidermonkey_id,
184            can_gc,
185        ));
186        assert!(
187            DomRoot::upcast::<Event>(event).fire(self.upcast(), can_gc),
188            "Guaranteed by DebuggerGetPossibleBreakpointsEvent::new"
189        );
190    }
191}
192
193impl DebuggerGlobalScopeMethods<crate::DomTypeHolder> for DebuggerGlobalScope {
194    // check-tidy: no specs after this line
195    fn NotifyNewSource(&self, args: &NotifyNewSource) {
196        let Some(devtools_chan) = self.as_global_scope().devtools_chan() else {
197            return;
198        };
199        let pipeline_id = PipelineId {
200            namespace_id: PipelineNamespaceId(args.pipelineId.namespaceId),
201            index: Index::new(args.pipelineId.index).expect("`pipelineId.index` must not be zero"),
202        };
203
204        if let Some(introduction_type) = args.introductionType.as_ref() {
205            // Check the `introductionType` and `url`, decide whether or not to create a source actor, and if so,
206            // tell the devtools server to create a source actor. Based on the Firefox impl in:
207            // - getDebuggerSourceURL() <https://searchfox.org/mozilla-central/rev/85667ab51e4b2a3352f7077a9ee43513049ed2d6/devtools/server/actors/utils/source-url.js#7-42>
208            // - getSourceURL() <https://searchfox.org/mozilla-central/rev/85667ab51e4b2a3352f7077a9ee43513049ed2d6/devtools/server/actors/source.js#67-109>
209            // - resolveSourceURL() <https://searchfox.org/mozilla-central/rev/85667ab51e4b2a3352f7077a9ee43513049ed2d6/devtools/server/actors/source.js#48-66>
210            // - SourceActor#_isInlineSource <https://searchfox.org/mozilla-central/rev/85667ab51e4b2a3352f7077a9ee43513049ed2d6/devtools/server/actors/source.js#130-143>
211            // - SourceActor#url <https://searchfox.org/mozilla-central/rev/85667ab51e4b2a3352f7077a9ee43513049ed2d6/devtools/server/actors/source.js#157-162>
212
213            // Firefox impl: getDebuggerSourceURL(), getSourceURL()
214            // TODO: handle `about:srcdoc` case (see Firefox getDebuggerSourceURL())
215            // TODO: remove trailing details that may have been appended by SpiderMonkey
216            // (currently impossible to do robustly due to <https://bugzilla.mozilla.org/show_bug.cgi?id=1982001>)
217            let url_original = args.url.str();
218            // FIXME: use page/worker url as base here
219            let url_original = ServoUrl::parse(&url_original).ok();
220
221            // If the source has a `urlOverride` (aka `displayURL` aka `//# sourceURL`), it should be a valid url,
222            // possibly relative to the page/worker url, and we should treat the source as coming from that url for
223            // devtools purposes, including the file tree in the Sources tab.
224            // Firefox impl: getSourceURL()
225            let url_override = args
226                .urlOverride
227                .as_ref()
228                .map(|url| url.str())
229                // FIXME: use page/worker url as base here, not `url_original`
230                .and_then(|url| ServoUrl::parse_with_base(url_original.as_ref(), &url).ok());
231
232            // If the `introductionType` is “eval or eval-like”, the `url` won’t be meaningful, so ignore these
233            // sources unless we have a `urlOverride` (aka `displayURL` aka `//# sourceURL`).
234            // Firefox impl: getDebuggerSourceURL(), getSourceURL()
235            if [
236                IntroductionType::INJECTED_SCRIPT_STR,
237                IntroductionType::EVAL_STR,
238                IntroductionType::DEBUGGER_EVAL_STR,
239                IntroductionType::FUNCTION_STR,
240                IntroductionType::JAVASCRIPT_URL_STR,
241                IntroductionType::EVENT_HANDLER_STR,
242                IntroductionType::DOM_TIMER_STR,
243            ]
244            .contains(&&*introduction_type.str()) &&
245                url_override.is_none()
246            {
247                debug!(
248                    "Not creating debuggee: `introductionType` is `{introduction_type}` but no valid url"
249                );
250                return;
251            }
252
253            // Sources with an `introductionType` of `inlineScript` are generally inline, meaning their contents
254            // are a substring of the page markup (hence not known to SpiderMonkey, requiring plumbing in Servo).
255            // But sources with a `urlOverride` are not inline, since they get their own place in the Sources tree.
256            // nor are sources created for `<iframe srcdoc>`, since they are not necessarily a substring of the
257            // page markup as originally sent by the server.
258            // Firefox impl: SourceActor#_isInlineSource
259            // TODO: handle `about:srcdoc` case (see Firefox SourceActor#_isInlineSource)
260            let inline = introduction_type.str() == "inlineScript" && url_override.is_none();
261            let Some(url) = url_override.or(url_original) else {
262                debug!("Not creating debuggee: no valid url");
263                return;
264            };
265
266            let worker_id = args.workerId.as_ref().map(|id| id.parse().unwrap());
267
268            let source_info = SourceInfo {
269                url,
270                introduction_type: introduction_type.str().to_owned(),
271                inline,
272                worker_id,
273                content: (!inline).then(|| args.text.to_string()),
274                content_type: None, // TODO
275                spidermonkey_id: args.spidermonkeyId,
276            };
277            if let Err(error) = devtools_chan.send(ScriptToDevtoolsControlMsg::CreateSourceActor(
278                self.devtools_to_script_sender.clone(),
279                pipeline_id,
280                source_info,
281            )) {
282                warn!("Failed to send to devtools server: {error:?}");
283            }
284        } else {
285            debug!("Not creating debuggee for script with no `introductionType`");
286        }
287    }
288
289    fn GetPossibleBreakpointsResult(
290        &self,
291        event: &DebuggerGetPossibleBreakpointsEvent,
292        result: Vec<RecommendedBreakpointLocation>,
293    ) {
294        info!("GetPossibleBreakpointsResult: {event:?} {result:?}");
295        let sender = self
296            .get_possible_breakpoints_result_sender
297            .take()
298            .expect("Guaranteed by Self::fire_get_possible_breakpoints()");
299        let _ = sender.send(
300            result
301                .into_iter()
302                .map(|entry| devtools_traits::RecommendedBreakpointLocation {
303                    offset: entry.offset,
304                    line_number: entry.lineNumber,
305                    column_number: entry.columnNumber,
306                    is_step_start: entry.isStepStart,
307                })
308                .collect(),
309        );
310    }
311}