]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/interceptors/UrlRequestInterceptor.cpp
Partial filter list implementation.
[PrivacyBrowserPC.git] / src / interceptors / UrlRequestInterceptor.cpp
1 /*
2  * Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc/>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "UrlRequestInterceptor.h"
22 #include "GlobalVariables.h"
23 #include "helpers/FilterListHelper.h"
24 #include "structs/RequestStruct.h"
25
26 // KDE Framework headers.
27 #include <KLocalizedString>
28
29 // Construct the class.
30 UrlRequestInterceptor::UrlRequestInterceptor(QObject *parentObjectPointer) : QWebEngineUrlRequestInterceptor(parentObjectPointer) {}
31
32 void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlRequestInfo)
33 {
34     // Clear the requests list if a main frame resource is being loaded.
35     if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
36         emit newMainFrameResource();
37
38     // Create a requests struct.
39     RequestStruct *requestStructPointer = new RequestStruct;
40
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();
46
47     // Check the filter lists.
48     bool requestNotHandled = globalFilterListHelperPointer->checkFilterLists(urlRequestInfo, requestStructPointer);
49
50     // Further process the request if it hasn't already been handled.
51     if (requestNotHandled) {
52         // Handle the request according to the resource type.
53         switch (urlRequestInfo.resourceType())
54         {
55             // A naughty HTTP ping request.
56             case QWebEngineUrlRequestInfo::ResourceTypePing:
57             {
58                 // Block the HTTP ping request.
59                 urlRequestInfo.block(true);
60
61                 // Mark the request struct as blocked.
62                 requestStructPointer->dispositionInt = FilterListHelper::BLOCKED;
63
64                 // Display the HTTP Ping blocked dialog.
65                 emit displayHttpPingDialog(urlRequestInfo.requestUrl().toString());
66
67                 break;
68             }
69
70             default:
71             {
72                 // Do nothing.
73                 break;
74             }
75         }
76
77         // Handle the request according to the navigation type.
78         switch (urlRequestInfo.navigationType())
79         {
80             case QWebEngineUrlRequestInfo::NavigationTypeLink:
81             case QWebEngineUrlRequestInfo::NavigationTypeTyped:
82             case QWebEngineUrlRequestInfo::NavigationTypeBackForward:
83             // case QWebEngineUrlRequestInfo::NavigationTypeReload:  This can be uncommented once https://redmine.stoutner.com/issues/821 has been fixed.
84             case QWebEngineUrlRequestInfo::NavigationTypeRedirect:
85             {
86                 // Only check the hosts if the main URL is changing.
87                 if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
88                 {
89                     // Get the hosts.
90                     QString requestingHost = urlRequestInfo.initiator().host();
91                     QString requestedHost = urlRequestInfo.requestUrl().host();
92
93                     // Reapply the domain settings if the host is changing.
94                     if (requestingHost != requestedHost)
95                         emit applyDomainSettings(requestedHost);
96                 }
97
98                 break;
99             }
100
101             default:
102                 // Do nothing.
103                 break;
104         }
105     }
106
107     // Send the request struct to the privacy WebEngine view.
108     emit requestProcessed(requestStructPointer);
109 }