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