use std::io;
#[cfg(any(windows, test))]
pub const MAX_WEBVIEW_EVENTS: usize = 256;
pub const MAX_WEBVIEW_MESSAGE_BYTES: usize = 64 * 1024;
pub const MAX_WEBVIEW_MESSAGE_UNITS: usize = 64 * 1024;
pub const MAX_WEBVIEW_WAIT_MILLISECONDS: u32 = 30_000;
pub(crate) fn validate_message(bytes: &[u8]) -> io::Result<()> {
if bytes.len() > MAX_WEBVIEW_MESSAGE_BYTES {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"WebView2 JSON message exceeds the bounded byte limit",
));
}
if bytes.contains(&0) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"WebView2 JSON message contains NUL",
));
}
Ok(())
}
pub(crate) fn message_utf8_length(units: impl ExactSizeIterator<Item = u16>) -> io::Result<usize> {
if units.len() > MAX_WEBVIEW_MESSAGE_UNITS {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"WebView2 UTF-16 payload exceeds its bound",
));
}
let mut bytes = 0;
for character in char::decode_utf16(units) {
let character = character.map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
"WebView2 payload contains unmatched UTF-16",
)
})?;
if character == '\0' {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"WebView2 JSON message contains NUL",
));
}
bytes += character.len_utf8();
if bytes > MAX_WEBVIEW_MESSAGE_BYTES {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"WebView2 JSON message exceeds the bounded byte limit",
));
}
}
Ok(bytes)
}
#[cfg(test)]
mod tests;