1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

Backend support for setting per-domain UserAgent.

see #399
This commit is contained in:
nowrep 2012-08-16 23:15:31 +02:00
parent 064fa19097
commit 3f5b6382a3
7 changed files with 103 additions and 27 deletions

View File

@ -47,6 +47,7 @@
#include "qzsettings.h"
#include "clearprivatedata.h"
#include "commandlineoptions.h"
#include "useragentmanager.h"
#ifdef Q_WS_MAC
#include <QFileOpenEvent>
@ -81,6 +82,7 @@ MainApplication::MainApplication(int &argc, char** argv)
, m_desktopNotifications(0)
, m_searchEnginesManager(0)
, m_dbWriter(new DatabaseWriter(this))
, m_uaManager(new UserAgentManager)
, m_isPrivateSession(false)
, m_isClosing(false)
, m_isStateChanged(false)
@ -350,7 +352,6 @@ void MainApplication::loadSettings()
setWheelScrollLines(settings.value("wheelScrollLines", wheelScrollLines()).toInt());
m_websettings->setUserStyleSheetUrl(userStyleSheet(settings.value("userStyleSheet", "").toString()));
WebPage::setUserAgent(settings.value("UserAgent", "").toString());
settings.endGroup();
settings.beginGroup("Browser-Fonts");
@ -382,6 +383,7 @@ void MainApplication::loadSettings()
}
qzSettings->loadSettings();
m_uaManager->loadSettings();
}
void MainApplication::reloadSettings()
@ -956,3 +958,8 @@ bool MainApplication::checkSettingsDir()
return dir.isReadable();
}
MainApplication::~MainApplication()
{
delete m_uaManager;
}

View File

