]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
Add a WebEngine Default user agent.
[PrivacyBrowserPC.git] / src / views / BrowserView.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "BrowserView.h"
22 #include "MouseEventFilter.h"
23 #include "Settings.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"
30
31 // Qt framework headers.
32 #include <QAction>
33 #include <QWebEngineProfile>
34
35 // Initialize the public static variables.
36 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
37
38 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
39 {
40     // Instantiate the browser view UI.
41     Ui::BrowserView browserViewUi;
42
43     // Setup the UI.
44     browserViewUi.setupUi(this);
45
46     // Get handles for the views.
47     webEngineViewPointer = browserViewUi.webEngineView;
48
49     // Create an off-the-record profile (the default when no profile name is specified).
50     webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
51
52     // Create a WebEngine page.
53     QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
54
55     // Set the WebEngine page.
56     webEngineViewPointer->setPage(webEnginePagePointer);
57
58     // Get handles for the aspects of the WebEngine.
59     webEngineHistoryPointer = webEnginePagePointer->history();
60     webEngineSettingsPointer = webEngineViewPointer->settings();
61
62     // Store a copy of the WebEngine default user agent.
63     webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
64
65     // Update the URL line edit when the URL changes.
66     connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
67
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()));
72
73     // Instantiate the mouse event filter pointer.
74     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
75
76     // Install the mouse event filter.
77     qApp->installEventFilter(mouseEventFilterPointer);
78
79     // Listen for hovered link URLs.
80     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
81
82     // Instantiate the URL request interceptor.
83     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
84
85     // Set the URL request interceptor.
86     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
87
88     // Reapply the domain settings when the host changes.
89     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
90
91     // Don't allow JavaScript to open windows.
92     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
93
94     // Allow keyboard navigation.
95     webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
96
97     // Enable full screen support.
98     webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
99
100     // Require user interaction to play media.
101     webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
102
103     // Limit WebRTC to public IP addresses.
104     webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
105
106     // Set the focus on the WebEngine view.
107     webEngineViewPointer->setFocus();
108 }
109
110 void BrowserView::applyApplicationSettings()
111 {
112     // Set the search engine URL.
113     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
114
115     // Emit the update search engine actions signal.
116     emit updateSearchEngineActions(Settings::searchEngine());
117 }
118
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()
122 {
123     // Apply the domain settings.  `true` reloads the website.
124     applyDomainSettings(webEngineViewPointer->url().host(), true);
125 }
126
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)
130 {
131     // Apply the domain settings  `false` does not reload the website.
132     applyDomainSettings(hostname, false);
133 }
134
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)
137 {
138     // Get the record for the hostname.
139     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
140
141     // Check if the hostname has domain settings.
142     if (domainQuery.isValid())  // The hostname has domain settings.
143     {
144         // Get the domain record.
145         QSqlRecord domainRecord = domainQuery.record();
146
147         // Set the JavaScript status.
148         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
149         {
150             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
151             {
152                 // Set the default JavaScript status.
153                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
154
155                 break;
156             }
157
158             case (DomainsDatabaseHelper::DISABLED):
159             {
160                 // Disable JavaScript.
161                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
162
163                 break;
164             }
165
166             case (DomainsDatabaseHelper::ENABLED):
167             {
168                 // Enable JavaScript.
169                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
170
171                 break;
172             }
173         }
174
175         // Set local storage.
176         switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
177         {
178             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
179             {
180                 // Set the default local storage status.
181                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
182
183                 break;
184             }
185
186             case (DomainsDatabaseHelper::DISABLED):
187             {
188                 // Disable local storage.
189                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
190
191                 break;
192             }
193
194             case (DomainsDatabaseHelper::ENABLED):
195             {
196                 // Enable local storage.
197                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
198
199                 break;
200             }
201         }
202
203         // Set the user agent.
204         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
205
206         // Check if a custom zoom factor is set.
207         if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
208         {
209             // Store the current zoom factor.
210             currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
211         }
212         else
213         {
214             // Reset the current zoom factor.
215             currentZoomFactor = Settings::zoomFactor();
216         }
217
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);
220
221         // Apply the domain settings palette to the URL line edit.
222         emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
223     }
224     else  // The hostname does not have domain settings.
225     {
226         // Set the JavaScript status.
227         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
228
229         // Set local storage.
230         webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
231
232         // Set the user agent.
233         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
234
235         // Store the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
236         currentZoomFactor = Settings::zoomFactor();
237
238         // Set the zoom factor.
239         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
240
241         // Apply the no domain settings palette to the URL line edit.
242         emit updateDomainSettingsIndicator(false, QStringLiteral(""));
243     }
244
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());
250
251     // Reload the website if requested.
252     if (reloadWebsite)
253     {
254         webEngineViewPointer->reload();
255     }
256 }
257
258 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
259 {
260     // Store the search engine name.
261     QString searchEngineName = searchEngineActionPointer->text();
262
263     // Strip out any `&` characters.
264     searchEngineName.remove('&');
265
266     // Store the search engine string.
267     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
268 }
269
270 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
271 {
272     // Get the user agent name.
273     QString userAgentName = userAgentActionPointer->text();
274
275     // Strip out any `&` characters.
276     userAgentName.remove('&');
277
278     // Apply the user agent.
279     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
280
281     // Reload the website.
282     webEngineViewPointer->reload();
283 }
284
285 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
286 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
287 {
288     // Update the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
289     currentZoomFactor = zoomFactor;
290
291     // Set the zoom factor.
292     webEngineViewPointer->setZoomFactor(zoomFactor);
293 }
294
295 void BrowserView::back() const
296 {
297     // Go back.
298     webEngineViewPointer->back();
299 }
300
301 void BrowserView::forward() const
302 {
303     // Go forward.
304     webEngineViewPointer->forward();
305 }
306
307 void BrowserView::home() const
308 {
309     // Load the homepage.
310     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
311 }
312
313 void BrowserView::loadFinished() const
314 {
315     // Hide the progress bar.
316     emit hideProgressBar();
317 }
318
319 void BrowserView::loadInitialWebsite()
320 {
321     // Apply the application settings.
322     applyApplicationSettings();
323
324     // Get the arguments.
325     QStringList argumentsStringList = qApp->arguments();
326
327     // Check to see if the arguments lists contains a URL.
328     if (argumentsStringList.size() > 1)
329     {
330         // Load the URL from the arguments list.
331         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
332     }
333     else
334     {
335         // Load the homepage.
336         home();
337     }
338 }
339
340 void BrowserView::loadProgress(const int &progress) const
341 {
342     // Show the progress bar.
343     emit showProgressBar(progress);
344 }
345
346 void BrowserView::loadStarted() const
347 {
348     // Show the progress bar.
349     emit showProgressBar(0);
350 }
351
352 void BrowserView::loadUrlFromLineEdit(QString url) const
353 {
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.
356     {
357         // Load the URL.
358         webEngineViewPointer->load(QUrl::fromUserInput(url));
359     }
360     else if (url.contains("."))  // The text is likely a URL.
361     {
362         // Check if the URL does not start with a valid protocol.
363         if (!url.startsWith("http"))
364         {
365             // Add `https://` to the beginning of the URL.
366             url = "https://" + url;
367         }
368
369         // Load the URL.
370         webEngineViewPointer->load(QUrl::fromUserInput(url));
371     }
372     else  // The text is likely a search.
373     {
374         // Load the search.
375         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
376     }
377 }
378
379 void BrowserView::pageLinkHovered(const QString &linkUrl) const
380 {
381     // Emit a signal so that the browser window can update the status bar.
382     emit linkHovered(linkUrl);
383 }
384
385 void BrowserView::refresh() const
386 {
387     // Reload the website.
388     webEngineViewPointer->reload();
389 }
390
391 void BrowserView::toggleJavaScript() const
392 {
393     // Toggle JavaScript.
394     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
395
396     // Update the JavaScript icon.
397     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
398
399     // Reload the website.
400     webEngineViewPointer->reload();
401 }
402
403 void BrowserView::toggleLocalStorage() const
404 {
405     // Toggle local storage.
406     webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
407
408     // Update the local storage icon.
409     emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
410
411     // Reload the website.
412     webEngineViewPointer->reload();
413 }
414
415 void BrowserView::updateUrl(const QUrl &url) const
416 {
417     // Update the URL line edit.
418     emit updateUrlLineEdit(url);
419
420     // Update the status of the forward and back buttons.
421     emit updateBackAction(webEngineHistoryPointer->canGoBack());
422     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
423
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);
426 }