Skip to content

armgit / moli

Public Imported from GitHub
0

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

11.1 KB
Raw Blame
use std::future::Future;

use anyhow::Result;

use crate::frame_owner_model::FrameDocumentScriptExecutionWork;

pub(crate) struct DocumentScriptExecutionStartReport<Work, PrepareFollowup> {
    work: Option<Work>,
    prepare_followup: PrepareFollowup,
}

impl<Work, PrepareFollowup> DocumentScriptExecutionStartReport<Work, PrepareFollowup> {
    pub(crate) fn execute(work: Work, prepare_followup: PrepareFollowup) -> Self {
        Self {
            work: Some(work),
            prepare_followup,
        }
    }

    pub(crate) fn dropped(prepare_followup: PrepareFollowup) -> Self {
        Self {
            work: None,
            prepare_followup,
        }
    }

    pub(crate) fn into_parts(self) -> (Option<Work>, PrepareFollowup) {
        (self.work, self.prepare_followup)
    }
}

pub(crate) trait DocumentScriptExecutionHooks {
    type Ready;
    type PreparedWork;
    type PrepareFollowup;
    type ExecutionResult;
    type PostExecutionFollowup;
    type Output;
    type ExecuteFuture<'owner>: Future<Output = Result<Self::ExecutionResult>> + 'owner
    where
        Self: 'owner;

    fn prepare_execution(
        &mut self,
        ready: Self::Ready,
    ) -> DocumentScriptExecutionStartReport<Self::PreparedWork, Self::PrepareFollowup>;

    fn execute_work(&mut self, work: Self::PreparedWork) -> Self::ExecuteFuture<'_>;

    fn prepare_post_execution_followup(
        &mut self,
        execution_result: Self::ExecutionResult,
    ) -> Result<Self::PostExecutionFollowup>;

    fn apply_post_execution_followup(
        &mut self,
        followup: Self::PostExecutionFollowup,
    ) -> Result<Self::Output>;

    fn outcome_for_dropped_ready(
        &mut self,
        prepare_followup: Self::PrepareFollowup,
    ) -> Result<Self::Output>;
}

pub(crate) type FrameDocumentScriptExecutionStartReport<PrepareFollowup> =
    DocumentScriptExecutionStartReport<FrameDocumentScriptExecutionWork, PrepareFollowup>;

pub(crate) struct DocumentScriptExecutionRunner<Hooks> {
    hooks: Hooks,
}

pub(crate) type FrameDocumentScriptExecutionOwner<Hooks> = DocumentScriptExecutionRunner<Hooks>;

impl<Hooks> DocumentScriptExecutionRunner<Hooks> {
    pub(crate) fn new(hooks: Hooks) -> Self {
        Self { hooks }
    }
}

