From 53a17c235589dede8390ff68c37806081aa1ae08 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Wed, 5 Sep 2018 16:21:59 -0400 Subject: [PATCH] Reduce use of static vars in browser plugin * Convert BrowserSettings into instanced class * Moved HostInstaller init into class constructor --- src/browser/BrowserAction.cpp | 4 +- src/browser/BrowserOptionDialog.cpp | 76 ++++++------ src/browser/BrowserService.cpp | 28 ++--- src/browser/BrowserService.h | 1 + src/browser/BrowserSettings.cpp | 21 ++-- src/browser/BrowserSettings.h | 178 +++++++++++++++------------- src/browser/HostInstaller.cpp | 74 +++++------- src/browser/HostInstaller.h | 14 +-- src/browser/NativeMessagingHost.cpp | 10 +- src/browser/NativeMessagingHost.h | 2 +- src/gui/MainWindow.cpp | 4 +- 11 files changed, 209 insertions(+), 203 deletions(-) diff --git a/src/browser/BrowserAction.cpp b/src/browser/BrowserAction.cpp index 2fcf4ee8..60cf8506 100644 --- a/src/browser/BrowserAction.cpp +++ b/src/browser/BrowserAction.cpp @@ -270,8 +270,8 @@ QJsonObject BrowserAction::handleGetLogins(const QJsonObject& json, const QStrin QJsonObject BrowserAction::handleGeneratePassword(const QJsonObject& json, const QString& action) { const QString nonce = json.value("nonce").toString(); - const QString password = BrowserSettings::generatePassword(); - const QString bits = QString::number(BrowserSettings::getbits()); // For some reason this always returns 1140 bits? + const QString password = browserSettings()->generatePassword(); + const QString bits = QString::number(browserSettings()->getbits()); // For some reason this always returns 1140 bits? if (nonce.isEmpty() || password.isEmpty()) { return QJsonObject(); diff --git a/src/browser/BrowserOptionDialog.cpp b/src/browser/BrowserOptionDialog.cpp index c5923c5d..e2ed8420 100644 --- a/src/browser/BrowserOptionDialog.cpp +++ b/src/browser/BrowserOptionDialog.cpp @@ -66,36 +66,36 @@ BrowserOptionDialog::~BrowserOptionDialog() void BrowserOptionDialog::loadSettings() { - BrowserSettings settings; - m_ui->enableBrowserSupport->setChecked(settings.isEnabled()); + auto settings = browserSettings(); + m_ui->enableBrowserSupport->setChecked(settings->isEnabled()); - m_ui->showNotification->setChecked(settings.showNotification()); - m_ui->bestMatchOnly->setChecked(settings.bestMatchOnly()); - m_ui->unlockDatabase->setChecked(settings.unlockDatabase()); - m_ui->matchUrlScheme->setChecked(settings.matchUrlScheme()); + m_ui->showNotification->setChecked(settings->showNotification()); + m_ui->bestMatchOnly->setChecked(settings->bestMatchOnly()); + m_ui->unlockDatabase->setChecked(settings->unlockDatabase()); + m_ui->matchUrlScheme->setChecked(settings->matchUrlScheme()); // hide unimplemented options // TODO: fix this m_ui->showNotification->hide(); - if (settings.sortByUsername()) { + if (settings->sortByUsername()) { m_ui->sortByUsername->setChecked(true); } else { m_ui->sortByTitle->setChecked(true); } - m_ui->alwaysAllowAccess->setChecked(settings.alwaysAllowAccess()); - m_ui->alwaysAllowUpdate->setChecked(settings.alwaysAllowUpdate()); - m_ui->searchInAllDatabases->setChecked(settings.searchInAllDatabases()); - m_ui->supportKphFields->setChecked(settings.supportKphFields()); - m_ui->supportBrowserProxy->setChecked(settings.supportBrowserProxy()); - m_ui->useCustomProxy->setChecked(settings.useCustomProxy()); - m_ui->customProxyLocation->setText(settings.customProxyLocation()); - m_ui->updateBinaryPath->setChecked(settings.updateBinaryPath()); - m_ui->chromeSupport->setChecked(settings.chromeSupport()); - m_ui->chromiumSupport->setChecked(settings.chromiumSupport()); - m_ui->firefoxSupport->setChecked(settings.firefoxSupport()); - m_ui->vivaldiSupport->setChecked(settings.vivaldiSupport()); + m_ui->alwaysAllowAccess->setChecked(settings->alwaysAllowAccess()); + m_ui->alwaysAllowUpdate->setChecked(settings->alwaysAllowUpdate()); + m_ui->searchInAllDatabases->setChecked(settings->searchInAllDatabases()); + m_ui->supportKphFields->setChecked(settings->supportKphFields()); + m_ui->supportBrowserProxy->setChecked(settings->supportBrowserProxy()); + m_ui->useCustomProxy->setChecked(settings->useCustomProxy()); + m_ui->customProxyLocation->setText(settings->customProxyLocation()); + m_ui->updateBinaryPath->setChecked(settings->updateBinaryPath()); + m_ui->chromeSupport->setChecked(settings->chromeSupport()); + m_ui->chromiumSupport->setChecked(settings->chromiumSupport()); + m_ui->firefoxSupport->setChecked(settings->firefoxSupport()); + m_ui->vivaldiSupport->setChecked(settings->vivaldiSupport()); #if defined(KEEPASSXC_DIST_APPIMAGE) m_ui->supportBrowserProxy->setChecked(true); @@ -113,28 +113,28 @@ void BrowserOptionDialog::loadSettings() void BrowserOptionDialog::saveSettings() { - BrowserSettings settings; - settings.setEnabled(m_ui->enableBrowserSupport->isChecked()); - settings.setShowNotification(m_ui->showNotification->isChecked()); - settings.setBestMatchOnly(m_ui->bestMatchOnly->isChecked()); - settings.setUnlockDatabase(m_ui->unlockDatabase->isChecked()); - settings.setMatchUrlScheme(m_ui->matchUrlScheme->isChecked()); - settings.setSortByUsername(m_ui->sortByUsername->isChecked()); + auto settings = browserSettings(); + settings->setEnabled(m_ui->enableBrowserSupport->isChecked()); + settings->setShowNotification(m_ui->showNotification->isChecked()); + settings->setBestMatchOnly(m_ui->bestMatchOnly->isChecked()); + settings->setUnlockDatabase(m_ui->unlockDatabase->isChecked()); + settings->setMatchUrlScheme(m_ui->matchUrlScheme->isChecked()); + settings->setSortByUsername(m_ui->sortByUsername->isChecked()); - settings.setSupportBrowserProxy(m_ui->supportBrowserProxy->isChecked()); - settings.setUseCustomProxy(m_ui->useCustomProxy->isChecked()); - settings.setCustomProxyLocation(m_ui->customProxyLocation->text()); + settings->setSupportBrowserProxy(m_ui->supportBrowserProxy->isChecked()); + settings->setUseCustomProxy(m_ui->useCustomProxy->isChecked()); + settings->setCustomProxyLocation(m_ui->customProxyLocation->text()); - settings.setUpdateBinaryPath(m_ui->updateBinaryPath->isChecked()); - settings.setAlwaysAllowAccess(m_ui->alwaysAllowAccess->isChecked()); - settings.setAlwaysAllowUpdate(m_ui->alwaysAllowUpdate->isChecked()); - settings.setSearchInAllDatabases(m_ui->searchInAllDatabases->isChecked()); - settings.setSupportKphFields(m_ui->supportKphFields->isChecked()); + settings->setUpdateBinaryPath(m_ui->updateBinaryPath->isChecked()); + settings->setAlwaysAllowAccess(m_ui->alwaysAllowAccess->isChecked()); + settings->setAlwaysAllowUpdate(m_ui->alwaysAllowUpdate->isChecked()); + settings->setSearchInAllDatabases(m_ui->searchInAllDatabases->isChecked()); + settings->setSupportKphFields(m_ui->supportKphFields->isChecked()); - settings.setChromeSupport(m_ui->chromeSupport->isChecked()); - settings.setChromiumSupport(m_ui->chromiumSupport->isChecked()); - settings.setFirefoxSupport(m_ui->firefoxSupport->isChecked()); - settings.setVivaldiSupport(m_ui->vivaldiSupport->isChecked()); + settings->setChromeSupport(m_ui->chromeSupport->isChecked()); + settings->setChromiumSupport(m_ui->chromiumSupport->isChecked()); + settings->setFirefoxSupport(m_ui->firefoxSupport->isChecked()); + settings->setVivaldiSupport(m_ui->vivaldiSupport->isChecked()); } void BrowserOptionDialog::showProxyLocationFileDialog() diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index e2ee6cac..bba651d5 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -34,7 +34,6 @@ #include "core/PasswordGenerator.h" #include "gui/MainWindow.h" -static const QUuid KEEPASSXCBROWSER_UUID = QUuid::fromRfc4122(QByteArray::fromHex("de887cc3036343b8974b5911b8816224")); static const char KEEPASSXCBROWSER_NAME[] = "KeePassXC-Browser Settings"; static const char ASSOCIATE_KEY_PREFIX[] = "Public Key: "; static const char KEEPASSXCBROWSER_GROUP_NAME[] = "KeePassXC-Browser Passwords"; @@ -44,6 +43,7 @@ BrowserService::BrowserService(DatabaseTabWidget* parent) : m_dbTabWidget(parent) , m_dialogActive(false) , m_bringToFrontRequested(false) + , m_keepassBrowserUUID(QUuid::fromRfc4122(QByteArray::fromHex("de887cc3036343b8974b5911b8816224"))) { connect(m_dbTabWidget, SIGNAL(databaseLocked(DatabaseWidget*)), this, SLOT(databaseLocked(DatabaseWidget*))); connect(m_dbTabWidget, SIGNAL(databaseUnlocked(DatabaseWidget*)), this, SLOT(databaseUnlocked(DatabaseWidget*))); @@ -65,7 +65,7 @@ bool BrowserService::isDatabaseOpened() const bool BrowserService::openDatabase(bool triggerUnlock) { - if (!BrowserSettings::unlockDatabase()) { + if (!browserSettings()->unlockDatabase()) { return false; } @@ -139,11 +139,11 @@ Entry* BrowserService::getConfigEntry(bool create) return nullptr; } - entry = db->resolveEntry(KEEPASSXCBROWSER_UUID); + entry = db->resolveEntry(m_keepassBrowserUUID); if (!entry && create) { entry = new Entry(); entry->setTitle(QLatin1String(KEEPASSXCBROWSER_NAME)); - entry->setUuid(KEEPASSXCBROWSER_UUID); + entry->setUuid(m_keepassBrowserUUID); entry->setAutoTypeEnabled(false); entry->setGroup(db->rootGroup()); return entry; @@ -243,7 +243,7 @@ QJsonArray BrowserService::findMatchingEntries(const QString& id, return result; } - const bool alwaysAllowAccess = BrowserSettings::alwaysAllowAccess(); + const bool alwaysAllowAccess = browserSettings()->alwaysAllowAccess(); const QString host = QUrl(url).host(); const QString submitHost = QUrl(submitUrl).host(); @@ -358,7 +358,7 @@ void BrowserService::updateEntry(const QString& id, if (username.compare(login, Qt::CaseSensitive) != 0 || entry->password().compare(password, Qt::CaseSensitive) != 0) { int dialogResult = QMessageBox::No; - if (!BrowserSettings::alwaysAllowUpdate()) { + if (!browserSettings()->alwaysAllowUpdate()) { QMessageBox msgBox; msgBox.setWindowTitle(tr("KeePassXC: Update Entry")); msgBox.setText(tr("Do you want to update the information in %1 - %2?").arg(QUrl(url).host()).arg(username)); @@ -371,7 +371,7 @@ void BrowserService::updateEntry(const QString& id, dialogResult = msgBox.exec(); } - if (BrowserSettings::alwaysAllowUpdate() || dialogResult == QMessageBox::Yes) { + if (browserSettings()->alwaysAllowUpdate() || dialogResult == QMessageBox::Yes) { entry->beginUpdate(); entry->setUsername(login); entry->setPassword(password); @@ -396,7 +396,7 @@ QList BrowserService::searchEntries(Database* db, const QString& hostnam // Ignore entry if port or scheme defined in the URL doesn't match if ((entryQUrl.port() > 0 && entryQUrl.port() != qUrl.port()) || - (BrowserSettings::matchUrlScheme() && entryScheme.compare(qUrl.scheme()) != 0)) { + (browserSettings()->matchUrlScheme() && entryScheme.compare(qUrl.scheme()) != 0)) { continue; } @@ -414,14 +414,14 @@ QList BrowserService::searchEntries(const QString& url, const StringPair { // Get the list of databases to search QList databases; - if (BrowserSettings::searchInAllDatabases()) { + if (browserSettings()->searchInAllDatabases()) { const int count = m_dbTabWidget->count(); for (int i = 0; i < count; ++i) { if (DatabaseWidget* dbWidget = qobject_cast(m_dbTabWidget->widget(i))) { if (Database* db = dbWidget->database()) { // Check if database is connected with KeePassXC-Browser for (const StringPair keyPair : keyList) { - Entry* entry = db->resolveEntry(KEEPASSXCBROWSER_UUID); + Entry* entry = db->resolveEntry(m_keepassBrowserUUID); if (entry) { QString key = entry->attributes()->value(QLatin1String(ASSOCIATE_KEY_PREFIX) + keyPair.first); if (!key.isEmpty() && keyPair.second == key) { @@ -564,18 +564,18 @@ QList BrowserService::sortEntries(QList& pwEntries, const QStrin } QList results; - QString field = BrowserSettings::sortByTitle() ? "Title" : "UserName"; + QString field = browserSettings()->sortByTitle() ? "Title" : "UserName"; for (int i = 100; i >= 0; i -= 5) { if (priorities.count(i) > 0) { // Sort same priority entries by Title or UserName auto entries = priorities.values(i); - std::sort(entries.begin(), entries.end(), [&priorities, &field](Entry* left, Entry* right) { + std::sort(entries.begin(), entries.end(), [&field](Entry* left, Entry* right) { return (QString::localeAwareCompare(left->attributes()->value(field), right->attributes()->value(field)) < 0) || ((QString::localeAwareCompare(left->attributes()->value(field), right->attributes()->value(field)) == 0) && (QString::localeAwareCompare(left->attributes()->value("UserName"), right->attributes()->value("UserName")) < 0)); }); results << entries; - if (BrowserSettings::bestMatchOnly() && !pwEntries.isEmpty()) { + if (browserSettings()->bestMatchOnly() && !pwEntries.isEmpty()) { // Early out once we find the highest batch of matches break; } @@ -642,7 +642,7 @@ QJsonObject BrowserService::prepareEntry(const Entry* entry) res["totp"] = entry->totp(); } - if (BrowserSettings::supportKphFields()) { + if (browserSettings()->supportKphFields()) { const EntryAttributes* attr = entry->attributes(); QJsonArray stringFields; for (const QString& key : attr->keys()) { diff --git a/src/browser/BrowserService.h b/src/browser/BrowserService.h index 4c6797d9..10c3a786 100644 --- a/src/browser/BrowserService.h +++ b/src/browser/BrowserService.h @@ -107,6 +107,7 @@ private: DatabaseTabWidget* const m_dbTabWidget; bool m_dialogActive; bool m_bringToFrontRequested; + QUuid m_keepassBrowserUUID; }; #endif // BROWSERSERVICE_H diff --git a/src/browser/BrowserSettings.cpp b/src/browser/BrowserSettings.cpp index 76163b9d..f7abdece 100644 --- a/src/browser/BrowserSettings.cpp +++ b/src/browser/BrowserSettings.cpp @@ -20,9 +20,16 @@ #include "BrowserSettings.h" #include "core/Config.h" -PasswordGenerator BrowserSettings::m_passwordGenerator; -PassphraseGenerator BrowserSettings::m_passPhraseGenerator; -HostInstaller BrowserSettings::m_hostInstaller; +BrowserSettings* BrowserSettings::m_instance(nullptr); + +BrowserSettings* BrowserSettings::instance() +{ + if (!m_instance) { + m_instance = new BrowserSettings(); + } + + return m_instance; +} bool BrowserSettings::isEnabled() { @@ -185,7 +192,7 @@ bool BrowserSettings::chromeSupport() void BrowserSettings::setChromeSupport(bool enabled) { m_hostInstaller.installBrowser( - HostInstaller::SupportedBrowsers::CHROME, enabled, supportBrowserProxy(), customProxyLocation()); + HostInstaller::SupportedBrowsers::CHROME, enabled, supportBrowserProxy(), customProxyLocation()); } bool BrowserSettings::chromiumSupport() @@ -196,7 +203,7 @@ bool BrowserSettings::chromiumSupport() void BrowserSettings::setChromiumSupport(bool enabled) { m_hostInstaller.installBrowser( - HostInstaller::SupportedBrowsers::CHROMIUM, enabled, supportBrowserProxy(), customProxyLocation()); + HostInstaller::SupportedBrowsers::CHROMIUM, enabled, supportBrowserProxy(), customProxyLocation()); } bool BrowserSettings::firefoxSupport() @@ -207,7 +214,7 @@ bool BrowserSettings::firefoxSupport() void BrowserSettings::setFirefoxSupport(bool enabled) { m_hostInstaller.installBrowser( - HostInstaller::SupportedBrowsers::FIREFOX, enabled, supportBrowserProxy(), customProxyLocation()); + HostInstaller::SupportedBrowsers::FIREFOX, enabled, supportBrowserProxy(), customProxyLocation()); } bool BrowserSettings::vivaldiSupport() @@ -218,7 +225,7 @@ bool BrowserSettings::vivaldiSupport() void BrowserSettings::setVivaldiSupport(bool enabled) { m_hostInstaller.installBrowser( - HostInstaller::SupportedBrowsers::VIVALDI, enabled, supportBrowserProxy(), customProxyLocation()); + HostInstaller::SupportedBrowsers::VIVALDI, enabled, supportBrowserProxy(), customProxyLocation()); } bool BrowserSettings::passwordUseNumbers() diff --git a/src/browser/BrowserSettings.h b/src/browser/BrowserSettings.h index 3c79dbed..3e84ba37 100644 --- a/src/browser/BrowserSettings.h +++ b/src/browser/BrowserSettings.h @@ -27,95 +27,105 @@ class BrowserSettings { public: - static bool isEnabled(); - static void setEnabled(bool enabled); + explicit BrowserSettings() = default; + static BrowserSettings* instance(); - static bool showNotification(); // TODO!! - static void setShowNotification(bool showNotification); - static bool bestMatchOnly(); - static void setBestMatchOnly(bool bestMatchOnly); - static bool unlockDatabase(); - static void setUnlockDatabase(bool unlockDatabase); - static bool matchUrlScheme(); - static void setMatchUrlScheme(bool matchUrlScheme); - static bool sortByUsername(); - static void setSortByUsername(bool sortByUsername = true); - static bool sortByTitle(); - static void setSortByTitle(bool sortByUsertitle = true); - static bool alwaysAllowAccess(); - static void setAlwaysAllowAccess(bool alwaysAllowAccess); - static bool alwaysAllowUpdate(); - static void setAlwaysAllowUpdate(bool alwaysAllowUpdate); - static bool searchInAllDatabases(); - static void setSearchInAllDatabases(bool searchInAllDatabases); - static bool supportKphFields(); - static void setSupportKphFields(bool supportKphFields); + bool isEnabled(); + void setEnabled(bool enabled); - static bool supportBrowserProxy(); - static void setSupportBrowserProxy(bool enabled); - static bool useCustomProxy(); - static void setUseCustomProxy(bool enabled); - static QString customProxyLocation(); - static void setCustomProxyLocation(QString location); - static bool updateBinaryPath(); - static void setUpdateBinaryPath(bool enabled); - static bool chromeSupport(); - static void setChromeSupport(bool enabled); - static bool chromiumSupport(); - static void setChromiumSupport(bool enabled); - static bool firefoxSupport(); - static void setFirefoxSupport(bool enabled); - static bool vivaldiSupport(); - static void setVivaldiSupport(bool enabled); + bool showNotification(); // TODO!! + void setShowNotification(bool showNotification); + bool bestMatchOnly(); + void setBestMatchOnly(bool bestMatchOnly); + bool unlockDatabase(); + void setUnlockDatabase(bool unlockDatabase); + bool matchUrlScheme(); + void setMatchUrlScheme(bool matchUrlScheme); + bool sortByUsername(); + void setSortByUsername(bool sortByUsername = true); + bool sortByTitle(); + void setSortByTitle(bool sortByUsertitle = true); + bool alwaysAllowAccess(); + void setAlwaysAllowAccess(bool alwaysAllowAccess); + bool alwaysAllowUpdate(); + void setAlwaysAllowUpdate(bool alwaysAllowUpdate); + bool searchInAllDatabases(); + void setSearchInAllDatabases(bool searchInAllDatabases); + bool supportKphFields(); + void setSupportKphFields(bool supportKphFields); - static bool passwordUseNumbers(); - static void setPasswordUseNumbers(bool useNumbers); - static bool passwordUseLowercase(); - static void setPasswordUseLowercase(bool useLowercase); - static bool passwordUseUppercase(); - static void setPasswordUseUppercase(bool useUppercase); - static bool passwordUseSpecial(); - static void setPasswordUseSpecial(bool useSpecial); - static bool passwordUseBraces(); - static void setPasswordUseBraces(bool useBraces); - static bool passwordUsePunctuation(); - static void setPasswordUsePunctuation(bool usePunctuation); - static bool passwordUseQuotes(); - static void setPasswordUseQuotes(bool useQuotes); - static bool passwordUseDashes(); - static void setPasswordUseDashes(bool useDashes); - static bool passwordUseMath(); - static void setPasswordUseMath(bool useMath); - static bool passwordUseLogograms(); - static void setPasswordUseLogograms(bool useLogograms); - static bool passwordUseEASCII(); - static void setPasswordUseEASCII(bool useEASCII); - static bool advancedMode(); - static void setAdvancedMode(bool advancedMode); - static QString passwordExcludedChars(); - static void setPasswordExcludedChars(QString chars); - static int passPhraseWordCount(); - static void setPassPhraseWordCount(int wordCount); - static QString passPhraseWordSeparator(); - static void setPassPhraseWordSeparator(QString separator); - static int generatorType(); - static void setGeneratorType(int type); - static bool passwordEveryGroup(); - static void setPasswordEveryGroup(bool everyGroup); - static bool passwordExcludeAlike(); - static void setPasswordExcludeAlike(bool excludeAlike); - static int passwordLength(); - static void setPasswordLength(int length); - static PasswordGenerator::CharClasses passwordCharClasses(); - static PasswordGenerator::GeneratorFlags passwordGeneratorFlags(); - static QString generatePassword(); - static int getbits(); - static void updateBinaryPaths(QString customProxyLocation = QString()); + bool supportBrowserProxy(); + void setSupportBrowserProxy(bool enabled); + bool useCustomProxy(); + void setUseCustomProxy(bool enabled); + QString customProxyLocation(); + void setCustomProxyLocation(QString location); + bool updateBinaryPath(); + void setUpdateBinaryPath(bool enabled); + bool chromeSupport(); + void setChromeSupport(bool enabled); + bool chromiumSupport(); + void setChromiumSupport(bool enabled); + bool firefoxSupport(); + void setFirefoxSupport(bool enabled); + bool vivaldiSupport(); + void setVivaldiSupport(bool enabled); + + bool passwordUseNumbers(); + void setPasswordUseNumbers(bool useNumbers); + bool passwordUseLowercase(); + void setPasswordUseLowercase(bool useLowercase); + bool passwordUseUppercase(); + void setPasswordUseUppercase(bool useUppercase); + bool passwordUseSpecial(); + void setPasswordUseSpecial(bool useSpecial); + bool passwordUseBraces(); + void setPasswordUseBraces(bool useBraces); + bool passwordUsePunctuation(); + void setPasswordUsePunctuation(bool usePunctuation); + bool passwordUseQuotes(); + void setPasswordUseQuotes(bool useQuotes); + bool passwordUseDashes(); + void setPasswordUseDashes(bool useDashes); + bool passwordUseMath(); + void setPasswordUseMath(bool useMath); + bool passwordUseLogograms(); + void setPasswordUseLogograms(bool useLogograms); + bool passwordUseEASCII(); + void setPasswordUseEASCII(bool useEASCII); + bool advancedMode(); + void setAdvancedMode(bool advancedMode); + QString passwordExcludedChars(); + void setPasswordExcludedChars(QString chars); + int passPhraseWordCount(); + void setPassPhraseWordCount(int wordCount); + QString passPhraseWordSeparator(); + void setPassPhraseWordSeparator(QString separator); + int generatorType(); + void setGeneratorType(int type); + bool passwordEveryGroup(); + void setPasswordEveryGroup(bool everyGroup); + bool passwordExcludeAlike(); + void setPasswordExcludeAlike(bool excludeAlike); + int passwordLength(); + void setPasswordLength(int length); + PasswordGenerator::CharClasses passwordCharClasses(); + PasswordGenerator::GeneratorFlags passwordGeneratorFlags(); + QString generatePassword(); + int getbits(); + void updateBinaryPaths(QString customProxyLocation = QString()); private: - static PasswordGenerator m_passwordGenerator; - static PassphraseGenerator m_passPhraseGenerator; - static HostInstaller m_hostInstaller; + static BrowserSettings* m_instance; + + PasswordGenerator m_passwordGenerator; + PassphraseGenerator m_passPhraseGenerator; + HostInstaller m_hostInstaller; }; +inline BrowserSettings* browserSettings() +{ + return BrowserSettings::instance(); +} + #endif // BROWSERSETTINGS_H diff --git a/src/browser/HostInstaller.cpp b/src/browser/HostInstaller.cpp index 95d202a2..99d09f4f 100644 --- a/src/browser/HostInstaller.cpp +++ b/src/browser/HostInstaller.cpp @@ -18,6 +18,7 @@ #include "HostInstaller.h" #include "config-keepassx.h" + #include #include #include @@ -27,35 +28,27 @@ #include #include -const QString HostInstaller::HOST_NAME = "org.keepassxc.keepassxc_browser"; -const QStringList HostInstaller::ALLOWED_ORIGINS = QStringList() - << "chrome-extension://iopaggbpplllidnfmcghoonnokmjoicf/" - << "chrome-extension://oboonakemofpalcgghocfoadofidjkkk/"; - -const QStringList HostInstaller::ALLOWED_EXTENSIONS = QStringList() << "keepassxc-browser@keepassxc.org"; - -#if defined(Q_OS_OSX) -const QString HostInstaller::TARGET_DIR_CHROME = "/Library/Application Support/Google/Chrome/NativeMessagingHosts"; -const QString HostInstaller::TARGET_DIR_CHROMIUM = "/Library/Application Support/Chromium/NativeMessagingHosts"; -const QString HostInstaller::TARGET_DIR_FIREFOX = "/Library/Application Support/Mozilla/NativeMessagingHosts"; -const QString HostInstaller::TARGET_DIR_VIVALDI = "/Library/Application Support/Vivaldi/NativeMessagingHosts"; -#elif defined(Q_OS_LINUX) -const QString HostInstaller::TARGET_DIR_CHROME = "/.config/google-chrome/NativeMessagingHosts"; -const QString HostInstaller::TARGET_DIR_CHROMIUM = "/.config/chromium/NativeMessagingHosts"; -const QString HostInstaller::TARGET_DIR_FIREFOX = "/.mozilla/native-messaging-hosts"; -const QString HostInstaller::TARGET_DIR_VIVALDI = "/.config/vivaldi/NativeMessagingHosts"; -#elif defined(Q_OS_WIN) -const QString HostInstaller::TARGET_DIR_CHROME = - "HKEY_CURRENT_USER\\Software\\Google\\Chrome\\NativeMessagingHosts\\" + HostInstaller::HOST_NAME; -const QString HostInstaller::TARGET_DIR_CHROMIUM = - "HKEY_CURRENT_USER\\Software\\Chromium\\NativeMessagingHosts\\" + HostInstaller::HOST_NAME; -const QString HostInstaller::TARGET_DIR_FIREFOX = - "HKEY_CURRENT_USER\\Software\\Mozilla\\NativeMessagingHosts\\" + HostInstaller::HOST_NAME; -const QString HostInstaller::TARGET_DIR_VIVALDI = - "HKEY_CURRENT_USER\\Software\\Vivaldi\\NativeMessagingHosts\\" + HostInstaller::HOST_NAME; -#endif - HostInstaller::HostInstaller() + : HOST_NAME("org.keepassxc.keepassxc_browser") + , ALLOWED_EXTENSIONS(QStringList() << "keepassxc-browser@keepassxc.org") + , ALLOWED_ORIGINS(QStringList() << "chrome-extension://iopaggbpplllidnfmcghoonnokmjoicf/" + << "chrome-extension://oboonakemofpalcgghocfoadofidjkkk/") +#if defined(Q_OS_OSX) + , TARGET_DIR_CHROME("/Library/Application Support/Google/Chrome/NativeMessagingHosts") + , TARGET_DIR_CHROMIUM("/Library/Application Support/Chromium/NativeMessagingHosts") + , TARGET_DIR_FIREFOX("/Library/Application Support/Mozilla/NativeMessagingHosts") + , TARGET_DIR_VIVALDI("/Library/Application Support/Vivaldi/NativeMessagingHosts") +#elif defined(Q_OS_LINUX) + , TARGET_DIR_CHROME("/.config/google-chrome/NativeMessagingHosts") + , TARGET_DIR_CHROMIUM("/.config/chromium/NativeMessagingHosts") + , TARGET_DIR_FIREFOX("/.mozilla/native-messaging-hosts") + , TARGET_DIR_VIVALDI("/.config/vivaldi/NativeMessagingHosts") +#elif defined(Q_OS_WIN) + , TARGET_DIR_CHROME("HKEY_CURRENT_USER\\Software\\Google\\Chrome\\NativeMessagingHosts\\org.keepassxc.keepassxc_browser") + , TARGET_DIR_CHROMIUM("HKEY_CURRENT_USER\\Software\\Chromium\\NativeMessagingHosts\\org.keepassxc.keepassxc_browser") + , TARGET_DIR_FIREFOX("HKEY_CURRENT_USER\\Software\\Mozilla\\NativeMessagingHosts\\org.keepassxc.keepassxc_browser") + , TARGET_DIR_VIVALDI("HKEY_CURRENT_USER\\Software\\Vivaldi\\NativeMessagingHosts\\org.keepassxc.keepassxc_browser") +#endif { } @@ -118,13 +111,13 @@ QString HostInstaller::getTargetPath(SupportedBrowsers browser) const { switch (browser) { case SupportedBrowsers::CHROME: - return HostInstaller::TARGET_DIR_CHROME; + return TARGET_DIR_CHROME; case SupportedBrowsers::CHROMIUM: - return HostInstaller::TARGET_DIR_CHROMIUM; + return TARGET_DIR_CHROMIUM; case SupportedBrowsers::FIREFOX: - return HostInstaller::TARGET_DIR_FIREFOX; + return TARGET_DIR_FIREFOX; case SupportedBrowsers::VIVALDI: - return HostInstaller::TARGET_DIR_VIVALDI; + return TARGET_DIR_VIVALDI; default: return QString(); } @@ -158,12 +151,12 @@ QString HostInstaller::getPath(SupportedBrowsers browser) const userPath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); } - QString winPath = QString("%1/%2_%3.json").arg(userPath, HostInstaller::HOST_NAME, getBrowserName(browser)); + QString winPath = QString("%1/%2_%3.json").arg(userPath, HOST_NAME, getBrowserName(browser)); winPath.replace("/", "\\"); return winPath; #else QString path = getTargetPath(browser); - return QString("%1%2/%3.json").arg(QDir::homePath(), path, HostInstaller::HOST_NAME); + return QString("%1%2/%3.json").arg(QDir::homePath(), path, HOST_NAME); #endif } @@ -207,19 +200,19 @@ QJsonObject HostInstaller::constructFile(SupportedBrowsers browser, const bool& #endif // #ifdef KEEPASSXC_DIST_APPIMAGE QJsonObject script; - script["name"] = HostInstaller::HOST_NAME; + script["name"] = HOST_NAME; script["description"] = "KeePassXC integration with native messaging support"; script["path"] = path; script["type"] = "stdio"; QJsonArray arr; if (browser == SupportedBrowsers::FIREFOX) { - for (const QString extension : HostInstaller::ALLOWED_EXTENSIONS) { + for (const QString& extension : ALLOWED_EXTENSIONS) { arr.append(extension); } script["allowed_extensions"] = arr; } else { - for (const QString origin : HostInstaller::ALLOWED_ORIGINS) { + for (const QString& origin : ALLOWED_ORIGINS) { arr.append(origin); } script["allowed_origins"] = arr; @@ -248,10 +241,5 @@ bool HostInstaller::saveFile(SupportedBrowsers browser, const QJsonObject& scrip } QJsonDocument doc(script); - qint64 bytesWritten = scriptFile.write(doc.toJson()); - if (bytesWritten < 0) { - return false; - } - - return true; + return scriptFile.write(doc.toJson()) >= 0; } diff --git a/src/browser/HostInstaller.h b/src/browser/HostInstaller.h index 2fac0609..3b985c30 100644 --- a/src/browser/HostInstaller.h +++ b/src/browser/HostInstaller.h @@ -55,13 +55,13 @@ private: bool saveFile(SupportedBrowsers browser, const QJsonObject& script); private: - static const QString HOST_NAME; - static const QStringList ALLOWED_EXTENSIONS; - static const QStringList ALLOWED_ORIGINS; - static const QString TARGET_DIR_CHROME; - static const QString TARGET_DIR_CHROMIUM; - static const QString TARGET_DIR_FIREFOX; - static const QString TARGET_DIR_VIVALDI; + const QString HOST_NAME; + const QStringList ALLOWED_EXTENSIONS; + const QStringList ALLOWED_ORIGINS; + const QString TARGET_DIR_CHROME; + const QString TARGET_DIR_CHROMIUM; + const QString TARGET_DIR_FIREFOX; + const QString TARGET_DIR_VIVALDI; }; #endif // HOSTINSTALLER_H diff --git a/src/browser/NativeMessagingHost.cpp b/src/browser/NativeMessagingHost.cpp index 7483b1ca..fc35bbec 100644 --- a/src/browser/NativeMessagingHost.cpp +++ b/src/browser/NativeMessagingHost.cpp @@ -30,14 +30,14 @@ NativeMessagingHost::NativeMessagingHost(DatabaseTabWidget* parent, const bool enabled) : NativeMessagingBase(enabled) , m_mutex(QMutex::Recursive) - , m_browserClients(m_browserService) , m_browserService(parent) + , m_browserClients(m_browserService) { m_localServer.reset(new QLocalServer(this)); m_localServer->setSocketOptions(QLocalServer::UserAccessOption); m_running.store(false); - if (BrowserSettings::isEnabled() && !m_running) { + if (browserSettings()->isEnabled() && !m_running) { run(); } @@ -64,8 +64,8 @@ void NativeMessagingHost::run() } // Update KeePassXC/keepassxc-proxy binary paths to Native Messaging scripts - if (BrowserSettings::updateBinaryPath()) { - BrowserSettings::updateBinaryPaths(BrowserSettings::useCustomProxy() ? BrowserSettings::customProxyLocation() + if (browserSettings()->updateBinaryPath()) { + browserSettings()->updateBinaryPaths(browserSettings()->useCustomProxy() ? browserSettings()->customProxyLocation() : ""); } @@ -75,7 +75,7 @@ void NativeMessagingHost::run() QtConcurrent::run(this, static_cast(&NativeMessagingHost::readNativeMessages)); #endif - if (BrowserSettings::supportBrowserProxy()) { + if (browserSettings()->supportBrowserProxy()) { QString serverPath = getLocalServerPath(); QFile::remove(serverPath); diff --git a/src/browser/NativeMessagingHost.h b/src/browser/NativeMessagingHost.h index d94284ab..fde8f051 100644 --- a/src/browser/NativeMessagingHost.h +++ b/src/browser/NativeMessagingHost.h @@ -58,8 +58,8 @@ private slots: private: QMutex m_mutex; - BrowserClients m_browserClients; BrowserService m_browserService; + BrowserClients m_browserClients; QSharedPointer m_localServer; SocketList m_socketList; }; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index ec5bfa5a..d5342cbc 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -66,7 +66,7 @@ class BrowserPlugin : public ISettingsPage public: BrowserPlugin(DatabaseTabWidget* tabWidget) { - m_nativeMessagingHost = QSharedPointer(new NativeMessagingHost(tabWidget, BrowserSettings::isEnabled())); + m_nativeMessagingHost = QSharedPointer(new NativeMessagingHost(tabWidget, browserSettings()->isEnabled())); } ~BrowserPlugin() @@ -103,7 +103,7 @@ public: void saveSettings(QWidget* widget) override { qobject_cast(widget)->saveSettings(); - if (BrowserSettings::isEnabled()) { + if (browserSettings()->isEnabled()) { m_nativeMessagingHost->run(); } else { m_nativeMessagingHost->stop();