File indexing completed on 2025-01-26 04:57:24

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "blockexternalresourcesurlinterceptor.h"
0008 using namespace WebEngineViewer;
0009 
0010 BlockExternalResourcesUrlInterceptor::BlockExternalResourcesUrlInterceptor(QObject *parent)
0011     : WebEngineViewer::NetworkPluginUrlInterceptorInterface(parent)
0012 {
0013 }
0014 
0015 BlockExternalResourcesUrlInterceptor::~BlockExternalResourcesUrlInterceptor() = default;
0016 
0017 void BlockExternalResourcesUrlInterceptor::setEnabled(bool enabled)
0018 {
0019     mEnabled = enabled;
0020 }
0021 
0022 bool BlockExternalResourcesUrlInterceptor::interceptRequest(const QUrl &url,
0023                                                             QWebEngineUrlRequestInfo::ResourceType resourceType,
0024                                                             QWebEngineUrlRequestInfo::NavigationType navigationType)
0025 {
0026     if (!mEnabled) {
0027         return false;
0028     }
0029     const QString scheme = url.scheme();
0030     if (scheme == QLatin1StringView("data") || scheme == QLatin1StringView("file")) {
0031         return false;
0032     }
0033 
0034     if (resourceType == QWebEngineUrlRequestInfo::ResourceTypeMedia || resourceType == QWebEngineUrlRequestInfo::ResourceTypePing
0035         || resourceType == QWebEngineUrlRequestInfo::ResourceTypePrefetch || resourceType == QWebEngineUrlRequestInfo::ResourceTypeFavicon
0036         || resourceType == QWebEngineUrlRequestInfo::ResourceTypeXhr || resourceType == QWebEngineUrlRequestInfo::ResourceTypeObject
0037         || resourceType == QWebEngineUrlRequestInfo::ResourceTypeScript || resourceType == QWebEngineUrlRequestInfo::ResourceTypeServiceWorker
0038         || resourceType == QWebEngineUrlRequestInfo::ResourceTypeSharedWorker || resourceType == QWebEngineUrlRequestInfo::ResourceTypeWorker
0039         || resourceType == QWebEngineUrlRequestInfo::ResourceTypeSubResource || resourceType == QWebEngineUrlRequestInfo::ResourceTypePluginResource
0040         || resourceType == QWebEngineUrlRequestInfo::ResourceTypeCspReport || resourceType == QWebEngineUrlRequestInfo::ResourceTypeUnknown) {
0041         return true;
0042     } else if (navigationType == QWebEngineUrlRequestInfo::NavigationTypeFormSubmitted) {
0043         Q_EMIT formSubmittedForbidden();
0044         return true;
0045     } else if (navigationType == QWebEngineUrlRequestInfo::NavigationTypeReload || navigationType == QWebEngineUrlRequestInfo::NavigationTypeTyped
0046                || navigationType == QWebEngineUrlRequestInfo::NavigationTypeBackForward || navigationType == QWebEngineUrlRequestInfo::NavigationTypeOther
0047                || navigationType == QWebEngineUrlRequestInfo::NavigationTypeRedirect) {
0048         return true;
0049     }
0050     return false;
0051 }
0052 
0053 bool BlockExternalResourcesUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
0054 {
0055     const QWebEngineUrlRequestInfo::ResourceType resourceType = info.resourceType();
0056     const QWebEngineUrlRequestInfo::NavigationType navigationType = info.navigationType();
0057 
0058     return interceptRequest(info.requestUrl(), resourceType, navigationType);
0059 }
0060 
0061 #include "moc_blockexternalresourcesurlinterceptor.cpp"