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"
22 #include "MouseEventFilter.h"
24 #include "ui_BrowserView.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(webEngineViewPointer);
76 // Install the mouse event filter.
77 qApp->installEventFilter(mouseEventFilterPointer);
79 // Listen for hovered link URLs.
80 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
82 // Instantiate the URL request interceptor.
83 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
85 // Set the URL request interceptor.
86 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
88 // Reapply the domain settings when the host changes.
89 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
91 // Don't allow JavaScript to open windows.
92 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
94 // Allow keyboard navigation.
95 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
97 // Enable full screen support.
98 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
100 // Require user interaction to play media.
101 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
103 // Limit WebRTC to public IP addresses.
104 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
106 // Set the focus on the WebEngine view.
107 webEngineViewPointer->setFocus();
110 void BrowserView::applyApplicationSettings()
112 // Set the search engine URL.
113 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
115 // Emit the update search engine actions signal.
116 emit updateSearchEngineActions(Settings::searchEngine());
119 // 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.
120 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
121 void BrowserView::applyDomainSettingsAndReload()
123 // Apply the domain settings. `true` reloads the website.
124 applyDomainSettings(webEngineViewPointer->url().host(), true);
127 // 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.
128 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
129 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
131 // Apply the domain settings `false` does not reload the website.
132 applyDomainSettings(hostname, false);
135 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
136 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
138 // Get the record for the hostname.
139 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
141 // Check if the hostname has domain settings.
142 if (domainQuery.isValid()) // The hostname has domain settings.
144 // Get the domain record.
145 QSqlRecord domainRecord = domainQuery.record();
147 // Set the JavaScript status.
148 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
150 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
152 // Set the default JavaScript status.
153 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
158 case (DomainsDatabaseHelper::DISABLED):
160 // Disable JavaScript.
161 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
166 case (DomainsDatabaseHelper::ENABLED):
168 // Enable JavaScript.
169 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
175 // Set local storage.
176 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
178 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
180 // Set the default local storage status.
181 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
186 case (DomainsDatabaseHelper::DISABLED):
188 // Disable local storage.
189 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
194 case (DomainsDatabaseHelper::ENABLED):
196 // Enable local storage.
197 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
203 // Set the user agent.
204 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
206 // Check if a custom zoom factor is set.
207 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
209 // Store the current zoom factor.
210 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
214 // Reset the current zoom factor.
215 currentZoomFactor = Settings::zoomFactor();
218 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
219 webEngineViewPointer->setZoomFactor(currentZoomFactor);
221 // Apply the domain settings palette to the URL line edit.
222 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
224 else // The hostname does not have domain settings.
226 // Set the JavaScript status.
227 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
229 // Set local storage.
230 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
232 // Set the user agent.
233 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
235 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
236 currentZoomFactor = Settings::zoomFactor();
238 // Set the zoom factor.
239 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
241 // Apply the no domain settings palette to the URL line edit.
242 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
245 // Emit the update actions signals.
246 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
247 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
248 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
249 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
251 // Reload the website if requested.
254 webEngineViewPointer->reload();
258 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
260 // Store the search engine name.
261 QString searchEngineName = searchEngineActionPointer->text();
263 // Strip out any `&` characters.
264 searchEngineName.remove('&');
266 // Store the search engine string.
267 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
270 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
272 // Get the user agent name.
273 QString userAgentName = userAgentActionPointer->text();
275 // Strip out any `&` characters.
276 userAgentName.remove('&');
278 // Apply the user agent.
279 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
281 // Reload the website.
282 webEngineViewPointer->reload();
285 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
286 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
288 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
289 currentZoomFactor = zoomFactor;
291 // Set the zoom factor.
292 webEngineViewPointer->setZoomFactor(zoomFactor);
295 void BrowserView::back() const
298 webEngineViewPointer->back();
301 void BrowserView::forward() const
304 webEngineViewPointer->forward();
307 void BrowserView::home() const
309 // Load the homepage.
310 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
313 void BrowserView::loadFinished() const
315 // Hide the progress bar.
316 emit hideProgressBar();
319 void BrowserView::loadInitialWebsite()
321 // Apply the application settings.
322 applyApplicationSettings();
324 // Get the arguments.
325 QStringList argumentsStringList = qApp->arguments();
327 // Check to see if the arguments lists contains a URL.
328 if (argumentsStringList.size() > 1)
330 // Load the URL from the arguments list.
331 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
335 // Load the homepage.
340 void BrowserView::loadProgress(const int &progress) const
342 // Show the progress bar.
343 emit showProgressBar(progress);
346 void BrowserView::loadStarted() const
348 // Show the progress bar.
349 emit showProgressBar(0);
352 void BrowserView::loadUrlFromLineEdit(QString url) const
354 // Decide if the text is more likely to be a URL or a search.
355 if (url.startsWith("file://")) // The text is likely a file URL.
358 webEngineViewPointer->load(QUrl::fromUserInput(url));
360 else if (url.contains(".")) // The text is likely a URL.
362 // Check if the URL does not start with a valid protocol.
363 if (!url.startsWith("http"))
365 // Add `https://` to the beginning of the URL.
366 url = "https://" + url;
370 webEngineViewPointer->load(QUrl::fromUserInput(url));
372 else // The text is likely a search.
375 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
379 void BrowserView::pageLinkHovered(const QString &linkUrl) const
381 // Emit a signal so that the browser window can update the status bar.
382 emit linkHovered(linkUrl);
385 void BrowserView::refresh() const
387 // Reload the website.
388 webEngineViewPointer->reload();
391 void BrowserView::toggleJavaScript() const
393 // Toggle JavaScript.
394 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
396 // Update the JavaScript icon.
397 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
399 // Reload the website.
400 webEngineViewPointer->reload();
403 void BrowserView::toggleLocalStorage() const
405 // Toggle local storage.
406 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
408 // Update the local storage icon.
409 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
411 // Reload the website.
412 webEngineViewPointer->reload();
415 void BrowserView::updateUrl(const QUrl &url) const
417 // Update the URL line edit.
418 emit updateUrlLineEdit(url);
420 // Update the status of the forward and back buttons.
421 emit updateBackAction(webEngineHistoryPointer->canGoBack());
422 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
424 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
425 webEngineViewPointer->setZoomFactor(currentZoomFactor);