2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "cookiejar.h"
30 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
31 #include <QStandardPaths>
33 #include <QDesktopServices>
36 #include <QTextStream>
38 TestBrowserCookieJar::TestBrowserCookieJar(QObject* parent)
39 : QNetworkCookieJar(parent)
40 , m_storageEnabled(false)
42 // We use a timer for the real disk write to avoid multiple IO
43 // syscalls in sequence (when loading pages which set multiple cookies).
44 m_timer.setInterval(10000);
45 m_timer.setSingleShot(true);
46 connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
48 #ifndef QT_NO_DESKTOPSERVICES
49 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
50 QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
52 QString path = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
55 QString path = QDir::homePath() + "/.QtTestBrowser";
59 m_file.setFileName(path + "/cookieJar");
62 TestBrowserCookieJar::~TestBrowserCookieJar()
64 if (m_storageEnabled) {
70 bool TestBrowserCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
72 bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
73 if (status && m_storageEnabled)
78 void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
80 m_storageEnabled = enabled;
82 if (enabled && allCookies().isEmpty())
85 // When disabling, save current cookies.
86 if (!enabled && !allCookies().isEmpty())
90 void TestBrowserCookieJar::scheduleSaveToDisk()
92 // We extract the raw cookies here because the user may
93 // enable/disable/clear cookies while the timer is running.
98 void TestBrowserCookieJar::extractRawCookies()
100 QList<QNetworkCookie> cookies = allCookies();
101 m_rawCookies.clear();
103 foreach (const QNetworkCookie &cookie, cookies) {
104 if (!cookie.isSessionCookie())
105 m_rawCookies.append(cookie.toRawForm());
109 void TestBrowserCookieJar::saveToDisk()
113 if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
114 QTextStream out(&m_file);
115 foreach (const QByteArray &cookie, m_rawCookies)
116 out << cookie + "\n";
119 qWarning("IO error handling cookiejar file");
122 void TestBrowserCookieJar::loadFromDisk()
124 if (!m_file.exists())
127 QList<QNetworkCookie> cookies;
129 if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
130 QTextStream in(&m_file);
132 cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
135 qWarning("IO error handling cookiejar file");
137 setAllCookies(cookies);
140 void TestBrowserCookieJar::reset()
142 setAllCookies(QList<QNetworkCookie>());
143 if (m_storageEnabled)
144 scheduleSaveToDisk();