From cac39bd88a396b885f59bc23506ba1a8b2bfca5e Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 8 May 2012 18:33:51 +0200 Subject: pbs2 clear cookies extension (tofix: multiple loaded URLs messages) --- src/fbgui/fbgui.cpp | 81 ++++++++++++++++++++++++++++++++++++++- src/fbgui/fbgui.h | 5 ++- src/fbgui/javascriptinterface.cpp | 10 +++++ src/fbgui/javascriptinterface.h | 8 ++++ testApp.sh | 2 +- 5 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/fbgui/fbgui.cpp b/src/fbgui/fbgui.cpp index 48e776b..c4f5f22 100644 --- a/src/fbgui/fbgui.cpp +++ b/src/fbgui/fbgui.cpp @@ -49,6 +49,12 @@ void fbgui::init() { QObject::connect(jsi, SIGNAL(quitFbgui()), this, SLOT(close())); QObject::connect(jsi, SIGNAL(shutDownClient()), this, SLOT(shutdownSystem())); + QObject::connect(jsi, SIGNAL(deleteCookies()), this, + SLOT(clearAllCookies())); + QObject::connect(jsi, SIGNAL(showCookies()), this, + SLOT(printCookies())); + QObject::connect(jsi, SIGNAL(showHistory()), this, + SLOT(printHistory())); QObject::connect(_webView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared()), jsi, SLOT(attachToDOM())); @@ -124,7 +130,6 @@ void fbgui::loadURL() { QObject::connect(_webView, SIGNAL(loadFinished(bool)), this, SLOT(loadURLDone(bool))); _webView->load(req, QNetworkAccessManager::PostOperation, postData); - } // TODO: error page if no host. } @@ -140,6 +145,80 @@ void fbgui::loadURLDone(bool success) { LOG4CXX_DEBUG(coreLogger, "Loaded URL: " << _webView->url().toString()); } } +//------------------------------------------------------------------------------------------- +/** + * This method clears all cookies aside from the ones sent by baseURL. + * + */ + +void fbgui::clearAllCookies() { + + LOG4CXX_DEBUG(coreLogger, "Clearing cookies..."); + + // persist pbs cookies and delete the rest. + QUrl currentURL(_webView->page()->mainFrame()->url()); + QNetworkCookieJar* cookieJar = new QNetworkCookieJar(); + QList cookieList = _webView->page()->networkAccessManager()->cookieJar()->cookiesForUrl(currentURL); + _webView->page()->networkAccessManager()->setCookieJar(cookieJar); + + // restart the session with pbs + loadURL(); +} + +void fbgui::printCookies() { + + QList cookieList = _webView->page()->networkAccessManager()->cookieJar()->cookiesForUrl(_webView->page()->mainFrame()->url()); + + if (cookieList.isEmpty()) { + LOG4CXX_DEBUG(coreLogger, "No cookies saved!"); + } + else { + QList::iterator i; + for (i = cookieList.begin(); i != cookieList.end(); ++i) { + if (i->domain() == baseURL.host() || i->domain() == "." + baseURL.host()) { + LOG4CXX_DEBUG(coreLogger, "Keeping cookie from domain '" << i->domain() << "': " << i->toRawForm()); + } + else { + LOG4CXX_DEBUG(coreLogger, "Deleting cookie from domain '" << i->domain() << "': " << i->toRawForm()); + } + } + } + +} +void fbgui::printHistory() { + LOG4CXX_DEBUG(coreLogger, "--------------------------------------------------"); + QList urls; + QList history = _webView->history()->backItems(10); + QList::iterator n; + for (n = history.begin(); n != history.end(); ++n) { + LOG4CXX_DEBUG(coreLogger, "History item: " << n->url().toString()); + if (!(urls.contains(n->url()))) { + urls.append(n->url()); + LOG4CXX_DEBUG(coreLogger, "Appended: " << n->url().toString()); + } + } + + QList::iterator u; + for (u = urls.begin(); u != urls.end(); ++u) { + LOG4CXX_DEBUG(coreLogger, "Cookie from URL " << u->toString()); + QList cookieList = _webView->page()->networkAccessManager()->cookieJar()->cookiesForUrl(*u); + if (cookieList.isEmpty()) { + LOG4CXX_DEBUG(coreLogger, "No cookies saved!"); + } + else { + QList::iterator i; + for (i = cookieList.begin(); i != cookieList.end(); ++i) { + if (i->domain() == baseURL.host()) { + LOG4CXX_DEBUG(coreLogger, "Keeping cookie from domain '" << i->domain() << "': " << i->toRawForm()); + } + else { + LOG4CXX_DEBUG(coreLogger, "Deleting cookie from domain '" << i->domain() << "': " << i->toRawForm()); + } + } + } + } + LOG4CXX_DEBUG(coreLogger, "--------------------------------------------------"); +} //------------------------------------------------------------------------------------------- /** diff --git a/src/fbgui/fbgui.h b/src/fbgui/fbgui.h index c300387..3eb4bcd 100644 --- a/src/fbgui/fbgui.h +++ b/src/fbgui/fbgui.h @@ -45,9 +45,12 @@ private: // QNetworkRequest for the Network Request QNetworkRequest* _qnr; + private slots: void loadURLDone(bool success); - + void clearAllCookies(); + void printCookies(); + void printHistory(); void loadSystem(); // prepares kexec by loading downloaded initramfs, kernel into kexec void prepareKexec(); diff --git a/src/fbgui/javascriptinterface.cpp b/src/fbgui/javascriptinterface.cpp index 8022797..c730dfb 100644 --- a/src/fbgui/javascriptinterface.cpp +++ b/src/fbgui/javascriptinterface.cpp @@ -80,6 +80,16 @@ void JavascriptInterface::loadJQuery() { } } +void JavascriptInterface::clearCookies() { + emit deleteCookies(); +} +void JavascriptInterface::dumpCookies() { + emit showCookies(); +} +void JavascriptInterface::dumpHistory() { + emit showHistory(); +} + //------------------------------------------------------------------------------------------------------- /** * This method quits the whole program. diff --git a/src/fbgui/javascriptinterface.h b/src/fbgui/javascriptinterface.h index 2b3c83f..e2885ce 100644 --- a/src/fbgui/javascriptinterface.h +++ b/src/fbgui/javascriptinterface.h @@ -34,6 +34,7 @@ protected: // loads jQuery code void loadJQuery(); + signals: // quit the application void quitFbgui(); @@ -41,11 +42,18 @@ signals: void shutDownClient(); // reboot the system. connected to fbgui::performReboot() void rebootClient(); + void deleteCookies(); + void showCookies(); + void showHistory(); public slots: // make sure the interface stays attached on webpage reload void attachToDOM(); + void clearCookies(); + void dumpCookies(); + void dumpHistory(); + void quit(); void shutDown(); void reboot(); diff --git a/testApp.sh b/testApp.sh index ad505d8..b35c18e 100755 --- a/testApp.sh +++ b/testApp.sh @@ -42,7 +42,7 @@ working_path=`dirname "$script_path"` display_id=$(grep -n $(whoami) /etc/passwd| head -n 1|awk -F : '{print $1}') # Start QT's virtual framebuffer with proper display_id -/usr/local/Trolltech/$QT_VERSION/bin/qvfb -width 1024 -height 768 -qwsdisplay :$display_id & +/usr/local/Trolltech/$QT_VERSION/bin/qvfb -width 1156 -height 800 -qwsdisplay :$display_id & # quick sleep to wait for qvfb loading sleep 0.2 # Start fbgui connecting to QVFb with display_id from above. -- cgit v1.2.3-55-g7522