webkit_web_process_extension6/auto/
web_page.rs1#![allow(deprecated)]
6
7use crate::{
8 ConsoleMessage, ContextMenu, Frame, ScriptWorld, URIRequest, URIResponse, UserMessage,
9 WebEditor, WebFormManager, WebHitTestResult, ffi,
10};
11use glib::{
12 object::ObjectType as _,
13 prelude::*,
14 signal::{SignalHandlerId, connect_raw},
15 translate::*,
16};
17use std::{boxed::Box as Box_, pin::Pin};
18
19glib::wrapper! {
20 #[doc(alias = "WebKitWebPage")]
21 pub struct WebPage(Object<ffi::WebKitWebPage, ffi::WebKitWebPageClass>);
22
23 match fn {
24 type_ => || ffi::webkit_web_page_get_type(),
25 }
26}
27
28impl WebPage {
29 #[doc(alias = "webkit_web_page_get_editor")]
30 #[doc(alias = "get_editor")]
31 pub fn editor(&self) -> Option<WebEditor> {
32 unsafe { from_glib_none(ffi::webkit_web_page_get_editor(self.to_glib_none().0)) }
33 }
34
35 #[doc(alias = "webkit_web_page_get_form_manager")]
36 #[doc(alias = "get_form_manager")]
37 pub fn form_manager(&self, world: Option<&ScriptWorld>) -> Option<WebFormManager> {
38 unsafe {
39 from_glib_none(ffi::webkit_web_page_get_form_manager(
40 self.to_glib_none().0,
41 world.to_glib_none().0,
42 ))
43 }
44 }
45
46 #[doc(alias = "webkit_web_page_get_id")]
47 #[doc(alias = "get_id")]
48 pub fn id(&self) -> u64 {
49 unsafe { ffi::webkit_web_page_get_id(self.to_glib_none().0) }
50 }
51
52 #[cfg_attr(feature = "v2_48", deprecated = "Since 2.48")]
53 #[allow(deprecated)]
54 #[doc(alias = "webkit_web_page_get_main_frame")]
55 #[doc(alias = "get_main_frame")]
56 pub fn main_frame(&self) -> Option<Frame> {
57 unsafe { from_glib_none(ffi::webkit_web_page_get_main_frame(self.to_glib_none().0)) }
58 }
59
60 #[doc(alias = "webkit_web_page_get_uri")]
61 #[doc(alias = "get_uri")]
62 pub fn uri(&self) -> Option<glib::GString> {
63 unsafe { from_glib_none(ffi::webkit_web_page_get_uri(self.to_glib_none().0)) }
64 }
65
66 #[doc(alias = "webkit_web_page_send_message_to_view")]
67 pub fn send_message_to_view<P: FnOnce(Result<UserMessage, glib::Error>) + 'static>(
68 &self,
69 message: &UserMessage,
70 cancellable: Option<&impl IsA<gio::Cancellable>>,
71 callback: P,
72 ) {
73 let main_context = glib::MainContext::ref_thread_default();
74 let is_main_context_owner = main_context.is_owner();
75 let has_acquired_main_context = (!is_main_context_owner)
76 .then(|| main_context.acquire().ok())
77 .flatten();
78 assert!(
79 is_main_context_owner || has_acquired_main_context.is_some(),
80 "Async operations only allowed if the thread is owning the MainContext"
81 );
82
83 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
84 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
85 unsafe extern "C" fn send_message_to_view_trampoline<
86 P: FnOnce(Result<UserMessage, glib::Error>) + 'static,
87 >(
88 _source_object: *mut glib::gobject_ffi::GObject,
89 res: *mut gio::ffi::GAsyncResult,
90 user_data: glib::ffi::gpointer,
91 ) {
92 unsafe {
93 let mut error = std::ptr::null_mut();
94 let ret = ffi::webkit_web_page_send_message_to_view_finish(
95 _source_object as *mut _,
96 res,
97 &mut error,
98 );
99 let result = if error.is_null() {
100 Ok(from_glib_full(ret))
101 } else {
102 Err(from_glib_full(error))
103 };
104 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
105 Box_::from_raw(user_data as *mut _);
106 let callback: P = callback.into_inner();
107 callback(result);
108 }
109 }
110 let callback = send_message_to_view_trampoline::<P>;
111 unsafe {
112 ffi::webkit_web_page_send_message_to_view(
113 self.to_glib_none().0,
114 message.to_glib_none().0,
115 cancellable.map(|p| p.as_ref()).to_glib_none().0,
116 Some(callback),
117 Box_::into_raw(user_data) as *mut _,
118 );
119 }
120 }
121
122 pub fn send_message_to_view_future(
123 &self,
124 message: &UserMessage,
125 ) -> Pin<Box_<dyn std::future::Future<Output = Result<UserMessage, glib::Error>> + 'static>>
126 {
127 let message = message.clone();
128 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
129 obj.send_message_to_view(&message, Some(cancellable), move |res| {
130 send.resolve(res);
131 });
132 }))
133 }
134
135 #[doc(alias = "console-message-sent")]
136 pub fn connect_console_message_sent<F: Fn(&Self, &ConsoleMessage) + 'static>(
137 &self,
138 f: F,
139 ) -> SignalHandlerId {
140 unsafe extern "C" fn console_message_sent_trampoline<
141 F: Fn(&WebPage, &ConsoleMessage) + 'static,
142 >(
143 this: *mut ffi::WebKitWebPage,
144 console_message: *mut ffi::WebKitConsoleMessage,
145 f: glib::ffi::gpointer,
146 ) {
147 unsafe {
148 let f: &F = &*(f as *const F);
149 f(&from_glib_borrow(this), &from_glib_borrow(console_message))
150 }
151 }
152 unsafe {
153 let f: Box_<F> = Box_::new(f);
154 connect_raw(
155 self.as_ptr() as *mut _,
156 c"console-message-sent".as_ptr(),
157 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
158 console_message_sent_trampoline::<F> as *const (),
159 )),
160 Box_::into_raw(f),
161 )
162 }
163 }
164
165 #[doc(alias = "context-menu")]
166 pub fn connect_context_menu<F: Fn(&Self, &ContextMenu, &WebHitTestResult) -> bool + 'static>(
167 &self,
168 f: F,
169 ) -> SignalHandlerId {
170 unsafe extern "C" fn context_menu_trampoline<
171 F: Fn(&WebPage, &ContextMenu, &WebHitTestResult) -> bool + 'static,
172 >(
173 this: *mut ffi::WebKitWebPage,
174 context_menu: *mut ffi::WebKitContextMenu,
175 hit_test_result: *mut ffi::WebKitWebHitTestResult,
176 f: glib::ffi::gpointer,
177 ) -> glib::ffi::gboolean {
178 unsafe {
179 let f: &F = &*(f as *const F);
180 f(
181 &from_glib_borrow(this),
182 &from_glib_borrow(context_menu),
183 &from_glib_borrow(hit_test_result),
184 )
185 .into_glib()
186 }
187 }
188 unsafe {
189 let f: Box_<F> = Box_::new(f);
190 connect_raw(
191 self.as_ptr() as *mut _,
192 c"context-menu".as_ptr(),
193 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
194 context_menu_trampoline::<F> as *const (),
195 )),
196 Box_::into_raw(f),
197 )
198 }
199 }
200
201 #[doc(alias = "document-loaded")]
202 pub fn connect_document_loaded<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
203 unsafe extern "C" fn document_loaded_trampoline<F: Fn(&WebPage) + 'static>(
204 this: *mut ffi::WebKitWebPage,
205 f: glib::ffi::gpointer,
206 ) {
207 unsafe {
208 let f: &F = &*(f as *const F);
209 f(&from_glib_borrow(this))
210 }
211 }
212 unsafe {
213 let f: Box_<F> = Box_::new(f);
214 connect_raw(
215 self.as_ptr() as *mut _,
216 c"document-loaded".as_ptr(),
217 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
218 document_loaded_trampoline::<F> as *const (),
219 )),
220 Box_::into_raw(f),
221 )
222 }
223 }
224
225 #[doc(alias = "send-request")]
226 pub fn connect_send_request<F: Fn(&Self, &URIRequest, &URIResponse) -> bool + 'static>(
227 &self,
228 f: F,
229 ) -> SignalHandlerId {
230 unsafe extern "C" fn send_request_trampoline<
231 F: Fn(&WebPage, &URIRequest, &URIResponse) -> bool + 'static,
232 >(
233 this: *mut ffi::WebKitWebPage,
234 request: *mut ffi::WebKitURIRequest,
235 redirected_response: *mut ffi::WebKitURIResponse,
236 f: glib::ffi::gpointer,
237 ) -> glib::ffi::gboolean {
238 unsafe {
239 let f: &F = &*(f as *const F);
240 f(
241 &from_glib_borrow(this),
242 &from_glib_borrow(request),
243 &from_glib_borrow(redirected_response),
244 )
245 .into_glib()
246 }
247 }
248 unsafe {
249 let f: Box_<F> = Box_::new(f);
250 connect_raw(
251 self.as_ptr() as *mut _,
252 c"send-request".as_ptr(),
253 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
254 send_request_trampoline::<F> as *const (),
255 )),
256 Box_::into_raw(f),
257 )
258 }
259 }
260
261 #[doc(alias = "user-message-received")]
262 pub fn connect_user_message_received<F: Fn(&Self, &UserMessage) -> bool + 'static>(
263 &self,
264 f: F,
265 ) -> SignalHandlerId {
266 unsafe extern "C" fn user_message_received_trampoline<
267 F: Fn(&WebPage, &UserMessage) -> bool + 'static,
268 >(
269 this: *mut ffi::WebKitWebPage,
270 message: *mut ffi::WebKitUserMessage,
271 f: glib::ffi::gpointer,
272 ) -> glib::ffi::gboolean {
273 unsafe {
274 let f: &F = &*(f as *const F);
275 f(&from_glib_borrow(this), &from_glib_borrow(message)).into_glib()
276 }
277 }
278 unsafe {
279 let f: Box_<F> = Box_::new(f);
280 connect_raw(
281 self.as_ptr() as *mut _,
282 c"user-message-received".as_ptr(),
283 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
284 user_message_received_trampoline::<F> as *const (),
285 )),
286 Box_::into_raw(f),
287 )
288 }
289 }
290
291 #[doc(alias = "uri")]
292 pub fn connect_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
293 unsafe extern "C" fn notify_uri_trampoline<F: Fn(&WebPage) + 'static>(
294 this: *mut ffi::WebKitWebPage,
295 _param_spec: glib::ffi::gpointer,
296 f: glib::ffi::gpointer,
297 ) {
298 unsafe {
299 let f: &F = &*(f as *const F);
300 f(&from_glib_borrow(this))
301 }
302 }
303 unsafe {
304 let f: Box_<F> = Box_::new(f);
305 connect_raw(
306 self.as_ptr() as *mut _,
307 c"notify::uri".as_ptr(),
308 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
309 notify_uri_trampoline::<F> as *const (),
310 )),
311 Box_::into_raw(f),
312 )
313 }
314 }
315}