From d37c965b5cc545db8845756ff2e059fd20a54869 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Sat, 12 Feb 2022 14:14:17 -0700 Subject: [PATCH] Prefer HTTPS for URLS without a protocol. --- src/BrowserWindow.cpp | 12 +++++------- src/BrowserWindow.h | 8 +++----- src/MainView.cpp | 38 ++++++++++++++++++++++---------------- src/MainView.h | 22 +++++++++------------- src/UserAgentHelper.cpp | 14 +++++++------- src/UserAgentHelper.h | 2 +- 6 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/BrowserWindow.cpp b/src/BrowserWindow.cpp index 546a951..e2f17bd 100644 --- a/src/BrowserWindow.cpp +++ b/src/BrowserWindow.cpp @@ -22,6 +22,7 @@ #include "Settings.h" #include "ui_SettingsPrivacy.h" #include "ui_SettingsGeneral.h" +#include "UserAgentHelper.h" // KDE Frameworks headers. #include @@ -53,7 +54,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() setupGUI(); } -void BrowserWindow::fileNew() +void BrowserWindow::fileNew() const { // Display a new instance of Privacy Browser. (new BrowserWindow)->show(); @@ -80,9 +81,6 @@ void BrowserWindow::settingsConfigure() QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent; userAgentLabelPointer = privacySettingsUi.userAgentLabel; - // Instantiate the user agent helper. - userAgentHelperPointer = new UserAgentHelper(); - // Display the initial user agent. updateUserAgentLabel(userAgentComboBoxPointer->currentText()); @@ -111,14 +109,14 @@ void BrowserWindow::settingsConfigure() } } -void BrowserWindow::updateStatusBar(const QString &statusBarMessage) +void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const { // Display the status bar message. statusBar()->showMessage(statusBarMessage); } -void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) +void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const { // Update the user agent label. - userAgentLabelPointer->setText(userAgentHelperPointer->getUserAgent(userAgentName)); + userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName)); } diff --git a/src/BrowserWindow.h b/src/BrowserWindow.h index 31ac510..7539099 100644 --- a/src/BrowserWindow.h +++ b/src/BrowserWindow.h @@ -22,7 +22,6 @@ // Application headers. #include "MainView.h" -#include "UserAgentHelper.h" // Qt framework headers. #include @@ -41,15 +40,14 @@ public: private Q_SLOTS: // Define the private slots. - void fileNew(); + void fileNew() const; void settingsConfigure(); - void updateStatusBar(const QString &statusBarMessage); - void updateUserAgentLabel(const QString &userAgentName); + void updateStatusBar(const QString &statusBarMessage) const; + void updateUserAgentLabel(const QString &userAgentName) const; private: // Define the private variables. MainView *mainViewPointer; QLabel *userAgentLabelPointer; - UserAgentHelper *userAgentHelperPointer; }; #endif diff --git a/src/MainView.cpp b/src/MainView.cpp index f879451..5acb447 100644 --- a/src/MainView.cpp +++ b/src/MainView.cpp @@ -23,6 +23,7 @@ #include "MouseEventFilter.h" #include "Settings.h" #include "ui_MainView.h" +#include "UserAgentHelper.h" // Qt framework headers. #include @@ -51,7 +52,7 @@ MainView::MainView(QWidget *parent) : QWidget(parent) webEngineSettingsPointer = webEngineViewPointer->settings(); // Update the webengine view from the URL line edit. - connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString))); + connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString))); // Update the URL line edit form the webengine view. connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); @@ -77,9 +78,6 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Disable the cache. webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache); - // Instantiate the user agent helper. - userAgentHelperPointer = new UserAgentHelper(); - // Don't allow JavaScript to open windows. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); @@ -111,18 +109,19 @@ MainView::MainView(QWidget *parent) : QWidget(parent) } } -void MainView::applyApplicationSettings() +void MainView::applyApplicationSettings() const { // TODO. } -void MainView::applyDomainSettingsAndReload() +// 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. +void MainView::applyDomainSettingsAndReload() const { // Apply the domain setings. `true` reloads the website. applyDomainSettings(true); } -void MainView::applyDomainSettings(bool reloadWebsite) +void MainView::applyDomainSettings(bool reloadWebsite) const { // Set the JavaScript status. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript()); @@ -138,7 +137,7 @@ void MainView::applyDomainSettings(bool reloadWebsite) } // Apply the user agent. - webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent())); + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent())); // Reload the website if requested. if (reloadWebsite) @@ -147,28 +146,35 @@ void MainView::applyDomainSettings(bool reloadWebsite) } } -void MainView::goHome() +void MainView::goHome() const { - // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing. - webEngineViewPointer->setUrl(Settings::homepage()); + // Load the homepage. + webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage().toString())); } -void MainView::loadUrl(const QString &urlFromUser) +void MainView::loadUrlFromTextBox(QString urlFromUser) const { // Remove the focus from the URL line edit. urlLineEditPointer->clearFocus(); - // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS. + // Check if the URL does not start with a valid protocol. + if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://")) + { + // Add `https://` to the beginning of the URL. + urlFromUser = "https://" + urlFromUser; + } + + // Load the URL. webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); } -void MainView::pageLinkHovered(const QString &linkUrl) +void MainView::pageLinkHovered(const QString &linkUrl) const { // Emit a signal so that the browser window can update the status bar. emit linkHovered(linkUrl); } -void MainView::toggleJavaScript() +void MainView::toggleJavaScript() const { // Toggle JavaScript. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); @@ -187,7 +193,7 @@ void MainView::toggleJavaScript() webEngineViewPointer->reload(); } -void MainView::updateInterface() +void MainView::updateInterface() const { // Update the URL line edit if it does not have focus. if (!urlLineEditPointer->hasFocus()) diff --git a/src/MainView.h b/src/MainView.h index f08975e..4509c66 100644 --- a/src/MainView.h +++ b/src/MainView.h @@ -20,9 +20,6 @@ #ifndef MAINVIEW_H #define MAINVIEW_H -// Application headers. -#include "UserAgentHelper.h" - // Qt framework headers. #include #include @@ -43,20 +40,20 @@ public: signals: // Define the signals. - void linkHovered(const QString &linkUrl); + void linkHovered(const QString &linkUrl) const; public Q_SLOTS: // Define the public slots. - void applyApplicationSettings(); - void applyDomainSettingsAndReload(); + void applyApplicationSettings() const; + void applyDomainSettingsAndReload() const; private Q_SLOTS: // Define the private slots. - void goHome(); - void loadUrl(const QString &urlFromUser); - void pageLinkHovered(const QString &linkUrl); - void toggleJavaScript(); - void updateInterface(); + void goHome() const; + void loadUrlFromTextBox(QString urlFromUser) const; + void pageLinkHovered(const QString &linkUrl) const; + void toggleJavaScript() const; + void updateInterface() const; private: // Define the private variables. @@ -68,9 +65,8 @@ private: QWebEngineProfile *webEngineProfilePointer; QWebEngineSettings *webEngineSettingsPointer; QWebEngineView *webEngineViewPointer; - UserAgentHelper *userAgentHelperPointer; // Define the private functions. - void applyDomainSettings(bool reloadWebsite); + void applyDomainSettings(bool reloadWebsite) const; }; #endif diff --git a/src/UserAgentHelper.cpp b/src/UserAgentHelper.cpp index d1182ef..960305b 100644 --- a/src/UserAgentHelper.cpp +++ b/src/UserAgentHelper.cpp @@ -25,31 +25,31 @@ UserAgentHelper::UserAgentHelper() {}; QString UserAgentHelper::getUserAgent(const QString &userAgentName) { - if (userAgentName == "Privacy Browser") + if (userAgentName == "Privacy Browser") // Privacy Browser. { return "PrivacyBrowser/1.0"; } - else if (userAgentName == "Firefox Linux") + else if (userAgentName == "Firefox Linux") // Firefox Linux. { return "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"; } - else if (userAgentName == "Chromium Linux") + else if (userAgentName == "Chromium Linux") // Chromium Linux. { return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"; } - else if (userAgentName == "Firefox Windows") + else if (userAgentName == "Firefox Windows") // Firefox Windows. { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0"; } - else if (userAgentName == "Chrome Windows") + else if (userAgentName == "Chrome Windows") // Chrome Windows. { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"; } - else if (userAgentName == "Edge Windows") + else if (userAgentName == "Edge Windows") // Edge Windows. { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34"; } - else if (userAgentName == "Safari macOS") + else if (userAgentName == "Safari macOS") // Safari macOS. { return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15"; } diff --git a/src/UserAgentHelper.h b/src/UserAgentHelper.h index 8d59e4f..f936968 100644 --- a/src/UserAgentHelper.h +++ b/src/UserAgentHelper.h @@ -30,6 +30,6 @@ public: UserAgentHelper(); // The public functions. - QString getUserAgent(const QString &userAgentName); + static QString getUserAgent(const QString &userAgentName); }; #endif -- 2.43.0