From: Soren Stoutner Date: Tue, 15 Feb 2022 19:29:49 +0000 (-0700) Subject: Reapply domain settings when the host changes. X-Git-Tag: v0.1~59 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=44cd064cb213ad693223ca117fe346d8b78456d6;p=PrivacyBrowserPC.git Reapply domain settings when the host changes. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 87b77b5..5a9c81b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ include(FeatureSummary) find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Core Gui + WebEngineCore WebEngineWidgets Widgets ) diff --git a/src/BrowserWindow.h b/src/BrowserWindow.h index 8d65827..a8679ad 100644 --- a/src/BrowserWindow.h +++ b/src/BrowserWindow.h @@ -39,7 +39,7 @@ public: BrowserWindow(); private Q_SLOTS: - // Define the private slots. + // The private slots. void fileNew() const; void settingsConfigure(); void updateSearchEngineLabel(const QString &searchEngineString) const; @@ -47,7 +47,7 @@ private Q_SLOTS: void updateUserAgentLabel(const QString &userAgentName) const; private: - // Define the private variables. + // The private variables. MainView *mainViewPointer; QLabel *searchEngineLabelPointer; QLabel *userAgentLabelPointer; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 79526bf..d63efaf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,7 @@ target_sources(privacy-browser PRIVATE main.cpp MainView.cpp MouseEventFilter.cpp + UrlRequestInterceptor.cpp ) # Add the Qt logging category. This will create the `debug.h` header file. @@ -49,6 +50,7 @@ target_link_libraries(privacy-browser Qt5::Core Qt5::Gui Qt5::Widgets + Qt5::WebEngineCore Qt5::WebEngineWidgets KF5::Completion KF5::ConfigWidgets diff --git a/src/MainView.cpp b/src/MainView.cpp index 9ddf1fb..3bef104 100644 --- a/src/MainView.cpp +++ b/src/MainView.cpp @@ -23,6 +23,7 @@ #include "MouseEventFilter.h" #include "Settings.h" #include "ui_MainView.h" +#include "UrlRequestInterceptor.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" @@ -76,6 +77,15 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Listen for hovered link URLs. connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString))); + // Instantiate the URL request interceptor. + UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor(); + + // Set the URL request interceptor. + webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer); + + // Reapply the domain settings when the host changes. + connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading())); + // Disable the cache. webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache); @@ -118,10 +128,17 @@ void MainView::applyApplicationSettings() const // This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument. void MainView::applyDomainSettingsAndReload() const { - // Apply the domain setings. `true` reloads the website. + // Apply the domain settings. `true` reloads the website. applyDomainSettings(true); } +// This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument. +void MainView::applyDomainSettingsWithoutReloading() const +{ + // Apply the domain settings `false` does not reload the website. + applyDomainSettings(false); +} + void MainView::applyDomainSettings(bool reloadWebsite) const { // Set the JavaScript status. diff --git a/src/MainView.h b/src/MainView.h index 4509c66..17c189d 100644 --- a/src/MainView.h +++ b/src/MainView.h @@ -39,16 +39,17 @@ public: explicit MainView(QWidget *parent); signals: - // Define the signals. + // The signals. void linkHovered(const QString &linkUrl) const; public Q_SLOTS: - // Define the public slots. + // The public slots. void applyApplicationSettings() const; void applyDomainSettingsAndReload() const; + void applyDomainSettingsWithoutReloading() const; private Q_SLOTS: - // Define the private slots. + // The private slots. void goHome() const; void loadUrlFromTextBox(QString urlFromUser) const; void pageLinkHovered(const QString &linkUrl) const; @@ -56,7 +57,7 @@ private Q_SLOTS: void updateInterface() const; private: - // Define the private variables. + // The private variables. QPushButton *backButtonPointer; QPushButton *forwardButtonPointer; QPushButton *javaScriptButtonPointer; @@ -66,7 +67,7 @@ private: QWebEngineSettings *webEngineSettingsPointer; QWebEngineView *webEngineViewPointer; - // Define the private functions. + // The private functions. void applyDomainSettings(bool reloadWebsite) const; }; #endif diff --git a/src/MouseEventFilter.h b/src/MouseEventFilter.h index 82cee6f..92c015a 100644 --- a/src/MouseEventFilter.h +++ b/src/MouseEventFilter.h @@ -34,10 +34,11 @@ public: MouseEventFilter(QWebEngineView *webEngineView); protected: + // The protected functions. bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override; private: - // Define the private variables. + // The private variables. QWebEngineView *webEngineViewPointer; }; #endif diff --git a/src/UrlRequestInterceptor.cpp b/src/UrlRequestInterceptor.cpp new file mode 100644 index 0000000..94a88aa --- /dev/null +++ b/src/UrlRequestInterceptor.cpp @@ -0,0 +1,50 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +// Application headers. +#include "UrlRequestInterceptor.h" + +// The default constructor. +UrlRequestInterceptor::UrlRequestInterceptor(QObject *parentObjectPointer) : QWebEngineUrlRequestInterceptor(parentObjectPointer) {} + +void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlRequestInfo) +{ + // Handle the request according to the resource type. + switch (urlRequestInfo.resourceType()) + { + case QWebEngineUrlRequestInfo::ResourceTypePing: + // Block HTTP ping requests. + urlRequestInfo.block(true); + break; + + default: + // Do nothing. + break; + } + + // Get the hosts. + QString requestingHost = urlRequestInfo.firstPartyUrl().host(); + QString requestedHost = urlRequestInfo.requestUrl().host(); + + // Reapply the domain settings if the host is changing. + if (requestingHost != requestedHost) + { + emit applyDomainSettings(); + } +} diff --git a/src/UrlRequestInterceptor.h b/src/UrlRequestInterceptor.h new file mode 100644 index 0000000..8f3306b --- /dev/null +++ b/src/UrlRequestInterceptor.h @@ -0,0 +1,42 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +#ifndef URLREQUESTINTERCEPTOR_H +#define URLREQUESTINTERCEPTOR_H + +// Qt framework headers. +#include + +class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor +{ + // Include the Q_OBJECT macro. + Q_OBJECT + +public: + // The default constructor. + UrlRequestInterceptor(QObject *parentObjectPointer = 0); + + // The public functions. + void interceptRequest(QWebEngineUrlRequestInfo &urlRequestInfo) override; + +signals: + // The signals. + void applyDomainSettings() const; +}; +#endif