gio/auto/
proxy_resolver.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::{ffi, AsyncResult, Cancellable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// `GProxyResolver` provides synchronous and asynchronous network proxy
11    /// resolution. `GProxyResolver` is used within [`SocketClient`][crate::SocketClient] through
12    /// the method [`SocketConnectableExt::proxy_enumerate()`][crate::prelude::SocketConnectableExt::proxy_enumerate()].
13    ///
14    /// Implementations of `GProxyResolver` based on
15    /// [libproxy](https://github.com/libproxy/libproxy) and GNOME settings can be
16    /// found in [glib-networking](https://gitlab.gnome.org/GNOME/glib-networking).
17    /// GIO comes with an implementation for use inside Flatpak portals.
18    ///
19    /// # Implements
20    ///
21    /// [`ProxyResolverExt`][trait@crate::prelude::ProxyResolverExt]
22    #[doc(alias = "GProxyResolver")]
23    pub struct ProxyResolver(Interface<ffi::GProxyResolver, ffi::GProxyResolverInterface>);
24
25    match fn {
26        type_ => || ffi::g_proxy_resolver_get_type(),
27    }
28}
29
30impl ProxyResolver {
31    pub const NONE: Option<&'static ProxyResolver> = None;
32
33    /// Gets the default #GProxyResolver for the system.
34    ///
35    /// # Returns
36    ///
37    /// the default #GProxyResolver, which
38    ///     will be a dummy object if no proxy resolver is available
39    #[doc(alias = "g_proxy_resolver_get_default")]
40    #[doc(alias = "get_default")]
41    #[allow(clippy::should_implement_trait)]
42    pub fn default() -> ProxyResolver {
43        unsafe { from_glib_none(ffi::g_proxy_resolver_get_default()) }
44    }
45}
46
47mod sealed {
48    pub trait Sealed {}
49    impl<T: super::IsA<super::ProxyResolver>> Sealed for T {}
50}
51
52/// Trait containing all [`struct@ProxyResolver`] methods.
53///
54/// # Implementors
55///
56/// [`ProxyResolver`][struct@crate::ProxyResolver], [`SimpleProxyResolver`][struct@crate::SimpleProxyResolver]
57pub trait ProxyResolverExt: IsA<ProxyResolver> + sealed::Sealed + 'static {
58    /// Checks if @self can be used on this system. (This is used
59    /// internally; g_proxy_resolver_get_default() will only return a proxy
60    /// resolver that returns [`true`] for this method.)
61    ///
62    /// # Returns
63    ///
64    /// [`true`] if @self is supported.
65    #[doc(alias = "g_proxy_resolver_is_supported")]
66    fn is_supported(&self) -> bool {
67        unsafe {
68            from_glib(ffi::g_proxy_resolver_is_supported(
69                self.as_ref().to_glib_none().0,
70            ))
71        }
72    }
73
74    /// Looks into the system proxy configuration to determine what proxy,
75    /// if any, to use to connect to @uri. The returned proxy URIs are of
76    /// the form `<protocol>://[user[:password]@]host[:port]` or
77    /// `direct://`, where `<protocol>` could be http, rtsp, socks
78    /// or other proxying protocol.
79    ///
80    /// If you don't know what network protocol is being used on the
81    /// socket, you should use `none` as the URI protocol.
82    /// In this case, the resolver might still return a generic proxy type
83    /// (such as SOCKS), but would not return protocol-specific proxy types
84    /// (such as http).
85    ///
86    /// `direct://` is used when no proxy is needed.
87    /// Direct connection should not be attempted unless it is part of the
88    /// returned array of proxies.
89    /// ## `uri`
90    /// a URI representing the destination to connect to
91    /// ## `cancellable`
92    /// a #GCancellable, or [`None`]
93    ///
94    /// # Returns
95    ///
96    /// A
97    ///               NULL-terminated array of proxy URIs. Must be freed
98    ///               with g_strfreev().
99    #[doc(alias = "g_proxy_resolver_lookup")]
100    fn lookup(
101        &self,
102        uri: &str,
103        cancellable: Option<&impl IsA<Cancellable>>,
104    ) -> Result<Vec<glib::GString>, glib::Error> {
105        unsafe {
106            let mut error = std::ptr::null_mut();
107            let ret = ffi::g_proxy_resolver_lookup(
108                self.as_ref().to_glib_none().0,
109                uri.to_glib_none().0,
110                cancellable.map(|p| p.as_ref()).to_glib_none().0,
111                &mut error,
112            );
113            if error.is_null() {
114                Ok(FromGlibPtrContainer::from_glib_full(ret))
115            } else {
116                Err(from_glib_full(error))
117            }
118        }
119    }
120
121    /// Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
122    /// details.
123    /// ## `uri`
124    /// a URI representing the destination to connect to
125    /// ## `cancellable`
126    /// a #GCancellable, or [`None`]
127    /// ## `callback`
128    /// callback to call after resolution completes
129    #[doc(alias = "g_proxy_resolver_lookup_async")]
130    fn lookup_async<P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static>(
131        &self,
132        uri: &str,
133        cancellable: Option<&impl IsA<Cancellable>>,
134        callback: P,
135    ) {
136        let main_context = glib::MainContext::ref_thread_default();
137        let is_main_context_owner = main_context.is_owner();
138        let has_acquired_main_context = (!is_main_context_owner)
139            .then(|| main_context.acquire().ok())
140            .flatten();
141        assert!(
142            is_main_context_owner || has_acquired_main_context.is_some(),
143            "Async operations only allowed if the thread is owning the MainContext"
144        );
145
146        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
147            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
148        unsafe extern "C" fn lookup_async_trampoline<
149            P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static,
150        >(
151            _source_object: *mut glib::gobject_ffi::GObject,
152            res: *mut crate::ffi::GAsyncResult,
153            user_data: glib::ffi::gpointer,
154        ) {
155            let mut error = std::ptr::null_mut();
156            let ret =
157                ffi::g_proxy_resolver_lookup_finish(_source_object as *mut _, res, &mut error);
158            let result = if error.is_null() {
159                Ok(FromGlibPtrContainer::from_glib_full(ret))
160            } else {
161                Err(from_glib_full(error))
162            };
163            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
164                Box_::from_raw(user_data as *mut _);
165            let callback: P = callback.into_inner();
166            callback(result);
167        }
168        let callback = lookup_async_trampoline::<P>;
169        unsafe {
170            ffi::g_proxy_resolver_lookup_async(
171                self.as_ref().to_glib_none().0,
172                uri.to_glib_none().0,
173                cancellable.map(|p| p.as_ref()).to_glib_none().0,
174                Some(callback),
175                Box_::into_raw(user_data) as *mut _,
176            );
177        }
178    }
179
180    fn lookup_future(
181        &self,
182        uri: &str,
183    ) -> Pin<
184        Box_<dyn std::future::Future<Output = Result<Vec<glib::GString>, glib::Error>> + 'static>,
185    > {
186        let uri = String::from(uri);
187        Box_::pin(crate::GioFuture::new(
188            self,
189            move |obj, cancellable, send| {
190                obj.lookup_async(&uri, Some(cancellable), move |res| {
191                    send.resolve(res);
192                });
193            },
194        ))
195    }
196}
197
198impl<O: IsA<ProxyResolver>> ProxyResolverExt for O {}