Reduce use of static vars in browser plugin

* Convert BrowserSettings into instanced class
* Moved HostInstaller init into class constructor
This commit is contained in:
Jonathan White 2018-09-05 16:21:59 -04:00
parent 57e60681f2
commit 53a17c2355
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
11 changed files with 209 additions and 203 deletions

View File

@ -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();

View File

@ -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()

View File

@ -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<Entry*> 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<Entry*> BrowserService::searchEntries(const QString& url, const StringPair
{
// Get the list of databases to search
QList<Database*> 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<DatabaseWidget*>(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<Entry*> BrowserService::sortEntries(QList<Entry*>& pwEntries, const QStrin
}
QList<Entry*> 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()) {

View File

@ -107,6 +107,7 @@ private:
DatabaseTabWidget* const m_dbTabWidget;
bool m_dialogActive;
bool m_bringToFrontRequested;
QUuid m_keepassBrowserUUID;
};
#endif // BROWSERSERVICE_H

View File

@ -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()
{

View File

@ -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

View File

@ -18,6 +18,7 @@
#include "HostInstaller.h"
#include "config-keepassx.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
@ -27,35 +28,27 @@
#include <QProcessEnvironment>
#include <QStandardPaths>
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;
}

View File

@ -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

View File

@ -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<void (NativeMessagingHost::*)()>(&NativeMessagingHost::readNativeMessages));
#endif
if (BrowserSettings::supportBrowserProxy()) {
if (browserSettings()->supportBrowserProxy()) {
QString serverPath = getLocalServerPath();
QFile::remove(serverPath);

View File

@ -58,8 +58,8 @@ private slots:
private:
QMutex m_mutex;
BrowserClients m_browserClients;
BrowserService m_browserService;
BrowserClients m_browserClients;
QSharedPointer<QLocalServer> m_localServer;
SocketList m_socketList;
};

View File

@ -66,7 +66,7 @@ class BrowserPlugin : public ISettingsPage
public:
BrowserPlugin(DatabaseTabWidget* tabWidget)
{
m_nativeMessagingHost = QSharedPointer<NativeMessagingHost>(new NativeMessagingHost(tabWidget, BrowserSettings::isEnabled()));
m_nativeMessagingHost = QSharedPointer<NativeMessagingHost>(new NativeMessagingHost(tabWidget, browserSettings()->isEnabled()));
}
~BrowserPlugin()
@ -103,7 +103,7 @@ public:
void saveSettings(QWidget* widget) override
{
qobject_cast<BrowserOptionDialog*>(widget)->saveSettings();
if (BrowserSettings::isEnabled()) {
if (browserSettings()->isEnabled()) {
m_nativeMessagingHost->run();
} else {
m_nativeMessagingHost->stop();