2 Copyright (C) 2010 ProFUSION embedded systems
3 Copyright (C) 2010 Samsung Electronics
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "ewk_cookies.h"
25 #include "CookieJarSoup.h"
28 #include "ResourceHandle.h"
31 #include <eina_safety_checks.h>
34 #include <libsoup/soup.h>
36 #include <wtf/text/CString.h>
40 * Set the path where the cookies are going to be stored. Use NULL for keep
41 * them just in memory.
43 * @param filename path to the cookies.txt file.
45 * @returns EINA_FALSE if it wasn't possible to create the cookie jar,
46 * EINA_FALSE otherwise.
48 EAPI Eina_Bool ewk_cookies_file_set(const char *filename)
51 SoupCookieJar* cookieJar = 0;
53 cookieJar = soup_cookie_jar_text_new(filename, FALSE);
55 cookieJar = soup_cookie_jar_new();
60 soup_cookie_jar_set_accept_policy(cookieJar, SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY);
62 SoupSession* session = WebCore::ResourceHandle::defaultSession();
63 SoupSessionFeature* oldjar = soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR);
65 soup_session_remove_feature(session, oldjar);
67 WebCore::setDefaultCookieJar(cookieJar);
68 soup_session_add_feature(session, SOUP_SESSION_FEATURE(cookieJar));
77 * Clear all the cookies from the cookie jar.
79 EAPI void ewk_cookies_clear()
84 SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
86 l = soup_cookie_jar_all_cookies(cookieJar);
87 for (p = l; p; p = p->next)
88 soup_cookie_jar_delete_cookie(cookieJar, (SoupCookie*)p->data);
95 * Returns a list of cookies in the cookie jar.
97 * @returns an Eina_List with all the cookies in the cookie jar.
99 EAPI Eina_List* ewk_cookies_get_all(void)
105 SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
107 l = soup_cookie_jar_all_cookies(cookieJar);
108 for (p = l; p; p = p->next) {
109 SoupCookie* cookie = static_cast<SoupCookie*>(p->data);
110 Ewk_Cookie* c = static_cast<Ewk_Cookie*>(malloc(sizeof(*c)));
111 c->name = strdup(cookie->name);
112 c->value = strdup(cookie->value);
113 c->domain = strdup(cookie->domain);
114 c->path = strdup(cookie->path);
115 c->expires = soup_date_to_time_t(cookie->expires);
116 c->secure = static_cast<Eina_Bool>(cookie->secure);
117 c->http_only = static_cast<Eina_Bool>(cookie->http_only);
118 el = eina_list_append(el, c);
121 soup_cookies_free(l);
127 * Deletes a cookie from the cookie jar.
129 * Note that the fields name, value, domain and path are used to match this
130 * cookie in the cookie jar.
132 * @param cookie an Ewk_Cookie that has the info relative to that cookie.
134 EAPI void ewk_cookies_cookie_del(Ewk_Cookie *cookie)
137 EINA_SAFETY_ON_NULL_RETURN(cookie);
140 SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
141 SoupCookie* c1 = soup_cookie_new(
142 cookie->name, cookie->value, cookie->domain, cookie->path, -1);
144 l = soup_cookie_jar_all_cookies(cookieJar);
145 for (p = l; p; p = p->next) {
146 SoupCookie* c2 = static_cast<SoupCookie*>(p->data);
147 if (soup_cookie_equal(c1, c2)) {
148 soup_cookie_jar_delete_cookie(cookieJar, c2);
153 soup_cookie_free(c1);
154 soup_cookies_free(l);
159 * Free the memory used by a cookie.
161 * @param cookie the Ewk_Cookie struct that will be freed.
163 EAPI void ewk_cookies_cookie_free(Ewk_Cookie *cookie)
166 EINA_SAFETY_ON_NULL_RETURN(cookie);
169 free(cookie->domain);
176 * Set the cookies accept policy.
178 * Possible values are: EWK_COOKIE_JAR_ACCEPT_ALWAYS, which accepts every
179 * cookie sent from any page; EWK_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY, which
180 * accepts cookies only from the main page; and EWK_COOKIE_JAR_ACCEPT_NEVER,
181 * which rejects all cookies.
183 * @param p the acceptance policy
185 EAPI void ewk_cookies_policy_set(Ewk_Cookie_Policy p)
188 SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
189 SoupCookieJarAcceptPolicy policy;
191 policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
193 case EWK_COOKIE_JAR_ACCEPT_NEVER:
194 policy = SOUP_COOKIE_JAR_ACCEPT_NEVER;
196 case EWK_COOKIE_JAR_ACCEPT_ALWAYS:
197 policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
199 case EWK_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
200 policy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
204 soup_cookie_jar_set_accept_policy(cookieJar, policy);
209 * Gets the acceptance policy used in the current cookie jar.
211 * @returns the current acceptance policy
213 EAPI Ewk_Cookie_Policy ewk_cookies_policy_get()
215 Ewk_Cookie_Policy ewk_policy = EWK_COOKIE_JAR_ACCEPT_ALWAYS;
217 SoupCookieJar* cookieJar = WebCore::defaultCookieJar();
218 SoupCookieJarAcceptPolicy policy;
220 policy = soup_cookie_jar_get_accept_policy(cookieJar);
222 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
223 ewk_policy = EWK_COOKIE_JAR_ACCEPT_NEVER;
225 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
226 ewk_policy = EWK_COOKIE_JAR_ACCEPT_ALWAYS;
228 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
229 ewk_policy = EWK_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;