1use crate::{ffi, Cookie, CookieJarAcceptPolicy, SessionFeature};
7use glib::{
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "SoupCookieJar")]
16 pub struct CookieJar(Object<ffi::SoupCookieJar, ffi::SoupCookieJarClass>) @implements SessionFeature;
17
18 match fn {
19 type_ => || ffi::soup_cookie_jar_get_type(),
20 }
21}
22
23impl CookieJar {
24 pub const NONE: Option<&'static CookieJar> = None;
25
26 #[doc(alias = "soup_cookie_jar_new")]
27 pub fn new() -> CookieJar {
28 assert_initialized_main_thread!();
29 unsafe { from_glib_full(ffi::soup_cookie_jar_new()) }
30 }
31
32 pub fn builder() -> CookieJarBuilder {
37 CookieJarBuilder::new()
38 }
39}
40
41impl Default for CookieJar {
42 fn default() -> Self {
43 Self::new()
44 }
45}
46
47#[must_use = "The builder must be built to be used"]
52pub struct CookieJarBuilder {
53 builder: glib::object::ObjectBuilder<'static, CookieJar>,
54}
55
56impl CookieJarBuilder {
57 fn new() -> Self {
58 Self {
59 builder: glib::object::Object::builder(),
60 }
61 }
62
63 pub fn accept_policy(self, accept_policy: CookieJarAcceptPolicy) -> Self {
64 Self {
65 builder: self.builder.property("accept-policy", accept_policy),
66 }
67 }
68
69 pub fn read_only(self, read_only: bool) -> Self {
70 Self {
71 builder: self.builder.property("read-only", read_only),
72 }
73 }
74
75 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
78 pub fn build(self) -> CookieJar {
79 self.builder.build()
80 }
81}
82
83mod sealed {
84 pub trait Sealed {}
85 impl<T: super::IsA<super::CookieJar>> Sealed for T {}
86}
87
88pub trait CookieJarExt: IsA<CookieJar> + sealed::Sealed + 'static {
89 #[doc(alias = "soup_cookie_jar_all_cookies")]
90 fn all_cookies(&self) -> Vec<Cookie> {
91 unsafe {
92 FromGlibPtrContainer::from_glib_full(ffi::soup_cookie_jar_all_cookies(
93 self.as_ref().to_glib_none().0,
94 ))
95 }
96 }
97
98 #[doc(alias = "soup_cookie_jar_get_accept_policy")]
99 #[doc(alias = "get_accept_policy")]
100 #[doc(alias = "accept-policy")]
101 fn accept_policy(&self) -> CookieJarAcceptPolicy {
102 unsafe {
103 from_glib(ffi::soup_cookie_jar_get_accept_policy(
104 self.as_ref().to_glib_none().0,
105 ))
106 }
107 }
108
109 #[doc(alias = "soup_cookie_jar_get_cookie_list")]
110 #[doc(alias = "get_cookie_list")]
111 fn cookie_list(&self, uri: &glib::Uri, for_http: bool) -> Vec<Cookie> {
112 unsafe {
113 FromGlibPtrContainer::from_glib_full(ffi::soup_cookie_jar_get_cookie_list(
114 self.as_ref().to_glib_none().0,
115 uri.to_glib_none().0,
116 for_http.into_glib(),
117 ))
118 }
119 }
120
121 #[doc(alias = "soup_cookie_jar_get_cookie_list_with_same_site_info")]
122 #[doc(alias = "get_cookie_list_with_same_site_info")]
123 fn cookie_list_with_same_site_info(
124 &self,
125 uri: &glib::Uri,
126 top_level: Option<&glib::Uri>,
127 site_for_cookies: Option<&glib::Uri>,
128 for_http: bool,
129 is_safe_method: bool,
130 is_top_level_navigation: bool,
131 ) -> Vec<Cookie> {
132 unsafe {
133 FromGlibPtrContainer::from_glib_full(
134 ffi::soup_cookie_jar_get_cookie_list_with_same_site_info(
135 self.as_ref().to_glib_none().0,
136 uri.to_glib_none().0,
137 top_level.to_glib_none().0,
138 site_for_cookies.to_glib_none().0,
139 for_http.into_glib(),
140 is_safe_method.into_glib(),
141 is_top_level_navigation.into_glib(),
142 ),
143 )
144 }
145 }
146
147 #[doc(alias = "soup_cookie_jar_get_cookies")]
148 #[doc(alias = "get_cookies")]
149 fn cookies(&self, uri: &glib::Uri, for_http: bool) -> Option<glib::GString> {
150 unsafe {
151 from_glib_full(ffi::soup_cookie_jar_get_cookies(
152 self.as_ref().to_glib_none().0,
153 uri.to_glib_none().0,
154 for_http.into_glib(),
155 ))
156 }
157 }
158
159 #[doc(alias = "soup_cookie_jar_is_persistent")]
160 fn is_persistent(&self) -> bool {
161 unsafe {
162 from_glib(ffi::soup_cookie_jar_is_persistent(
163 self.as_ref().to_glib_none().0,
164 ))
165 }
166 }
167
168 #[doc(alias = "soup_cookie_jar_set_accept_policy")]
169 #[doc(alias = "accept-policy")]
170 fn set_accept_policy(&self, policy: CookieJarAcceptPolicy) {
171 unsafe {
172 ffi::soup_cookie_jar_set_accept_policy(
173 self.as_ref().to_glib_none().0,
174 policy.into_glib(),
175 );
176 }
177 }
178
179 #[doc(alias = "soup_cookie_jar_set_cookie")]
180 fn set_cookie(&self, uri: &glib::Uri, cookie: &str) {
181 unsafe {
182 ffi::soup_cookie_jar_set_cookie(
183 self.as_ref().to_glib_none().0,
184 uri.to_glib_none().0,
185 cookie.to_glib_none().0,
186 );
187 }
188 }
189
190 #[doc(alias = "soup_cookie_jar_set_cookie_with_first_party")]
191 fn set_cookie_with_first_party(&self, uri: &glib::Uri, first_party: &glib::Uri, cookie: &str) {
192 unsafe {
193 ffi::soup_cookie_jar_set_cookie_with_first_party(
194 self.as_ref().to_glib_none().0,
195 uri.to_glib_none().0,
196 first_party.to_glib_none().0,
197 cookie.to_glib_none().0,
198 );
199 }
200 }
201
202 #[doc(alias = "read-only")]
203 fn is_read_only(&self) -> bool {
204 ObjectExt::property(self.as_ref(), "read-only")
205 }
206
207 #[doc(alias = "changed")]
208 fn connect_changed<F: Fn(&Self, &Cookie, &Cookie) + 'static>(&self, f: F) -> SignalHandlerId {
209 unsafe extern "C" fn changed_trampoline<
210 P: IsA<CookieJar>,
211 F: Fn(&P, &Cookie, &Cookie) + 'static,
212 >(
213 this: *mut ffi::SoupCookieJar,
214 old_cookie: *mut ffi::SoupCookie,
215 new_cookie: *mut ffi::SoupCookie,
216 f: glib::ffi::gpointer,
217 ) {
218 let f: &F = &*(f as *const F);
219 f(
220 CookieJar::from_glib_borrow(this).unsafe_cast_ref(),
221 &from_glib_borrow(old_cookie),
222 &from_glib_borrow(new_cookie),
223 )
224 }
225 unsafe {
226 let f: Box_<F> = Box_::new(f);
227 connect_raw(
228 self.as_ptr() as *mut _,
229 b"changed\0".as_ptr() as *const _,
230 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
231 changed_trampoline::<Self, F> as *const (),
232 )),
233 Box_::into_raw(f),
234 )
235 }
236 }
237
238 #[doc(alias = "accept-policy")]
239 fn connect_accept_policy_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
240 unsafe extern "C" fn notify_accept_policy_trampoline<
241 P: IsA<CookieJar>,
242 F: Fn(&P) + 'static,
243 >(
244 this: *mut ffi::SoupCookieJar,
245 _param_spec: glib::ffi::gpointer,
246 f: glib::ffi::gpointer,
247 ) {
248 let f: &F = &*(f as *const F);
249 f(CookieJar::from_glib_borrow(this).unsafe_cast_ref())
250 }
251 unsafe {
252 let f: Box_<F> = Box_::new(f);
253 connect_raw(
254 self.as_ptr() as *mut _,
255 b"notify::accept-policy\0".as_ptr() as *const _,
256 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
257 notify_accept_policy_trampoline::<Self, F> as *const (),
258 )),
259 Box_::into_raw(f),
260 )
261 }
262 }
263}
264
265impl<O: IsA<CookieJar>> CookieJarExt for O {}