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 // Store a copy of each cookie when it is added.
63 connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
65 // Store a copy of the WebEngine default user agent.
66 webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
68 // Update the URL line edit when the URL changes.
69 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
71 // Update the progress bar.
72 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
73 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
74 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
76 // Instantiate the mouse event filter pointer.
77 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
79 // Install the mouse event filter.
80 qApp->installEventFilter(mouseEventFilterPointer);
82 // Process mouse forward and back commands.
83 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
84 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
86 // Listen for hovered link URLs.
87 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
89 // Instantiate the URL request interceptor.
90 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
92 // Set the URL request interceptor.
93 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
95 // Reapply the domain settings when the host changes.
96 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
98 // Don't allow JavaScript to open windows.
99 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
101 // Allow keyboard navigation.
102 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
104 // Enable full screen support.
105 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
107 // Require user interaction to play media.
108 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
110 // Limit WebRTC to public IP addresses.
111 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
113 // Set the focus on the WebEngine view.
114 webEngineViewPointer->setFocus();
117 BrowserView::~BrowserView()
119 // Delay the deletion of the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
120 webEnginePagePointer->deleteLater();
123 void BrowserView::applyApplicationSettings()
125 // Set the search engine URL.
126 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
128 // Emit the update search engine actions signal.
129 emit updateSearchEngineActions(Settings::searchEngine());
132 // 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.
133 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
134 void BrowserView::applyDomainSettingsAndReload()
136 // Apply the domain settings. `true` reloads the website.
137 applyDomainSettings(webEngineViewPointer->url().host(), true);
140 // 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.
141 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
142 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
144 // Apply the domain settings `false` does not reload the website.
145 applyDomainSettings(hostname, false);
148 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
149 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
151 // Get the record for the hostname.
152 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
154 // Check if the hostname has domain settings.
155 if (domainQuery.isValid()) // The hostname has domain settings.
157 // Get the domain record.
158 QSqlRecord domainRecord = domainQuery.record();
160 // Set the JavaScript status.
161 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
163 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
165 // Set the default JavaScript status.
166 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
171 case (DomainsDatabaseHelper::DISABLED):
173 // Disable JavaScript.
174 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
179 case (DomainsDatabaseHelper::ENABLED):
181 // Enable JavaScript.
182 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
188 // Set local storage.
189 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
191 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
193 // Set the default local storage status.
194 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
199 case (DomainsDatabaseHelper::DISABLED):
201 // Disable local storage.
202 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
207 case (DomainsDatabaseHelper::ENABLED):
209 // Enable local storage.
210 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
216 // Set the user agent.
217 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
219 // Check if a custom zoom factor is set.
220 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
222 // Store the current zoom factor.
223 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
227 // Reset the current zoom factor.
228 currentZoomFactor = Settings::zoomFactor();
231 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
232 webEngineViewPointer->setZoomFactor(currentZoomFactor);
234 // Apply the domain settings palette to the URL line edit.
235 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
237 else // The hostname does not have domain settings.
239 // Set the JavaScript status.
240 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
242 // Set local storage.
243 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
245 // Set the user agent.
246 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
248 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
249 currentZoomFactor = Settings::zoomFactor();
251 // Set the zoom factor.
252 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
254 // Apply the no domain settings palette to the URL line edit.
255 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
258 // Emit the update actions signals.
259 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
260 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
261 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
262 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
264 // Reload the website if requested.
267 webEngineViewPointer->reload();
271 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
273 // Store the search engine name.
274 QString searchEngineName = searchEngineActionPointer->text();
276 // Strip out any `&` characters.
277 searchEngineName.remove('&');
279 // Store the search engine string.
280 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
283 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
285 // Get the user agent name.
286 QString userAgentName = userAgentActionPointer->text();
288 // Strip out any `&` characters.
289 userAgentName.remove('&');
291 // Apply the user agent.
292 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
294 // Reload the website.
295 webEngineViewPointer->reload();
298 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
299 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
301 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
302 currentZoomFactor = zoomFactor;
304 // Set the zoom factor.
305 webEngineViewPointer->setZoomFactor(zoomFactor);
308 void BrowserView::back() const
311 webEngineViewPointer->back();
314 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
316 // Add the cookie to the cookie list.
317 emit addCookie(cookie);
320 void BrowserView::deleteAllCookies() const
322 // Delete all the cookies.
323 webEngineCookieStorePointer->deleteAllCookies();
326 void BrowserView::forward() const
329 webEngineViewPointer->forward();
332 void BrowserView::home() const
334 // Load the homepage.
335 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
338 void BrowserView::loadFinished() const
340 // Hide the progress bar.
341 emit hideProgressBar();
344 void BrowserView::loadInitialWebsite()
346 // Apply the application settings.
347 applyApplicationSettings();
349 // Get the arguments.
350 QStringList argumentsStringList = qApp->arguments();
352 // Check to see if the arguments lists contains a URL.
353 if (argumentsStringList.size() > 1)
355 // Load the URL from the arguments list.
356 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
360 // Load the homepage.
365 void BrowserView::loadProgress(const int &progress) const
367 // Show the progress bar.
368 emit showProgressBar(progress);
371 void BrowserView::loadStarted() const
373 // Show the progress bar.
374 emit showProgressBar(0);
377 void BrowserView::loadUrlFromLineEdit(QString url) const
379 // Decide if the text is more likely to be a URL or a search.
380 if (url.startsWith("file://")) // The text is likely a file URL.
383 webEngineViewPointer->load(QUrl::fromUserInput(url));
385 else if (url.contains(".")) // The text is likely a URL.
387 // Check if the URL does not start with a valid protocol.
388 if (!url.startsWith("http"))
390 // Add `https://` to the beginning of the URL.
391 url = "https://" + url;
395 webEngineViewPointer->load(QUrl::fromUserInput(url));
397 else // The text is likely a search.
400 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
404 void BrowserView::mouseBack() const
406 // Go back if possible.
407 if (webEngineHistoryPointer->canGoBack())
409 // Clear the URL line edit focus.
410 emit clearUrlLineEditFocus();
413 webEngineViewPointer->back();
417 void BrowserView::mouseForward() const
419 // Go forward if possible.
420 if (webEngineHistoryPointer->canGoForward())
422 // Clear the URL line edit focus.
423 emit clearUrlLineEditFocus();
426 webEngineViewPointer->forward();
430 void BrowserView::pageLinkHovered(const QString &linkUrl) const
432 // Emit a signal so that the browser window can update the status bar.
433 emit linkHovered(linkUrl);
436 void BrowserView::refresh() const
438 // Reload the website.
439 webEngineViewPointer->reload();
442 void BrowserView::toggleJavaScript() const
444 // Toggle JavaScript.
445 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
447 // Update the JavaScript icon.
448 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
450 // Reload the website.
451 webEngineViewPointer->reload();
454 void BrowserView::toggleLocalStorage() const
456 // Toggle local storage.
457 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
459 // Update the local storage icon.
460 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
462 // Reload the website.
463 webEngineViewPointer->reload();
466 void BrowserView::updateUrl(const QUrl &url) const
468 // Update the URL line edit.
469 emit updateUrlLineEdit(url);
471 // Update the status of the forward and back buttons.
472 emit updateBackAction(webEngineHistoryPointer->canGoBack());
473 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
475 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
476 webEngineViewPointer->setZoomFactor(currentZoomFactor);