Changeset 95568 in webkit


Ignore:
Timestamp:
Sep 20, 2011 2:01:12 PM (13 years ago)
Author:
alexis.menard@openbossa.org
Message:

[Qt] [WK2] Implement a persistent cookie storage.
https://bugs.webkit.org/show_bug.cgi?id=65309

Reviewed by Chang Shu.

Source/WebCore:

Implement a cookie storage for the Qt port on WebKit2.
The implementation is using a SQLite database to store the cookies
and restore them. It uses a static object as CookieJar is not an
object but a set of global functions. The actual saving/restoring is on
the WebProcess side where our network stack lives.

Existing tests cover the new implementation. Unfortunately there is one
case that we can't easily simulate : login in a website, make sure that the webprocess
is not running and then going back to this website and see that we are logged.

  • WebCore.pri:
  • WebCore.pro:
  • platform/qt/CookieJarQt.cpp:

(WebCore::getHostnamesWithCookies):
(WebCore::deleteCookiesForHostname):
(WebCore::deleteAllCookies):
(WebCore::SharedCookieJarQt::shared):
(WebCore::SharedCookieJarQt::create):
(WebCore::SharedCookieJarQt::destroy):
(WebCore::SharedCookieJarQt::getHostnamesWithCookies):
(WebCore::SharedCookieJarQt::deleteCookiesForHostname):
(WebCore::SharedCookieJarQt::deleteAllCookies):
(WebCore::SharedCookieJarQt::SharedCookieJarQt):
(WebCore::SharedCookieJarQt::~SharedCookieJarQt):
(WebCore::SharedCookieJarQt::setCookiesFromUrl):
(WebCore::SharedCookieJarQt::ensureDatabaseTable):
(WebCore::SharedCookieJarQt::loadCookies):

  • platform/qt/CookieJarQt.h: Added.

Source/WebKit2:

Add parameter to the WebProcess creation to specify where cookies should be saved.
It also use the new cookie storage implementation and set it to our network stack
so cookies are used when using it.

  • Shared/WebProcessCreationParameters.cpp:

(WebKit::WebProcessCreationParameters::encode):
(WebKit::WebProcessCreationParameters::decode):

  • Shared/WebProcessCreationParameters.h:
  • UIProcess/qt/WebContextQt.cpp:

(WebKit::WebContext::platformInitializeWebProcess):

  • WebProcess/qt/WebProcessQt.cpp:

(WebKit::WebProcess::platformInitializeWebProcess):
(WebKit::WebProcess::platformTerminate):

