File indexing completed on 2024-05-12 04:57:51

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 
0019 #include "adblockurlinterceptor.h"
0020 #include "adblockrule.h"
0021 #include "qztools.h"
0022 
0023 #include <QUrlQuery>
0024 
0025 AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
0026     : UrlInterceptor(manager)
0027     , m_manager(manager)
0028 {
0029 }
0030 
0031 void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request)
0032 {
0033     QString ruleFilter;
0034     QString ruleSubscription;
0035     if (!m_manager->block(request, ruleFilter, ruleSubscription)) {
0036         return;
0037     }
0038 
0039     if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
0040         QString url = QSL("qrc:adblock/data/adblock.html?direction=%DIRECTION%&title=%1&rule=%3").arg(
0041             tr("Blocked content"),
0042             tr("Blocked by <i>%1 (%2)</i>").arg(ruleFilter, ruleSubscription)
0043         );
0044         url = QzTools::applyDirectionToPage(url);
0045 
0046         request.redirect(QUrl(url));
0047     } else {
0048         request.block(true);
0049     }
0050 
0051     AdBlockedRequest r;
0052     r.requestUrl = request.requestUrl();
0053     r.firstPartyUrl = request.firstPartyUrl();
0054     r.requestMethod = request.requestMethod();
0055     r.resourceType = request.resourceType();
0056     r.navigationType = request.navigationType();
0057     r.rule = ruleFilter;
0058     Q_EMIT requestBlocked(r);
0059 }