mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Catch more panics.
Stop using catch_unwind and use set_hook, allowing us to catch more panics. Also reporting Embedder::Panic messages.
This commit is contained in:
parent
ef8eabd930
commit
593200e693
3 changed files with 185 additions and 240 deletions
|
@ -148,6 +148,8 @@ pub trait HostTrait {
|
||||||
fn on_media_session_set_position_state(&self, duration: f64, position: f64, playback_rate: f64);
|
fn on_media_session_set_position_state(&self, duration: f64, position: f64, playback_rate: f64);
|
||||||
/// Called when devtools server is started
|
/// Called when devtools server is started
|
||||||
fn on_devtools_started(&self, port: Result<u16, ()>, token: String);
|
fn on_devtools_started(&self, port: Result<u16, ()>, token: String);
|
||||||
|
/// Called when we get a panic message from constellation
|
||||||
|
fn on_panic(&self, reason: String, backtrace: Option<String>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ServoGlue {
|
pub struct ServoGlue {
|
||||||
|
@ -764,6 +766,9 @@ impl ServoGlue {
|
||||||
.host_callbacks
|
.host_callbacks
|
||||||
.on_devtools_started(port, token);
|
.on_devtools_started(port, token);
|
||||||
},
|
},
|
||||||
|
EmbedderMsg::Panic(reason, backtrace) => {
|
||||||
|
self.callbacks.host_callbacks.on_panic(reason, backtrace);
|
||||||
|
},
|
||||||
EmbedderMsg::Status(..) |
|
EmbedderMsg::Status(..) |
|
||||||
EmbedderMsg::SelectFiles(..) |
|
EmbedderMsg::SelectFiles(..) |
|
||||||
EmbedderMsg::MoveTo(..) |
|
EmbedderMsg::MoveTo(..) |
|
||||||
|
@ -773,7 +778,6 @@ impl ServoGlue {
|
||||||
EmbedderMsg::NewFavicon(..) |
|
EmbedderMsg::NewFavicon(..) |
|
||||||
EmbedderMsg::HeadParsed |
|
EmbedderMsg::HeadParsed |
|
||||||
EmbedderMsg::SetFullscreenState(..) |
|
EmbedderMsg::SetFullscreenState(..) |
|
||||||
EmbedderMsg::Panic(..) |
|
|
||||||
EmbedderMsg::ReportProfile(..) => {},
|
EmbedderMsg::ReportProfile(..) => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ mod prefs;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
mod vslogger;
|
mod vslogger;
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
use env_logger;
|
use env_logger;
|
||||||
use keyboard_types::Key;
|
use keyboard_types::Key;
|
||||||
|
@ -27,7 +26,6 @@ use std::ffi::{CStr, CString};
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os::raw::{c_char, c_uint, c_void};
|
use std::os::raw::{c_char, c_uint, c_void};
|
||||||
use std::panic::{self, UnwindSafe};
|
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::{Mutex, RwLock};
|
use std::sync::{Mutex, RwLock};
|
||||||
|
@ -51,22 +49,15 @@ pub extern "C" fn register_panic_handler(on_panic: extern "C" fn(*const c_char))
|
||||||
*ON_PANIC.write().unwrap() = on_panic;
|
*ON_PANIC.write().unwrap() = on_panic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Catch any panic function used by extern "C" functions.
|
/// Report panic to embedder.
|
||||||
fn catch_any_panic<T, F: FnOnce() -> T + UnwindSafe>(function: F) -> T {
|
fn report_panic(reason: &str, backtrace: Option<String>) {
|
||||||
match panic::catch_unwind(function) {
|
let message = match backtrace {
|
||||||
Err(_) => {
|
Some(bt) => format!("Servo panic ({})\n{}", reason, bt),
|
||||||
let thread = std::thread::current()
|
None => format!("Servo panic ({})", reason),
|
||||||
.name()
|
};
|
||||||
.map(|n| format!(" for thread \"{}\"", n))
|
let error = CString::new(message).expect("Can't create string");
|
||||||
.unwrap_or("".to_owned());
|
(ON_PANIC.read().unwrap())(error.as_ptr());
|
||||||
let message = format!("Stack trace{}\n{:?}", thread, Backtrace::new());
|
panic!("At that point, embedder should have thrown");
|
||||||
let error = CString::new(message).expect("Can't create string");
|
|
||||||
(ON_PANIC.read().unwrap())(error.as_ptr());
|
|
||||||
// At that point the embedder is supposed to have panicked
|
|
||||||
panic!("Uncaught Rust panic");
|
|
||||||
},
|
|
||||||
Ok(r) => r,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
@ -396,6 +387,31 @@ unsafe fn init(
|
||||||
wakeup: extern "C" fn(),
|
wakeup: extern "C" fn(),
|
||||||
callbacks: CHostCallbacks,
|
callbacks: CHostCallbacks,
|
||||||
) {
|
) {
|
||||||
|
// Catch any panics.
|
||||||
|
std::panic::set_hook(Box::new(|info| {
|
||||||
|
let msg = match info.payload().downcast_ref::<&'static str>() {
|
||||||
|
Some(s) => *s,
|
||||||
|
None => match info.payload().downcast_ref::<String>() {
|
||||||
|
Some(s) => &**s,
|
||||||
|
None => "Box<Any>",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let current_thread = std::thread::current();
|
||||||
|
let name = current_thread.name().unwrap_or("<unnamed>");
|
||||||
|
let details = if let Some(location) = info.location() {
|
||||||
|
format!(
|
||||||
|
"{} (thread {}, at {}:{})",
|
||||||
|
msg,
|
||||||
|
name,
|
||||||
|
location.file(),
|
||||||
|
location.line()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!("{} (thread {})", msg, name)
|
||||||
|
};
|
||||||
|
report_panic("General panic handler", Some(details));
|
||||||
|
}));
|
||||||
|
|
||||||
let args = if !opts.args.is_null() {
|
let args = if !opts.args.is_null() {
|
||||||
let args = CStr::from_ptr(opts.args);
|
let args = CStr::from_ptr(opts.args);
|
||||||
args.to_str()
|
args.to_str()
|
||||||
|
@ -463,19 +479,17 @@ pub extern "C" fn init_with_egl(
|
||||||
wakeup: extern "C" fn(),
|
wakeup: extern "C" fn(),
|
||||||
callbacks: CHostCallbacks,
|
callbacks: CHostCallbacks,
|
||||||
) {
|
) {
|
||||||
catch_any_panic(|| {
|
let gl = gl_glue::egl::init().unwrap();
|
||||||
let gl = gl_glue::egl::init().unwrap();
|
unsafe {
|
||||||
unsafe {
|
init(
|
||||||
init(
|
opts,
|
||||||
opts,
|
gl.gl_wrapper,
|
||||||
gl.gl_wrapper,
|
Some(gl.gl_context),
|
||||||
Some(gl.gl_context),
|
Some(gl.display),
|
||||||
Some(gl.display),
|
wakeup,
|
||||||
wakeup,
|
callbacks,
|
||||||
callbacks,
|
)
|
||||||
)
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
|
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
|
||||||
|
@ -485,244 +499,186 @@ pub extern "C" fn init_with_gl(
|
||||||
wakeup: extern "C" fn(),
|
wakeup: extern "C" fn(),
|
||||||
callbacks: CHostCallbacks,
|
callbacks: CHostCallbacks,
|
||||||
) {
|
) {
|
||||||
catch_any_panic(|| {
|
let gl = gl_glue::gl::init().unwrap();
|
||||||
let gl = gl_glue::gl::init().unwrap();
|
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
|
||||||
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn deinit() {
|
pub extern "C" fn deinit() {
|
||||||
catch_any_panic(|| {
|
debug!("deinit");
|
||||||
debug!("deinit");
|
simpleservo::deinit();
|
||||||
simpleservo::deinit();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn request_shutdown() {
|
pub extern "C" fn request_shutdown() {
|
||||||
catch_any_panic(|| {
|
debug!("request_shutdown");
|
||||||
debug!("request_shutdown");
|
call(|s| s.request_shutdown());
|
||||||
call(|s| s.request_shutdown());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn set_batch_mode(batch: bool) {
|
pub extern "C" fn set_batch_mode(batch: bool) {
|
||||||
catch_any_panic(|| {
|
debug!("set_batch_mode");
|
||||||
debug!("set_batch_mode");
|
call(|s| s.set_batch_mode(batch));
|
||||||
call(|s| s.set_batch_mode(batch));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn on_context_menu_closed(result: CContextMenuResult, item: u32) {
|
pub extern "C" fn on_context_menu_closed(result: CContextMenuResult, item: u32) {
|
||||||
catch_any_panic(|| {
|
debug!("on_context_menu_closed");
|
||||||
debug!("on_context_menu_closed");
|
call(|s| s.on_context_menu_closed(result.convert(item)));
|
||||||
call(|s| s.on_context_menu_closed(result.convert(item)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn resize(width: i32, height: i32) {
|
pub extern "C" fn resize(width: i32, height: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("resize {}/{}", width, height);
|
||||||
debug!("resize {}/{}", width, height);
|
call(|s| {
|
||||||
call(|s| {
|
let coordinates = Coordinates::new(0, 0, width, height, width, height);
|
||||||
let coordinates = Coordinates::new(0, 0, width, height, width, height);
|
s.resize(coordinates)
|
||||||
s.resize(coordinates)
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn perform_updates() {
|
pub extern "C" fn perform_updates() {
|
||||||
catch_any_panic(|| {
|
debug!("perform_updates");
|
||||||
debug!("perform_updates");
|
// We might have allocated some memory to respond to a potential
|
||||||
// We might have allocated some memory to respond to a potential
|
// request, from the embedder, for a copy of Servo's preferences.
|
||||||
// request, from the embedder, for a copy of Servo's preferences.
|
prefs::free_prefs();
|
||||||
prefs::free_prefs();
|
call(|s| s.perform_updates());
|
||||||
call(|s| s.perform_updates());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn is_uri_valid(url: *const c_char) -> bool {
|
pub extern "C" fn is_uri_valid(url: *const c_char) -> bool {
|
||||||
catch_any_panic(|| {
|
debug!("is_uri_valid");
|
||||||
debug!("is_uri_valid");
|
let url = unsafe { CStr::from_ptr(url) };
|
||||||
let url = unsafe { CStr::from_ptr(url) };
|
let url = url.to_str().expect("Can't read string");
|
||||||
let url = url.to_str().expect("Can't read string");
|
simpleservo::is_uri_valid(url)
|
||||||
simpleservo::is_uri_valid(url)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn load_uri(url: *const c_char) -> bool {
|
pub extern "C" fn load_uri(url: *const c_char) -> bool {
|
||||||
catch_any_panic(|| {
|
debug!("load_url");
|
||||||
debug!("load_url");
|
let url = unsafe { CStr::from_ptr(url) };
|
||||||
let url = unsafe { CStr::from_ptr(url) };
|
let url = url.to_str().expect("Can't read string");
|
||||||
let url = url.to_str().expect("Can't read string");
|
call(|s| Ok(s.load_uri(url).is_ok()))
|
||||||
call(|s| Ok(s.load_uri(url).is_ok()))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn clear_cache() {
|
pub extern "C" fn clear_cache() {
|
||||||
catch_any_panic(|| {
|
debug!("clear_cache");
|
||||||
debug!("clear_cache");
|
call(|s| s.clear_cache())
|
||||||
call(|s| s.clear_cache())
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn reload() {
|
pub extern "C" fn reload() {
|
||||||
catch_any_panic(|| {
|
debug!("reload");
|
||||||
debug!("reload");
|
call(|s| s.reload());
|
||||||
call(|s| s.reload());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn stop() {
|
pub extern "C" fn stop() {
|
||||||
catch_any_panic(|| {
|
debug!("stop");
|
||||||
debug!("stop");
|
call(|s| s.stop());
|
||||||
call(|s| s.stop());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn refresh() {
|
pub extern "C" fn refresh() {
|
||||||
catch_any_panic(|| {
|
debug!("refresh");
|
||||||
debug!("refresh");
|
call(|s| s.refresh());
|
||||||
call(|s| s.refresh());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn go_back() {
|
pub extern "C" fn go_back() {
|
||||||
catch_any_panic(|| {
|
debug!("go_back");
|
||||||
debug!("go_back");
|
call(|s| s.go_back());
|
||||||
call(|s| s.go_back());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn go_forward() {
|
pub extern "C" fn go_forward() {
|
||||||
catch_any_panic(|| {
|
debug!("go_forward");
|
||||||
debug!("go_forward");
|
call(|s| s.go_forward());
|
||||||
call(|s| s.go_forward());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn scroll_start(dx: i32, dy: i32, x: i32, y: i32) {
|
pub extern "C" fn scroll_start(dx: i32, dy: i32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("scroll_start");
|
||||||
debug!("scroll_start");
|
call(|s| s.scroll_start(dx as f32, dy as f32, x, y));
|
||||||
call(|s| s.scroll_start(dx as f32, dy as f32, x, y));
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn scroll_end(dx: i32, dy: i32, x: i32, y: i32) {
|
pub extern "C" fn scroll_end(dx: i32, dy: i32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("scroll_end");
|
||||||
debug!("scroll_end");
|
call(|s| s.scroll_end(dx as f32, dy as f32, x, y));
|
||||||
call(|s| s.scroll_end(dx as f32, dy as f32, x, y));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn scroll(dx: i32, dy: i32, x: i32, y: i32) {
|
pub extern "C" fn scroll(dx: i32, dy: i32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("scroll");
|
||||||
debug!("scroll");
|
call(|s| s.scroll(dx as f32, dy as f32, x, y));
|
||||||
call(|s| s.scroll(dx as f32, dy as f32, x, y));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn touch_down(x: f32, y: f32, pointer_id: i32) {
|
pub extern "C" fn touch_down(x: f32, y: f32, pointer_id: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("touch down");
|
||||||
debug!("touch down");
|
call(|s| s.touch_down(x, y, pointer_id));
|
||||||
call(|s| s.touch_down(x, y, pointer_id));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn touch_up(x: f32, y: f32, pointer_id: i32) {
|
pub extern "C" fn touch_up(x: f32, y: f32, pointer_id: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("touch up");
|
||||||
debug!("touch up");
|
call(|s| s.touch_up(x, y, pointer_id));
|
||||||
call(|s| s.touch_up(x, y, pointer_id));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn touch_move(x: f32, y: f32, pointer_id: i32) {
|
pub extern "C" fn touch_move(x: f32, y: f32, pointer_id: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("touch move");
|
||||||
debug!("touch move");
|
call(|s| s.touch_move(x, y, pointer_id));
|
||||||
call(|s| s.touch_move(x, y, pointer_id));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn touch_cancel(x: f32, y: f32, pointer_id: i32) {
|
pub extern "C" fn touch_cancel(x: f32, y: f32, pointer_id: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("touch cancel");
|
||||||
debug!("touch cancel");
|
call(|s| s.touch_cancel(x, y, pointer_id));
|
||||||
call(|s| s.touch_cancel(x, y, pointer_id));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) {
|
pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("pinchzoom_start");
|
||||||
debug!("pinchzoom_start");
|
call(|s| s.pinchzoom_start(factor, x as u32, y as u32));
|
||||||
call(|s| s.pinchzoom_start(factor, x as u32, y as u32));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn pinchzoom(factor: f32, x: i32, y: i32) {
|
pub extern "C" fn pinchzoom(factor: f32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("pinchzoom");
|
||||||
debug!("pinchzoom");
|
call(|s| s.pinchzoom(factor, x as u32, y as u32));
|
||||||
call(|s| s.pinchzoom(factor, x as u32, y as u32));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn pinchzoom_end(factor: f32, x: i32, y: i32) {
|
pub extern "C" fn pinchzoom_end(factor: f32, x: i32, y: i32) {
|
||||||
catch_any_panic(|| {
|
debug!("pinchzoom_end");
|
||||||
debug!("pinchzoom_end");
|
call(|s| s.pinchzoom_end(factor, x as u32, y as u32));
|
||||||
call(|s| s.pinchzoom_end(factor, x as u32, y as u32));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn mouse_move(x: f32, y: f32) {
|
pub extern "C" fn mouse_move(x: f32, y: f32) {
|
||||||
catch_any_panic(|| {
|
debug!("mouse_move");
|
||||||
debug!("mouse_move");
|
call(|s| s.mouse_move(x, y));
|
||||||
call(|s| s.mouse_move(x, y));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn mouse_down(x: f32, y: f32, button: CMouseButton) {
|
pub extern "C" fn mouse_down(x: f32, y: f32, button: CMouseButton) {
|
||||||
catch_any_panic(|| {
|
debug!("mouse_down");
|
||||||
debug!("mouse_down");
|
call(|s| s.mouse_down(x, y, button.convert()));
|
||||||
call(|s| s.mouse_down(x, y, button.convert()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn mouse_up(x: f32, y: f32, button: CMouseButton) {
|
pub extern "C" fn mouse_up(x: f32, y: f32, button: CMouseButton) {
|
||||||
catch_any_panic(|| {
|
debug!("mouse_up");
|
||||||
debug!("mouse_up");
|
call(|s| s.mouse_up(x, y, button.convert()));
|
||||||
call(|s| s.mouse_up(x, y, button.convert()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn click(x: f32, y: f32) {
|
pub extern "C" fn click(x: f32, y: f32) {
|
||||||
catch_any_panic(|| {
|
debug!("click");
|
||||||
debug!("click");
|
call(|s| s.click(x, y));
|
||||||
call(|s| s.click(x, y));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -738,46 +694,38 @@ pub extern "C" fn key_up(name: *const c_char) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn key_event(name: *const c_char, up: bool) {
|
fn key_event(name: *const c_char, up: bool) {
|
||||||
catch_any_panic(|| {
|
let name = unsafe { CStr::from_ptr(name) };
|
||||||
let name = unsafe { CStr::from_ptr(name) };
|
let name = match name.to_str() {
|
||||||
let name = match name.to_str() {
|
Ok(name) => name,
|
||||||
Ok(name) => name,
|
Err(..) => {
|
||||||
Err(..) => {
|
warn!("Couldn't not read str");
|
||||||
warn!("Couldn't not read str");
|
return;
|
||||||
return;
|
},
|
||||||
},
|
};
|
||||||
};
|
let key = Key::from_str(&name);
|
||||||
let key = Key::from_str(&name);
|
if let Ok(key) = key {
|
||||||
if let Ok(key) = key {
|
call(|s| if up { s.key_up(key) } else { s.key_down(key) });
|
||||||
call(|s| if up { s.key_up(key) } else { s.key_down(key) });
|
} else {
|
||||||
} else {
|
warn!("Received unknown keys");
|
||||||
warn!("Received unknown keys");
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn media_session_action(action: CMediaSessionActionType) {
|
pub extern "C" fn media_session_action(action: CMediaSessionActionType) {
|
||||||
catch_any_panic(|| {
|
debug!("media_session_action");
|
||||||
debug!("media_session_action");
|
call(|s| s.media_session_action(action.convert()));
|
||||||
call(|s| s.media_session_action(action.convert()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn change_visibility(visible: bool) {
|
pub extern "C" fn change_visibility(visible: bool) {
|
||||||
catch_any_panic(|| {
|
debug!("change_visibility");
|
||||||
debug!("change_visibility");
|
call(|s| s.change_visibility(visible));
|
||||||
call(|s| s.change_visibility(visible));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn ime_dismissed() {
|
pub extern "C" fn ime_dismissed() {
|
||||||
catch_any_panic(|| {
|
debug!("ime_dismissed");
|
||||||
debug!("ime_dismissed");
|
call(|s| s.ime_dismissed());
|
||||||
call(|s| s.ime_dismissed());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WakeupCallback(extern "C" fn());
|
pub struct WakeupCallback(extern "C" fn());
|
||||||
|
@ -952,6 +900,10 @@ impl HostTrait for HostCallbacks {
|
||||||
Some(contents_str.to_owned())
|
Some(contents_str.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_panic(&self, reason: String, details: Option<String>) {
|
||||||
|
report_panic(&reason, details);
|
||||||
|
}
|
||||||
|
|
||||||
fn on_devtools_started(&self, port: Result<u16, ()>, token: String) {
|
fn on_devtools_started(&self, port: Result<u16, ()>, token: String) {
|
||||||
let token = CString::new(token).expect("Can't create string");
|
let token = CString::new(token).expect("Can't create string");
|
||||||
match port {
|
match port {
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
//! retrieve an array (CPREFS) of struct of pointers (CPrefs) to the C-compatible preferences
|
//! retrieve an array (CPREFS) of struct of pointers (CPrefs) to the C-compatible preferences
|
||||||
//! (LocalCPref).
|
//! (LocalCPref).
|
||||||
|
|
||||||
use crate::catch_any_panic;
|
|
||||||
use crate::simpleservo::{self, PrefValue};
|
use crate::simpleservo::{self, PrefValue};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
@ -136,49 +135,41 @@ pub extern "C" fn get_pref_as_bool(ptr: *const c_void) -> *const bool {
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn reset_all_prefs() {
|
pub extern "C" fn reset_all_prefs() {
|
||||||
catch_any_panic(|| {
|
debug!("reset_all_prefs");
|
||||||
debug!("reset_all_prefs");
|
simpleservo::reset_all_prefs()
|
||||||
simpleservo::reset_all_prefs()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn reset_pref(key: *const c_char) -> bool {
|
pub extern "C" fn reset_pref(key: *const c_char) -> bool {
|
||||||
catch_any_panic(|| {
|
debug!("reset_pref");
|
||||||
debug!("reset_pref");
|
let key = unsafe { CStr::from_ptr(key) };
|
||||||
let key = unsafe { CStr::from_ptr(key) };
|
let key = key.to_str().expect("Can't read string");
|
||||||
let key = key.to_str().expect("Can't read string");
|
simpleservo::reset_pref(key)
|
||||||
simpleservo::reset_pref(key)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn get_pref(key: *const c_char) -> CPref {
|
pub extern "C" fn get_pref(key: *const c_char) -> CPref {
|
||||||
catch_any_panic(|| {
|
debug!("get_pref");
|
||||||
debug!("get_pref");
|
LOCALCPREFS.with(|localmap| {
|
||||||
LOCALCPREFS.with(|localmap| {
|
let key = unsafe { CStr::from_ptr(key) };
|
||||||
let key = unsafe { CStr::from_ptr(key) };
|
let key = key.to_str().expect("Can't read string");
|
||||||
let key = key.to_str().expect("Can't read string");
|
let (value, is_default) = simpleservo::get_pref(key);
|
||||||
let (value, is_default) = simpleservo::get_pref(key);
|
let local = LocalCPref {
|
||||||
let local = LocalCPref {
|
key: CString::new(key).unwrap(),
|
||||||
key: CString::new(key).unwrap(),
|
value: LocalCPrefValue::new(&value),
|
||||||
value: LocalCPrefValue::new(&value),
|
is_default: is_default,
|
||||||
is_default: is_default,
|
};
|
||||||
};
|
let cpref = CPref::new(&local);
|
||||||
let cpref = CPref::new(&local);
|
localmap.borrow_mut().insert(key.to_string(), local);
|
||||||
localmap.borrow_mut().insert(key.to_string(), local);
|
cpref
|
||||||
cpref
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pref(key: *const c_char, value: PrefValue) -> bool {
|
fn set_pref(key: *const c_char, value: PrefValue) -> bool {
|
||||||
catch_any_panic(|| {
|
debug!("set_pref");
|
||||||
debug!("set_pref");
|
let key = unsafe { CStr::from_ptr(key) };
|
||||||
let key = unsafe { CStr::from_ptr(key) };
|
let key = key.to_str().expect("Can't read string");
|
||||||
let key = key.to_str().expect("Can't read string");
|
simpleservo::set_pref(key, value).is_ok()
|
||||||
simpleservo::set_pref(key, value).is_ok()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -206,33 +197,31 @@ pub extern "C" fn set_str_pref(key: *const c_char, value: *const c_char) -> bool
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn get_prefs() -> CPrefList {
|
pub extern "C" fn get_prefs() -> CPrefList {
|
||||||
// Called from any thread
|
// Called from any thread
|
||||||
catch_any_panic(|| {
|
debug!("get_prefs");
|
||||||
debug!("get_prefs");
|
let map = simpleservo::get_prefs();
|
||||||
let map = simpleservo::get_prefs();
|
let local: BTreeMap<String, LocalCPref> = map
|
||||||
let local: BTreeMap<String, LocalCPref> = map
|
.into_iter()
|
||||||
.into_iter()
|
.map(|(key, (value, is_default))| {
|
||||||
.map(|(key, (value, is_default))| {
|
let l = LocalCPref {
|
||||||
let l = LocalCPref {
|
key: CString::new(key.clone()).unwrap(),
|
||||||
key: CString::new(key.clone()).unwrap(),
|
value: LocalCPrefValue::new(&value),
|
||||||
value: LocalCPrefValue::new(&value),
|
is_default: is_default,
|
||||||
is_default: is_default,
|
};
|
||||||
};
|
(key, l)
|
||||||
(key, l)
|
})
|
||||||
})
|
.collect();
|
||||||
.collect();
|
|
||||||
|
|
||||||
let ptrs: Vec<CPref> = local.iter().map(|(_, local)| CPref::new(&local)).collect();
|
let ptrs: Vec<CPref> = local.iter().map(|(_, local)| CPref::new(&local)).collect();
|
||||||
|
|
||||||
let list = CPrefList {
|
let list = CPrefList {
|
||||||
len: ptrs.len(),
|
len: ptrs.len(),
|
||||||
list: ptrs.as_ptr(),
|
list: ptrs.as_ptr(),
|
||||||
};
|
};
|
||||||
|
|
||||||
LOCALCPREFS.with(|p| *p.borrow_mut() = local);
|
LOCALCPREFS.with(|p| *p.borrow_mut() = local);
|
||||||
CPREFS.with(|p| *p.borrow_mut() = ptrs);
|
CPREFS.with(|p| *p.borrow_mut() = ptrs);
|
||||||
|
|
||||||
list
|
list
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn free_prefs() {
|
pub(crate) fn free_prefs() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue