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::{AsyncResult, Cancellable, ffi};
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 /// ` could be http, rtsp, socks
70 /// or other proxying protocol.
71 ///
72 /// If you don't know what network protocol is being used on the
73 /// socket, you should use `none` as the URI protocol.
74 /// In this case, the resolver might still return a generic proxy type
75 /// (such as SOCKS), but would not return protocol-specific proxy types
76 /// (such as http).
77 ///
78 /// `direct://` is used when no proxy is needed.
79 /// Direct connection should not be attempted unless it is part of the
80 /// returned array of proxies.
81 /// ## `uri`
82 /// a URI representing the destination to connect to
83 /// ## `cancellable`
84 /// a #GCancellable, or [`None`]
85 ///
86 /// # Returns
87 ///
88 /// A
89 /// NULL-terminated array of proxy URIs. Must be freed
90 /// with g_strfreev().
91 #[doc(alias = "g_proxy_resolver_lookup")]
92 fn lookup(
93 &self,
94 uri: &str,
95 cancellable: Option<&impl IsA<Cancellable>>,
96 ) -> Result<Vec<glib::GString>, glib::Error> {
97 unsafe {
98 let mut error = std::ptr::null_mut();
99 let ret = ffi::g_proxy_resolver_lookup(
100 self.as_ref().to_glib_none().0,
101 uri.to_glib_none().0,
102 cancellable.map(|p| p.as_ref()).to_glib_none().0,
103 &mut error,
104 );
105 if error.is_null() {
106 Ok(FromGlibPtrContainer::from_glib_full(ret))
107 } else {
108 Err(from_glib_full(error))
109 }
110 }
111 }
112
113 /// Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
114 /// details.
115 /// ## `uri`
116 /// a URI representing the destination to connect to
117 /// ## `cancellable`
118 /// a #GCancellable, or [`None`]
119 /// ## `callback`
120 /// callback to call after resolution completes
121 #[doc(alias = "g_proxy_resolver_lookup_async")]
122 fn lookup_async<P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static>(
123 &self,
124 uri: &str,
125 cancellable: Option<&impl IsA<Cancellable>>,
126 callback: P,
127 ) {
128 let main_context = glib::MainContext::ref_thread_default();
129 let is_main_context_owner = main_context.is_owner();
130 let has_acquired_main_context = (!is_main_context_owner)
131 .then(|| main_context.acquire().ok())
132 .flatten();
133 assert!(
134 is_main_context_owner || has_acquired_main_context.is_some(),
135 "Async operations only allowed if the thread is owning the MainContext"
136 );
137
138 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
139 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
140 unsafe extern "C" fn lookup_async_trampoline<
141 P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static,
142 >(
143 _source_object: *mut glib::gobject_ffi::GObject,
144 res: *mut crate::ffi::GAsyncResult,
145 user_data: glib::ffi::gpointer,
146 ) {
147 unsafe {
148 let mut error = std::ptr::null_mut();
149 let ret =
150 ffi::g_proxy_resolver_lookup_finish(_source_object as *mut _, res, &mut error);
151 let result = if error.is_null() {
152 Ok(FromGlibPtrContainer::from_glib_full(ret))
153 } else {
154 Err(from_glib_full(error))
155 };
156 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
157 Box_::from_raw(user_data as *mut _);
158 let callback: P = callback.into_inner();
159 callback(result);
160 }
161 }
162 let callback = lookup_async_trampoline::<P>;
163 unsafe {
164 ffi::g_proxy_resolver_lookup_async(
165 self.as_ref().to_glib_none().0,
166 uri.to_glib_none().0,
167 cancellable.map(|p| p.as_ref()).to_glib_none().0,
168 Some(callback),
169 Box_::into_raw(user_data) as *mut _,
170 );
171 }
172 }
173
174 fn lookup_future(
175 &self,
176 uri: &str,
177 ) -> Pin<
178 Box_<dyn std::future::Future<Output = Result<Vec<glib::GString>, glib::Error>> + 'static>,
179 > {
180 let uri = String::from(uri);
181 Box_::pin(crate::GioFuture::new(
182 self,
183 move |obj, cancellable, send| {
184 obj.lookup_async(&uri, Some(cancellable), move |res| {
185 send.resolve(res);
186 });
187 },
188 ))
189 }
190}
191
192impl<O: IsA<ProxyResolver>> ProxyResolverExt for O {}