2 * Copyright 2022-2023 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 "PrivacyWebEngineView.h"
23 #include "databases/CookiesDatabase.h"
24 #include "databases/DomainsDatabase.h"
25 #include "interceptors/UrlRequestInterceptor.h"
26 #include "windows/BrowserWindow.h"
28 // Qt toolkit headers.
29 #include <QContextMenuEvent>
32 // Construct the class.
33 PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr)
35 // Create an off-the-record profile (the default when no profile name is specified).
36 webEngineProfilePointer = new QWebEngineProfile(QLatin1String(""));
38 // Create a WebEngine page.
39 QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
41 // Set the WebEngine page.
42 setPage(webEnginePagePointer);
44 // Get handles for the various aspects of the WebEngine.
45 webEngineSettingsPointer = webEnginePagePointer->settings();
47 // Instantiate the URL request interceptor.
48 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
50 // Set the URL request interceptor.
51 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
53 // Reapply the domain settings when the host changes.
54 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
57 void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
59 //qDebug() << "Add cookie: " << cookie.toRawForm();
61 // Add the new cookie to the list.
62 cookieListPointer->push_front(cookie);
64 // Update the cookie if it is durable and has new data.
65 if (CookiesDatabase::isUpdate(cookie))
66 CookiesDatabase::updateCookie(cookie);
68 // Update the cookies action.
69 emit updateCookiesAction(cookieListPointer->size());
72 void PrivacyWebEngineView::applyDomainSettingsWithoutReloading(const QString &hostname)
74 // Apply the domain settings `false` does not reload the website.
75 applyDomainSettings(hostname, false);
78 void PrivacyWebEngineView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
80 // Get the record for the hostname.
81 QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname);
83 // Check if the hostname has domain settings.
84 if (domainQuery.isValid()) // The hostname has domain settings.
86 // Get the domain record.
87 QSqlRecord domainRecord = domainQuery.record();
89 // Store the domain settings name.
90 domainSettingsName = domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString();
92 // Set the JavaScript status.
93 switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt())
95 // Set the default JavaScript status.
96 case (DomainsDatabase::SYSTEM_DEFAULT):
98 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
103 // Disable JavaScript.
104 case (DomainsDatabase::DISABLED):
106 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
111 // Enable JavaScript.
112 case (DomainsDatabase::ENABLED):
114 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
120 // Set the local storage status.
121 switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt())
123 // Set the default local storage status.
124 case (DomainsDatabase::SYSTEM_DEFAULT):
126 localStorageEnabled = Settings::localStorageEnabled();
131 // Disable local storage.
132 case (DomainsDatabase::DISABLED):
134 localStorageEnabled = false;
139 // Enable local storage.
140 case (DomainsDatabase::ENABLED):
142 localStorageEnabled = true;
148 // Set the DOM storage status.
149 switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt())
151 // Set the default DOM storage status. QWebEngineSettings confusingly calls this local storage.
152 case (DomainsDatabase::SYSTEM_DEFAULT):
154 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
159 // Disable DOM storage. QWebEngineSettings confusingly calls this local storage.
160 case (DomainsDatabase::DISABLED):
162 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
167 // Enable DOM storage. QWebEngineSettings confusingly calls this local storage.
168 case (DomainsDatabase::ENABLED):
170 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
176 // Set the user agent.
177 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString()));
179 // Check if a custom zoom factor is set.
180 if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt())
182 // Store the current zoom factor.
183 setZoomFactor(domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble());
187 // Reset the current zoom factor.
188 setZoomFactor(Settings::zoomFactor());
191 else // The hostname does not have domain settings.
193 // Reset the domain settings name.
194 domainSettingsName = QLatin1String("");
196 // Set the JavaScript status.
197 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
199 // Set the local storage status.
200 localStorageEnabled = Settings::localStorageEnabled();
202 // Set DOM storage. In QWebEngineSettings it is called Local Storage.
203 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
205 // Set the user agent.
206 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
208 // Set the zoom factor.
209 setZoomFactor(Settings::zoomFactor());
212 // Reload the website if requested.
220 void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
221 // Get a handle for the
222 QWebEnginePage *webEnginePagePointer = page();
224 // Get a handle for the menu.
225 QMenu *contextMenu = webEnginePagePointer->createStandardContextMenu();
227 // Get the list of context menu actions.
228 const QList<QAction *> contextMenuActionsList = contextMenu->actions();
230 // Add the open link in new background tab action if the context menu already contains the open link in new window action.
231 if (contextMenuActionsList.contains(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow)))
232 contextMenu->insertAction(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow), webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewBackgroundTab));
234 // Display the menu using the location in the context menu event.
235 contextMenu->popup(contextMenuEvent->globalPos());
238 QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
239 // Get a handle for the browser window.
240 BrowserWindow *browserWindowPointer = qobject_cast<BrowserWindow*>(window());
242 // Create the requested window type.
243 switch (webWindowType)
245 case QWebEnginePage::WebBrowserTab:
247 // Create the new tab and return the privacy WebEngine view pointer. `true` removes the focus from the blank URL line edit.
248 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
249 return browserWindowPointer->tabWidgetPointer->addTab(true);
252 case QWebEnginePage::WebBrowserWindow:
254 // Create a new browser window.
255 BrowserWindow *newBrowserWindowPointer = new BrowserWindow();
257 // Show the new browser window.
258 newBrowserWindowPointer->show();
260 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
261 return newBrowserWindowPointer->tabWidgetPointer->loadBlankInitialWebsite();
264 case QWebEnginePage::WebBrowserBackgroundTab:
266 // Create the new tab and return the privacy WebEngine view pointer. `false` does not clear the URL line edit. `true` creates a background tab.
267 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
268 return browserWindowPointer->tabWidgetPointer->addTab(false, true);
273 // Return a null pointer for opening a web dialog.
279 void PrivacyWebEngineView::removeCookieFromList(const QNetworkCookie &cookie) const
281 //qDebug() << "Remove cookie: " << cookie.toRawForm();
283 // Remove the cookie from the list.
284 cookieListPointer->remove(cookie);
286 // Update the cookies action.
287 emit updateCookiesAction(cookieListPointer->size());