diff --git a/components/script/body.rs b/components/script/body.rs index 863eac7706a..32a590dd517 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -27,7 +27,7 @@ use crate::dom::bindings::codegen::Bindings::XMLHttpRequestBinding::BodyInit; use crate::dom::bindings::error::{Error, Fallible}; use crate::dom::bindings::refcounted::Trusted; use crate::dom::bindings::reflector::DomObject; -use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::root::{Dom, DomRoot}; use crate::dom::bindings::str::{DOMString, USVString}; use crate::dom::bindings::trace::RootedTraceableBox; use crate::dom::blob::{normalize_type_string, Blob}; @@ -249,7 +249,7 @@ impl TransmitBodyConnectHandler { // and the corresponding IPC route in `component::net::http_loader`. let promise_handler = Box::new(TransmitBodyPromiseHandler { bytes_sender: bytes_sender.clone(), - stream: rooted_stream.clone(), + stream: Dom::from_ref(&rooted_stream.clone()), control_sender: control_sender.clone(), }); @@ -274,11 +274,12 @@ impl TransmitBodyConnectHandler { /// The handler of read promises of body streams used in /// . #[derive(Clone, JSTraceable, MallocSizeOf)] +#[crown::unrooted_must_root_lint::must_root] struct TransmitBodyPromiseHandler { #[ignore_malloc_size_of = "Channels are hard"] #[no_trace] bytes_sender: IpcSender, - stream: DomRoot, + stream: Dom, #[ignore_malloc_size_of = "Channels are hard"] #[no_trace] control_sender: IpcSender, @@ -610,12 +611,13 @@ impl Callback for ConsumeBodyPromiseRejectionHandler { } #[derive(Clone, JSTraceable, MallocSizeOf)] +#[crown::unrooted_must_root_lint::must_root] /// The promise handler used to consume the body, /// struct ConsumeBodyPromiseHandler { #[ignore_malloc_size_of = "Rc are hard"] result_promise: Rc, - stream: Option>, + stream: Option>, body_type: DomRefCell>, mime_type: DomRefCell>>, bytes: DomRefCell>>, @@ -649,6 +651,7 @@ impl ConsumeBodyPromiseHandler { impl Callback for ConsumeBodyPromiseHandler { /// Continuing Step 4 of /// Step 3 of . + #[allow(crown::unrooted_must_root)] fn callback(&self, cx: JSContext, v: HandleValue, _realm: InRealm, can_gc: CanGc) { let stream = self .stream @@ -775,7 +778,7 @@ fn consume_body_with_promise( let promise_handler = Box::new(ConsumeBodyPromiseHandler { result_promise: promise.clone(), - stream: Some(stream), + stream: Some(Dom::from_ref(&stream)), body_type: DomRefCell::new(Some(body_type)), mime_type: DomRefCell::new(Some(object.get_mime_type(can_gc))), // Step 2. diff --git a/components/script/dom/promisenativehandler.rs b/components/script/dom/promisenativehandler.rs index 42614a4cc34..4b57cd08f63 100644 --- a/components/script/dom/promisenativehandler.rs +++ b/components/script/dom/promisenativehandler.rs @@ -14,6 +14,10 @@ use crate::dom::globalscope::GlobalScope; use crate::realms::InRealm; use crate::script_runtime::{CanGc, JSContext as SafeJSContext}; +/// Types that implement the `Callback` trait follow the same rooting requirements +/// as types that use the `#[dom_struct]` attribute. +/// Prefer storing `Dom` members inside them instead of `DomRoot` +/// to minimize redundant work by the garbage collector. pub trait Callback: JSTraceable + MallocSizeOf { fn callback(&self, cx: SafeJSContext, v: HandleValue, realm: InRealm, can_gc: CanGc); }