mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
NetworkUrlInterceptor: Add proper locking
This commit is contained in:
parent
394221c5c0
commit
9c17277dd5
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2015-2017 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -29,10 +29,34 @@ NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent)
|
||||
|
||||
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
|
||||
{
|
||||
if (m_sendDNT)
|
||||
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
|
||||
m_mutex.lock();
|
||||
|
||||
info.setHttpHeader(QByteArrayLiteral("User-Agent"), mApp->userAgentManager()->userAgentForUrl(info.firstPartyUrl()).toUtf8());
|
||||
if (m_sendDNT) {
|
||||
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
|
||||
}
|
||||
|
||||
const QString host = info.firstPartyUrl().host();
|
||||
|
||||
if (m_usePerDomainUserAgent) {
|
||||
QString userAgent;
|
||||
if (m_userAgentsList.contains(host)) {
|
||||
userAgent = m_userAgentsList.value(host);
|
||||
} else {
|
||||
QHashIterator<QString, QString> i(m_userAgentsList);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
if (host.endsWith(i.key())) {
|
||||
userAgent = i.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!userAgent.isEmpty()) {
|
||||
info.setHttpHeader(QByteArrayLiteral("User-Agent"), userAgent.toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
m_mutex.unlock();
|
||||
|
||||
foreach (UrlInterceptor *interceptor, m_interceptors) {
|
||||
interceptor->interceptRequest(info);
|
||||
@ -52,8 +76,15 @@ void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor)
|
||||
|
||||
void NetworkUrlInterceptor::loadSettings()
|
||||
{
|
||||
m_mutex.lock();
|
||||
|
||||
Settings settings;
|
||||
settings.beginGroup("Web-Browser-Settings");
|
||||
m_sendDNT = settings.value("DoNotTrack", false).toBool();
|
||||
settings.endGroup();
|
||||
|
||||
m_usePerDomainUserAgent = mApp->userAgentManager()->usePerDomainUserAgents();
|
||||
m_userAgentsList = mApp->userAgentManager()->perDomainUserAgentsList();
|
||||
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2015 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -19,6 +19,7 @@
|
||||
#ifndef NETWORKURLINTERCEPTOR_H
|
||||
#define NETWORKURLINTERCEPTOR_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QWebEngineUrlRequestInterceptor>
|
||||
|
||||
#include "qzcommon.h"
|
||||
@ -38,8 +39,11 @@ public:
|
||||
void loadSettings();
|
||||
|
||||
private:
|
||||
QMutex m_mutex;
|
||||
QList<UrlInterceptor*> m_interceptors;
|
||||
bool m_sendDNT;
|
||||
bool m_sendDNT = false;
|
||||
bool m_usePerDomainUserAgent = false;
|
||||
QHash<QString, QString> m_userAgentsList;
|
||||
};
|
||||
|
||||
#endif // NETWORKURLINTERCEPTOR_H
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -26,12 +26,12 @@
|
||||
#include "settings.h"
|
||||
#include "datapaths.h"
|
||||
#include "iconprovider.h"
|
||||
#include "useragentmanager.h"
|
||||
#include "sessionmanager.h"
|
||||
|
||||
#include <QTimer>
|
||||
#include <QSettings>
|
||||
#include <QUrlQuery>
|
||||
#include <QWebEngineProfile>
|
||||
#include <QWebEngineUrlRequestJob>
|
||||
|
||||
static QString authorString(const char* name, const QString &mail)
|
||||
@ -409,7 +409,7 @@ QString FalkonSchemeReply::configPage()
|
||||
}
|
||||
|
||||
QString page = cPage;
|
||||
page.replace(QLatin1String("%USER-AGENT%"), mApp->userAgentManager()->userAgentForUrl(QUrl()));
|
||||
page.replace(QLatin1String("%USER-AGENT%"), mApp->webProfile()->httpUserAgent());
|
||||
|
||||
QString pluginsString;
|
||||
const QList<Plugins::Plugin> &availablePlugins = mApp->plugins()->getAvailablePlugins();
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2015 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -26,6 +26,8 @@ class UrlInterceptor : public QObject
|
||||
{
|
||||
public:
|
||||
explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { }
|
||||
|
||||
// Runs on IO thread!
|
||||
virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0;
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -56,26 +56,6 @@ void UserAgentManager::loadSettings()
|
||||
QWebEngineProfile::defaultProfile()->setHttpUserAgent(userAgent);
|
||||
}
|
||||
|
||||
QString UserAgentManager::userAgentForUrl(const QUrl &url) const
|
||||
{
|
||||
const QString host = url.host();
|
||||
|
||||
if (m_usePerDomainUserAgent) {
|
||||
if (m_userAgentsList.contains(host)) {
|
||||
return m_userAgentsList.value(host);
|
||||
}
|
||||
QHashIterator<QString, QString> i(m_userAgentsList);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
if (host.endsWith(i.key())) {
|
||||
return i.value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QWebEngineProfile::defaultProfile()->httpUserAgent();
|
||||
}
|
||||
|
||||
QString UserAgentManager::globalUserAgent() const
|
||||
{
|
||||
return m_globalUserAgent;
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2010-2015 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -34,8 +34,6 @@ public:
|
||||
|
||||
void loadSettings();
|
||||
|
||||
QString userAgentForUrl(const QUrl &url) const;
|
||||
|
||||
QString globalUserAgent() const;
|
||||
QString defaultUserAgent() const;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
||||
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -21,6 +21,7 @@
|
||||
#include "qztools.h"
|
||||
#include "mainapplication.h"
|
||||
#include "settings.h"
|
||||
#include "networkmanager.h"
|
||||
|
||||
#include <QFormLayout>
|
||||
#include <QLineEdit>
|
||||
@ -177,6 +178,7 @@ void UserAgentDialog::accept()
|
||||
settings.endGroup();
|
||||
|
||||
m_manager->loadSettings();
|
||||
mApp->networkManager()->loadSettings();
|
||||
close();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user