Skip to main content

ohos_web_binding/callback/
mod.rs

1use std::ffi::c_char;
2use std::ffi::c_void;
3use std::mem::ManuallyDrop;
4
5pub(crate) struct WebComponentLifeCycleContext {
6    pub(crate) callback: Box<dyn FnMut()>,
7}
8
9pub(crate) type OnControllerAttachContext = WebComponentLifeCycleContext;
10pub(crate) type OnPageBeginContext = WebComponentLifeCycleContext;
11pub(crate) type OnPageEndContext = WebComponentLifeCycleContext;
12pub(crate) type OnDestroyContext = WebComponentLifeCycleContext;
13
14/// # Safety
15pub unsafe extern "C" fn on_controller_attach(_web_tag: *const c_char, user_data: *mut c_void) {
16    let mut ctx =
17        unsafe { ManuallyDrop::new(Box::from_raw(user_data as *mut OnControllerAttachContext)) };
18    let cb = &mut ctx.callback;
19    cb();
20}
21
22/// # Safety
23pub unsafe extern "C" fn on_page_begin(_web_tag: *const c_char, user_data: *mut c_void) {
24    let mut ctx = unsafe { ManuallyDrop::new(Box::from_raw(user_data as *mut OnPageBeginContext)) };
25    let cb = &mut ctx.callback;
26    cb();
27}
28
29/// # Safety
30pub unsafe extern "C" fn on_page_end(_web_tag: *const c_char, user_data: *mut c_void) {
31    let mut ctx = unsafe { ManuallyDrop::new(Box::from_raw(user_data as *mut OnPageEndContext)) };
32    let cb = &mut ctx.callback;
33    cb();
34}
35
36/// # Safety
37pub unsafe extern "C" fn on_destroy(_web_tag: *const c_char, user_data: *mut c_void) {
38    let mut ctx = unsafe { ManuallyDrop::new(Box::from_raw(user_data as *mut OnDestroyContext)) };
39    let cb = &mut ctx.callback;
40    cb();
41}