net: Use RequestId to cancel fetches instead of creating an IPC channel (#34883)

Instead of creating an IPC channel for every fetch, allow cancelling
fetches based on the `RequestId` of the original request. This requires
that `RequestId`s be UUIDs so that they are unique between processes
that might communicating with the resource process.

In addition, the resource process loop now keeps a `HashMap` or `Weak`
handles to cancellers and cleans them up.

This allows for creating mutiple `FetchCanceller`s in `script` for a
single fetch request, allowing integration of the media and video
elements to integrate with the `Document` canceller list -- meaning
these fetches also get cancelled when the `Document` unloads.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-11 12:49:22 +01:00 committed by GitHub
parent e2be55b873
commit 748954d610
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 179 additions and 226 deletions

View file

@ -18,7 +18,6 @@ use html5ever::serialize::SerializeOpts;
use http::header::{self, HeaderMap, HeaderName, HeaderValue};
use http::Method;
use hyper_serde::Serde;
use ipc_channel::ipc;
use js::jsapi::{Heap, JS_ClearPendingException};
use js::jsval::{JSVal, NullValue};
use js::rust::wrappers::JS_ParseJSON;
@ -291,16 +290,6 @@ impl XMLHttpRequest {
fn sync_in_window(&self) -> bool {
self.sync.get() && self.global().is::<Window>()
}
fn initiate_async_xhr(
context: Arc<Mutex<XHRContext>>,
task_source: SendableTaskSource,
global: &GlobalScope,
init: RequestBuilder,
cancellation_chan: ipc::IpcReceiver<()>,
) {
global.fetch(init, context, task_source, Some(cancellation_chan));
}
}
impl XMLHttpRequestMethods<crate::DomTypeHolder> for XMLHttpRequest {
@ -1548,7 +1537,7 @@ impl XMLHttpRequest {
self.response_status.set(Err(()));
}
fn fetch(&self, init: RequestBuilder, global: &GlobalScope) -> ErrorResult {
fn fetch(&self, request_builder: RequestBuilder, global: &GlobalScope) -> ErrorResult {
let xhr = Trusted::new(self);
let context = Arc::new(Mutex::new(XHRContext {
@ -1556,7 +1545,7 @@ impl XMLHttpRequest {
gen_id: self.generation_id.get(),
sync_status: DomRefCell::new(None),
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
url: init.url.clone(),
url: request_builder.url.clone(),
}));
let (task_source, script_port) = if self.sync.get() {
@ -1577,15 +1566,8 @@ impl XMLHttpRequest {
)
};
let cancel_receiver = self.canceller.borrow_mut().initialize();
XMLHttpRequest::initiate_async_xhr(
context.clone(),
task_source,
global,
init,
cancel_receiver,
);
*self.canceller.borrow_mut() = FetchCanceller::new(request_builder.id);
global.fetch(request_builder, context.clone(), task_source);
if let Some(script_port) = script_port {
loop {