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
47/// Trait containing all [`struct@ProxyResolver`] methods.
48///
49/// # Implementors
50///
51/// [`ProxyResolver`][struct@crate::ProxyResolver], [`SimpleProxyResolver`][struct@crate::SimpleProxyResolver]
52pub trait ProxyResolverExt: IsA<ProxyResolver> + 'static {
53    /// Checks if @self can be used on this system. (This is used
54    /// internally; g_proxy_resolver_get_default() will only return a proxy
55    /// resolver that returns [`true`] for this method.)
56    ///
57    /// # Returns
58    ///
59    /// [`true`] if @self is supported.
60    #[doc(alias = "g_proxy_resolver_is_supported")]
61    fn is_supported(&self) -> bool {
62        unsafe {
63            from_glib(ffi::g_proxy_resolver_is_supported(
64                self.as_ref().to_glib_none().0,
65            ))
66        }
67    }
68
69    /// Looks into the system proxy configuration to determine what proxy,
70    /// if any, to use to connect to @uri. The returned proxy URIs are of
71    /// the form `<protocol>://[user[:password]@]host[:port]` or
72    /// `direct://`, where `<protocol>` could be http, rtsp, socks
73    /// or other proxying protocol.
74    ///
75    /// If you don't know what network protocol is being used on the
76    /// socket, you should use `none` as the URI protocol.
77    /// In this case, the resolver might still return a generic proxy type
78    /// (such as SOCKS), but would not return protocol-specific proxy types
79    /// (such as http).
80    ///
81    /// `direct://` is used when no proxy is needed.
82    /// Direct connection should not be attempted unless it is part of the
83    /// returned array of proxies.
84    /// ## `uri`
85    /// a URI representing the destination to connect to
86    /// ## `cancellable`
87    /// a #GCancellable, or [`None`]
88    ///
89    /// # Returns
90    ///
91    /// A
92    ///               NULL-terminated array of proxy URIs. Must be freed
93    ///               with g_strfreev().
94    #[doc(alias = "g_proxy_resolver_lookup")]
95    fn lookup(
96        &self,
97        uri: &str,
98        cancellable: Option<&impl IsA<Cancellable>>,
99    ) -> Result<Vec<glib::GString>, glib::Error> {
100        unsafe {
101            let mut error = std::ptr::null_mut();
102            let ret = ffi::g_proxy_resolver_lookup(
103                self.as_ref().to_glib_none().0,
104                uri.to_glib_none().0,
105                cancellable.map(|p| p.as_ref()).to_glib_none().0,
106                &mut error,
107            );
108            if error.is_null() {
109                Ok(FromGlibPtrContainer::from_glib_full(ret))
110            } else {
111                Err(from_glib_full(error))
112            }
113        }
114    }
115
116    /// Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
117    /// details.
118    /// ## `uri`
119    /// a URI representing the destination to connect to
120    /// ## `cancellable`
121    /// a #GCancellable, or [`None`]
122    /// ## `callback`
123    /// callback to call after resolution completes
124    #[doc(alias = "g_proxy_resolver_lookup_async")]
125    fn lookup_async<P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static>(
126        &self,
127        uri: &str,
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 lookup_async_trampoline<
144            P: FnOnce(Result<Vec<glib::GString>, 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 =
152                ffi::g_proxy_resolver_lookup_finish(_source_object as *mut _, res, &mut error);
153            let result = if error.is_null() {
154                Ok(FromGlibPtrContainer::from_glib_full(ret))
155            } else {
156                Err(from_glib_full(error))
157            };
158            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
159                Box_::from_raw(user_data as *mut _);
160            let callback: P = callback.into_inner();
161            callback(result);
162        }
163        let callback = lookup_async_trampoline::<P>;
164        unsafe {
165            ffi::g_proxy_resolver_lookup_async(
166                self.as_ref().to_glib_none().0,
167                uri.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 lookup_future(
176        &self,
177        uri: &str,
178    ) -> Pin<
179        Box_<dyn std::future::Future<Output = Result<Vec<glib::GString>, glib::Error>> + 'static>,
180    > {
181        let uri = String::from(uri);
182        Box_::pin(crate::GioFuture::new(
183            self,
184            move |obj, cancellable, send| {
185                obj.lookup_async(&uri, Some(cancellable), move |res| {
186                    send.resolve(res);
187                });
188            },
189        ))
190    }
191}
192
193impl<O: IsA<ProxyResolver>> ProxyResolverExt for O {}