]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Reapply domain settings when the host changes.
authorSoren Stoutner <soren@stoutner.com>
Tue, 15 Feb 2022 19:29:49 +0000 (12:29 -0700)
committerSoren Stoutner <soren@stoutner.com>
Tue, 15 Feb 2022 19:29:49 +0000 (12:29 -0700)
CMakeLists.txt
src/BrowserWindow.h
src/CMakeLists.txt
src/MainView.cpp
src/MainView.h
src/MouseEventFilter.h
src/UrlRequestInterceptor.cpp [new file with mode: 0644]
src/UrlRequestInterceptor.h [new file with mode: 0644]

index 87b77b50708d018e16e71d1da91ebbb84962d815..5a9c81b4d8a1164e39d8ce43bc44dd337db9676c 100644 (file)
@@ -46,6 +46,7 @@ include(FeatureSummary)
 find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
     Core
     Gui
 find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
     Core
     Gui
+    WebEngineCore
     WebEngineWidgets
     Widgets
 )
     WebEngineWidgets
     Widgets
 )
index 8d65827e1b781a99a8957faead12cfe6dac9e5f2..a8679ad4eab8a9eb04c14e65f1573abd48832fb5 100644 (file)
@@ -39,7 +39,7 @@ public:
     BrowserWindow();
 
 private Q_SLOTS:
     BrowserWindow();
 
 private Q_SLOTS:
-    // Define the private slots.
+    // The private slots.
     void fileNew() const;
     void settingsConfigure();
     void updateSearchEngineLabel(const QString &searchEngineString) const;
     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:
     void updateUserAgentLabel(const QString &userAgentName) const;
 
 private:
-    // Define the private variables.
+    // The private variables.
     MainView *mainViewPointer;
     QLabel *searchEngineLabelPointer;
     QLabel *userAgentLabelPointer;
     MainView *mainViewPointer;
     QLabel *searchEngineLabelPointer;
     QLabel *userAgentLabelPointer;
index 79526bf4ea6b772d6dbbc287075b3472458baf3d..d63efaf63864901b9ed8f927679120febd4724c0 100644 (file)
@@ -25,6 +25,7 @@ target_sources(privacy-browser PRIVATE
     main.cpp
     MainView.cpp
     MouseEventFilter.cpp
     main.cpp
     MainView.cpp
     MouseEventFilter.cpp
+    UrlRequestInterceptor.cpp
 )
 
 # Add the Qt logging category.  This will create the `debug.h` header file.
 )
 
 # 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::Core
     Qt5::Gui
     Qt5::Widgets
+    Qt5::WebEngineCore
     Qt5::WebEngineWidgets
     KF5::Completion
     KF5::ConfigWidgets
     Qt5::WebEngineWidgets
     KF5::Completion
     KF5::ConfigWidgets
index 9ddf1fb4524177e07627da213c047bb9d7b6354e..3bef1040a88bb160939b685cd2fbf10635f69844 100644 (file)
@@ -23,6 +23,7 @@
 #include "MouseEventFilter.h"
 #include "Settings.h"
 #include "ui_MainView.h"
 #include "MouseEventFilter.h"
 #include "Settings.h"
 #include "ui_MainView.h"
+#include "UrlRequestInterceptor.h"
 #include "helpers/SearchEngineHelper.h"
 #include "helpers/UserAgentHelper.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)));
 
     // 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);
 
     // 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
 {
 // 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);
 }
 
     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.
 void MainView::applyDomainSettings(bool reloadWebsite) const
 {
     // Set the JavaScript status.
index 4509c66cf52efaf8aec13cd0f3fbf2f14cefccc1..17c189dee5842be8256c228492f1a5869c18ed47 100644 (file)
@@ -39,16 +39,17 @@ public:
     explicit MainView(QWidget *parent);
 
 signals:
     explicit MainView(QWidget *parent);
 
 signals:
-    // Define the signals.
+    // The signals.
     void linkHovered(const QString &linkUrl) const;
 
 public Q_SLOTS:
     void linkHovered(const QString &linkUrl) const;
 
 public Q_SLOTS:
-    // Define the public slots.
+    // The public slots.
     void applyApplicationSettings() const;
     void applyDomainSettingsAndReload() const;
     void applyApplicationSettings() const;
     void applyDomainSettingsAndReload() const;
+    void applyDomainSettingsWithoutReloading() const;
 
 private Q_SLOTS:
 
 private Q_SLOTS:
-    // Define the private slots.
+    // The private slots.
     void goHome() const;
     void loadUrlFromTextBox(QString urlFromUser) const;
     void pageLinkHovered(const QString &linkUrl) const;
     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:
     void updateInterface() const;
 
 private:
-    // Define the private variables.
+    // The private variables.
     QPushButton *backButtonPointer;
     QPushButton *forwardButtonPointer;
     QPushButton *javaScriptButtonPointer;
     QPushButton *backButtonPointer;
     QPushButton *forwardButtonPointer;
     QPushButton *javaScriptButtonPointer;
@@ -66,7 +67,7 @@ private:
     QWebEngineSettings *webEngineSettingsPointer;
     QWebEngineView *webEngineViewPointer;
 
     QWebEngineSettings *webEngineSettingsPointer;
     QWebEngineView *webEngineViewPointer;
 
-    // Define the private functions.
+    // The private functions.
     void applyDomainSettings(bool reloadWebsite) const;
 };
 #endif
     void applyDomainSettings(bool reloadWebsite) const;
 };
 #endif
index 82cee6f7b2b224272b895f80bf1a86222addf5e6..92c015afedc673983eb87d28d2beb1be25c8daea 100644 (file)
@@ -34,10 +34,11 @@ public:
     MouseEventFilter(QWebEngineView *webEngineView);
 
 protected:
     MouseEventFilter(QWebEngineView *webEngineView);
 
 protected:
+    // The protected functions.
     bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override;
 
 private:
     bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override;
 
 private:
-    // Define the private variables.
+    // The private variables.
     QWebEngineView *webEngineViewPointer;
 };
 #endif
     QWebEngineView *webEngineViewPointer;
 };
 #endif
diff --git a/src/UrlRequestInterceptor.cpp b/src/UrlRequestInterceptor.cpp
new file mode 100644 (file)
index 0000000..94a88aa
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/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 <http://www.gnu.org/licenses/>.
+ */
+
+// 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 (file)
index 0000000..8f3306b
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef URLREQUESTINTERCEPTOR_H
+#define URLREQUESTINTERCEPTOR_H
+
+// Qt framework headers.
+#include <QtWebEngineCore>
+
+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