Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

57.7 KB
Raw Blame
use super::target_session_owner::TargetSessionOwnerMut;
use super::*;
use crate::automation::{DevToolsNetworkInterceptId, DevToolsNetworkResourceType};
use crate::conn::state::{
    TargetFetchConfig, TargetFetchOwner, TargetFetchSubresourceInterceptionSnapshot,
};
use crate::conn::{
    CapturedBody, CommandOwnerScope, CompletedFetchResponseBodyStreamReadDispatch,
    FetchInterceptionPattern, FetchRequestStage, InFlightSubresourceFetchRequest,
    PausedDocumentTransfer, PendingFetchAuthNavigation, PendingFetchNavigation,
    PendingFetchResponseBodyStreamRead, PendingFetchResponseBodyStreamReadStart,
    PendingSubresourceFetchAuthRequest, PendingSubresourceFetchRequest,
    PendingSubresourceFetchResponseRequest, TargetRuntimeSlot,
};
use crate::domains::network::TargetIoStreamRead;

pub(crate) type SessionOwnerPendingFetchState = (
    Vec<PendingFetchNavigation>,
    Vec<PendingFetchAuthNavigation>,
    Vec<PausedDocumentTransfer>,
    Vec<(String, PendingSubresourceFetchRequest)>,
    Vec<(String, PendingSubresourceFetchAuthRequest)>,
    Vec<(String, PendingSubresourceFetchResponseRequest)>,
);

struct SessionPendingFetchOwner<'a>(&'a mut TargetFetchOwner);

impl std::ops::Deref for SessionPendingFetchOwner<'_> {
    type Target = TargetFetchOwner;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl std::ops::DerefMut for SessionPendingFetchOwner<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0
    }
}

impl SessionPendingFetchOwner<'_> {
    fn in_flight_subresource_fetch_request_identity(
        &self,
        internal_id: u64,
    ) -> Option<(String, crate::conn::TargetPageResidenceIdentity)> {
        Some((
            self.in_flight_subresource_fetch_request_id(internal_id)?
                .to_owned(),
            self.in_flight_subresource_fetch_request_page_owner(internal_id)?
                .clone(),
        ))
    }
}

struct SessionFetchBodyStreamOwner<'a> {
    owner_key: String,
    fetch_owner: &'a mut TargetFetchOwner,
    runtime_slot: &'a mut TargetRuntimeSlot,
}

impl SessionFetchBodyStreamOwner<'_> {
    fn open_pending_fetch_response_body_stream(
        &mut self,
        request_id: &str,
    ) -> anyhow::Result<Option<String>> {
        let handle = target_scoped_stream_handle(
            &self.owner_key,
            self.runtime_slot.allocate_io_stream_handle(),
        );
        self.fetch_owner.open_pending_fetch_response_body_stream(
            self.runtime_slot,
            request_id,
            handle,
        )
    }

    fn start_pending_fetch_response_body_stream_read(
        &mut self,
        handle: &str,
        offset: Option<usize>,
        size: Option<usize>,
    ) -> PendingFetchResponseBodyStreamReadStart {
        self.fetch_owner
            .start_pending_fetch_response_body_stream_read(handle, offset, size)
    }

    fn finish_pending_fetch_response_body_stream_read(
        &mut self,
        completed: CompletedFetchResponseBodyStreamReadDispatch,
    ) -> PendingFetchResponseBodyStreamRead {
        self.fetch_owner
            .finish_pending_fetch_response_body_stream_read(self.runtime_slot, completed)
    }

    fn close_pending_fetch_response_body_stream(&mut self, handle: &str) -> bool {
        self.fetch_owner
            .close_pending_fetch_response_body_stream(handle)
    }
}

fn fetch_body_stream_owner_for_target_mut<'a>(
    browser_context: &'a mut BrowserContext,
    target_id: &str,
) -> Option<SessionFetchBodyStreamOwner<'a>> {
    let owner_key = fetch_stream_owner_key(&browser_context.id, target_id);
    let active_target = browser_context.page_target_mut(target_id)?;
    Some(SessionFetchBodyStreamOwner {
        owner_key,
        fetch_owner: &mut active_target.fetch_owner,
        runtime_slot: &mut active_target.runtime_slot,
    })
}

fn runtime_slot_for_target_scoped_stream_mut<'a>(
    browser_context: &'a mut BrowserContext,
    target_id: &str,
) -> Option<&'a mut TargetRuntimeSlot> {
    browser_context
        .page_target_mut(target_id)
        .map(|target| &mut target.runtime_slot)
}

impl TargetSessionOwnerMut<'_> {
    fn open_scoped_io_stream_body_source(&mut self, body: CapturedBody) -> Result<String, String> {
        let owner_key = fetch_stream_owner_key(&self.browser_context.id, &self.target_id);
        let Some(target) = self.browser_context.page_target_mut(&self.target_id) else {
            return Err("NoDocumentLoaded".to_owned());
        };
        let handle = target_scoped_stream_handle(
            &owner_key,
            target.runtime_slot.allocate_io_stream_handle(),
        );
        target
            .runtime_slot
            .insert_io_stream_body_source(handle.clone(), body, 0);
        Ok(handle)
    }
}

fn fetch_stream_owner_key(browser_context_id: &str, target_id: &str) -> String {
    format!("{browser_context_id}:{target_id}")
}

fn target_scoped_stream_handle(owner_key: &str, handle: String) -> String {
    format!("{owner_key}:{handle}")
}

#[derive(Debug)]
struct TargetScopedStreamOwner {
    browser_context_id: String,
    target_id: String,
}

fn target_scoped_stream_owner_from_handle(handle: &str) -> Option<TargetScopedStreamOwner> {
    let (owner_key, stream_id) = handle.rsplit_once(':')?;
    if stream_id.is_empty() {
        return None;
    }
    let (browser_context_id, target_id) = owner_key.split_once(':')?;
    if browser_context_id.is_empty() || target_id.is_empty() {
        return None;
    }
    Some(TargetScopedStreamOwner {
        browser_context_id: browser_context_id.to_owned(),
        target_id: target_id.to_owned(),
    })
}

