]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/PrivacyWebEngineView.cpp
Partial filter list implementation.
[PrivacyBrowserPC.git] / src / widgets / PrivacyWebEngineView.cpp
index 05c765925d8aa6da639c4f4f019634ffc3d956f5..15ee508b74eeaac03ad3d3e7c4e4c894b516edb6 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
  *
- * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ * 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
 // Application headers.
 #include "PrivacyWebEngineView.h"
 #include "Settings.h"
+#include "ui_HttpAuthenticationDialog.h"
 #include "databases/CookiesDatabase.h"
 #include "databases/DomainsDatabase.h"
+#include "dialogs/HttpAuthenticationDialog.h"
+#include "helpers/FilterListHelper.h"
 #include "interceptors/UrlRequestInterceptor.h"
 #include "windows/BrowserWindow.h"
 
@@ -50,11 +53,14 @@ PrivacyWebEngineView::PrivacyWebEngineView(QWidget *parentWidgetPointer) : QWebE
     // Set the URL request interceptor.
     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
 
-    // Reapply the domain settings when the host changes.
+    // Connect the URL request interceptor signals.
     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(const QString&)), this, SLOT(applyDomainSettingsWithoutReloading(const QString&)));
-
-    // Display HTTP Ping blocked dialogs.
+    connect(urlRequestInterceptorPointer, SIGNAL(newMainFrameResource()), this, SLOT(clearRequestsList()));
     connect(urlRequestInterceptorPointer, SIGNAL(displayHttpPingDialog(const QString&)), this, SLOT(displayHttpPingDialog(const QString&)));
+    connect(urlRequestInterceptorPointer, SIGNAL(requestProcessed(RequestStruct*)), this, SLOT(storeRequest(RequestStruct*)));
+
+    // Handle HTTP authentication requests.
+    connect(webEnginePagePointer, SIGNAL(authenticationRequired(const QUrl&, QAuthenticator*)), this, SLOT(handleAuthenticationRequest(const QUrl&, QAuthenticator*)));
 }
 
 void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
@@ -69,7 +75,7 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
         CookiesDatabase::updateCookie(cookie);
 
     // Update the cookies action.
-    emit updateCookiesAction(cookieListPointer->size());
+    emit numberOfCookiesChanged(cookieListPointer->size());
 }
 
 void PrivacyWebEngineView::applyDomainSettingsWithoutReloading(const QString &hostname)
@@ -216,10 +222,25 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo
     if (reloadWebsite)
         reload();
 
+    // Reset the HTTP authentication dialog counter.
+    httpAuthenticationDialogsDisplayed = 0;
+
     // Update the UI.
     emit updateUi(this);
 }
 
+void PrivacyWebEngineView::clearRequestsList()
+{
+    // Reset the number of blocked requests.
+    blockedRequests = 0;
+
+    // Clear the requests list.
+    requestsListPointer->clear();
+
+    // Update the blocked requests action.
+    emit(requestBlocked(blockedRequests));
+}
+
 void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
     // Get a handle for the
     QWebEnginePage *webEnginePagePointer = page();
@@ -298,6 +319,21 @@ void PrivacyWebEngineView::displayHttpPingDialog(const QString &httpPingUrl) con
     emit displayHttpPingBlockedDialog(httpPingUrl);
 }
 
+void PrivacyWebEngineView::handleAuthenticationRequest(const QUrl &requestUrl, QAuthenticator *authenticatorPointer)
+{
+    // Only display the HTTP authentication dialog if it hasn't already been displayed three times for this URL.
+    if (httpAuthenticationDialogsDisplayed < 3) {
+        // Increment the HTTP authentication dialog display counter.
+        ++httpAuthenticationDialogsDisplayed;
+
+        // Instantiate an HTTP authentication dialog.
+        HttpAuthenticationDialog *httpAuthenticationDialogPointer = new HttpAuthenticationDialog(parentWidget(), requestUrl, authenticatorPointer);
+
+        // Display the dialog.  This must be `exec()` instead of `show()` so that the website doesn't proceed before populating the authentication pointer.
+        httpAuthenticationDialogPointer->exec();
+    }
+}
+
 void PrivacyWebEngineView::removeCookieFromList(const QNetworkCookie &cookie) const
 {
     //qDebug() << "Remove cookie:  " << cookie.toRawForm();
@@ -306,5 +342,21 @@ void PrivacyWebEngineView::removeCookieFromList(const QNetworkCookie &cookie) co
     cookieListPointer->remove(cookie);
 
     // Update the cookies action.
-    emit updateCookiesAction(cookieListPointer->size());
+    emit numberOfCookiesChanged(cookieListPointer->size());
+}
+
+void PrivacyWebEngineView::storeRequest(RequestStruct *requestStructPointer)
+{
+    // Store the request struct in the list.
+    requestsListPointer->append(requestStructPointer);
+
+    // Track blocked requests.
+    if (requestStructPointer->dispositionInt == FilterListHelper::BLOCKED)
+    {
+        // Update the blocked requests counter.
+        ++blockedRequests;
+
+        // Update the blocked requests action.
+        emit(requestBlocked(blockedRequests));
+    }
 }