X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fwidgets%2FPrivacyWebEngineView.cpp;fp=src%2Fwidgets%2FPrivacyWebEngineView.cpp;h=bc207cb21c94d5823b832642bb59ac193739a68a;hp=d7b3f8c1de62a7be5050dc274503d29a690781a2;hb=8756d450d1d44dd8e840f7e3de7b1d72ca5b7d8e;hpb=95aa7dff6f4da3aa85775d46600f9be2e2f856eb diff --git a/src/widgets/PrivacyWebEngineView.cpp b/src/widgets/PrivacyWebEngineView.cpp index d7b3f8c..bc207cb 100644 --- a/src/widgets/PrivacyWebEngineView.cpp +++ b/src/widgets/PrivacyWebEngineView.cpp @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Soren Stoutner . + * Copyright 2022-2023 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -19,7 +19,10 @@ // Application headers. #include "PrivacyWebEngineView.h" +#include "Settings.h" #include "databases/CookiesDatabase.h" +#include "databases/DomainsDatabase.h" +#include "interceptors/UrlRequestInterceptor.h" #include "windows/BrowserWindow.h" // Qt toolkit headers. @@ -27,7 +30,29 @@ #include // Construct the class. -PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {} +PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) +{ + // Create an off-the-record profile (the default when no profile name is specified). + webEngineProfilePointer = new QWebEngineProfile(QLatin1String("")); + + // Create a WebEngine page. + QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer); + + // Set the WebEngine page. + setPage(webEnginePagePointer); + + // Get handles for the various aspects of the WebEngine. + webEngineSettingsPointer = webEnginePagePointer->settings(); + + // 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(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString))); +} void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const { @@ -44,6 +69,154 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const emit updateCookiesAction(cookieListPointer->size()); } +void PrivacyWebEngineView::applyDomainSettingsWithoutReloading(const QString &hostname) +{ + // Apply the domain settings `false` does not reload the website. + applyDomainSettings(hostname, false); +} + +void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) +{ + // Get the record for the hostname. + QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname); + + // 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(); + + // Set the JavaScript status. + switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt()) + { + // Set the default JavaScript status. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); + + break; + } + + // Disable JavaScript. + case (DomainsDatabase::DISABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); + + break; + } + + // Enable JavaScript. + case (DomainsDatabase::ENABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); + + break; + } + } + + // Set the local storage status. + switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt()) + { + // Set the default local storage status. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + localStorageEnabled = Settings::localStorageEnabled(); + + break; + } + + // Disable local storage. + case (DomainsDatabase::DISABLED): + { + localStorageEnabled = false; + + break; + } + + // Enable local storage. + case (DomainsDatabase::ENABLED): + { + localStorageEnabled = true; + + break; + } + } + + // Set the DOM storage status. + switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt()) + { + // Set the default DOM storage status. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::SYSTEM_DEFAULT): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); + + break; + } + + // Disable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::DISABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); + + break; + } + + // Enable DOM storage. QWebEngineSettings confusingly calls this local storage. + case (DomainsDatabase::ENABLED): + { + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); + + break; + } + } + + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString())); + + // Check if a custom zoom factor is set. + if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt()) + { + // Store the current zoom factor. + setZoomFactor(domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble()); + } + else + { + // Reset the current zoom factor. + setZoomFactor(Settings::zoomFactor()); + } + } + else // The hostname does not have domain settings. + { + // Reset the domain settings name. + domainSettingsName = QLatin1String(""); + + // Set the JavaScript status. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); + + // Set the local storage status. + localStorageEnabled = Settings::localStorageEnabled(); + + // Set DOM storage. In QWebEngineSettings it is called Local Storage. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); + + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent())); + + // Set the zoom factor. + setZoomFactor(Settings::zoomFactor()); + } + + // Reload the website if requested. + if (reloadWebsite) + reload(); + + // Update the UI. + emit updateUi(this); +} + void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) { // Get a handle for the QWebEnginePage *webEnginePagePointer = page(); @@ -66,7 +239,7 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType // Get a handle for the browser window. BrowserWindow *browserWindowPointer = qobject_cast(window()); - // Create the requsted window type. + // Create the requested window type. switch (webWindowType) { case QWebEnginePage::WebBrowserTab: @@ -97,7 +270,7 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType default: { - // Return an null pointer for opening a background tab and opening a web dialog. + // Return a null pointer for opening a web dialog. return nullptr; } }