fn target_scoped_stream_owner_matches_session(
    conn: &CdpConnection,
    session_id: Option<&str>,
    owner: &TargetScopedStreamOwner,
) -> bool {
    let Some((browser_context_id, target_id)) = conn.target_owner_identity_for_session(session_id)
    else {
        return false;
    };
    if browser_context_id != owner.browser_context_id {
        return false;
    }
    target_id.unwrap_or_else(|| "active".to_owned()) == owner.target_id
}

fn remove_network_intercept_from_browser_context(
    browser_context: &mut BrowserContext,
    intercept_id: &str,
) -> Result<Option<Option<moli_core::page::PendingPageCommand>>, String> {
    let target = browser_context.active_page_target_mut();
    if target.fetch_owner.remove_network_intercept(intercept_id) {
        let (subresource_enabled, subresource_resource_type) =
            target.fetch_owner.subresource_interception_config();
        let Some(page) = target.runtime_slot.loaded_page_mut() else {
            return Ok(Some(None));
        };
        return page
            .start_set_fetch_subresource_interception(
                subresource_enabled,
                subresource_resource_type,
            )
            .map(Some)
            .map(Some)
            .map_err(|error| format!("failed to update page fetch interception: {error}"));
    }

    let target_ids = browser_context
        .background_targets()
        .map(|target| target.target_id().to_owned())
        .collect::<Vec<_>>();
    for target_id in target_ids {
        let removed = browser_context
            .page_target_mut(&target_id)
            .expect("background target must remain registered")
            .fetch_owner
            .remove_network_intercept(intercept_id);
        if removed {
            return Ok(Some(None));
        }
    }

    Ok(None)
}

impl CdpConnection {
    pub(crate) fn target_fetch_subresource_interception_snapshot_for_owner(
        &self,
        owner: &CommandOwnerScope,
    ) -> Option<TargetFetchSubresourceInterceptionSnapshot> {
        self.target_session_owner_aggregate_fetch_config_for_owner(owner)
            .map(|config| config.subresource_interception_snapshot())
    }

    pub(crate) fn target_fetch_subresource_interception_snapshot_for_target(
        &self,
        target_id: &str,
    ) -> Option<TargetFetchSubresourceInterceptionSnapshot> {
        self.target_fetch_config_for_target(target_id)
            .map(|config| config.subresource_interception_snapshot())
    }

    pub(crate) fn target_fetch_event_session_id_for_owner(
        &self,
        owner: &CommandOwnerScope,
    ) -> Option<String> {
        self.target_session_owner_aggregate_fetch_config_for_owner(owner)
            .and_then(|config| config.session_id().map(str::to_owned))
    }

    pub(crate) fn target_fetch_handle_auth_requests_for_owner(
        &self,
        owner: &CommandOwnerScope,
    ) -> bool {
        self.target_session_owner_aggregate_fetch_config_for_owner(owner)
            .is_some_and(|config| config.handle_auth_requests())
    }

    pub(crate) fn target_fetch_matches_auth_required_for_owner(
        &self,
        owner: &CommandOwnerScope,
        url: &url::Url,
    ) -> bool {
        self.target_session_owner_aggregate_fetch_config_for_owner(owner)
            .is_some_and(|config| config.matches_auth_required(url))
    }

    pub(crate) fn target_fetch_matching_auth_required_network_intercepts_for_owner(
        &self,
        owner: &CommandOwnerScope,
        url: &url::Url,
    ) -> Vec<DevToolsNetworkInterceptId> {
        self.target_session_owner_aggregate_fetch_config_for_owner(owner)
            .map(|config| config.matching_auth_required_network_intercepts(url))
            .unwrap_or_default()
    }

    pub(crate) fn target_fetch_matching_auth_required_network_intercepts_for_target(
        &self,
        target_id: &str,
        url: &url::Url,
    ) -> Vec<DevToolsNetworkInterceptId> {
        self.target_fetch_config_for_target(target_id)
            .map(|config| config.matching_auth_required_network_intercepts(url))
            .unwrap_or_default()
    }

    pub(crate) fn target_fetch_matching_network_intercepts_for_target(
        &self,
        target_id: &str,
        request_stage: FetchRequestStage,
        resource_type: DevToolsNetworkResourceType,
        url: &url::Url,
    ) -> Vec<DevToolsNetworkInterceptId> {
        self.target_fetch_config_for_target(target_id)
            .map(|config| config.matching_network_intercepts(request_stage, resource_type, url))
            .unwrap_or_default()
    }

    fn target_fetch_config_for_target(&self, target_id: &str) -> Option<TargetFetchConfig> {
        match self.target_session_route_for_target_id(target_id)? {
            CdpSessionRoute::PageTarget {
                browser_context_id,
                target_id,
                ..
            } => self
                .browser_context_by_id(&browser_context_id)?
                .page_target(&target_id)
                .map(|target| target.fetch_owner.config_snapshot()),
            CdpSessionRoute::Browser
            | CdpSessionRoute::BrowserContext { .. }
            | CdpSessionRoute::TabTarget { .. }
            | CdpSessionRoute::SharedWorkerTarget { .. }
            | CdpSessionRoute::DedicatedWorkerTarget { .. }
            | CdpSessionRoute::ServiceWorkerTarget { .. } => None,
        }
    }

