Falkon Develop
Cross-platform Qt-based web browser
networkurlinterceptor.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "urlinterceptor.h"
20#include "settings.h"
21#include "mainapplication.h"
22#include "useragentmanager.h"
23
24#include <QMutexLocker>
25
27 : QWebEngineUrlRequestInterceptor(parent)
28 , m_sendDNT(false)
29{
30}
31
32void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
33{
34 QMutexLocker lock(&m_mutex);
35
36 if (m_sendDNT) {
37 info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
38 }
39
40 const QString host = info.firstPartyUrl().host();
41
42 if (m_usePerDomainUserAgent) {
43 QString userAgent;
44 if (m_userAgentsList.contains(host)) {
45 userAgent = m_userAgentsList.value(host);
46 } else {
47 QHashIterator<QString, QString> i(m_userAgentsList);
48 while (i.hasNext()) {
49 i.next();
50 if (host.endsWith(i.key())) {
51 userAgent = i.value();
52 break;
53 }
54 }
55 }
56 if (!userAgent.isEmpty()) {
57 info.setHttpHeader(QByteArrayLiteral("User-Agent"), userAgent.toUtf8());
58 }
59 }
60
61 for (UrlInterceptor *interceptor : std::as_const(m_interceptors)) {
62 interceptor->interceptRequest(info);
63 }
64}
65
67{
68 QMutexLocker lock(&m_mutex);
69
70 if (!m_interceptors.contains(interceptor)) {
71 m_interceptors.append(interceptor);
72 }
73}
74
76{
77 QMutexLocker lock(&m_mutex);
78
79 m_interceptors.removeOne(interceptor);
80}
81
83{
84 QMutexLocker lock(&m_mutex);
85
86 Settings settings;
87 settings.beginGroup(QSL("Web-Browser-Settings"));
88 m_sendDNT = settings.value(QSL("DoNotTrack"), false).toBool();
89 settings.endGroup();
90
91 m_usePerDomainUserAgent = mApp->userAgentManager()->usePerDomainUserAgents();
92 m_userAgentsList = mApp->userAgentManager()->perDomainUserAgentsList();
93}
void interceptRequest(QWebEngineUrlRequestInfo &info) Q_DECL_OVERRIDE
NetworkUrlInterceptor(QObject *parent=nullptr)
void installUrlInterceptor(UrlInterceptor *interceptor)
void removeUrlInterceptor(UrlInterceptor *interceptor)
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
#define mApp
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40