X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fviews%2FBrowserView.cpp;h=6bdbda6536cc2dcea6f421267ccc8a5781860959;hb=823acbeb2cc27030857f249ab7157d8562e7b7fe;hp=440c694ab02a7ca130bfc58dbeb85ba9fa2a1b8b;hpb=588db73b94af7b596b0e532f4557aa8b6c41f5c3;p=PrivacyBrowserPC.git diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index 440c694..6bdbda6 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -21,8 +21,9 @@ #include "BrowserView.h" #include "Settings.h" #include "ui_BrowserView.h" +#include "databases/CookiesDatabase.h" +#include "databases/DomainsDatabase.h" #include "filters/MouseEventFilter.h" -#include "helpers/DomainsDatabaseHelper.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" #include "interceptors/UrlRequestInterceptor.h" @@ -72,19 +73,42 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) // Set the local storage filter. webEngineCookieStorePointer->setCookieFilter([this](const QWebEngineCookieStore::FilterRequest &filterRequest) { - // qDebug() << "Page URL: " << filterRequest.firstPartyUrl << ", Local storage URL: " << filterRequest.origin << ", Is third-party: " << filterRequest.thirdParty; + //qDebug().noquote().nospace() << "Page URL: " << filterRequest.firstPartyUrl << ", Local storage URL: " << filterRequest.origin << ", Is third-party: " << filterRequest.thirdParty; // Block all third party local storage requests, including the sneaky ones that don't register a first party URL. if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral(""))) + { + //qDebug() << "Request blocked."; + + // Return false. return false; + } + /* TODO. Waiting for a solution to . // Check each tab to see if this local storage request should be allowed. for (PrivacyWebEngine *privacyWebEnginePointer : *privacyWebEngineListPointer) { + //qDebug().noquote().nospace() << "Local storage: " << privacyWebEnginePointer->localStorageEnabled << ". WebEngine URL: " << webEngineViewPointer->url().host() << ". Request Host: " << filterRequest.firstPartyUrl.host(); + // Allow this local storage request if it comes from a tab with local storage enabled. if (privacyWebEnginePointer->localStorageEnabled && (webEngineViewPointer->url().host() == filterRequest.firstPartyUrl.host())) + { + //qDebug() << "Request allowed."; + + // Return true. return true; + } } + */ + + // Allow the request if it is first party and local storage is enabled. + if (!filterRequest.thirdParty && currentPrivacyWebEnginePointer->localStorageEnabled) + { + // Return true. + return true; + } + + //qDebug() << "Request blocked."; // Block any remaining local storage requests. return false; @@ -94,6 +118,13 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie))); connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), this, SLOT(cookieRemoved(QNetworkCookie))); + // Get a list of durable cookies. + QList *durableCookiesListPointer = CookiesDatabase::getCookies(); + + // Add the durable cookies to the store. + for (QNetworkCookie *cookiePointer : *durableCookiesListPointer) + addCookieToStore(*cookiePointer); + // Store a copy of the WebEngine default user agent. webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent(); @@ -152,7 +183,8 @@ BrowserView::~BrowserView() webEnginePagePointer->deleteLater(); } -void BrowserView::addCookieToStore(QNetworkCookie &cookie) const +// The cookie is copied instead of referenced so that changes made to the cookie do not create a race condition with the display of the cookie in the dialog. +void BrowserView::addCookieToStore(QNetworkCookie cookie) const { // Create a url. QUrl url; @@ -178,7 +210,7 @@ void BrowserView::applyApplicationSettings() searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine()); // Emit the update search engine actions signal. - emit updateSearchEngineActions(Settings::searchEngine()); + emit updateSearchEngineActions(Settings::searchEngine(), 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. @@ -201,7 +233,7 @@ void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname) void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) { // Get the record for the hostname. - QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname); + QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname); // Check if the hostname has domain settings. if (domainQuery.isValid()) // The hostname has domain settings. @@ -210,10 +242,10 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload QSqlRecord domainRecord = domainQuery.record(); // Set the JavaScript status. - switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt()) + switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt()) { // Set the default JavaScript status. - case (DomainsDatabaseHelper::SYSTEM_DEFAULT): + case (DomainsDatabase::SYSTEM_DEFAULT): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled()); @@ -221,7 +253,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Disable JavaScript. - case (DomainsDatabaseHelper::DISABLED): + case (DomainsDatabase::DISABLED): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false); @@ -229,7 +261,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Enable JavaScript. - case (DomainsDatabaseHelper::ENABLED): + case (DomainsDatabase::ENABLED): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true); @@ -238,10 +270,10 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Set the local storage status. - switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt()) + switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt()) { // Set the default local storage status. - case (DomainsDatabaseHelper::SYSTEM_DEFAULT): + case (DomainsDatabase::SYSTEM_DEFAULT): { currentPrivacyWebEnginePointer->localStorageEnabled = Settings::localStorageEnabled(); @@ -249,7 +281,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Disable local storage. - case (DomainsDatabaseHelper::DISABLED): + case (DomainsDatabase::DISABLED): { currentPrivacyWebEnginePointer->localStorageEnabled = false; @@ -257,7 +289,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Enable local storage. - case (DomainsDatabaseHelper::ENABLED): + case (DomainsDatabase::ENABLED): { currentPrivacyWebEnginePointer->localStorageEnabled = true; @@ -266,10 +298,10 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Set the DOM storage status. - switch (domainRecord.field(DomainsDatabaseHelper::DOM_STORAGE).value().toInt()) + switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt()) { // Set the default DOM storage status. - case (DomainsDatabaseHelper::SYSTEM_DEFAULT): + case (DomainsDatabase::SYSTEM_DEFAULT): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled()); @@ -277,7 +309,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Disable DOM storage. - case (DomainsDatabaseHelper::DISABLED): + case (DomainsDatabase::DISABLED): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); @@ -285,7 +317,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Enable DOM storage. - case (DomainsDatabaseHelper::ENABLED): + case (DomainsDatabase::ENABLED): { webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); @@ -294,13 +326,13 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Set the user agent. - webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString())); + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString())); // Check if a custom zoom factor is set. - if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt()) + if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt()) { // Store the current zoom factor. - currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble(); + currentZoomFactor = domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble(); } else { @@ -312,7 +344,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(currentZoomFactor); // Apply the domain settings palette to the URL line edit. - emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString()); + emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString()); } else // The hostname does not have domain settings. { @@ -342,14 +374,12 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); emit updateLocalStorageAction(currentPrivacyWebEnginePointer->localStorageEnabled); emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); - emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent()); + emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), true); emit updateZoomFactorAction(webEngineViewPointer->zoomFactor()); // Reload the website if requested. if (reloadWebsite) - { webEngineViewPointer->reload(); - } } void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer) @@ -362,6 +392,9 @@ void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer) // Store the search engine string. searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName); + + // Update the search engine actionas. + emit updateSearchEngineActions(searchEngineName, false); } void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const @@ -375,6 +408,9 @@ void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const // Apply the user agent. webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName)); + // Update the user agent actions. + emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), false); + // Reload the website. webEngineViewPointer->reload(); } @@ -500,7 +536,7 @@ void BrowserView::loadUrlFromLineEdit(QString url) const void BrowserView::mouseBack() const { // Go back if possible. - if (webEngineHistoryPointer->canGoBack()) + if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoBack()) { // Clear the URL line edit focus. emit clearUrlLineEditFocus(); @@ -513,7 +549,7 @@ void BrowserView::mouseBack() const void BrowserView::mouseForward() const { // Go forward if possible. - if (webEngineHistoryPointer->canGoForward()) + if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoForward()) { // Clear the URL line edit focus. emit clearUrlLineEditFocus();