Skip to content

armgit / moli

Public Imported from GitHub
0

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

2.0 KB
Raw Blame
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};

/// Identity and lifetime of a script-created top-level browsing context.
/// Both its opener's reference and its own Page retain this handle.
#[derive(Clone, Debug)]
pub struct RendererAuxiliaryWindow(Arc<AuxiliaryWindowState>);

#[derive(Debug)]
struct AuxiliaryWindowState {
    id: u64,
    closed: AtomicBool,
    is_popup: AtomicBool,
    frame_sandbox: crate::document_runtime::DocumentSandboxPolicy,
}

impl RendererAuxiliaryWindow {
    pub(super) fn new(
        id: u64,
        frame_sandbox: crate::document_runtime::DocumentSandboxPolicy,
    ) -> Self {
        Self(Arc::new(AuxiliaryWindowState {
            id,
            closed: AtomicBool::new(false),
            is_popup: AtomicBool::new(false),
            frame_sandbox,
        }))
    }

    pub fn id(&self) -> u64 {
        self.0.id
    }

    pub fn is_closed(&self) -> bool {
        self.0.closed.load(Ordering::Acquire)
    }

    /// Popup UI state belongs to the browsing context, including after its
    /// initial Document is transferred from the opener to an independent Page.
    pub(crate) fn is_popup(&self) -> bool {
        self.0.is_popup.load(Ordering::Acquire)
    }

    pub(crate) fn set_is_popup(&self, is_popup: bool) {
        self.0.is_popup.store(is_popup, Ordering::Release);
    }

    pub(crate) fn frame_sandbox(&self) -> crate::document_runtime::DocumentSandboxPolicy {
        self.0.frame_sandbox
    }

    /// Returns true only for the first accepted close request.
    pub fn close(&self) -> bool {
        !self.0.closed.swap(true, Ordering::AcqRel)
    }
}

impl PartialEq for RendererAuxiliaryWindow {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for RendererAuxiliaryWindow {}

/// The Location operation accepted for an existing auxiliary context.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RendererAuxiliaryNavigationKind {
    Assign,
    Replace,
    Reload,
}