/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![cfg_attr(crown, allow(crown::jscontext_first_arg))]
use std::cell::RefCell;
use devtools_traits::{
BlackboxCoverage, DebuggerValue, DevtoolScriptControlMsg, EvaluateJSReply,
GetEnvironmentRequest, ScriptToDevtoolsControlMsg, SourceInfo, WorkerId,
};
use dom_struct::dom_struct;
use embedder_traits::ScriptToEmbedderChan;
use embedder_traits::resources::{self, Resource};
use js::context::JSContext;
use js::rust::wrappers2::JS_DefineDebuggerObject;
use net_traits::ResourceThreads;
use profile_traits::{mem, time};
use script_bindings::interfaces::HasOrigin;
use script_bindings::reflector::DomObject;
use servo_base::generic_channel::{GenericCallback, GenericSender, channel};
use servo_base::id::{Index, PipelineId, PipelineNamespaceId};
use servo_constellation_traits::ScriptToConstellationSender;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use storage_traits::StorageThreads;
use crate::dom::bindings::codegen::Bindings::DebuggerGetEnvironmentEventBinding::EnvironmentInfo;
use crate::dom::bindings::codegen::Bindings::DebuggerGlobalScopeBinding;
use crate::dom::bindings::codegen::Bindings::DebuggerInterruptEventBinding::{
FrameInfo, FrameOffset, PauseReason,
};
use crate::dom::bindings::codegen::GenericBindings::DebuggerEvalEventBinding::EvalResult;
use crate::dom::bindings::codegen::GenericBindings::DebuggerGetPossibleBreakpointsEventBinding::RecommendedBreakpointLocation;
use crate::dom::bindings::codegen::GenericBindings::DebuggerGlobalScopeBinding::{
DebuggerGlobalScopeMethods, NotifyNewSource, PipelineIdInit,
};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::utils::define_all_exposed_interfaces;
use crate::dom::debugger::debuggerblackboxevent::DebuggerBlackboxEvent;
use crate::dom::debugger::debuggerclearbreakpointevent::DebuggerClearBreakpointEvent;
use crate::dom::debugger::debuggerframeevent::DebuggerFrameEvent;
use crate::dom::debugger::debuggergetenvironmentevent::DebuggerGetEnvironmentEvent;
use crate::dom::debugger::debuggerinterruptevent::DebuggerInterruptEvent;
use crate::dom::debugger::debuggerresumeevent::DebuggerResumeEvent;
use crate::dom::debugger::debuggersetbreakpointevent::DebuggerSetBreakpointEvent;
use crate::dom::debugger::debuggerunblackboxevent::DebuggerUnblackboxEvent;
use crate::dom::globalscope::GlobalScope;
use crate::dom::types::{
DebuggerAddDebuggeeEvent, DebuggerEvalEvent, DebuggerGetPossibleBreakpointsEvent, Event,
};
#[cfg(feature = "webgpu")]
use crate::dom::webgpu::identityhub::IdentityHub;
use crate::event_loop::script_thread::with_script_thread;
use crate::realms::enter_auto_realm;
use crate::runtime::script_runtime::IntroductionType;
#[dom_struct]
/// Global scope for interacting with the devtools Debugger API.
///
///
pub(crate) struct DebuggerGlobalScope {
global_scope: GlobalScope,
#[no_trace]
devtools_to_script_sender: GenericSender,
#[no_trace]
get_possible_breakpoints_result_sender:
RefCell>>>,
#[no_trace]
eval_result_sender: RefCell >>,
#[no_trace]
get_list_frame_result_sender: RefCell >>>,
#[no_trace]
get_environment_result_sender: RefCell >>,
#[no_trace]
pipeline_id: PipelineId,
#[no_trace]
origin: MutableOrigin,
}
impl DebuggerGlobalScope {
/// Create a new heap-allocated `DebuggerGlobalScope`.
///
/// `debugger_pipeline_id` is the pipeline id to use when creating the debugger’s [`GlobalScope`]:
/// - in normal script threads, it should be set to `PipelineId::new()`, because those threads can generate
/// pipeline ids, and they may contain debuggees from more than one pipeline
/// - in web worker threads, it should be set to the pipeline id of the page that created the thread, because
/// those threads can’t generate pipeline ids, and they only contain one debuggee from one pipeline
#[expect(unsafe_code, clippy::too_many_arguments)]
pub(crate) fn new(
debugger_pipeline_id: PipelineId,
script_to_devtools_sender: Option>,
devtools_to_script_sender: GenericSender,
mem_profiler_chan: mem::ProfilerChan,
time_profiler_chan: time::ProfilerChan,
script_to_constellation_sender: ScriptToConstellationSender,
script_to_embedder_chan: ScriptToEmbedderChan,
resource_threads: ResourceThreads,
storage_threads: StorageThreads,
#[cfg(feature = "webgpu")] gpu_id_hub: std::sync::Arc,
cx: &mut JSContext,
) -> DomRoot {
let global = Box::new(Self {
global_scope: GlobalScope::new_inherited(
script_to_devtools_sender,
mem_profiler_chan,
time_profiler_chan,
script_to_constellation_sender,
script_to_embedder_chan,
resource_threads,
storage_threads,
ServoUrl::parse_with_base(None, "about:internal/debugger")
.expect("Guaranteed by argument"),
None,
#[cfg(feature = "webgpu")]
gpu_id_hub,
None,
false,
),
devtools_to_script_sender,
get_possible_breakpoints_result_sender: RefCell::new(None),
get_list_frame_result_sender: RefCell::new(None),
get_environment_result_sender: RefCell::new(None),
eval_result_sender: RefCell::new(None),
pipeline_id: debugger_pipeline_id,
origin: MutableOrigin::new(ImmutableOrigin::new_opaque()),
});
let global =
DebuggerGlobalScopeBinding::Wrap::(cx, &global.origin(), global);
let mut realm = enter_auto_realm(cx, &*global);
let mut realm = realm.current_realm();
define_all_exposed_interfaces(&mut realm, global.upcast());
assert!(unsafe {
// Invariants: `obj` must be a handle to a JS global object.
JS_DefineDebuggerObject(&mut realm, global.global_scope.reflector().get_jsobject())
});
global
}
pub(crate) fn origin(&self) -> MutableOrigin {
self.origin.clone()
}
pub(crate) fn as_global_scope(&self) -> &GlobalScope {
self.upcast::()
}
pub(crate) fn execute(&self, cx: &mut JSContext) {
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
let _ = self.global_scope.evaluate_js_on_global(
cx,
resources::read_string(Resource::DebuggerJS).into(),
"",
None,
None,
);
}
pub(crate) fn fire_add_debuggee(
&self,
cx: &mut JSContext,
debuggee_global: &GlobalScope,
debuggee_pipeline_id: PipelineId,
debuggee_worker_id: Option,
) {
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm;
let debuggee_pipeline_id =
crate::dom::pipelineid::PipelineId::new(cx, self.upcast(), debuggee_pipeline_id);
let event = DomRoot::upcast::(DebuggerAddDebuggeeEvent::new(
cx,
self.upcast(),
debuggee_global,
&debuggee_pipeline_id,
debuggee_worker_id.map(|id| id.to_string().into()),
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerAddDebuggeeEvent::new"
);
}
#[expect(clippy::too_many_arguments)]
pub(crate) fn fire_eval(
&self,
cx: &mut JSContext,
code: DOMString,
debuggee_pipeline_id: PipelineId,
debuggee_worker_id: Option,
frame_actor_id: Option,
eager: bool,
result_sender: GenericSender,
) {
assert!(
self.eval_result_sender
.replace(Some(result_sender))
.is_none()
);
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm;
let debuggee_pipeline_id =
crate::dom::pipelineid::PipelineId::new(cx, self.upcast(), debuggee_pipeline_id);
let event = DomRoot::upcast::(DebuggerEvalEvent::new(
cx,
self.upcast(),
code,
&debuggee_pipeline_id,
debuggee_worker_id.map(|id| id.to_string().into()),
frame_actor_id.map(|id| id.into()),
eager,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerEvalEvent::new"
);
}
pub(crate) fn fire_get_possible_breakpoints(
&self,
cx: &mut JSContext,
spidermonkey_id: u32,
result_sender: GenericSender>,
) {
assert!(
self.get_possible_breakpoints_result_sender
.replace(Some(result_sender))
.is_none()
);
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
let event = DomRoot::upcast::(DebuggerGetPossibleBreakpointsEvent::new(
cx,
self.upcast(),
spidermonkey_id,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerGetPossibleBreakpointsEvent::new"
);
}
pub(crate) fn fire_set_breakpoint(
&self,
cx: &mut JSContext,
spidermonkey_id: u32,
script_id: u32,
offset: u32,
) {
let event = DomRoot::upcast::(DebuggerSetBreakpointEvent::new(
cx,
self.upcast(),
spidermonkey_id,
script_id,
offset,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerSetBreakpointEvent::new"
);
}
pub(crate) fn fire_interrupt(&self, cx: &mut js::context::JSContext) {
let event = DomRoot::upcast::(DebuggerInterruptEvent::new(cx, self.upcast()));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerInterruptEvent::new"
);
}
pub(crate) fn fire_list_frames(
&self,
cx: &mut js::context::JSContext,
pipeline_id: PipelineId,
start: u32,
count: u32,
result_sender: GenericSender>,
) {
assert!(
self.get_list_frame_result_sender
.replace(Some(result_sender))
.is_none()
);
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
let pipeline_id = crate::dom::pipelineid::PipelineId::new(cx, self.upcast(), pipeline_id);
let event = DomRoot::upcast::(DebuggerFrameEvent::new(
cx,
self.upcast(),
&pipeline_id,
start,
count,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerFrameEvent::new"
);
}
pub(crate) fn fire_get_environment(
&self,
cx: &mut JSContext,
request: GetEnvironmentRequest,
result_sender: GenericSender,
) {
assert!(
self.get_environment_result_sender
.replace(Some(result_sender))
.is_none()
);
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
let event = DomRoot::upcast::(DebuggerGetEnvironmentEvent::new(cx, self, request));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerGetEnvironmentEvent::new"
);
}
pub(crate) fn fire_resume(
&self,
cx: &mut JSContext,
resume_limit_type: Option,
frame_actor_id: Option,
) {
let event = DomRoot::upcast::(DebuggerResumeEvent::new(
cx,
self.upcast(),
resume_limit_type.map(DOMString::from),
frame_actor_id.map(DOMString::from),
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerResumeEvent::new"
);
}
pub(crate) fn fire_clear_breakpoint(
&self,
cx: &mut JSContext,
spidermonkey_id: u32,
script_id: u32,
offset: u32,
) {
let event = DomRoot::upcast::(DebuggerClearBreakpointEvent::new(
cx,
self.upcast(),
spidermonkey_id,
script_id,
offset,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerClearBreakpointEvent::new"
);
}
pub(crate) fn fire_blackbox(
&self,
cx: &mut JSContext,
spidermonkey_id: u32,
coverage: BlackboxCoverage,
) {
let event = DomRoot::upcast::(DebuggerBlackboxEvent::new(
cx,
self.upcast(),
spidermonkey_id,
coverage,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerBlackboxEvent::new"
);
}
pub(crate) fn fire_unblackbox(
&self,
cx: &mut JSContext,
spidermonkey_id: u32,
coverage: BlackboxCoverage,
) {
let event = DomRoot::upcast::(DebuggerUnblackboxEvent::new(
cx,
self.upcast(),
spidermonkey_id,
coverage,
));
assert!(
event.fire(cx, self.upcast()),
"Guaranteed by DebuggerUnblackboxEvent::new"
);
}
pub(crate) fn pipeline_id(&self) -> PipelineId {
self.pipeline_id
}
}
impl DebuggerGlobalScopeMethods for DebuggerGlobalScope {
// check-tidy: no specs after this line
fn NotifyNewSource(&self, args: &NotifyNewSource) {
let Some(devtools_chan) = self.as_global_scope().devtools_chan() else {
return;
};
let pipeline_id = PipelineId {
namespace_id: PipelineNamespaceId(args.pipelineId.namespaceId),
index: Index::new(args.pipelineId.index).expect("`pipelineId.index` must not be zero"),
};
if let Some(introduction_type) = args.introductionType.as_ref() {
// Check the `introductionType` and `url`, decide whether or not to create a source actor, and if so,
// tell the devtools server to create a source actor. Based on the Firefox impl in:
// - getDebuggerSourceURL()
// - getSourceURL()
// - resolveSourceURL()
// - SourceActor#_isInlineSource
// - SourceActor#url
// Firefox impl: getDebuggerSourceURL(), getSourceURL()
// TODO: handle `about:srcdoc` case (see Firefox getDebuggerSourceURL())
// TODO: remove trailing details that may have been appended by SpiderMonkey
// (currently impossible to do robustly due to )
let url_original = args.url.str();
// FIXME: use page/worker url as base here
let url_original = ServoUrl::parse(&url_original).ok();
// If the source has a `urlOverride` (aka `displayURL` aka `//# sourceURL`), it should be a valid url,
// possibly relative to the page/worker url, and we should treat the source as coming from that url for
// devtools purposes, including the file tree in the Sources tab.
// Firefox impl: getSourceURL()
let url_override = args
.urlOverride
.as_ref()
.map(|url| url.str())
// FIXME: use page/worker url as base here, not `url_original`
.and_then(|url| ServoUrl::parse_with_base(url_original.as_ref(), &url).ok());
// If the `introductionType` is “eval or eval-like”, the `url` won’t be meaningful, so ignore these
// sources unless we have a `urlOverride` (aka `displayURL` aka `//# sourceURL`).
// Firefox impl: getDebuggerSourceURL(), getSourceURL()
if [
IntroductionType::INJECTED_SCRIPT_STR,
IntroductionType::EVAL_STR,
IntroductionType::DEBUGGER_EVAL_STR,
IntroductionType::FUNCTION_STR,
IntroductionType::JAVASCRIPT_URL_STR,
IntroductionType::EVENT_HANDLER_STR,
IntroductionType::DOM_TIMER_STR,
]
.contains(&&*introduction_type.str()) &&
url_override.is_none()
{
debug!(
"Not creating debuggee: `introductionType` is `{introduction_type}` but no valid url"
);
return;
}
// Sources with an `introductionType` of `inlineScript` are generally inline, meaning their contents
// are a substring of the page markup (hence not known to SpiderMonkey, requiring plumbing in Servo).
// But sources with a `urlOverride` are not inline, since they get their own place in the Sources tree.
// nor are sources created for `