gio/auto/
proxy.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, IOStream, ProxyAddress};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// A `GProxy` handles connecting to a remote host via a given type of
11    /// proxy server. It is implemented by the `gio-proxy` extension point.
12    /// The extensions are named after their proxy protocol name. As an
13    /// example, a SOCKS5 proxy implementation can be retrieved with the
14    /// name `socks5` using the function
15    /// [`IOExtensionPoint::extension_by_name()`][crate::IOExtensionPoint::extension_by_name()].
16    ///
17    /// # Implements
18    ///
19    /// [`ProxyExt`][trait@crate::prelude::ProxyExt]
20    #[doc(alias = "GProxy")]
21    pub struct Proxy(Interface<ffi::GProxy, ffi::GProxyInterface>);
22
23    match fn {
24        type_ => || ffi::g_proxy_get_type(),
25    }
26}
27
28impl Proxy {
29    pub const NONE: Option<&'static Proxy> = None;
30
31    /// Find the `gio-proxy` extension point for a proxy implementation that supports
32    /// the specified protocol.
33    /// ## `protocol`
34    /// the proxy protocol name (e.g. http, socks, etc)
35    ///
36    /// # Returns
37    ///
38    /// return a #GProxy or NULL if protocol
39    ///               is not supported.
40    #[doc(alias = "g_proxy_get_default_for_protocol")]
41    #[doc(alias = "get_default_for_protocol")]
42    pub fn default_for_protocol(protocol: &str) -> Option<Proxy> {
43        unsafe {
44            from_glib_full(ffi::g_proxy_get_default_for_protocol(
45                protocol.to_glib_none().0,
46            ))
47        }
48    }
49}
50
51mod sealed {
52    pub trait Sealed {}
53    impl<T: super::IsA<super::Proxy>> Sealed for T {}
54}
55
56/// Trait containing all [`struct@Proxy`] methods.
57///
58/// # Implementors
59///
60/// [`Proxy`][struct@crate::Proxy]
61pub trait ProxyExt: IsA<Proxy> + sealed::Sealed + 'static {
62    /// Given @connection to communicate with a proxy (eg, a
63    /// #GSocketConnection that is connected to the proxy server), this
64    /// does the necessary handshake to connect to @proxy_address, and if
65    /// required, wraps the #GIOStream to handle proxy payload.
66    /// ## `connection`
67    /// a #GIOStream
68    /// ## `proxy_address`
69    /// a #GProxyAddress
70    /// ## `cancellable`
71    /// a #GCancellable
72    ///
73    /// # Returns
74    ///
75    /// a #GIOStream that will replace @connection. This might
76    ///               be the same as @connection, in which case a reference
77    ///               will be added.
78    #[doc(alias = "g_proxy_connect")]
79    fn connect(
80        &self,
81        connection: &impl IsA<IOStream>,
82        proxy_address: &impl IsA<ProxyAddress>,
83        cancellable: Option<&impl IsA<Cancellable>>,
84    ) -> Result<IOStream, glib::Error> {
85        unsafe {
86            let mut error = std::ptr::null_mut();
87            let ret = ffi::g_proxy_connect(
88                self.as_ref().to_glib_none().0,
89                connection.as_ref().to_glib_none().0,
90                proxy_address.as_ref().to_glib_none().0,
91                cancellable.map(|p| p.as_ref()).to_glib_none().0,
92                &mut error,
93            );
94            if error.is_null() {
95                Ok(from_glib_full(ret))
96            } else {
97                Err(from_glib_full(error))
98            }
99        }
100    }
101
102    /// Asynchronous version of g_proxy_connect().
103    /// ## `connection`
104    /// a #GIOStream
105    /// ## `proxy_address`
106    /// a #GProxyAddress
107    /// ## `cancellable`
108    /// a #GCancellable
109    /// ## `callback`
110    /// a #GAsyncReadyCallback
111    #[doc(alias = "g_proxy_connect_async")]
112    fn connect_async<P: FnOnce(Result<IOStream, glib::Error>) + 'static>(
113        &self,
114        connection: &impl IsA<IOStream>,
115        proxy_address: &impl IsA<ProxyAddress>,
116        cancellable: Option<&impl IsA<Cancellable>>,
117        callback: P,
118    ) {
119        let main_context = glib::MainContext::ref_thread_default();
120        let is_main_context_owner = main_context.is_owner();
121        let has_acquired_main_context = (!is_main_context_owner)
122            .then(|| main_context.acquire().ok())
123            .flatten();
124        assert!(
125            is_main_context_owner || has_acquired_main_context.is_some(),
126            "Async operations only allowed if the thread is owning the MainContext"
127        );
128
129        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
130            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
131        unsafe extern "C" fn connect_async_trampoline<
132            P: FnOnce(Result<IOStream, glib::Error>) + 'static,
133        >(
134            _source_object: *mut glib::gobject_ffi::GObject,
135            res: *mut crate::ffi::GAsyncResult,
136            user_data: glib::ffi::gpointer,
137        ) {
138            let mut error = std::ptr::null_mut();
139            let ret = ffi::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
140            let result = if error.is_null() {
141                Ok(from_glib_full(ret))
142            } else {
143                Err(from_glib_full(error))
144            };
145            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
146                Box_::from_raw(user_data as *mut _);
147            let callback: P = callback.into_inner();
148            callback(result);
149        }
150        let callback = connect_async_trampoline::<P>;
151        unsafe {
152            ffi::g_proxy_connect_async(
153                self.as_ref().to_glib_none().0,
154                connection.as_ref().to_glib_none().0,
155                proxy_address.as_ref().to_glib_none().0,
156                cancellable.map(|p| p.as_ref()).to_glib_none().0,
157                Some(callback),
158                Box_::into_raw(user_data) as *mut _,
159            );
160        }
161    }
162
163    fn connect_future(
164        &self,
165        connection: &(impl IsA<IOStream> + Clone + 'static),
166        proxy_address: &(impl IsA<ProxyAddress> + Clone + 'static),
167    ) -> Pin<Box_<dyn std::future::Future<Output = Result<IOStream, glib::Error>> + 'static>> {
168        let connection = connection.clone();
169        let proxy_address = proxy_address.clone();
170        Box_::pin(crate::GioFuture::new(
171            self,
172            move |obj, cancellable, send| {
173                obj.connect_async(&connection, &proxy_address, Some(cancellable), move |res| {
174                    send.resolve(res);
175                });
176            },
177        ))
178    }
179
180    /// Some proxy protocols expect to be passed a hostname, which they
181    /// will resolve to an IP address themselves. Others, like SOCKS4, do
182    /// not allow this. This function will return [`false`] if @self is
183    /// implementing such a protocol. When [`false`] is returned, the caller
184    /// should resolve the destination hostname first, and then pass a
185    /// #GProxyAddress containing the stringified IP address to
186    /// g_proxy_connect() or g_proxy_connect_async().
187    ///
188    /// # Returns
189    ///
190    /// [`true`] if hostname resolution is supported.
191    #[doc(alias = "g_proxy_supports_hostname")]
192    fn supports_hostname(&self) -> bool {
193        unsafe {
194            from_glib(ffi::g_proxy_supports_hostname(
195                self.as_ref().to_glib_none().0,
196            ))
197        }
198    }
199}
200
201impl<O: IsA<Proxy>> ProxyExt for O {}