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.
33 #include <QWebEngineProfile>
35 // Initialize the public static variables.
36 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
38 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
40 // Instantiate the browser view UI.
41 Ui::BrowserView browserViewUi;
44 browserViewUi.setupUi(this);
46 // Get handles for the views.
47 webEngineViewPointer = browserViewUi.webEngineView;
49 // Create an off-the-record profile (the default when no profile name is specified).
50 webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
52 // Create a WebEngine page.
53 QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
55 // Set the WebEngine page.
56 webEngineViewPointer->setPage(webEnginePagePointer);
58 // Get handles for the aspects of the WebEngine.
59 webEngineHistoryPointer = webEnginePagePointer->history();
60 webEngineSettingsPointer = webEngineViewPointer->settings();
62 // Store a copy of the WebEngine default user agent.
63 webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
65 // Update the URL line edit when the URL changes.
66 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
68 // Update the progress bar.
69 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
70 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
71 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
73 // Instantiate the mouse event filter pointer.
74 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
76 // Install the mouse event filter.
77 qApp->installEventFilter(mouseEventFilterPointer);
79 // Process mouse forward and back commands.
80 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
81 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
83 // Listen for hovered link URLs.
84 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
86 // Instantiate the URL request interceptor.
87 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
89 // Set the URL request interceptor.
90 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
92 // Reapply the domain settings when the host changes.
93 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
95 // Don't allow JavaScript to open windows.
96 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
98 // Allow keyboard navigation.
99 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
101 // Enable full screen support.
102 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
104 // Require user interaction to play media.
105 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
107 // Limit WebRTC to public IP addresses.
108 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
110 // Set the focus on the WebEngine view.
111 webEngineViewPointer->setFocus();
114 void BrowserView::applyApplicationSettings()
116 // Set the search engine URL.
117 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
119 // Emit the update search engine actions signal.
120 emit updateSearchEngineActions(Settings::searchEngine());
123 // 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.
124 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
125 void BrowserView::applyDomainSettingsAndReload()
127 // Apply the domain settings. `true` reloads the website.
128 applyDomainSettings(webEngineViewPointer->url().host(), true);
131 // 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.
132 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
133 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
135 // Apply the domain settings `false` does not reload the website.
136 applyDomainSettings(hostname, false);
139 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
140 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
142 // Get the record for the hostname.
143 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
145 // Check if the hostname has domain settings.
146 if (domainQuery.isValid()) // The hostname has domain settings.
148 // Get the domain record.
149 QSqlRecord domainRecord = domainQuery.record();
151 // Set the JavaScript status.
152 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
154 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
156 // Set the default JavaScript status.
157 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
162 case (DomainsDatabaseHelper::DISABLED):
164 // Disable JavaScript.
165 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
170 case (DomainsDatabaseHelper::ENABLED):
172 // Enable JavaScript.
173 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
179 // Set local storage.
180 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
182 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
184 // Set the default local storage status.
185 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
190 case (DomainsDatabaseHelper::DISABLED):
192 // Disable local storage.
193 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
198 case (DomainsDatabaseHelper::ENABLED):
200 // Enable local storage.
201 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
207 // Set the user agent.
208 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
210 // Check if a custom zoom factor is set.
211 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
213 // Store the current zoom factor.
214 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
218 // Reset the current zoom factor.
219 currentZoomFactor = Settings::zoomFactor();
222 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
223 webEngineViewPointer->setZoomFactor(currentZoomFactor);
225 // Apply the domain settings palette to the URL line edit.
226 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
228 else // The hostname does not have domain settings.
230 // Set the JavaScript status.
231 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
233 // Set local storage.
234 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
236 // Set the user agent.
237 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
239 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
240 currentZoomFactor = Settings::zoomFactor();
242 // Set the zoom factor.
243 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
245 // Apply the no domain settings palette to the URL line edit.
246 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
249 // Emit the update actions signals.
250 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
251 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
252 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
253 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
255 // Reload the website if requested.
258 webEngineViewPointer->reload();
262 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
264 // Store the search engine name.
265 QString searchEngineName = searchEngineActionPointer->text();
267 // Strip out any `&` characters.
268 searchEngineName.remove('&');
270 // Store the search engine string.
271 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
274 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
276 // Get the user agent name.
277 QString userAgentName = userAgentActionPointer->text();
279 // Strip out any `&` characters.
280 userAgentName.remove('&');
282 // Apply the user agent.
283 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
285 // Reload the website.
286 webEngineViewPointer->reload();
289 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
290 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
292 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
293 currentZoomFactor = zoomFactor;
295 // Set the zoom factor.
296 webEngineViewPointer->setZoomFactor(zoomFactor);
299 void BrowserView::back() const
302 webEngineViewPointer->back();
305 void BrowserView::forward() const
308 webEngineViewPointer->forward();
311 void BrowserView::home() const
313 // Load the homepage.
314 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
317 void BrowserView::loadFinished() const
319 // Hide the progress bar.
320 emit hideProgressBar();
323 void BrowserView::loadInitialWebsite()
325 // Apply the application settings.
326 applyApplicationSettings();
328 // Get the arguments.
329 QStringList argumentsStringList = qApp->arguments();
331 // Check to see if the arguments lists contains a URL.
332 if (argumentsStringList.size() > 1)
334 // Load the URL from the arguments list.
335 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
339 // Load the homepage.
344 void BrowserView::loadProgress(const int &progress) const
346 // Show the progress bar.
347 emit showProgressBar(progress);
350 void BrowserView::loadStarted() const
352 // Show the progress bar.
353 emit showProgressBar(0);
356 void BrowserView::loadUrlFromLineEdit(QString url) const
358 // Decide if the text is more likely to be a URL or a search.
359 if (url.startsWith("file://")) // The text is likely a file URL.
362 webEngineViewPointer->load(QUrl::fromUserInput(url));
364 else if (url.contains(".")) // The text is likely a URL.
366 // Check if the URL does not start with a valid protocol.
367 if (!url.startsWith("http"))
369 // Add `https://` to the beginning of the URL.
370 url = "https://" + url;
374 webEngineViewPointer->load(QUrl::fromUserInput(url));
376 else // The text is likely a search.
379 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
383 void BrowserView::mouseBack() const
385 // Go back if possible.
386 if (webEngineHistoryPointer->canGoBack())
388 // Clear the URL line edit focus.
389 emit clearUrlLineEditFocus();
392 webEngineViewPointer->back();
396 void BrowserView::mouseForward() const
398 // Go forward if possible.
399 if (webEngineHistoryPointer->canGoForward())
401 // Clear the URL line edit focus.
402 emit clearUrlLineEditFocus();
405 webEngineViewPointer->forward();
409 void BrowserView::pageLinkHovered(const QString &linkUrl) const
411 // Emit a signal so that the browser window can update the status bar.
412 emit linkHovered(linkUrl);
415 void BrowserView::refresh() const
417 // Reload the website.
418 webEngineViewPointer->reload();
421 void BrowserView::toggleJavaScript() const
423 // Toggle JavaScript.
424 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
426 // Update the JavaScript icon.
427 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
429 // Reload the website.
430 webEngineViewPointer->reload();
433 void BrowserView::toggleLocalStorage() const
435 // Toggle local storage.
436 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
438 // Update the local storage icon.
439 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
441 // Reload the website.
442 webEngineViewPointer->reload();
445 void BrowserView::updateUrl(const QUrl &url) const
447 // Update the URL line edit.
448 emit updateUrlLineEdit(url);
450 // Update the status of the forward and back buttons.
451 emit updateBackAction(webEngineHistoryPointer->canGoBack());
452 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
454 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
455 webEngineViewPointer->setZoomFactor(currentZoomFactor);