1 module webkit2.CookieManager;
2 
3 private import gio.AsyncResultIF;
4 private import gio.Cancellable;
5 private import glib.ErrorG;
6 private import glib.GException;
7 private import glib.ListG;
8 private import glib.Str;
9 private import gobject.ObjectG;
10 private import gobject.Signals;
11 private import soup.Cookie;
12 private import std.algorithm;
13 private import webkit2.c.functions;
14 public  import webkit2.c.types;
15 
16 
17 /**
18  * The WebKitCookieManager defines how to set up and handle cookies.
19  * You can get it from a #WebKitWebsiteDataManager with
20  * webkit_website_data_manager_get_cookie_manager(), and use it to set where to
21  * store cookies with webkit_cookie_manager_set_persistent_storage(),
22  * or to set the acceptance policy, with webkit_cookie_manager_get_accept_policy().
23  */
24 public class CookieManager : ObjectG
25 {
26 	/** the main Gtk struct */
27 	protected WebKitCookieManager* webKitCookieManager;
28 
29 	/** Get the main Gtk struct */
30 	public WebKitCookieManager* getCookieManagerStruct(bool transferOwnership = false)
31 	{
32 		if (transferOwnership)
33 			ownedRef = false;
34 		return webKitCookieManager;
35 	}
36 
37 	/** the main Gtk struct as a void* */
38 	protected override void* getStruct()
39 	{
40 		return cast(void*)webKitCookieManager;
41 	}
42 
43 	/**
44 	 * Sets our main struct and passes it to the parent class.
45 	 */
46 	public this (WebKitCookieManager* webKitCookieManager, bool ownedRef = false)
47 	{
48 		this.webKitCookieManager = webKitCookieManager;
49 		super(cast(GObject*)webKitCookieManager, ownedRef);
50 	}
51 
52 
53 	/** */
54 	public static GType getType()
55 	{
56 		return webkit_cookie_manager_get_type();
57 	}
58 
59 	/**
60 	 * Asynchronously add a #SoupCookie to the underlying storage.
61 	 *
62 	 * When the operation is finished, @callback will be called. You can then call
63 	 * webkit_cookie_manager_add_cookie_finish() to get the result of the operation.
64 	 *
65 	 * Params:
66 	 *     cookie = the #SoupCookie to be added
67 	 *     cancellable = a #GCancellable or %NULL to ignore
68 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
69 	 *     userData = the data to pass to callback function
70 	 *
71 	 * Since: 2.20
72 	 */
73 	public void addCookie(Cookie cookie, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
74 	{
75 		webkit_cookie_manager_add_cookie(webKitCookieManager, (cookie is null) ? null : cookie.getCookieStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
76 	}
77 
78 	/**
79 	 * Finish an asynchronous operation started with webkit_cookie_manager_add_cookie().
80 	 *
81 	 * Params:
82 	 *     result = a #GAsyncResult
83 	 *
84 	 * Returns: %TRUE if the cookie was added or %FALSE in case of error.
85 	 *
86 	 * Since: 2.20
87 	 *
88 	 * Throws: GException on failure.
89 	 */
90 	public bool addCookieFinish(AsyncResultIF result)
91 	{
92 		GError* err = null;
93 
94 		auto __p = webkit_cookie_manager_add_cookie_finish(webKitCookieManager, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
95 
96 		if (err !is null)
97 		{
98 			throw new GException( new ErrorG(err) );
99 		}
100 
101 		return __p;
102 	}
103 
104 	/**
105 	 * Delete all cookies of @cookie_manager
106 	 *
107 	 * Deprecated: Use webkit_website_data_manager_clear() instead.
108 	 */
109 	public void deleteAllCookies()
110 	{
111 		webkit_cookie_manager_delete_all_cookies(webKitCookieManager);
112 	}
113 
114 	/**
115 	 * Asynchronously delete a #SoupCookie from the current session.
116 	 *
117 	 * When the operation is finished, @callback will be called. You can then call
118 	 * webkit_cookie_manager_delete_cookie_finish() to get the result of the operation.
119 	 *
120 	 * Params:
121 	 *     cookie = the #SoupCookie to be deleted
122 	 *     cancellable = a #GCancellable or %NULL to ignore
123 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
124 	 *     userData = the data to pass to callback function
125 	 *
126 	 * Since: 2.20
127 	 */
128 	public void deleteCookie(Cookie cookie, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
129 	{
130 		webkit_cookie_manager_delete_cookie(webKitCookieManager, (cookie is null) ? null : cookie.getCookieStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
131 	}
132 
133 	/**
134 	 * Finish an asynchronous operation started with webkit_cookie_manager_delete_cookie().
135 	 *
136 	 * Params:
137 	 *     result = a #GAsyncResult
138 	 *
139 	 * Returns: %TRUE if the cookie was deleted or %FALSE in case of error.
140 	 *
141 	 * Since: 2.20
142 	 *
143 	 * Throws: GException on failure.
144 	 */
145 	public bool deleteCookieFinish(AsyncResultIF result)
146 	{
147 		GError* err = null;
148 
149 		auto __p = webkit_cookie_manager_delete_cookie_finish(webKitCookieManager, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
150 
151 		if (err !is null)
152 		{
153 			throw new GException( new ErrorG(err) );
154 		}
155 
156 		return __p;
157 	}
158 
159 	/**
160 	 * Remove all cookies of @cookie_manager for the given @domain.
161 	 *
162 	 * Deprecated: Use webkit_website_data_manager_remove() instead.
163 	 *
164 	 * Params:
165 	 *     domain = a domain name
166 	 */
167 	public void deleteCookiesForDomain(string domain)
168 	{
169 		webkit_cookie_manager_delete_cookies_for_domain(webKitCookieManager, Str.toStringz(domain));
170 	}
171 
172 	/**
173 	 * Asynchronously get the cookie acceptance policy of @cookie_manager.
174 	 * Note that when policy was set to %WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY and
175 	 * ITP is enabled, this will return %WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS.
176 	 * See also webkit_website_data_manager_set_itp_enabled().
177 	 *
178 	 * When the operation is finished, @callback will be called. You can then call
179 	 * webkit_cookie_manager_get_accept_policy_finish() to get the result of the operation.
180 	 *
181 	 * Params:
182 	 *     cancellable = a #GCancellable or %NULL to ignore
183 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
184 	 *     userData = the data to pass to callback function
185 	 */
186 	public void getAcceptPolicy(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
187 	{
188 		webkit_cookie_manager_get_accept_policy(webKitCookieManager, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
189 	}
190 
191 	/**
192 	 * Finish an asynchronous operation started with webkit_cookie_manager_get_accept_policy().
193 	 *
194 	 * Params:
195 	 *     result = a #GAsyncResult
196 	 *
197 	 * Returns: the cookie acceptance policy of @cookie_manager as a #WebKitCookieAcceptPolicy.
198 	 *
199 	 * Throws: GException on failure.
200 	 */
201 	public WebKitCookieAcceptPolicy getAcceptPolicyFinish(AsyncResultIF result)
202 	{
203 		GError* err = null;
204 
205 		auto __p = webkit_cookie_manager_get_accept_policy_finish(webKitCookieManager, (result is null) ? null : result.getAsyncResultStruct(), &err);
206 
207 		if (err !is null)
208 		{
209 			throw new GException( new ErrorG(err) );
210 		}
211 
212 		return __p;
213 	}
214 
215 	/**
216 	 * Asynchronously get a list of #SoupCookie from @cookie_manager associated with @uri, which
217 	 * must be either an HTTP or an HTTPS URL.
218 	 *
219 	 * When the operation is finished, @callback will be called. You can then call
220 	 * webkit_cookie_manager_get_cookies_finish() to get the result of the operation.
221 	 *
222 	 * Params:
223 	 *     uri = the URI associated to the cookies to be retrieved
224 	 *     cancellable = a #GCancellable or %NULL to ignore
225 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
226 	 *     userData = the data to pass to callback function
227 	 *
228 	 * Since: 2.20
229 	 */
230 	public void getCookies(string uri, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
231 	{
232 		webkit_cookie_manager_get_cookies(webKitCookieManager, Str.toStringz(uri), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
233 	}
234 
235 	/**
236 	 * Finish an asynchronous operation started with webkit_cookie_manager_get_cookies().
237 	 * The return value is a #GSList of #SoupCookie instances which should be released
238 	 * with g_list_free_full() and soup_cookie_free().
239 	 *
240 	 * Params:
241 	 *     result = a #GAsyncResult
242 	 *
243 	 * Returns: A #GList of #SoupCookie instances.
244 	 *
245 	 * Since: 2.20
246 	 *
247 	 * Throws: GException on failure.
248 	 */
249 	public ListG getCookiesFinish(AsyncResultIF result)
250 	{
251 		GError* err = null;
252 
253 		auto __p = webkit_cookie_manager_get_cookies_finish(webKitCookieManager, (result is null) ? null : result.getAsyncResultStruct(), &err);
254 
255 		if (err !is null)
256 		{
257 			throw new GException( new ErrorG(err) );
258 		}
259 
260 		if(__p is null)
261 		{
262 			return null;
263 		}
264 
265 		return new ListG(cast(GList*) __p, true);
266 	}
267 
268 	/**
269 	 * Asynchronously get the list of domains for which @cookie_manager contains cookies.
270 	 *
271 	 * When the operation is finished, @callback will be called. You can then call
272 	 * webkit_cookie_manager_get_domains_with_cookies_finish() to get the result of the operation.
273 	 *
274 	 * Deprecated: Use webkit_website_data_manager_fetch() instead.
275 	 *
276 	 * Params:
277 	 *     cancellable = a #GCancellable or %NULL to ignore
278 	 *     callback = a #GAsyncReadyCallback to call when the request is satisfied
279 	 *     userData = the data to pass to callback function
280 	 */
281 	public void getDomainsWithCookies(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
282 	{
283 		webkit_cookie_manager_get_domains_with_cookies(webKitCookieManager, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
284 	}
285 
286 	/**
287 	 * Finish an asynchronous operation started with webkit_cookie_manager_get_domains_with_cookies().
288 	 * The return value is a %NULL terminated list of strings which should
289 	 * be released with g_strfreev().
290 	 *
291 	 * Deprecated: Use webkit_website_data_manager_fetch_finish() instead.
292 	 *
293 	 * Params:
294 	 *     result = a #GAsyncResult
295 	 *
296 	 * Returns: A %NULL terminated array of domain names
297 	 *     or %NULL in case of error.
298 	 *
299 	 * Throws: GException on failure.
300 	 */
301 	public string[] getDomainsWithCookiesFinish(AsyncResultIF result)
302 	{
303 		GError* err = null;
304 
305 		auto retStr = webkit_cookie_manager_get_domains_with_cookies_finish(webKitCookieManager, (result is null) ? null : result.getAsyncResultStruct(), &err);
306 
307 		if (err !is null)
308 		{
309 			throw new GException( new ErrorG(err) );
310 		}
311 
312 		scope(exit) Str.freeStringArray(retStr);
313 		return Str.toStringArray(retStr);
314 	}
315 
316 	/**
317 	 * Set the cookie acceptance policy of @cookie_manager as @policy.
318 	 * Note that ITP has its own way to handle third-party cookies, so when it's enabled,
319 	 * and @policy is set to %WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY, %WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS
320 	 * will be used instead. Once disabled, the policy will be set back to %WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY.
321 	 * See also webkit_website_data_manager_set_itp_enabled().
322 	 *
323 	 * Params:
324 	 *     policy = a #WebKitCookieAcceptPolicy
325 	 */
326 	public void setAcceptPolicy(WebKitCookieAcceptPolicy policy)
327 	{
328 		webkit_cookie_manager_set_accept_policy(webKitCookieManager, policy);
329 	}
330 
331 	/**
332 	 * Set the @filename where non-session cookies are stored persistently using
333 	 * @storage as the format to read/write the cookies.
334 	 * Cookies are initially read from @filename to create an initial set of cookies.
335 	 * Then, non-session cookies will be written to @filename when the WebKitCookieManager::changed
336 	 * signal is emitted.
337 	 * By default, @cookie_manager doesn't store the cookies persistently, so you need to call this
338 	 * method to keep cookies saved across sessions.
339 	 *
340 	 * This method should never be called on a #WebKitCookieManager associated to an ephemeral #WebKitWebsiteDataManager.
341 	 *
342 	 * Params:
343 	 *     filename = the filename to read to/write from
344 	 *     storage = a #WebKitCookiePersistentStorage
345 	 */
346 	public void setPersistentStorage(string filename, WebKitCookiePersistentStorage storage)
347 	{
348 		webkit_cookie_manager_set_persistent_storage(webKitCookieManager, Str.toStringz(filename), storage);
349 	}
350 
351 	/**
352 	 * This signal is emitted when cookies are added, removed or modified.
353 	 */
354 	gulong addOnChanged(void delegate(CookieManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
355 	{
356 		return Signals.connect(this, "changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
357 	}
358 }