Messagelib

blockexternalresourcesurlinterceptor.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "blockexternalresourcesurlinterceptor.h"
8 using namespace WebEngineViewer;
9 
10 BlockExternalResourcesUrlInterceptor::BlockExternalResourcesUrlInterceptor(QObject *parent)
12 {
13 }
14 
15 BlockExternalResourcesUrlInterceptor::~BlockExternalResourcesUrlInterceptor() = default;
16 
17 void BlockExternalResourcesUrlInterceptor::setEnabled(bool enabled)
18 {
19  mEnabled = enabled;
20 }
21 
22 bool 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 == QLatin1String("data") || scheme == QLatin1String("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 
53 bool 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 }
QString scheme() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.