use std::ffi::{CString, c_char};
use crate::{Error, ffi::*, settings::Settings};
pub fn init(settings: Settings) -> Result<(), Error> {
unsafe {
let c_settings = CSettings {
locale: to_cstr_ptr_opt(settings.locale.as_deref()),
cache_path: to_cstr_ptr_opt(settings.cache_path.as_deref()),
root_cache_path: to_cstr_ptr_opt(settings.root_cache_path.as_deref()),
browser_subprocess_path: to_cstr_ptr_opt(settings.browser_subprocess_path.as_deref()),
};
if !wef_init(&c_settings) {
return Err(Error::InitializeBrowserProcess);
}
}
Ok(())
}
pub fn exec_process() -> Result<bool, Error> {
let args: Vec<CString> = std::env::args()
.filter_map(|arg| CString::new(arg).ok())
.collect();
let c_args: Vec<*const c_char> = args.iter().map(|arg| arg.as_ptr()).collect();
Ok(unsafe { wef_exec_process(c_args.as_ptr(), args.len() as i32) })
}
pub fn shutdown() {
unsafe { wef_shutdown() };
}
pub fn launch<F, R>(settings: Settings, f: F) -> R
where
F: FnOnce() -> R,
{
#[cfg(not(target_os = "macos"))]
if exec_process().expect("failed to execute process") {
std::process::exit(0);
}
#[cfg(target_os = "macos")]
let _ = crate::FrameworkLoader::load_in_main().expect("failed to load CEF framework");
init(settings).expect("failed to initialize CEF");
let res = f();
shutdown();
res
}