]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/PrivacyWebEngineView.cpp
Fix HTTP authentication dialogs displaying indefinitely. https://redmine.stoutner...
[PrivacyBrowserPC.git] / src / widgets / PrivacyWebEngineView.cpp
index 8f61f9156acd62994e26da68eab4dbb845a8cc8d..42c331fec5340447ed05da6433a6475db30f7de4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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>.
  *
 // 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 "interceptors/UrlRequestInterceptor.h"
 #include "windows/BrowserWindow.h"
 
@@ -55,6 +57,9 @@ PrivacyWebEngineView::PrivacyWebEngineView(QWidget *parentWidgetPointer) : QWebE
 
     // Display HTTP Ping blocked dialogs.
     connect(urlRequestInterceptorPointer, SIGNAL(displayHttpPingDialog(const QString&)), this, SLOT(displayHttpPingDialog(const QString&)));
+
+    // Handle HTTP authentication requests.
+    connect(webEnginePagePointer, SIGNAL(authenticationRequired(const QUrl&, QAuthenticator*)), this, SLOT(handleAuthenticationRequest(const QUrl&, QAuthenticator*)));
 }
 
 void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
@@ -216,6 +221,9 @@ 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);
 }
@@ -259,9 +267,9 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType
     {
         case QWebEnginePage::WebBrowserTab:
         {
-            // Create the new tab and return the privacy WebEngine view pointer.  `true` removes the focus from the blank URL line edit.
+            // Create the new tab and return the privacy WebEngine view pointer.  `true` removes the focus from the blank URL line edit.  `true` adds the new tab adjacent to the current tab.
             // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
-            return browserWindowPointer->tabWidgetPointer->addTab(true);
+            return browserWindowPointer->tabWidgetPointer->addTab(true, true);
         }
 
         case QWebEnginePage::WebBrowserWindow:
@@ -278,9 +286,10 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType
 
         case QWebEnginePage::WebBrowserBackgroundTab:
         {
-            // Create the new tab and return the privacy WebEngine view pointer.  `false` does not clear the URL line edit.  `true` creates a background tab.
+            // Create the new tab and return the privacy WebEngine view pointer.  `false` does not clear the URL line edit.  `true` adds the new tab adjacent to the current tab.
+            // `true` creates a background tab.
             // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
-            return browserWindowPointer->tabWidgetPointer->addTab(false, true);
+            return browserWindowPointer->tabWidgetPointer->addTab(false, true, true);
         }
 
         default:
@@ -297,6 +306,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();