Location:
trunk/Source
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r95567 r95568  
     12011-09-20  Alexis Menard  <alexis.menard@openbossa.org>
     2
     3        [Qt] [WK2] Implement a persistent cookie storage.
     4        https://bugs.webkit.org/show_bug.cgi?id=65309
     5
     6        Reviewed by Chang Shu.
     7
     8        Implement a cookie storage for the Qt port on WebKit2.
     9        The implementation is using a SQLite database to store the cookies
     10        and restore them. It uses a static object as CookieJar is not an
     11        object but a set of global functions. The actual saving/restoring is on
     12        the WebProcess side where our network stack lives.
     13
     14        Existing tests cover the new implementation. Unfortunately there is one
     15        case that we can't easily simulate : login in a website, make sure that the webprocess
     16        is not running and then going back to this website and see that we are logged.
     17
     18        * WebCore.pri:
     19        * WebCore.pro:
     20        * platform/qt/CookieJarQt.cpp:
     21        (WebCore::getHostnamesWithCookies):
     22        (WebCore::deleteCookiesForHostname):
     23        (WebCore::deleteAllCookies):
     24        (WebCore::SharedCookieJarQt::shared):
     25        (WebCore::SharedCookieJarQt::create):
     26        (WebCore::SharedCookieJarQt::destroy):
     27        (WebCore::SharedCookieJarQt::getHostnamesWithCookies):
     28        (WebCore::SharedCookieJarQt::deleteCookiesForHostname):
     29        (WebCore::SharedCookieJarQt::deleteAllCookies):
     30        (WebCore::SharedCookieJarQt::SharedCookieJarQt):
     31        (WebCore::SharedCookieJarQt::~SharedCookieJarQt):
     32        (WebCore::SharedCookieJarQt::setCookiesFromUrl):
     33        (WebCore::SharedCookieJarQt::ensureDatabaseTable):
     34        (WebCore::SharedCookieJarQt::loadCookies):
     35        * platform/qt/CookieJarQt.h: Added.
     36
    1372011-09-20  David Hyatt  <hyatt@apple.com>
    238
  • trunk/Source/WebCore/WebCore.pri

    r94890 r95568  
    66CONFIG += texmap
    77
    8 QT *= network
     8QT *= network sql
    99
    1010SOURCE_DIR = $$replace(PWD, /WebCore, "")
  • trunk/Source/WebCore/WebCore.pro

    r95551 r95568  
    20972097    platform/PopupMenu.h \
    20982098    platform/qt/ClipboardQt.h \
     2099    platform/qt/CookieJarQt.h \
    20992100    platform/qt/QWebPageClient.h \
    21002101    platform/qt/QtStyleOptionWebComboBox.h \
  • trunk/Source/WebCore/platform/qt/CookieJarQt.cpp

    r90341 r95568  
    11/*
    22 * Copyright (C) 2006 George Staikos <staikos@kde.org>
     3 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
    34 *
    45 * All rights reserved.
     
    2728
    2829#include "config.h"
     30#include "CookieJarQt.h"
     31
    2932#include "CookieJar.h"
    3033
     
    3942#include "qwebpage.h"
    4043#include "qwebsettings.h"
     44#include <QDateTime>
     45#include <QDir>
    4146#include <QNetworkAccessManager>
    4247#include <QNetworkCookie>
     48#include <QSqlQuery>
    4349#include <QStringList>
    4450
    4551namespace WebCore {
     52
     53static SharedCookieJarQt* s_sharedCookieJarQt = 0;
    4654
    4755static QNetworkCookieJar *cookieJar(const Document *document)
     
    145153void getHostnamesWithCookies(HashSet<String>& hostnames)
    146154{
    147     // FIXME: Not yet implemented
     155    SharedCookieJarQt* jar = SharedCookieJarQt::shared();
     156    if (jar)
     157        jar->getHostnamesWithCookies(hostnames);
    148158}
    149159
    150160void deleteCookiesForHostname(const String& hostname)
    151161{
    152     // FIXME: Not yet implemented
     162    SharedCookieJarQt* jar = SharedCookieJarQt::shared();
     163    if (jar)
     164        jar->deleteCookiesForHostname(hostname);
    153165}
    154166
    155167void deleteAllCookies()
    156168{
    157     // FIXME: Not yet implemented
    158 }
     169    SharedCookieJarQt* jar = SharedCookieJarQt::shared();
     170    if (jar)
     171        jar->deleteAllCookies();
     172}
     173
     174SharedCookieJarQt* SharedCookieJarQt::shared()
     175{
     176    return s_sharedCookieJarQt;
     177}
     178
     179SharedCookieJarQt* SharedCookieJarQt::create(const String& cookieStorageDirectory)
     180{
     181    if (!s_sharedCookieJarQt)
     182        s_sharedCookieJarQt = new SharedCookieJarQt(cookieStorageDirectory);
     183
     184    return s_sharedCookieJarQt;
     185}
     186
     187void SharedCookieJarQt::destroy()
     188{
     189    delete s_sharedCookieJarQt;
     190    s_sharedCookieJarQt = 0;
     191}
     192
     193void SharedCookieJarQt::getHostnamesWithCookies(HashSet<String>& hostnames)
     194{
     195    QList<QNetworkCookie> cookies = allCookies();
     196    foreach (const QNetworkCookie& networkCookie, cookies)
     197        hostnames.add(networkCookie.domain());
     198}
     199
     200void SharedCookieJarQt::deleteCookiesForHostname(const String& hostname)
     201{
     202    QList<QNetworkCookie> cookies = allCookies();
     203    QList<QNetworkCookie>::Iterator it = cookies.begin();
     204    QList<QNetworkCookie>::Iterator end = cookies.end();
     205    QSqlQuery sqlQuery(m_database);
     206    sqlQuery.prepare(QLatin1String("DELETE FROM cookies WHERE cookieId=:cookieIdvalue"));
     207    while (it != end) {
     208        if (it->domain() == QString(hostname)) {
     209            sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), it->domain().append(QLatin1String(it->name())));
     210            sqlQuery.exec();
     211            it = cookies.erase(it);
     212        } else
     213            it++;
     214    }
     215    setAllCookies(cookies);
     216}
     217
     218void SharedCookieJarQt::deleteAllCookies()
     219{
     220    QSqlQuery sqlQuery(m_database);
     221    sqlQuery.prepare(QLatin1String("DELETE * FROM cookies"));
     222    sqlQuery.exec();
     223    setAllCookies(QList<QNetworkCookie>());
     224}
     225
     226SharedCookieJarQt::SharedCookieJarQt(const String& cookieStorageDirectory)
     227{
     228    m_database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
     229    const QString cookieStoragePath = cookieStorageDirectory;
     230    QDir().mkpath(cookieStoragePath + QLatin1String(".QtWebKit/"));
     231    const QString dataBaseName = cookieStoragePath + QLatin1String(".QtWebKit/cookies.db");
     232    m_database.setDatabaseName(dataBaseName);
     233    ensureDatabaseTable();
     234    loadCookies();
     235}
     236
     237SharedCookieJarQt::~SharedCookieJarQt()
     238{
     239    m_database.close();
     240}
     241
     242bool 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
     262void 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
     273void 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"
    159285
    160286}
  • trunk/Source/WebKit2/ChangeLog

    r95565 r95568  
     12011-09-20  Alexis Menard  <alexis.menard@openbossa.org>
     2
     3        [Qt] [WK2] Implement a persistent cookie storage.
     4        https://bugs.webkit.org/show_bug.cgi?id=65309
     5
     6        Reviewed by Chang Shu.
     7
     8        Add parameter to the WebProcess creation to specify where cookies should be saved.
     9        It also use the new cookie storage implementation and set it to our network stack
     10        so cookies are used when using it.
     11
     12        * Shared/WebProcessCreationParameters.cpp:
     13        (WebKit::WebProcessCreationParameters::encode):
     14        (WebKit::WebProcessCreationParameters::decode):
     15        * Shared/WebProcessCreationParameters.h:
     16        * UIProcess/qt/WebContextQt.cpp:
     17        (WebKit::WebContext::platformInitializeWebProcess):
     18        * WebProcess/qt/WebProcessQt.cpp:
     19        (WebKit::WebProcess::platformInitializeWebProcess):
     20        (WebKit::WebProcess::platformTerminate):
     21
    1222011-09-20  Nayan Kumar K  <nayankk@motorola.com>
    223
  • trunk/Source/WebKit2/Shared/WebProcessCreationParameters.cpp

    r92713 r95568  
    9292#endif // USE(CFURLSTORAGESESSIONS)
    9393#endif
     94#if PLATFORM(QT)
     95    encoder->encode(cookieStorageDirectory);
     96#endif
    9497}
    9598
     
    173176#endif
    174177
     178#if PLATFORM(QT)
     179    if (!decoder->decode(parameters.cookieStorageDirectory))
     180        return false;
     181#endif
     182
    175183    return true;
    176184}
  • trunk/Source/WebKit2/Shared/WebProcessCreationParameters.h

    r92713 r95568  
    112112#endif // USE(CFURLSTORAGESESSIONS)
    113113#endif // PLATFORM(WIN)
     114#if PLATFORM(QT)
     115    String cookieStorageDirectory;
     116#endif
    114117};
    115118
  • trunk/Source/WebKit2/UIProcess/qt/WebContextQt.cpp

    r81274 r95568  
    3030#include "ApplicationCacheStorage.h"
    3131#include "WebProcessCreationParameters.h"
     32#include <QDesktopServices>
    3233#include <QProcess>
    3334
     
    4344}
    4445
    45 void WebContext::platformInitializeWebProcess(WebProcessCreationParameters&)
     46void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
    4647{
    4748    qRegisterMetaType<QProcess::ExitStatus>("QProcess::ExitStatus");
     49    parameters.cookieStorageDirectory = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    4850}
    4951
  • trunk/Source/WebKit2/WebProcess/qt/WebProcessQt.cpp

    r80753 r95568  
    3030#include <WebCore/RuntimeEnabledFeatures.h>
    3131#include <QNetworkAccessManager>
     32#include <QNetworkCookieJar>
     33#include <WebCore/CookieJarQt.h>
    3234
    3335namespace WebKit {
     
    4547{
    4648    m_networkAccessManager = new QNetworkAccessManager;
     49    ASSERT(!parameters.cookieStorageDirectory.isEmpty() && !parameters.cookieStorageDirectory.isNull());
     50    WebCore::SharedCookieJarQt* jar = WebCore::SharedCookieJarQt::create(parameters.cookieStorageDirectory);
     51    m_networkAccessManager->setCookieJar(jar);
     52    // Do not let QNetworkAccessManager delete the jar.
     53    jar->setParent(0);
    4754
    4855    // Disable runtime enabled features that have no WebKit2 implementation yet.
     
    6067    delete m_networkAccessManager;
    6168    m_networkAccessManager = 0;
     69    WebCore::SharedCookieJarQt::shared()->destroy();
    6270}
    6371
Note: See TracChangeset for help on using the changeset viewer.