    pub(crate) fn allocate_pending_subresource_fetch_request_ids_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
    ) -> Option<(String, String)> {
        let mut network_request_id_allocator =
            std::mem::take(&mut self.network_request_id_allocator);
        let result = self
            .runtime_session_owner_slot_mut_for_owner(owner)
            .map(|runtime_slot| {
                runtime_slot
                    .request_id_allocator()
                    .allocate_pending_subresource_fetch_request_ids(
                        &mut network_request_id_allocator,
                    )
            });
        self.network_request_id_allocator = network_request_id_allocator;
        result
    }

    pub(crate) fn allocate_fetch_navigation_request_id_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
    ) -> Option<String> {
        self.runtime_session_owner_slot_mut_for_owner(owner)
            .map(|runtime_slot| {
                runtime_slot
                    .request_id_allocator()
                    .allocate_fetch_navigation_request_id()
            })
    }

    #[cfg(test)]
    pub(crate) fn open_io_stream_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        bytes: Vec<u8>,
    ) -> Result<String, String> {
        self.open_io_stream_body_source_for_session_owner(
            session_id,
            CapturedBody::from_bytes_spooled(bytes),
        )
    }

    pub(crate) fn open_io_stream_body_source_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        body: CapturedBody,
    ) -> Result<String, String> {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.open_io_stream_body_source_for_owner(&owner, body)
    }

    pub(crate) fn open_io_stream_body_source_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        body: CapturedBody,
    ) -> Result<String, String> {
        let Some(mut owner) = self.target_session_owner_mut_for_owner(owner) else {
            return Err("NoDocumentLoaded".to_owned());
        };
        owner.open_scoped_io_stream_body_source(body)
    }

    pub(crate) fn read_io_stream_for_stream_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
        offset: Option<usize>,
        size: Option<usize>,
    ) -> Option<TargetIoStreamRead> {
        let Some(owner) = target_scoped_stream_owner_from_handle(handle) else {
            return self
                .runtime_session_owner_slot_mut(session_id)
                .and_then(|runtime_slot| runtime_slot.read_io_stream(handle, offset, size));
        };
        if !target_scoped_stream_owner_matches_session(self, session_id, &owner) {
            return None;
        }
        let browser_context = self.browser_context_by_id_mut(&owner.browser_context_id)?;
        runtime_slot_for_target_scoped_stream_mut(browser_context, &owner.target_id)?
            .read_io_stream(handle, offset, size)
    }

    pub(crate) fn register_synthetic_websocket_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        network_request_id: String,
        socket_id: u64,
    ) -> bool {
        let Some(runtime_slot) = self.runtime_session_owner_slot_mut_for_owner(owner) else {
            return false;
        };
        runtime_slot.register_synthetic_websocket_request(
            request_id,
            network_request_id,
            socket_id,
        );
        true
    }

    pub(crate) fn synthetic_websocket_socket_id_for_session_owner(
        &self,
        session_id: Option<&str>,
        request_id: &str,
    ) -> Option<u64> {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.synthetic_websocket_socket_id_for_owner(&owner, request_id)
    }

    pub(crate) fn synthetic_websocket_socket_id_for_owner(
        &self,
        owner: &CommandOwnerScope,
        request_id: &str,
    ) -> Option<u64> {
        self.runtime_session_owner_slot_for_owner(owner)
            .and_then(|runtime_slot| {
                runtime_slot.synthetic_websocket_socket_id_for_request(request_id)
            })
    }

    pub(crate) fn pending_fetch_request_session_route(
        &self,
        request_id: &str,
    ) -> Option<CdpSessionRoute> {
        self.browser_contexts()
            .find_map(|browser_context| pending_fetch_request_route(browser_context, request_id))
    }

    pub(crate) fn pending_subresource_fetch_request_residence_is_current(
        &self,
        pending: &PendingSubresourceFetchRequest,
    ) -> bool {
        pending
            .installed_page_owner()
            .is_none_or(|owner| self.target_page_residence_identity_is_current(owner))
    }

    fn installed_subresource_fetch_request_is_current(
        &self,
        pending: &PendingSubresourceFetchRequest,
    ) -> bool {
        pending
            .installed_page_owner()
            .is_some_and(|owner| self.target_page_residence_identity_is_current(owner))
    }

    pub(crate) fn claim_subresource_continue_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        expected_page_owner: &crate::conn::TargetPageResidenceIdentity,
        internal_id: u64,
        allow_pending_completion: bool,
    ) -> Option<crate::conn::ClaimedSubresourceContinueRequest> {
        if !self.target_page_residence_identity_is_current(expected_page_owner) {
            return None;
        }
        self.target_session_owner_mut_for_owner(owner)?
            .claim_subresource_continue_request(
                expected_page_owner,
                internal_id,
                owner.session_id(),
                allow_pending_completion,
            )
    }

    pub(crate) fn consume_pending_request_action_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: &str,
    ) -> Option<Result<(), &'static str>> {
        self.target_session_owner_mut_for_owner(owner)?
            .consume_pending_request_action(request_id)
    }

    pub(crate) fn take_pending_fetch_navigation_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingFetchNavigation> {
        self.target_session_owner_mut_for_owner(owner)?
            .take_pending_fetch_navigation_for_action_session(request_id, action_session_id)
    }

    pub(crate) fn take_pending_fetch_auth_navigation_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingFetchAuthNavigation> {
        self.target_session_owner_mut_for_owner(owner)?
            .take_pending_fetch_auth_navigation_for_action_session(request_id, action_session_id)
    }

    pub(crate) fn register_pending_fetch_auth_navigation_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: String,
        pending: PendingFetchAuthNavigation,
    ) -> bool {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.register_pending_fetch_auth_navigation_for_owner(&owner, request_id, pending)
    }

    pub(crate) fn register_pending_fetch_auth_navigation_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        pending: PendingFetchAuthNavigation,
    ) -> bool {
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_fetch_auth_navigation(request_id, pending)
            })
    }

    pub(crate) fn register_pending_fetch_response_navigation_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        document_navigation_token: Option<crate::conn::DocumentNavigationToken>,
        navigation: crate::conn::NavigationDispatchState,
        body: crate::conn::DocumentBodySource,
    ) -> bool {
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_fetch_response_navigation(
                    request_id,
                    document_navigation_token,
                    navigation,
                    body,
                )
            })
    }

    pub(crate) fn take_fetch_response_transfer_for_cancellation_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: &str,
    ) -> Option<PausedDocumentTransfer> {
        self.target_session_owner_mut_for_owner(owner)?
            .take_fetch_response_transfer_for_cancellation(request_id)
    }

    pub(crate) fn start_pending_fetch_response_body_read_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: &str,
    ) -> Option<crate::conn::PendingDocumentBodyRead> {
        self.target_session_owner_mut_for_owner(owner)?
            .pending_fetch_owner_mut()?
            .start_pending_fetch_response_body_read(request_id)
    }

    pub(crate) fn take_available_fetch_response_transfer_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: &str,
    ) -> Option<PausedDocumentTransfer> {
        self.target_session_owner_mut_for_owner(owner)?
            .take_available_fetch_response_transfer(request_id)
    }

    pub(crate) fn register_pending_fetch_response_transfer_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        transfer: PausedDocumentTransfer,
    ) -> bool {
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_fetch_response_transfer(request_id, transfer)
            })
    }

    pub(crate) fn finish_pending_fetch_response_body_read_for_owner<T>(
        &mut self,
        owner: &CommandOwnerScope,
        completed: crate::conn::CompletedDocumentBodyRead<T>,
    ) -> Option<anyhow::Result<T>> {
        self.target_session_owner_mut_for_owner(owner)?
            .pending_fetch_owner_mut()?
            .finish_pending_fetch_response_body_read(completed)
    }

    pub(crate) fn pending_subresource_fetch_response_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingSubresourceFetchResponseRequest> {
        self.target_session_owner_mut_for_owner(owner)?
            .pending_subresource_fetch_response_request(request_id, action_session_id)
    }

    pub(crate) fn mark_pending_subresource_fetch_response_body_taken_as_stream_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> bool {
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.mark_pending_subresource_fetch_response_body_taken_as_stream(
                    request_id,
                    action_session_id,
                )
            })
    }

    pub(crate) fn open_pending_fetch_response_body_stream_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: &str,
    ) -> anyhow::Result<Option<String>> {
        let Some(mut owner) = self.target_session_owner_mut(session_id) else {
            return Ok(None);
        };
        owner.open_pending_fetch_response_body_stream(request_id)
    }

    pub(crate) fn start_pending_fetch_response_body_stream_read_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
        offset: Option<usize>,
        size: Option<usize>,
    ) -> PendingFetchResponseBodyStreamReadStart {
        let Some(mut owner) = self.target_session_owner_mut(session_id) else {
            return PendingFetchResponseBodyStreamReadStart::NotFound;
        };
        owner.start_pending_fetch_response_body_stream_read(handle, offset, size)
    }

    pub(crate) fn start_pending_fetch_response_body_stream_read_for_stream_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
        offset: Option<usize>,
        size: Option<usize>,
    ) -> PendingFetchResponseBodyStreamReadStart {
        let Some(stream_owner) = target_scoped_stream_owner_from_handle(handle) else {
            return self.start_pending_fetch_response_body_stream_read_for_session_owner(
                session_id, handle, offset, size,
            );
        };
        if !target_scoped_stream_owner_matches_session(self, session_id, &stream_owner) {
            return PendingFetchResponseBodyStreamReadStart::NotFound;
        }
        let Some(browser_context) =
            self.browser_context_by_id_mut(&stream_owner.browser_context_id)
        else {
            return PendingFetchResponseBodyStreamReadStart::NotFound;
        };
        let Some(mut owner) =
            fetch_body_stream_owner_for_target_mut(browser_context, &stream_owner.target_id)
        else {
            return PendingFetchResponseBodyStreamReadStart::NotFound;
        };
        owner.start_pending_fetch_response_body_stream_read(handle, offset, size)
    }

    pub(crate) fn finish_pending_fetch_response_body_stream_read_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        completed: CompletedFetchResponseBodyStreamReadDispatch,
    ) -> PendingFetchResponseBodyStreamRead {
        let Some(mut owner) = self.target_session_owner_mut(session_id) else {
            return PendingFetchResponseBodyStreamRead::NotFound;
        };
        owner.finish_pending_fetch_response_body_stream_read(completed)
    }

    pub(crate) fn finish_pending_fetch_response_body_stream_read_for_stream_owner(
        &mut self,
        session_id: Option<&str>,
        completed: CompletedFetchResponseBodyStreamReadDispatch,
    ) -> PendingFetchResponseBodyStreamRead {
        let stream_owner = target_scoped_stream_owner_from_handle(&completed.handle);
        let Some(stream_owner) = stream_owner else {
            return self.finish_pending_fetch_response_body_stream_read_for_session_owner(
                session_id, completed,
            );
        };
        if !target_scoped_stream_owner_matches_session(self, session_id, &stream_owner) {
            return PendingFetchResponseBodyStreamRead::NotFound;
        }
        let Some(browser_context) =
            self.browser_context_by_id_mut(&stream_owner.browser_context_id)
        else {
            return PendingFetchResponseBodyStreamRead::NotFound;
        };
        let Some(mut owner) =
            fetch_body_stream_owner_for_target_mut(browser_context, &stream_owner.target_id)
        else {
            return PendingFetchResponseBodyStreamRead::NotFound;
        };
        owner.finish_pending_fetch_response_body_stream_read(completed)
    }

    pub(crate) fn close_pending_fetch_response_body_stream_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
    ) -> bool {
        self.target_session_owner_mut(session_id)
            .is_some_and(|mut owner| owner.close_pending_fetch_response_body_stream(handle))
    }

    pub(crate) fn close_pending_fetch_response_body_stream_for_stream_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
    ) -> bool {
        let Some(stream_owner) = target_scoped_stream_owner_from_handle(handle) else {
            return self
                .close_pending_fetch_response_body_stream_for_session_owner(session_id, handle);
        };
        if !target_scoped_stream_owner_matches_session(self, session_id, &stream_owner) {
            return false;
        }
        let Some(browser_context) =
            self.browser_context_by_id_mut(&stream_owner.browser_context_id)
        else {
            return false;
        };
        fetch_body_stream_owner_for_target_mut(browser_context, &stream_owner.target_id)
            .is_some_and(|mut owner| owner.close_pending_fetch_response_body_stream(handle))
    }

    pub(crate) fn close_io_stream_for_stream_owner(
        &mut self,
        session_id: Option<&str>,
        handle: &str,
    ) -> bool {
        let Some(stream_owner) = target_scoped_stream_owner_from_handle(handle) else {
            return self
                .runtime_session_owner_slot_mut(session_id)
                .is_some_and(|runtime_slot| runtime_slot.close_io_stream(handle));
        };
        if !target_scoped_stream_owner_matches_session(self, session_id, &stream_owner) {
            return false;
        }
        let Some(browser_context) =
            self.browser_context_by_id_mut(&stream_owner.browser_context_id)
        else {
            return false;
        };
        runtime_slot_for_target_scoped_stream_mut(browser_context, &stream_owner.target_id)
            .is_some_and(|runtime_slot| runtime_slot.close_io_stream(handle))
    }

    #[cfg(test)]
    pub(crate) fn take_pending_subresource_fetch_request_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingSubresourceFetchRequest> {
        let pending = self
            .target_session_owner_mut(session_id)?
            .take_pending_subresource_fetch_request(request_id, session_id)?;
        self.pending_subresource_fetch_request_residence_is_current(&pending)
            .then_some(pending)
    }

    pub(crate) fn take_pending_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingSubresourceFetchRequest> {
        let pending = self
            .target_session_owner_mut_for_owner(owner)?
            .take_pending_subresource_fetch_request(request_id, action_session_id)?;
        self.pending_subresource_fetch_request_residence_is_current(&pending)
            .then_some(pending)
    }

    pub(crate) fn take_pending_subresource_fetch_auth_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingSubresourceFetchAuthRequest> {
        let pending = self
            .target_session_owner_mut_for_owner(owner)?
            .take_pending_subresource_fetch_auth_request(request_id, action_session_id)?;
        self.target_page_residence_identity_is_current(&pending.page_owner)
            .then_some(pending)
    }

    pub(crate) fn take_pending_subresource_fetch_response_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        action_session_id: Option<&str>,
        request_id: &str,
    ) -> Option<PendingSubresourceFetchResponseRequest> {
        let pending = self
            .target_session_owner_mut_for_owner(owner)?
            .take_pending_subresource_fetch_response_request(request_id, action_session_id)?;
        self.target_page_residence_identity_is_current(&pending.page_owner)
            .then_some(pending)
    }

    pub(crate) fn take_in_flight_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        internal_id: u64,
    ) -> Option<InFlightSubresourceFetchRequest> {
        let in_flight = self
            .target_session_owner_mut_for_owner(owner)?
            .take_in_flight_subresource_fetch_request(internal_id)?;
        self.installed_subresource_fetch_request_is_current(&in_flight.pending)
            .then_some(in_flight)
    }

    pub(crate) fn in_flight_subresource_fetch_request_id_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        internal_id: u64,
    ) -> Option<String> {
        let (request_id, page_owner) = self
            .target_session_owner_mut_for_owner(owner)?
            .in_flight_subresource_fetch_request_identity(internal_id)?;
        self.target_page_residence_identity_is_current(&page_owner)
            .then_some(request_id)
    }

    pub(crate) fn register_pending_subresource_fetch_request_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: String,
        pending: PendingSubresourceFetchRequest,
    ) -> bool {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.register_pending_subresource_fetch_request_for_owner(&owner, request_id, pending)
    }

    pub(crate) fn register_pending_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        pending: PendingSubresourceFetchRequest,
    ) -> bool {
        if !self.pending_subresource_fetch_request_residence_is_current(&pending) {
            return false;
        }
        self.record_pending_subresource_network_request_identity_for_owner(owner, &pending);
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_subresource_fetch_request(request_id, pending)
            })
    }

    pub(crate) fn register_in_flight_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: Option<String>,
        pending: PendingSubresourceFetchRequest,
    ) -> bool {
        if !self.installed_subresource_fetch_request_is_current(&pending) {
            return false;
        }
        self.record_pending_subresource_network_request_identity_for_owner(owner, &pending);
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_in_flight_subresource_fetch_request(request_id, pending)
            })
    }

    pub(crate) fn register_in_flight_response_stage_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: Option<String>,
        pending: PendingSubresourceFetchRequest,
        response_stage_blocked_intercepts: Vec<DevToolsNetworkInterceptId>,
    ) -> bool {
        if !self.installed_subresource_fetch_request_is_current(&pending) {
            return false;
        }
        self.record_pending_subresource_network_request_identity_for_owner(owner, &pending);
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_in_flight_response_stage_subresource_fetch_request(
                    request_id,
                    pending,
                    response_stage_blocked_intercepts,
                );
                true
            })
    }

    pub(crate) fn register_in_flight_deferred_response_stage_subresource_fetch_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: Option<String>,
        pending: PendingSubresourceFetchRequest,
    ) -> bool {
        if !self.installed_subresource_fetch_request_is_current(&pending) {
            return false;
        }
        self.record_pending_subresource_network_request_identity_for_owner(owner, &pending);
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_in_flight_subresource_fetch_request_with_response_match_policy(
                    request_id,
                    pending,
                    crate::conn::ResponseStageUrlMatchPolicy::MatchFinalUrl,
                )
            })
    }

    pub(crate) fn register_pending_subresource_fetch_auth_request_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: String,
        pending: PendingSubresourceFetchAuthRequest,
    ) -> bool {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.register_pending_subresource_fetch_auth_request_for_owner(&owner, request_id, pending)
    }

    pub(crate) fn register_pending_subresource_fetch_auth_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        pending: PendingSubresourceFetchAuthRequest,
    ) -> bool {
        if !self.target_page_residence_identity_is_current(&pending.page_owner) {
            return false;
        }
        self.record_pending_subresource_auth_network_request_identity_for_owner(owner, &pending);
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_subresource_fetch_auth_request(request_id, pending)
            })
    }

    pub(crate) fn register_pending_subresource_fetch_response_request_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        request_id: String,
        pending: PendingSubresourceFetchResponseRequest,
    ) -> bool {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.register_pending_subresource_fetch_response_request_for_owner(
            &owner, request_id, pending,
        )
    }

    pub(crate) fn register_pending_subresource_fetch_response_request_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        request_id: String,
        pending: PendingSubresourceFetchResponseRequest,
    ) -> bool {
        if !self.target_page_residence_identity_is_current(&pending.page_owner) {
            return false;
        }
        self.record_pending_subresource_response_network_request_identity_for_owner(
            owner, &pending,
        );
        self.target_session_owner_mut_for_owner(owner)
            .is_some_and(|mut owner| {
                owner.register_pending_subresource_fetch_response_request(request_id, pending)
            })
    }

    fn record_pending_subresource_network_request_identity_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        pending: &PendingSubresourceFetchRequest,
    ) {
        let Some(handle) = pending.network_request_handle else {
            return;
        };
        if let Some(runtime_slot) = self.runtime_session_owner_slot_mut_for_owner(owner) {
            runtime_slot.record_subresource_request_id_for_handle_if_absent(
                handle,
                pending.network_request_id.clone(),
            );
        }
    }

    fn record_pending_subresource_auth_network_request_identity_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        pending: &PendingSubresourceFetchAuthRequest,
    ) {
        let Some(handle) = pending.network_request_handle else {
            return;
        };
        if let Some(runtime_slot) = self.runtime_session_owner_slot_mut_for_owner(owner) {
            runtime_slot.record_subresource_request_id_for_handle_if_absent(
                handle,
                pending.network_request_id.clone(),
            );
        }
    }

    fn record_pending_subresource_response_network_request_identity_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
        pending: &PendingSubresourceFetchResponseRequest,
    ) {
        let Some(handle) = pending.network_request_handle else {
            return;
        };
        if let Some(runtime_slot) = self.runtime_session_owner_slot_mut_for_owner(owner) {
            runtime_slot.record_subresource_request_id_for_handle_if_absent(
                handle,
                pending.network_request_id.clone(),
            );
        }
    }

    pub(crate) fn start_enable_fetch_for_session_owner(
        &mut self,
        session_id: Option<&str>,
        handle_auth_requests: bool,
        patterns: Vec<FetchInterceptionPattern>,
    ) -> Result<Option<moli_core::page::PendingPageCommand>, String> {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.start_enable_fetch_for_owner(&owner, handle_auth_requests, patterns)
    }

    pub(crate) fn start_enable_fetch_for_owner(
        &mut self,
        command_owner: &CommandOwnerScope,
        handle_auth_requests: bool,
        patterns: Vec<FetchInterceptionPattern>,
    ) -> Result<Option<moli_core::page::PendingPageCommand>, String> {
        let Some(mut owner) = self.target_session_owner_mut_for_owner(command_owner) else {
            return Err("BrowserContextNotLoaded".to_owned());
        };
        let (subresource_enabled, subresource_resource_type) = owner.configure_fetch(
            command_owner.session_id().map(str::to_owned),
            handle_auth_requests,
            patterns,
        );
        let Some(page) = owner.runtime_slot_mut().loaded_page_mut() else {
            return Ok(None);
        };
        page.start_set_fetch_subresource_interception(
            subresource_enabled,
            subresource_resource_type,
        )
        .map(Some)
        .map_err(|error| format!("failed to update page fetch interception: {error}"))
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn start_add_network_intercept_for_owner(
        &mut self,
        command_owner: &CommandOwnerScope,
        intercept_session_id: Option<String>,
        intercept_id: String,
        handle_auth_requests: bool,
        auth_url_patterns: Vec<String>,
        patterns: Vec<FetchInterceptionPattern>,
    ) -> Result<Option<moli_core::page::PendingPageCommand>, String> {
        let Some(mut owner) = self.target_session_owner_mut_for_owner(command_owner) else {
            return Err("BrowserContextNotLoaded".to_owned());
        };
        let (subresource_enabled, subresource_resource_type) = owner.add_network_intercept(
            intercept_id,
            intercept_session_id,
            handle_auth_requests,
            auth_url_patterns,
            patterns,
        );
        let Some(page) = owner.runtime_slot_mut().loaded_page_mut() else {
            return Ok(None);
        };
        page.start_set_fetch_subresource_interception(
            subresource_enabled,
            subresource_resource_type,
        )
        .map(Some)
        .map_err(|error| format!("failed to update page fetch interception: {error}"))
    }

    pub(crate) fn start_remove_network_intercept_for_owner(
        &mut self,
        command_owner: &CommandOwnerScope,
        intercept_id: &str,
        allow_global_lookup: bool,
    ) -> Result<Option<moli_core::page::PendingPageCommand>, String> {
        let Some(mut owner) = self.target_session_owner_mut_for_owner(command_owner) else {
            return Err("BrowserContextNotLoaded".to_owned());
        };
        let Some((subresource_enabled, subresource_resource_type)) =
            owner.remove_network_intercept(intercept_id)
        else {
            if allow_global_lookup {
                return self.start_remove_network_intercept_from_any_target(intercept_id);
            }
            return Err("NetworkInterceptNotFound".to_owned());
        };
        let Some(page) = owner.runtime_slot_mut().loaded_page_mut() else {
            return Ok(None);
        };
        page.start_set_fetch_subresource_interception(
            subresource_enabled,
            subresource_resource_type,
        )
        .map(Some)
        .map_err(|error| format!("failed to update page fetch interception: {error}"))
    }

    fn start_remove_network_intercept_from_any_target(
        &mut self,
        intercept_id: &str,
    ) -> Result<Option<moli_core::page::PendingPageCommand>, String> {
        if let Some(browser_context) = self.browser_context.as_mut()
            && let Some(pending) =
                remove_network_intercept_from_browser_context(browser_context, intercept_id)?
        {
            return Ok(pending);
        }
        for browser_context in &mut self.inactive_browser_contexts {
            if let Some(pending) =
                remove_network_intercept_from_browser_context(browser_context, intercept_id)?
            {
                return Ok(pending);
            }
        }
        Err("NetworkInterceptNotFound".to_owned())
    }

    pub(crate) fn start_disable_fetch_for_session_owner(
        &mut self,
        session_id: Option<&str>,
    ) -> Result<
        Option<(
            SessionOwnerPendingFetchState,
            Option<moli_core::page::PendingPageCommand>,
        )>,
        String,
    > {
        let owner = CommandOwnerScope::capture(self, session_id);
        self.start_disable_fetch_for_owner(&owner)
    }

    pub(crate) fn start_disable_fetch_for_owner(
        &mut self,
        command_owner: &CommandOwnerScope,
    ) -> Result<
        Option<(
            SessionOwnerPendingFetchState,
            Option<moli_core::page::PendingPageCommand>,
        )>,
        String,
    > {
        let Some(mut owner) = self.target_session_owner_mut_for_owner(command_owner) else {
            return Ok(None);
        };
        let (pending, (subresource_enabled, subresource_resource_type), page_update_required) =
            owner
                .reset_fetch_config_for_session_and_drain_pending_state(command_owner.session_id());
        let page_command = if page_update_required
            && let Some(page) = owner.runtime_slot_mut().loaded_page_mut()
        {
            Some(
                page.start_set_fetch_subresource_interception(
                    subresource_enabled,
                    subresource_resource_type,
                )
                .map_err(|error| error.to_string())?,
            )
        } else {
            None
        };
        Ok(Some((pending, page_command)))
    }

    pub(crate) fn take_pending_fetch_state_for_owner(
        &mut self,
        owner: &CommandOwnerScope,
    ) -> Option<SessionOwnerPendingFetchState> {
        Some(
            self.target_session_owner_mut_for_owner(owner)?
                .drain_fetch_pending_state(),
        )
    }

    pub(crate) fn take_superseded_intercepted_document_navigations(
        &mut self,
        owner: &CommandOwnerScope,
        current: &crate::conn::DocumentNavigationToken,
    ) -> Vec<crate::conn::NavigationDispatchState> {
        let Some(mut target) = self.target_session_owner_mut_for_owner(owner) else {
            return Vec::new();
        };
        let Some(mut fetch) = target.pending_fetch_owner_mut() else {
            return Vec::new();
        };
        let retired = fetch.take_superseded_document_navigations(current);
        retired
            .into_iter()
            .map(|(token, navigation)| {
                if let Some(token) = token {
                    let finish = target
                        .runtime_slot_mut()
                        .finish_renderer_document_navigation(&token)
                        .expect("a newly admitted navigation keeps the renderer channel open");
                    // The replacement was registered before retiring its predecessors.
                    // Finishing old work must not release output or replay calls yet.
                    debug_assert!(finish.released_output.is_empty());
                    debug_assert!(finish.renderer_call_replacements.is_none());
                }
                navigation
            })
            .collect()
    }
}

