Skip to content

Commit

Permalink
convert inAutoType from boolean block to QMutex
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZ3ro committed Jan 29, 2018
1 parent b991b15 commit 0f42bfb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
35 changes: 16 additions & 19 deletions src/autotype/AutoType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ AutoType* AutoType::m_instance = nullptr;

AutoType::AutoType(QObject* parent, bool test)
: QObject(parent)
, m_inAutoType(false)
, m_autoTypeDelay(0)
, m_currentGlobalKey(static_cast<Qt::Key>(0))
, m_currentGlobalModifiers(0)
Expand Down Expand Up @@ -143,9 +142,7 @@ QStringList AutoType::windowTitles()

void AutoType::resetInAutoType()
{
Q_ASSERT(m_inAutoType);

m_inAutoType = false;
m_inAutoType.unlock();
}

void AutoType::raiseWindow()
Expand Down Expand Up @@ -200,11 +197,6 @@ int AutoType::callEventFilter(void* event)
*/
void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, const QString& sequence, WId window)
{
Q_ASSERT(m_inAutoType);
if (!m_inAutoType) {
return;
}

// no edit to the sequence beyond this point
if (!verifyAutoTypeSyntax(sequence)) {
return;
Expand Down Expand Up @@ -250,7 +242,7 @@ void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, c
*/
void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
{
if (m_inAutoType || !m_plugin) {
if (!m_plugin) {
return;
}

Expand All @@ -259,11 +251,13 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
return;
}

m_inAutoType = true;
if (!m_inAutoType.tryLock()) {
return;
}

executeAutoTypeActions(entry, hideWindow, sequences.first());

m_inAutoType = false;
m_inAutoType.unlock();
}

/**
Expand All @@ -272,7 +266,7 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
*/
void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
{
if (m_inAutoType || !m_plugin) {
if (!m_plugin) {
return;
}

Expand All @@ -282,15 +276,17 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
return;
}

m_inAutoType = true;
if (!m_inAutoType.tryLock()) {
return;
}

QList<AutoTypeMatch> matchList;

for (Database* db : dbList) {
const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive();
for (Entry* entry : dbEntries) {
const QList<QString> sequences = autoTypeSequences(entry, windowTitle);
for (QString sequence : sequences) {
for (const QString& sequence : sequences) {
if (!sequence.isEmpty()) {
matchList << AutoTypeMatch(entry,sequence);
}
Expand All @@ -299,14 +295,14 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
}

if (matchList.isEmpty()) {
m_inAutoType = false;
m_inAutoType.unlock();
QString message = tr("Couldn't find an entry that matches the window title:");
message.append("\n\n");
message.append(windowTitle);
MessageBox::information(nullptr, tr("Auto-Type - KeePassXC"), message);
} else if ((matchList.size() == 1) && !config()->get("security/autotypeask").toBool()) {
executeAutoTypeActions(matchList.first().entry, nullptr, matchList.first().sequence);
m_inAutoType = false;
m_inAutoType.unlock();
} else {
m_windowFromGlobal = m_plugin->activeWindow();
AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog();
Expand All @@ -326,13 +322,14 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)

void AutoType::performAutoTypeFromGlobal(AutoTypeMatch match)
{
Q_ASSERT(m_inAutoType);
// We don't care about the result here, the mutex should already be locked. Now it's locked for sure
m_inAutoType.tryLock();

m_plugin->raiseWindow(m_windowFromGlobal);

executeAutoTypeActions(match.entry, nullptr, match.sequence, m_windowFromGlobal);

m_inAutoType = false;
m_inAutoType.unlock();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/autotype/AutoType.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
Expand All @@ -22,6 +22,7 @@
#include <QObject>
#include <QStringList>
#include <QWidget>
#include <QMutex>

#include "core/AutoTypeMatch.h"

Expand Down Expand Up @@ -84,7 +85,7 @@ private slots:
bool windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl);
bool windowMatches(const QString& windowTitle, const QString& windowPattern);

bool m_inAutoType;
QMutex m_inAutoType;
int m_autoTypeDelay;
Qt::Key m_currentGlobalKey;
Qt::KeyboardModifiers m_currentGlobalModifiers;
Expand Down
1 change: 1 addition & 0 deletions src/autotype/AutoTypeAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <QChar>
#include <Qt>
#include <QObject>

#include "core/Global.h"

Expand Down

0 comments on commit 0f42bfb

Please sign in to comment.