use std::path::{Path, PathBuf};
pub struct WebContext {
data: WebContextData,
#[allow(dead_code)] os: WebContextImpl,
}
impl WebContext {
pub fn new(data_directory: Option<PathBuf>) -> Self {
let data = WebContextData { data_directory };
let os = WebContextImpl::new(&data);
Self { data, os }
}
pub fn data_directory(&self) -> Option<&Path> {
self.data.data_directory()
}
pub fn set_allows_automation(&mut self, flag: bool) {
self.os.set_allows_automation(flag);
}
}
impl Default for WebContext {
fn default() -> Self {
let data = WebContextData::default();
let os = WebContextImpl::new(&data);
Self { data, os }
}
}
#[derive(Default)]
struct WebContextData {
data_directory: Option<PathBuf>,
}
impl WebContextData {
pub fn data_directory(&self) -> Option<&Path> {
self.data_directory.as_deref()
}
}
#[cfg(not(target_os = "linux"))]
struct WebContextImpl;
#[cfg(not(target_os = "linux"))]
impl WebContextImpl {
fn new(_data: &WebContextData) -> Self {
Self
}
fn set_allows_automation(&mut self, _flag: bool) {}
}
#[cfg(target_os = "linux")]
use self::unix::WebContextImpl;
#[cfg(target_os = "linux")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "linux")))]
pub mod unix {
use webkit2gtk::{
ApplicationInfo, WebContext, WebContextBuilder, WebContextExt as WebkitWebContextExt,
WebsiteDataManagerBuilder,
};
pub(super) struct WebContextImpl {
app_info: ApplicationInfo,
context: WebContext,
automation: bool,
}
impl WebContextImpl {
pub fn new(data: &super::WebContextData) -> Self {
let mut context_builder = WebContextBuilder::new();
if let Some(data_directory) = data.data_directory() {
let data_manager = WebsiteDataManagerBuilder::new()
.local_storage_directory(
&data_directory
.join("localstorage")
.to_string_lossy()
.into_owned(),
)
.indexeddb_directory(
&data_directory
.join("databases")
.join("indexeddb")
.to_string_lossy()
.into_owned(),
)
.build();
context_builder = context_builder.website_data_manager(&data_manager);
}
let context = context_builder.build();
let automation = false;
context.set_automation_allowed(automation);
let app_info = ApplicationInfo::new();
app_info.set_name(env!("CARGO_PKG_NAME"));
app_info.set_version(
env!("CARGO_PKG_VERSION_MAJOR")
.parse()
.expect("invalid wry version major"),
env!("CARGO_PKG_VERSION_MINOR")
.parse()
.expect("invalid wry version minor"),
env!("CARGO_PKG_VERSION_PATCH")
.parse()
.expect("invalid wry version patch"),
);
Self {
app_info,
context,
automation,
}
}
pub fn set_allows_automation(&mut self, flag: bool) {
self.automation = flag;
self.context.set_automation_allowed(flag);
}
}
pub trait WebContextExt {
fn app_info(&self) -> &ApplicationInfo;
fn context(&self) -> &WebContext;
fn allows_automation(&self) -> bool;
}
impl WebContextExt for super::WebContext {
fn app_info(&self) -> &ApplicationInfo {
&self.os.app_info
}
fn context(&self) -> &WebContext {
&self.os.context
}
fn allows_automation(&self) -> bool {
self.os.automation
}
}
}