use crate::error::Result;
use crate::protocol::{Browser, Page, Request, ResponseObject, Route};
use crate::server::channel_owner::{ChannelOwner, ChannelOwnerImpl};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use crate::protocol::event_registry::EventRegistry;
use tokio::sync::oneshot;
type RouteHandlerFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type BindingCallbackFuture = Pin<Box<dyn Future<Output = serde_json::Value> + Send>>;
type ServiceWorkerHandlerFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type ServiceWorkerHandler =
Arc<dyn Fn(crate::protocol::Worker) -> ServiceWorkerHandlerFuture + Send + Sync>;
type CtxHandlerFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type BindingCallback = Arc<dyn Fn(Vec<serde_json::Value>) -> BindingCallbackFuture + Send + Sync>;
type WsRouteHandlerFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
#[derive(Clone)]
struct RouteHandlerEntry {
pattern: String,
handler: Arc<dyn Fn(Route) -> RouteHandlerFuture + Send + Sync>,
in_flight: Arc<crate::protocol::in_flight::InFlight>,
}
#[derive(Clone)]
struct ContextWsRouteHandlerEntry {
pattern: String,
handler: Arc<dyn Fn(crate::protocol::WebSocketRoute) -> WsRouteHandlerFuture + Send + Sync>,
}
#[derive(Clone)]
pub struct BrowserContext {
base: ChannelOwnerImpl,
browser: Option<Browser>,
pages: Arc<Mutex<Vec<Page>>>,
route_handlers: Arc<Mutex<Vec<RouteHandlerEntry>>>,
request_context_guid: Option<String>,
tracing_guid: Option<String>,
debugger_guid: Option<String>,
default_timeout_ms: Arc<std::sync::atomic::AtomicU64>,
default_navigation_timeout_ms: Arc<std::sync::atomic::AtomicU64>,
page_events: Arc<EventRegistry<Page>>,
close_events: Arc<EventRegistry<()>>,
request: Arc<EventRegistry<Request>>,
request_finished: Arc<EventRegistry<Request>>,
request_failed: Arc<EventRegistry<Request>>,
response: Arc<EventRegistry<ResponseObject>>,
dialog: Arc<EventRegistry<crate::protocol::Dialog>>,
dialog_closed: Arc<EventRegistry<crate::protocol::Dialog>>,
binding_callbacks: Arc<Mutex<HashMap<String, BindingCallback>>>,
console: Arc<EventRegistry<crate::protocol::ConsoleMessage>>,
weberror: Arc<EventRegistry<crate::protocol::WebError>>,
serviceworker_handlers: Arc<Mutex<Vec<ServiceWorkerHandler>>>,
forwarders: events::Forwarders,
serviceworker_waiters: Arc<Mutex<Vec<oneshot::Sender<crate::protocol::Worker>>>>,
service_workers_list: Arc<Mutex<Vec<crate::protocol::Worker>>>,
ws_route_handlers: Arc<Mutex<Vec<ContextWsRouteHandlerEntry>>>,
is_closed: Arc<AtomicBool>,
option_har: Arc<tokio::sync::Mutex<Option<crate::protocol::tracing::HarRecording>>>,
}
mod lifecycle;
mod state;
mod network;
mod events;
mod bindings;
mod options;
mod dispatch;
pub use options::{
AcceptDownloads, BrowserContextOptions, BrowserContextOptionsBuilder, RecordHar, RecordVideo,
Viewport,
};
pub use state::{
ClearCookiesOptions, Cookie, Geolocation, GrantPermissionsOptions, HttpCredentials,
HttpCredentialsSend, LocalStorageItem, Origin, StorageState, StorageStateOptions,
};
impl std::fmt::Debug for BrowserContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BrowserContext")
.field("guid", &self.guid())
.finish()
}
}