@ -46,6 +46,7 @@ class DesktopNotificationsFactory;
class IconProvider;
class SearchEnginesManager;
class DatabaseWriter;
class UserAgentManager;
class QT_QUPZILLA_EXPORT MainApplication : public QtSingleApplication
{
@ -58,6 +59,7 @@ public:
QString THEMESDIR;
explicit MainApplication(int &argc, char** argv);
~MainApplication();
void connectDatabase();
void loadSettings();
@ -94,7 +96,9 @@ public:
SearchEnginesManager* searchEnginesManager();
QNetworkDiskCache* networkCache();
DesktopNotificationsFactory* desktopNotifications();
DatabaseWriter* dbWriter() { return m_dbWriter; }
UserAgentManager* uaManager() { return m_uaManager; }
#ifdef Q_WS_MAC
bool event(QEvent* e);
@ -144,6 +148,7 @@ private:
DesktopNotificationsFactory* m_desktopNotifications;
SearchEnginesManager* m_searchEnginesManager;
DatabaseWriter* m_dbWriter;
UserAgentManager* m_uaManager;
QList<QWeakPointer<QupZilla> > m_mainWindows;

View File

@ -180,7 +180,8 @@ SOURCES += \
tools/emptynetworkreply.cpp \
3rdparty/processinfo.cpp \
preferences/pluginsmanager.cpp \
other/qzsettings.cpp
other/qzsettings.cpp \
other/useragentmanager.cpp
HEADERS += \
webview/tabpreview.h \
@ -332,7 +333,8 @@ HEADERS += \
tools/emptynetworkreply.h \
3rdparty/processinfo.h \
preferences/pluginsmanager.h \
other/qzsettings.h
other/qzsettings.h \
other/useragentmanager.h
FORMS += \
preferences/autofillmanager.ui \

View File

@ -0,0 +1,52 @@
#include "useragentmanager.h"
#include "qupzilla.h"
#include "globalfunctions.h"
#include "settings.h"
UserAgentManager::UserAgentManager()
{
m_fakeUserAgent = QString("Mozilla/5.0 (%1) AppleWebKit/%2 (KHTML, like Gecko) Chrome/10.0 Safari/%2").arg(qz_buildSystem(), QupZilla::WEBKITVERSION);
}
void UserAgentManager::loadSettings()
{
Settings settings;
settings.beginGroup("Web-Browser-Settings");
m_globalUserAgent = settings.value("UserAgent", QString()).toString();
settings.endGroup();
settings.beginGroup("UserAgent-Settings");
m_usePerDomainUserAgent = settings.value("UsePerDomainUA", false).toBool();
QStringList domainList = settings.value("DomainList", QStringList()).toStringList();
QStringList userAgentsList = settings.value("UserAgentsList", QStringList()).toStringList();
settings.endGroup();
m_usePerDomainUserAgent = (m_usePerDomainUserAgent && domainList.count() == userAgentsList.count());
if (m_usePerDomainUserAgent) {
for (int i = 0; i < domainList.count(); ++i) {
m_userAgentsList[domainList.at(i)] = userAgentsList.at(i);
}
}
}
QString UserAgentManager::userAgentForUrl(const QUrl &url)
{
const QString &host = url.host();
if (m_usePerDomainUserAgent) {
QHashIterator<QString, QString> i(m_userAgentsList);
while (i.hasNext()) {
i.next();
if (host.endsWith(i.key())) {
return i.value();
}
}
}
if (host.contains("google")) {
return m_fakeUserAgent;
}
return m_globalUserAgent;
}

View File

@ -0,0 +1,28 @@
#ifndef USERAGENTMANAGER_H
#define USERAGENTMANAGER_H
#include <QHash>
#include "qz_namespace.h"
class QUrl;
class QT_QUPZILLA_EXPORT UserAgentManager
{
public:
explicit UserAgentManager();
void loadSettings();
QString userAgentForUrl(const QUrl &url);
private:
QString m_globalUserAgent;
QString m_fakeUserAgent;
bool m_usePerDomainUserAgent;
QHash<QString, QString> m_userAgentsList;
};
#endif // USERAGENTMANAGER_H

View File

@ -34,6 +34,7 @@
#include "adblockmanager.h"
#include "iconprovider.h"
#include "qzsettings.h"
#include "useragentmanager.h"
#ifdef NONBLOCK_JS_DIALOGS
#include "ui_jsconfirm.h"
@ -55,8 +56,6 @@
#include <QWebFrame>
QString WebPage::s_lastUploadLocation = QDir::homePath();
QString WebPage::s_userAgent;
QString WebPage::s_fakeUserAgent;
QUrl WebPage::s_lastUnsupportedUrl;
QTime WebPage::s_lastUnsupportedUrlTime;
QList<WebPage*> WebPage::s_livingPages;
@ -180,11 +179,6 @@ bool WebPage::isLoading() const
return m_loadProgress < 100;
}
void WebPage::setUserAgent(const QString &agent)
{
s_userAgent = agent;
}
void WebPage::urlChanged(const QUrl &url)
{
Q_UNUSED(url)
@ -570,27 +564,18 @@ void WebPage::cleanBlockedObjects()
QString WebPage::userAgentForUrl(const QUrl &url) const
{
const QString &host = url.host();
QString userAgent = mApp->uaManager()->userAgentForUrl(url);
// Let Google services play nice with us
if (host.contains("google")) {
if (s_fakeUserAgent.isEmpty()) {
s_fakeUserAgent = QString("Mozilla/5.0 (%1) AppleWebKit/%2 (KHTML, like Gecko) Chrome/10.0 Safari/%2").arg(qz_buildSystem(), QupZilla::WEBKITVERSION);
}
return s_fakeUserAgent;
}
if (s_userAgent.isEmpty()) {
s_userAgent = QWebPage::userAgentForUrl(url);
if (userAgent.isEmpty()) {
userAgent = QWebPage::userAgentForUrl(url);
#ifdef Q_WS_MAC
#ifdef __i386__ || __x86_64__
m_userAgent.replace("PPC Mac OS X", "Intel Mac OS X");
userAgent.replace("PPC Mac OS X", "Intel Mac OS X");
#endif
#endif
}
return s_userAgent;
return userAgent;
}
bool WebPage::supportsExtension(Extension extension) const

View File

@ -74,7 +74,6 @@ public:
void addRejectedCerts(const QList<QSslCertificate> &certs);
bool containsRejectedCerts(const QList<QSslCertificate> &certs);
static void setUserAgent(const QString &agent);
QString userAgentForUrl(const QUrl &url) const;
static bool isPointerSafeToUse(WebPage* page);
@ -118,8 +117,6 @@ private:
void desktopServicesOpen(const QUrl &url);
static QString s_lastUploadLocation;
static QString s_userAgent;
static QString s_fakeUserAgent;
static QUrl s_lastUnsupportedUrl;
static QTime s_lastUnsupportedUrlTime;
static QList<WebPage*> s_livingPages;