X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fwidgets%2FPrivacyWebEngineView.cpp;h=ba56d31d76c6827f9b3e1e60927c756f9ed4e896;hb=refs%2Fheads%2Fmaster;hp=96a0da5d67735743225293e0ca6a8db1990e5922;hpb=3108332092c1c2807f1e13c417c487fd07aed177;p=PrivacyBrowserPC.git diff --git a/src/widgets/PrivacyWebEngineView.cpp b/src/widgets/PrivacyWebEngineView.cpp index 96a0da5..15ee508 100644 --- a/src/widgets/PrivacyWebEngineView.cpp +++ b/src/widgets/PrivacyWebEngineView.cpp @@ -1,7 +1,7 @@ /* - * Copyright 2022-2023 Soren Stoutner . + * Copyright 2022-2024 Soren Stoutner . * - * This file is part of Privacy Browser PC . + * 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 @@ -20,8 +20,11 @@ // 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" @@ -30,7 +33,7 @@ #include // Construct the class. -PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) +PrivacyWebEngineView::PrivacyWebEngineView(QWidget *parentWidgetPointer) : QWebEngineView(parentWidgetPointer) { // Create an off-the-record profile (the default when no profile name is specified). webEngineProfilePointer = new QWebEngineProfile(QLatin1String("")); @@ -45,16 +48,19 @@ PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) webEngineSettingsPointer = webEnginePagePointer->settings(); // Instantiate the URL request interceptor. - UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor(); + UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor(this); // 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) @@ -86,14 +92,11 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo // Check if the hostname has domain settings. if (domainQuery.isValid()) // The hostname has domain settings. { - // Get the domain record. - QSqlRecord domainRecord = domainQuery.record(); - // Store the domain settings name. - domainSettingsName = domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString(); + domainSettingsName = domainQuery.value(DomainsDatabase::DOMAIN_NAME).toString(); // Set the JavaScript status. - switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt()) + switch (domainQuery.value(DomainsDatabase::JAVASCRIPT).toInt()) { // Set the default JavaScript status. case (DomainsDatabase::SYSTEM_DEFAULT): @@ -103,25 +106,25 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo break; } - // Disable JavaScript. - case (DomainsDatabase::DISABLED): + // Enable JavaScript. + case (DomainsDatabase::ENABLED): { - webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); break; } - // Enable JavaScript. - case (DomainsDatabase::ENABLED): + // Disable JavaScript. + case (DomainsDatabase::DISABLED): { - webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); break; } } // Set the local storage status. - switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt()) + switch (domainQuery.value(DomainsDatabase::LOCAL_STORAGE).toInt()) { // Set the default local storage status. case (DomainsDatabase::SYSTEM_DEFAULT): @@ -131,25 +134,25 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo break; } - // Disable local storage. - case (DomainsDatabase::DISABLED): + // Enable local storage. + case (DomainsDatabase::ENABLED): { - localStorageEnabled = false; + localStorageEnabled = true; break; } - // Enable local storage. - case (DomainsDatabase::ENABLED): + // Disable local storage. + case (DomainsDatabase::DISABLED): { - localStorageEnabled = true; + localStorageEnabled = false; break; } } // Set the DOM storage status. - switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt()) + switch (domainQuery.value(DomainsDatabase::DOM_STORAGE).toInt()) { // Set the default DOM storage status. QWebEngineSettings confusingly calls this local storage. case (DomainsDatabase::SYSTEM_DEFAULT): @@ -159,36 +162,36 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo break; } - // Disable DOM storage. QWebEngineSettings confusingly calls this local storage. - case (DomainsDatabase::DISABLED): + // Enable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::ENABLED): { - webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); break; } - // Enable DOM storage. QWebEngineSettings confusingly calls this local storage. - case (DomainsDatabase::ENABLED): + // Disable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::DISABLED): { - webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); break; } } // Set the user agent. - webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString())); + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainQuery.value(DomainsDatabase::USER_AGENT).toString())); // Check if a custom zoom factor is set. - if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt()) + if (domainQuery.value(DomainsDatabase::ZOOM_FACTOR).toInt()) { // Store the current zoom factor. - setZoomFactor(domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble()); + defaultZoomFactor = domainQuery.value(DomainsDatabase::CUSTOM_ZOOM_FACTOR).toDouble(); } else { - // Reset the current zoom factor. - setZoomFactor(Settings::zoomFactor()); + // Store the current zoom factor. + defaultZoomFactor = Settings::zoomFactor(); } } else // The hostname does not have domain settings. @@ -208,18 +211,36 @@ void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bo // Set the user agent. webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent())); - // Set the zoom factor. - setZoomFactor(Settings::zoomFactor()); + // Store the zoom factor. + defaultZoomFactor = Settings::zoomFactor(); } + // Set the current zoom factor. + setZoomFactor(defaultZoomFactor); + // Reload the website if requested. 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(); @@ -259,9 +280,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 +299,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 +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(); @@ -305,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)); + } }