Skip to content

armgit / moli

Public Imported from GitHub
0

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

5.1 KB
Raw Blame
use super::*;

pub(super) fn start_pending_runtime_disable_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> RuntimeCommandTaskStep {
    let owner_scope = CommandOwnerScope::capture(conn, cmd.session_id);
    let response_delivery = cmd.terminal_response_delivery();
    start_runtime_disable_command_for_owner(conn, cmd, owner_scope, response_delivery)
}

pub(super) fn start_runtime_disable_command_for_owner(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
    owner_scope: CommandOwnerScope,
    response_delivery: RendererInspectorResponseDelivery,
) -> RuntimeCommandTaskStep {
    if !conn
        .runtime_session_owner_slot_for_owner(&owner_scope)
        .is_some_and(|slot| slot.has_loaded_page())
    {
        return RuntimeCommandTaskStep::Complete(disable_command_output_plan_sync_for_owner(
            conn,
            &owner_scope,
        ));
    }

    let pending = match start_pending_runtime_inspector_dispatch_with_delivery(
        conn,
        cmd,
        &owner_scope,
        cmd.json.to_owned(),
        response_delivery,
    ) {
        Ok(pending) => pending,
        Err(message) => {
            return RuntimeCommandTaskStep::Complete(runtime_inspector_error_plan(cmd.id, message));
        }
    };

    RuntimeCommandTaskStep::Pending(Box::new(PendingRuntimeCommandDispatch {
        command_id: cmd.id,
        action: "disable",
        owner_scope,
        object_group: None,
        release_object_ids: Vec::new(),
        release_object_group: None,
        await_promise: false,
        wait_for_deferred_reply: false,
        pending: PendingRuntimeCommandKind::Inspector { pending },
    }))
}

pub(super) fn start_runtime_run_if_waiting_for_debugger_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> RuntimeCommandTaskStep {
    if let Some(crate::conn::CdpSessionRoute::TabTarget { tab_target_id, .. }) =
        conn.session_route(cmd.session_id)
    {
        // A Tab has a browser-side debugger barrier, but no V8 context. Resume
        // only this attachment; never dispatch the command to an active Page.
        let page_target_id = conn
            .primary_page_target_id_for_tab_target_id(&tab_target_id)
            .map(str::to_owned);
        if conn.release_waiting_for_debugger_session(cmd.session_id)
            && let Some(page_target_id) = page_target_id
        {
            crate::domains::target::schedule_initial_document_target_url_navigation_after_debugger_barrier_release_for_target(
                conn, &page_target_id,
            );
        }
        return RuntimeCommandTaskStep::Complete(CommandOutputPlan::success());
    }
    if !conn
        .runtime_session_owner_slot(cmd.session_id)
        .is_some_and(|slot| slot.has_loaded_page())
    {
        return RuntimeCommandTaskStep::Complete(CommandOutputPlan::success());
    }

    let owner_scope = CommandOwnerScope::capture(conn, cmd.session_id);
    let pending = match start_pending_runtime_inspector_dispatch_with_delivery(
        conn,
        cmd,
        &owner_scope,
        cmd.json.to_owned(),
        cmd.terminal_response_delivery(),
    ) {
        Ok(pending) => pending,
        Err(message) if message == "NoDocumentLoaded" => {
            return RuntimeCommandTaskStep::Complete(CommandOutputPlan::success());
        }
        Err(message) => {
            return RuntimeCommandTaskStep::Complete(runtime_inspector_error_plan(cmd.id, message));
        }
    };

    RuntimeCommandTaskStep::Pending(Box::new(PendingRuntimeCommandDispatch {
        command_id: cmd.id,
        action: "runIfWaitingForDebugger",
        owner_scope,
        object_group: None,
        release_object_ids: Vec::new(),
        release_object_group: None,
        await_promise: false,
        wait_for_deferred_reply: false,
        pending: PendingRuntimeCommandKind::Inspector { pending },
    }))
}

pub(super) fn start_runtime_discard_console_entries_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> RuntimeCommandTaskStep {
    if !conn
        .runtime_session_owner_slot(cmd.session_id)
        .is_some_and(|slot| slot.has_loaded_page())
    {
        advance_runtime_observable_cursors_to_current_for_session_owner(conn, cmd.session_id);
        return RuntimeCommandTaskStep::Complete(CommandOutputPlan::success());
    }
    let owner_scope = CommandOwnerScope::capture(conn, cmd.session_id);
    let pending = match start_pending_runtime_inspector_dispatch_with_delivery(
        conn,
        cmd,
        &owner_scope,
        cmd.json.to_owned(),
        cmd.terminal_response_delivery(),
    ) {
        Ok(pending) => pending,
        Err(message) => {
            return RuntimeCommandTaskStep::Complete(runtime_inspector_error_plan(cmd.id, message));
        }
    };
    RuntimeCommandTaskStep::Pending(Box::new(PendingRuntimeCommandDispatch {
        command_id: cmd.id,
        action: "discardConsoleEntries",
        owner_scope,
        object_group: None,
        release_object_ids: Vec::new(),
        release_object_group: None,
        await_promise: false,
        wait_for_deferred_reply: false,
        pending: PendingRuntimeCommandKind::Inspector { pending },
    }))
}