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 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
39 // Instantiate the browser view UI.
40 Ui::BrowserView browserViewUi;
43 browserViewUi.setupUi(this);
45 // Get handles for the views.
46 webEngineViewPointer = browserViewUi.webEngineView;
48 // Create an off-the-record profile (the default when no profile name is specified).
49 webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
51 // Create a WebEngine page.
52 webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
54 // Set the WebEngine page.
55 webEngineViewPointer->setPage(webEnginePagePointer);
57 // Get handles for the aspects of the WebEngine.
58 webEngineHistoryPointer = webEnginePagePointer->history();
59 webEngineSettingsPointer = webEngineViewPointer->settings();
60 webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
62 // Process cookie changes.
63 connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
64 connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), this, SLOT(cookieRemoved(QNetworkCookie)));
66 // Store a copy of the WebEngine default user agent.
67 webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
69 // Update the URL line edit when the URL changes.
70 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
72 // Update the progress bar.
73 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
74 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
75 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
77 // Instantiate the mouse event filter pointer.
78 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
80 // Install the mouse event filter.
81 qApp->installEventFilter(mouseEventFilterPointer);
83 // Process mouse forward and back commands.
84 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
85 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
87 // Listen for hovered link URLs.
88 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
90 // Instantiate the URL request interceptor.
91 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
93 // Set the URL request interceptor.
94 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
96 // Reapply the domain settings when the host changes.
97 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
99 // Don't allow JavaScript to open windows.
100 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
102 // Allow keyboard navigation.
103 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
105 // Enable full screen support.
106 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
108 // Require user interaction to play media.
109 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
111 // Limit WebRTC to public IP addresses.
112 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
114 // Set the focus on the WebEngine view.
115 webEngineViewPointer->setFocus();
118 BrowserView::~BrowserView()
120 // Delay the deletion of the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
121 webEnginePagePointer->deleteLater();
124 // 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.
125 void BrowserView::addCookieToStore(QNetworkCookie cookie) const
130 // 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>
131 if (!cookie.domain().startsWith(QStringLiteral(".")))
134 url.setHost(cookie.domain());
135 url.setScheme(QStringLiteral("https"));
137 // Clear the domain from the cookie.
138 cookie.setDomain(QStringLiteral(""));
141 // Add the cookie to the store.
142 webEngineCookieStorePointer->setCookie(cookie, url);
145 void BrowserView::applyApplicationSettings()
147 // Set the search engine URL.
148 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
150 // Emit the update search engine actions signal.
151 emit updateSearchEngineActions(Settings::searchEngine());
154 // 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.
155 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
156 void BrowserView::applyDomainSettingsAndReload()
158 // Apply the domain settings. `true` reloads the website.
159 applyDomainSettings(webEngineViewPointer->url().host(), true);
162 // 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.
163 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
164 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
166 // Apply the domain settings `false` does not reload the website.
167 applyDomainSettings(hostname, false);
170 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
171 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
173 // Get the record for the hostname.
174 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
176 // Check if the hostname has domain settings.
177 if (domainQuery.isValid()) // The hostname has domain settings.
179 // Get the domain record.
180 QSqlRecord domainRecord = domainQuery.record();
182 // Set the JavaScript status.
183 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
185 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
187 // Set the default JavaScript status.
188 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
193 case (DomainsDatabaseHelper::DISABLED):
195 // Disable JavaScript.
196 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
201 case (DomainsDatabaseHelper::ENABLED):
203 // Enable JavaScript.
204 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
210 // Set local storage.
211 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
213 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
215 // Set the default local storage status.
216 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
221 case (DomainsDatabaseHelper::DISABLED):
223 // Disable local storage.
224 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
229 case (DomainsDatabaseHelper::ENABLED):
231 // Enable local storage.
232 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
238 // Set the user agent.
239 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
241 // Check if a custom zoom factor is set.
242 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
244 // Store the current zoom factor.
245 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
249 // Reset the current zoom factor.
250 currentZoomFactor = Settings::zoomFactor();
253 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
254 webEngineViewPointer->setZoomFactor(currentZoomFactor);
256 // Apply the domain settings palette to the URL line edit.
257 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
259 else // The hostname does not have domain settings.
261 // Set the JavaScript status.
262 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
264 // Set local storage.
265 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
267 // Set the user agent.
268 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
270 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
271 currentZoomFactor = Settings::zoomFactor();
273 // Set the zoom factor.
274 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
276 // Apply the no domain settings palette to the URL line edit.
277 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
280 // Emit the update actions signals.
281 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
282 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
283 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
284 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
286 // Reload the website if requested.
289 webEngineViewPointer->reload();
293 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
295 // Store the search engine name.
296 QString searchEngineName = searchEngineActionPointer->text();
298 // Strip out any `&` characters.
299 searchEngineName.remove('&');
301 // Store the search engine string.
302 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
305 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
307 // Get the user agent name.
308 QString userAgentName = userAgentActionPointer->text();
310 // Strip out any `&` characters.
311 userAgentName.remove('&');
313 // Apply the user agent.
314 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
316 // Reload the website.
317 webEngineViewPointer->reload();
320 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
321 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
323 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
324 currentZoomFactor = zoomFactor;
326 // Set the zoom factor.
327 webEngineViewPointer->setZoomFactor(zoomFactor);
330 void BrowserView::back() const
333 webEngineViewPointer->back();
336 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
338 // Add the cookie to the cookie list.
339 emit addCookie(cookie);
342 void BrowserView::cookieRemoved(const QNetworkCookie &cookie) const
344 // Remove the cookie from the cookie list.
345 emit removeCookie(cookie);
348 void BrowserView::deleteAllCookies() const
350 // Delete all the cookies.
351 webEngineCookieStorePointer->deleteAllCookies();
354 void BrowserView::deleteCookieFromStore(const QNetworkCookie &cookie) const
356 // Delete the cookie.
357 webEngineCookieStorePointer->deleteCookie(cookie);
360 void BrowserView::forward() const
363 webEngineViewPointer->forward();
366 void BrowserView::home() const
368 // Load the homepage.
369 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
372 void BrowserView::loadFinished() const
374 // Hide the progress bar.
375 emit hideProgressBar();
378 void BrowserView::loadInitialWebsite()
380 // Apply the application settings.
381 applyApplicationSettings();
383 // Get the arguments.
384 QStringList argumentsStringList = qApp->arguments();
386 // Check to see if the arguments lists contains a URL.
387 if (argumentsStringList.size() > 1)
389 // Load the URL from the arguments list.
390 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
394 // Load the homepage.
399 void BrowserView::loadProgress(const int &progress) const
401 // Show the progress bar.
402 emit showProgressBar(progress);
405 void BrowserView::loadStarted() const
407 // Show the progress bar.
408 emit showProgressBar(0);
411 void BrowserView::loadUrlFromLineEdit(QString url) const
413 // Decide if the text is more likely to be a URL or a search.
414 if (url.startsWith("file://")) // The text is likely a file URL.
417 webEngineViewPointer->load(QUrl::fromUserInput(url));
419 else if (url.contains(".")) // The text is likely a URL.
421 // Check if the URL does not start with a valid protocol.
422 if (!url.startsWith("http"))
424 // Add `https://` to the beginning of the URL.
425 url = "https://" + url;
429 webEngineViewPointer->load(QUrl::fromUserInput(url));
431 else // The text is likely a search.
434 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
438 void BrowserView::mouseBack() const
440 // Go back if possible.
441 if (webEngineHistoryPointer->canGoBack())
443 // Clear the URL line edit focus.
444 emit clearUrlLineEditFocus();
447 webEngineViewPointer->back();
451 void BrowserView::mouseForward() const
453 // Go forward if possible.
454 if (webEngineHistoryPointer->canGoForward())
456 // Clear the URL line edit focus.
457 emit clearUrlLineEditFocus();
460 webEngineViewPointer->forward();
464 void BrowserView::pageLinkHovered(const QString &linkUrl) const
466 // Emit a signal so that the browser window can update the status bar.
467 emit linkHovered(linkUrl);
470 void BrowserView::refresh() const
472 // Reload the website.
473 webEngineViewPointer->reload();
476 void BrowserView::toggleJavaScript() const
478 // Toggle JavaScript.
479 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
481 // Update the JavaScript icon.
482 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
484 // Reload the website.
485 webEngineViewPointer->reload();
488 void BrowserView::toggleLocalStorage() const
490 // Toggle local storage.
491 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
493 // Update the local storage icon.
494 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
496 // Reload the website.
497 webEngineViewPointer->reload();
500 void BrowserView::updateUrl(const QUrl &url) const
502 // Update the URL line edit.
503 emit updateUrlLineEdit(url);
505 // Update the status of the forward and back buttons.
506 emit updateBackAction(webEngineHistoryPointer->canGoBack());
507 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
509 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
510 webEngineViewPointer->setZoomFactor(currentZoomFactor);