#![allow(clippy::missing_safety_doc)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct RawEngine(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct RawEngineParams(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct RawStreamEngine(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct RawBidirectionalStream(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct RawCertVerifier(pub usize);
pub type RawDialer = extern "C" fn(
context: *mut std::ffi::c_void,
address: *const std::os::raw::c_char,
port: u16,
) -> isize;
pub type RawUdpDialer = extern "C" fn(
context: *mut std::ffi::c_void,
address: *const std::os::raw::c_char,
port: u16,
out_local_address: *mut std::os::raw::c_char,
out_local_port: *mut u16,
out_socket_id: *mut u64,
) -> isize;
pub type RawUdpSocketClose = extern "C" fn(socket_id: u64);
#[repr(C)]
pub struct RawBidirectionalStreamCallback {
pub on_stream_ready: Option<extern "C" fn(stream: RawBidirectionalStream)>,
pub on_response_headers_received: Option<
extern "C" fn(
stream: RawBidirectionalStream,
headers: *const RawBidirectionalStreamHeaderArray,
negotiated_protocol: *const std::os::raw::c_char,
),
>,
pub on_read_completed: Option<
extern "C" fn(
stream: RawBidirectionalStream,
data: *mut std::os::raw::c_char,
bytes_read: i32,
),
>,
pub on_write_completed: Option<
extern "C" fn(stream: RawBidirectionalStream, data: *const std::os::raw::c_char),
>,
pub on_response_trailers_received: Option<
extern "C" fn(
stream: RawBidirectionalStream,
trailers: *const RawBidirectionalStreamHeaderArray,
),
>,
pub on_succeeded: Option<extern "C" fn(stream: RawBidirectionalStream)>,
pub on_failed: Option<extern "C" fn(stream: RawBidirectionalStream, net_error: i32)>,
pub on_canceled: Option<extern "C" fn(stream: RawBidirectionalStream)>,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct RawBidirectionalStreamHeader {
pub key: *const std::os::raw::c_char,
pub value: *const std::os::raw::c_char,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct RawBidirectionalStreamHeaderArray {
pub count: usize,
pub capacity: usize,
pub headers: *const RawBidirectionalStreamHeader,
}
pub const STATIC_LINKED: bool = cfg!(cronet_static_link);
pub fn downloaded_library_path() -> Option<&'static str> {
option_env!("CRONET_DOWNLOADED_LIB_PATH")
}
pub unsafe fn load_downloaded_library() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(cronet_static_link)]
{
Ok(())
}
#[cfg(not(cronet_static_link))]
match downloaded_library_path() {
Some(path) => unsafe { load_library(path) },
None => Err("libcronet-sys was built without the `download` feature; \
no prebuilt library path is available"
.into()),
}
}
#[cfg(not(cronet_static_link))]
mod dynamic;
#[cfg(not(cronet_static_link))]
pub use dynamic::*;
#[cfg(cronet_static_link)]
mod static_link;
#[cfg(cronet_static_link)]
pub use static_link::*;
#[cfg(all(
test,
any(
feature = "download",
all(feature = "download-static", cronet_static_link)
)
))]
mod download_tests {
#[test]
fn downloaded_path_points_at_a_real_file() {
if std::env::var_os("CRONET_LIB_DIR").is_some() && super::STATIC_LINKED {
return;
}
let path = super::downloaded_library_path()
.expect("`download` feature should set CRONET_DOWNLOADED_LIB_PATH");
assert!(
std::path::Path::new(path).exists(),
"downloaded library should exist on disk: {path}"
);
}
}
#[cfg(all(test, cronet_static_link))]
mod static_tests {
#[test]
fn static_backend_does_not_load_a_runtime_library() {
assert!(unsafe { super::load_library("/nonexistent/cronet-library") }.is_ok());
assert!(unsafe { super::load_downloaded_library() }.is_ok());
}
}
#[cfg(all(test, not(cronet_static_link), not(feature = "download")))]
mod dynamic_tests {
#[test]
#[cfg_attr(miri, ignore = "requires the native dynamic loader")]
fn dynamic_backend_uses_runtime_library_without_static_download() {
assert!(super::downloaded_library_path().is_none());
assert!(unsafe { super::load_library("/nonexistent/cronet-library") }.is_err());
}
}