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 "filters/MouseEventFilter.h"
25 #include "helpers/DomainsDatabaseHelper.h"
26 #include "helpers/SearchEngineHelper.h"
27 #include "helpers/UserAgentHelper.h"
28 #include "interceptors/UrlRequestInterceptor.h"
29 #include "windows/BrowserWindow.h"
31 // Qt framework headers.
34 // Initialize the public static variables.
35 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
37 // Construct the class.
38 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
40 // Initialize the variables.
41 privacyWebEngineListPointer = new QList<PrivacyWebEngine*>;
43 // Instantiate the browser view UI.
44 Ui::BrowserView browserViewUi;
47 browserViewUi.setupUi(this);
49 // Get handles for the views.
50 webEngineViewPointer = browserViewUi.webEngineView;
52 // Create an off-the-record profile (the default when no profile name is specified).
53 webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
55 // Create a WebEngine page.
56 webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
58 // Set the WebEngine page.
59 webEngineViewPointer->setPage(webEnginePagePointer);
61 // Get handles for the aspects of the WebEngine.
62 webEngineHistoryPointer = webEnginePagePointer->history();
63 webEngineSettingsPointer = webEngineViewPointer->settings();
64 webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
66 // Initialize the current privacy web engine pointer.
67 currentPrivacyWebEnginePointer = new PrivacyWebEngine(webEngineViewPointer);
69 // Populate the privacy web engine list.
70 privacyWebEngineListPointer->append(currentPrivacyWebEnginePointer);
72 // Set the cookie filter.
73 webEngineCookieStorePointer->setCookieFilter([this](const QWebEngineCookieStore::FilterRequest &filterRequest)
75 // qDebug() << "Cookie page URL: " << filterRequest.firstPartyUrl << ", Cookie URL: " << filterRequest.origin << ", Is third-party: " << filterRequest.thirdParty;
77 // Block all third party local storage requests, including the sneaky ones that don't register a first party URL.
78 if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral("")))
81 // Check each tab to see if this local storage request should be allowed.
82 for (PrivacyWebEngine *privacyWebEnginePointer : *privacyWebEngineListPointer)
84 // Allow this local storage request if it comes from a tab with local storage enabled.
85 if (privacyWebEnginePointer->cookiesEnabled && (webEngineViewPointer->url().host() == filterRequest.firstPartyUrl.host()))
89 // Block any remaining local storage requests.
93 // Process cookie changes.
94 connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
95 connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), this, SLOT(cookieRemoved(QNetworkCookie)));
97 // Store a copy of the WebEngine default user agent.
98 webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
100 // Update the URL line edit when the URL changes.
101 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
103 // Update the progress bar.
104 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
105 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
106 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
108 // Instantiate the mouse event filter pointer.
109 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
111 // Install the mouse event filter.
112 qApp->installEventFilter(mouseEventFilterPointer);
114 // Process mouse forward and back commands.
115 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
116 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
118 // Listen for hovered link URLs.
119 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
121 // Instantiate the URL request interceptor.
122 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
124 // Set the URL request interceptor.
125 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
127 // Reapply the domain settings when the host changes.
128 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
130 // Don't allow JavaScript to open windows.
131 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
133 // Allow keyboard navigation.
134 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
136 // Enable full screen support.
137 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
139 // Require user interaction to play media.
140 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
142 // Limit WebRTC to public IP addresses.
143 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
145 // Set the focus on the WebEngine view.
146 webEngineViewPointer->setFocus();
149 BrowserView::~BrowserView()
151 // Delay the deletion of the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
152 webEnginePagePointer->deleteLater();
155 // 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.
156 void BrowserView::addCookieToStore(QNetworkCookie cookie) const
161 // 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>
162 if (!cookie.domain().startsWith(QStringLiteral(".")))
165 url.setHost(cookie.domain());
166 url.setScheme(QStringLiteral("https"));
168 // Clear the domain from the cookie.
169 cookie.setDomain(QStringLiteral(""));
172 // Add the cookie to the store.
173 webEngineCookieStorePointer->setCookie(cookie, url);
176 void BrowserView::applyApplicationSettings()
178 // Set the search engine URL.
179 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
181 // Emit the update search engine actions signal.
182 emit updateSearchEngineActions(Settings::searchEngine());
185 // 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.
186 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
187 void BrowserView::applyDomainSettingsAndReload()
189 // Apply the domain settings. `true` reloads the website.
190 applyDomainSettings(webEngineViewPointer->url().host(), true);
193 // 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.
194 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
195 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
197 // Apply the domain settings `false` does not reload the website.
198 applyDomainSettings(hostname, false);
201 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
202 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
204 // Get the record for the hostname.
205 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
207 // Check if the hostname has domain settings.
208 if (domainQuery.isValid()) // The hostname has domain settings.
210 // Get the domain record.
211 QSqlRecord domainRecord = domainQuery.record();
213 // Set the JavaScript status.
214 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
216 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
218 // Set the default JavaScript status.
219 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
224 case (DomainsDatabaseHelper::DISABLED):
226 // Disable JavaScript.
227 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
232 case (DomainsDatabaseHelper::ENABLED):
234 // Enable JavaScript.
235 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
241 // Set the cookie status. TODO.
242 currentPrivacyWebEnginePointer->cookiesEnabled = Settings::cookiesEnabled();
245 switch (domainRecord.field(DomainsDatabaseHelper::DOM_STORAGE).value().toInt())
247 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
249 // Set the default DOM storage status.
250 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
255 case (DomainsDatabaseHelper::DISABLED):
257 // Disable DOM storage.
258 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
263 case (DomainsDatabaseHelper::ENABLED):
265 // Enable DOM storage.
266 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
272 // Set the user agent.
273 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
275 // Check if a custom zoom factor is set.
276 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
278 // Store the current zoom factor.
279 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
283 // Reset the current zoom factor.
284 currentZoomFactor = Settings::zoomFactor();
287 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
288 webEngineViewPointer->setZoomFactor(currentZoomFactor);
290 // Apply the domain settings palette to the URL line edit.
291 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
293 else // The hostname does not have domain settings.
295 // Set the JavaScript status.
296 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
298 // Set the cookie status.
299 currentPrivacyWebEnginePointer->cookiesEnabled = Settings::cookiesEnabled();
302 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
304 // Set the user agent.
305 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
307 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
308 currentZoomFactor = Settings::zoomFactor();
310 // Set the zoom factor.
311 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
313 // Apply the no domain settings palette to the URL line edit.
314 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
317 // Emit the update actions signals.
318 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
319 emit updateCookiesAction(currentPrivacyWebEnginePointer->cookiesEnabled);
320 emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
321 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
322 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
324 // Reload the website if requested.
327 webEngineViewPointer->reload();
331 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
333 // Store the search engine name.
334 QString searchEngineName = searchEngineActionPointer->text();
336 // Strip out any `&` characters.
337 searchEngineName.remove('&');
339 // Store the search engine string.
340 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
343 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
345 // Get the user agent name.
346 QString userAgentName = userAgentActionPointer->text();
348 // Strip out any `&` characters.
349 userAgentName.remove('&');
351 // Apply the user agent.
352 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
354 // Reload the website.
355 webEngineViewPointer->reload();
358 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
359 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
361 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
362 currentZoomFactor = zoomFactor;
364 // Set the zoom factor.
365 webEngineViewPointer->setZoomFactor(zoomFactor);
368 void BrowserView::back() const
371 webEngineViewPointer->back();
374 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
376 // Add the cookie to the cookie list.
377 emit addCookie(cookie);
380 void BrowserView::cookieRemoved(const QNetworkCookie &cookie) const
382 // Remove the cookie from the cookie list.
383 emit removeCookie(cookie);
386 void BrowserView::deleteAllCookies() const
388 // Delete all the cookies.
389 webEngineCookieStorePointer->deleteAllCookies();
392 void BrowserView::deleteCookieFromStore(const QNetworkCookie &cookie) const
394 // Delete the cookie.
395 webEngineCookieStorePointer->deleteCookie(cookie);
398 void BrowserView::forward() const
401 webEngineViewPointer->forward();
404 void BrowserView::home() const
406 // Load the homepage.
407 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
410 void BrowserView::loadFinished() const
412 // Hide the progress bar.
413 emit hideProgressBar();
416 void BrowserView::loadInitialWebsite()
418 // Apply the application settings.
419 applyApplicationSettings();
421 // Get the arguments.
422 QStringList argumentsStringList = qApp->arguments();
424 // Check to see if the arguments lists contains a URL.
425 if (argumentsStringList.size() > 1)
427 // Load the URL from the arguments list.
428 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
432 // Load the homepage.
437 void BrowserView::loadProgress(const int &progress) const
439 // Show the progress bar.
440 emit showProgressBar(progress);
443 void BrowserView::loadStarted() const
445 // Show the progress bar.
446 emit showProgressBar(0);
449 void BrowserView::loadUrlFromLineEdit(QString url) const
451 // Decide if the text is more likely to be a URL or a search.
452 if (url.startsWith("file://")) // The text is likely a file URL.
455 webEngineViewPointer->load(QUrl::fromUserInput(url));
457 else if (url.contains(".")) // The text is likely a URL.
459 // Check if the URL does not start with a valid protocol.
460 if (!url.startsWith("http"))
462 // Add `https://` to the beginning of the URL.
463 url = "https://" + url;
467 webEngineViewPointer->load(QUrl::fromUserInput(url));
469 else // The text is likely a search.
472 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
476 void BrowserView::mouseBack() const
478 // Go back if possible.
479 if (webEngineHistoryPointer->canGoBack())
481 // Clear the URL line edit focus.
482 emit clearUrlLineEditFocus();
485 webEngineViewPointer->back();
489 void BrowserView::mouseForward() const
491 // Go forward if possible.
492 if (webEngineHistoryPointer->canGoForward())
494 // Clear the URL line edit focus.
495 emit clearUrlLineEditFocus();
498 webEngineViewPointer->forward();
502 void BrowserView::pageLinkHovered(const QString &linkUrl) const
504 // Emit a signal so that the browser window can update the status bar.
505 emit linkHovered(linkUrl);
508 void BrowserView::refresh() const
510 // Reload the website.
511 webEngineViewPointer->reload();
514 void BrowserView::toggleCookies()
517 currentPrivacyWebEnginePointer->cookiesEnabled = !currentPrivacyWebEnginePointer->cookiesEnabled;
519 // Update the cookies icon.
520 emit updateCookiesAction(currentPrivacyWebEnginePointer->cookiesEnabled);
522 // Reload the website.
523 webEngineViewPointer->reload();
526 void BrowserView::toggleJavaScript() const
528 // Toggle JavaScript.
529 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
531 // Update the JavaScript icon.
532 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
534 // Reload the website.
535 webEngineViewPointer->reload();
538 void BrowserView::toggleDomStorage() const
540 // Toggle DOM storage.
541 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
543 // Update the DOM storage action icon.
544 emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
546 // Reload the website.
547 webEngineViewPointer->reload();
550 void BrowserView::updateUrl(const QUrl &url) const
552 // Update the URL line edit.
553 emit updateUrlLineEdit(url);
555 // Update the status of the forward and back buttons.
556 emit updateBackAction(webEngineHistoryPointer->canGoBack());
557 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
559 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
560 webEngineViewPointer->setZoomFactor(currentZoomFactor);