use std::fmt;
use millipede_core::proxy::ProxyInfo;
use crate::{BrowserError, BrowserPage};
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct LaunchContext {
pub proxy: Option<ProxyInfo>,
pub extra_args: Vec<String>,
}
impl LaunchContext {
pub fn new() -> Self {
Self::default()
}
pub fn proxy(mut self, proxy: ProxyInfo) -> Self {
self.proxy = Some(proxy);
self
}
pub fn extra_args(mut self, extra_args: Vec<String>) -> Self {
self.extra_args = extra_args;
self
}
}
impl fmt::Debug for LaunchContext {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("LaunchContext")
.field(
"proxy_endpoint",
&self
.proxy
.as_ref()
.map(|proxy| (&proxy.hostname, proxy.port)),
)
.field("extra_args", &self.extra_args)
.finish()
}
}
#[async_trait::async_trait]
pub trait BrowserProvider: Send + Sync + 'static {
type Browser: Send + Sync + 'static;
type Page: BrowserPage + Clone;
type LaunchOptions: Default + Clone + Send + Sync + 'static;
async fn launch(
&self,
opts: Self::LaunchOptions,
ctx: &LaunchContext,
) -> Result<Self::Browser, BrowserError>;
async fn new_page(&self, browser: &Self::Browser) -> Result<Self::Page, BrowserError>;
async fn close_page(&self, page: Self::Page) -> Result<(), BrowserError>;
async fn close_browser(&self, browser: Self::Browser) -> Result<(), BrowserError>;
}