fn pending_fetch_request_route(
    browser_context: &BrowserContext,
    request_id: &str,
) -> Option<CdpSessionRoute> {
    browser_context.page_targets.iter().find_map(|target| {
        target
            .fetch_owner
            .contains_pending_request(request_id)
            .then(|| CdpSessionRoute::PageTarget {
                browser_context_id: browser_context.id.clone(),
                target_id: target.target_id().to_owned(),
                session_key: moli_page_types::DevToolsSessionKey::Primary,
            })
    })
}

impl TargetSessionOwnerMut<'_> {
    fn session_id(&self) -> Option<&str> {
        self.command_session_id.as_deref()
    }

    fn pending_fetch_owner_mut(&mut self) -> Option<SessionPendingFetchOwner<'_>> {
        Some(SessionPendingFetchOwner(
            &mut self
                .browser_context
                .page_target_mut(&self.target_id)?
                .fetch_owner,
        ))
    }

    fn fetch_body_stream_owner_mut(&mut self) -> Option<SessionFetchBodyStreamOwner<'_>> {
        fetch_body_stream_owner_for_target_mut(self.browser_context, &self.target_id)
    }

    pub(super) fn register_pending_fetch_navigation_request(
        &mut self,
        pending: PendingFetchNavigation,
    ) -> Option<()> {
        self.pending_fetch_owner_mut()?
            .register_pending_fetch_navigation_request(pending);
        Some(())
    }

    fn consume_pending_request_action(
        &mut self,
        request_id: &str,
    ) -> Option<Result<(), &'static str>> {
        Some(
            self.pending_fetch_owner_mut()?
                .consume_pending_request_action(request_id),
        )
    }

    fn take_pending_fetch_navigation_for_action_session(
        &mut self,
        request_id: &str,
        action_session_id: Option<&str>,
    ) -> Option<PendingFetchNavigation> {
        self.pending_fetch_owner_mut()?
            .take_pending_fetch_navigation_for_action_session(request_id, action_session_id)
    }

    fn take_pending_fetch_auth_navigation_for_action_session(
        &mut self,
        request_id: &str,
        action_session_id: Option<&str>,
    ) -> Option<PendingFetchAuthNavigation> {
        self.pending_fetch_owner_mut()?
            .take_pending_fetch_auth_navigation_for_action_session(request_id, action_session_id)
    }

    fn register_pending_fetch_auth_navigation(
        &mut self,
        request_id: String,
        pending: PendingFetchAuthNavigation,
    ) -> bool {
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_fetch_auth_navigation(request_id, pending);
        true
    }

    fn register_pending_fetch_response_navigation(
        &mut self,
        request_id: String,
        document_navigation_token: Option<crate::conn::DocumentNavigationToken>,
        navigation: crate::conn::NavigationDispatchState,
        body: crate::conn::DocumentBodySource,
    ) -> bool {
        if let Some(token) = document_navigation_token.as_ref()
            && let crate::conn::DocumentBodySource::StreamingRaw { response, .. } = &body
        {
            // Navigation cancellation also covers a response already handed
            // to a body reader or a continuation outside the Fetch owner.
            self.runtime_slot_mut()
                .page_slot_mut()
                .add_document_navigation_cancellation(token, response.cancellation_handle());
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_fetch_response_navigation(
            request_id,
            document_navigation_token,
            navigation,
            body,
        );
        true
    }

    fn take_fetch_response_transfer_for_cancellation(
        &mut self,
        request_id: &str,
    ) -> Option<PausedDocumentTransfer> {
        self.pending_fetch_owner_mut()?
            .take_fetch_response_transfer_for_cancellation(request_id)
    }

    fn take_available_fetch_response_transfer(
        &mut self,
        request_id: &str,
    ) -> Option<PausedDocumentTransfer> {
        self.pending_fetch_owner_mut()?
            .take_available_fetch_response_transfer(request_id)
    }

    fn register_pending_fetch_response_transfer(
        &mut self,
        request_id: String,
        transfer: PausedDocumentTransfer,
    ) -> bool {
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_fetch_response_transfer(request_id, transfer);
        true
    }

    fn pending_subresource_fetch_response_request(
        &mut self,
        request_id: &str,
        session_id: Option<&str>,
    ) -> Option<PendingSubresourceFetchResponseRequest> {
        self.pending_fetch_owner_mut()?
            .pending_subresource_fetch_response_request(request_id, session_id)
            .cloned()
    }

    fn mark_pending_subresource_fetch_response_body_taken_as_stream(
        &mut self,
        request_id: &str,
        session_id: Option<&str>,
    ) -> bool {
        self.pending_fetch_owner_mut().is_some_and(|mut owner| {
            owner.mark_pending_subresource_fetch_response_body_taken_as_stream(
                request_id, session_id,
            )
        })
    }

    fn take_pending_subresource_fetch_request(
        &mut self,
        request_id: &str,
        session_id: Option<&str>,
    ) -> Option<PendingSubresourceFetchRequest> {
        self.pending_fetch_owner_mut()?
            .take_pending_subresource_fetch_request(request_id, session_id)
    }

    fn take_pending_subresource_fetch_auth_request(
        &mut self,
        request_id: &str,
        session_id: Option<&str>,
    ) -> Option<PendingSubresourceFetchAuthRequest> {
        self.pending_fetch_owner_mut()?
            .take_pending_subresource_fetch_auth_request(request_id, session_id)
    }

    fn take_pending_subresource_fetch_response_request(
        &mut self,
        request_id: &str,
        session_id: Option<&str>,
    ) -> Option<PendingSubresourceFetchResponseRequest> {
        self.pending_fetch_owner_mut()?
            .take_pending_subresource_fetch_response_request(request_id, session_id)
    }

    fn take_in_flight_subresource_fetch_request(
        &mut self,
        internal_id: u64,
    ) -> Option<InFlightSubresourceFetchRequest> {
        self.pending_fetch_owner_mut()?
            .take_in_flight_subresource_fetch_request(internal_id)
    }

    fn claim_subresource_continue_request(
        &mut self,
        expected_page_owner: &crate::conn::TargetPageResidenceIdentity,
        internal_id: u64,
        session_id: Option<&str>,
        allow_pending_completion: bool,
    ) -> Option<crate::conn::ClaimedSubresourceContinueRequest> {
        self.pending_fetch_owner_mut()?
            .claim_subresource_continue_request(
                expected_page_owner,
                internal_id,
                session_id,
                allow_pending_completion,
            )
    }

    fn in_flight_subresource_fetch_request_identity(
        &mut self,
        internal_id: u64,
    ) -> Option<(String, crate::conn::TargetPageResidenceIdentity)> {
        self.pending_fetch_owner_mut()?
            .in_flight_subresource_fetch_request_identity(internal_id)
    }

    pub(super) fn register_pending_subresource_fetch_request(
        &mut self,
        request_id: String,
        mut pending: PendingSubresourceFetchRequest,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_subresource_fetch_request(request_id, pending);
        true
    }

    fn register_in_flight_subresource_fetch_request(
        &mut self,
        request_id: Option<String>,
        mut pending: PendingSubresourceFetchRequest,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_in_flight_subresource_fetch_request(request_id, pending);
        true
    }

    fn register_in_flight_response_stage_subresource_fetch_request(
        &mut self,
        request_id: Option<String>,
        mut pending: PendingSubresourceFetchRequest,
        response_stage_blocked_intercepts: Vec<DevToolsNetworkInterceptId>,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_in_flight_response_stage_subresource_fetch_request(
            request_id,
            pending,
            response_stage_blocked_intercepts,
        );
        true
    }

    fn register_in_flight_subresource_fetch_request_with_response_match_policy(
        &mut self,
        request_id: Option<String>,
        mut pending: PendingSubresourceFetchRequest,
        response_stage_url_match_policy: crate::conn::ResponseStageUrlMatchPolicy,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_in_flight_subresource_fetch_request_with_response_match_policy(
            request_id,
            pending,
            response_stage_url_match_policy,
        );
        true
    }

    fn register_pending_subresource_fetch_auth_request(
        &mut self,
        request_id: String,
        mut pending: PendingSubresourceFetchAuthRequest,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_subresource_fetch_auth_request(request_id, pending);
        true
    }

    fn register_pending_subresource_fetch_response_request(
        &mut self,
        request_id: String,
        mut pending: PendingSubresourceFetchResponseRequest,
    ) -> bool {
        if pending.owner_session_id.is_none() {
            pending.owner_session_id = self.session_id().map(str::to_owned);
        }
        if pending.action_session_id.is_none() {
            pending.action_session_id = pending.owner_session_id.clone();
        }
        let Some(mut owner) = self.pending_fetch_owner_mut() else {
            return false;
        };
        owner.register_pending_subresource_fetch_response_request(request_id, pending);
        true
    }

    fn open_pending_fetch_response_body_stream(
        &mut self,
        request_id: &str,
    ) -> anyhow::Result<Option<String>> {
        let Some(mut owner) = self.fetch_body_stream_owner_mut() else {
            return Ok(None);
        };
        owner.open_pending_fetch_response_body_stream(request_id)
    }

    fn start_pending_fetch_response_body_stream_read(
        &mut self,
        handle: &str,
        offset: Option<usize>,
        size: Option<usize>,
    ) -> PendingFetchResponseBodyStreamReadStart {
        let Some(mut owner) = self.fetch_body_stream_owner_mut() else {
            return PendingFetchResponseBodyStreamReadStart::NotFound;
        };
        owner.start_pending_fetch_response_body_stream_read(handle, offset, size)
    }

    fn finish_pending_fetch_response_body_stream_read(
        &mut self,
        completed: CompletedFetchResponseBodyStreamReadDispatch,
    ) -> PendingFetchResponseBodyStreamRead {
        let Some(mut owner) = self.fetch_body_stream_owner_mut() else {
            return PendingFetchResponseBodyStreamRead::NotFound;
        };
        owner.finish_pending_fetch_response_body_stream_read(completed)
    }

    fn close_pending_fetch_response_body_stream(&mut self, handle: &str) -> bool {
        self.fetch_body_stream_owner_mut()
            .is_some_and(|mut owner| owner.close_pending_fetch_response_body_stream(handle))
    }
}