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