2 * Copyright (C) 2006 George Staikos <staikos@kde.org>
3 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "CookieJarQt.h"
32 #include "CookieJar.h"
36 #include "FrameLoaderClientQt.h"
38 #include "NetworkingContext.h"
39 #include "PlatformString.h"
40 #include "ThirdPartyCookiesQt.h"
42 #include <QNetworkAccessManager>
43 #include <QNetworkCookie>
45 #include <QStringList>
49 static SharedCookieJarQt* s_sharedCookieJarQt = 0;
51 static NetworkingContext* networkingContext(const Document* document)
55 Frame* frame = document->frame();
58 FrameLoader* loader = frame->loader();
61 return loader->networkingContext();
64 void setCookies(Document* document, const KURL& url, const String& value)
66 NetworkingContext* context = networkingContext(document);
69 QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
73 QUrl urlForCookies(url);
74 QUrl firstPartyUrl(document->firstPartyForCookies());
75 if (!thirdPartyCookiePolicyPermits(context, urlForCookies, firstPartyUrl))
78 QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(QString(value).toLatin1());
79 QList<QNetworkCookie>::Iterator it = cookies.begin();
80 while (it != cookies.end()) {
82 it = cookies.erase(it);
87 jar->setCookiesFromUrl(cookies, urlForCookies);
90 String cookies(const Document* document, const KURL& url)
92 NetworkingContext* context = networkingContext(document);
95 QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
97 QUrl urlForCookies(url);
98 QUrl firstPartyUrl(document->firstPartyForCookies());
99 if (!thirdPartyCookiePolicyPermits(context, urlForCookies, firstPartyUrl))
102 QList<QNetworkCookie> cookies = jar->cookiesForUrl(urlForCookies);
103 if (cookies.isEmpty())
106 QStringList resultCookies;
107 foreach (const QNetworkCookie& networkCookie, cookies) {
108 if (networkCookie.isHttpOnly())
110 resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
113 return resultCookies.join(QLatin1String("; "));
116 String cookieRequestHeaderFieldValue(const Document* document, const KURL &url)
118 NetworkingContext* context = networkingContext(document);
121 QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
123 QList<QNetworkCookie> cookies = jar->cookiesForUrl(QUrl(url));
124 if (cookies.isEmpty())
127 QStringList resultCookies;
128 foreach (QNetworkCookie networkCookie, cookies)
129 resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
131 return resultCookies.join(QLatin1String("; "));
134 bool cookiesEnabled(const Document* document)
136 NetworkingContext* context = networkingContext(document);
139 return context->networkAccessManager()->cookieJar();
142 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies)
144 // FIXME: Not yet implemented
146 return false; // return true when implemented
149 void deleteCookie(const Document*, const KURL&, const String&)
151 // FIXME: Not yet implemented
154 void getHostnamesWithCookies(HashSet<String>& hostnames)
156 SharedCookieJarQt* jar = SharedCookieJarQt::shared();
158 jar->getHostnamesWithCookies(hostnames);
161 void deleteCookiesForHostname(const String& hostname)
163 SharedCookieJarQt* jar = SharedCookieJarQt::shared();
165 jar->deleteCookiesForHostname(hostname);
168 void deleteAllCookies()
170 SharedCookieJarQt* jar = SharedCookieJarQt::shared();
172 jar->deleteAllCookies();
175 SharedCookieJarQt* SharedCookieJarQt::shared()
177 return s_sharedCookieJarQt;
180 SharedCookieJarQt* SharedCookieJarQt::create(const String& cookieStorageDirectory)
182 if (!s_sharedCookieJarQt)
183 s_sharedCookieJarQt = new SharedCookieJarQt(cookieStorageDirectory);
185 return s_sharedCookieJarQt;
188 void SharedCookieJarQt::destroy()
190 delete s_sharedCookieJarQt;
191 s_sharedCookieJarQt = 0;
194 void SharedCookieJarQt::getHostnamesWithCookies(HashSet<String>& hostnames)
196 QList<QNetworkCookie> cookies = allCookies();
197 foreach (const QNetworkCookie& networkCookie, cookies)
198 hostnames.add(networkCookie.domain());
201 void SharedCookieJarQt::deleteCookiesForHostname(const String& hostname)
203 QList<QNetworkCookie> cookies = allCookies();
204 QList<QNetworkCookie>::Iterator it = cookies.begin();
205 QList<QNetworkCookie>::Iterator end = cookies.end();
206 QSqlQuery sqlQuery(m_database);
207 sqlQuery.prepare(QLatin1String("DELETE FROM cookies WHERE cookieId=:cookieIdvalue"));
209 if (it->domain() == QString(hostname)) {
210 sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), it->domain().append(QLatin1String(it->name())));
212 it = cookies.erase(it);
216 setAllCookies(cookies);
219 void SharedCookieJarQt::deleteAllCookies()
221 QSqlQuery sqlQuery(m_database);
222 sqlQuery.prepare(QLatin1String("DELETE * FROM cookies"));
224 setAllCookies(QList<QNetworkCookie>());
227 SharedCookieJarQt::SharedCookieJarQt(const String& cookieStorageDirectory)
229 m_database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
230 const QString cookieStoragePath = cookieStorageDirectory;
231 const QString dataBaseName = cookieStoragePath + QLatin1String("cookies.db");
232 m_database.setDatabaseName(dataBaseName);
233 ensureDatabaseTable();
237 SharedCookieJarQt::~SharedCookieJarQt()
242 bool SharedCookieJarQt::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
244 if (!QNetworkCookieJar::setCookiesFromUrl(cookieList, url))
246 QSqlQuery sqlQuery(m_database);
247 sqlQuery.prepare(QLatin1String("INSERT OR REPLACE INTO cookies (cookieId, cookie) VALUES (:cookieIdvalue, :cookievalue)"));
248 QVariantList cookiesIds;
249 QVariantList cookiesValues;
250 foreach (const QNetworkCookie &cookie, cookiesForUrl(url)) {
251 if (cookie.isSessionCookie())
253 cookiesIds.append(cookie.domain().append(QLatin1String(cookie.name())));
254 cookiesValues.append(cookie.toRawForm());
256 sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), cookiesIds);
257 sqlQuery.bindValue(QLatin1String(":cookievalue"), cookiesValues);
258 sqlQuery.execBatch();
262 void SharedCookieJarQt::ensureDatabaseTable()
264 if (!m_database.open()) {
265 qWarning("Can't open cookie database");
268 QSqlQuery sqlQuery(m_database);
269 sqlQuery.prepare(QLatin1String("CREATE TABLE IF NOT EXISTS cookies (cookieId VARCHAR PRIMARY KEY, cookie BLOB);"));
273 void SharedCookieJarQt::loadCookies()
275 QList<QNetworkCookie> cookies;
276 QSqlQuery sqlQuery(m_database);
277 sqlQuery.prepare(QLatin1String("SELECT cookie FROM cookies"));
279 while (sqlQuery.next())
280 cookies.append(QNetworkCookie::parseCookies(sqlQuery.value(0).toByteArray()));
281 setAllCookies(cookies);
284 #include "moc_CookieJarQt.cpp"