webkit-web-process-extension6 0.6.0

Rust bindings for WebKitWebProcessExtension 6
Documentation
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from webkit-gir-files
// DO NOT EDIT

use crate::{UserMessage, WebPage, ffi};
use glib::{
    object::ObjectType as _,
    prelude::*,
    signal::{SignalHandlerId, connect_raw},
    translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};

glib::wrapper! {
    #[doc(alias = "WebKitWebProcessExtension")]
    pub struct WebProcessExtension(Object<ffi::WebKitWebProcessExtension, ffi::WebKitWebProcessExtensionClass>);

    match fn {
        type_ => || ffi::webkit_web_process_extension_get_type(),
    }
}

impl WebProcessExtension {
    #[doc(alias = "webkit_web_process_extension_get_page")]
    #[doc(alias = "get_page")]
    pub fn page(&self, page_id: u64) -> Option<WebPage> {
        unsafe {
            from_glib_none(ffi::webkit_web_process_extension_get_page(
                self.to_glib_none().0,
                page_id,
            ))
        }
    }

    #[doc(alias = "webkit_web_process_extension_send_message_to_context")]
    pub fn send_message_to_context<P: FnOnce(Result<UserMessage, glib::Error>) + 'static>(
        &self,
        message: &UserMessage,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
        callback: P,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
        unsafe extern "C" fn send_message_to_context_trampoline<
            P: FnOnce(Result<UserMessage, glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut gio::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            unsafe {
                let mut error = std::ptr::null_mut();
                let ret = ffi::webkit_web_process_extension_send_message_to_context_finish(
                    _source_object as *mut _,
                    res,
                    &mut error,
                );
                let result = if error.is_null() {
                    Ok(from_glib_full(ret))
                } else {
                    Err(from_glib_full(error))
                };
                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
                    Box_::from_raw(user_data as *mut _);
                let callback: P = callback.into_inner();
                callback(result);
            }
        }
        let callback = send_message_to_context_trampoline::<P>;
        unsafe {
            ffi::webkit_web_process_extension_send_message_to_context(
                self.to_glib_none().0,
                message.to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    pub fn send_message_to_context_future(
        &self,
        message: &UserMessage,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UserMessage, glib::Error>> + 'static>>
    {
        let message = message.clone();
        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
            obj.send_message_to_context(&message, Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    #[doc(alias = "page-created")]
    pub fn connect_page_created<F: Fn(&Self, &WebPage) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn page_created_trampoline<
            F: Fn(&WebProcessExtension, &WebPage) + 'static,
        >(
            this: *mut ffi::WebKitWebProcessExtension,
            web_page: *mut ffi::WebKitWebPage,
            f: glib::ffi::gpointer,
        ) {
            unsafe {
                let f: &F = &*(f as *const F);
                f(&from_glib_borrow(this), &from_glib_borrow(web_page))
            }
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"page-created".as_ptr(),
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    page_created_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "user-message-received")]
    pub fn connect_user_message_received<F: Fn(&Self, &UserMessage) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn user_message_received_trampoline<
            F: Fn(&WebProcessExtension, &UserMessage) + 'static,
        >(
            this: *mut ffi::WebKitWebProcessExtension,
            message: *mut ffi::WebKitUserMessage,
            f: glib::ffi::gpointer,
        ) {
            unsafe {
                let f: &F = &*(f as *const F);
                f(&from_glib_borrow(this), &from_glib_borrow(message))
            }
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"user-message-received".as_ptr(),
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    user_message_received_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}