File indexing completed on 2024-05-12 04:58:10

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "networkurlinterceptor.h"
0019 #include "urlinterceptor.h"
0020 #include "settings.h"
0021 #include "mainapplication.h"
0022 #include "useragentmanager.h"
0023 
0024 #include <QMutexLocker>
0025 
0026 NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent)
0027     : QWebEngineUrlRequestInterceptor(parent)
0028     , m_sendDNT(false)
0029 {
0030 }
0031 
0032 void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
0033 {
0034     QMutexLocker lock(&m_mutex);
0035 
0036     if (m_sendDNT) {
0037         info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
0038     }
0039 
0040     const QString host = info.firstPartyUrl().host();
0041 
0042     if (m_usePerDomainUserAgent) {
0043         QString userAgent;
0044         if (m_userAgentsList.contains(host)) {
0045             userAgent = m_userAgentsList.value(host);
0046         } else {
0047             QHashIterator<QString, QString> i(m_userAgentsList);
0048             while (i.hasNext()) {
0049                 i.next();
0050                 if (host.endsWith(i.key())) {
0051                     userAgent = i.value();
0052                     break;
0053                 }
0054             }
0055         }
0056         if (!userAgent.isEmpty()) {
0057             info.setHttpHeader(QByteArrayLiteral("User-Agent"), userAgent.toUtf8());
0058         }
0059     }
0060 
0061     for (UrlInterceptor *interceptor : std::as_const(m_interceptors)) {
0062         interceptor->interceptRequest(info);
0063     }
0064 }
0065 
0066 void NetworkUrlInterceptor::installUrlInterceptor(UrlInterceptor *interceptor)
0067 {
0068     QMutexLocker lock(&m_mutex);
0069 
0070     if (!m_interceptors.contains(interceptor)) {
0071         m_interceptors.append(interceptor);
0072     }
0073 }
0074 
0075 void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor)
0076 {
0077     QMutexLocker lock(&m_mutex);
0078 
0079     m_interceptors.removeOne(interceptor);
0080 }
0081 
0082 void NetworkUrlInterceptor::loadSettings()
0083 {
0084     QMutexLocker lock(&m_mutex);
0085 
0086     Settings settings;
0087     settings.beginGroup(QSL("Web-Browser-Settings"));
0088     m_sendDNT = settings.value(QSL("DoNotTrack"), false).toBool();
0089     settings.endGroup();
0090 
0091     m_usePerDomainUserAgent = mApp->userAgentManager()->usePerDomainUserAgents();
0092     m_userAgentsList = mApp->userAgentManager()->perDomainUserAgentsList();
0093 }