]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
Rename Local Storage to DOM Storage. https://redmine.stoutner.com/issues/852
[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 "Settings.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"
30
31 // Qt framework headers.
32 #include <QAction>
33
34 // Initialize the public static variables.
35 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
36
37 // Construct the class.
38 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
39 {
40     // Initialize the variables.
41     privacyWebEngineListPointer = new QList<PrivacyWebEngine*>;
42
43     // Instantiate the browser view UI.
44     Ui::BrowserView browserViewUi;
45
46     // Setup the UI.
47     browserViewUi.setupUi(this);
48
49     // Get handles for the views.
50     webEngineViewPointer = browserViewUi.webEngineView;
51
52     // Create an off-the-record profile (the default when no profile name is specified).
53     webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
54
55     // Create a WebEngine page.
56     webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
57
58     // Set the WebEngine page.
59     webEngineViewPointer->setPage(webEnginePagePointer);
60
61     // Get handles for the aspects of the WebEngine.
62     webEngineHistoryPointer = webEnginePagePointer->history();
63     webEngineSettingsPointer = webEngineViewPointer->settings();
64     webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
65
66     // Initialize the current privacy web engine pointer.
67     currentPrivacyWebEnginePointer = new PrivacyWebEngine(webEngineViewPointer);
68
69     // Populate the privacy web engine list.
70     privacyWebEngineListPointer->append(currentPrivacyWebEnginePointer);
71
72     // Set the cookie filter.
73     webEngineCookieStorePointer->setCookieFilter([this](const QWebEngineCookieStore::FilterRequest &filterRequest)
74     {
75         // qDebug() << "Cookie page URL:  " << filterRequest.firstPartyUrl << ", Cookie URL:  " << filterRequest.origin << ",  Is third-party:  " << filterRequest.thirdParty;
76
77         // Block all third party local storage requests, including the sneaky ones that don't register a first party URL.
78         if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral("")))
79             return false;
80
81         // Check each tab to see if this local storage request should be allowed.
82         for (PrivacyWebEngine *privacyWebEnginePointer : *privacyWebEngineListPointer)
83         {
84             // Allow this local storage request if it comes from a tab with local storage enabled.
85             if (privacyWebEnginePointer->cookiesEnabled && (webEngineViewPointer->url().host() == filterRequest.firstPartyUrl.host()))
86                 return true;
87         }
88
89         // Block any remaining local storage requests.
90         return false;
91     });
92
93     // Process cookie changes.
94     connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
95     connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), this, SLOT(cookieRemoved(QNetworkCookie)));
96
97     // Store a copy of the WebEngine default user agent.
98     webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
99
100     // Update the URL line edit when the URL changes.
101     connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
102
103     // Update the progress bar.
104     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
105     connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
106     connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
107
108     // Instantiate the mouse event filter pointer.
109     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
110
111     // Install the mouse event filter.
112     qApp->installEventFilter(mouseEventFilterPointer);
113
114     // Process mouse forward and back commands.
115     connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
116     connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
117
118     // Listen for hovered link URLs.
119     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
120
121     // Instantiate the URL request interceptor.
122     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
123
124     // Set the URL request interceptor.
125     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
126
127     // Reapply the domain settings when the host changes.
128     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
129
130     // Don't allow JavaScript to open windows.
131     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
132
133     // Allow keyboard navigation.
134     webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
135
136     // Enable full screen support.
137     webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
138
139     // Require user interaction to play media.
140     webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
141
142     // Limit WebRTC to public IP addresses.
143     webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
144
145     // Set the focus on the WebEngine view.
146     webEngineViewPointer->setFocus();
147 }
148
149 BrowserView::~BrowserView()
150 {
151     // Delay the deletion of the WebEngine page to prevent the following error:  `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
152     webEnginePagePointer->deleteLater();
153 }
154
155 // 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.
156 void BrowserView::addCookieToStore(QNetworkCookie cookie) const
157 {
158     // Create a url.
159     QUrl url;
160
161     // 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>
162     if (!cookie.domain().startsWith(QStringLiteral(".")))
163     {
164         // Populate the URL.
165         url.setHost(cookie.domain());
166         url.setScheme(QStringLiteral("https"));
167
168         // Clear the domain from the cookie.
169         cookie.setDomain(QStringLiteral(""));
170     }
171
172     // Add the cookie to the store.
173     webEngineCookieStorePointer->setCookie(cookie, url);
174 }
175
176 void BrowserView::applyApplicationSettings()
177 {
178     // Set the search engine URL.
179     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
180
181     // Emit the update search engine actions signal.
182     emit updateSearchEngineActions(Settings::searchEngine());
183 }
184
185 // 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.
186 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
187 void BrowserView::applyDomainSettingsAndReload()
188 {
189     // Apply the domain settings.  `true` reloads the website.
190     applyDomainSettings(webEngineViewPointer->url().host(), true);
191 }
192
193 // 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.
194 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
195 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
196 {
197     // Apply the domain settings  `false` does not reload the website.
198     applyDomainSettings(hostname, false);
199 }
200
201 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
202 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
203 {
204     // Get the record for the hostname.
205     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
206
207     // Check if the hostname has domain settings.
208     if (domainQuery.isValid())  // The hostname has domain settings.
209     {
210         // Get the domain record.
211         QSqlRecord domainRecord = domainQuery.record();
212
213         // Set the JavaScript status.
214         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
215         {
216             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
217             {
218                 // Set the default JavaScript status.
219                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
220
221                 break;
222             }
223
224             case (DomainsDatabaseHelper::DISABLED):
225             {
226                 // Disable JavaScript.
227                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
228
229                 break;
230             }
231
232             case (DomainsDatabaseHelper::ENABLED):
233             {
234                 // Enable JavaScript.
235                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
236
237                 break;
238             }
239         }
240
241         // Set the cookie status.  TODO.
242         currentPrivacyWebEnginePointer->cookiesEnabled = Settings::cookiesEnabled();
243
244         // Set DOM storage.
245         switch (domainRecord.field(DomainsDatabaseHelper::DOM_STORAGE).value().toInt())
246         {
247             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
248             {
249                 // Set the default DOM storage status.
250                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
251
252                 break;
253             }
254
255             case (DomainsDatabaseHelper::DISABLED):
256             {
257                 // Disable DOM storage.
258                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
259
260                 break;
261             }
262
263             case (DomainsDatabaseHelper::ENABLED):
264             {
265                 // Enable DOM storage.
266                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
267
268                 break;
269             }
270         }
271
272         // Set the user agent.
273         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
274
275         // Check if a custom zoom factor is set.
276         if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
277         {
278             // Store the current zoom factor.
279             currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
280         }
281         else
282         {
283             // Reset the current zoom factor.
284             currentZoomFactor = Settings::zoomFactor();
285         }
286
287         // Set the zoom factor.    The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
288         webEngineViewPointer->setZoomFactor(currentZoomFactor);
289
290         // Apply the domain settings palette to the URL line edit.
291         emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
292     }
293     else  // The hostname does not have domain settings.
294     {
295         // Set the JavaScript status.
296         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
297
298         // Set the cookie status.
299         currentPrivacyWebEnginePointer->cookiesEnabled = Settings::cookiesEnabled();
300
301         // Set DOM storage.
302         webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
303
304         // Set the user agent.
305         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
306
307         // Store the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
308         currentZoomFactor = Settings::zoomFactor();
309
310         // Set the zoom factor.
311         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
312
313         // Apply the no domain settings palette to the URL line edit.
314         emit updateDomainSettingsIndicator(false, QStringLiteral(""));
315     }
316
317     // Emit the update actions signals.
318     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
319     emit updateCookiesAction(currentPrivacyWebEnginePointer->cookiesEnabled);
320     emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
321     emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
322     emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
323
324     // Reload the website if requested.
325     if (reloadWebsite)
326     {
327         webEngineViewPointer->reload();
328     }
329 }
330
331 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
332 {
333     // Store the search engine name.
334     QString searchEngineName = searchEngineActionPointer->text();
335
336     // Strip out any `&` characters.
337     searchEngineName.remove('&');
338
339     // Store the search engine string.
340     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
341 }
342
343 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
344 {
345     // Get the user agent name.
346     QString userAgentName = userAgentActionPointer->text();
347
348     // Strip out any `&` characters.
349     userAgentName.remove('&');
350
351     // Apply the user agent.
352     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
353
354     // Reload the website.
355     webEngineViewPointer->reload();
356 }
357
358 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
359 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
360 {
361     // Update the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
362     currentZoomFactor = zoomFactor;
363
364     // Set the zoom factor.
365     webEngineViewPointer->setZoomFactor(zoomFactor);
366 }
367
368 void BrowserView::back() const
369 {
370     // Go back.
371     webEngineViewPointer->back();
372 }
373
374 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
375 {
376     // Add the cookie to the cookie list.
377     emit addCookie(cookie);
378 }
379
380 void BrowserView::cookieRemoved(const QNetworkCookie &cookie) const
381 {
382     // Remove the cookie from the cookie list.
383     emit removeCookie(cookie);
384 }
385
386 void BrowserView::deleteAllCookies() const
387 {
388     // Delete all the cookies.
389     webEngineCookieStorePointer->deleteAllCookies();
390 }
391
392 void BrowserView::deleteCookieFromStore(const QNetworkCookie &cookie) const
393 {
394     // Delete the cookie.
395     webEngineCookieStorePointer->deleteCookie(cookie);
396 }
397
398 void BrowserView::forward() const
399 {
400     // Go forward.
401     webEngineViewPointer->forward();
402 }
403
404 void BrowserView::home() const
405 {
406     // Load the homepage.
407     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
408 }
409
410 void BrowserView::loadFinished() const
411 {
412     // Hide the progress bar.
413     emit hideProgressBar();
414 }
415
416 void BrowserView::loadInitialWebsite()
417 {
418     // Apply the application settings.
419     applyApplicationSettings();
420
421     // Get the arguments.
422     QStringList argumentsStringList = qApp->arguments();
423
424     // Check to see if the arguments lists contains a URL.
425     if (argumentsStringList.size() > 1)
426     {
427         // Load the URL from the arguments list.
428         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
429     }
430     else
431     {
432         // Load the homepage.
433         home();
434     }
435 }
436
437 void BrowserView::loadProgress(const int &progress) const
438 {
439     // Show the progress bar.
440     emit showProgressBar(progress);
441 }
442
443 void BrowserView::loadStarted() const
444 {
445     // Show the progress bar.
446     emit showProgressBar(0);
447 }
448
449 void BrowserView::loadUrlFromLineEdit(QString url) const
450 {
451     // Decide if the text is more likely to be a URL or a search.
452     if (url.startsWith("file://"))  // The text is likely a file URL.
453     {
454         // Load the URL.
455         webEngineViewPointer->load(QUrl::fromUserInput(url));
456     }
457     else if (url.contains("."))  // The text is likely a URL.
458     {
459         // Check if the URL does not start with a valid protocol.
460         if (!url.startsWith("http"))
461         {
462             // Add `https://` to the beginning of the URL.
463             url = "https://" + url;
464         }
465
466         // Load the URL.
467         webEngineViewPointer->load(QUrl::fromUserInput(url));
468     }
469     else  // The text is likely a search.
470     {
471         // Load the search.
472         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
473     }
474 }
475
476 void BrowserView::mouseBack() const
477 {
478     // Go back if possible.
479     if (webEngineHistoryPointer->canGoBack())
480     {
481         // Clear the URL line edit focus.
482         emit clearUrlLineEditFocus();
483
484         // Go back.
485         webEngineViewPointer->back();
486     }
487 }
488
489 void BrowserView::mouseForward() const
490 {
491     // Go forward if possible.
492     if (webEngineHistoryPointer->canGoForward())
493     {
494         // Clear the URL line edit focus.
495         emit clearUrlLineEditFocus();
496
497         // Go forward.
498         webEngineViewPointer->forward();
499     }
500 }
501
502 void BrowserView::pageLinkHovered(const QString &linkUrl) const
503 {
504     // Emit a signal so that the browser window can update the status bar.
505     emit linkHovered(linkUrl);
506 }
507
508 void BrowserView::refresh() const
509 {
510     // Reload the website.
511     webEngineViewPointer->reload();
512 }
513
514 void BrowserView::toggleCookies()
515 {
516     // Toggle cookies.
517     currentPrivacyWebEnginePointer->cookiesEnabled = !currentPrivacyWebEnginePointer->cookiesEnabled;
518
519     // Update the cookies icon.
520     emit updateCookiesAction(currentPrivacyWebEnginePointer->cookiesEnabled);
521
522     // Reload the website.
523     webEngineViewPointer->reload();
524 }
525
526 void BrowserView::toggleJavaScript() const
527 {
528     // Toggle JavaScript.
529     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
530
531     // Update the JavaScript icon.
532     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
533
534     // Reload the website.
535     webEngineViewPointer->reload();
536 }
537
538 void BrowserView::toggleDomStorage() const
539 {
540     // Toggle DOM storage.
541     webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
542
543     // Update the DOM storage action icon.
544     emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
545
546     // Reload the website.
547     webEngineViewPointer->reload();
548 }
549
550 void BrowserView::updateUrl(const QUrl &url) const
551 {
552     // Update the URL line edit.
553     emit updateUrlLineEdit(url);
554
555     // Update the status of the forward and back buttons.
556     emit updateBackAction(webEngineHistoryPointer->canGoBack());
557     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
558
559     // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  <https://redmine.stoutner.com/issues/799>
560     webEngineViewPointer->setZoomFactor(currentZoomFactor);
561 }