#[cfg(gtk)]
use crate::webkitgtk::WebContextImpl;
use std::{
collections::HashSet,
path::{Path, PathBuf},
};
#[derive(Debug)]
pub struct WebContext {
data_directory: Option<PathBuf>,
#[allow(dead_code)] pub(crate) os: WebContextImpl,
#[allow(dead_code)] pub(crate) custom_protocols: HashSet<String>,
}
impl WebContext {
pub fn new(data_directory: Option<PathBuf>) -> Self {
Self {
os: WebContextImpl::new(data_directory.as_deref()),
data_directory,
custom_protocols: Default::default(),
}
}
#[cfg(gtk)]
pub(crate) fn new_ephemeral() -> Self {
Self {
os: WebContextImpl::new_ephemeral(),
data_directory: None,
custom_protocols: Default::default(),
}
}
pub fn data_directory(&self) -> Option<&Path> {
self.data_directory.as_deref()
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
pub(crate) fn register_custom_protocol(&mut self, name: String) -> Result<(), crate::Error> {
if self.is_custom_protocol_registered(&name) {
return Err(crate::Error::ContextDuplicateCustomProtocol(name));
}
self.custom_protocols.insert(name);
Ok(())
}
pub fn is_custom_protocol_registered(&self, name: &str) -> bool {
self.custom_protocols.contains(name)
}
pub fn set_allows_automation(&mut self, flag: bool) {
self.os.set_allows_automation(flag);
}
}
impl Default for WebContext {
fn default() -> Self {
Self::new(None)
}
}
#[cfg(not(gtk))]
#[derive(Debug)]
pub(crate) struct WebContextImpl;
#[cfg(not(gtk))]
impl WebContextImpl {
fn new(_: Option<&Path>) -> Self {
Self
}
fn set_allows_automation(&mut self, _flag: bool) {}
}