tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / qt / CookieJarQt.cpp
1 /*
2  * Copyright (C) 2006 George Staikos <staikos@kde.org>
3  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include "config.h"
30 #include "CookieJarQt.h"
31
32 #include "CookieJar.h"
33
34 #include "Cookie.h"
35 #include "Document.h"
36 #include "FrameLoaderClientQt.h"
37 #include "KURL.h"
38 #include "NetworkingContext.h"
39 #include "PlatformString.h"
40 #include "ThirdPartyCookiesQt.h"
41 #include <QDateTime>
42 #include <QNetworkAccessManager>
43 #include <QNetworkCookie>
44 #include <QSqlQuery>
45 #include <QStringList>
46
47 namespace WebCore {
48
49 static SharedCookieJarQt* s_sharedCookieJarQt = 0;
50
51 static NetworkingContext* networkingContext(const Document* document)
52 {
53     if (!document)
54         return 0;
55     Frame* frame = document->frame();
56     if (!frame)
57         return 0;
58     FrameLoader* loader = frame->loader();
59     if (!loader)
60         return 0;
61     return loader->networkingContext();
62 }
63
64 void setCookies(Document* document, const KURL& url, const String& value)
65 {
66     NetworkingContext* context = networkingContext(document);
67     if (!context)
68         return;
69     QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
70     if (!jar)
71         return;
72
73     QUrl urlForCookies(url);
74     QUrl firstPartyUrl(document->firstPartyForCookies());
75     if (!thirdPartyCookiePolicyPermits(context, urlForCookies, firstPartyUrl))
76         return;
77
78     QList<QNetworkCookie> cookies = QNetworkCookie::parseCookies(QString(value).toLatin1());
79     QList<QNetworkCookie>::Iterator it = cookies.begin();
80     while (it != cookies.end()) {
81         if (it->isHttpOnly())
82             it = cookies.erase(it);
83         else
84             ++it;
85     }
86
87     jar->setCookiesFromUrl(cookies, urlForCookies);
88 }
89
90 String cookies(const Document* document, const KURL& url)
91 {
92     NetworkingContext* context = networkingContext(document);
93     if (!context)
94         return String();
95     QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
96
97     QUrl urlForCookies(url);
98     QUrl firstPartyUrl(document->firstPartyForCookies());
99     if (!thirdPartyCookiePolicyPermits(context, urlForCookies, firstPartyUrl))
100         return String();
101
102     QList<QNetworkCookie> cookies = jar->cookiesForUrl(urlForCookies);
103     if (cookies.isEmpty())
104         return String();
105
106     QStringList resultCookies;
107     foreach (const QNetworkCookie& networkCookie, cookies) {
108         if (networkCookie.isHttpOnly())
109             continue;
110         resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
111     }
112
113     return resultCookies.join(QLatin1String("; "));
114 }
115
116 String cookieRequestHeaderFieldValue(const Document* document, const KURL &url)
117 {
118     NetworkingContext* context = networkingContext(document);
119     if (!context)
120         return String();
121     QNetworkCookieJar* jar = context->networkAccessManager()->cookieJar();
122
123     QList<QNetworkCookie> cookies = jar->cookiesForUrl(QUrl(url));
124     if (cookies.isEmpty())
125         return String();
126
127     QStringList resultCookies;
128     foreach (QNetworkCookie networkCookie, cookies)
129         resultCookies.append(QString::fromLatin1(networkCookie.toRawForm(QNetworkCookie::NameAndValueOnly).constData()));
130
131     return resultCookies.join(QLatin1String("; "));
132 }
133
134 bool cookiesEnabled(const Document* document)
135 {
136     NetworkingContext* context = networkingContext(document);
137     if (!context)
138         return false;
139     return context->networkAccessManager()->cookieJar();
140 }
141
142 bool getRawCookies(const Document*, const KURL&, Vector<Cookie>& rawCookies)
143 {
144     // FIXME: Not yet implemented
145     rawCookies.clear();
146     return false; // return true when implemented
147 }
148
149 void deleteCookie(const Document*, const KURL&, const String&)
150 {
151     // FIXME: Not yet implemented
152 }
153
154 void getHostnamesWithCookies(HashSet<String>& hostnames)
155 {
156     SharedCookieJarQt* jar = SharedCookieJarQt::shared();
157     if (jar)
158         jar->getHostnamesWithCookies(hostnames);
159 }
160
161 void deleteCookiesForHostname(const String& hostname)
162 {
163     SharedCookieJarQt* jar = SharedCookieJarQt::shared();
164     if (jar)
165         jar->deleteCookiesForHostname(hostname);
166 }
167
168 void deleteAllCookies()
169 {
170     SharedCookieJarQt* jar = SharedCookieJarQt::shared();
171     if (jar)
172         jar->deleteAllCookies();
173 }
174
175 SharedCookieJarQt* SharedCookieJarQt::shared()
176 {
177     return s_sharedCookieJarQt;
178 }
179
180 SharedCookieJarQt* SharedCookieJarQt::create(const String& cookieStorageDirectory)
181 {
182     if (!s_sharedCookieJarQt)
183         s_sharedCookieJarQt = new SharedCookieJarQt(cookieStorageDirectory);
184
185     return s_sharedCookieJarQt;
186 }
187
188 void SharedCookieJarQt::destroy()
189 {
190     delete s_sharedCookieJarQt;
191     s_sharedCookieJarQt = 0;
192 }
193
194 void SharedCookieJarQt::getHostnamesWithCookies(HashSet<String>& hostnames)
195 {
196     QList<QNetworkCookie> cookies = allCookies();
197     foreach (const QNetworkCookie& networkCookie, cookies)
198         hostnames.add(networkCookie.domain());
199 }
200
201 void SharedCookieJarQt::deleteCookiesForHostname(const String& hostname)
202 {
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"));
208     while (it != end) {
209         if (it->domain() == QString(hostname)) {
210             sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), it->domain().append(QLatin1String(it->name())));
211             sqlQuery.exec();
212             it = cookies.erase(it);
213         } else
214             it++;
215     }
216     setAllCookies(cookies);
217 }
218
219 void SharedCookieJarQt::deleteAllCookies()
220 {
221     QSqlQuery sqlQuery(m_database);
222     sqlQuery.prepare(QLatin1String("DELETE * FROM cookies"));
223     sqlQuery.exec();
224     setAllCookies(QList<QNetworkCookie>());
225 }
226
227 SharedCookieJarQt::SharedCookieJarQt(const String& cookieStorageDirectory)
228 {
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();
234     loadCookies();
235 }
236
237 SharedCookieJarQt::~SharedCookieJarQt()
238 {
239     m_database.close();
240 }
241
242 bool SharedCookieJarQt::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
243 {
244     if (!QNetworkCookieJar::setCookiesFromUrl(cookieList, url))
245         return false;
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())
252             continue;
253         cookiesIds.append(cookie.domain().append(QLatin1String(cookie.name())));
254         cookiesValues.append(cookie.toRawForm());
255     }
256     sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), cookiesIds);
257     sqlQuery.bindValue(QLatin1String(":cookievalue"), cookiesValues);
258     sqlQuery.execBatch();
259     return true;
260 }
261
262 void SharedCookieJarQt::ensureDatabaseTable()
263 {
264     if (!m_database.open()) {
265         qWarning("Can't open cookie database");
266         return;
267     }
268     QSqlQuery sqlQuery(m_database);
269     sqlQuery.prepare(QLatin1String("CREATE TABLE IF NOT EXISTS cookies (cookieId VARCHAR PRIMARY KEY, cookie BLOB);"));
270     sqlQuery.exec();
271 }
272
273 void SharedCookieJarQt::loadCookies()
274 {
275     QList<QNetworkCookie> cookies;
276     QSqlQuery sqlQuery(m_database);
277     sqlQuery.prepare(QLatin1String("SELECT cookie FROM cookies"));
278     sqlQuery.exec();
279     while (sqlQuery.next())
280         cookies.append(QNetworkCookie::parseCookies(sqlQuery.value(0).toByteArray()));
281     setAllCookies(cookies);
282 }
283
284 #include "moc_CookieJarQt.cpp"
285
286 }
287
288 // vim: ts=4 sw=4 et