2 * Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc/>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "UrlRequestInterceptor.h"
22 #include "GlobalVariables.h"
23 #include "helpers/FilterListHelper.h"
24 #include "structs/RequestStruct.h"
26 // KDE Framework headers.
27 #include <KLocalizedString>
29 // Construct the class.
30 UrlRequestInterceptor::UrlRequestInterceptor(QObject *parentObjectPointer) : QWebEngineUrlRequestInterceptor(parentObjectPointer) {}
32 void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlRequestInfo)
34 // Clear the requests list if a main frame resource is being loaded.
35 if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
36 emit newMainFrameResource();
38 // Create a requests struct.
39 RequestStruct *requestStructPointer = new RequestStruct;
41 // Store the basic request information.
42 requestStructPointer->navigationTypeInt = urlRequestInfo.navigationType();
43 requestStructPointer->requestMethodString = urlRequestInfo.requestMethod();
44 requestStructPointer->resourceTypeInt = urlRequestInfo.resourceType();
45 requestStructPointer->urlString = urlRequestInfo.requestUrl().toString();
47 // Check the filter lists.
48 bool continueProcessing = globalFilterListHelperPointer->checkFilterLists(urlRequestInfo, requestStructPointer);
50 // Further process the request if it hasn't already been handled.
51 if (continueProcessing) {
52 // Handle the request according to the resource type.
53 switch (urlRequestInfo.resourceType())
55 // A naughty HTTP ping request.
56 case QWebEngineUrlRequestInfo::ResourceTypePing:
58 // Block the HTTP ping request.
59 urlRequestInfo.block(true);
61 // Mark the request struct as blocked.
62 requestStructPointer->dispositionInt = FilterListHelper::BLOCKED;
64 // Mark the ping as blocked by the default behavior.
65 requestStructPointer->filterListTitle = i18nc("Default HTTP ping blocking", "Default blocking of all HTTP ping requests.");
67 // Display the HTTP Ping blocked dialog.
68 emit displayHttpPingDialog(urlRequestInfo.requestUrl().toString());
80 // Handle the request according to the navigation type.
81 switch (urlRequestInfo.navigationType())
83 case QWebEngineUrlRequestInfo::NavigationTypeLink:
84 case QWebEngineUrlRequestInfo::NavigationTypeTyped:
85 case QWebEngineUrlRequestInfo::NavigationTypeBackForward:
86 // case QWebEngineUrlRequestInfo::NavigationTypeReload: This can be uncommented once https://redmine.stoutner.com/issues/821 has been fixed.
87 case QWebEngineUrlRequestInfo::NavigationTypeRedirect:
89 // Only check the hosts if the main URL is changing.
90 if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
93 QString requestingHost = urlRequestInfo.initiator().host();
94 QString requestedHost = urlRequestInfo.requestUrl().host();
96 // Reapply the domain settings if the host is changing.
97 if (requestingHost != requestedHost)
98 emit applyDomainSettings(requestedHost);
110 // Send the request struct to the privacy WebEngine view.
111 emit requestProcessed(requestStructPointer);