Messagelib

blockexternalresourcesurlinterceptor.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "blockexternalresourcesurlinterceptor.h"
8using namespace WebEngineViewer;
9
10BlockExternalResourcesUrlInterceptor::BlockExternalResourcesUrlInterceptor(QObject *parent)
11 : WebEngineViewer::NetworkPluginUrlInterceptorInterface(parent)
12{
13}
14
15BlockExternalResourcesUrlInterceptor::~BlockExternalResourcesUrlInterceptor() = default;
16
17void BlockExternalResourcesUrlInterceptor::setEnabled(bool enabled)
18{
19 mEnabled = enabled;
20}
21
22bool BlockExternalResourcesUrlInterceptor::interceptRequest(const QUrl &url,
23 QWebEngineUrlRequestInfo::ResourceType resourceType,
24 QWebEngineUrlRequestInfo::NavigationType navigationType)
25{
26 if (!mEnabled) {
27 return false;
28 }
29 const QString scheme = url.scheme();
30 if (scheme == QLatin1StringView("data") || scheme == QLatin1StringView("file")) {
31 return false;
32 }
33
34 if (resourceType == QWebEngineUrlRequestInfo::ResourceTypeMedia || resourceType == QWebEngineUrlRequestInfo::ResourceTypePing
35 || resourceType == QWebEngineUrlRequestInfo::ResourceTypePrefetch || resourceType == QWebEngineUrlRequestInfo::ResourceTypeFavicon
36 || resourceType == QWebEngineUrlRequestInfo::ResourceTypeXhr || resourceType == QWebEngineUrlRequestInfo::ResourceTypeObject
37 || resourceType == QWebEngineUrlRequestInfo::ResourceTypeScript || resourceType == QWebEngineUrlRequestInfo::ResourceTypeServiceWorker
38 || resourceType == QWebEngineUrlRequestInfo::ResourceTypeSharedWorker || resourceType == QWebEngineUrlRequestInfo::ResourceTypeWorker
39 || resourceType == QWebEngineUrlRequestInfo::ResourceTypeSubResource || resourceType == QWebEngineUrlRequestInfo::ResourceTypePluginResource
40 || resourceType == QWebEngineUrlRequestInfo::ResourceTypeCspReport || resourceType == QWebEngineUrlRequestInfo::ResourceTypeUnknown) {
41 return true;
42 } else if (navigationType == QWebEngineUrlRequestInfo::NavigationTypeFormSubmitted) {
43 Q_EMIT formSubmittedForbidden();
44 return true;
45 } else if (navigationType == QWebEngineUrlRequestInfo::NavigationTypeReload || navigationType == QWebEngineUrlRequestInfo::NavigationTypeTyped
46 || navigationType == QWebEngineUrlRequestInfo::NavigationTypeBackForward || navigationType == QWebEngineUrlRequestInfo::NavigationTypeOther
47 || navigationType == QWebEngineUrlRequestInfo::NavigationTypeRedirect) {
48 return true;
49 }
50 return false;
51}
52
53bool BlockExternalResourcesUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
54{
55 const QWebEngineUrlRequestInfo::ResourceType resourceType = info.resourceType();
56 const QWebEngineUrlRequestInfo::NavigationType navigationType = info.navigationType();
57
58 return interceptRequest(info.requestUrl(), resourceType, navigationType);
59}
60
61#include "moc_blockexternalresourcesurlinterceptor.cpp"
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
QString scheme() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.