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
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            let mut error = std::ptr::null_mut();
134            let ret = ffi::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
135            let result = if error.is_null() {
136                Ok(from_glib_full(ret))
137            } else {
138                Err(from_glib_full(error))
139            };
140            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
141                Box_::from_raw(user_data as *mut _);
142            let callback: P = callback.into_inner();
143            callback(result);
144        }
145        let callback = connect_async_trampoline::<P>;
146        unsafe {
147            ffi::g_proxy_connect_async(
148                self.as_ref().to_glib_none().0,
149                connection.as_ref().to_glib_none().0,
150                proxy_address.as_ref().to_glib_none().0,
151                cancellable.map(|p| p.as_ref()).to_glib_none().0,
152                Some(callback),
153                Box_::into_raw(user_data) as *mut _,
154            );
155        }
156    }
157
158    fn connect_future(
159        &self,
160        connection: &(impl IsA<IOStream> + Clone + 'static),
161        proxy_address: &(impl IsA<ProxyAddress> + Clone + 'static),
162    ) -> Pin<Box_<dyn std::future::Future<Output = Result<IOStream, glib::Error>> + 'static>> {
163        let connection = connection.clone();
164        let proxy_address = proxy_address.clone();
165        Box_::pin(crate::GioFuture::new(
166            self,
167            move |obj, cancellable, send| {
168                obj.connect_async(&connection, &proxy_address, Some(cancellable), move |res| {
169                    send.resolve(res);
170                });
171            },
172        ))
173    }
174
175    /// Some proxy protocols expect to be passed a hostname, which they
176    /// will resolve to an IP address themselves. Others, like SOCKS4, do
177    /// not allow this. This function will return [`false`] if @self is
178    /// implementing such a protocol. When [`false`] is returned, the caller
179    /// should resolve the destination hostname first, and then pass a
180    /// #GProxyAddress containing the stringified IP address to
181    /// g_proxy_connect() or g_proxy_connect_async().
182    ///
183    /// # Returns
184    ///
185    /// [`true`] if hostname resolution is supported.
186    #[doc(alias = "g_proxy_supports_hostname")]
187    fn supports_hostname(&self) -> bool {
188        unsafe {
189            from_glib(ffi::g_proxy_supports_hostname(
190                self.as_ref().to_glib_none().0,
191            ))
192        }
193    }
194}
195
196impl<O: IsA<Proxy>> ProxyExt for O {}