2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "BrowserView.h"
23 #include "ui_BrowserView.h"
24 #include "databases/CookiesDatabase.h"
25 #include "databases/DomainsDatabase.h"
26 #include "filters/MouseEventFilter.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
29 #include "interceptors/UrlRequestInterceptor.h"
30 #include "windows/BrowserWindow.h"
32 // Qt framework headers.
35 // Initialize the public static variables.
36 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
38 // Construct the class.
39 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
41 // Initialize the variables.
42 privacyWebEngineListPointer = new QList<PrivacyWebEngine*>;
44 // Instantiate the browser view UI.
45 Ui::BrowserView browserViewUi;
48 browserViewUi.setupUi(this);
50 // Get handles for the views.
51 webEngineViewPointer = browserViewUi.webEngineView;
53 // Create an off-the-record profile (the default when no profile name is specified).
54 webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
56 // Create a WebEngine page.
57 webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
59 // Set the WebEngine page.
60 webEngineViewPointer->setPage(webEnginePagePointer);
62 // Handle full screen requests.
63 connect(webEnginePagePointer, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this, SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
65 // Get handles for the aspects of the WebEngine.
66 webEngineHistoryPointer = webEnginePagePointer->history();
67 webEngineSettingsPointer = webEngineViewPointer->settings();
68 webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
70 // Initialize the current privacy web engine pointer.
71 currentPrivacyWebEnginePointer = new PrivacyWebEngine(webEngineViewPointer);
73 // Populate the privacy web engine list.
74 privacyWebEngineListPointer->append(currentPrivacyWebEnginePointer);
76 // Set the local storage filter.
77 webEngineCookieStorePointer->setCookieFilter([this](const QWebEngineCookieStore::FilterRequest &filterRequest)
79 //qDebug().noquote().nospace() << "Page URL: " << filterRequest.firstPartyUrl << ", Local storage URL: " << filterRequest.origin << ", Is third-party: " << filterRequest.thirdParty;
81 // Block all third party local storage requests, including the sneaky ones that don't register a first party URL.
82 if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral("")))
84 //qDebug() << "Request blocked.";
90 /* TODO. Waiting for a solution to <https://redmine.stoutner.com/issues/857>.
91 // Check each tab to see if this local storage request should be allowed.
92 for (PrivacyWebEngine *privacyWebEnginePointer : *privacyWebEngineListPointer)
94 //qDebug().noquote().nospace() << "Local storage: " << privacyWebEnginePointer->localStorageEnabled << ". WebEngine URL: " << webEngineViewPointer->url().host() << ". Request Host: " << filterRequest.firstPartyUrl.host();
96 // Allow this local storage request if it comes from a tab with local storage enabled.
97 if (privacyWebEnginePointer->localStorageEnabled && (webEngineViewPointer->url().host() == filterRequest.firstPartyUrl.host()))
99 //qDebug() << "Request allowed.";
107 // Allow the request if it is first party and local storage is enabled.
108 if (!filterRequest.thirdParty && currentPrivacyWebEnginePointer->localStorageEnabled)
114 //qDebug() << "Request blocked.";
116 // Block any remaining local storage requests.
120 // Process cookie changes.
121 connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
122 connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), this, SLOT(cookieRemoved(QNetworkCookie)));
124 // Get a list of durable cookies.
125 QList<QNetworkCookie*> *durableCookiesListPointer = CookiesDatabase::getCookies();
127 // Add the durable cookies to the store.
128 for (QNetworkCookie *cookiePointer : *durableCookiesListPointer)
129 addCookieToStore(*cookiePointer);
131 // Store a copy of the WebEngine default user agent.
132 webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
134 // Update the URL line edit when the URL changes.
135 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
137 // Update the progress bar.
138 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
139 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
140 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
142 // Instantiate the mouse event filter pointer.
143 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
145 // Install the mouse event filter.
146 qApp->installEventFilter(mouseEventFilterPointer);
148 // Process mouse forward and back commands.
149 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
150 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
152 // Listen for hovered link URLs.
153 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
155 // Instantiate the URL request interceptor.
156 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
158 // Set the URL request interceptor.
159 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
161 // Reapply the domain settings when the host changes.
162 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
164 // Don't allow JavaScript to open windows.
165 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
167 // Allow keyboard navigation.
168 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
170 // Enable full screen support.
171 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
173 // Require user interaction to play media.
174 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
176 // Limit WebRTC to public IP addresses.
177 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
179 // Set the focus on the WebEngine view.
180 webEngineViewPointer->setFocus();
183 BrowserView::~BrowserView()
185 // Delay the deletion of the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
186 webEnginePagePointer->deleteLater();
189 // 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.
190 void BrowserView::addCookieToStore(QNetworkCookie cookie) const
195 // Check to see if the domain does not start with a `.` because Qt makes this harder than it should be. <https://doc.qt.io/qt-5/qwebenginecookiestore.html#setCookie>
196 if (!cookie.domain().startsWith(QStringLiteral(".")))
199 url.setHost(cookie.domain());
200 url.setScheme(QStringLiteral("https"));
202 // Clear the domain from the cookie.
203 cookie.setDomain(QStringLiteral(""));
206 // Add the cookie to the store.
207 webEngineCookieStorePointer->setCookie(cookie, url);
210 void BrowserView::applyApplicationSettings()
212 // Set the search engine URL.
213 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
215 // Emit the update search engine actions signal.
216 emit updateSearchEngineActions(Settings::searchEngine(), true);
219 // 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.
220 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
221 void BrowserView::applyDomainSettingsAndReload()
223 // Apply the domain settings. `true` reloads the website.
224 applyDomainSettings(webEngineViewPointer->url().host(), true);
227 // 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.
228 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
229 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
231 // Apply the domain settings `false` does not reload the website.
232 applyDomainSettings(hostname, false);
235 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
236 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
238 // Get the record for the hostname.
239 QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname);
241 // Check if the hostname has domain settings.
242 if (domainQuery.isValid()) // The hostname has domain settings.
244 // Get the domain record.
245 QSqlRecord domainRecord = domainQuery.record();
247 // Set the JavaScript status.
248 switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt())
250 // Set the default JavaScript status.
251 case (DomainsDatabase::SYSTEM_DEFAULT):
253 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
258 // Disable JavaScript.
259 case (DomainsDatabase::DISABLED):
261 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
266 // Enable JavaScript.
267 case (DomainsDatabase::ENABLED):
269 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
275 // Set the local storage status.
276 switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt())
278 // Set the default local storage status.
279 case (DomainsDatabase::SYSTEM_DEFAULT):
281 currentPrivacyWebEnginePointer->localStorageEnabled = Settings::localStorageEnabled();
286 // Disable local storage.
287 case (DomainsDatabase::DISABLED):
289 currentPrivacyWebEnginePointer->localStorageEnabled = false;
294 // Enable local storage.
295 case (DomainsDatabase::ENABLED):
297 currentPrivacyWebEnginePointer->localStorageEnabled = true;
303 // Set the DOM storage status.
304 switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt())
306 // Set the default DOM storage status.
307 case (DomainsDatabase::SYSTEM_DEFAULT):
309 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
314 // Disable DOM storage.
315 case (DomainsDatabase::DISABLED):
317 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
322 // Enable DOM storage.
323 case (DomainsDatabase::ENABLED):
325 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
331 // Set the user agent.
332 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString()));
334 // Check if a custom zoom factor is set.
335 if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt())
337 // Store the current zoom factor.
338 currentZoomFactor = domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble();
342 // Reset the current zoom factor.
343 currentZoomFactor = Settings::zoomFactor();
346 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
347 webEngineViewPointer->setZoomFactor(currentZoomFactor);
349 // Apply the domain settings palette to the URL line edit.
350 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString());
352 else // The hostname does not have domain settings.
354 // Set the JavaScript status.
355 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
357 // Set the local storage status.
358 currentPrivacyWebEnginePointer->localStorageEnabled = Settings::localStorageEnabled();
361 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
363 // Set the user agent.
364 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
366 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
367 currentZoomFactor = Settings::zoomFactor();
369 // Set the zoom factor.
370 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
372 // Apply the no domain settings palette to the URL line edit.
373 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
376 // Emit the update actions signals.
377 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
378 emit updateLocalStorageAction(currentPrivacyWebEnginePointer->localStorageEnabled);
379 emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
380 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), true);
381 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
383 // Reload the website if requested.
385 webEngineViewPointer->reload();
388 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
390 // Store the search engine name.
391 QString searchEngineName = searchEngineActionPointer->text();
393 // Strip out any `&` characters.
394 searchEngineName.remove('&');
396 // Store the search engine string.
397 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
399 // Update the search engine actionas.
400 emit updateSearchEngineActions(searchEngineName, false);
403 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
405 // Get the user agent name.
406 QString userAgentName = userAgentActionPointer->text();
408 // Strip out any `&` characters.
409 userAgentName.remove('&');
411 // Apply the user agent.
412 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
414 // Update the user agent actions.
415 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), false);
417 // Reload the website.
418 webEngineViewPointer->reload();
421 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
422 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
424 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
425 currentZoomFactor = zoomFactor;
427 // Set the zoom factor.
428 webEngineViewPointer->setZoomFactor(zoomFactor);
431 void BrowserView::back() const
434 webEngineViewPointer->back();
437 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
439 // Add the cookie to the cookie list.
440 emit addCookie(cookie);
443 void BrowserView::cookieRemoved(const QNetworkCookie &cookie) const
445 // Remove the cookie from the cookie list.
446 emit removeCookie(cookie);
449 void BrowserView::deleteAllCookies() const
451 // Delete all the cookies.
452 webEngineCookieStorePointer->deleteAllCookies();
455 void BrowserView::deleteCookieFromStore(const QNetworkCookie &cookie) const
457 // Delete the cookie.
458 webEngineCookieStorePointer->deleteCookie(cookie);
461 void BrowserView::forward() const
464 webEngineViewPointer->forward();
467 void BrowserView::fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest) const
470 emit fullScreenRequested(fullScreenRequest.toggleOn());
472 // Accept the request.
473 fullScreenRequest.accept();
476 void BrowserView::home() const
478 // Load the homepage.
479 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
482 void BrowserView::loadFinished() const
484 // Hide the progress bar.
485 emit hideProgressBar();
488 void BrowserView::loadInitialWebsite()
490 // Apply the application settings.
491 applyApplicationSettings();
493 // Get the arguments.
494 QStringList argumentsStringList = qApp->arguments();
496 // Check to see if the arguments lists contains a URL.
497 if (argumentsStringList.size() > 1)
499 // Load the URL from the arguments list.
500 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
504 // Load the homepage.
509 void BrowserView::loadProgress(const int &progress) const
511 // Show the progress bar.
512 emit showProgressBar(progress);
515 void BrowserView::loadStarted() const
517 // Show the progress bar.
518 emit showProgressBar(0);
521 void BrowserView::loadUrlFromLineEdit(QString url) const
523 // Decide if the text is more likely to be a URL or a search.
524 if (url.startsWith("file://")) // The text is likely a file URL.
527 webEngineViewPointer->load(QUrl::fromUserInput(url));
529 else if (url.contains(".")) // The text is likely a URL.
531 // Check if the URL does not start with a valid protocol.
532 if (!url.startsWith("http"))
534 // Add `https://` to the beginning of the URL.
535 url = "https://" + url;
539 webEngineViewPointer->load(QUrl::fromUserInput(url));
541 else // The text is likely a search.
544 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
548 void BrowserView::mouseBack() const
550 // Go back if possible.
551 if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoBack())
553 // Clear the URL line edit focus.
554 emit clearUrlLineEditFocus();
557 webEngineViewPointer->back();
561 void BrowserView::mouseForward() const
563 // Go forward if possible.
564 if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoForward())
566 // Clear the URL line edit focus.
567 emit clearUrlLineEditFocus();
570 webEngineViewPointer->forward();
574 void BrowserView::pageLinkHovered(const QString &linkUrl) const
576 // Emit a signal so that the browser window can update the status bar.
577 emit linkHovered(linkUrl);
580 void BrowserView::refresh() const
582 // Reload the website.
583 webEngineViewPointer->reload();
586 void BrowserView::toggleDomStorage() const
588 // Toggle DOM storage.
589 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
591 // Update the DOM storage action.
592 emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
594 // Reload the website.
595 webEngineViewPointer->reload();
598 void BrowserView::toggleJavaScript() const
600 // Toggle JavaScript.
601 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
603 // Update the JavaScript action.
604 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
606 // Reload the website.
607 webEngineViewPointer->reload();
610 void BrowserView::toggleLocalStorage()
612 // Toggle local storeage.
613 currentPrivacyWebEnginePointer->localStorageEnabled = !currentPrivacyWebEnginePointer->localStorageEnabled;
615 // Update the local storage action.
616 emit updateLocalStorageAction(currentPrivacyWebEnginePointer->localStorageEnabled);
618 // Reload the website.
619 webEngineViewPointer->reload();
622 void BrowserView::updateUrl(const QUrl &url) const
624 // Update the URL line edit.
625 emit updateUrlLineEdit(url);
627 // Update the status of the forward and back buttons.
628 emit updateBackAction(webEngineHistoryPointer->canGoBack());
629 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
631 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
632 webEngineViewPointer->setZoomFactor(currentZoomFactor);