1 /* 2 * Copyright (C) 2008 Xan Lopez <xan (at) gnome.org> 3 * Copyright (C) 2009 Igalia S.L. 4 * Copyright (C) 2008 Apple Inc. All rights reserved. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #include "config.h" 22 #include "CookieJarSoup.h" 23 24 #include "Cookie.h" 25 #include "Document.h" 26 #include "GOwnPtrSoup.h" 27 #include "KURL.h" 28 #include <wtf/text/CString.h> 29 30 namespace WebCore { 31 32 static bool cookiesInitialized; 33 static SoupCookieJar* cookieJar; 34 35 SoupCookieJar* defaultCookieJar() 36 { 37 if (!cookiesInitialized) { 38 cookiesInitialized = true; 39 40 cookieJar = soup_cookie_jar_new(); 41 soup_cookie_jar_set_accept_policy(cookieJar, SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY); 42 } 43 44 return cookieJar; 45 } 46 47 void setDefaultCookieJar(SoupCookieJar* jar) 48 { 49 cookiesInitialized = true; 50 51 if (cookieJar) 52 g_object_unref(cookieJar); 53 54 cookieJar = jar; 55 56 if (cookieJar) 57 g_object_ref(cookieJar); 58 } 59 60 void setCookies(Document* document, const KURL& url, const String& value) 61 { 62 SoupCookieJar* jar = defaultCookieJar(); 63 if (!jar) 64 return; 65 66 GOwnPtr<SoupURI> origin(soup_uri_new(url.string().utf8().data())); 67 68 GOwnPtr<SoupURI> firstParty(soup_uri_new(document->firstPartyForCookies().string().utf8().data())); 69 70 soup_cookie_jar_set_cookie_with_first_party(jar, 71 origin.get(), 72 firstParty.get(), 73 value.utf8().data()); 74 } 75 76 String cookies(const Document* /*document*/, const KURL& url) 77 { 78 SoupCookieJar* jar = defaultCookieJar(); 79 if (!jar) 80 return String(); 81 82 SoupURI* uri = soup_uri_new(url.string().utf8().data()); 83 char* cookies = soup_cookie_jar_get_cookies(jar, uri, FALSE); 84 soup_uri_free(uri); 85 86 String result(String::fromUTF8(cookies)); 87 g_free(cookies); 88 89 return result; 90 } 91 92 String cookieRequestHeaderFieldValue(const Document* /*document*/, const KURL& url) 93 { 94 SoupCookieJar* jar = defaultCookieJar(); 95 if (!jar) 96 return String(); 97 98 SoupURI* uri = soup_uri_new(url.string().utf8().data()); 99 char* cookies = soup_cookie_jar_get_cookies(jar, uri, TRUE); 100 soup_uri_free(uri); 101 102 String result(String::fromUTF8(cookies)); 103 g_free(cookies); 104 105 return result; 106 } 107 108 bool cookiesEnabled(const Document* /*document*/) 109 { 110 return defaultCookieJar(); 111 } 112 113 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies) 114 { 115 // FIXME: Not yet implemented 116 rawCookies.clear(); 117 return false; // return true when implemented 118 } 119 120 void deleteCookie(const Document*, const KURL&, const String&) 121 { 122 // FIXME: Not yet implemented 123 } 124 125 void getHostnamesWithCookies(HashSet<String>& hostnames) 126 { 127 // FIXME: Not yet implemented 128 } 129 130 void deleteCookiesForHostname(const String& hostname) 131 { 132 // FIXME: Not yet implemented 133 } 134 135 void deleteAllCookies() 136 { 137 // FIXME: Not yet implemented 138 } 139 140 } 141