impl<Hooks> DocumentScriptExecutionRunner<Hooks>
where
    Hooks: DocumentScriptExecutionHooks,
{
    pub(crate) async fn run_ready_work(&mut self, ready: Hooks::Ready) -> Result<Hooks::Output> {
        let start_report = self.hooks.prepare_execution(ready);
        let (work, prepare_followup) = start_report.into_parts();
        match work {
            Some(work) => {
                let execution_result = self.hooks.execute_work(work).await?;
                let followup = self
                    .hooks
                    .prepare_post_execution_followup(execution_result)?;
                self.hooks.apply_post_execution_followup(followup)
            }
            None => self.hooks.outcome_for_dropped_ready(prepare_followup),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::future::{Ready, ready};

    use crate::document_runtime::DomHandle;
    use crate::document_script_scheduler::DocumentScriptExecutionOutcome;
    use crate::frame_owner_model::{
        DocumentId, FrameDocumentExternalClassicScriptExecutionAction,
        FrameDocumentScriptExecutionWork, FrameDocumentTaskOwner, FrameId, FrameRealmId,
        FrameSchedulerLaneId, FrameScriptJob, FrameScriptJobKind, FrameScriptSource, LocalWindowId,
        PendingChildExternalClassicDocumentScript,
    };
    use moli_fetch::RequestCredentialsMode;

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct FakePrepareFollowup {
        prepared: bool,
        dropped: bool,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct FakeExecutionFollowup {
        attempted: bool,
    }

    #[derive(Default)]
    struct FakeFrameDocumentScriptHooks {
        events: Vec<&'static str>,
        prepared_work: Option<FrameDocumentScriptExecutionWork>,
        fail_execution: bool,
        dropped_followups: Vec<FakePrepareFollowup>,
        execution_followups: Vec<FakeExecutionFollowup>,
    }

    impl DocumentScriptExecutionHooks for FakeFrameDocumentScriptHooks {
        type Ready = &'static str;
        type PreparedWork = FrameDocumentScriptExecutionWork;
        type PrepareFollowup = FakePrepareFollowup;
        type ExecutionResult = FakeExecutionFollowup;
        type PostExecutionFollowup = FakeExecutionFollowup;
        type Output = DocumentScriptExecutionOutcome;
        type ExecuteFuture<'owner>
            = Ready<Result<FakeExecutionFollowup>>
        where
            Self: 'owner;

        fn prepare_execution(
            &mut self,
            ready: &'static str,
        ) -> FrameDocumentScriptExecutionStartReport<FakePrepareFollowup> {
            self.events.push(ready);
            match self.prepared_work.take() {
                Some(work) => FrameDocumentScriptExecutionStartReport::execute(
                    work,
                    FakePrepareFollowup {
                        prepared: true,
                        dropped: false,
                    },
                ),
                None => FrameDocumentScriptExecutionStartReport::dropped(FakePrepareFollowup {
                    prepared: false,
                    dropped: true,
                }),
            }
        }

        fn execute_work(
            &mut self,
            work: FrameDocumentScriptExecutionWork,
        ) -> Self::ExecuteFuture<'_> {
            self.events.push(match work {
                FrameDocumentScriptExecutionWork::ExternalClassic(_) => "external-classic",
                FrameDocumentScriptExecutionWork::JavascriptUrl(_) => "javascript-url",
                FrameDocumentScriptExecutionWork::ModuleScript(_) => "module-script",
            });
            if self.fail_execution {
                return ready(Err(anyhow::anyhow!(
                    "frame document script execution failed"
                )));
            }
            ready(Ok(FakeExecutionFollowup { attempted: true }))
        }

        fn prepare_post_execution_followup(
            &mut self,
            execution_followup: FakeExecutionFollowup,
        ) -> Result<FakeExecutionFollowup> {
            Ok(execution_followup)
        }

        fn apply_post_execution_followup(
            &mut self,
            execution_followup: FakeExecutionFollowup,
        ) -> Result<DocumentScriptExecutionOutcome> {
            self.execution_followups.push(execution_followup);
            Ok(DocumentScriptExecutionOutcome::Progressed)
        }

        fn outcome_for_dropped_ready(
            &mut self,
            prepare_followup: FakePrepareFollowup,
        ) -> Result<DocumentScriptExecutionOutcome> {
            self.dropped_followups.push(prepare_followup);
            Ok(DocumentScriptExecutionOutcome::Progressed)
        }
    }

    fn external_classic_work() -> FrameDocumentScriptExecutionWork {
        let script_url = url::Url::parse("https://frame-document-script-owner.test/dynamic.js")
            .expect("dynamic script URL should parse");
        let owner = FrameDocumentTaskOwner::new(
            FrameSchedulerLaneId(100),
            LocalWindowId(10),
            DocumentId(20),
        );
        let pending = PendingChildExternalClassicDocumentScript {
            child_handle: DomHandle::new(1),
            owner,
            realm_id: Some(FrameRealmId(30)),
            script_handle: DomHandle::new(40),
            load_delay:
                crate::frame_owner_model::ChildDocumentAsyncClassicScriptLoadDelay::AlreadyUnblocked,
            source_result: Ok("globalThis.__dynamic = true;".to_owned()),
            script_url: script_url.clone(),
            script_base_url: script_url.clone(),
        };
        let job = FrameScriptJob {
            frame_id: FrameId("dynamic-frame".to_owned()),
            local_window_id: owner.local_window_id,
            document_id: owner.document_id,
            current_script: Some(DomHandle::new(40)),
            kind: FrameScriptJobKind::ExternalClassic,
            source: FrameScriptSource::SourceText("globalThis.__dynamic = true;".to_owned()),
            script_url: script_url.clone(),
            base_url: script_url,
            script_nonce: None,
            script_integrity: None,
            credentials_mode: RequestCredentialsMode::SameOrigin,
            referrer_policy: None,
        };
        FrameDocumentScriptExecutionWork::external_classic(
            FrameDocumentExternalClassicScriptExecutionAction::new(
                pending.execution_target(FrameRealmId(30)),
                crate::frame_owner_model::FrameDocumentExternalClassicScriptExecution::ScriptJob(
                    Box::new(job),
                ),
            ),
        )
    }

    #[tokio::test]
    async fn started_frame_document_script_execution_runs_work_and_reports_followup() {
        let hooks = FakeFrameDocumentScriptHooks {
            prepared_work: Some(external_classic_work()),
            ..Default::default()
        };
        let mut owner = FrameDocumentScriptExecutionOwner::new(hooks);

        let outcome = owner
            .run_ready_work("prepare")
            .await
            .expect("frame document script owner should run ready work");

        assert_eq!(outcome, DocumentScriptExecutionOutcome::Progressed);
        assert_eq!(owner.hooks.events, ["prepare", "external-classic"]);
        assert_eq!(
            owner.hooks.execution_followups,
            [FakeExecutionFollowup { attempted: true }]
        );
        assert!(owner.hooks.dropped_followups.is_empty());
    }

    #[tokio::test]
    async fn dropped_frame_document_script_ready_reports_prepare_followup_without_execute() {
        let hooks = FakeFrameDocumentScriptHooks::default();
        let mut owner = FrameDocumentScriptExecutionOwner::new(hooks);

        let outcome = owner
            .run_ready_work("prepare")
            .await
            .expect("frame document script owner should run dropped ready work");

        assert_eq!(outcome, DocumentScriptExecutionOutcome::Progressed);
        assert_eq!(owner.hooks.events, ["prepare"]);
        assert_eq!(
            owner.hooks.dropped_followups,
            [FakePrepareFollowup {
                prepared: false,
                dropped: true,
            }]
        );
        assert!(owner.hooks.execution_followups.is_empty());
    }

    #[tokio::test]
    async fn frame_document_script_execution_error_stops_before_outcome_mapping() {
        let hooks = FakeFrameDocumentScriptHooks {
            prepared_work: Some(external_classic_work()),
            fail_execution: true,
            ..Default::default()
        };
        let mut owner = FrameDocumentScriptExecutionOwner::new(hooks);

        let error = owner
            .run_ready_work("prepare")
            .await
            .expect_err("frame document script owner should propagate execution failures");

        assert_eq!(error.to_string(), "frame document script execution failed");
        assert_eq!(owner.hooks.events, ["prepare", "external-classic"]);
        assert!(owner.hooks.execution_followups.is_empty());
        assert!(owner.hooks.dropped_followups.is_empty());
    }
}