Skip to main content

webkit6/auto/
favicon_database.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// from webkit-gir-files
4// DO NOT EDIT
5
6use crate::ffi;
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{SignalHandlerId, connect_raw},
11    translate::*,
12};
13use std::{boxed::Box as Box_, pin::Pin};
14
15glib::wrapper! {
16    /// Provides access to the icons associated with web sites.
17    ///
18    /// WebKit will automatically look for available icons in `<link>`
19    /// elements on opened pages as well as an existing favicon.ico and
20    /// load the images found into a memory cache if possible. That cache
21    /// is frozen to an on-disk database for persistence.
22    ///
23    /// If [`enable-private-browsing`][struct@crate::Settings#enable-private-browsing] is [`true`], new icons
24    /// won't be added to the on-disk database and no existing icons will
25    /// be deleted from it. Nevertheless, WebKit will still store them in
26    /// the in-memory cache during the current execution.
27    ///
28    /// ## Signals
29    ///
30    ///
31    /// #### `favicon-changed`
32    ///  This signal is emitted when the favicon URI of `page_uri` has
33    /// been changed to `favicon_uri` in the database. You can connect
34    /// to this signal and call [`FaviconDatabase::favicon()`][crate::FaviconDatabase::favicon()]
35    /// to get the favicon. If you are interested in the favicon of a
36    /// [`WebView`][crate::WebView] it's easier to use the [`favicon`][struct@crate::WebView#favicon]
37    /// property. See [`WebViewExt::favicon()`][crate::prelude::WebViewExt::favicon()] for more details.
38    ///
39    ///
40    #[doc(alias = "WebKitFaviconDatabase")]
41    pub struct FaviconDatabase(Object<ffi::WebKitFaviconDatabase, ffi::WebKitFaviconDatabaseClass>);
42
43    match fn {
44        type_ => || ffi::webkit_favicon_database_get_type(),
45    }
46}
47
48impl FaviconDatabase {
49    /// Clears all icons from the database.
50    #[doc(alias = "webkit_favicon_database_clear")]
51    pub fn clear(&self) {
52        unsafe {
53            ffi::webkit_favicon_database_clear(self.to_glib_none().0);
54        }
55    }
56
57    /// Asynchronously obtains a favicon image.
58    ///
59    /// Asynchronously obtains an image of the favicon for the
60    /// given page URI. It returns the cached icon if it's in the database
61    /// asynchronously waiting for the icon to be read from the database.
62    ///
63    /// This is an asynchronous method. When the operation is finished, callback will
64    /// be invoked. You can then call `webkit_favicon_database_get_favicon_finish()`
65    /// to get the result of the operation.
66    /// ## `page_uri`
67    /// URI of the page for which we want to retrieve the favicon
68    /// ## `cancellable`
69    /// A [`gio::Cancellable`][crate::gio::Cancellable] or [`None`].
70    /// ## `callback`
71    /// A `GAsyncReadyCallback` to call when the request is
72    ///  satisfied or [`None`] if you don't care about the result.
73    #[doc(alias = "webkit_favicon_database_get_favicon")]
74    #[doc(alias = "get_favicon")]
75    pub fn favicon<P: FnOnce(Result<gdk::Texture, glib::Error>) + 'static>(
76        &self,
77        page_uri: &str,
78        cancellable: Option<&impl IsA<gio::Cancellable>>,
79        callback: P,
80    ) {
81        let main_context = glib::MainContext::ref_thread_default();
82        let is_main_context_owner = main_context.is_owner();
83        let has_acquired_main_context = (!is_main_context_owner)
84            .then(|| main_context.acquire().ok())
85            .flatten();
86        assert!(
87            is_main_context_owner || has_acquired_main_context.is_some(),
88            "Async operations only allowed if the thread is owning the MainContext"
89        );
90
91        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
92            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
93        unsafe extern "C" fn favicon_trampoline<
94            P: FnOnce(Result<gdk::Texture, glib::Error>) + 'static,
95        >(
96            _source_object: *mut glib::gobject_ffi::GObject,
97            res: *mut gio::ffi::GAsyncResult,
98            user_data: glib::ffi::gpointer,
99        ) {
100            unsafe {
101                let mut error = std::ptr::null_mut();
102                let ret = ffi::webkit_favicon_database_get_favicon_finish(
103                    _source_object as *mut _,
104                    res,
105                    &mut error,
106                );
107                let result = if error.is_null() {
108                    Ok(from_glib_full(ret))
109                } else {
110                    Err(from_glib_full(error))
111                };
112                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
113                    Box_::from_raw(user_data as *mut _);
114                let callback: P = callback.into_inner();
115                callback(result);
116            }
117        }
118        let callback = favicon_trampoline::<P>;
119        unsafe {
120            ffi::webkit_favicon_database_get_favicon(
121                self.to_glib_none().0,
122                page_uri.to_glib_none().0,
123                cancellable.map(|p| p.as_ref()).to_glib_none().0,
124                Some(callback),
125                Box_::into_raw(user_data) as *mut _,
126            );
127        }
128    }
129
130    pub fn favicon_future(
131        &self,
132        page_uri: &str,
133    ) -> Pin<Box_<dyn std::future::Future<Output = Result<gdk::Texture, glib::Error>> + 'static>>
134    {
135        let page_uri = String::from(page_uri);
136        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
137            obj.favicon(&page_uri, Some(cancellable), move |res| {
138                send.resolve(res);
139            });
140        }))
141    }
142
143    /// Obtains the URI of the favicon for the given `page_uri`.
144    /// ## `page_uri`
145    /// URI of the page containing the icon
146    ///
147    /// # Returns
148    ///
149    /// a newly allocated URI for the favicon, or [`None`] if the
150    /// database doesn't have a favicon for `page_uri`.
151    #[doc(alias = "webkit_favicon_database_get_favicon_uri")]
152    #[doc(alias = "get_favicon_uri")]
153    pub fn favicon_uri(&self, page_uri: &str) -> Option<glib::GString> {
154        unsafe {
155            from_glib_full(ffi::webkit_favicon_database_get_favicon_uri(
156                self.to_glib_none().0,
157                page_uri.to_glib_none().0,
158            ))
159        }
160    }
161
162    /// This signal is emitted when the favicon URI of `page_uri` has
163    /// been changed to `favicon_uri` in the database. You can connect
164    /// to this signal and call [`favicon()`][Self::favicon()]
165    /// to get the favicon. If you are interested in the favicon of a
166    /// [`WebView`][crate::WebView] it's easier to use the [`favicon`][struct@crate::WebView#favicon]
167    /// property. See [`WebViewExt::favicon()`][crate::prelude::WebViewExt::favicon()] for more details.
168    /// ## `page_uri`
169    /// the URI of the Web page containing the icon
170    /// ## `favicon_uri`
171    /// the URI of the favicon
172    #[doc(alias = "favicon-changed")]
173    pub fn connect_favicon_changed<F: Fn(&Self, &str, &str) + 'static>(
174        &self,
175        f: F,
176    ) -> SignalHandlerId {
177        unsafe extern "C" fn favicon_changed_trampoline<
178            F: Fn(&FaviconDatabase, &str, &str) + 'static,
179        >(
180            this: *mut ffi::WebKitFaviconDatabase,
181            page_uri: *mut std::ffi::c_char,
182            favicon_uri: *mut std::ffi::c_char,
183            f: glib::ffi::gpointer,
184        ) {
185            unsafe {
186                let f: &F = &*(f as *const F);
187                f(
188                    &from_glib_borrow(this),
189                    &glib::GString::from_glib_borrow(page_uri),
190                    &glib::GString::from_glib_borrow(favicon_uri),
191                )
192            }
193        }
194        unsafe {
195            let f: Box_<F> = Box_::new(f);
196            connect_raw(
197                self.as_ptr() as *mut _,
198                c"favicon-changed".as_ptr(),
199                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
200                    favicon_changed_trampoline::<F> as *const (),
201                )),
202                Box_::into_raw(f),
203            )
204        }
205    }
206}