From b4cf98998e579704c453f03897c354e5d01a1f49 Mon Sep 17 00:00:00 2001 From: thez3ro Date: Mon, 29 Jan 2018 19:44:32 +0100 Subject: [PATCH] convert inAutoType from boolean block to QMutex --- src/autotype/AutoType.cpp | 35 ++++++++++++++++------------------- src/autotype/AutoType.h | 5 +++-- src/autotype/AutoTypeAction.h | 1 + 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index 5bd10115a..73bb3ef4b 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -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(0)) , m_currentGlobalModifiers(0) @@ -143,9 +142,7 @@ QStringList AutoType::windowTitles() void AutoType::resetInAutoType() { - Q_ASSERT(m_inAutoType); - - m_inAutoType = false; + m_inAutoType.unlock(); } void AutoType::raiseWindow() @@ -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; @@ -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; } @@ -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(); } /** @@ -272,7 +266,7 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow) */ void AutoType::performGlobalAutoType(const QList& dbList) { - if (m_inAutoType || !m_plugin) { + if (!m_plugin) { return; } @@ -282,7 +276,9 @@ void AutoType::performGlobalAutoType(const QList& dbList) return; } - m_inAutoType = true; + if (!m_inAutoType.tryLock()) { + return; + } QList matchList; @@ -290,7 +286,7 @@ void AutoType::performGlobalAutoType(const QList& dbList) const QList dbEntries = db->rootGroup()->entriesRecursive(); for (Entry* entry : dbEntries) { const QList sequences = autoTypeSequences(entry, windowTitle); - for (QString sequence : sequences) { + for (const QString& sequence : sequences) { if (!sequence.isEmpty()) { matchList << AutoTypeMatch(entry,sequence); } @@ -299,14 +295,14 @@ void AutoType::performGlobalAutoType(const QList& 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(); @@ -326,13 +322,14 @@ void AutoType::performGlobalAutoType(const QList& 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(); } /** diff --git a/src/autotype/AutoType.h b/src/autotype/AutoType.h index db60133ae..3b22106bd 100644 --- a/src/autotype/AutoType.h +++ b/src/autotype/AutoType.h @@ -1,4 +1,4 @@ -/* + /* * Copyright (C) 2012 Felix Geyer * Copyright (C) 2017 KeePassXC Team * @@ -22,6 +22,7 @@ #include #include #include +#include #include "core/AutoTypeMatch.h" @@ -84,7 +85,7 @@ private: 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; diff --git a/src/autotype/AutoTypeAction.h b/src/autotype/AutoTypeAction.h index 490f0d89f..7f0d829c0 100644 --- a/src/autotype/AutoTypeAction.h +++ b/src/autotype/AutoTypeAction.h @@ -20,6 +20,7 @@ #include #include +#include #include "core/Global.h"