gio/auto/tls_interaction.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// DO NOT EDIT
4
5use crate::{
6 ffi, AsyncResult, Cancellable, TlsCertificateRequestFlags, TlsConnection, TlsInteractionResult,
7 TlsPassword,
8};
9use glib::{prelude::*, translate::*};
10use std::{boxed::Box as Box_, pin::Pin};
11
12glib::wrapper! {
13 /// `GTlsInteraction` provides a mechanism for the TLS connection and database
14 /// code to interact with the user. It can be used to ask the user for passwords.
15 ///
16 /// To use a `GTlsInteraction` with a TLS connection use
17 /// [`TlsConnectionExt::set_interaction()`][crate::prelude::TlsConnectionExt::set_interaction()].
18 ///
19 /// Callers should instantiate a derived class that implements the various
20 /// interaction methods to show the required dialogs.
21 ///
22 /// Callers should use the 'invoke' functions like
23 /// [`TlsInteractionExt::invoke_ask_password()`][crate::prelude::TlsInteractionExt::invoke_ask_password()] to run interaction methods.
24 /// These functions make sure that the interaction is invoked in the main loop
25 /// and not in the current thread, if the current thread is not running the
26 /// main loop.
27 ///
28 /// Derived classes can choose to implement whichever interactions methods they’d
29 /// like to support by overriding those virtual methods in their class
30 /// initialization function. Any interactions not implemented will return
31 /// `G_TLS_INTERACTION_UNHANDLED`. If a derived class implements an async method,
32 /// it must also implement the corresponding finish method.
33 ///
34 /// # Implements
35 ///
36 /// [`TlsInteractionExt`][trait@crate::prelude::TlsInteractionExt], [`trait@glib::ObjectExt`]
37 #[doc(alias = "GTlsInteraction")]
38 pub struct TlsInteraction(Object<ffi::GTlsInteraction, ffi::GTlsInteractionClass>);
39
40 match fn {
41 type_ => || ffi::g_tls_interaction_get_type(),
42 }
43}
44
45impl TlsInteraction {
46 pub const NONE: Option<&'static TlsInteraction> = None;
47}
48
49mod sealed {
50 pub trait Sealed {}
51 impl<T: super::IsA<super::TlsInteraction>> Sealed for T {}
52}
53
54/// Trait containing all [`struct@TlsInteraction`] methods.
55///
56/// # Implementors
57///
58/// [`TlsInteraction`][struct@crate::TlsInteraction]
59pub trait TlsInteractionExt: IsA<TlsInteraction> + sealed::Sealed + 'static {
60 /// Run synchronous interaction to ask the user for a password. In general,
61 /// g_tls_interaction_invoke_ask_password() should be used instead of this
62 /// function.
63 ///
64 /// Derived subclasses usually implement a password prompt, although they may
65 /// also choose to provide a password from elsewhere. The @password value will
66 /// be filled in and then @callback will be called. Alternatively the user may
67 /// abort this password request, which will usually abort the TLS connection.
68 ///
69 /// If the interaction is cancelled by the cancellation object, or by the
70 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
71 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
72 /// not support immediate cancellation.
73 /// ## `password`
74 /// a #GTlsPassword object
75 /// ## `cancellable`
76 /// an optional #GCancellable cancellation object
77 ///
78 /// # Returns
79 ///
80 /// The status of the ask password interaction.
81 #[doc(alias = "g_tls_interaction_ask_password")]
82 fn ask_password(
83 &self,
84 password: &impl IsA<TlsPassword>,
85 cancellable: Option<&impl IsA<Cancellable>>,
86 ) -> Result<TlsInteractionResult, glib::Error> {
87 unsafe {
88 let mut error = std::ptr::null_mut();
89 let ret = ffi::g_tls_interaction_ask_password(
90 self.as_ref().to_glib_none().0,
91 password.as_ref().to_glib_none().0,
92 cancellable.map(|p| p.as_ref()).to_glib_none().0,
93 &mut error,
94 );
95 if error.is_null() {
96 Ok(from_glib(ret))
97 } else {
98 Err(from_glib_full(error))
99 }
100 }
101 }
102
103 /// Run asynchronous interaction to ask the user for a password. In general,
104 /// g_tls_interaction_invoke_ask_password() should be used instead of this
105 /// function.
106 ///
107 /// Derived subclasses usually implement a password prompt, although they may
108 /// also choose to provide a password from elsewhere. The @password value will
109 /// be filled in and then @callback will be called. Alternatively the user may
110 /// abort this password request, which will usually abort the TLS connection.
111 ///
112 /// If the interaction is cancelled by the cancellation object, or by the
113 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
114 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
115 /// not support immediate cancellation.
116 ///
117 /// Certain implementations may not support immediate cancellation.
118 /// ## `password`
119 /// a #GTlsPassword object
120 /// ## `cancellable`
121 /// an optional #GCancellable cancellation object
122 /// ## `callback`
123 /// will be called when the interaction completes
124 #[doc(alias = "g_tls_interaction_ask_password_async")]
125 fn ask_password_async<P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static>(
126 &self,
127 password: &impl IsA<TlsPassword>,
128 cancellable: Option<&impl IsA<Cancellable>>,
129 callback: P,
130 ) {
131 let main_context = glib::MainContext::ref_thread_default();
132 let is_main_context_owner = main_context.is_owner();
133 let has_acquired_main_context = (!is_main_context_owner)
134 .then(|| main_context.acquire().ok())
135 .flatten();
136 assert!(
137 is_main_context_owner || has_acquired_main_context.is_some(),
138 "Async operations only allowed if the thread is owning the MainContext"
139 );
140
141 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
142 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
143 unsafe extern "C" fn ask_password_async_trampoline<
144 P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static,
145 >(
146 _source_object: *mut glib::gobject_ffi::GObject,
147 res: *mut crate::ffi::GAsyncResult,
148 user_data: glib::ffi::gpointer,
149 ) {
150 let mut error = std::ptr::null_mut();
151 let ret = ffi::g_tls_interaction_ask_password_finish(
152 _source_object as *mut _,
153 res,
154 &mut error,
155 );
156 let result = if error.is_null() {
157 Ok(from_glib(ret))
158 } else {
159 Err(from_glib_full(error))
160 };
161 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
162 Box_::from_raw(user_data as *mut _);
163 let callback: P = callback.into_inner();
164 callback(result);
165 }
166 let callback = ask_password_async_trampoline::<P>;
167 unsafe {
168 ffi::g_tls_interaction_ask_password_async(
169 self.as_ref().to_glib_none().0,
170 password.as_ref().to_glib_none().0,
171 cancellable.map(|p| p.as_ref()).to_glib_none().0,
172 Some(callback),
173 Box_::into_raw(user_data) as *mut _,
174 );
175 }
176 }
177
178 fn ask_password_future(
179 &self,
180 password: &(impl IsA<TlsPassword> + Clone + 'static),
181 ) -> Pin<
182 Box_<dyn std::future::Future<Output = Result<TlsInteractionResult, glib::Error>> + 'static>,
183 > {
184 let password = password.clone();
185 Box_::pin(crate::GioFuture::new(
186 self,
187 move |obj, cancellable, send| {
188 obj.ask_password_async(&password, Some(cancellable), move |res| {
189 send.resolve(res);
190 });
191 },
192 ))
193 }
194
195 /// Invoke the interaction to ask the user for a password. It invokes this
196 /// interaction in the main loop, specifically the #GMainContext returned by
197 /// g_main_context_get_thread_default() when the interaction is created. This
198 /// is called by called by #GTlsConnection or #GTlsDatabase to ask the user
199 /// for a password.
200 ///
201 /// Derived subclasses usually implement a password prompt, although they may
202 /// also choose to provide a password from elsewhere. The @password value will
203 /// be filled in and then @callback will be called. Alternatively the user may
204 /// abort this password request, which will usually abort the TLS connection.
205 ///
206 /// The implementation can either be a synchronous (eg: modal dialog) or an
207 /// asynchronous one (eg: modeless dialog). This function will take care of
208 /// calling which ever one correctly.
209 ///
210 /// If the interaction is cancelled by the cancellation object, or by the
211 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
212 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
213 /// not support immediate cancellation.
214 /// ## `password`
215 /// a #GTlsPassword object
216 /// ## `cancellable`
217 /// an optional #GCancellable cancellation object
218 ///
219 /// # Returns
220 ///
221 /// The status of the ask password interaction.
222 #[doc(alias = "g_tls_interaction_invoke_ask_password")]
223 fn invoke_ask_password(
224 &self,
225 password: &impl IsA<TlsPassword>,
226 cancellable: Option<&impl IsA<Cancellable>>,
227 ) -> Result<TlsInteractionResult, glib::Error> {
228 unsafe {
229 let mut error = std::ptr::null_mut();
230 let ret = ffi::g_tls_interaction_invoke_ask_password(
231 self.as_ref().to_glib_none().0,
232 password.as_ref().to_glib_none().0,
233 cancellable.map(|p| p.as_ref()).to_glib_none().0,
234 &mut error,
235 );
236 if error.is_null() {
237 Ok(from_glib(ret))
238 } else {
239 Err(from_glib_full(error))
240 }
241 }
242 }
243
244 /// Invoke the interaction to ask the user to choose a certificate to
245 /// use with the connection. It invokes this interaction in the main
246 /// loop, specifically the #GMainContext returned by
247 /// g_main_context_get_thread_default() when the interaction is
248 /// created. This is called by called by #GTlsConnection when the peer
249 /// requests a certificate during the handshake.
250 ///
251 /// Derived subclasses usually implement a certificate selector,
252 /// although they may also choose to provide a certificate from
253 /// elsewhere. Alternatively the user may abort this certificate
254 /// request, which may or may not abort the TLS connection.
255 ///
256 /// The implementation can either be a synchronous (eg: modal dialog) or an
257 /// asynchronous one (eg: modeless dialog). This function will take care of
258 /// calling which ever one correctly.
259 ///
260 /// If the interaction is cancelled by the cancellation object, or by the
261 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
262 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
263 /// not support immediate cancellation.
264 /// ## `connection`
265 /// a #GTlsConnection object
266 /// ## `flags`
267 /// flags providing more information about the request
268 /// ## `cancellable`
269 /// an optional #GCancellable cancellation object
270 ///
271 /// # Returns
272 ///
273 /// The status of the certificate request interaction.
274 #[doc(alias = "g_tls_interaction_invoke_request_certificate")]
275 fn invoke_request_certificate(
276 &self,
277 connection: &impl IsA<TlsConnection>,
278 flags: TlsCertificateRequestFlags,
279 cancellable: Option<&impl IsA<Cancellable>>,
280 ) -> Result<TlsInteractionResult, glib::Error> {
281 unsafe {
282 let mut error = std::ptr::null_mut();
283 let ret = ffi::g_tls_interaction_invoke_request_certificate(
284 self.as_ref().to_glib_none().0,
285 connection.as_ref().to_glib_none().0,
286 flags.into_glib(),
287 cancellable.map(|p| p.as_ref()).to_glib_none().0,
288 &mut error,
289 );
290 if error.is_null() {
291 Ok(from_glib(ret))
292 } else {
293 Err(from_glib_full(error))
294 }
295 }
296 }
297
298 /// Run synchronous interaction to ask the user to choose a certificate to use
299 /// with the connection. In general, g_tls_interaction_invoke_request_certificate()
300 /// should be used instead of this function.
301 ///
302 /// Derived subclasses usually implement a certificate selector, although they may
303 /// also choose to provide a certificate from elsewhere. Alternatively the user may
304 /// abort this certificate request, which will usually abort the TLS connection.
305 ///
306 /// If [`TlsInteractionResult::Handled`][crate::TlsInteractionResult::Handled] is returned, then the #GTlsConnection
307 /// passed to g_tls_interaction_request_certificate() will have had its
308 /// #GTlsConnection:certificate filled in.
309 ///
310 /// If the interaction is cancelled by the cancellation object, or by the
311 /// user then [`TlsInteractionResult::Failed`][crate::TlsInteractionResult::Failed] will be returned with an error that
312 /// contains a [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] error code. Certain implementations may
313 /// not support immediate cancellation.
314 /// ## `connection`
315 /// a #GTlsConnection object
316 /// ## `flags`
317 /// flags providing more information about the request
318 /// ## `cancellable`
319 /// an optional #GCancellable cancellation object
320 ///
321 /// # Returns
322 ///
323 /// The status of the request certificate interaction.
324 #[doc(alias = "g_tls_interaction_request_certificate")]
325 fn request_certificate(
326 &self,
327 connection: &impl IsA<TlsConnection>,
328 flags: TlsCertificateRequestFlags,
329 cancellable: Option<&impl IsA<Cancellable>>,
330 ) -> Result<TlsInteractionResult, glib::Error> {
331 unsafe {
332 let mut error = std::ptr::null_mut();
333 let ret = ffi::g_tls_interaction_request_certificate(
334 self.as_ref().to_glib_none().0,
335 connection.as_ref().to_glib_none().0,
336 flags.into_glib(),
337 cancellable.map(|p| p.as_ref()).to_glib_none().0,
338 &mut error,
339 );
340 if error.is_null() {
341 Ok(from_glib(ret))
342 } else {
343 Err(from_glib_full(error))
344 }
345 }
346 }
347
348 /// Run asynchronous interaction to ask the user for a certificate to use with
349 /// the connection. In general, g_tls_interaction_invoke_request_certificate() should
350 /// be used instead of this function.
351 ///
352 /// Derived subclasses usually implement a certificate selector, although they may
353 /// also choose to provide a certificate from elsewhere. @callback will be called
354 /// when the operation completes. Alternatively the user may abort this certificate
355 /// request, which will usually abort the TLS connection.
356 /// ## `connection`
357 /// a #GTlsConnection object
358 /// ## `flags`
359 /// flags providing more information about the request
360 /// ## `cancellable`
361 /// an optional #GCancellable cancellation object
362 /// ## `callback`
363 /// will be called when the interaction completes
364 #[doc(alias = "g_tls_interaction_request_certificate_async")]
365 fn request_certificate_async<P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static>(
366 &self,
367 connection: &impl IsA<TlsConnection>,
368 flags: TlsCertificateRequestFlags,
369 cancellable: Option<&impl IsA<Cancellable>>,
370 callback: P,
371 ) {
372 let main_context = glib::MainContext::ref_thread_default();
373 let is_main_context_owner = main_context.is_owner();
374 let has_acquired_main_context = (!is_main_context_owner)
375 .then(|| main_context.acquire().ok())
376 .flatten();
377 assert!(
378 is_main_context_owner || has_acquired_main_context.is_some(),
379 "Async operations only allowed if the thread is owning the MainContext"
380 );
381
382 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
383 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
384 unsafe extern "C" fn request_certificate_async_trampoline<
385 P: FnOnce(Result<TlsInteractionResult, glib::Error>) + 'static,
386 >(
387 _source_object: *mut glib::gobject_ffi::GObject,
388 res: *mut crate::ffi::GAsyncResult,
389 user_data: glib::ffi::gpointer,
390 ) {
391 let mut error = std::ptr::null_mut();
392 let ret = ffi::g_tls_interaction_request_certificate_finish(
393 _source_object as *mut _,
394 res,
395 &mut error,
396 );
397 let result = if error.is_null() {
398 Ok(from_glib(ret))
399 } else {
400 Err(from_glib_full(error))
401 };
402 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
403 Box_::from_raw(user_data as *mut _);
404 let callback: P = callback.into_inner();
405 callback(result);
406 }
407 let callback = request_certificate_async_trampoline::<P>;
408 unsafe {
409 ffi::g_tls_interaction_request_certificate_async(
410 self.as_ref().to_glib_none().0,
411 connection.as_ref().to_glib_none().0,
412 flags.into_glib(),
413 cancellable.map(|p| p.as_ref()).to_glib_none().0,
414 Some(callback),
415 Box_::into_raw(user_data) as *mut _,
416 );
417 }
418 }
419
420 fn request_certificate_future(
421 &self,
422 connection: &(impl IsA<TlsConnection> + Clone + 'static),
423 flags: TlsCertificateRequestFlags,
424 ) -> Pin<
425 Box_<dyn std::future::Future<Output = Result<TlsInteractionResult, glib::Error>> + 'static>,
426 > {
427 let connection = connection.clone();
428 Box_::pin(crate::GioFuture::new(
429 self,
430 move |obj, cancellable, send| {
431 obj.request_certificate_async(&connection, flags, Some(cancellable), move |res| {
432 send.resolve(res);
433 });
434 },
435 ))
436 }
437}
438
439impl<O: IsA<TlsInteraction>> TlsInteractionExt for O {}