webkit6/auto/web_resource.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::{URIRequest, URIResponse, 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 /// Represents a resource at the end of a URI.
17 ///
18 /// A [`WebResource`][crate::WebResource] encapsulates content for each resource at the
19 /// end of a particular URI. For example, one [`WebResource`][crate::WebResource] will
20 /// be created for each separate image and stylesheet when a page is
21 /// loaded.
22 ///
23 /// You can access the response and the URI for a given
24 /// [`WebResource`][crate::WebResource], using [`uri()`][Self::uri()] and
25 /// [`response()`][Self::response()], as well as the raw data, using
26 /// [`data()`][Self::data()].
27 ///
28 /// ## Properties
29 ///
30 ///
31 /// #### `response`
32 /// The [`URIResponse`][crate::URIResponse] associated with this resource.
33 ///
34 /// Readable
35 ///
36 ///
37 /// #### `uri`
38 /// The current active URI of the [`WebResource`][crate::WebResource].
39 /// See [`WebResource::uri()`][crate::WebResource::uri()] for more details.
40 ///
41 /// Readable
42 ///
43 /// ## Signals
44 ///
45 ///
46 /// #### `failed`
47 /// This signal is emitted when an error occurs during the resource
48 /// load operation.
49 ///
50 ///
51 ///
52 ///
53 /// #### `failed-with-tls-errors`
54 /// This signal is emitted when a TLS error occurs during the resource load operation.
55 ///
56 ///
57 ///
58 ///
59 /// #### `finished`
60 /// This signal is emitted when the resource load finishes successfully
61 /// or due to an error. In case of errors [`failed`][struct@crate::WebResource#failed] signal
62 /// is emitted before this one.
63 ///
64 ///
65 ///
66 ///
67 /// #### `sent-request`
68 /// This signal is emitted when `request` has been sent to the
69 /// server. In case of a server redirection this signal is
70 /// emitted again with the `request` argument containing the new
71 /// request sent to the server due to the redirection and the
72 /// `redirected_response` parameter containing the response
73 /// received by the server for the initial request.
74 ///
75 ///
76 #[doc(alias = "WebKitWebResource")]
77 pub struct WebResource(Object<ffi::WebKitWebResource, ffi::WebKitWebResourceClass>);
78
79 match fn {
80 type_ => || ffi::webkit_web_resource_get_type(),
81 }
82}
83
84impl WebResource {
85 /// Asynchronously get the raw data for `self`.
86 ///
87 /// When the operation is finished, `callback` will be called. You can then call
88 /// `webkit_web_resource_get_data_finish()` to get the result of the operation.
89 /// ## `cancellable`
90 /// a [`gio::Cancellable`][crate::gio::Cancellable] or [`None`] to ignore
91 /// ## `callback`
92 /// a `GAsyncReadyCallback` to call when the request is satisfied
93 #[doc(alias = "webkit_web_resource_get_data")]
94 #[doc(alias = "get_data")]
95 pub fn data<P: FnOnce(Result<Vec<u8>, glib::Error>) + 'static>(
96 &self,
97 cancellable: Option<&impl IsA<gio::Cancellable>>,
98 callback: P,
99 ) {
100 let main_context = glib::MainContext::ref_thread_default();
101 let is_main_context_owner = main_context.is_owner();
102 let has_acquired_main_context = (!is_main_context_owner)
103 .then(|| main_context.acquire().ok())
104 .flatten();
105 assert!(
106 is_main_context_owner || has_acquired_main_context.is_some(),
107 "Async operations only allowed if the thread is owning the MainContext"
108 );
109
110 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
111 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
112 unsafe extern "C" fn data_trampoline<P: FnOnce(Result<Vec<u8>, glib::Error>) + 'static>(
113 _source_object: *mut glib::gobject_ffi::GObject,
114 res: *mut gio::ffi::GAsyncResult,
115 user_data: glib::ffi::gpointer,
116 ) {
117 unsafe {
118 let mut error = std::ptr::null_mut();
119 let mut length = std::mem::MaybeUninit::uninit();
120 let ret = ffi::webkit_web_resource_get_data_finish(
121 _source_object as *mut _,
122 res,
123 length.as_mut_ptr(),
124 &mut error,
125 );
126 let result = if error.is_null() {
127 Ok(FromGlibContainer::from_glib_full_num(
128 ret,
129 length.assume_init() as _,
130 ))
131 } else {
132 Err(from_glib_full(error))
133 };
134 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
135 Box_::from_raw(user_data as *mut _);
136 let callback: P = callback.into_inner();
137 callback(result);
138 }
139 }
140 let callback = data_trampoline::<P>;
141 unsafe {
142 ffi::webkit_web_resource_get_data(
143 self.to_glib_none().0,
144 cancellable.map(|p| p.as_ref()).to_glib_none().0,
145 Some(callback),
146 Box_::into_raw(user_data) as *mut _,
147 );
148 }
149 }
150
151 pub fn data_future(
152 &self,
153 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<u8>, glib::Error>> + 'static>> {
154 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
155 obj.data(Some(cancellable), move |res| {
156 send.resolve(res);
157 });
158 }))
159 }
160
161 /// Retrieves the [`URIResponse`][crate::URIResponse] of the resource load operation.
162 ///
163 /// This method returns [`None`] if called before the response
164 /// is received from the server. You can connect to notify::response
165 /// signal to be notified when the response is received.
166 ///
167 /// # Returns
168 ///
169 /// the [`URIResponse`][crate::URIResponse], or [`None`] if
170 /// the response hasn't been received yet.
171 #[doc(alias = "webkit_web_resource_get_response")]
172 #[doc(alias = "get_response")]
173 pub fn response(&self) -> Option<URIResponse> {
174 unsafe { from_glib_none(ffi::webkit_web_resource_get_response(self.to_glib_none().0)) }
175 }
176
177 /// Returns the current active URI of `self`.
178 ///
179 /// The active URI might change during
180 /// a load operation:
181 ///
182 /// `<orderedlist>`
183 /// `<listitem>``<para>`
184 /// When the resource load starts, the active URI is the requested URI
185 /// `</para>``</listitem>`
186 /// `<listitem>``<para>`
187 /// When the initial request is sent to the server, [`sent-request`][struct@crate::WebResource#sent-request]
188 /// signal is emitted without a redirected response, the active URI is the URI of
189 /// the request sent to the server.
190 /// `</para>``</listitem>`
191 /// `<listitem>``<para>`
192 /// In case of a server redirection, [`sent-request`][struct@crate::WebResource#sent-request] signal
193 /// is emitted again with a redirected response, the active URI is the URI the request
194 /// was redirected to.
195 /// `</para>``</listitem>`
196 /// `<listitem>``<para>`
197 /// When the response is received from the server, the active URI is the final
198 /// one and it will not change again.
199 /// `</para>``</listitem>`
200 /// `</orderedlist>`
201 ///
202 /// You can monitor the active URI by connecting to the notify::uri
203 /// signal of `self`.
204 ///
205 /// # Returns
206 ///
207 /// the current active URI of `self`
208 #[doc(alias = "webkit_web_resource_get_uri")]
209 #[doc(alias = "get_uri")]
210 pub fn uri(&self) -> Option<glib::GString> {
211 unsafe { from_glib_none(ffi::webkit_web_resource_get_uri(self.to_glib_none().0)) }
212 }
213
214 /// This signal is emitted when an error occurs during the resource
215 /// load operation.
216 /// ## `error`
217 /// the [`glib::Error`][crate::glib::Error] that was triggered
218 #[doc(alias = "failed")]
219 pub fn connect_failed<F: Fn(&Self, &glib::Error) + 'static>(&self, f: F) -> SignalHandlerId {
220 unsafe extern "C" fn failed_trampoline<F: Fn(&WebResource, &glib::Error) + 'static>(
221 this: *mut ffi::WebKitWebResource,
222 error: *mut glib::ffi::GError,
223 f: glib::ffi::gpointer,
224 ) {
225 unsafe {
226 let f: &F = &*(f as *const F);
227 f(&from_glib_borrow(this), &from_glib_borrow(error))
228 }
229 }
230 unsafe {
231 let f: Box_<F> = Box_::new(f);
232 connect_raw(
233 self.as_ptr() as *mut _,
234 c"failed".as_ptr(),
235 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
236 failed_trampoline::<F> as *const (),
237 )),
238 Box_::into_raw(f),
239 )
240 }
241 }
242
243 /// This signal is emitted when a TLS error occurs during the resource load operation.
244 /// ## `certificate`
245 /// a [`gio::TlsCertificate`][crate::gio::TlsCertificate]
246 /// ## `errors`
247 /// a [`gio::TlsCertificateFlags`][crate::gio::TlsCertificateFlags] with the verification status of `certificate`
248 #[doc(alias = "failed-with-tls-errors")]
249 pub fn connect_failed_with_tls_errors<
250 F: Fn(&Self, &gio::TlsCertificate, gio::TlsCertificateFlags) + 'static,
251 >(
252 &self,
253 f: F,
254 ) -> SignalHandlerId {
255 unsafe extern "C" fn failed_with_tls_errors_trampoline<
256 F: Fn(&WebResource, &gio::TlsCertificate, gio::TlsCertificateFlags) + 'static,
257 >(
258 this: *mut ffi::WebKitWebResource,
259 certificate: *mut gio::ffi::GTlsCertificate,
260 errors: gio::ffi::GTlsCertificateFlags,
261 f: glib::ffi::gpointer,
262 ) {
263 unsafe {
264 let f: &F = &*(f as *const F);
265 f(
266 &from_glib_borrow(this),
267 &from_glib_borrow(certificate),
268 from_glib(errors),
269 )
270 }
271 }
272 unsafe {
273 let f: Box_<F> = Box_::new(f);
274 connect_raw(
275 self.as_ptr() as *mut _,
276 c"failed-with-tls-errors".as_ptr(),
277 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
278 failed_with_tls_errors_trampoline::<F> as *const (),
279 )),
280 Box_::into_raw(f),
281 )
282 }
283 }
284
285 /// This signal is emitted when the resource load finishes successfully
286 /// or due to an error. In case of errors [`failed`][struct@crate::WebResource#failed] signal
287 /// is emitted before this one.
288 #[doc(alias = "finished")]
289 pub fn connect_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
290 unsafe extern "C" fn finished_trampoline<F: Fn(&WebResource) + 'static>(
291 this: *mut ffi::WebKitWebResource,
292 f: glib::ffi::gpointer,
293 ) {
294 unsafe {
295 let f: &F = &*(f as *const F);
296 f(&from_glib_borrow(this))
297 }
298 }
299 unsafe {
300 let f: Box_<F> = Box_::new(f);
301 connect_raw(
302 self.as_ptr() as *mut _,
303 c"finished".as_ptr(),
304 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
305 finished_trampoline::<F> as *const (),
306 )),
307 Box_::into_raw(f),
308 )
309 }
310 }
311
312 /// This signal is emitted when `request` has been sent to the
313 /// server. In case of a server redirection this signal is
314 /// emitted again with the `request` argument containing the new
315 /// request sent to the server due to the redirection and the
316 /// `redirected_response` parameter containing the response
317 /// received by the server for the initial request.
318 /// ## `request`
319 /// a [`URIRequest`][crate::URIRequest]
320 /// ## `redirected_response`
321 /// a [`URIResponse`][crate::URIResponse], or [`None`]
322 #[doc(alias = "sent-request")]
323 pub fn connect_sent_request<F: Fn(&Self, &URIRequest, &URIResponse) + 'static>(
324 &self,
325 f: F,
326 ) -> SignalHandlerId {
327 unsafe extern "C" fn sent_request_trampoline<
328 F: Fn(&WebResource, &URIRequest, &URIResponse) + 'static,
329 >(
330 this: *mut ffi::WebKitWebResource,
331 request: *mut ffi::WebKitURIRequest,
332 redirected_response: *mut ffi::WebKitURIResponse,
333 f: glib::ffi::gpointer,
334 ) {
335 unsafe {
336 let f: &F = &*(f as *const F);
337 f(
338 &from_glib_borrow(this),
339 &from_glib_borrow(request),
340 &from_glib_borrow(redirected_response),
341 )
342 }
343 }
344 unsafe {
345 let f: Box_<F> = Box_::new(f);
346 connect_raw(
347 self.as_ptr() as *mut _,
348 c"sent-request".as_ptr(),
349 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
350 sent_request_trampoline::<F> as *const (),
351 )),
352 Box_::into_raw(f),
353 )
354 }
355 }
356
357 #[doc(alias = "response")]
358 pub fn connect_response_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
359 unsafe extern "C" fn notify_response_trampoline<F: Fn(&WebResource) + 'static>(
360 this: *mut ffi::WebKitWebResource,
361 _param_spec: glib::ffi::gpointer,
362 f: glib::ffi::gpointer,
363 ) {
364 unsafe {
365 let f: &F = &*(f as *const F);
366 f(&from_glib_borrow(this))
367 }
368 }
369 unsafe {
370 let f: Box_<F> = Box_::new(f);
371 connect_raw(
372 self.as_ptr() as *mut _,
373 c"notify::response".as_ptr(),
374 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
375 notify_response_trampoline::<F> as *const (),
376 )),
377 Box_::into_raw(f),
378 )
379 }
380 }
381
382 #[doc(alias = "uri")]
383 pub fn connect_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
384 unsafe extern "C" fn notify_uri_trampoline<F: Fn(&WebResource) + 'static>(
385 this: *mut ffi::WebKitWebResource,
386 _param_spec: glib::ffi::gpointer,
387 f: glib::ffi::gpointer,
388 ) {
389 unsafe {
390 let f: &F = &*(f as *const F);
391 f(&from_glib_borrow(this))
392 }
393 }
394 unsafe {
395 let f: Box_<F> = Box_::new(f);
396 connect_raw(
397 self.as_ptr() as *mut _,
398 c"notify::uri".as_ptr(),
399 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
400 notify_uri_trampoline::<F> as *const (),
401 )),
402 Box_::into_raw(f),
403 )
404 }
405 }
406}