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::{AsyncResult, Cancellable, IOStream, ProxyAddress, ffi};
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
51/// Trait containing all [`struct@Proxy`] methods.
52///
53/// # Implementors
54///
55/// [`Proxy`][struct@crate::Proxy]
56pub trait ProxyExt: IsA<Proxy> + 'static {
57    /// Given @connection to communicate with a proxy (eg, a
58    /// #GSocketConnection that is connected to the proxy server), this
59    /// does the necessary handshake to connect to @proxy_address, and if
60    /// required, wraps the #GIOStream to handle proxy payload.
61    /// ## `connection`
62    /// a #GIOStream
63    /// ## `proxy_address`
64    /// a #GProxyAddress
65    /// ## `cancellable`
66    /// a #GCancellable
67    ///
68    /// # Returns
69    ///
70    /// a #GIOStream that will replace @connection. This might
71    ///               be the same as @connection, in which case a reference
72    ///               will be added.
73    #[doc(alias = "g_proxy_connect")]
74    fn connect(
75        &self,
76        connection: &impl IsA<IOStream>,
77        proxy_address: &impl IsA<ProxyAddress>,
78        cancellable: Option<&impl IsA<Cancellable>>,
79    ) -> Result<IOStream, glib::Error> {
80        unsafe {
81            let mut error = std::ptr::null_mut();
82            let ret = ffi::g_proxy_connect(
83                self.as_ref().to_glib_none().0,
84                connection.as_ref().to_glib_none().0,
85                proxy_address.as_ref().to_glib_none().0,
86                cancellable.map(|p| p.as_ref()).to_glib_none().0,
87                &mut error,
88            );
89            if error.is_null() {
90                Ok(from_glib_full(ret))
91            } else {
92                Err(from_glib_full(error))
93            }
94        }
95    }
96
97    /// Asynchronous version of g_proxy_connect().
98    /// ## `connection`
99    /// a #GIOStream
100    /// ## `proxy_address`
101    /// a #GProxyAddress
102    /// ## `cancellable`
103    /// a #GCancellable
104    /// ## `callback`
105    /// a #GAsyncReadyCallback
106    #[doc(alias = "g_proxy_connect_async")]
107    fn connect_async<P: FnOnce(Result<IOStream, glib::Error>) + 'static>(
108        &self,
109        connection: &impl IsA<IOStream>,
110        proxy_address: &impl IsA<ProxyAddress>,
111        cancellable: Option<&impl IsA<Cancellable>>,
112        callback: P,
113    ) {
114        let main_context = glib::MainContext::ref_thread_default();
115        let is_main_context_owner = main_context.is_owner();
116        let has_acquired_main_context = (!is_main_context_owner)
117            .then(|| main_context.acquire().ok())
118            .flatten();
119        assert!(
120            is_main_context_owner || has_acquired_main_context.is_some(),
121            "Async operations only allowed if the thread is owning the MainContext"
122        );
123
124        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
125            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
126        unsafe extern "C" fn connect_async_trampoline<
127            P: FnOnce(Result<IOStream, glib::Error>) + 'static,
128        >(
129            _source_object: *mut glib::gobject_ffi::GObject,
130            res: *mut crate::ffi::GAsyncResult,
131            user_data: glib::ffi::gpointer,
132        ) {
133            unsafe {
134                let mut error = std::ptr::null_mut();
135                let ret = ffi::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
136                let result = if error.is_null() {
137                    Ok(from_glib_full(ret))
138                } else {
139                    Err(from_glib_full(error))
140                };
141                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
142                    Box_::from_raw(user_data as *mut _);
143                let callback: P = callback.into_inner();
144                callback(result);
145            }
146        }
147        let callback = connect_async_trampoline::<P>;
148        unsafe {
149            ffi::g_proxy_connect_async(
150                self.as_ref().to_glib_none().0,
151                connection.as_ref().to_glib_none().0,
152                proxy_address.as_ref().to_glib_none().0,
153                cancellable.map(|p| p.as_ref()).to_glib_none().0,
154                Some(callback),
155                Box_::into_raw(user_data) as *mut _,
156            );
157        }
158    }
159
160    fn connect_future(
161        &self,
162        connection: &(impl IsA<IOStream> + Clone + 'static),
163        proxy_address: &(impl IsA<ProxyAddress> + Clone + 'static),
164    ) -> Pin<Box_<dyn std::future::Future<Output = Result<IOStream, glib::Error>> + 'static>> {
165        let connection = connection.clone();
166        let proxy_address = proxy_address.clone();
167        Box_::pin(crate::GioFuture::new(
168            self,
169            move |obj, cancellable, send| {
170                obj.connect_async(&connection, &proxy_address, Some(cancellable), move |res| {
171                    send.resolve(res);
172                });
173            },
174        ))
175    }
176
177    /// Some proxy protocols expect to be passed a hostname, which they
178    /// will resolve to an IP address themselves. Others, like SOCKS4, do
179    /// not allow this. This function will return [`false`] if @self is
180    /// implementing such a protocol. When [`false`] is returned, the caller
181    /// should resolve the destination hostname first, and then pass a
182    /// #GProxyAddress containing the stringified IP address to
183    /// g_proxy_connect() or g_proxy_connect_async().
184    ///
185    /// # Returns
186    ///
187    /// [`true`] if hostname resolution is supported.
188    #[doc(alias = "g_proxy_supports_hostname")]
189    fn supports_hostname(&self) -> bool {
190        unsafe {
191            from_glib(ffi::g_proxy_supports_hostname(
192                self.as_ref().to_glib_none().0,
193            ))
194        }
195    }
196}
197
198impl<O: IsA<Proxy>> ProxyExt for O {}