Skip to main content

webkit_web_process_extension6/auto/
web_process_extension.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from webkit-gir-files
4// DO NOT EDIT
5
6use crate::{UserMessage, WebPage, ffi};
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{SignalHandlerId, connect_raw},
11    translate::*,
12};
13use std::{boxed::Box as Box_, pin::Pin};
14
15glib::wrapper! {
16    #[doc(alias = "WebKitWebProcessExtension")]
17    pub struct WebProcessExtension(Object<ffi::WebKitWebProcessExtension, ffi::WebKitWebProcessExtensionClass>);
18
19    match fn {
20        type_ => || ffi::webkit_web_process_extension_get_type(),
21    }
22}
23
24impl WebProcessExtension {
25    #[doc(alias = "webkit_web_process_extension_get_page")]
26    #[doc(alias = "get_page")]
27    pub fn page(&self, page_id: u64) -> Option<WebPage> {
28        unsafe {
29            from_glib_none(ffi::webkit_web_process_extension_get_page(
30                self.to_glib_none().0,
31                page_id,
32            ))
33        }
34    }
35
36    #[doc(alias = "webkit_web_process_extension_send_message_to_context")]
37    pub fn send_message_to_context<P: FnOnce(Result<UserMessage, glib::Error>) + 'static>(
38        &self,
39        message: &UserMessage,
40        cancellable: Option<&impl IsA<gio::Cancellable>>,
41        callback: P,
42    ) {
43        let main_context = glib::MainContext::ref_thread_default();
44        let is_main_context_owner = main_context.is_owner();
45        let has_acquired_main_context = (!is_main_context_owner)
46            .then(|| main_context.acquire().ok())
47            .flatten();
48        assert!(
49            is_main_context_owner || has_acquired_main_context.is_some(),
50            "Async operations only allowed if the thread is owning the MainContext"
51        );
52
53        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
54            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
55        unsafe extern "C" fn send_message_to_context_trampoline<
56            P: FnOnce(Result<UserMessage, glib::Error>) + 'static,
57        >(
58            _source_object: *mut glib::gobject_ffi::GObject,
59            res: *mut gio::ffi::GAsyncResult,
60            user_data: glib::ffi::gpointer,
61        ) {
62            unsafe {
63                let mut error = std::ptr::null_mut();
64                let ret = ffi::webkit_web_process_extension_send_message_to_context_finish(
65                    _source_object as *mut _,
66                    res,
67                    &mut error,
68                );
69                let result = if error.is_null() {
70                    Ok(from_glib_full(ret))
71                } else {
72                    Err(from_glib_full(error))
73                };
74                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
75                    Box_::from_raw(user_data as *mut _);
76                let callback: P = callback.into_inner();
77                callback(result);
78            }
79        }
80        let callback = send_message_to_context_trampoline::<P>;
81        unsafe {
82            ffi::webkit_web_process_extension_send_message_to_context(
83                self.to_glib_none().0,
84                message.to_glib_none().0,
85                cancellable.map(|p| p.as_ref()).to_glib_none().0,
86                Some(callback),
87                Box_::into_raw(user_data) as *mut _,
88            );
89        }
90    }
91
92    pub fn send_message_to_context_future(
93        &self,
94        message: &UserMessage,
95    ) -> Pin<Box_<dyn std::future::Future<Output = Result<UserMessage, glib::Error>> + 'static>>
96    {
97        let message = message.clone();
98        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
99            obj.send_message_to_context(&message, Some(cancellable), move |res| {
100                send.resolve(res);
101            });
102        }))
103    }
104
105    #[doc(alias = "page-created")]
106    pub fn connect_page_created<F: Fn(&Self, &WebPage) + 'static>(&self, f: F) -> SignalHandlerId {
107        unsafe extern "C" fn page_created_trampoline<
108            F: Fn(&WebProcessExtension, &WebPage) + 'static,
109        >(
110            this: *mut ffi::WebKitWebProcessExtension,
111            web_page: *mut ffi::WebKitWebPage,
112            f: glib::ffi::gpointer,
113        ) {
114            unsafe {
115                let f: &F = &*(f as *const F);
116                f(&from_glib_borrow(this), &from_glib_borrow(web_page))
117            }
118        }
119        unsafe {
120            let f: Box_<F> = Box_::new(f);
121            connect_raw(
122                self.as_ptr() as *mut _,
123                c"page-created".as_ptr(),
124                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
125                    page_created_trampoline::<F> as *const (),
126                )),
127                Box_::into_raw(f),
128            )
129        }
130    }
131
132    #[doc(alias = "user-message-received")]
133    pub fn connect_user_message_received<F: Fn(&Self, &UserMessage) + 'static>(
134        &self,
135        f: F,
136    ) -> SignalHandlerId {
137        unsafe extern "C" fn user_message_received_trampoline<
138            F: Fn(&WebProcessExtension, &UserMessage) + 'static,
139        >(
140            this: *mut ffi::WebKitWebProcessExtension,
141            message: *mut ffi::WebKitUserMessage,
142            f: glib::ffi::gpointer,
143        ) {
144            unsafe {
145                let f: &F = &*(f as *const F);
146                f(&from_glib_borrow(this), &from_glib_borrow(message))
147            }
148        }
149        unsafe {
150            let f: Box_<F> = Box_::new(f);
151            connect_raw(
152                self.as_ptr() as *mut _,
153                c"user-message-received".as_ptr(),
154                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
155                    user_message_received_trampoline::<F> as *const (),
156                )),
157                Box_::into_raw(f),
158            )
159        }
160    }
161}